Skip to content

Commit a9763de

Browse files
Merge sourcelocation in CSEMIRBuilder::getDominatingInstrForID. (#90922)
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 After this patch, the line table 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 0 0 1 0 0 0 0x0000000000000064 12 13 1 0 0 0 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 ```
1 parent fa750f0 commit a9763de

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

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

Lines changed: 6 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,11 @@ class LegalizationArtifactCombiner {
99100
const LLT DstTy = MRI.getType(DstReg);
100101
if (isInstLegal({TargetOpcode::G_CONSTANT, {DstTy}})) {
101102
auto &CstVal = SrcMI->getOperand(1);
103+
auto *MergedLocation = DILocation::getMergedLocation(
104+
MI.getDebugLoc().get(), SrcMI->getDebugLoc().get());
105+
// Set the debug location to the merged location of the SrcMI and the MI
106+
// if the aext fold is successful.
107+
Builder.setDebugLoc(MergedLocation);
102108
Builder.buildConstant(
103109
DstReg, CstVal.getCImm()->getValue().sext(DstTy.getSizeInBits()));
104110
UpdatedDefs.push_back(DstReg);

llvm/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ 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+
// Update the spliced machineinstr's debug location by merging it with the
55+
// debug location of the instruction at the insertion point.
56+
auto *Loc = DILocation::getMergedLocation(getDebugLoc().get(),
57+
MI->getDebugLoc().get());
58+
MI->setDebugLoc(Loc);
5459
CurMBB->splice(CurrPos, CurMBB, MI);
5560
}
5661
return MachineInstrBuilder(getMF(), MI);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This test checks to make sure that when an instruction (%3 in the test) is
2+
# moved due to matching a result of a fold of two other instructions
3+
# (%1, and %2 in the test) in the legalizer, the DILocation of the
4+
# instruction that is moved (%3) is updated appropriately.
5+
6+
# RUN: llc %s -run-pass=legalizer -mtriple=aarch64 -o - | FileCheck %s
7+
# CHECK-NOT: %2:_(s32) = G_CONSTANT i32 0, debug-location !DILocation(line: 13
8+
# CHECK: %2:_(s32) = G_CONSTANT i32 0, debug-location !DILocation(line: 0,
9+
--- |
10+
11+
define i32 @main(i32 %0, ptr %1) #0 !dbg !57 {
12+
entry:
13+
ret i32 0, !dbg !71
14+
}
15+
!3 = !DIFile(filename: "main.swift", directory: "/Volumes/Data/swift")
16+
!23 = distinct !DICompileUnit(language: DW_LANG_Swift, file: !3, sdk: "blah.sdk")
17+
!57 = distinct !DISubprogram(name: "main", unit: !23)
18+
!64 = distinct !DILexicalBlock(scope: !57, column: 1)
19+
!66 = distinct !DILexicalBlock(scope: !64, column: 1)
20+
!68 = !DILocation(line: 12, scope: !66)
21+
!70 = distinct !DILexicalBlock(scope: !66, column: 1)
22+
!71 = !DILocation(line: 13, scope: !70)
23+
name: main
24+
body: |
25+
bb.0:
26+
%1:_(s8) = G_CONSTANT i8 0, debug-location !68
27+
%2:_(s32) = G_ANYEXT %1(s8), debug-location !68
28+
$w2 = COPY %2(s32), debug-location !68
29+
%3:_(s32) = G_CONSTANT i32 0, debug-location !71
30+
$w0 = COPY %3(s32), debug-location !71

0 commit comments

Comments
 (0)