Add support for multi-module bitcode files to llvm-dis

Summary:
This change allows llvm-dis to disassemble multi-module bitcode
files, including the associated module summary.

Reviewers: tejohnson, pcc, mehdi_amini

Subscribers: llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D70153
This commit is contained in:
Matthew Voss 2019-11-12 16:29:37 -08:00
parent caa9493da8
commit 141bb5f308
5 changed files with 92 additions and 51 deletions

View File

@ -0,0 +1,15 @@
; RUN: llvm-as %s -o %t.o
; RUN: llvm-cat -b -o %t2.o %t.o %t.o
; RUN: llvm-dis -o %t3 %t2.o
; RUN: llvm-as -o %t4.o %t3.0
; RUN: llvm-as -o %t5.o %t3.1
; RUN: cmp %t4.o %t5.o
; RUN: llvm-cat -b -o %t6.o %t5.o %t4.o
; RUN: llvm-dis -o %t7.o %t6.o
; RUN: diff %t7.o.0 %t7.o.1
; RUN: FileCheck < %t7.o.0 %s
; RUN: FileCheck < %t7.o.1 %s
; CHECK: source_filename = "{{.*}}multi-mod-disassemble.ll"
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"

View File

@ -0,0 +1,18 @@
; RUN: llvm-as %s -o %t.o
; RUN: llvm-cat -b -o %t2.o %t.o %t.o
; RUN: llvm-dis -o %t3 %t2.o
; RUN: llvm-as -o %t4.o %t3.0
; RUN: llvm-as -o %t5.o %t3.1
; RUN: cmp %t4.o %t5.o
; RUN: llvm-cat -b -o %t6.o %t5.o %t4.o
; RUN: llvm-dis -o %t7.o %t6.o
; RUN: diff %t7.o.0 %t7.o.1
; RUN: FileCheck < %t7.o.0 %s
; RUN: FileCheck < %t7.o.1 %s
; ModuleID = 'multi-summary-disassemble.o'
^0 = module: (path: "multi-summary-disassemble.ll", hash: (1369602428, 2747878711, 259090915, 2507395659, 1141468049))
^1 = gv: (guid: 3, summaries: (function: (module: ^0, flags: (linkage: internal, notEligibleToImport: 0, live: 0, dsoLocal: 1), insts: 1)))
; CHECK: ^0 = module: (path:
; CHECK: ^1 = gv: (guid: 3, summaries: (function: (module: ^0,

View File

@ -66,11 +66,6 @@ INSERT-ARRAY: INSERTVAL: Invalid array index
INSERT-STRUCT: INSERTVAL: Invalid struct index
INSERT-IDXS: INSERTVAL: Invalid type
RUN: not llvm-dis -disable-output %p/Inputs/invalid-no-proper-module.bc 2>&1 | \
RUN: FileCheck --check-prefix=NO-MODULE %s
NO-MODULE: Expected a single module
RUN: not llvm-dis -disable-output %p/Inputs/invalid-fp-shift.bc 2>&1 | \
RUN: FileCheck --check-prefix=FP-SHIFT %s

View File

@ -1,6 +1,4 @@
; RUN: llvm-cat -o %t %s %S/Inputs/multi-module.ll
; RUN: not llvm-dis -o - %t 2>&1 | FileCheck --check-prefix=ERROR %s
; ERROR: Expected a single module
; RUN: llvm-bcanalyzer -dump %t | FileCheck --check-prefix=BCA %s
@ -10,18 +8,15 @@
; RUN: llvm-as -o %t1 %s
; RUN: llvm-as -o %t2 %S/Inputs/multi-module.ll
; RUN: llvm-cat -o %t %t1 %t2
; RUN: not llvm-dis -o - %t 2>&1 | FileCheck --check-prefix=ERROR %s
; RUN: llvm-bcanalyzer -dump %t | FileCheck --check-prefix=BCA %s
; RUN: llvm-cat -b -o %t %t1 %t2
; RUN: not llvm-dis -o - %t 2>&1 | FileCheck --check-prefix=ERROR %s
; RUN: llvm-bcanalyzer -dump %t | FileCheck --check-prefix=BCA %s
; RUN: llvm-modextract -n 0 -o - %t | llvm-dis | FileCheck --check-prefix=IR1 %s
; RUN: llvm-modextract -n 1 -o - %t | llvm-dis | FileCheck --check-prefix=IR2 %s
; RUN: llvm-cat -b -o %t3 %t %t
; RUN: not llvm-dis -o - %t3 2>&1 | FileCheck --check-prefix=ERROR %s
; RUN: llvm-bcanalyzer -dump %t3 | FileCheck --check-prefix=BCA4 %s
; RUN: llvm-modextract -n 0 -o - %t3 | llvm-dis | FileCheck --check-prefix=IR1 %s

View File

@ -158,53 +158,71 @@ int main(int argc, char **argv) {
std::unique_ptr<MemoryBuffer> MB =
ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
std::unique_ptr<Module> M = ExitOnErr(getLazyBitcodeModule(
*MB, Context, /*ShouldLazyLoadMetadata=*/true, SetImporting));
if (MaterializeMetadata)
ExitOnErr(M->materializeMetadata());
else
ExitOnErr(M->materializeAll());
BitcodeLTOInfo LTOInfo = ExitOnErr(getBitcodeLTOInfo(*MB));
std::unique_ptr<ModuleSummaryIndex> Index;
if (LTOInfo.HasSummary)
Index = ExitOnErr(getModuleSummaryIndex(*MB));
BitcodeFileContents IF = ExitOnErr(llvm::getBitcodeFileContents(*MB));
// Just use stdout. We won't actually print anything on it.
if (DontPrint)
OutputFilename = "-";
const size_t N = IF.Mods.size();
if (OutputFilename.empty()) { // Unspecified output, infer it.
if (InputFilename == "-") {
OutputFilename = "-";
if (OutputFilename == "-" && N > 1)
errs() << "only single module bitcode files can be written to stdout\n";
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());
BitcodeLTOInfo LTOInfo = ExitOnErr(MB.getLTOInfo());
std::unique_ptr<ModuleSummaryIndex> Index;
if (LTOInfo.HasSummary)
Index = ExitOnErr(MB.getSummary());
std::string FinalFilename(OutputFilename);
// Just use stdout. We won't actually print anything on it.
if (DontPrint)
FinalFilename = "-";
if (OutputFilename.empty()) { // Unspecified output, infer it.
if (InputFilename == "-") {
FinalFilename = "-";
} else {
StringRef IFN = InputFilename;
FinalFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
if (N > 1)
FinalFilename += std::string(".") + std::to_string(i);
FinalFilename += ".ll";
}
} else {
StringRef IFN = InputFilename;
OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
OutputFilename += ".ll";
if (N > 1)
FinalFilename += std::string(".") + std::to_string(i);
}
std::error_code EC;
std::unique_ptr<ToolOutputFile> Out(
new ToolOutputFile(FinalFilename, EC, sys::fs::OF_Text));
if (EC) {
errs() << EC.message() << '\n';
return 1;
}
std::unique_ptr<AssemblyAnnotationWriter> Annotator;
if (ShowAnnotations)
Annotator.reset(new CommentWriter());
// All that llvm-dis does is write the assembly to a file.
if (!DontPrint) {
M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
if (Index)
Index->print(Out->os());
}
// Declare success.
Out->keep();
}
std::error_code EC;
std::unique_ptr<ToolOutputFile> Out(
new ToolOutputFile(OutputFilename, EC, sys::fs::OF_Text));
if (EC) {
errs() << EC.message() << '\n';
return 1;
}
std::unique_ptr<AssemblyAnnotationWriter> Annotator;
if (ShowAnnotations)
Annotator.reset(new CommentWriter());
// All that llvm-dis does is write the assembly to a file.
if (!DontPrint) {
M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
if (Index)
Index->print(Out->os());
}
// Declare success.
Out->keep();
return 0;
}