From 58211f55c5d42394b3de3310a825d707e7622833 Mon Sep 17 00:00:00 2001 From: Stephen Tozer Date: Fri, 11 Apr 2025 15:38:58 +0100 Subject: [PATCH] [llvm-reduce] Fix incorrectly ignored null MD in ReduceDIMetadata (#108541) Commit c2e62c7 updated the ReduceDIMetadata pass to be able to remove DIGlobalVariableExpressions from MDNode operands; it also accidentally prevented null operands from being preserved, which results in an assertion being triggered: `Targets == NoChunksCounter.count() && "number of chunks changes when reducing"' This patch allows us to correctly preserve null operands once again. I've not got a test case for this yet - I'm hoping this patch is just trivially correct as-is, because I've not got the hang of reducing a test case for llvm-reduce yet, but I can get a test case generated if needed. --- .../llvm-reduce/108541-null-metadata-crash.ll | 47 +++++++++++++++++++ .../llvm-reduce/deltas/ReduceDIMetadata.cpp | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 llvm/test/tools/llvm-reduce/108541-null-metadata-crash.ll diff --git a/llvm/test/tools/llvm-reduce/108541-null-metadata-crash.ll b/llvm/test/tools/llvm-reduce/108541-null-metadata-crash.ll new file mode 100644 index 000000000000..d9a4f38cae4a --- /dev/null +++ b/llvm/test/tools/llvm-reduce/108541-null-metadata-crash.ll @@ -0,0 +1,47 @@ +; RUN: llvm-reduce %s -o %t --delta-passes=di-metadata --test FileCheck --test-arg %s --test-arg --input-file --abort-on-invalid-reduction +; CHECK: , !dbg !11 + +;; Tests for the bug fixed in PR#108541, where the presence of null metadata +;; could result in a crash. + +define i1 @ham() { +bb: + %call = call fastcc i32 @hoge() + ret i1 false +} + +define fastcc i32 @hoge() { +bb: + br i1 poison, label %bb1, label %bb2 + +bb1: ; preds = %bb + br i1 false, label %bb2, label %bb2, !dbg !11 + +bb2: ; preds = %bb1, %bb1, %bb + ret i32 0 +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!10} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 20.0.0git", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: !2, globals: !2, imports: !3, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "108541-metadata-crash.cpp", directory: "/tmp") +!2 = !{} +!3 = !{!4} +!4 = !DIImportedEntity(tag: DW_TAG_imported_declaration, scope: !5, entity: !6, file: !7, line: 134) +!5 = !DINamespace(name: "std", scope: null) +!6 = !DISubprogram(name: "abort", scope: !7, file: !7, line: 730, type: !8, flags: DIFlagPrototyped | DIFlagNoReturn, spFlags: DISPFlagOptimized) +!7 = !DIFile(filename: "108541-metadata-crash.cpp", directory: "") +!8 = !DISubroutineType(types: !9) +!9 = !{null} +!10 = !{i32 2, !"Debug Info Version", i32 3} +!11 = !DILocation(line: 26, column: 11, scope: !12) +!12 = distinct !DILexicalBlock(scope: !14, file: !13, line: 26, column: 11) +!13 = !DIFile(filename: "108541-metadata-crash.cpp", directory: "/tmp") +!14 = distinct !DILexicalBlock(scope: !15, file: !13, line: 25, column: 5) +!15 = distinct !DILexicalBlock(scope: !16, file: !13, line: 24, column: 9) +!16 = distinct !DILexicalBlock(scope: !17, file: !13, line: 14, column: 3) +!17 = distinct !DILexicalBlock(scope: !18, file: !13, line: 13, column: 3) +!18 = distinct !DILexicalBlock(scope: !19, file: !13, line: 13, column: 3) +!19 = distinct !DISubprogram(name: "hoge", linkageName: "hoge", scope: !13, file: !13, line: 10, type: !20, scopeLine: 11, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2) +!20 = distinct !DISubroutineType(types: !2) diff --git a/llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp b/llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp index 8d1d73785f56..34e0791d4e97 100644 --- a/llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp +++ b/llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp @@ -67,8 +67,8 @@ void identifyUninterestingMDNodes(Oracle &O, MDNodeList &MDs) { // Don't add uninteresting operands to the tuple. if (!O.shouldKeep()) continue; - TN.push_back(Op); } + TN.push_back(Tup->getOperand(I)); } if (TN.size() != Tup->getNumOperands()) DbgNode->replaceOperandWith(OpIdx, DbgNode->get(DbgNode->getContext(), TN));