Skip to content

Commit 87d57fd

Browse files
committed
Merge remote-tracking branch 'apple/master' into apple-master-llvm-swift5-transition
2 parents 9174079 + d3ad4ed commit 87d57fd

File tree

10 files changed

+120
-75
lines changed

10 files changed

+120
-75
lines changed

include/swift/Subsystems.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,9 @@ namespace swift {
248248
void serialize(ModuleOrSourceFile DC, const SerializationOptions &options,
249249
const SILModule *M = nullptr);
250250

251-
/// Get the CPU and subtarget feature options to use when emitting code.
252-
std::tuple<llvm::TargetOptions, std::string, std::vector<std::string>>
251+
/// Get the CPU, subtarget feature options, and triple to use when emitting code.
252+
std::tuple<llvm::TargetOptions, std::string, std::vector<std::string>,
253+
std::string>
253254
getIRTargetOptions(IRGenOptions &Opts, ASTContext &Ctx);
254255

255256
/// Turn the given Swift module into either LLVM IR or native code

lib/IRGen/IRGen.cpp

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
127127
BuilderWrapper.IRGOpts.SanitizeCoverage));
128128
}
129129

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>
131132
swift::getIRTargetOptions(IRGenOptions &Opts, ASTContext &Ctx) {
132133
// Things that maybe we should collect from the command line:
133134
// - relocation model
@@ -141,7 +142,7 @@ swift::getIRTargetOptions(IRGenOptions &Opts, ASTContext &Ctx) {
141142

142143
auto *Clang = static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
143144
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);
145146
}
146147

147148
void setModuleFlags(IRGenModule &IGM) {
@@ -188,7 +189,7 @@ void swift::performLLVMOptimizations(IRGenOptions &Opts, llvm::Module *Module,
188189
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
189190
addAddressSanitizerPasses);
190191
}
191-
192+
192193
if (Opts.Sanitizers & SanitizerKind::Thread) {
193194
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
194195
addThreadSanitizerPass);
@@ -520,24 +521,18 @@ bool swift::performLLVM(IRGenOptions &Opts, DiagnosticEngine *Diags,
520521

521522
std::unique_ptr<llvm::TargetMachine>
522523
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;
534527

535528
// Set up TargetOptions and create the target features string.
536529
TargetOptions TargetOpts;
537530
std::string CPU;
531+
std::string EffectiveClangTriple;
538532
std::vector<std::string> targetFeaturesArray;
539-
std::tie(TargetOpts, CPU, targetFeaturesArray)
533+
std::tie(TargetOpts, CPU, targetFeaturesArray, EffectiveClangTriple)
540534
= getIRTargetOptions(Opts, Ctx);
535+
const llvm::Triple &EffectiveTriple = llvm::Triple(EffectiveClangTriple);
541536
std::string targetFeatures;
542537
if (!targetFeaturesArray.empty()) {
543538
llvm::SubtargetFeatures features;
@@ -548,13 +543,23 @@ swift::createTargetMachine(IRGenOptions &Opts, ASTContext &Ctx) {
548543
targetFeatures = features.getString();
549544
}
550545

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+
551556
// 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);
555560
if (!TargetMachine) {
556561
Ctx.Diags.diagnose(SourceLoc(), diag::no_llvm_target,
557-
Triple.str(), "no LLVM target machine");
562+
EffectiveTriple.str(), "no LLVM target machine");
558563
return nullptr;
559564
}
560565
return std::unique_ptr<llvm::TargetMachine>(TargetMachine);
@@ -652,7 +657,7 @@ static void embedBitcode(llvm::Module *M, const IRGenOptions &Opts)
652657
static void initLLVMModule(const IRGenModule &IGM) {
653658
auto *Module = IGM.getModule();
654659
assert(Module && "Expected llvm:Module for IR generation!");
655-
660+
656661
Module->setTargetTriple(IGM.Triple.str());
657662

658663
// Set the module's string representation.
@@ -731,7 +736,7 @@ static std::unique_ptr<llvm::Module> performIRGeneration(IRGenOptions &Opts,
731736

732737
// Run SIL level IRGen preparation passes.
733738
runIRGenPreparePasses(*SILMod, IGM);
734-
739+
735740
{
736741
SharedTimer timer("IRGen");
737742
// Emit the module contents.
@@ -879,7 +884,7 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
879884
}
880885
}
881886
} _igmDeleter(irgen);
882-
887+
883888
auto OutputIter = Opts.OutputFilenames.begin();
884889
bool IGMcreated = false;
885890

@@ -890,7 +895,7 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
890895
auto nextSF = dyn_cast<SourceFile>(File);
891896
if (!nextSF || nextSF->ASTStage < SourceFile::TypeChecked)
892897
continue;
893-
898+
894899
// There must be an output filename for each source file.
895900
// We ignore additional output filenames.
896901
if (OutputIter == Opts.OutputFilenames.end()) {
@@ -903,9 +908,9 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
903908
if (!targetMachine) continue;
904909

905910
// 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.
907912
auto Context = new LLVMContext();
908-
913+
909914
// Create the IR emitter.
910915
IRGenModule *IGM = new IRGenModule(irgen, std::move(targetMachine),
911916
nextSF, *Context,
@@ -920,7 +925,7 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
920925
DidRunSILCodeGenPreparePasses = true;
921926
}
922927
}
923-
928+
924929
if (!IGMcreated) {
925930
// TODO: Check this already at argument parsing.
926931
Ctx.Diags.diagnose(SourceLoc(), diag::no_input_files_for_mt);
@@ -929,7 +934,7 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
929934

930935
// Emit the module contents.
931936
irgen.emitGlobalTopLevel();
932-
937+
933938
for (auto *File : M->getFiles()) {
934939
if (auto *SF = dyn_cast<SourceFile>(File)) {
935940
IRGenModule *IGM = irgen.getGenModule(SF);
@@ -940,7 +945,7 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
940945
});
941946
}
942947
}
943-
948+
944949
// Okay, emit any definitions that we suddenly need.
945950
irgen.emitLazyDefinitions();
946951

