Skip to content

Commit

Permalink
[opt][clang] Enable using -module-summary/-flto=thin with -S/-emit-llvm
Browse files Browse the repository at this point in the history
Enable using -module-summary with -S
(similarly to what currently can be achieved with opt <input> -o - | llvm-dis).
This is a recommit of ef9e624.

Test plan: ninja check-all

Differential revision: https://reviews.llvm.org/D137768
  • Loading branch information
alexander-shaposhnikov committed Nov 18, 2022
1 parent ab9eac7 commit 34ab474
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 27 deletions.
39 changes: 20 additions & 19 deletions clang/lib/CodeGen/BackendUtil.cpp
Expand Up @@ -983,19 +983,24 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule)
MPM.addPass(VerifierPass());

switch (Action) {
case Backend_EmitBC:
if (Action == Backend_EmitBC || Action == Backend_EmitLL) {
if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
if (!ThinLinkOS)
return;
}
if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
CodeGenOpts.EnableSplitLTOUnit);
MPM.addPass(ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &ThinLinkOS->os()
: nullptr));
if (Action == Backend_EmitBC) {
if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
if (!ThinLinkOS)
return;
}
MPM.addPass(ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &ThinLinkOS->os()
: nullptr));
} else {
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
/*EmitLTOSummary=*/true));
}

} else {
// Emit a module summary by default for Regular LTO except for ld64
// targets
Expand All @@ -1007,17 +1012,13 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
uint32_t(1));
}
MPM.addPass(
BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
if (Action == Backend_EmitBC)
MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
EmitLTOSummary));
else
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
EmitLTOSummary));
}
break;

case Backend_EmitLL:
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
break;

default:
break;
}

// Now that we have all of the passes ready, run them.
Expand Down
3 changes: 3 additions & 0 deletions clang/test/CodeGen/split-lto-unit.c
@@ -1,12 +1,15 @@
// ; Check that -flto=thin without -fsplit-lto-unit has EnableSplitLTOUnit = 0
// RUN: %clang_cc1 -flto=thin -emit-llvm-bc < %s | llvm-dis -o - | FileCheck %s
// RUN: %clang_cc1 -flto=thin -emit-llvm < %s | FileCheck %s
// CHECK: !{i32 1, !"EnableSplitLTOUnit", i32 0}
//
// ; Check that -flto=thin with -fsplit-lto-unit has EnableSplitLTOUnit = 1
// RUN: %clang_cc1 -flto=thin -fsplit-lto-unit -emit-llvm-bc < %s | llvm-dis -o - | FileCheck %s --check-prefix=SPLIT
// RUN: %clang_cc1 -flto=thin -fsplit-lto-unit -emit-llvm < %s | FileCheck %s --check-prefix=SPLIT
// SPLIT: !{i32 1, !"EnableSplitLTOUnit", i32 1}
//
// ; Check that regular LTO has EnableSplitLTOUnit = 1
// RUN: %clang_cc1 -flto -triple x86_64-pc-linux-gnu -emit-llvm-bc < %s | llvm-dis -o - | FileCheck %s --implicit-check-not="EnableSplitLTOUnit" --check-prefix=SPLIT
// RUN: %clang_cc1 -flto -triple x86_64-pc-linux-gnu -emit-llvm < %s | FileCheck %s --implicit-check-not="EnableSplitLTOUnit" --check-prefix=SPLIT

int main(void) {}
4 changes: 3 additions & 1 deletion llvm/include/llvm/IRPrinter/IRPrintingPasses.h
Expand Up @@ -34,11 +34,13 @@ class PrintModulePass : public PassInfoMixin<PrintModulePass> {
raw_ostream &OS;
std::string Banner;
bool ShouldPreserveUseListOrder;
bool EmitSummaryIndex;

public:
PrintModulePass();
PrintModulePass(raw_ostream &OS, const std::string &Banner = "",
bool ShouldPreserveUseListOrder = false);
bool ShouldPreserveUseListOrder = false,
bool EmitSummaryIndex = false);

PreservedAnalyses run(Module &M, AnalysisManager<Module> &);
static bool isRequired() { return true; }
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/IRPrinter/CMakeLists.txt
Expand Up @@ -8,6 +8,7 @@ add_llvm_component_library(LLVMIRPrinter
intrinsics_gen

LINK_COMPONENTS
Analysis
Core
Support
)
19 changes: 16 additions & 3 deletions llvm/lib/IRPrinter/IRPrintingPasses.cpp
Expand Up @@ -12,6 +12,7 @@

#include "llvm/IRPrinter/IRPrintingPasses.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Analysis/ModuleSummaryAnalysis.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PrintPasses.h"
Expand All @@ -23,11 +24,13 @@ using namespace llvm;

PrintModulePass::PrintModulePass() : OS(dbgs()) {}
PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
bool ShouldPreserveUseListOrder)
bool ShouldPreserveUseListOrder,
bool EmitSummaryIndex)
: OS(OS), Banner(Banner),
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
EmitSummaryIndex(EmitSummaryIndex) {}

PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &) {
PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &AM) {
if (llvm::isFunctionInPrintList("*")) {
if (!Banner.empty())
OS << Banner << "\n";
Expand All @@ -44,6 +47,16 @@ PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &) {
}
}
}

ModuleSummaryIndex *Index =
EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
: nullptr;
if (Index) {
if (Index->modulePaths().empty())
Index->addModule("", 0);
Index->print(OS);
}

return PreservedAnalyses::all();
}

Expand Down
2 changes: 2 additions & 0 deletions llvm/test/Bitcode/thinlto-function-summary.ll
Expand Up @@ -31,6 +31,8 @@


; RUN: opt -passes=name-anon-globals -module-summary < %s | llvm-dis | FileCheck %s
; RUN: opt -passes=name-anon-globals -module-summary -S < %s | FileCheck %s
; RUN: opt -passes=name-anon-globals -module-summary -S < %s | llvm-as | llvm-dis | FileCheck %s
; Check that this round-trips correctly.

; ModuleID = '<stdin>'
Expand Down
8 changes: 4 additions & 4 deletions llvm/tools/opt/NewPMDriver.cpp
Expand Up @@ -443,16 +443,16 @@ bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
MPM.addPass(NewPMCheckDebugifyPass(false, "", &DIStatsMap));
if (VerifyDIPreserve)
MPM.addPass(NewPMCheckDebugifyPass(
false, "", nullptr, DebugifyMode::OriginalDebugInfo, &DebugInfoBeforePass,
VerifyDIPreserveExport));
false, "", nullptr, DebugifyMode::OriginalDebugInfo,
&DebugInfoBeforePass, VerifyDIPreserveExport));

// Add any relevant output pass at the end of the pipeline.
switch (OK) {
case OK_NoOutput:
break; // No output pass needed.
case OK_OutputAssembly:
MPM.addPass(
PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
MPM.addPass(PrintModulePass(
Out->os(), "", ShouldPreserveAssemblyUseListOrder, EmitSummaryIndex));
break;
case OK_OutputBitcode:
MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
Expand Down
1 change: 1 addition & 0 deletions utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
Expand Up @@ -1386,6 +1386,7 @@ cc_library(
]),
copts = llvm_copts,
deps = [
":Analysis",
":Core",
":Support",
],
Expand Down

0 comments on commit 34ab474

Please sign in to comment.