Skip to content

Commit aaad45a

Browse files
committed
[clang][CodeGen][Comgr] Omit pre-opt link when post-opt is link requested (llvm#85672)
Currently, when the -relink-builtin-bitcodes-postop option is used we link builtin bitcodes twice: once before optimization, and again after optimization. With this change, we omit the pre-opt linking when the option is set, and we rename the option to the following: -Xclang -mlink-builtin-bitcodes-postopt (-Xclang -mno-link-builtin-bitcodes-postopt) The goal of this change is to reduce compile time. We do lose the theoretical benefits of pre-opt linking, but in practice these are small than the overhead of linking twice. However we may be able to address this in a future patch by adjusting the position of the builtin-bitcode linking pass. Compilations not setting the option are unaffected Comgr: update the option name used Change-Id: Ife876232f6d3f1c1d728c16d2fec893817c5fa91
1 parent ddd287d commit aaad45a

File tree

8 files changed

+45
-57
lines changed

8 files changed

+45
-57
lines changed

amd/comgr/src/comgr-compiler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,8 +1076,8 @@ amd_comgr_status_t AMDGPUCompiler::addDeviceLibraries() {
10761076
return AMD_COMGR_STATUS_ERROR;
10771077
}
10781078
Args.push_back(Saver.save(Twine("--rocm-path=") + FakeRocmDir).data());
1079-
Args.push_back("-mllvm");
1080-
Args.push_back("-relink-builtin-bitcode-postop");
1079+
Args.push_back("-Xclang");
1080+
Args.push_back("-mlink-builtin-bitcode-postopt");
10811081
NoGpuLib = false;
10821082

10831083
for (auto DeviceLib : getDeviceLibraries()) {

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ CODEGENOPT(UnrollLoops , 1, 0) ///< Control whether loops are unrolled.
298298
CODEGENOPT(RerollLoops , 1, 0) ///< Control whether loops are rerolled.
299299
CODEGENOPT(NoUseJumpTables , 1, 0) ///< Set when -fno-jump-tables is enabled.
300300
VALUE_CODEGENOPT(UnwindTables, 2, 0) ///< Unwind tables (1) or asynchronous unwind tables (2)
301+
CODEGENOPT(LinkBitcodePostopt, 1, 0) ///< Link builtin bitcodes after optimization pipeline.
301302
CODEGENOPT(VectorizeLoop , 1, 0) ///< Run loop vectorizer.
302303
CODEGENOPT(VectorizeSLP , 1, 0) ///< Run SLP vectorizer.
303304
CODEGENOPT(ProfileSampleAccurate, 1, 0) ///< Sample profile is accurate.

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7005,6 +7005,11 @@ def mlink_bitcode_file : Separate<["-"], "mlink-bitcode-file">,
70057005
def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
70067006
HelpText<"Link and internalize needed symbols from the given bitcode file "
70077007
"before performing optimizations.">;
7008+
defm link_builtin_bitcode_postopt: BoolMOption<"link-builtin-bitcode-postopt",
7009+
CodeGenOpts<"LinkBitcodePostopt">, DefaultFalse,
7010+
PosFlag<SetTrue, [], [ClangOption], "Link builtin bitcodes after the "
7011+
"optimization pipeline">,
7012+
NegFlag<SetFalse, [], [ClangOption]>>;
70087013
def vectorize_loops : Flag<["-"], "vectorize-loops">,
70097014
HelpText<"Run the Loop vectorization passes">,
70107015
MarshallingInfoFlag<CodeGenOpts<"VectorizeLoop">>;

clang/lib/CodeGen/BackendConsumer.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ class BackendConsumer : public ASTConsumer {
115115
// Links each entry in LinkModules into our module. Returns true on error.
116116
bool LinkInModules(llvm::Module *M, bool ShouldLinkFiles = true);
117117

118-
// Load a bitcode module from -mlink-builtin-bitcode option using
119-
// methods from a BackendConsumer instead of CompilerInstance
120-
bool ReloadModules(llvm::Module *M);
121-
122118
/// Get the best possible source location to represent a diagnostic that
123119
/// may have associated debug info.
124120
const FullSourceLoc getBestLocationFromDebugLoc(

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ static cl::opt<bool> ClSanitizeOnOptimizerEarlyEP(
104104
cl::desc("Insert sanitizers on OptimizerEarlyEP."), cl::init(false));
105105

106106
extern cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate;
107-
108-
// Re-link builtin bitcodes after optimization
109-
cl::opt<bool> ClRelinkBuiltinBitcodePostop(
110-
"relink-builtin-bitcode-postop", cl::Optional,
111-
cl::desc("Re-link builtin bitcodes after optimization."), cl::init(false));
112107
} // namespace llvm
113108

114109
namespace {
@@ -1012,11 +1007,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10121007
}
10131008
}
10141009

1015-
// Re-link against any bitcodes supplied via the -mlink-builtin-bitcode option
1016-
// Some optimizations may generate new function calls that would not have
1017-
// been linked pre-optimization (i.e. fused sincos calls generated by
1018-
// AMDGPULibCalls::fold_sincos.)
1019-
if (ClRelinkBuiltinBitcodePostop)
1010+
// Link against bitcodes supplied via the -mlink-builtin-bitcode option
1011+
if (CodeGenOpts.LinkBitcodePostopt)
10201012
MPM.addPass(LinkInModulesPass(BC, false));
10211013

10221014
// Add a verifier pass if requested. We don't have to do this if the action

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ using namespace llvm;
5858

5959
#define DEBUG_TYPE "codegenaction"
6060

61-
namespace llvm {
62-
extern cl::opt<bool> ClRelinkBuiltinBitcodePostop;
63-
}
64-
6561
namespace clang {
6662
class BackendConsumer;
6763
class ClangDiagnosticHandler final : public DiagnosticHandler {
@@ -230,35 +226,6 @@ void BackendConsumer::HandleInterestingDecl(DeclGroupRef D) {
230226
HandleTopLevelDecl(D);
231227
}
232228

233-
bool BackendConsumer::ReloadModules(llvm::Module *M) {
234-
for (const CodeGenOptions::BitcodeFileToLink &F :
235-
CodeGenOpts.LinkBitcodeFiles) {
236-
auto BCBuf = FileMgr.getBufferForFile(F.Filename);
237-
if (!BCBuf) {
238-
Diags.Report(diag::err_cannot_open_file)
239-
<< F.Filename << BCBuf.getError().message();
240-
LinkModules.clear();
241-
return true;
242-
}
243-
244-
LLVMContext &Ctx = getModule()->getContext();
245-
Expected<std::unique_ptr<llvm::Module>> ModuleOrErr =
246-
getOwningLazyBitcodeModule(std::move(*BCBuf), Ctx);
247-
248-
if (!ModuleOrErr) {
249-
handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
250-
Diags.Report(diag::err_cannot_open_file) << F.Filename << EIB.message();
251-
});
252-
LinkModules.clear();
253-
return true;
254-
}
255-
LinkModules.push_back({std::move(ModuleOrErr.get()), F.PropagateAttrs,
256-
F.Internalize, F.LinkFlags});
257-
}
258-
259-
return false; // success
260-
}
261-
262229
// Links each entry in LinkModules into our module. Returns true on error.
263230
bool BackendConsumer::LinkInModules(llvm::Module *M, bool ShouldLinkFiles) {
264231
for (auto &LM : LinkModules) {
@@ -357,7 +324,7 @@ void BackendConsumer::HandleTranslationUnit(ASTContext &C) {
357324
}
358325

359326
// Link each LinkModule into our module.
360-
if (LinkInModules(getModule()))
327+
if (!CodeGenOpts.LinkBitcodePostopt && LinkInModules(getModule()))
361328
return;
362329

363330
for (auto &F : getModule()->functions()) {
@@ -1282,7 +1249,7 @@ void CodeGenAction::ExecuteAction() {
12821249
std::move(LinkModules), *VMContext, nullptr);
12831250

12841251
// Link in each pending link module.
1285-
if (Result.LinkInModules(&*TheModule))
1252+
if (!CodeGenOpts.LinkBitcodePostopt && Result.LinkInModules(&*TheModule))
12861253
return;
12871254

12881255
// PR44896: Force DiscardValueNames as false. DiscardValueNames cannot be

clang/lib/CodeGen/LinkInModulesPass.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@ PreservedAnalyses LinkInModulesPass::run(Module &M, ModuleAnalysisManager &AM) {
2828
if (!BC)
2929
return PreservedAnalyses::all();
3030

31-
// Re-load bitcode modules from files
32-
if (BC->ReloadModules(&M))
33-
report_fatal_error("Bitcode module re-loading failed, aborted!");
34-
3531
if (BC->LinkInModules(&M, ShouldLinkFiles))
36-
report_fatal_error("Bitcode module re-linking failed, aborted!");
32+
report_fatal_error("Bitcode module postopt linking failed, aborted!");
3733

38-
return PreservedAnalyses::all();
34+
return PreservedAnalyses::none();
3935
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// REQUIRES: amdgpu-registered-target
2+
3+
// Test that -mlink-bitcode-postopt correctly enables LinkInModulesPass
4+
5+
// RUN: %clang_cc1 -triple amdgcn-- -emit-llvm-bc -o /dev/null \
6+
// RUN: -mllvm -print-pipeline-passes \
7+
// RUN: %s 2>&1 | FileCheck --check-prefixes=DEFAULT %s
8+
9+
// DEFAULT-NOT: LinkInModulesPass
10+
11+
// RUN: %clang_cc1 -triple amdgcn-- -emit-llvm-bc -o /dev/null \
12+
// RUN: -mllvm -print-pipeline-passes \
13+
// RUN: -mlink-builtin-bitcode-postopt \
14+
// RUN: %s 2>&1 | FileCheck --check-prefixes=OPTION-POSITIVE %s
15+
16+
// OPTION-POSITIVE: LinkInModulesPass
17+
18+
// RUN: %clang_cc1 -triple amdgcn-- -emit-llvm-bc -o /dev/null \
19+
// RUN: -mllvm -print-pipeline-passes \
20+
// RUN: -mno-link-builtin-bitcode-postopt \
21+
// RUN: %s 2>&1 | FileCheck --check-prefixes=OPTION-NEGATIVE %s
22+
23+
// OPTION-NEGATIVE-NOT: LinkInModulesPass
24+
25+
// RUN: %clang_cc1 -triple amdgcn-- -emit-llvm-bc -o /dev/null \
26+
// RUN: -mllvm -print-pipeline-passes \
27+
// RUN: -mlink-builtin-bitcode-postopt \
28+
// RUN: -mno-link-builtin-bitcode-postopt \
29+
// RUN: %s 2>&1 | FileCheck --check-prefixes=OPTION-POSITIVE-NEGATIVE %s
30+
31+
// OPTION-POSITIVE-NEGATIVE-NOT: LinkInModulesPass

0 commit comments

Comments
 (0)