[llvm] 47b1623 - [llvm-reduce] Fail verifier less when removing debug metadata

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 7 16:23:01 PDT 2022


Author: Arthur Eubanks
Date: 2022-10-07T16:22:13-07:00
New Revision: 47b1623b1fc4b8aaa2d4c310bbc3a895cbb1f527

URL: https://github.com/llvm/llvm-project/commit/47b1623b1fc4b8aaa2d4c310bbc3a895cbb1f527
DIFF: https://github.com/llvm/llvm-project/commit/47b1623b1fc4b8aaa2d4c310bbc3a895cbb1f527.diff

LOG: [llvm-reduce] Fail verifier less when removing debug metadata

Without this patch, we hit the following a lot:
"llvm.dbg.declare intrinsic requires a !dbg attachment"
"DICompileUnit not listed in llvm.dbg.cu"

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D135492

Added: 
    llvm/test/tools/llvm-reduce/debug-metadata-verifier.ll

Modified: 
    llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-reduce/debug-metadata-verifier.ll b/llvm/test/tools/llvm-reduce/debug-metadata-verifier.ll
new file mode 100644
index 000000000000..82d31872ffbc
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/debug-metadata-verifier.ll
@@ -0,0 +1,33 @@
+; RUN: llvm-reduce %s -o %t --delta-passes=metadata --test %python --test-arg %p/Inputs/remove-metadata.py --abort-on-invalid-reduction
+; RUN: FileCheck %s --input-file %t
+
+; CHECK: call void @llvm.dbg.declare{{.*}}, !dbg
+; CHECK: !llvm.dbg.cu = !{!0}
+; CHECK-NOT: uninteresting
+
+define i32 @main() !dbg !4 {
+entry:
+  %i = alloca i32, align 4
+  call void @llvm.dbg.declare(metadata ptr %i, metadata !10, metadata !DIExpression()), !dbg !11
+  ret i32 0
+}
+
+declare void @llvm.dbg.declare(metadata, metadata, metadata) #0
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3}
+!interesting = !{}
+!uninteresting = !{}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 16.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "/tmp/a.c", directory: "/tmp", checksumkind: CSK_MD5, checksum: "3b0a4b024d464b367033485450c7a5f9")
+!2 = !{i32 7, !"Dwarf Version", i32 5}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = distinct !DISubprogram(name: "main", scope: !5, file: !5, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
+!5 = !DIFile(filename: "/tmp/a.c", directory: "", checksumkind: CSK_MD5, checksum: "3b0a4b024d464b367033485450c7a5f9")
+!6 = !DISubroutineType(types: !7)
+!7 = !{!8}
+!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!9 = !{}
+!10 = !DILocalVariable(name: "i", scope: !4, file: !5, line: 2, type: !8)
+!11 = !DILocation(line: 2, column: 6, scope: !4)

diff  --git a/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp b/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
index 078230e80954..0681e35e40de 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
@@ -16,17 +16,26 @@
 #include "llvm/ADT/Sequence.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/IR/InstIterator.h"
+#include "llvm/IR/IntrinsicInst.h"
 #include <vector>
 
 using namespace llvm;
 
+static bool shouldKeepDebugIntrinsicMetadata(Instruction &I, MDNode &MD) {
+  return isa<DILocation>(MD) && isa<DbgInfoIntrinsic>(I);
+}
+
+static bool shouldKeepDebugNamedMetadata(NamedMDNode &MD) {
+  return MD.getName() == "llvm.dbg.cu" && MD.getNumOperands() != 0;
+}
+
 /// Removes all the Named and Unnamed Metadata Nodes, as well as any debug
 /// functions that aren't inside the desired Chunks.
 static void extractMetadataFromModule(Oracle &O, Module &Program) {
   // Get out-of-chunk Named metadata nodes
   SmallVector<NamedMDNode *> NamedNodesToDelete;
   for (NamedMDNode &MD : Program.named_metadata())
-    if (!O.shouldKeep())
+    if (!shouldKeepDebugNamedMetadata(MD) && !O.shouldKeep())
       NamedNodesToDelete.push_back(&MD);
 
   for (NamedMDNode *NN : NamedNodesToDelete) {
@@ -58,9 +67,10 @@ static void extractMetadataFromModule(Oracle &O, Module &Program) {
     for (Instruction &I : instructions(F)) {
       SmallVector<std::pair<unsigned, MDNode *>> MDs;
       I.getAllMetadata(MDs);
-      for (std::pair<unsigned, MDNode *> &MD : MDs)
-        if (!O.shouldKeep())
+      for (std::pair<unsigned, MDNode *> &MD : MDs) {
+        if (!shouldKeepDebugIntrinsicMetadata(I, *MD.second) && !O.shouldKeep())
           I.setMetadata(MD.first, nullptr);
+      }
     }
   }
 }


        


More information about the llvm-commits mailing list