[PATCH] D117244: [llvm-dis] Add an option `dump-thinlto-index-only` in llvm-dis to read ThinLTO minimized code only.

Mingming Liu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 18 17:10:35 PST 2022


luna updated this revision to Diff 401052.
luna retitled this revision from "[Bitcode] [ThinLTO] Add a new bitcode module record for THINLTO_INDEX_FLAG" to "  [llvm-dis] Add an option `dump-thinlto-index-only` in llvm-dis to read ThinLTO minimized code only.".
luna edited the summary of this revision.
luna added a comment.

Add a flag in `llvm-dis` to print index, and it's up to command runner to decide if the input is a fit (e.g., a ThinLTO bitcode).

Add a regression test for it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117244/new/

https://reviews.llvm.org/D117244

Files:
  llvm/lib/Bitcode/Reader/BitcodeReader.cpp
  llvm/test/Bitcode/thinlto-index-disassembled-by-llvm-dis.ll
  llvm/tools/llvm-dis/llvm-dis.cpp


Index: llvm/tools/llvm-dis/llvm-dis.cpp
===================================================================
--- llvm/tools/llvm-dis/llvm-dis.cpp
+++ llvm/tools/llvm-dis/llvm-dis.cpp
@@ -74,6 +74,10 @@
                                  "then materialize only the metadata"),
                         cl::cat(DisCategory));
 
+static cl::opt<bool> DumpThinLTOIndexOnly(
+    "dump-thinlto-index-only",
+    cl::desc("Only read thinlto index and dump the index as a bitcode file."),
+    cl::init(false), cl::Hidden, cl::cat(DisCategory));
 namespace {
 
 static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
@@ -186,12 +190,17 @@
 
     for (size_t I = 0; I < N; ++I) {
       BitcodeModule MB = IF.Mods[I];
-      std::unique_ptr<Module> M = ExitOnErr(
-          MB.getLazyModule(Context, MaterializeMetadata, SetImporting));
-      if (MaterializeMetadata)
-        ExitOnErr(M->materializeMetadata());
-      else
-        ExitOnErr(M->materializeAll());
+
+      std::unique_ptr<Module> M;
+
+      if (!DumpThinLTOIndexOnly) {
+        M = ExitOnErr(
+            MB.getLazyModule(Context, MaterializeMetadata, SetImporting));
+        if (MaterializeMetadata)
+          ExitOnErr(M->materializeMetadata());
+        else
+          ExitOnErr(M->materializeAll());
+      }
 
       BitcodeLTOInfo LTOInfo = ExitOnErr(MB.getLTOInfo());
       std::unique_ptr<ModuleSummaryIndex> Index;
@@ -233,7 +242,8 @@
 
       // All that llvm-dis does is write the assembly to a file.
       if (!DontPrint) {
-        M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
+        if (M)
+          M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
         if (Index)
           Index->print(Out->os());
       }
Index: llvm/test/Bitcode/thinlto-index-disassembled-by-llvm-dis.ll
===================================================================
--- /dev/null
+++ llvm/test/Bitcode/thinlto-index-disassembled-by-llvm-dis.ll
@@ -0,0 +1,26 @@
+; Tests that `llvm-dis` could disassemble ThinLTO minimized bitcode
+; (e.g., bitcode generated by `opt --thin-link-bitcode-file=<minimized-bitcode>`,
+;  or `clang -Xclang -fthin-link-bitcode=<minimized-bitcode>`)
+
+; %t.o is the thin-lto ready bitcode, and %t.thinlink.bc is the minimized bitcode. 
+; RUN: opt -thinlto-bc %s -thin-link-bitcode-file=%t.thinlink.bc -o %t.o
+
+; With default options, `llvm-dis` returns invalid record for ThinLTO minimized bitcode.
+; RUN: not llvm-dis %t.thinlink.bc 2>&1 | FileCheck %s --check-prefix=INVALID-RECORD
+
+; INVALID-RECORD: Invalid record
+
+; Tests that `llvm-dis` could disassemble minimized bitcode with `--dump-thinlto-index-only`, and verify its content.
+; RUN: llvm-dis --dump-thinlto-index-only %t.thinlink.bc -o - | FileCheck %s --check-prefix=DIS
+
+; DIS: ^0 = module: (path: "{{.*}}thinlto-index-disassembled-by-llvm-dis.ll.tmp.thinlink.bc", hash:
+; DIS: ^1 = gv: (name: "aplusb", summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 0, live: 0, dsoLocal: 0, canAutoHide: 0), insts: 2))) ; guid = 
+; DIS: ^2 = blockcount: 1
+
+source_filename = "add.cpp"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+define i32 @aplusb(i32 %a, i32 %b) {
+  %add = add i32 %b, %a
+  ret i32 %add
+}
Index: llvm/lib/Bitcode/Reader/BitcodeReader.cpp
===================================================================
--- llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -3265,6 +3265,11 @@
   // dllstorageclass, comdat, attributes, preemption specifier,
   // partition strtab offset, partition strtab size] (name in VST)
   // v2: [strtab_offset, strtab_size, v1]
+  //
+  // FIXME: BitcodeReader should handle GLOBALVAR written by
+  // ThinLinkBitcodeWriter. (see
+  // https://github.com/llvm/llvm-project/blob/b6a93967d9c11e79802b5e75cec1584d6c8aa472/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp#L4741
+  // for the format)
   StringRef Name;
   std::tie(Name, Record) = readNameFromStrtab(Record);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117244.401052.patch
Type: text/x-patch
Size: 4131 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220119/8bc0a4ef/attachment.bin>


More information about the llvm-commits mailing list