diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp index 166521a276643..f9ded507f8328 100644 --- a/llvm/lib/IR/DebugInfo.cpp +++ b/llvm/lib/IR/DebugInfo.cpp @@ -375,6 +375,38 @@ bool DebugInfoFinder::addScope(DIScope *Scope) { return true; } +/// Recursively handle DILocations in followup metadata etc. +/// +/// TODO: If for example a followup loop metadata would refence itself this +/// function would go into infinite recursion. We do not expect such cycles in +/// the loop metadata (except for the self-referencing first element +/// "LoopID"). However, we could at least handle such situations more gracefully +/// somehow (e.g. by keeping track of visited nodes and dropping metadata). +static Metadata *updateLoopMetadataDebugLocationsRecursive( + Metadata *MetadataIn, function_ref Updater) { + const MDTuple *M = dyn_cast_or_null(MetadataIn); + // The loop metadata options should start with a MDString. + if (!M || M->getNumOperands() < 1 || !isa(M->getOperand(0))) + return MetadataIn; + + bool Updated = false; + SmallVector MDs{M->getOperand(0)}; + for (Metadata *MD : llvm::drop_begin(M->operands())) { + if (!MD) { + MDs.push_back(nullptr); + continue; + } + Metadata *NewMD = + Updater(updateLoopMetadataDebugLocationsRecursive(MD, Updater)); + if (NewMD) + MDs.push_back(NewMD); + Updated |= NewMD != MD; + } + + assert(!M->isDistinct() && "M should not be distinct."); + return Updated ? MDNode::get(M->getContext(), MDs) : MetadataIn; +} + static MDNode *updateLoopMetadataDebugLocationsImpl( MDNode *OrigLoopID, function_ref Updater) { assert(OrigLoopID && OrigLoopID->getNumOperands() > 0 && @@ -385,11 +417,11 @@ static MDNode *updateLoopMetadataDebugLocationsImpl( // Save space for the self-referential LoopID. SmallVector MDs = {nullptr}; - for (unsigned i = 1; i < OrigLoopID->getNumOperands(); ++i) { - Metadata *MD = OrigLoopID->getOperand(i); + for (Metadata *MD : llvm::drop_begin(OrigLoopID->operands())) { if (!MD) MDs.push_back(nullptr); - else if (Metadata *NewMD = Updater(MD)) + else if (Metadata *NewMD = Updater( + updateLoopMetadataDebugLocationsRecursive(MD, Updater))) MDs.push_back(NewMD); } diff --git a/llvm/test/Transforms/Inline/dilocation-loop-metadata-update.ll b/llvm/test/Transforms/Inline/dilocation-loop-metadata-update.ll new file mode 100644 index 0000000000000..1bc132663331b --- /dev/null +++ b/llvm/test/Transforms/Inline/dilocation-loop-metadata-update.ll @@ -0,0 +1,83 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 +; RUN: opt < %s -passes=inline -S | FileCheck %s + +; When inlining we need to update DILocation recursively for the followup +; metadata when updating llvm.loop metadata. + +define void @a() !dbg !3 { +; CHECK-LABEL: define void @a( +; CHECK-SAME: ) !dbg [[DBG3:![0-9]+]] { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: br label %[[FOR_BODY:.*]] +; CHECK: [[FOR_BODY]]: +; CHECK-NEXT: br label %[[FOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]] +; +entry: + br label %for.body + +for.body: ; preds = %for.body, %entry + br label %for.body, !llvm.loop !6 +} + +define void @f() !dbg !17 { +; CHECK-LABEL: define void @f( +; CHECK-SAME: ) !dbg [[DBG17:![0-9]+]] { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: br label %[[A_EXIT:.*]] +; CHECK: [[A_EXIT]]: +; CHECK-NEXT: br label %[[A_EXIT]], !llvm.loop [[LOOP18:![0-9]+]] +; CHECK: [[A_EXIT1:.*:]] +; CHECK-NEXT: ret void +; +entry: + call void @a(), !dbg !18 + ret void +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!2} + +!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "foo.c", directory: "/") +!2 = !{i32 2, !"Debug Info Version", i32 3} +!3 = distinct !DISubprogram(name: "a", scope: !1, file: !1, line: 3, type: !4, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0) +!4 = !DISubroutineType(types: !5) +!5 = !{} +!6 = distinct !{!6, !7, !8, !9, !10, !11} +!7 = !DILocation(line: 6, column: 3, scope: !3) +!8 = !DILocation(line: 7, column: 22, scope: !3) +!9 = !{!"llvm.loop.mustprogress"} +!10 = !{!"llvm.loop.distribute.enable", i1 true} +!11 = !{!"llvm.loop.distribute.followup_all", !7, !8, !9, !12, !13, !14} +!12 = !{!"llvm.loop.vectorize.width", i32 8} +!13 = !{!"llvm.loop.vectorize.enable", i1 true} +!14 = !{!"llvm.loop.vectorize.followup_all", !7, !8, !9, !15, !16} +!15 = !{!"llvm.loop.isvectorized"} +!16 = !{!"llvm.loop.unroll.count", i32 1} +!17 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 9, type: !4, scopeLine: 9, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0) +!18 = !DILocation(line: 9, column: 12, scope: !17) +;. +; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug, splitDebugInlining: false, nameTableKind: None) +; CHECK: [[META1]] = !DIFile(filename: "{{.*}}foo.c", directory: {{.*}}) +; CHECK: [[DBG3]] = distinct !DISubprogram(name: "a", scope: [[META1]], file: [[META1]], line: 3, type: [[META4:![0-9]+]], scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]]) +; CHECK: [[META4]] = !DISubroutineType(types: [[META5:![0-9]+]]) +; CHECK: [[META5]] = !{} +; CHECK: [[LOOP6]] = distinct !{[[LOOP6]], [[META7:![0-9]+]], [[META8:![0-9]+]], [[META9:![0-9]+]], [[META10:![0-9]+]], [[META11:![0-9]+]]} +; CHECK: [[META7]] = !DILocation(line: 6, column: 3, scope: [[DBG3]]) +; CHECK: [[META8]] = !DILocation(line: 7, column: 22, scope: [[DBG3]]) +; CHECK: [[META9]] = !{!"llvm.loop.mustprogress"} +; CHECK: [[META10]] = !{!"llvm.loop.distribute.enable", i1 true} +; CHECK: [[META11]] = !{!"llvm.loop.distribute.followup_all", [[META7]], [[META8]], [[META9]], [[META12:![0-9]+]], [[META13:![0-9]+]], [[META14:![0-9]+]]} +; CHECK: [[META12]] = !{!"llvm.loop.vectorize.width", i32 8} +; CHECK: [[META13]] = !{!"llvm.loop.vectorize.enable", i1 true} +; CHECK: [[META14]] = !{!"llvm.loop.vectorize.followup_all", [[META7]], [[META8]], [[META9]], [[META15:![0-9]+]], [[META16:![0-9]+]]} +; CHECK: [[META15]] = !{!"llvm.loop.isvectorized"} +; CHECK: [[META16]] = !{!"llvm.loop.unroll.count", i32 1} +; CHECK: [[DBG17]] = distinct !DISubprogram(name: "f", scope: [[META1]], file: [[META1]], line: 9, type: [[META4]], scopeLine: 9, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]]) +; CHECK: [[LOOP18]] = distinct !{[[LOOP18]], [[META19:![0-9]+]], [[META21:![0-9]+]], [[META9]], [[META10]], [[META22:![0-9]+]]} +; CHECK: [[META19]] = !DILocation(line: 6, column: 3, scope: [[DBG3]], inlinedAt: [[META20:![0-9]+]]) +; CHECK: [[META20]] = distinct !DILocation(line: 9, column: 12, scope: [[DBG17]]) +; CHECK: [[META21]] = !DILocation(line: 7, column: 22, scope: [[DBG3]], inlinedAt: [[META20]]) +; CHECK: [[META22]] = !{!"llvm.loop.distribute.followup_all", [[META19]], [[META21]], [[META9]], [[META12]], [[META13]], [[META23:![0-9]+]]} +; CHECK: [[META23]] = !{!"llvm.loop.vectorize.followup_all", [[META19]], [[META21]], [[META9]], [[META15]], [[META16]]} +;.