@@ -127,7 +127,8 @@ static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
127
127
BuilderWrapper.IRGOpts .SanitizeCoverage ));
128
128
}
129
129
130
- std::tuple<llvm::TargetOptions, std::string, std::vector<std::string>>
130
+ std::tuple<llvm::TargetOptions, std::string, std::vector<std::string>,
131
+ std::string>
131
132
swift::getIRTargetOptions (IRGenOptions &Opts, ASTContext &Ctx) {
132
133
// Things that maybe we should collect from the command line:
133
134
// - relocation model
@@ -141,7 +142,7 @@ swift::getIRTargetOptions(IRGenOptions &Opts, ASTContext &Ctx) {
141
142
142
143
auto *Clang = static_cast <ClangImporter *>(Ctx.getClangModuleLoader ());
143
144
clang::TargetOptions &ClangOpts = Clang->getTargetInfo ().getTargetOpts ();
144
- return std::make_tuple (TargetOpts, ClangOpts.CPU , ClangOpts.Features );
145
+ return std::make_tuple (TargetOpts, ClangOpts.CPU , ClangOpts.Features , ClangOpts. Triple );
145
146
}
146
147
147
148
void setModuleFlags (IRGenModule &IGM) {
@@ -188,7 +189,7 @@ void swift::performLLVMOptimizations(IRGenOptions &Opts, llvm::Module *Module,
188
189
PMBuilder.addExtension (PassManagerBuilder::EP_EnabledOnOptLevel0,
189
190
addAddressSanitizerPasses);
190
191
}
191
-
192
+
192
193
if (Opts.Sanitizers & SanitizerKind::Thread) {
193
194
PMBuilder.addExtension (PassManagerBuilder::EP_OptimizerLast,
194
195
addThreadSanitizerPass);
@@ -520,24 +521,18 @@ bool swift::performLLVM(IRGenOptions &Opts, DiagnosticEngine *Diags,
520
521
521
522
std::unique_ptr<llvm::TargetMachine>
522
523
swift::createTargetMachine (IRGenOptions &Opts, ASTContext &Ctx) {
523
- const llvm::Triple &Triple = Ctx.LangOpts .Target ;
524
- std::string Error;
525
- const Target *Target = TargetRegistry::lookupTarget (Triple.str (), Error);
526
- if (!Target) {
527
- Ctx.Diags .diagnose (SourceLoc (), diag::no_llvm_target, Triple.str (), Error);
528
- return nullptr ;
529
- }
530
-
531
- CodeGenOpt::Level OptLevel = (Opts.shouldOptimize () ?
532
- CodeGenOpt::Default // -Os
533
- : CodeGenOpt::None);
524
+ CodeGenOpt::Level OptLevel = Opts.shouldOptimize ()
525
+ ? CodeGenOpt::Default // -Os
526
+ : CodeGenOpt::None;
534
527
535
528
// Set up TargetOptions and create the target features string.
536
529
TargetOptions TargetOpts;
537
530
std::string CPU;
531
+ std::string EffectiveClangTriple;
538
532
std::vector<std::string> targetFeaturesArray;
539
- std::tie (TargetOpts, CPU, targetFeaturesArray)
533
+ std::tie (TargetOpts, CPU, targetFeaturesArray, EffectiveClangTriple )
540
534
= getIRTargetOptions (Opts, Ctx);
535
+ const llvm::Triple &EffectiveTriple = llvm::Triple (EffectiveClangTriple);
541
536
std::string targetFeatures;
542
537
if (!targetFeaturesArray.empty ()) {
543
538
llvm::SubtargetFeatures features;
@@ -548,13 +543,23 @@ swift::createTargetMachine(IRGenOptions &Opts, ASTContext &Ctx) {
548
543
targetFeatures = features.getString ();
549
544
}
550
545
546
+ std::string Error;
547
+ const Target *Target =
548
+ TargetRegistry::lookupTarget (EffectiveTriple.str (), Error);
549
+ if (!Target) {
550
+ Ctx.Diags .diagnose (SourceLoc (), diag::no_llvm_target, EffectiveTriple.str (),
551
+ Error);
552
+ return nullptr ;
553
+ }
554
+
555
+
551
556
// Create a target machine.
552
- llvm::TargetMachine *TargetMachine =
553
- Target-> createTargetMachine (Triple .str (), CPU, targetFeatures, TargetOpts,
554
- Reloc::PIC_, None , OptLevel);
557
+ llvm::TargetMachine *TargetMachine = Target-> createTargetMachine (
558
+ EffectiveTriple .str (), CPU, targetFeatures, TargetOpts, Reloc::PIC_ ,
559
+ CodeModel::Default , OptLevel);
555
560
if (!TargetMachine) {
556
561
Ctx.Diags .diagnose (SourceLoc (), diag::no_llvm_target,
557
- Triple .str (), " no LLVM target machine" );
562
+ EffectiveTriple .str (), " no LLVM target machine" );
558
563
return nullptr ;
559
564
}
560
565
return std::unique_ptr<llvm::TargetMachine>(TargetMachine);
@@ -652,7 +657,7 @@ static void embedBitcode(llvm::Module *M, const IRGenOptions &Opts)
652
657
static void initLLVMModule (const IRGenModule &IGM) {
653
658
auto *Module = IGM.getModule ();
654
659
assert (Module && " Expected llvm:Module for IR generation!" );
655
-
660
+
656
661
Module->setTargetTriple (IGM.Triple .str ());
657
662
658
663
// Set the module's string representation.
@@ -731,7 +736,7 @@ static std::unique_ptr<llvm::Module> performIRGeneration(IRGenOptions &Opts,
731
736
732
737
// Run SIL level IRGen preparation passes.
733
738
runIRGenPreparePasses (*SILMod, IGM);
734
-
739
+
735
740
{
736
741
SharedTimer timer (" IRGen" );
737
742
// Emit the module contents.
@@ -879,7 +884,7 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
879
884
}
880
885
}
881
886
} _igmDeleter (irgen);
882
-
887
+
883
888
auto OutputIter = Opts.OutputFilenames .begin ();
884
889
bool IGMcreated = false ;
885
890
@@ -890,7 +895,7 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
890
895
auto nextSF = dyn_cast<SourceFile>(File);
891
896
if (!nextSF || nextSF->ASTStage < SourceFile::TypeChecked)
892
897
continue ;
893
-
898
+
894
899
// There must be an output filename for each source file.
895
900
// We ignore additional output filenames.
896
901
if (OutputIter == Opts.OutputFilenames .end ()) {
@@ -903,9 +908,9 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
903
908
if (!targetMachine) continue ;
904
909
905
910
// This (and the IGM itself) will get deleted by the IGMDeleter
906
- // as long as the IGM is registered with the IRGenerator.
911
+ // as long as the IGM is registered with the IRGenerator.
907
912
auto Context = new LLVMContext ();
908
-
913
+
909
914
// Create the IR emitter.
910
915
IRGenModule *IGM = new IRGenModule (irgen, std::move (targetMachine),
911
916
nextSF, *Context,
@@ -920,7 +925,7 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
920
925
DidRunSILCodeGenPreparePasses = true ;
921
926
}
922
927
}
923
-
928
+
924
929
if (!IGMcreated) {
925
930
// TODO: Check this already at argument parsing.
926
931
Ctx.Diags .diagnose (SourceLoc (), diag::no_input_files_for_mt);
@@ -929,7 +934,7 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
929
934
930
935
// Emit the module contents.
931
936
irgen.emitGlobalTopLevel ();
932
-
937
+
933
938
for (auto *File : M->getFiles ()) {
934
939
if (auto *SF = dyn_cast<SourceFile>(File)) {
935
940
IRGenModule *IGM = irgen.getGenModule (SF);
@@ -940,7 +945,7 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
940
945
});
941
946
}
942
947
}
943
-
948
+
944
949
// Okay, emit any definitions that we suddenly need.
945
950
irgen.emitLazyDefinitions ();
946
951
@@ -961,32 +966,32 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
961
966
962
967
// Emit symbols for eliminated dead methods.
963
968
PrimaryGM->emitVTableStubs ();
964
-
969
+
965
970
// Verify type layout if we were asked to.
966
971
if (!Opts.VerifyTypeLayoutNames .empty ())
967
972
PrimaryGM->emitTypeVerifier ();
968
-
973
+
969
974
std::for_each (Opts.LinkLibraries .begin (), Opts.LinkLibraries .end (),
970
975
[&](LinkLibrary linkLib) {
971
976
PrimaryGM->addLinkLibrary (linkLib);
972
977
});
973
-
978
+
974
979
// Hack to handle thunks eagerly synthesized by the Clang importer.
975
980
swift::ModuleDecl *prev = nullptr ;
976
981
for (auto external : Ctx.ExternalDefinitions ) {
977
982
swift::ModuleDecl *next = external->getModuleContext ();
978
983
if (next == prev)
979
984
continue ;
980
985
prev = next;
981
-
986
+
982
987
if (next->getName () == M->getName ())
983
988
continue ;
984
-
989
+
985
990
next->collectLinkLibraries ([&](LinkLibrary linkLib) {
986
991
PrimaryGM->addLinkLibrary (linkLib);
987
992
});
988
993
}
989
-
994
+
990
995
llvm::StringSet<> referencedGlobals;
991
996
992
997
for (auto it = irgen.begin (); it != irgen.end (); ++it) {
@@ -1014,7 +1019,7 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
1014
1019
for (auto it = irgen.begin (); it != irgen.end (); ++it) {
1015
1020
IRGenModule *IGM = it->second ;
1016
1021
llvm::Module *M = IGM->getModule ();
1017
-
1022
+
1018
1023
// Update the linkage of shared functions/globals.
1019
1024
// If a shared function/global is referenced from another file it must have
1020
1025
// weak instead of linkonce linkage. Otherwise LLVM would remove the
0 commit comments