Skip to content

Commit

Permalink
[Assignment Tracking][14/*] Account for assignment tracking in instco…
Browse files Browse the repository at this point in the history
…mbine

The Assignment Tracking debug-info feature is outlined in this RFC:

https://discourse.llvm.org/t/
rfc-assignment-tracking-a-better-way-of-specifying-variable-locations-in-ir

Most of the updates here are just to ensure DIAssignID attachments are
maintained and propagated correctly.

Reviewed By: jmorse

Differential Revision: https://reviews.llvm.org/D133307
  • Loading branch information
OCHyams committed Nov 18, 2022
1 parent 9e088ef commit fcd5098
Show file tree
Hide file tree
Showing 10 changed files with 725 additions and 3 deletions.
14 changes: 11 additions & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Expand Up @@ -34,6 +34,7 @@
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
Expand Down Expand Up @@ -223,6 +224,7 @@ Instruction *InstCombinerImpl::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) {
S->setMetadata(LLVMContext::MD_mem_parallel_loop_access, LoopMemParallelMD);
if (AccessGroupMD)
S->setMetadata(LLVMContext::MD_access_group, AccessGroupMD);
S->copyMetadata(*MI, LLVMContext::MD_DIAssignID);

if (auto *MT = dyn_cast<MemTransferInst>(MI)) {
// non-atomics can be volatile
Expand Down Expand Up @@ -294,9 +296,15 @@ Instruction *InstCombinerImpl::SimplifyAnyMemSet(AnyMemSetInst *MI) {
Dest = Builder.CreateBitCast(Dest, NewDstPtrTy);

// Extract the fill value and store.
uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
StoreInst *S = Builder.CreateStore(ConstantInt::get(ITy, Fill), Dest,
MI->isVolatile());
const uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
Constant *FillVal = ConstantInt::get(ITy, Fill);
StoreInst *S = Builder.CreateStore(FillVal, Dest, MI->isVolatile());
S->copyMetadata(*MI, LLVMContext::MD_DIAssignID);
for (auto *DAI : at::getAssignmentMarkers(S)) {
if (any_of(DAI->location_ops(), [&](Value *V) { return V == FillC; }))
DAI->replaceVariableLocationOp(FillC, FillVal);
}

S->setAlignment(Alignment);
if (isa<AtomicMemSetInst>(MI))
S->setOrdering(AtomicOrdering::Unordered);
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Expand Up @@ -14,6 +14,7 @@
#include "llvm/ADT/SetVector.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/KnownBits.h"
#include "llvm/Transforms/InstCombine/InstCombiner.h"
Expand Down Expand Up @@ -163,6 +164,8 @@ Instruction *InstCombinerImpl::PromoteCastOfAllocation(BitCastInst &CI,
New->setAlignment(AI.getAlign());
New->takeName(&AI);
New->setUsedWithInAlloca(AI.isUsedWithInAlloca());
New->setMetadata(LLVMContext::MD_DIAssignID,
AI.getMetadata(LLVMContext::MD_DIAssignID));

// 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
Expand Down
Expand Up @@ -511,6 +511,7 @@ static StoreInst *combineStoreToNewValue(InstCombinerImpl &IC, StoreInst &SI,
// here.
switch (ID) {
case LLVMContext::MD_dbg:
case LLVMContext::MD_DIAssignID:
case LLVMContext::MD_tbaa:
case LLVMContext::MD_prof:
case LLVMContext::MD_fpmath:
Expand Down Expand Up @@ -1554,6 +1555,7 @@ bool InstCombinerImpl::mergeStoreIntoSuccessor(StoreInst &SI) {
SI.getOrdering(), SI.getSyncScopeID());
InsertNewInstBefore(NewSI, *BBI);
NewSI->setDebugLoc(MergedLoc);
NewSI->mergeDIAssignID({&SI, OtherStore});

// If the two stores had AA tags, merge them.
AAMDNodes AATags = SI.getAAMetadata();
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Expand Up @@ -4163,6 +4163,11 @@ static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock,
if (!SunkVariables.insert(DbgUserVariable).second)
continue;

// Leave dbg.assign intrinsics in their original positions and there should
// be no need to insert a clone.
if (isa<DbgAssignIntrinsic>(User))
continue;

DIIClones.emplace_back(cast<DbgVariableIntrinsic>(User->clone()));
if (isa<DbgDeclareInst>(User) && isa<CastInst>(I))
DIIClones.back()->replaceVariableLocationOp(I, I->getOperand(0));
Expand Down
@@ -0,0 +1,74 @@
; RUN: opt -passes=instcombine -S %s -o - -experimental-assignment-tracking \
; RUN: | FileCheck %s

;; NOTE: This test uses typed pointers because it is testing a code path that
;; doesn't get exercised with opaque pointers. If/when PromoteCastOfAllocation
;; is removed from visitBitCast this test should just be deleted.

;; Check that allocas generated in InstCombine's PromoteCastOfAllocation
;; have DIAssignID copied from the original alloca.
;;
;; $ cat reduce.cpp
;; struct c {
;; c(int);
;; int a, b;
;; };
;; c d() {
;; c e(1);
;; return e;
;; }
;; $ clang -O2 -c -g reduce.cpp -fno-inline -Xclang -disable-llvm-passes -emit-llvm -S \
;; | opt -passes=declare-to-assign -S

; 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: ![[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"

%struct.c = type { i32, i32 }

define dso_local i64 @_Z1dv() !dbg !7 {
entry:
%retval = alloca %struct.c, align 4, !DIAssignID !21
call void @llvm.dbg.assign(metadata i1 undef, metadata !20, metadata !DIExpression(), metadata !21, metadata %struct.c* %retval, metadata !DIExpression()), !dbg !22
call void @_ZN1cC1Ei(%struct.c* %retval, i32 1), !dbg !23
%0 = bitcast %struct.c* %retval to i64*, !dbg !24
%1 = load i64, i64* %0, align 4, !dbg !24
ret i64 %1, !dbg !24
}

declare dso_local void @_ZN1cC1Ei(%struct.c*, i32) unnamed_addr
declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4, !5}
!llvm.ident = !{!6}

!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 12.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "reduce.cpp", directory: "/")
!2 = !{}
!3 = !{i32 7, !"Dwarf Version", i32 4}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = !{i32 1, !"wchar_size", i32 4}
!6 = !{!"clang version 12.0.0"}
!7 = distinct !DISubprogram(name: "d", linkageName: "_Z1dv", scope: !1, file: !1, line: 5, type: !8, scopeLine: 5, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !19)
!8 = !DISubroutineType(types: !9)
!9 = !{!10}
!10 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "c", file: !1, line: 1, size: 64, flags: DIFlagTypePassByValue | DIFlagNonTrivial, elements: !11, identifier: "_ZTS1c")
!11 = !{!12, !14, !15}
!12 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !10, file: !1, line: 3, baseType: !13, size: 32)
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!14 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !10, file: !1, line: 3, baseType: !13, size: 32, offset: 32)
!15 = !DISubprogram(name: "c", scope: !10, file: !1, line: 2, type: !16, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)
!16 = !DISubroutineType(types: !17)
!17 = !{null, !18, !13}
!18 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer)
!19 = !{!20}
!20 = !DILocalVariable(name: "e", scope: !7, file: !1, line: 6, type: !10)
!21 = distinct !DIAssignID()
!22 = !DILocation(line: 0, scope: !7)
!23 = !DILocation(line: 6, column: 5, scope: !7)
!24 = !DILocation(line: 7, column: 3, scope: !7)
@@ -0,0 +1,78 @@
; RUN: opt %s -S -passes=instcombine -o - -experimental-assignment-tracking \
; RUN: | FileCheck %s

;; $ cat test.cpp
;; void esc(int*);
;; void fun() {
;; int local[5];
;; __builtin_memset(local, 8, 4 * 4);
;; __builtin_memset(local, 0, 2 * 4);
;; esc(local);
;; }
;; IR grabbed before instcombine in:
;; clang++ -O2 -g -Xclang -fexperimental-assignment-tracking

;; Instcombine is going to turn the second memset into a store. Check that it
;; inherits the DIAssignID from the memset and that the dbg.assign's value
;; component is correct.

; CHECK: store i64 0, ptr %local, align 16{{.*}}, !DIAssignID ![[ID:[0-9]+]]
; CHECK-NEXT: call void @llvm.dbg.assign(metadata i64 0, metadata !{{.*}}, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 64), metadata ![[ID]], metadata ptr %local, metadata !DIExpression())

define dso_local void @_Z3funv() local_unnamed_addr #0 !dbg !7 {
entry:
%local = alloca [5 x i32], align 16, !DIAssignID !16
call void @llvm.dbg.assign(metadata i1 undef, metadata !11, metadata !DIExpression(), metadata !16, metadata ptr %local, metadata !DIExpression()), !dbg !17
%0 = bitcast ptr %local to ptr, !dbg !18
call void @llvm.lifetime.start.p0i8(i64 20, ptr %0) #5, !dbg !18
%arraydecay = getelementptr inbounds [5 x i32], ptr %local, i64 0, i64 0, !dbg !19
%1 = bitcast ptr %arraydecay to ptr, !dbg !19
call void @llvm.memset.p0i8.i64(ptr align 16 %1, i8 8, i64 16, i1 false), !dbg !19, !DIAssignID !20
call void @llvm.dbg.assign(metadata i8 8, metadata !11, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 128), metadata !20, metadata ptr %1, metadata !DIExpression()), !dbg !17
call void @llvm.memset.p0i8.i64(ptr align 16 %1, i8 0, i64 8, i1 false), !dbg !21, !DIAssignID !22
call void @llvm.dbg.assign(metadata i8 0, metadata !11, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 64), metadata !22, metadata ptr %1, metadata !DIExpression()), !dbg !17
call void @_Z3escPi(ptr noundef %arraydecay), !dbg !23
call void @llvm.lifetime.end.p0i8(i64 20, ptr %0) #5, !dbg !24
ret void, !dbg !24
}

declare void @llvm.lifetime.start.p0i8(i64 immarg, ptr nocapture)
declare void @llvm.memset.p0i8.i64(ptr nocapture writeonly, i8, i64, i1 immarg)
declare !dbg !25 dso_local void @_Z3escPi(ptr noundef) local_unnamed_addr
declare void @llvm.lifetime.end.p0i8(i64 immarg, ptr nocapture)
declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!2, !3, !4, !5}
!llvm.ident = !{!6}

!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 14.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "test.cpp", directory: "/")
!2 = !{i32 7, !"Dwarf Version", i32 5}
!3 = !{i32 2, !"Debug Info Version", i32 3}
!4 = !{i32 1, !"wchar_size", i32 4}
!5 = !{i32 7, !"uwtable", i32 1}
!6 = !{!"clang version 14.0.0"}
!7 = distinct !DISubprogram(name: "fun", linkageName: "_Z3funv", scope: !1, file: !1, line: 2, type: !8, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !10)
!8 = !DISubroutineType(types: !9)
!9 = !{null}
!10 = !{!11}
!11 = !DILocalVariable(name: "local", scope: !7, file: !1, line: 3, type: !12)
!12 = !DICompositeType(tag: DW_TAG_array_type, baseType: !13, size: 160, elements: !14)
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!14 = !{!15}
!15 = !DISubrange(count: 5)
!16 = distinct !DIAssignID()
!17 = !DILocation(line: 0, scope: !7)
!18 = !DILocation(line: 3, column: 3, scope: !7)
!19 = !DILocation(line: 4, column: 3, scope: !7)
!20 = distinct !DIAssignID()
!21 = !DILocation(line: 5, column: 3, scope: !7)
!22 = distinct !DIAssignID()
!23 = !DILocation(line: 6, column: 3, scope: !7)
!24 = !DILocation(line: 7, column: 1, scope: !7)
!25 = !DISubprogram(name: "esc", linkageName: "_Z3escPi", scope: !1, file: !1, line: 1, type: !26, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !29)
!26 = !DISubroutineType(types: !27)
!27 = !{null, !28}
!28 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64)
!29 = !{}
@@ -0,0 +1,108 @@
; RUN: opt %s -S -passes=instcombine -experimental-assignment-tracking | FileCheck %s

;; Check that instcombine merges the DIAssignID metadata when merging two
;; stores into a successor. Filecheck directives inline.
;;
;; Generated from the following source:
;; int c;
;; void esc(int*);
;; int get();
;; void fun() {
;; int local;
;; if (c) {
;; get();
;; local = 2;
;; } else {
;; local = 2;
;; }
;; esc(&local);
;; }

; CHECK: if.then:
; CHECK-NEXT: %call = call
; CHECK-NEXT: call void @llvm.dbg.assign(metadata i32 2, metadata ![[LOCAL:[0-9]+]], metadata !DIExpression(), metadata ![[MERGED_ID:[0-9]+]], metadata ptr %local, metadata !DIExpression()), !dbg

; CHECK: if.else:
; CHECK-NEXT: call void @llvm.dbg.assign(metadata i32 2, metadata ![[LOCAL]], metadata !DIExpression(), metadata ![[MERGED_ID]], metadata ptr %local, metadata !DIExpression()), !dbg

; CHECK: if.end:
; CHECK-NEXT: store i32 2, ptr %local{{.*}}!DIAssignID ![[MERGED_ID]]

; CHECK: ![[LOCAL]] = !DILocalVariable(name: "local",

@c = dso_local local_unnamed_addr global i32 0, align 4, !dbg !0

; Function Attrs: uwtable mustprogress
define dso_local void @_Z3funv() local_unnamed_addr !dbg !11 {
entry:
%local = alloca i32, align 4
%0 = bitcast ptr %local to ptr, !dbg !16
call void @llvm.lifetime.start.p0i8(i64 4, ptr %0), !dbg !16
%1 = load i32, ptr @c, align 4, !dbg !17
%tobool = icmp ne i32 %1, 0, !dbg !17
br i1 %tobool, label %if.then, label %if.else, !dbg !23

if.then: ; preds = %entry
%call = call i32 @_Z3getv(), !dbg !24
store i32 2, ptr %local, align 4, !dbg !26, !DIAssignID !27
call void @llvm.dbg.assign(metadata i32 2, metadata !15, metadata !DIExpression(), metadata !27, metadata ptr %local, metadata !DIExpression()), !dbg !26
br label %if.end, !dbg !28

if.else: ; preds = %entry
store i32 2, ptr %local, align 4, !dbg !29, !DIAssignID !31
call void @llvm.dbg.assign(metadata i32 2, metadata !15, metadata !DIExpression(), metadata !31, metadata ptr %local, metadata !DIExpression()), !dbg !29
br label %if.end

if.end: ; preds = %if.else, %if.then
call void @_Z3escPi(ptr %local), !dbg !32
call void @llvm.lifetime.end.p0i8(i64 4, ptr %0), !dbg !33
ret void, !dbg !33
}

declare !dbg !34 dso_local i32 @_Z3getv() local_unnamed_addr
declare !dbg !37 dso_local void @_Z3escPi(ptr) local_unnamed_addr
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)

!llvm.dbg.cu = !{!2}
!llvm.module.flags = !{!7, !8, !9}
!llvm.ident = !{!10}

!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "c", scope: !2, file: !3, line: 1, type: !6, isLocal: false, isDefinition: true)
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, producer: "clang version 12.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None)
!3 = !DIFile(filename: "test.cpp", directory: "/")
!4 = !{}
!5 = !{!0}
!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!7 = !{i32 7, !"Dwarf Version", i32 4}
!8 = !{i32 2, !"Debug Info Version", i32 3}
!9 = !{i32 1, !"wchar_size", i32 4}
!10 = !{!"clang version 12.0.0"}
!11 = distinct !DISubprogram(name: "fun", linkageName: "_Z3funv", scope: !3, file: !3, line: 4, type: !12, scopeLine: 4, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !14)
!12 = !DISubroutineType(types: !13)
!13 = !{null}
!14 = !{!15}
!15 = !DILocalVariable(name: "local", scope: !11, file: !3, line: 5, type: !6)
!16 = !DILocation(line: 5, column: 3, scope: !11)
!17 = !DILocation(line: 6, column: 7, scope: !18)
!18 = distinct !DILexicalBlock(scope: !11, file: !3, line: 6, column: 7)
!23 = !DILocation(line: 6, column: 7, scope: !11)
!24 = !DILocation(line: 7, column: 5, scope: !25)
!25 = distinct !DILexicalBlock(scope: !18, file: !3, line: 6, column: 10)
!26 = !DILocation(line: 8, column: 11, scope: !25)
!27 = distinct !DIAssignID()
!28 = !DILocation(line: 9, column: 3, scope: !25)
!29 = !DILocation(line: 10, column: 11, scope: !30)
!30 = distinct !DILexicalBlock(scope: !18, file: !3, line: 9, column: 10)
!31 = distinct !DIAssignID()
!32 = !DILocation(line: 12, column: 3, scope: !11)
!33 = !DILocation(line: 13, column: 1, scope: !11)
!34 = !DISubprogram(name: "get", linkageName: "_Z3getv", scope: !3, file: !3, line: 3, type: !35, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !4)
!35 = !DISubroutineType(types: !36)
!36 = !{!6}
!37 = !DISubprogram(name: "esc", linkageName: "_Z3escPi", scope: !3, file: !3, line: 2, type: !38, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !4)
!38 = !DISubroutineType(types: !39)
!39 = !{null, !40}
!40 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6, size: 64)

0 comments on commit fcd5098

Please sign in to comment.