[llvm] Merge sourcelocation in CSEMIRBuilder::getDominatingInstrForID. (PR #90922)

Shubham Sandeep Rastogi via llvm-commits llvm-commits at lists.llvm.org
Fri May 3 11:04:54 PDT 2024


https://github.com/rastogishubham updated https://github.com/llvm/llvm-project/pull/90922

>From e6fe227c0ccd4d3b5badba6295581afc7e710538 Mon Sep 17 00:00:00 2001
From: Shubham Sandeep Rastogi <srastogi22 at apple.com>
Date: Thu, 2 May 2024 15:17:12 -0700
Subject: [PATCH] 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
---
 .../GlobalISel/LegalizationArtifactCombiner.h |  3 +
 .../CodeGen/GlobalISel/MachineIRBuilder.h     |  4 ++
 llvm/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp | 10 +++
 .../DebugInfo/merge-locations-legalizer.mir   | 62 +++++++++++++++++++
 4 files changed, 79 insertions(+)
 create mode 100644 llvm/test/DebugInfo/merge-locations-legalizer.mir

diff --git a/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h b/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
index 305bef7dd3ea63..b80699adcd33e3 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
@@ -26,6 +26,7 @@
 #include "llvm/CodeGen/Register.h"
 #include "llvm/CodeGen/TargetOpcodes.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/Support/Debug.h"
 
 #define DEBUG_TYPE "legalizer"
@@ -99,6 +100,8 @@ class LegalizationArtifactCombiner {
       const LLT DstTy = MRI.getType(DstReg);
       if (isInstLegal({TargetOpcode::G_CONSTANT, {DstTy}})) {
         auto &CstVal = SrcMI->getOperand(1);
+        Builder.MergedLocation = DILocation::getMergedLocation(
+            MI.getDebugLoc().get(), SrcMI->getDebugLoc().get());
         Builder.buildConstant(
             DstReg, CstVal.getCImm()->getValue().sext(DstTy.getSizeInBits()));
         UpdatedDefs.push_back(DstReg);
diff --git a/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h b/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
index e15f7a7172e1a4..982df4fb400337 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
@@ -243,6 +243,10 @@ class MachineIRBuilder {
   }
 
 public:
+  /// This is the merged location of an instruction which is a result of a fold
+  /// in the legalizer.
+  DILocation *MergedLocation = nullptr;
+
   /// Some constructors for easy use.
   MachineIRBuilder() = default;
   MachineIRBuilder(MachineFunction &MF) { setMF(MF); }
diff --git a/llvm/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp b/llvm/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp
index 551ba1e6036c17..8e27a72c52a3f2 100644
--- a/llvm/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp
@@ -51,6 +51,16 @@ CSEMIRBuilder::getDominatingInstrForID(FoldingSetNodeID &ID,
       // this builder will have the def ready.
       setInsertPt(*CurMBB, std::next(MII));
     } else if (!dominates(MI, CurrPos)) {
+      // MergedLocation represents the merged location of two instructions that
+      // were folded, if it is set, merge the location of MI with the merged
+      // location and set it as MI's new location before moving it to the
+      // CurrPos.
+      if (MergedLocation) {
+        auto *Loc = DILocation::getMergedLocation(MergedLocation,
+                                                  MI->getDebugLoc().get());
+        MI->setDebugLoc(Loc);
+        MergedLocation = nullptr;
+      }
       CurMBB->splice(CurrPos, CurMBB, MI);
     }
     return MachineInstrBuilder(getMF(), MI);
diff --git a/llvm/test/DebugInfo/merge-locations-legalizer.mir b/llvm/test/DebugInfo/merge-locations-legalizer.mir
new file mode 100644
index 00000000000000..b35ea63bdd444b
--- /dev/null
+++ b/llvm/test/DebugInfo/merge-locations-legalizer.mir
@@ -0,0 +1,62 @@
+# RUN: llc %s -O0 --start-before=legalizer --stop-after=legalizer -o - | FileCheck %s 
+# CHECK-NOT: %35:_(s32) = G_CONSTANT i32 0, debug-location !71
+# CHECK: %35:_(s32) = G_CONSTANT i32 0, debug-location !DILocation(line: 0,
+--- |
+  source_filename = "/tmp/main.ll"
+  target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
+  target triple = "arm64-apple-macosx14.0.0"
+  
+  define i32 @main(i32 %0, ptr %1) #0 !dbg !57 {
+  entry:
+    ret i32 0, !dbg !71
+  }
+  !3 = !DIFile(filename: "main.swift", directory: "/Volumes/Data/swift")
+  !23 = distinct !DICompileUnit(language: DW_LANG_Swift, file: !3, sdk: "blah.sdk")
+  !57 = distinct !DISubprogram(name: "main", unit: !23)
+  !64 = distinct !DILexicalBlock(scope: !57, column: 1)
+  !66 = distinct !DILexicalBlock(scope: !64, column: 1)
+  !68 = !DILocation(line: 12, scope: !66)
+  !70 = distinct !DILexicalBlock(scope: !66, column: 1)
+  !71 = !DILocation(line: 13, scope: !70)
+name:            main
+registers:
+  - { id: 0, class: _, preferred-register: '' }
+  - { id: 1, class: _, preferred-register: '' }
+  - { id: 2, class: _, preferred-register: '' }
+  - { id: 3, class: _, preferred-register: '' }
+  - { id: 4, class: _, preferred-register: '' }
+  - { id: 5, class: _, preferred-register: '' }
+  - { id: 6, class: _, preferred-register: '' }
+  - { id: 7, class: _, preferred-register: '' }
+  - { id: 8, class: _, preferred-register: '' }
+  - { id: 9, class: _, preferred-register: '' }
+  - { id: 10, class: _, preferred-register: '' }
+  - { id: 11, class: _, preferred-register: '' }
+  - { id: 12, class: _, preferred-register: '' }
+  - { id: 13, class: _, preferred-register: '' }
+  - { id: 14, class: _, preferred-register: '' }
+  - { id: 15, class: gpr64, preferred-register: '' }
+  - { id: 18, class: _, preferred-register: '' }
+  - { id: 19, class: _, preferred-register: '' }
+  - { id: 20, class: _, preferred-register: '' }
+  - { id: 21, class: _, preferred-register: '' }
+  - { id: 22, class: _, preferred-register: '' }
+  - { id: 23, class: _, preferred-register: '' }
+  - { id: 24, class: _, preferred-register: '' }
+  - { id: 25, class: _, preferred-register: '' }
+  - { id: 26, class: _, preferred-register: '' }
+  - { id: 27, class: _, preferred-register: '' }
+  - { id: 28, class: _, preferred-register: '' }
+  - { id: 29, class: _, preferred-register: '' }
+  - { id: 30, class: _, preferred-register: '' }
+  - { id: 31, class: _, preferred-register: '' }
+  - { id: 32, class: _, preferred-register: '' }
+  - { id: 33, class: _, preferred-register: '' }
+  - { id: 34, class: _, preferred-register: '' }
+body:             |
+  bb.1.entry:
+    %16:_(s8) = G_CONSTANT i8 0, debug-location !68
+    %17:_(s32) = G_ANYEXT %16(s8), debug-location !68
+    $w2 = COPY %17(s32), debug-location !68
+    %35:_(s32) = G_CONSTANT i32 0, debug-location !71
+    $w0 = COPY %35(s32), debug-location !71



More information about the llvm-commits mailing list