Skip to content

[FastISel][DebugInfo] Handle dbg.value targeting allocas #67187

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 1 commit into from
Oct 2, 2023

Conversation

felipepiovezan
Copy link
Contributor

FastISel currently drops dbg.values targeting allocas. It may seem surprising that a simple case would fail to be lowered, but dbg.values targeting allocas are not common; we usually have dbg.declares doing that, and those are handled by the common code between FastISel and SelectionDAGISel.

This patch addresses the issue by querying the static alloca map from FuncInfo. If we have a frame index for it, we create a DBG_VALUE intrinsic from it.

@felipepiovezan felipepiovezan requested review from adrian-prantl and a team September 22, 2023 19:47
@llvmbot llvmbot added backend:X86 llvm:SelectionDAG SelectionDAGISel as well labels Sep 22, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 22, 2023

@llvm/pr-subscribers-debuginfo
@llvm/pr-subscribers-llvm-selectiondag

@llvm/pr-subscribers-backend-x86

Changes

FastISel currently drops dbg.values targeting allocas. It may seem surprising that a simple case would fail to be lowered, but dbg.values targeting allocas are not common; we usually have dbg.declares doing that, and those are handled by the common code between FastISel and SelectionDAGISel.

This patch addresses the issue by querying the static alloca map from FuncInfo. If we have a frame index for it, we create a DBG_VALUE intrinsic from it.


Full diff: https://github.com/llvm/llvm-project/pull/67187.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/FastISel.cpp (+8)
  • (added) llvm/test/CodeGen/X86/fast-isel-dbg-value-alloca.ll (+32)
diff --git a/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp b/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
index f0affce7b6b805c..a831295863399b5 100644
--- a/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
@@ -1327,6 +1327,14 @@ bool FastISel::selectIntrinsicCall(const IntrinsicInst *II) {
                         << *DI << "\n");
       return true;
     }
+    if (auto SI = FuncInfo.StaticAllocaMap.find(dyn_cast<AllocaInst>(V));
+        SI != FuncInfo.StaticAllocaMap.end()) {
+      MachineOperand FrameIndexOp = MachineOperand::CreateFI(SI->second);
+      bool IsIndirect = false;
+      BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD.getDL(), II, IsIndirect,
+              FrameIndexOp, Var, Expr);
+      return true;
+    }
     if (Register Reg = lookUpRegForValue(V)) {
       // FIXME: This does not handle register-indirect values at offset 0.
       if (!FuncInfo.MF->useDebugInstrRef()) {
diff --git a/llvm/test/CodeGen/X86/fast-isel-dbg-value-alloca.ll b/llvm/test/CodeGen/X86/fast-isel-dbg-value-alloca.ll
new file mode 100644
index 000000000000000..77f883ee9bd3a73
--- /dev/null
+++ b/llvm/test/CodeGen/X86/fast-isel-dbg-value-alloca.ll
@@ -0,0 +1,32 @@
+; RUN: llc -fast-isel -fast-isel-abort=1 -mtriple=x86_64-unknown-unknown -stop-after=finalize-isel %s -o - | \
+; RUN:    FileCheck %s
+
+define void @foo(ptr noalias nocapture %arg) !dbg !38 {
+  %k.debug = alloca ptr, align 8
+  store ptr %arg, ptr %k.debug, align 8, !dbg !70
+  call void @llvm.dbg.value(metadata ptr %k.debug, metadata !55, metadata !DIExpression(DW_OP_deref)), !dbg !70
+; CHECK: call void @llvm.dbg.value(metadata ptr %{{.*}}, metadata ![[VAR:.*]], metadata ![[EXPR:.*]])
+; CHECK: DBG_VALUE %stack.0{{.*}}, $noreg, ![[VAR]], ![[EXPR]]
+  ret void, !dbg !70
+}
+
+declare void @llvm.dbg.value(metadata, metadata, metadata)
+
+!llvm.module.flags = !{!6, !7, !8, !9}
+!llvm.dbg.cu = !{!16}
+
+!6 = !{i32 7, !"Dwarf Version", i32 4}
+!7 = !{i32 2, !"Debug Info Version", i32 3}
+!8 = !{i32 1, !"wchar_size", i32 4}
+!9 = !{i32 8, !"PIC Level", i32 2}
+!16 = distinct !DICompileUnit(language: DW_LANG_Swift, file: !17, producer: "blah", isOptimized: false, runtimeVersion: 5, emissionKind: FullDebug, sysroot: "blah", sdk: "blah")
+!17 = !DIFile(filename: "blah", directory: "blah")
+!38 = distinct !DISubprogram(name: "blah", linkageName: "$blah", scope: !17, file: !17, line: 34, type: !39, scopeLine: 34, spFlags: DISPFlagDefinition, unit: !16, retainedNodes: !43)
+!39 = !DISubroutineType(types: !40)
+!40 = !{!41, !41}
+!41 = !DICompositeType(tag: DW_TAG_structure_type, name: "blah")
+!43 = !{!49, !55}
+!49 = !DILocalVariable(name: "x", arg: 1, scope: !38, file: !17, line: 34, type: !41)
+!55 = !DILocalVariable(name: "k", scope: !56, file: !17, line: 36, type: !41)
+!56 = distinct !DILexicalBlock(scope: !38, file: !17, line: 36, column: 9)
+!70 = !DILocation(line: 36, column: 9, scope: !56)

FastISel currently drops dbg.values targeting allocas. It may seem surprising
that a simple case would fail to be lowered, but dbg.values targeting allocas
are not common; we usually have dbg.declares doing that, and those are handled
by the common code between FastISel and SelectionDAGISel.

This patch addresses the issue by querying the static alloca map from FuncInfo.
If we have a frame index for it, we create a DBG_VALUE intrinsic from it.

One X86 test now generates more debug information and an extra redundant label
that we had to account for.
@felipepiovezan
Copy link
Contributor Author

felipepiovezan commented Sep 25, 2023

Updated a failing test

@felipepiovezan felipepiovezan merged commit a41ce98 into llvm:main Oct 2, 2023
@felipepiovezan felipepiovezan deleted the felipe/fastiseldbgvalue branch October 2, 2023 14:10
felipepiovezan added a commit to felipepiovezan/llvm-project that referenced this pull request Oct 3, 2023
FastISel currently drops dbg.values targeting allocas. It may seem
surprising that a simple case would fail to be lowered, but dbg.values
targeting allocas are not common; we usually have dbg.declares doing
that, and those are handled by the common code between FastISel and
SelectionDAGISel.

This patch addresses the issue by querying the static alloca map from
FuncInfo. If we have a frame index for it, we create a DBG_VALUE
intrinsic from it.

Cherry-picked from  a41ce98
felipepiovezan added a commit to felipepiovezan/llvm-project that referenced this pull request Oct 3, 2023
The underlying issue has since been fixed by
llvm#67187
felipepiovezan added a commit to swiftlang/llvm-project that referenced this pull request Oct 3, 2023
…alue-fix

[cherry-pick] [FastISel][DebugInfo] Handle dbg.value targeting allocas (llvm#67187)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants