7
7
// ===----------------------------------------------------------------------===//
8
8
9
9
#include " clang/CodeGen/BackendUtil.h"
10
+ #include " BackendConsumer.h"
11
+ #include " LinkInModulesPass.h"
10
12
#include " clang/Basic/CodeGenOptions.h"
11
13
#include " clang/Basic/Diagnostic.h"
12
14
#include " clang/Basic/LangOptions.h"
@@ -133,11 +135,6 @@ static cl::opt<PGOOptions::ColdFuncOpt> ClPGOColdFuncAttr(
133
135
134
136
extern cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate;
135
137
136
- // Re-link builtin bitcodes after optimization
137
- cl::opt<bool > ClRelinkBuiltinBitcodePostop (
138
- " relink-builtin-bitcode-postop" , cl::Optional,
139
- cl::desc (" Re-link builtin bitcodes after optimization." ), cl::init(false ));
140
-
141
138
static cl::opt<bool > SYCLNativeCPUBackend (
142
139
" sycl-native-cpu-backend" , cl::init(false ),
143
140
cl::desc(" Re-link builtin bitcodes after optimization." ));
@@ -158,7 +155,7 @@ class EmitAssemblyHelper {
158
155
const CodeGenOptions &CodeGenOpts;
159
156
const clang::TargetOptions &TargetOpts;
160
157
const LangOptions &LangOpts;
161
- Module *TheModule;
158
+ llvm:: Module *TheModule;
162
159
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS;
163
160
164
161
Timer CodeGenerationTime;
@@ -201,10 +198,9 @@ class EmitAssemblyHelper {
201
198
return F;
202
199
}
203
200
204
- void
205
- RunOptimizationPipeline (BackendAction Action,
206
- std::unique_ptr<raw_pwrite_stream> &OS,
207
- std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS);
201
+ void RunOptimizationPipeline (
202
+ BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
203
+ std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC);
208
204
void RunCodegenPipeline (BackendAction Action,
209
205
std::unique_ptr<raw_pwrite_stream> &OS,
210
206
std::unique_ptr<llvm::ToolOutputFile> &DwoOS);
@@ -232,7 +228,7 @@ class EmitAssemblyHelper {
232
228
const HeaderSearchOptions &HeaderSearchOpts,
233
229
const CodeGenOptions &CGOpts,
234
230
const clang::TargetOptions &TOpts,
235
- const LangOptions &LOpts, Module *M,
231
+ const LangOptions &LOpts, llvm:: Module *M,
236
232
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
237
233
: Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
238
234
TargetOpts (TOpts), LangOpts(LOpts), TheModule(M), VFS(std::move(VFS)),
@@ -247,8 +243,8 @@ class EmitAssemblyHelper {
247
243
std::unique_ptr<TargetMachine> TM;
248
244
249
245
// Emit output using the new pass manager for the optimization pipeline.
250
- void EmitAssembly (BackendAction Action,
251
- std::unique_ptr<raw_pwrite_stream> OS );
246
+ void EmitAssembly (BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS,
247
+ BackendConsumer *BC );
252
248
};
253
249
} // namespace
254
250
@@ -731,7 +727,7 @@ static void addSanitizers(const Triple &TargetTriple,
731
727
// the logic of the original code, but operates on "shadow" values. It
732
728
// can benefit from re-running some general purpose optimization
733
729
// passes.
734
- MPM.addPass (RequireAnalysisPass<GlobalsAA, Module>());
730
+ MPM.addPass (RequireAnalysisPass<GlobalsAA, llvm:: Module>());
735
731
FunctionPassManager FPM;
736
732
FPM.addPass (EarlyCSEPass (true /* Enable mem-ssa. */ ));
737
733
FPM.addPass (InstCombinePass ());
@@ -790,7 +786,7 @@ static void addSanitizers(const Triple &TargetTriple,
790
786
SanitizersCallback (NewMPM, Level);
791
787
if (!NewMPM.isEmpty ()) {
792
788
// Sanitizers can abandon<GlobalsAA>.
793
- NewMPM.addPass (RequireAnalysisPass<GlobalsAA, Module>());
789
+ NewMPM.addPass (RequireAnalysisPass<GlobalsAA, llvm:: Module>());
794
790
MPM.addPass (std::move (NewMPM));
795
791
}
796
792
});
@@ -812,7 +808,7 @@ static void addSanitizers(const Triple &TargetTriple,
812
808
813
809
void EmitAssemblyHelper::RunOptimizationPipeline (
814
810
BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
815
- std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS) {
811
+ std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC ) {
816
812
std::optional<PGOOptions> PGOOpt;
817
813
818
814
if (CodeGenOpts.hasProfileIRInstr ())
@@ -1163,6 +1159,10 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
1163
1159
}
1164
1160
}
1165
1161
1162
+ // Link against bitcodes supplied via the -mlink-builtin-bitcode option
1163
+ if (CodeGenOpts.LinkBitcodePostopt )
1164
+ MPM.addPass (LinkInModulesPass (BC, false ));
1165
+
1166
1166
// Add a verifier pass if requested. We don't have to do this if the action
1167
1167
// requires code generation because there will already be a verifier pass in
1168
1168
// the code-generation pipeline.
@@ -1175,7 +1175,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
1175
1175
CodeGenOpts.FatLTO ) {
1176
1176
if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses ) {
1177
1177
if (!TheModule->getModuleFlag (" EnableSplitLTOUnit" ))
1178
- TheModule->addModuleFlag (Module::Error, " EnableSplitLTOUnit" ,
1178
+ TheModule->addModuleFlag (llvm:: Module::Error, " EnableSplitLTOUnit" ,
1179
1179
CodeGenOpts.EnableSplitLTOUnit );
1180
1180
if (Action == Backend_EmitBC) {
1181
1181
if (!CodeGenOpts.ThinLinkBitcodeFile .empty ()) {
@@ -1195,9 +1195,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
1195
1195
bool EmitLTOSummary = shouldEmitRegularLTOSummary ();
1196
1196
if (EmitLTOSummary) {
1197
1197
if (!TheModule->getModuleFlag (" ThinLTO" ) && !CodeGenOpts.UnifiedLTO )
1198
- TheModule->addModuleFlag (Module::Error, " ThinLTO" , uint32_t (0 ));
1198
+ TheModule->addModuleFlag (llvm:: Module::Error, " ThinLTO" , uint32_t (0 ));
1199
1199
if (!TheModule->getModuleFlag (" EnableSplitLTOUnit" ))
1200
- TheModule->addModuleFlag (Module::Error, " EnableSplitLTOUnit" ,
1200
+ TheModule->addModuleFlag (llvm:: Module::Error, " EnableSplitLTOUnit" ,
1201
1201
uint32_t (1 ));
1202
1202
}
1203
1203
if (Action == Backend_EmitBC) {
@@ -1280,7 +1280,8 @@ void EmitAssemblyHelper::RunCodegenPipeline(
1280
1280
}
1281
1281
1282
1282
void EmitAssemblyHelper::EmitAssembly (BackendAction Action,
1283
- std::unique_ptr<raw_pwrite_stream> OS) {
1283
+ std::unique_ptr<raw_pwrite_stream> OS,
1284
+ BackendConsumer *BC) {
1284
1285
TimeRegion Region (CodeGenOpts.TimePasses ? &CodeGenerationTime : nullptr );
1285
1286
setCommandLineOpts (CodeGenOpts);
1286
1287
@@ -1296,7 +1297,7 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
1296
1297
cl::PrintOptionValues ();
1297
1298
1298
1299
std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
1299
- RunOptimizationPipeline (Action, OS, ThinLinkOS);
1300
+ RunOptimizationPipeline (Action, OS, ThinLinkOS, BC );
1300
1301
RunCodegenPipeline (Action, OS, DwoOS);
1301
1302
1302
1303
if (ThinLinkOS)
@@ -1306,11 +1307,12 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
1306
1307
}
1307
1308
1308
1309
static void runThinLTOBackend (
1309
- DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex, Module *M,
1310
- const HeaderSearchOptions &HeaderOpts, const CodeGenOptions &CGOpts,
1311
- const clang::TargetOptions &TOpts, const LangOptions &LOpts,
1312
- std::unique_ptr<raw_pwrite_stream> OS, std::string SampleProfile,
1313
- std::string ProfileRemapping, BackendAction Action) {
1310
+ DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex,
1311
+ llvm::Module *M, const HeaderSearchOptions &HeaderOpts,
1312
+ const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts,
1313
+ const LangOptions &LOpts, std::unique_ptr<raw_pwrite_stream> OS,
1314
+ std::string SampleProfile, std::string ProfileRemapping,
1315
+ BackendAction Action) {
1314
1316
DenseMap<StringRef, DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
1315
1317
ModuleToDefinedGVSummaries;
1316
1318
CombinedIndex->collectDefinedGVSummariesPerModule (ModuleToDefinedGVSummaries);
@@ -1379,18 +1381,18 @@ static void runThinLTOBackend(
1379
1381
Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput ;
1380
1382
switch (Action) {
1381
1383
case Backend_EmitNothing:
1382
- Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1384
+ Conf.PreCodeGenModuleHook = [](size_t Task, const llvm:: Module &Mod) {
1383
1385
return false ;
1384
1386
};
1385
1387
break ;
1386
1388
case Backend_EmitLL:
1387
- Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1389
+ Conf.PreCodeGenModuleHook = [&](size_t Task, const llvm:: Module &Mod) {
1388
1390
M->print (*OS, nullptr , CGOpts.EmitLLVMUseLists );
1389
1391
return false ;
1390
1392
};
1391
1393
break ;
1392
1394
case Backend_EmitBC:
1393
- Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1395
+ Conf.PreCodeGenModuleHook = [&](size_t Task, const llvm:: Module &Mod) {
1394
1396
WriteBitcodeToFile (*M, *OS, CGOpts.EmitLLVMUseLists );
1395
1397
return false ;
1396
1398
};
@@ -1409,14 +1411,12 @@ static void runThinLTOBackend(
1409
1411
}
1410
1412
}
1411
1413
1412
- void clang::EmitBackendOutput (DiagnosticsEngine &Diags,
1413
- const HeaderSearchOptions &HeaderOpts,
1414
- const CodeGenOptions &CGOpts,
1415
- const clang::TargetOptions &TOpts,
1416
- const LangOptions &LOpts, StringRef TDesc,
1417
- Module *M, BackendAction Action,
1418
- IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
1419
- std::unique_ptr<raw_pwrite_stream> OS) {
1414
+ void clang::EmitBackendOutput (
1415
+ DiagnosticsEngine &Diags, const HeaderSearchOptions &HeaderOpts,
1416
+ const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts,
1417
+ const LangOptions &LOpts, StringRef TDesc, llvm::Module *M,
1418
+ BackendAction Action, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
1419
+ std::unique_ptr<raw_pwrite_stream> OS, BackendConsumer *BC) {
1420
1420
1421
1421
llvm::TimeTraceScope TimeScope (" Backend" );
1422
1422
@@ -1459,7 +1459,7 @@ void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
1459
1459
}
1460
1460
1461
1461
EmitAssemblyHelper AsmHelper (Diags, HeaderOpts, CGOpts, TOpts, LOpts, M, VFS);
1462
- AsmHelper.EmitAssembly (Action, std::move (OS));
1462
+ AsmHelper.EmitAssembly (Action, std::move (OS), BC );
1463
1463
1464
1464
// Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1465
1465
// DataLayout.
0 commit comments