[llvm] [DEBUGINFO] Propagate debug metadata for sext SDNode. (PR #135971)

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 16 07:34:53 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-debuginfo

Author: Alexander Peskov (apeskov)

<details>
<summary>Changes</summary>

In some cases of chained `sext` operators the debug metadata can be missed. This patch propagates proper metadata to resulting node.

Particular case of issue is NVPTX codegen for function with bool local variable:
```
void test(int i) {
  bool xyz = i == 0;
  foo(i);
}
```

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


2 Files Affected:

- (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (+3-1) 
- (added) llvm/test/DebugInfo/NVPTX/debug-bool-var.ll (+43) 


``````````diff
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 8682c40898046..c5825a64c1c18 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -6244,7 +6244,9 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
       SDNodeFlags Flags;
       if (OpOpcode == ISD::ZERO_EXTEND)
         Flags.setNonNeg(N1->getFlags().hasNonNeg());
-      return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
+      SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
+      transferDbgValues(N1, NewVal);
+      return NewVal;
     }
     if (N1.isUndef())
       // sext(undef) = 0, because the top bits will all be the same.
diff --git a/llvm/test/DebugInfo/NVPTX/debug-bool-var.ll b/llvm/test/DebugInfo/NVPTX/debug-bool-var.ll
new file mode 100644
index 0000000000000..7e50f1ea18fea
--- /dev/null
+++ b/llvm/test/DebugInfo/NVPTX/debug-bool-var.ll
@@ -0,0 +1,43 @@
+; RUN: llc < %s -mtriple=nvptx64-nvidia-cuda | FileCheck %s
+
+declare void @foo(i32)
+
+define void @test1(i32 noundef %gid) !dbg !3 {
+entry:
+  ;
+  ; verify that debug info exists for "xyz" variable
+  ;
+  ; CHECK-LABEL:    DW_TAG_variable
+  ; CHECK:      .b8 120     // DW_AT_name
+  ; CHECK-NEXT: .b8 121
+  ; CHECK-NEXT: .b8 122
+  ; CHECK-NEXT: .b8 0
+  ; CHECK-NEXT: .b8 1       // DW_AT_decl_file
+  ; CHECK-NEXT: .b8 6       // DW_AT_decl_line
+  ;
+  %cmp = icmp eq i32 %gid, 0, !dbg !12
+  %conv = zext i1 %cmp to i32, !dbg !12
+  %conv1 = trunc i32 %conv to i8, !dbg !12
+    #dbg_value(i8 %conv1, !10, !DIExpression(), !13)
+  %conv3 = sext i8 %conv1 to i32
+  call void @foo(i32 %conv3)
+  ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "test.cu", directory: "/source/dir")
+!2 = !{i32 1, !"Debug Info Version", i32 3}
+!3 = distinct !DISubprogram(name: "test1", linkageName: "_test1i", scope: !1, file: !1, line: 5, type: !4, scopeLine: 5, unit: !0, retainedNodes: !8)
+!4 = !DISubroutineType(types: !5)
+!5 = !{!6, !7}
+!6 = !DIBasicType(tag: DW_TAG_unspecified_type, name: "void")
+!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!8 = !{}
+!9 = distinct !DILexicalBlock(scope: !3, file: !1, line: 5, column: 30)
+!10 = !DILocalVariable(name: "xyz", scope: !9, file: !1, line: 6, type: !11)
+!11 = !DIBasicType(name: "bool", size: 8, encoding: DW_ATE_boolean)
+!12 = !DILocation(line: 1, column: 3, scope: !9)
+!13 = !DILocation(line: 2, scope: !9)

``````````

</details>


https://github.com/llvm/llvm-project/pull/135971


More information about the llvm-commits mailing list