[PATCH] D148203: [Assignment Tracking][SelectionDAG] Fix dereferencing nullptr location from dbg.declare

Orlando Cazalet-Hyams via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 13 02:04:24 PDT 2023


Orlando created this revision.
Orlando added reviewers: jmorse, StephenTozer, aeubanks.
Orlando added a project: debug-info.
Herald added subscribers: ecnelises, hiraditya.
Herald added a project: All.
Orlando requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Debug intrinsics sometimes end up with empty metadata location operands. The debug intrinsic interfaces return nullptr when retrieving location operand in this case.

When assignment tracking is not enabled a dbg.declare with a nullptr location operand is skipped. Do the same when assignment tracking is enabled (a nullptr address component of a dbg.assign is already handled correctly.

N.B. my "empty metadata is the same as poison" patch stack (D140902 <https://reviews.llvm.org/D140902>) hasn't landed yet as I'm focussing on assignment tracking for now.


https://reviews.llvm.org/D148203

Files:
  llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
  llvm/test/DebugInfo/assignment-tracking/X86/single-memory-location.ll


Index: llvm/test/DebugInfo/assignment-tracking/X86/single-memory-location.ll
===================================================================
--- llvm/test/DebugInfo/assignment-tracking/X86/single-memory-location.ll
+++ llvm/test/DebugInfo/assignment-tracking/X86/single-memory-location.ll
@@ -2,7 +2,11 @@
 ; RUN: | FileCheck %s
 
 ;; Check that a dbg.assign for a fully stack-homed variable causes the variable
-;; location to appear in the Machine Function side table.
+;; location to appear in the Machine Function side table (variable 'local').
+;;
+;; The variable 'local2' is tracked using a dbg.declare. Check the variable is
+;; dropped and doesn't cause a crash when the address is an empty metadata
+;; tuple and assignment tracking is enabled for the function.
 ;;
 ;; $ cat test.cpp
 ;; void maybe_writes(int*);
@@ -17,10 +21,13 @@
 
 ; CHECK: ![[VAR:[0-9]+]] = !DILocalVariable(name: "local",
 ; CHECK: stack:
-; CHECK-NEXT: - { id: 0, name: local, type: default, offset: 0, size: 4, alignment: 4, 
-; CHECK-NEXT:     stack-id: default, callee-saved-register: '', callee-saved-restored: true, 
-; CHECK-NEXT:     debug-info-variable: '![[VAR]]', debug-info-expression: '!DIExpression()', 
+; CHECK-NEXT: - { id: 0, name: local, type: default, offset: 0, size: 4, alignment: 4,
+; CHECK-NEXT:     stack-id: default, callee-saved-register: '', callee-saved-restored: true,
+; CHECK-NEXT:     debug-info-variable: '![[VAR]]', debug-info-expression: '!DIExpression()',
 ; CHECK-NEXT:     debug-info-location: '!{{.+}}' }
+; CHECK-NEXT: - { id: 1, name: local2, type: default, offset: 0, size: 4, alignment: 4,
+; CHECK-NEXT:     stack-id: default, callee-saved-register: '', callee-saved-restored: true,
+; CHECK-NEXT:     debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
 
 source_filename = "test.cpp"
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
@@ -30,6 +37,8 @@
 entry:
   %local = alloca i32, align 4, !DIAssignID !13
   call void @llvm.dbg.assign(metadata i1 undef, metadata !12, metadata !DIExpression(), metadata !13, metadata ptr %local, metadata !DIExpression()), !dbg !14
+  %local2 = alloca i32, align 4
+  call void @llvm.dbg.declare(metadata !2, metadata !32, metadata !DIExpression()), !dbg !14
   %0 = bitcast ptr %local to ptr, !dbg !15
   call void @llvm.lifetime.start.p0i8(i64 4, ptr nonnull %0), !dbg !15
   call void @_Z12maybe_writesPi(ptr nonnull %local), !dbg !16
@@ -44,6 +53,7 @@
 declare void @llvm.lifetime.start.p0i8(i64 immarg, ptr nocapture)
 declare void @llvm.lifetime.end.p0i8(i64 immarg, ptr nocapture)
 declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)
+declare void @llvm.dbg.declare(metadata, metadata, metadata)
 
 !llvm.dbg.cu = !{!0}
 !llvm.module.flags = !{!3, !4, !5, !1000}
@@ -77,4 +87,5 @@
 !29 = !DISubprogram(name: "ext", linkageName: "_Z3extiiiiiiiiii", scope: !1, file: !1, line: 2, type: !30, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2)
 !30 = !DISubroutineType(types: !31)
 !31 = !{null, !10, !10, !10, !10, !10, !10, !10, !10, !10, !10}
+!32 = !DILocalVariable(name: "local2", scope: !7, file: !1, line: 4, type: !10)
 !1000 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -1345,6 +1345,11 @@
 static void processDbgDeclare(FunctionLoweringInfo &FuncInfo,
                               const Value *Address, DIExpression *Expr,
                               DILocalVariable *Var, DebugLoc DbgLoc) {
+  if (!Address) {
+    LLVM_DEBUG(dbgs() << "processDbgDeclares skipping " << *Var
+                      << " (bad address)\n");
+    return;
+  }
   MachineFunction *MF = FuncInfo.MF;
   const DataLayout &DL = MF->getDataLayout();
 
@@ -1386,13 +1391,7 @@
   for (const BasicBlock &BB : *FuncInfo.Fn) {
     for (const Instruction &I : BB) {
       if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(&I)) {
-        Value *Address = DI->getAddress();
-        if (!Address) {
-          LLVM_DEBUG(dbgs() << "processDbgDeclares skipping " << *DI
-                            << " (bad address)\n");
-          continue;
-        }
-        processDbgDeclare(FuncInfo, Address, DI->getExpression(),
+        processDbgDeclare(FuncInfo, DI->getAddress(), DI->getExpression(),
                           DI->getVariable(), DI->getDebugLoc());
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148203.513104.patch
Type: text/x-patch
Size: 4625 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230413/2a7a15c3/attachment.bin>


More information about the llvm-commits mailing list