1010//
1111// ===----------------------------------------------------------------------===//
1212
13- #ifdef IN_JITRT
14- #include " runtime/jit-rt/cpp-so/optimizer.h"
15- #include " runtime/jit-rt/cpp-so/valueparser.h"
16- #include " runtime/jit-rt/cpp-so/utils.h"
17- #endif
18-
1913#include " gen/optimizer.h"
20-
21- #ifndef IN_JITRT
22- #include " dmd/errors.h"
23- #include " gen/logger.h"
24- #endif
25-
2614#include " gen/passes/GarbageCollect2Stack.h"
2715#include " gen/passes/StripExternals.h"
2816#include " gen/passes/SimplifyDRuntimeCalls.h"
2917#include " gen/passes/Passes.h"
3018
31- #ifndef IN_JITRT
19+ #ifdef IN_JITRT
20+ #include " runtime/jit-rt/cpp-so/optimizer.h"
21+ #include " runtime/jit-rt/cpp-so/valueparser.h"
22+ #include " runtime/jit-rt/cpp-so/utils.h"
23+ #else
24+ #include " dmd/errors.h"
3225#include " driver/cl_options.h"
3326#include " driver/cl_options_instrumentation.h"
3427#include " driver/cl_options_sanitizers.h"
3528#include " driver/plugins.h"
3629#include " driver/targetmachine.h"
30+ #include " gen/logger.h"
3731#endif
3832
3933#if LDC_LLVM_VER < 1700
@@ -355,7 +349,9 @@ static void addGarbageCollect2StackPass(ModulePassManager &mpm,
355349 }
356350}
357351
352+ // Exclude these functions, that access cmdline options, from the JIT RT.
358353#ifndef IN_JITRT
354+
359355static llvm::Optional<PGOOptions> getPGOOptions () {
360356 // FIXME: Do we have these anywhere?
361357 bool debugInfoForProfiling = false ;
@@ -409,6 +405,44 @@ static llvm::Optional<PGOOptions> getPGOOptions() {
409405 return std::nullopt ;
410406#endif
411407}
408+
409+ // Imitate behavior of clang
410+ static bool shouldEmitRegularLTOSummary (llvm::Module const &M) {
411+ return opts::isUsingLTO () &&
412+ llvm::Triple (M.getTargetTriple ()).getVendor () != llvm::Triple::Apple;
413+ }
414+
415+ // / Check whether we should emit a flag for UnifiedLTO.
416+ // / The UnifiedLTO module flag should be set when UnifiedLTO is enabled for
417+ // / ThinLTO or Full LTO with module summaries.
418+ static bool shouldEmitUnifiedLTOModuleFlag (llvm::Module const &M) {
419+ return opts::ltoUnified &&
420+ (opts::prepareForThinLTO () || shouldEmitRegularLTOSummary (M));
421+ }
422+
423+ static void addLTOModuleFlags (llvm::Module *M) {
424+ if (opts::ltoFatObjects) {
425+ if (opts::prepareForThinLTO ()) {
426+ // TODO: implement support for EnableSplitLTOUnit
427+ // if (!M->getModuleFlag("EnableSplitLTOUnit"))
428+ // M->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
429+ // CodeGenOpts.EnableSplitLTOUnit);
430+ } else {
431+ if (shouldEmitRegularLTOSummary (*M)) {
432+ if (!M->getModuleFlag (" ThinLTO" ) && !opts::ltoUnified)
433+ M->addModuleFlag (llvm::Module::Error, " ThinLTO" , uint32_t (0 ));
434+ if (!M->getModuleFlag (" EnableSplitLTOUnit" ))
435+ M->addModuleFlag (llvm::Module::Error, " EnableSplitLTOUnit" ,
436+ uint32_t (1 ));
437+ }
438+ }
439+
440+ if (shouldEmitUnifiedLTOModuleFlag (*M) &&
441+ !M->getModuleFlag (" UnifiedLTO" ))
442+ M->addModuleFlag (llvm::Module::Error, " UnifiedLTO" , uint32_t (1 ));
443+ }
444+ }
445+
412446#endif // !IN_JITRT
413447
414448static PipelineTuningOptions getPipelineTuningOptions (unsigned optLevelVal, unsigned sizeLevelVal) {
@@ -585,12 +619,11 @@ void runOptimizationPasses(llvm::Module *M, llvm::TargetMachine *TM) {
585619 mpm = pb.buildO0DefaultPipeline (level, ltoPrelink);
586620#if LDC_LLVM_VER >= 1700
587621 } else if (opts::ltoFatObjects && opts::isUsingLTO ()) {
588- mpm = pb.buildFatLTODefaultPipeline (level,
589- opts::isUsingThinLTO (),
590- opts::isUsingThinLTO ()
591- );
622+ mpm = pb.buildFatLTODefaultPipeline (level, opts::prepareForThinLTO (),
623+ opts::prepareForThinLTO () ||
624+ shouldEmitRegularLTOSummary (*M));
592625#endif
593- } else if (opts::isUsingThinLTO ()) {
626+ } else if (opts::prepareForThinLTO ()) {
594627 mpm = pb.buildThinLTOPreLinkDefaultPipeline (level);
595628 } else if (opts::isUsingLTO ()) {
596629 mpm = pb.buildLTOPreLinkDefaultPipeline (level);
@@ -599,6 +632,9 @@ void runOptimizationPasses(llvm::Module *M, llvm::TargetMachine *TM) {
599632 mpm = pb.buildPerModuleDefaultPipeline (level);
600633 }
601634
635+ #ifndef IN_JITRT
636+ addLTOModuleFlags (M);
637+ #endif
602638
603639 mpm.run (*M,mam);
604640}
0 commit comments