Index: include/llvm/IR/IRBuilder.h =================================================================== --- include/llvm/IR/IRBuilder.h +++ include/llvm/IR/IRBuilder.h @@ -32,6 +32,7 @@ #include "llvm/IR/Instruction.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Intrinsics.h" +#include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/Operator.h" @@ -133,8 +134,10 @@ void SetInsertPoint(Instruction *I) { BB = I->getParent(); InsertPt = I->getIterator(); - assert(InsertPt != BB->end() && "Can't read debug loc from end()"); - SetCurrentDebugLocation(I->getDebugLoc()); + assert(I->getIterator() != BB->end() && "Can't read debug loc from end()"); + // Avoid taking DebugLocs from debug intrinsics + auto DbgSrcInst = skipDebugIntrinsics(InsertPt); + SetCurrentDebugLocation(DbgSrcInst->getDebugLoc()); } /// This specifies that created instructions should be inserted at the @@ -142,8 +145,11 @@ void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) { BB = TheBB; InsertPt = IP; - if (IP != TheBB->end()) - SetCurrentDebugLocation(IP->getDebugLoc()); + if (IP != TheBB->end()) { + // Avoid taking DebugLocs from debug intrinsics + auto DbgSrcInst = skipDebugIntrinsics(InsertPt); + SetCurrentDebugLocation(DbgSrcInst->getDebugLoc()); + } } /// Set location information used by debugging information. Index: test/CodeGen/AMDGPU/llvm.dbg.value.ll =================================================================== --- test/CodeGen/AMDGPU/llvm.dbg.value.ll +++ test/CodeGen/AMDGPU/llvm.dbg.value.ll @@ -12,7 +12,7 @@ define amdgpu_kernel void @test_debug_value(i32 addrspace(1)* nocapture %globalptr_arg) #0 !dbg !4 { entry: tail call void @llvm.dbg.value(metadata i32 addrspace(1)* %globalptr_arg, metadata !10, metadata !13), !dbg !14 - store i32 123, i32 addrspace(1)* %globalptr_arg, align 4 + store i32 123, i32 addrspace(1)* %globalptr_arg, align 4, !dbg !14 ret void } @@ -27,7 +27,7 @@ define amdgpu_kernel void @only_undef_dbg_value() #1 { bb: call void @llvm.dbg.value(metadata <4 x float> undef, metadata !10, metadata !DIExpression(DW_OP_constu, 1, DW_OP_swap, DW_OP_xderef)) #2, !dbg !14 - ret void + ret void, !dbg !14 } declare void @llvm.dbg.value(metadata, metadata, metadata) #1 Index: unittests/IR/IRBuilderTest.cpp =================================================================== --- unittests/IR/IRBuilderTest.cpp +++ unittests/IR/IRBuilderTest.cpp @@ -722,4 +722,50 @@ EXPECT_EQ(MN2, MF2->getRawElements()); EXPECT_TRUE(verifyModule(*M)); } + +// Test that IRBuilder won't read DebugLocs from debug intrinsics at the +// insertion location. +TEST_F(IRBuilderTest, InsertionDebugLoc) { + IRBuilder<> Builder(BB); + Value *V; + Instruction *I; + + // Create debug environment for test. + DIBuilder DIB(*M); + auto File = DIB.createFile("bees", "/"); + auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "hands", true, "", 0); + auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None)); + auto Func = DIB.createFunction( + CU, "shoe", "", File, 1, Type, 1, DINode::FlagZero, + DISubprogram::SPFlagDefinition); + auto Lex = DIB.createLexicalBlock(Func, File, 1, 1); + auto DIFloat = DIB.createBasicType("float", 32, dwarf::DW_ATE_float); + auto DIExpr = DIB.createExpression(); + auto LocalVar = DIB.createAutoVariable(Lex, "sandal", File, 2, DIFloat); + + // Create our "Real code" (TM) debug location, on line 10. + DebugLoc NormalDebugLoc = DebugLoc::get(10, 1, Lex); + // Create a debug location specially for a dbg.value we'l create, on line 2. + auto DbgValLoc = DILocation::get(Ctx, 2, 3, Lex); + + // Create some floating point operations with a dbg.value in the middle. + Builder.SetCurrentDebugLocation(NormalDebugLoc); + V = Builder.CreateLoad(GV->getValueType(), GV); + I = cast(Builder.CreateFAdd(V, V)); + auto DbgVal = DIB.insertDbgValueIntrinsic(I, LocalVar, DIExpr, DbgValLoc, BB); + Builder.CreateStore(I, GV); + Builder.CreateRetVoid(); + + // Now insert an extra instruction at the location of the dbg.value. + // IRBuilder should seek and find the "Real" debug location, and ignore the + // location of the intervening dbg.value. + Builder.SetInsertPoint(DbgVal); + Instruction *I2 = cast(Builder.CreateFAdd(I, V)); + + // Additionally created FAdd should have the normal debug location. + EXPECT_FALSE(I2->getDebugLoc().get() == DbgValLoc); + EXPECT_TRUE(I2->getDebugLoc() == NormalDebugLoc); + + DIB.finalize(); +} }