Skip to content

Merge sourcelocation in CSEMIRBuilder::getDominatingInstrForID. (#90922) #8765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/Register.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/Support/Debug.h"

#define DEBUG_TYPE "legalizer"
Expand Down Expand Up @@ -96,6 +97,11 @@ class LegalizationArtifactCombiner {
const LLT DstTy = MRI.getType(DstReg);
if (isInstLegal({TargetOpcode::G_CONSTANT, {DstTy}})) {
auto &CstVal = SrcMI->getOperand(1);
auto *MergedLocation = DILocation::getMergedLocation(
MI.getDebugLoc().get(), SrcMI->getDebugLoc().get());
// Set the debug location to the merged location of the SrcMI and the MI
// if the aext fold is successful.
Builder.setDebugLoc(MergedLocation);
Builder.buildConstant(
DstReg, CstVal.getCImm()->getValue().sext(DstTy.getSizeInBits()));
UpdatedDefs.push_back(DstReg);
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ CSEMIRBuilder::getDominatingInstrForID(FoldingSetNodeID &ID,
// this builder will have the def ready.
setInsertPt(*CurMBB, std::next(MII));
} else if (!dominates(MI, CurrPos)) {
// Update the spliced machineinstr's debug location by merging it with the
// debug location of the instruction at the insertion point.
auto *Loc = DILocation::getMergedLocation(getDebugLoc().get(),
MI->getDebugLoc().get());
MI->setDebugLoc(Loc);
CurMBB->splice(CurrPos, CurMBB, MI);
}
return MachineInstrBuilder(getMF(), MI);
Expand Down
30 changes: 30 additions & 0 deletions llvm/test/DebugInfo/AArch64/merge-locations-legalizer.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This test checks to make sure that when an instruction (%3 in the test) is
# moved due to matching a result of a fold of two other instructions
# (%1, and %2 in the test) in the legalizer, the DILocation of the
# instruction that is moved (%3) is updated appropriately.

# RUN: llc %s -run-pass=legalizer -mtriple=aarch64 -o - | FileCheck %s
# CHECK-NOT: %2:_(s32) = G_CONSTANT i32 0, debug-location !DILocation(line: 13
# CHECK: %2:_(s32) = G_CONSTANT i32 0, debug-location !DILocation(line: 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
body: |
bb.0:
%1:_(s8) = G_CONSTANT i8 0, debug-location !68
%2:_(s32) = G_ANYEXT %1(s8), debug-location !68
$w2 = COPY %2(s32), debug-location !68
%3:_(s32) = G_CONSTANT i32 0, debug-location !71
$w0 = COPY %3(s32), debug-location !71