[llvm] 5a1d92f - [InstCombine] Update debug intrinsics when rewriting allocas

Matthias Gehre via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 24 23:21:01 PST 2022


Author: Matthias Gehre
Date: 2022-11-25T08:20:54+01:00
New Revision: 5a1d92fa3ed2744007d353f2c1e86c5ec2fc6927

URL: https://github.com/llvm/llvm-project/commit/5a1d92fa3ed2744007d353f2c1e86c5ec2fc6927
DIFF: https://github.com/llvm/llvm-project/commit/5a1d92fa3ed2744007d353f2c1e86c5ec2fc6927.diff

LOG: [InstCombine] Update debug intrinsics when rewriting allocas

Added: 
    llvm/test/Transforms/InstCombine/dbg-cast-of-allocation.ll
    llvm/test/Transforms/InstCombine/dbg-simplify-alloca-size.ll

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
    llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
    llvm/test/DebugInfo/Generic/assignment-tracking/instcombine/alloca-bitcast.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 6ec4fec4b8c39..d6f6dd5512699 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -167,6 +167,8 @@ Instruction *InstCombinerImpl::PromoteCastOfAllocation(BitCastInst &CI,
   New->setMetadata(LLVMContext::MD_DIAssignID,
                    AI.getMetadata(LLVMContext::MD_DIAssignID));
 
+  replaceAllDbgUsesWith(AI, *New, *New, DT);
+
   // If the allocation has multiple real uses, insert a cast and change all
   // things that used it to use the new cast.  This will also hack on CI, but it
   // will die soon.

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index 3ce62384f4374..d38e856cd4d68 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -169,7 +169,7 @@ static bool isDereferenceableForAllocaSize(const Value *V, const AllocaInst *AI,
 }
 
 static Instruction *simplifyAllocaArraySize(InstCombinerImpl &IC,
-                                            AllocaInst &AI) {
+                                            AllocaInst &AI, DominatorTree &DT) {
   // Check for array size of 1 (scalar allocation).
   if (!AI.isArrayAllocation()) {
     // i32 1 is the canonical array size for scalar allocations.
@@ -188,6 +188,8 @@ static Instruction *simplifyAllocaArraySize(InstCombinerImpl &IC,
                                                 nullptr, AI.getName());
       New->setAlignment(AI.getAlign());
 
+      replaceAllDbgUsesWith(AI, *New, *New, DT);
+
       // Scan to the end of the allocation instructions, to skip over a block of
       // allocas if possible...also skip interleaved debug info
       //
@@ -356,7 +358,7 @@ void PointerReplacer::replacePointer(Instruction &I, Value *V) {
 }
 
 Instruction *InstCombinerImpl::visitAllocaInst(AllocaInst &AI) {
-  if (auto *I = simplifyAllocaArraySize(*this, AI))
+  if (auto *I = simplifyAllocaArraySize(*this, AI, DT))
     return I;
 
   if (AI.getAllocatedType()->isSized()) {

diff  --git a/llvm/test/DebugInfo/Generic/assignment-tracking/instcombine/alloca-bitcast.ll b/llvm/test/DebugInfo/Generic/assignment-tracking/instcombine/alloca-bitcast.ll
index 468e287ebf160..7d3cd785ec005 100644
--- a/llvm/test/DebugInfo/Generic/assignment-tracking/instcombine/alloca-bitcast.ll
+++ b/llvm/test/DebugInfo/Generic/assignment-tracking/instcombine/alloca-bitcast.ll
@@ -23,7 +23,7 @@
 ; CHECK: entry:
 ; CHECK-NEXT: %retval = alloca i64, align 8, !DIAssignID ![[ID:[0-9]+]]
 ; CHECK-NEXT: %tmpcast = bitcast i64* %retval to %struct.c*
-; CHECK-NEXT: call void @llvm.dbg.assign(metadata i1 undef, metadata ![[e:[0-9]+]], metadata !DIExpression(), metadata ![[ID]], metadata %struct.c* %tmpcast, metadata !DIExpression()), !dbg
+; CHECK-NEXT: call void @llvm.dbg.assign(metadata i1 undef, metadata ![[e:[0-9]+]], metadata !DIExpression(), metadata ![[ID]], metadata i64* %retval, metadata !DIExpression()), !dbg
 ; CHECK: ![[e]] = !DILocalVariable(name: "e",
 
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

diff  --git a/llvm/test/Transforms/InstCombine/dbg-cast-of-allocation.ll b/llvm/test/Transforms/InstCombine/dbg-cast-of-allocation.ll
new file mode 100644
index 0000000000000..4089f493f41ee
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/dbg-cast-of-allocation.ll
@@ -0,0 +1,42 @@
+; RUN: opt -S --passes=instcombine %s | FileCheck %s
+
+; https://github.com/llvm/llvm-project/issues/56807
+%si16 = type { i8, i8 }
+
+declare void @foo(i16* %pixels)
+
+declare void @llvm.dbg.declare(metadata, metadata, metadata)
+
+; CHECK-LABEL: @toplevel(
+; CHECK:  entry:
+; CHECK-NEXT:    %pixels = alloca i16
+; CHECK-NEXT:    call void @llvm.dbg.declare(metadata i16* %pixels, metadata ![[MD:[0-9]+]], metadata !DIExpression()), !dbg ![[DBG:[0-9]+]]
+; CHECK-NEXT:    call void @foo(i16* nonnull %pixels)
+; CHECK-NEXT:    ret void
+define dso_local void @toplevel() {
+entry:
+  %pixels = alloca %si16
+  call void @llvm.dbg.declare(metadata %si16* %pixels, metadata !11, metadata !DIExpression()), !dbg !12
+  %arraydecay = bitcast %si16* %pixels to i16*
+  call void @foo(i16* nonnull %arraydecay)
+  ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4, !5}
+
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 11.1.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "test.cpp", directory: "/path/to/test_cpp")
+!2 = !{}
+!3 = !{i32 7, !"Dwarf Version", i32 4}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = !{i32 1, !"wchar_size", i32 4}
+!7 = distinct !DISubprogram(name: "toplevel", linkageName: "_Z8toplevelv", scope: !1, file: !1, line: 9, type: !8, scopeLine: 9, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
+!8 = !DISubroutineType(types: !9)
+!9 = !{!10, !10}
+!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+; CHECK: ![[MD]] = !DILocalVariable(name: "pixels"
+!11 = !DILocalVariable(name: "pixels", arg: 1, scope: !7, file: !1, line: 9, type: !10)
+; CHECK: ![[DBG]] = !DILocation(line: 9, column: 16,
+!12 = !DILocation(line: 9, column: 16, scope: !7)

diff  --git a/llvm/test/Transforms/InstCombine/dbg-simplify-alloca-size.ll b/llvm/test/Transforms/InstCombine/dbg-simplify-alloca-size.ll
new file mode 100644
index 0000000000000..acbed65dcafc3
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/dbg-simplify-alloca-size.ll
@@ -0,0 +1,40 @@
+; RUN: opt -S --passes=instcombine %s | FileCheck %s
+
+; https://github.com/llvm/llvm-project/issues/56807
+declare void @foo(i8* %pixels)
+
+declare void @llvm.dbg.declare(metadata, metadata, metadata)
+
+; CHECK-LABEL: @toplevel(
+; CHECK:  entry:
+; CHECK-NEXT:    %pixels1 = alloca [3 x i8], align 1
+; CHECK-NEXT:    call void @llvm.dbg.declare(metadata [3 x i8]* %pixels1, metadata ![[MD:[0-9]+]], metadata !DIExpression()), !dbg ![[DBG:[0-9]+]]
+; CHECK-NEXT:    %pixels1.sub = getelementptr inbounds [3 x i8], [3 x i8]* %pixels1, i64 0, i64 0
+; CHECK-NEXT:    call void @foo(i8* nonnull %pixels1.sub)
+; CHECK-NEXT:    ret void
+define dso_local void @toplevel() {
+entry:
+  %pixels = alloca i8, i32 3
+  call void @llvm.dbg.declare(metadata i8* %pixels, metadata !11, metadata !DIExpression()), !dbg !12
+  call void @foo(i8* %pixels)
+  ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4, !5}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 11.1.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "test.cpp", directory: "/path/to/test_cpp")
+!2 = !{}
+!3 = !{i32 7, !"Dwarf Version", i32 4}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = !{i32 1, !"wchar_size", i32 4}
+!7 = distinct !DISubprogram(name: "toplevel", linkageName: "_Z8toplevelv", scope: !1, file: !1, line: 9, type: !8, scopeLine: 9, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
+!8 = !DISubroutineType(types: !9)
+!9 = !{!10, !10}
+!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+; CHECK: ![[MD]] = !DILocalVariable(name: "pixels"
+!11 = !DILocalVariable(name: "pixels", arg: 1, scope: !7, file: !1, line: 9, type: !10)
+; CHECK: ![[DBG]] = !DILocation(line: 9, column: 16,
+!12 = !DILocation(line: 9, column: 16, scope: !7)
+


        


More information about the llvm-commits mailing list