Skip to content

Commit 2d4fc05

Browse files
Merge sourcelocation in CSEMIRBuilder::getDominatingInstrForID.
Make sure to merge the sourcelocation of the Dominating Instruction that is hoisted in a basic block in the CSEMIRBuilder in the legalizer pass. If this is not done, we can have a incorrect line table entry that makes the instruction pointer jump around. For example the line table without this patch looks like: Address Line Column File ISA Discriminator OpIndex Flags ------------------ ------ ------ ------ --- ------------- ------- ------------- 0x0000000000000000 0 0 1 0 0 0 is_stmt 0x0000000000000010 11 14 1 0 0 0 is_stmt prologue_end 0x0000000000000028 12 1 1 0 0 0 is_stmt 0x000000000000002c 12 15 1 0 0 0 0x000000000000004c 12 13 1 0 0 0 0x000000000000005c 13 1 1 0 0 0 is_stmt 0x0000000000000064 12 13 1 0 0 0 is_stmt 0x000000000000007c 13 7 1 0 0 0 is_stmt 0x00000000000000c8 13 1 1 0 0 0 0x00000000000000e8 13 1 1 0 0 0 epilogue_begin 0x00000000000000f8 13 1 1 0 0 0 end_sequence The line table entry for 0x000000000000005c should be 0
1 parent 37277d8 commit 2d4fc05

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/CodeGen/Register.h"
2727
#include "llvm/CodeGen/TargetOpcodes.h"
2828
#include "llvm/IR/Constants.h"
29+
#include "llvm/IR/DebugInfoMetadata.h"
2930
#include "llvm/Support/Debug.h"
3031

3132
#define DEBUG_TYPE "legalizer"
@@ -99,6 +100,8 @@ class LegalizationArtifactCombiner {
99100
const LLT DstTy = MRI.getType(DstReg);
100101
if (isInstLegal({TargetOpcode::G_CONSTANT, {DstTy}})) {
101102
auto &CstVal = SrcMI->getOperand(1);
103+
Builder.MergedLocation = DILocation::getMergedLocation(
104+
MI.getDebugLoc().get(), SrcMI->getDebugLoc().get());
102105
Builder.buildConstant(
103106
DstReg, CstVal.getCImm()->getValue().sext(DstTy.getSizeInBits()));
104107
UpdatedDefs.push_back(DstReg);

llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ class MachineIRBuilder {
243243
}
244244

245245
public:
246+
/// This is the merged location of an instruction which is a result of a fold
247+
/// in the legalizer.
248+
DILocation *MergedLocation = nullptr;
249+
246250
/// Some constructors for easy use.
247251
MachineIRBuilder() = default;
248252
MachineIRBuilder(MachineFunction &MF) { setMF(MF); }

llvm/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ CSEMIRBuilder::getDominatingInstrForID(FoldingSetNodeID &ID,
5151
// this builder will have the def ready.
5252
setInsertPt(*CurMBB, std::next(MII));
5353
} else if (!dominates(MI, CurrPos)) {
54+
// MergedLocation represents the merged location of two instructions that
55+
// were folded, if it is set, merge the location of MI with the merged
56+
// location and set it as MI's new location before moving it to the
57+
// CurrPos.
58+
if (MergedLocation) {
59+
auto *Loc = DILocation::getMergedLocation(MergedLocation,
60+
MI->getDebugLoc().get());
61+
MI->setDebugLoc(Loc);
62+
MergedLocation = nullptr;
63+
}
5464
CurMBB->splice(CurrPos, CurMBB, MI);
5565
}
5666
return MachineInstrBuilder(getMF(), MI);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# This test checks to make sure that when an instruction (%35 in the test) is
2+
# moved due to matching a result of a fold of two other instructions
3+
# (%16, and %17 in the test) in the legalizer, the DILocation of the
4+
# instruction that is moved (%35) is updated appropriately.
5+
6+
# RUN: llc %s -O0 --start-before=legalizer --stop-after=legalizer -o - | FileCheck %s
7+
# CHECK-NOT: %35:_(s32) = G_CONSTANT i32 0, debug-location !71
8+
# CHECK: %35:_(s32) = G_CONSTANT i32 0, debug-location !DILocation(line: 0,
9+
--- |
10+
source_filename = "/tmp/main.ll"
11+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
12+
target triple = "arm64-apple-macosx14.0.0"
13+
14+
define i32 @main(i32 %0, ptr %1) #0 !dbg !57 {
15+
entry:
16+
ret i32 0, !dbg !71
17+
}
18+
!3 = !DIFile(filename: "main.swift", directory: "/Volumes/Data/swift")
19+
!23 = distinct !DICompileUnit(language: DW_LANG_Swift, file: !3, sdk: "blah.sdk")
20+
!57 = distinct !DISubprogram(name: "main", unit: !23)
21+
!64 = distinct !DILexicalBlock(scope: !57, column: 1)
22+
!66 = distinct !DILexicalBlock(scope: !64, column: 1)
23+
!68 = !DILocation(line: 12, scope: !66)
24+
!70 = distinct !DILexicalBlock(scope: !66, column: 1)
25+
!71 = !DILocation(line: 13, scope: !70)
26+
name: main
27+
registers:
28+
- { id: 0, class: _, preferred-register: '' }
29+
- { id: 1, class: _, preferred-register: '' }
30+
- { id: 2, class: _, preferred-register: '' }
31+
- { id: 3, class: _, preferred-register: '' }
32+
- { id: 4, class: _, preferred-register: '' }
33+
- { id: 5, class: _, preferred-register: '' }
34+
- { id: 6, class: _, preferred-register: '' }
35+
- { id: 7, class: _, preferred-register: '' }
36+
- { id: 8, class: _, preferred-register: '' }
37+
- { id: 9, class: _, preferred-register: '' }
38+
- { id: 10, class: _, preferred-register: '' }
39+
- { id: 11, class: _, preferred-register: '' }
40+
- { id: 12, class: _, preferred-register: '' }
41+
- { id: 13, class: _, preferred-register: '' }
42+
- { id: 14, class: _, preferred-register: '' }
43+
- { id: 15, class: gpr64, preferred-register: '' }
44+
- { id: 18, class: _, preferred-register: '' }
45+
- { id: 19, class: _, preferred-register: '' }
46+
- { id: 20, class: _, preferred-register: '' }
47+
- { id: 21, class: _, preferred-register: '' }
48+
- { id: 22, class: _, preferred-register: '' }
49+
- { id: 23, class: _, preferred-register: '' }
50+
- { id: 24, class: _, preferred-register: '' }
51+
- { id: 25, class: _, preferred-register: '' }
52+
- { id: 26, class: _, preferred-register: '' }
53+
- { id: 27, class: _, preferred-register: '' }
54+
- { id: 28, class: _, preferred-register: '' }
55+
- { id: 29, class: _, preferred-register: '' }
56+
- { id: 30, class: _, preferred-register: '' }
57+
- { id: 31, class: _, preferred-register: '' }
58+
- { id: 32, class: _, preferred-register: '' }
59+
- { id: 33, class: _, preferred-register: '' }
60+
- { id: 34, class: _, preferred-register: '' }
61+
body: |
62+
bb.1.entry:
63+
%16:_(s8) = G_CONSTANT i8 0, debug-location !68
64+
%17:_(s32) = G_ANYEXT %16(s8), debug-location !68
65+
$w2 = COPY %17(s32), debug-location !68
66+
%35:_(s32) = G_CONSTANT i32 0, debug-location !71
67+
$w0 = COPY %35(s32), debug-location !71

0 commit comments

Comments
 (0)