Skip to content

Commit 4a646ca

Browse files
committed
[Instruction] Add updateLocationAfterHoist helper
Introduce a helper on Instruction which can be used to update the debug location after hoisting. Use this in GVN and LICM, where we were mistakenly introducing new line 0 locations after hoisting (the docs recommend dropping the location in this case). For more context, see the discussion in https://reviews.llvm.org/D60913. Differential Revision: https://reviews.llvm.org/D85670
1 parent 479f5bf commit 4a646ca

File tree

8 files changed

+76
-14
lines changed

8 files changed

+76
-14
lines changed

llvm/include/llvm/IR/Instruction.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ class Instruction : public User,
492492
/// merged DebugLoc.
493493
void applyMergedLocation(const DILocation *LocA, const DILocation *LocB);
494494

495+
/// Updates the debug location given that the instruction has been hoisted
496+
/// from a block to a predecessor of that block.
497+
void updateLocationAfterHoist();
498+
495499
private:
496500
/// Return true if we have an entry in the on-the-side metadata hash.
497501
bool hasMetadataHashEntry() const {

llvm/lib/IR/DebugInfo.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,24 @@ void Instruction::applyMergedLocation(const DILocation *LocA,
696696
setDebugLoc(DILocation::getMergedLocation(LocA, LocB));
697697
}
698698

699+
void Instruction::updateLocationAfterHoist() {
700+
const DebugLoc &DL = getDebugLoc();
701+
if (!DL)
702+
return;
703+
704+
// If we didn't hoist a call, drop the location to allow a location from a
705+
// preceding instruction to propagate.
706+
if (!isa<CallBase>(this)) {
707+
setDebugLoc(DebugLoc());
708+
return;
709+
}
710+
711+
// Set a line 0 location for (potentially inlinable) calls. Set the scope to
712+
// the parent function's scope if it's available, and drop any inlinedAt info
713+
// to avoid making it look like the inlined callee was reached early.
714+
setDebugLoc(DebugLoc::get(0, 0, DL.getScope()));
715+
}
716+
699717
//===----------------------------------------------------------------------===//
700718
// LLVM C API implementations.
701719
//===----------------------------------------------------------------------===//

llvm/lib/Transforms/Scalar/GVN.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#include "llvm/IR/Constant.h"
4747
#include "llvm/IR/Constants.h"
4848
#include "llvm/IR/DataLayout.h"
49-
#include "llvm/IR/DebugInfoMetadata.h"
5049
#include "llvm/IR/DebugLoc.h"
5150
#include "llvm/IR/Dominators.h"
5251
#include "llvm/IR/Function.h"
@@ -1314,8 +1313,7 @@ bool GVN::PerformLoadPRE(LoadInst *LI, AvailValInBlkVect &ValuesPerBlock,
13141313
// Instructions that have been inserted in predecessor(s) to materialize
13151314
// the load address do not retain their original debug locations. Doing
13161315
// so could lead to confusing (but correct) source attributions.
1317-
if (const DebugLoc &DL = I->getDebugLoc())
1318-
I->setDebugLoc(DebugLoc::get(0, 0, DL.getScope(), DL.getInlinedAt()));
1316+
I->updateLocationAfterHoist();
13191317

13201318
// FIXME: We really _ought_ to insert these value numbers into their
13211319
// parent's availability map. However, in doing so, we risk getting into

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,10 +1658,7 @@ static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
16581658
// Move the new node to the destination block, before its terminator.
16591659
moveInstructionBefore(I, *Dest->getTerminator(), *SafetyInfo, MSSAU, SE);
16601660

1661-
// Apply line 0 debug locations when we are moving instructions to different
1662-
// basic blocks because we want to avoid jumpy line tables.
1663-
if (const DebugLoc &DL = I.getDebugLoc())
1664-
I.setDebugLoc(DebugLoc::get(0, 0, DL.getScope(), DL.getInlinedAt()));
1661+
I.updateLocationAfterHoist();
16651662

16661663
if (isa<LoadInst>(I))
16671664
++NumMovedLoads;

llvm/test/DebugInfo/Generic/licm-hoist-debug-loc.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
; We make sure that the instruction that is hoisted into the preheader
1919
; does not have a debug location.
2020
; CHECK: for.body.lr.ph:
21-
; CHECK: getelementptr{{.*}}%p.addr, i64 4{{.*}} !dbg [[zero:![0-9]+]]
21+
; CHECK: getelementptr{{.*}}%p.addr, i64 4{{$}}
2222
; CHECK: for.body:
23-
; CHECK: [[zero]] = !DILocation(line: 0
2423
;
2524
; ModuleID = 't.ll'
2625
source_filename = "test.c"

llvm/test/Transforms/GVN/PRE/phi-translate.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ target datalayout = "e-p:64:64:64"
44

55
; CHECK-LABEL: @foo(
66
; CHECK: entry.end_crit_edge:
7-
; CHECK: %[[INDEX:[a-z0-9.]+]] = sext i32 %x to i64{{.*}} !dbg [[ZERO_LOC:![0-9]+]]
8-
; CHECK: %[[ADDRESS:[a-z0-9.]+]] = getelementptr [100 x i32], [100 x i32]* @G, i64 0, i64 %[[INDEX]]{{.*}} !dbg [[ZERO_LOC]]
7+
; CHECK: %[[INDEX:[a-z0-9.]+]] = sext i32 %x to i64{{$}}
8+
; CHECK: %[[ADDRESS:[a-z0-9.]+]] = getelementptr [100 x i32], [100 x i32]* @G, i64 0, i64 %[[INDEX]]{{$}}
99
; CHECK: %n.pre = load i32, i32* %[[ADDRESS]], align 4, !dbg [[N_LOC:![0-9]+]]
1010
; CHECK: br label %end
1111
; CHECK: then:
@@ -14,8 +14,7 @@ target datalayout = "e-p:64:64:64"
1414
; CHECK: %n = phi i32 [ %n.pre, %entry.end_crit_edge ], [ %z, %then ], !dbg [[N_LOC]]
1515
; CHECK: ret i32 %n
1616

17-
; CHECK-DAG: [[N_LOC]] = !DILocation(line: 47, column: 1, scope: !{{.*}})
18-
; CHECK-DAG: [[ZERO_LOC]] = !DILocation(line: 0
17+
; CHECK: [[N_LOC]] = !DILocation(line: 47, column: 1, scope: !{{.*}})
1918

2019
@G = external global [100 x i32]
2120
define i32 @foo(i32 %x, i32 %z) !dbg !6 {

llvm/test/Transforms/LICM/hoisting-preheader-debugloc.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; RUN: opt -S -licm < %s | FileCheck %s
22

3-
; CHECK: %arrayidx4.promoted = load i32, i32* %arrayidx4, align 1, !tbaa !59
3+
; CHECK: %arrayidx4.promoted = load i32, i32* %arrayidx4, align 1, !tbaa !{{[0-9]+$}}
44

55
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
66
target triple = "x86_64-unknown-linux-gnu"

llvm/unittests/IR/InstructionsTest.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,5 +1304,52 @@ TEST(InstructionsTest, UnaryOperator) {
13041304
I->deleteValue();
13051305
}
13061306

1307+
TEST(InstructionsTest, UpdateLocationAfterHoist) {
1308+
LLVMContext C;
1309+
std::unique_ptr<Module> M = parseIR(C,
1310+
R"(
1311+
declare void @callee()
1312+
1313+
define void @f() {
1314+
call void @callee() ; Inst with no location.
1315+
call void @callee(), !dbg !11 ; Call with location.
1316+
ret void, !dbg !11 ; Non-call inst with location.
1317+
}
1318+
1319+
!llvm.dbg.cu = !{!0}
1320+
!llvm.module.flags = !{!3, !4}
1321+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1322+
!1 = !DIFile(filename: "t2.c", directory: "foo")
1323+
!2 = !{}
1324+
!3 = !{i32 2, !"Dwarf Version", i32 4}
1325+
!4 = !{i32 2, !"Debug Info Version", i32 3}
1326+
!8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2)
1327+
!9 = !DISubroutineType(types: !10)
1328+
!10 = !{null}
1329+
!11 = !DILocation(line: 2, column: 7, scope: !8)
1330+
)");
1331+
ASSERT_TRUE(M);
1332+
Function *F = cast<Function>(M->getNamedValue("f"));
1333+
BasicBlock &BB = F->front();
1334+
1335+
auto *I1 = BB.getFirstNonPHI();
1336+
auto *I2 = I1->getNextNode();
1337+
auto *I3 = BB.getTerminator();
1338+
1339+
EXPECT_FALSE(bool(I1->getDebugLoc()));
1340+
I1->updateLocationAfterHoist();
1341+
EXPECT_FALSE(bool(I1->getDebugLoc()));
1342+
1343+
const MDNode *Scope = I2->getDebugLoc().getScope();
1344+
EXPECT_EQ(I2->getDebugLoc().getLine(), 2U);
1345+
I2->updateLocationAfterHoist();
1346+
EXPECT_EQ(I2->getDebugLoc().getLine(), 0U);
1347+
EXPECT_EQ(I2->getDebugLoc().getScope(), Scope);
1348+
1349+
EXPECT_EQ(I3->getDebugLoc().getLine(), 2U);
1350+
I3->updateLocationAfterHoist();
1351+
EXPECT_FALSE(bool(I3->getDebugLoc()));
1352+
}
1353+
13071354
} // end anonymous namespace
13081355
} // end namespace llvm

0 commit comments

Comments
 (0)