[PATCH] D145759: [MachineCombiner] Preserve debug instruction number

Felipe de Azevedo Piovezan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 9 19:36:25 PST 2023


fdeazeve created this revision.
fdeazeve added reviewers: aprantl, jmorse.
Herald added subscribers: pengfei, hiraditya.
Herald added a project: All.
fdeazeve requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Each target's `TargetInstrInfo` is responsible for announcing which code
patterns it is able to transform during the MachineCombiner pass.
Currently, these patterns are applied without preserving the debug
instruction number required by the InstrRef implementation of
LiveDebugValues. As such, we've seen a number of examples where debug
information is dropped for variables in InstrRef mode that were
otherwise available in VarLoc mode. This has been observed both in X86
and AArch examples.

This commit is an initial attempt at preserving said numbers by changing
the general (target agnostic) implementation of TargetInstrInfo: the
reassociation pattern can must the debug number of the "top level"
instruction, i.e., the instruction whose value represents the final
value of the arithmetic expression. Intermediate values must have their
debug number dropped, as they have no equivalent value in the
unoptimized code.

Future work is required to update each target's
`TargetInstrInfo::genAlternativeCodeSequence` method.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145759

Files:
  llvm/lib/CodeGen/TargetInstrInfo.cpp
  llvm/test/CodeGen/X86/machine-combiner-dbg.ll


Index: llvm/test/CodeGen/X86/machine-combiner-dbg.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/X86/machine-combiner-dbg.ll
@@ -0,0 +1,37 @@
+; RUN: llc -mtriple=x86_64-gnu-linux -disable-fix -stop-after=machine-combiner < %s | FileCheck %s
+
+define float @reassoc_me(float %f1, float %f2, float %f3, float %f4) !dbg !9 {
+  %add = fadd reassoc nsz float %f1, %f2, !dbg !21
+  %add1 = fadd reassoc nsz float %add, %f3, !dbg !21
+  %add2 = fadd reassoc nsz float %add1, %f4, !dbg !21
+  call void @llvm.dbg.value(metadata float %add2, metadata !20, metadata !DIExpression()), !dbg !21
+  ret float %add2, !dbg !21
+}
+
+; CHECK: ![[VAR:.*]] = !DILocalVariable(name: "temp3"
+
+; CHECK:      %[[arg3:.*]]:fr32 = COPY $xmm3
+; CHECK-NEXT: %[[arg2:.*]]:fr32 = COPY $xmm2
+; CHECK-NEXT: %[[arg1:.*]]:fr32 = COPY $xmm1
+; CHECK-NEXT: %[[arg0:.*]]:fr32 = COPY $xmm0
+; CHECK-NEXT: %[[add1:.*]]:fr32 = {{.*}} ADDSSrr %[[arg0]], %[[arg1]]
+; CHECK-NEXT: %[[add2:.*]]:fr32 = {{.*}} ADDSSrr %[[arg2]], %[[arg3]]
+; CHECK-NEXT:                            ADDSSrr %[[add1]], killed %[[add2]], {{.*}} debug-instr-number 1
+; CHECK-NEXT: DBG_INSTR_REF ![[VAR]], !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(1, 0)
+
+declare void @llvm.dbg.value(metadata, metadata, metadata)
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "hello", isOptimized: true, emissionKind: FullDebug)
+!1 = !DIFile(filename: "reassoc.c", directory: "/Users/piovezan/workspace/radar_investigations/105791650_instrref/reassoc")
+!2 = !{i32 7, !"Dwarf Version", i32 4}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!9 = distinct !DISubprogram(name: "reassoc_me", scope: !1, file: !1, line: 1, type: !10, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !13)
+!10 = !DISubroutineType(types: !11)
+!11 = !{!12, !12, !12, !12, !12}
+!12 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
+!13 = !{!20}
+!20 = !DILocalVariable(name: "temp3", scope: !9, file: !1, line: 4, type: !12)
+!21 = !DILocation(line: 0, scope: !9)
Index: llvm/lib/CodeGen/TargetInstrInfo.cpp
===================================================================
--- llvm/lib/CodeGen/TargetInstrInfo.cpp
+++ llvm/lib/CodeGen/TargetInstrInfo.cpp
@@ -1017,6 +1017,17 @@
   InsInstrs.push_back(MIB2);
   DelInstrs.push_back(&Prev);
   DelInstrs.push_back(&Root);
+
+  // We transformed:
+  // B = A op X (Prev)
+  // C = B op Y (Root)
+  // Into:
+  // B = X op Y (MIB1)
+  // C = A op B (MIB2)
+  // C has the same value as before, B doesn't; as such, keep the debug number
+  // of C but not of B.
+  if (unsigned OldRootNum = Root.peekDebugInstrNum())
+    MIB2.getInstr()->setDebugInstrNum(OldRootNum);
 }
 
 void TargetInstrInfo::genAlternativeCodeSequence(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145759.504009.patch
Type: text/x-patch
Size: 2935 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230310/4c5da3fd/attachment.bin>


More information about the llvm-commits mailing list