[BitcodeAnalyzer] allow a motivated user to dump BLOCKINFO
authorwilliam woodruff <william@yossarian.net>
Sun, 10 Oct 2021 04:10:22 +0000 (09:40 +0530)
committerShivam Gupta <shivam98.tkg@gmail.com>
Sun, 10 Oct 2021 04:45:14 +0000 (10:15 +0530)
This adds the `--dump-blockinfo` flag to `llvm-bcanalyzer`, allowing a sufficiently motivated user to dump (parts of) the `BLOCKINFO_BLOCK` block. The default behavior is unchanged, and `--dump-blockinfo` only takes effect in the same context as other flags that control dump behavior (i.e., requires that `--dump` is also passed).

Reviewed By: tejohnson

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

llvm/include/llvm/Bitcode/BitcodeAnalyzer.h
llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp
llvm/test/Other/bcanalyzer-dump-blockinfo-option.txt [new file with mode: 0644]
llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp

index de828be3bf1b1f01038149b772794e77df082841..f6fc284da33fc6888cc5a3138fbd0ab6faded0e5 100644 (file)
@@ -42,6 +42,8 @@ struct BCDumpOptions {
   bool Symbolic = false;
   /// Print binary blobs using hex escapes.
   bool ShowBinaryBlobs = false;
+  /// Print BLOCKINFO block details.
+  bool DumpBlockinfo = false;
 
   BCDumpOptions(raw_ostream &OS) : OS(OS) {}
 };
index f577d3886e015ac28a9e6b6a2c646bcfa2f912d8..7777c5d9b1dcd427388ff471e6ca219bff4c2bc6 100644 (file)
@@ -744,7 +744,7 @@ Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
   // BLOCKINFO is a special part of the stream.
   bool DumpRecords = O.hasValue();
   if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
-    if (O)
+    if (O && !O->DumpBlockinfo)
       O->OS << Indent << "<BLOCKINFO_BLOCK/>\n";
     Expected<Optional<BitstreamBlockInfo>> MaybeNewBlockInfo =
         Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
@@ -758,8 +758,8 @@ Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
     if (Error Err = Stream.JumpToBit(BlockBitStart))
       return Err;
     // It's not really interesting to dump the contents of the blockinfo
-    // block.
-    DumpRecords = false;
+    // block, so only do it if the user explicitly requests it.
+    DumpRecords = O && O->DumpBlockinfo;
   }
 
   unsigned NumWords = 0;
diff --git a/llvm/test/Other/bcanalyzer-dump-blockinfo-option.txt b/llvm/test/Other/bcanalyzer-dump-blockinfo-option.txt
new file mode 100644 (file)
index 0000000..4fdc06f
--- /dev/null
@@ -0,0 +1,12 @@
+# RUN: llvm-bcanalyzer --dump --dump-blockinfo %S/Inputs/has-block-info.bc | FileCheck %s
+
+# CHECK: <BLOCKINFO_BLOCK NumWords=13 BlockCodeSize=2>
+# CHECK:   <SETBID op0=8/>
+# CHECK:   <BLOCKNAME op0=65 op1=66 op2=67 op3=0/>
+# CHECK:   <SETRECORDNAME op0=0 op1=65 op2=65 op3=65 op4=0/>
+# CHECK:   <SETRECORDNAME op0=1 op1=66 op2=66 op3=66 op4=0/>
+# CHECK:   <SETBID op0=9/>
+# CHECK:   <BLOCKNAME op0=88 op1=89 op2=90 op3=0/>
+# CHECK:   <SETRECORDNAME op0=0 op1=88 op2=88 op3=88 op4=0/>
+# CHECK:   <SETRECORDNAME op0=1 op1=89 op2=89 op3=89 op4=0/>
+# CHECK: </BLOCKINFO_BLOCK>
index f4851bfb2a9cf40c7cacbfd36d8644e9a22b5ac9..a238b0cf5922449f1701161fcd6c189a28ad6ddf 100644 (file)
@@ -11,8 +11,9 @@
 //  llvm-bcanalyzer [options] x.bc - Read LLVM bitcode from the x.bc file
 //
 //  Options:
-//      --help      - Output information about command line switches
-//      --dump      - Dump low-level bitcode structure in readable format
+//      --help            - Output information about command line switches
+//      --dump            - Dump low-level bitcode structure in readable format
+//      --dump-blockinfo  - Dump the BLOCKINFO_BLOCK, when used with --dump
 //
 // This tool provides analytical information about a bitcode file. It is
 // intended as an aid to developers of bitcode reading and writing software. It
@@ -47,6 +48,11 @@ static cl::opt<std::string> InputFilename(cl::Positional,
 static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"),
                           cl::cat(BCAnalyzerCategory));
 
+static cl::opt<bool> DumpBlockinfo("dump-blockinfo",
+                                   cl::desc("Include BLOCKINFO details in low"
+                                            " level dump"),
+                                   cl::cat(BCAnalyzerCategory));
+
 //===----------------------------------------------------------------------===//
 // Bitcode specific analysis.
 //===----------------------------------------------------------------------===//
@@ -114,6 +120,7 @@ int main(int argc, char **argv) {
   O.Histogram = !NoHistogram;
   O.Symbolic = !NonSymbolic;
   O.ShowBinaryBlobs = ShowBinaryBlobs;
+  O.DumpBlockinfo = DumpBlockinfo;
 
   ExitOnErr(BA.analyze(
       Dump ? Optional<BCDumpOptions>(O) : Optional<BCDumpOptions>(None),