[llvm] [RemoveDIs] Handle DPValues in replaceDbgDeclare (PR #73507)

Orlando Cazalet-Hyams via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 12 07:22:40 PST 2023


https://github.com/OCHyams updated https://github.com/llvm/llvm-project/pull/73507

>From c752d00c6abbd5c8b2160dc7d08eb90bb8d684ad Mon Sep 17 00:00:00 2001
From: OCHyams <orlando.hyams at sony.com>
Date: Mon, 27 Nov 2023 11:56:33 +0000
Subject: [PATCH 1/3] [RemoveDIs] Handle DPValues in replaceDbgDeclare

---
 llvm/lib/Transforms/Utils/Local.cpp         | 21 ++++++++++++---------
 llvm/test/DebugInfo/Generic/block-asan.ll   |  1 +
 llvm/test/DebugInfo/X86/asan_debug_info.ll  |  4 ++++
 llvm/test/Transforms/SafeStack/ARM/debug.ll |  3 ++-
 4 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index ff92abb77ac208..17598ab3c28605 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -2117,19 +2117,22 @@ bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
                              DIBuilder &Builder, uint8_t DIExprFlags,
                              int Offset) {
   SmallVector<DbgDeclareInst *, 1> DbgDeclares;
-  findDbgDeclares(DbgDeclares, Address);
-  for (DbgVariableIntrinsic *DII : DbgDeclares) {
-    const DebugLoc &Loc = DII->getDebugLoc();
+  SmallVector<DPValue *, 1> DPValues;
+  findDbgDeclares(DbgDeclares, Address, &DPValues);
+
+  auto ReplaceOne = [&](auto *DII) {
     auto *DIVar = DII->getVariable();
     auto *DIExpr = DII->getExpression();
     assert(DIVar && "Missing variable");
     DIExpr = DIExpression::prepend(DIExpr, DIExprFlags, Offset);
-    // Insert llvm.dbg.declare immediately before DII, and remove old
-    // llvm.dbg.declare.
-    Builder.insertDeclare(NewAddress, DIVar, DIExpr, Loc, DII);
-    DII->eraseFromParent();
-  }
-  return !DbgDeclares.empty();
+    DII->setExpression(DIExpr);
+    DII->replaceVariableLocationOp(Address, NewAddress);
+  };
+
+  for_each(DbgDeclares, ReplaceOne);
+  for_each(DPValues, ReplaceOne);
+
+  return !DbgDeclares.empty() || !DPValues.empty();
 }
 
 static void updateOneDbgValueForAlloca(const DebugLoc &Loc,
diff --git a/llvm/test/DebugInfo/Generic/block-asan.ll b/llvm/test/DebugInfo/Generic/block-asan.ll
index db49289bd6eced..ceaf3f1d5e9a3e 100644
--- a/llvm/test/DebugInfo/Generic/block-asan.ll
+++ b/llvm/test/DebugInfo/Generic/block-asan.ll
@@ -1,4 +1,5 @@
 ; RUN: opt -S -passes=asan %s | FileCheck %s
+; RUN: opt --try-experimental-debuginfo-iterators -S -passes=asan %s | FileCheck %s
 
 ; The IR of this testcase is generated from the following C code:
 ; void bar (int);
diff --git a/llvm/test/DebugInfo/X86/asan_debug_info.ll b/llvm/test/DebugInfo/X86/asan_debug_info.ll
index 5e06b093c41d96..7a0dcdccffc8b7 100644
--- a/llvm/test/DebugInfo/X86/asan_debug_info.ll
+++ b/llvm/test/DebugInfo/X86/asan_debug_info.ll
@@ -2,6 +2,10 @@
 ; RUN:   llc -O0 -filetype=obj - -o - | \
 ; RUN:   llvm-dwarfdump - | FileCheck %s
 
+; RUN: opt --try-experimental-debginfo-iterators < %s -passes=asan -asan-use-after-return=never -S | \
+; RUN:   llc -O0 -filetype=obj - -o - | \
+; RUN:   llvm-dwarfdump - | FileCheck %s
+
 ; For this test case, ASan used to produce IR which resulted in the following
 ; DWARF (at -O0):
 ;
diff --git a/llvm/test/Transforms/SafeStack/ARM/debug.ll b/llvm/test/Transforms/SafeStack/ARM/debug.ll
index c38ee62fa6aa49..c88770684b7f3f 100644
--- a/llvm/test/Transforms/SafeStack/ARM/debug.ll
+++ b/llvm/test/Transforms/SafeStack/ARM/debug.ll
@@ -1,5 +1,6 @@
-; RUN: opt -safe-stack -safestack-use-pointer-address < %s -S | FileCheck %s
 ; RUN: opt -passes=safe-stack -safestack-use-pointer-address < %s -S | FileCheck %s
+; RUN: opt --try-experimental-debuginfo-iterators -passes=safe-stack -safestack-use-pointer-address < %s -S | FileCheck %s
+
 target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
 target triple = "armv7-pc-linux-android"
 

>From 290f4dcbdd6706ccb990b8c30f357a9a8263fa18 Mon Sep 17 00:00:00 2001
From: OCHyams <orlando.hyams at sony.com>
Date: Mon, 27 Nov 2023 12:07:11 +0000
Subject: [PATCH 2/3] fix typo

---
 llvm/test/DebugInfo/X86/asan_debug_info.ll | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/DebugInfo/X86/asan_debug_info.ll b/llvm/test/DebugInfo/X86/asan_debug_info.ll
index 7a0dcdccffc8b7..4a4743ee0df571 100644
--- a/llvm/test/DebugInfo/X86/asan_debug_info.ll
+++ b/llvm/test/DebugInfo/X86/asan_debug_info.ll
@@ -2,7 +2,7 @@
 ; RUN:   llc -O0 -filetype=obj - -o - | \
 ; RUN:   llvm-dwarfdump - | FileCheck %s
 
-; RUN: opt --try-experimental-debginfo-iterators < %s -passes=asan -asan-use-after-return=never -S | \
+; RUN: opt --try-experimental-debuginfo-iterators < %s -passes=asan -asan-use-after-return=never -S | \
 ; RUN:   llc -O0 -filetype=obj - -o - | \
 ; RUN:   llvm-dwarfdump - | FileCheck %s
 

>From 2213724e9ea50367aaa60f22e3b5d7617fc5476e Mon Sep 17 00:00:00 2001
From: OCHyams <orlando.hyams at sony.com>
Date: Tue, 12 Dec 2023 15:21:35 +0000
Subject: [PATCH 3/3] add misssing test run line

---
 llvm/test/Transforms/SafeStack/ARM/debug.ll | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/test/Transforms/SafeStack/ARM/debug.ll b/llvm/test/Transforms/SafeStack/ARM/debug.ll
index c88770684b7f3f..fbdb6361c2f6a3 100644
--- a/llvm/test/Transforms/SafeStack/ARM/debug.ll
+++ b/llvm/test/Transforms/SafeStack/ARM/debug.ll
@@ -1,3 +1,4 @@
+; RUN: opt -safe-stack -safestack-use-pointer-address < %s -S | FileCheck %s
 ; RUN: opt -passes=safe-stack -safestack-use-pointer-address < %s -S | FileCheck %s
 ; RUN: opt --try-experimental-debuginfo-iterators -passes=safe-stack -safestack-use-pointer-address < %s -S | FileCheck %s
 



More information about the llvm-commits mailing list