Skip to content

Commit 11a6799

Browse files
authored
[clang][CodeGen] Omit pre-opt link when post-opt is link requested (#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
1 parent 50b45b2 commit 11a6799

File tree

7 files changed

+43
-55
lines changed

7 files changed

+43
-55
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ CODEGENOPT(UnrollLoops , 1, 0) ///< Control whether loops are unrolled.
309309
CODEGENOPT(RerollLoops , 1, 0) ///< Control whether loops are rerolled.
310310
CODEGENOPT(NoUseJumpTables , 1, 0) ///< Set when -fno-jump-tables is enabled.
311311
VALUE_CODEGENOPT(UnwindTables, 2, 0) ///< Unwind tables (1) or asynchronous unwind tables (2)
312+
CODEGENOPT(LinkBitcodePostopt, 1, 0) ///< Link builtin bitcodes after optimization pipeline.
312313
CODEGENOPT(VectorizeLoop , 1, 0) ///< Run loop vectorizer.
313314
CODEGENOPT(VectorizeSLP , 1, 0) ///< Run SLP vectorizer.
314315
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
@@ -7092,6 +7092,11 @@ def mlink_bitcode_file : Separate<["-"], "mlink-bitcode-file">,
70927092
def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
70937093
HelpText<"Link and internalize needed symbols from the given bitcode file "
70947094
"before performing optimizations.">;
7095+
defm link_builtin_bitcode_postopt: BoolMOption<"link-builtin-bitcode-postopt",
7096+
CodeGenOpts<"LinkBitcodePostopt">, DefaultFalse,
7097+
PosFlag<SetTrue, [], [ClangOption], "Link builtin bitcodes after the "
7098+
"optimization pipeline">,
7099+
NegFlag<SetFalse, [], [ClangOption]>>;
70957100
def vectorize_loops : Flag<["-"], "vectorize-loops">,
70967101
HelpText<"Run the Loop vectorization passes">,
70977102
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
@@ -120,11 +120,6 @@ static cl::opt<PGOOptions::ColdFuncOpt> ClPGOColdFuncAttr(
120120
"Mark cold functions with optnone.")));
121121

122122
extern cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate;
123-
124-
// Re-link builtin bitcodes after optimization
125-
cl::opt<bool> ClRelinkBuiltinBitcodePostop(
126-
"relink-builtin-bitcode-postop", cl::Optional,
127-
cl::desc("Re-link builtin bitcodes after optimization."));
128123
} // namespace llvm
129124

130125
namespace {
@@ -1055,11 +1050,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10551050
}
10561051
}
10571052

1058-
// Re-link against any bitcodes supplied via the -mlink-builtin-bitcode option
1059-
// Some optimizations may generate new function calls that would not have
1060-
// been linked pre-optimization (i.e. fused sincos calls generated by
1061-
// AMDGPULibCalls::fold_sincos.)
1062-
if (ClRelinkBuiltinBitcodePostop)
1053+
// Link against bitcodes supplied via the -mlink-builtin-bitcode option
1054+
if (CodeGenOpts.LinkBitcodePostopt)
10631055
MPM.addPass(LinkInModulesPass(BC, false));
10641056

10651057
// 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
@@ -60,10 +60,6 @@ using namespace llvm;
6060

6161
#define DEBUG_TYPE "codegenaction"
6262

63-
namespace llvm {
64-
extern cl::opt<bool> ClRelinkBuiltinBitcodePostop;
65-
}
66-
6763
namespace clang {
6864
class BackendConsumer;
6965
class ClangDiagnosticHandler final : public DiagnosticHandler {
@@ -232,35 +228,6 @@ void BackendConsumer::HandleInterestingDecl(DeclGroupRef D) {
232228
HandleTopLevelDecl(D);
233229
}
234230

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

364331
// Link each LinkModule into our module.
365-
if (LinkInModules(getModule()))
332+
if (!CodeGenOpts.LinkBitcodePostopt && LinkInModules(getModule()))
366333
return;
367334

368335
for (auto &F : getModule()->functions()) {
@@ -1232,7 +1199,7 @@ void CodeGenAction::ExecuteAction() {
12321199
std::move(LinkModules), *VMContext, nullptr);
12331200

12341201
// Link in each pending link module.
1235-
if (Result.LinkInModules(&*TheModule))
1202+
if (!CodeGenOpts.LinkBitcodePostopt && Result.LinkInModules(&*TheModule))
12361203
return;
12371204

12381205
// 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)