Skip to content

Commit

Permalink
[BitcodeAnalyzer] allow a motivated user to dump BLOCKINFO
Browse files Browse the repository at this point in the history
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
  • Loading branch information
woodruffw authored and xgupta committed Oct 10, 2021
1 parent f95d9c9 commit e7fc254
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 2 additions & 0 deletions llvm/include/llvm/Bitcode/BitcodeAnalyzer.h
Expand Up @@ -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) {}
};
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions llvm/test/Other/bcanalyzer-dump-blockinfo-option.txt
@@ -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>
11 changes: 9 additions & 2 deletions llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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.
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit e7fc254

Please sign in to comment.