@@ -961,32 +966,32 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
961966

962967
// Emit symbols for eliminated dead methods.
963968
PrimaryGM->emitVTableStubs();
964-
969+
965970
// Verify type layout if we were asked to.
966971
if (!Opts.VerifyTypeLayoutNames.empty())
967972
PrimaryGM->emitTypeVerifier();
968-
973+
969974
std::for_each(Opts.LinkLibraries.begin(), Opts.LinkLibraries.end(),
970975
[&](LinkLibrary linkLib) {
971976
PrimaryGM->addLinkLibrary(linkLib);
972977
});
973-
978+
974979
// Hack to handle thunks eagerly synthesized by the Clang importer.
975980
swift::ModuleDecl *prev = nullptr;
976981
for (auto external : Ctx.ExternalDefinitions) {
977982
swift::ModuleDecl *next = external->getModuleContext();
978983
if (next == prev)
979984
continue;
980985
prev = next;
981-
986+
982987
if (next->getName() == M->getName())
983988
continue;
984-
989+
985990
next->collectLinkLibraries([&](LinkLibrary linkLib) {
986991
PrimaryGM->addLinkLibrary(linkLib);
987992
});
988993
}
989-
994+
990995
llvm::StringSet<> referencedGlobals;
991996

992997
for (auto it = irgen.begin(); it != irgen.end(); ++it) {
@@ -1014,7 +1019,7 @@ static void performParallelIRGeneration(IRGenOptions &Opts,
10141019
for (auto it = irgen.begin(); it != irgen.end(); ++it) {
10151020
IRGenModule *IGM = it->second;
10161021
llvm::Module *M = IGM->getModule();
1017-
1022+
10181023
// Update the linkage of shared functions/globals.
10191024
// If a shared function/global is referenced from another file it must have
10201025
// weak instead of linkonce linkage. Otherwise LLVM would remove the

lib/IRGen/IRGenModule.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
130130
ClangCodeGen(createClangCodeGenerator(Context, LLVMContext, irgen.Opts,
131131
ModuleName)),
132132
Module(*ClangCodeGen->GetModule()), LLVMContext(Module.getContext()),
133-
DataLayout(target->createDataLayout()), Triple(Context.LangOpts.Target),
134-
TargetMachine(std::move(target)), silConv(irgen.SIL),
135-
OutputFilename(OutputFilename),
133+
DataLayout(target->createDataLayout()),
134+
Triple(irgen.getEffectiveClangTriple()), TargetMachine(std::move(target)),
135+
silConv(irgen.SIL), OutputFilename(OutputFilename),
136136
TargetInfo(SwiftTargetInfo::get(*this)), DebugInfo(nullptr),
137137
ModuleHash(nullptr), ObjCInterop(Context.LangOpts.EnableObjCInterop),
138138
UseDarwinPreStableABIBit(Context.LangOpts.UseDarwinPreStableABIBit),
@@ -1120,3 +1120,10 @@ IRGenModule *IRGenerator::getGenModule(SILFunction *f) {
11201120

11211121
return getPrimaryIGM();
11221122
}
1123+
1124+
llvm::Triple IRGenerator::getEffectiveClangTriple() {
1125+
auto CI = static_cast<ClangImporter *>(
1126+
&*SIL.getASTContext().getClangModuleLoader());
1127+
assert(CI && "no clang module loader");
1128+
return llvm::Triple(CI->getTargetInfo().getTargetOpts().Triple);
1129+
}

lib/IRGen/IRGenModule.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ class IRGenerator {
389389
}
390390
return nullptr;
391391
}
392+
393+
/// Return the effective triple used by clang.
394+
llvm::Triple getEffectiveClangTriple();
392395
};
393396

394397
class ConstantReference {
@@ -427,7 +430,7 @@ class IRGenModule {
427430
llvm::Module &Module;
428431
llvm::LLVMContext &LLVMContext;
429432
const llvm::DataLayout DataLayout;
430-
const llvm::Triple &Triple;
433+
const llvm::Triple Triple;
431434
std::unique_ptr<llvm::TargetMachine> TargetMachine;
432435
ModuleDecl *getSwiftModule() const;
433436
Lowering::TypeConverter &getSILTypes() const;

lib/Immediate/Immediate.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,9 @@ int swift::RunImmediately(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
359359
std::string ErrorMsg;
360360
llvm::TargetOptions TargetOpt;
361361
std::string CPU;
362+
std::string Triple;
362363
std::vector<std::string> Features;
363-
std::tie(TargetOpt, CPU, Features)
364+
std::tie(TargetOpt, CPU, Features, Triple)
364365
= getIRTargetOptions(IRGenOpts, swiftModule->getASTContext());
365366
builder.setRelocationModel(llvm::Reloc::PIC_);
366367
builder.setTargetOptions(TargetOpt);

lib/Immediate/REPL.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,8 +989,9 @@ class REPLEnvironment {
989989
std::string ErrorMsg;
990990
llvm::TargetOptions TargetOpt;
991991
std::string CPU;
992+
std::string Triple;
992993
std::vector<std::string> Features;
993-
std::tie(TargetOpt, CPU, Features)
994+
std::tie(TargetOpt, CPU, Features, Triple)
994995
= getIRTargetOptions(IRGenOpts, CI.getASTContext());
995996

996997
builder.setRelocationModel(llvm::Reloc::PIC_);

0 commit comments

Comments
 (0)