Do all target info management in Clang, and drop -target-abi / -target-feature.

Previously we hardcoded a few important default CPUs, ABIs, and features into
Swift's driver, duplicating work in Clang. Now that we're using Clang's
driver to create the Clang "sub-compiler", we can delegate this work to Clang.

As part of this, I've dropped the options for -target-abi (which was a
frontend-only option anyway) and -target-feature (which was a hidden driver
option and is a frontend-only option in /Clang/). We can revisit this later
if it becomes interesting. I left in -target-cpu, which is now mapped
directly to Clang's -mcpu=.

Swift SVN r22449
This commit is contained in:
Jordan Rose
2014-10-01 23:55:40 +00:00
parent 30c65eff7e
commit 49a6c8eb7b
11 changed files with 43 additions and 128 deletions

View File

@@ -51,10 +51,6 @@ public:
std::string Triple; std::string Triple;
// The command line string that is to be stored in the DWARF debug info. // The command line string that is to be stored in the DWARF debug info.
std::string DWARFDebugFlags; std::string DWARFDebugFlags;
// The CPU and features.
std::string TargetCPU;
std::vector<std::string> TargetFeatures;
std::string TargetABI;
/// The libraries and frameworks specified on the command line. /// The libraries and frameworks specified on the command line.
SmallVector<LinkLibrary, 4> LinkLibraries; SmallVector<LinkLibrary, 4> LinkLibraries;

View File

@@ -30,6 +30,11 @@ public:
/// A directory for overriding Clang's resource directory. /// A directory for overriding Clang's resource directory.
std::string OverrideResourceDir; std::string OverrideResourceDir;
/// The target CPU to compile for.
///
/// Equivalent to Clang's -mcpu=.
std::string TargetCPU;
/// If true, matched getter-like and setter-like methods will be imported as /// If true, matched getter-like and setter-like methods will be imported as
/// properties. /// properties.
bool InferImplicitProperties = false; bool InferImplicitProperties = false;

View File

@@ -18,9 +18,6 @@ let Flags = [FrontendOption, NoDriverOption] in {
def triple : Separate<["-"], "triple">, Alias<target>; def triple : Separate<["-"], "triple">, Alias<target>;
def target_abi : Separate<["-"], "target-abi">,
HelpText<"Target a particular ABI">;
def color_diagnostics : Flag<["-"], "color-diagnostics">, def color_diagnostics : Flag<["-"], "color-diagnostics">,
HelpText<"Print diagnostics in color">; HelpText<"Print diagnostics in color">;

View File

@@ -339,9 +339,5 @@ def target_legacy_spelling : Joined<["--"], "target=">,
def target_cpu : Separate<["-"], "target-cpu">, Flags<[FrontendOption]>, def target_cpu : Separate<["-"], "target-cpu">, Flags<[FrontendOption]>,
HelpText<"Generate code for a particular CPU variant">; HelpText<"Generate code for a particular CPU variant">;
def target_feature : Separate<["-"], "target-feature">,
Flags<[FrontendOption, HelpHidden]>,
HelpText<"Generate code with a particular CPU feature enabled or disabled">,
MetaVarName<"[+-]<feature-name>">;
include "FrontendOptions.td" include "FrontendOptions.td"

View File

@@ -218,8 +218,6 @@ ClangImporter::create(ASTContext &ctx,
"-x", "objective-c", "-std=gnu11", "-fobjc-arc", "-fmodules", "-fblocks", "-x", "objective-c", "-std=gnu11", "-fobjc-arc", "-fmodules", "-fblocks",
"-fsyntax-only", "-w", "-femit-all-decls", "-fsyntax-only", "-w", "-femit-all-decls",
"-target", irGenOpts.Triple, "-target", irGenOpts.Triple,
// "-target-cpu", irGenOpts.TargetCPU,
// "-target-abi", irGenOpts.TargetABI,
SHIMS_INCLUDE_FLAG, searchPathOpts.RuntimeResourcePath, SHIMS_INCLUDE_FLAG, searchPathOpts.RuntimeResourcePath,
"-DSWIFT_CLASS_EXTRA=__attribute__((annotate(\"" "-DSWIFT_CLASS_EXTRA=__attribute__((annotate(\""
SWIFT_NATIVE_ANNOTATION_STRING "\")))", SWIFT_NATIVE_ANNOTATION_STRING "\")))",
@@ -260,6 +258,18 @@ ClangImporter::create(ASTContext &ctx,
invocationArgStrs.push_back("-fapplication-extension"); invocationArgStrs.push_back("-fapplication-extension");
} }
if (!importerOpts.TargetCPU.empty()) {
invocationArgStrs.push_back("-mcpu=" + importerOpts.TargetCPU);
} else if (triple.isOSDarwin()) {
// Special case: arm64 defaults to the "cyclone" CPU for Darwin,
// but Clang only detects this if we use -arch.
if (triple.getArch() == llvm::Triple::aarch64 ||
triple.getArch() == llvm::Triple::aarch64_be) {
invocationArgStrs.push_back("-mcpu=cyclone");
}
}
if (searchPathOpts.SDKPath.empty()) { if (searchPathOpts.SDKPath.empty()) {
invocationArgStrs.push_back("-Xclang"); invocationArgStrs.push_back("-Xclang");
invocationArgStrs.push_back("-nostdsysteminc"); invocationArgStrs.push_back("-nostdsysteminc");
@@ -312,11 +322,6 @@ ClangImporter::create(ASTContext &ctx,
invocationArgStrs.push_back(overrideResourceDir); invocationArgStrs.push_back(overrideResourceDir);
} }
for (auto &feature : irGenOpts.TargetFeatures) {
invocationArgStrs.push_back("-target-feature");
invocationArgStrs.push_back(feature);
}
for (auto extraArg : importerOpts.ExtraArgs) { for (auto extraArg : importerOpts.ExtraArgs) {
invocationArgStrs.push_back(extraArg); invocationArgStrs.push_back(extraArg);
} }

View File

@@ -73,80 +73,6 @@ static void addPrimaryInputsOfType(ArgStringList &Arguments, const Job *J,
} }
} }
// FIXME: This should use the logic in clang::driver::Clang::ConstructJob.
static void configureDefaultCPU(const llvm::Triple &triple,
ArgStringList &args) {
switch (triple.getArch()) {
case llvm::Triple::aarch64:
case llvm::Triple::aarch64_be:
if (!triple.isOSDarwin())
return;
args.push_back("-target-cpu");
args.push_back("cyclone");
args.push_back("-target-feature");
args.push_back("+neon");
break;
case llvm::Triple::arm:
case llvm::Triple::armeb:
case llvm::Triple::thumb:
case llvm::Triple::thumbeb:
if (auto CPUStr = triple.getARMCPUForArch()) {
args.push_back("-target-cpu");
args.push_back(CPUStr);
}
break;
case llvm::Triple::x86:
case llvm::Triple::x86_64:
if (triple.isOSDarwin()) {
args.push_back("-target-cpu");
if (triple.getArchName() == "x86_64h") {
args.push_back("core-avx2");
args.push_back("-target-feature");
args.push_back("-rdrnd,-aes,-pclmul,-rtm,-hle,-fsgsbase");
} else {
bool is64Bit = (triple.getArch() == llvm::Triple::x86_64);
args.push_back(is64Bit ? "core2" : "yonah");
}
}
break;
default:
break;
}
}
// Configure the ABI default
static void configureDefaultABI(const llvm::Triple &triple,
ArgStringList &args) {
if (!triple.isOSDarwin())
return;
const char *ABIName = nullptr;
switch (triple.getArch()) {
case llvm::Triple::aarch64:
ABIName = "darwinpcs";
break;
case llvm::Triple::arm:
case llvm::Triple::armeb:
case llvm::Triple::thumb:
case llvm::Triple::thumbeb:
// FIXME: We should use aapcs for M-class processors, EABI,
// etc. See Clang's driver. For current targets we just
// need to support apcs-gnu.
ABIName = "apcs-gnu";
break;
default:
// We do not need to specify a -target-abi for other architectures
// we currently care about.
return;
}
args.push_back("-target-abi");
args.push_back(ABIName);
}
/// Handle arguments common to all invocations of the frontend (compilation, /// Handle arguments common to all invocations of the frontend (compilation,
/// module-merging, LLDB's REPL, etc). /// module-merging, LLDB's REPL, etc).
static void addCommonFrontendArgs(const ToolChain &TC, static void addCommonFrontendArgs(const ToolChain &TC,
@@ -168,12 +94,6 @@ static void addCommonFrontendArgs(const ToolChain &TC,
// Handle the CPU and its preferences. // Handle the CPU and its preferences.
if (auto arg = inputArgs.getLastArg(options::OPT_target_cpu)) if (auto arg = inputArgs.getLastArg(options::OPT_target_cpu))
arg->render(inputArgs, arguments); arg->render(inputArgs, arguments);
else
configureDefaultCPU(Triple, arguments);
inputArgs.AddAllArgs(arguments, options::OPT_target_feature);
// Default the ABI based on the triple.
configureDefaultABI(Triple, arguments);
if (!OI.SDKPath.empty()) { if (!OI.SDKPath.empty()) {
arguments.push_back("-sdk"); arguments.push_back("-sdk");

View File

@@ -668,6 +668,9 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts, ArgList &Args,
Opts.ModuleCachePath = A->getValue(); Opts.ModuleCachePath = A->getValue();
} }
if (const Arg *A = Args.getLastArg(OPT_target_cpu))
Opts.TargetCPU = A->getValue();
for (const Arg *A : make_range(Args.filtered_begin(OPT_Xcc), for (const Arg *A : make_range(Args.filtered_begin(OPT_Xcc),
Args.filtered_end())) { Args.filtered_end())) {
Opts.ExtraArgs.push_back(A->getValue()); Opts.ExtraArgs.push_back(A->getValue());
@@ -906,16 +909,6 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
Opts.LinkLibraries.push_back(LinkLibrary(A->getValue(), Kind)); Opts.LinkLibraries.push_back(LinkLibrary(A->getValue(), Kind));
} }
if (const Arg *A = Args.getLastArg(OPT_target_cpu))
Opts.TargetCPU = A->getValue();
if (const Arg *A = Args.getLastArg(OPT_target_abi))
Opts.TargetABI = A->getValue();
for (const Arg *A : make_range(Args.filtered_begin(OPT_target_feature),
Args.filtered_end())) {
Opts.TargetFeatures.push_back(A->getValue());
}
Opts.DisableLLVMOptzns |= Args.hasArg(OPT_disable_llvm_optzns); Opts.DisableLLVMOptzns |= Args.hasArg(OPT_disable_llvm_optzns);
Opts.DisableLLVMARCOpts |= Args.hasArg(OPT_disable_llvm_arc_opts); Opts.DisableLLVMARCOpts |= Args.hasArg(OPT_disable_llvm_arc_opts);
Opts.EnableDynamicValueTypeLayout |= Opts.EnableDynamicValueTypeLayout |=

View File

@@ -23,6 +23,7 @@
#include "swift/Basic/Platform.h" #include "swift/Basic/Platform.h"
#include "swift/ClangImporter/ClangImporter.h" #include "swift/ClangImporter/ClangImporter.h"
#include "swift/LLVMPasses/PassesFwd.h" #include "swift/LLVMPasses/PassesFwd.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/Bitcode/BitcodeWriterPass.h" #include "llvm/Bitcode/BitcodeWriterPass.h"
#include "llvm/Bitcode/ReaderWriter.h" #include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/IR/DataLayout.h" #include "llvm/IR/DataLayout.h"
@@ -93,25 +94,29 @@ static std::unique_ptr<llvm::Module> performIRGeneration(IRGenOptions &Opts,
CodeGenOpt::Level OptLevel = Opts.Optimize ? CodeGenOpt::Aggressive CodeGenOpt::Level OptLevel = Opts.Optimize ? CodeGenOpt::Aggressive
: CodeGenOpt::None; : CodeGenOpt::None;
auto *Clang = static_cast<ClangImporter *>(M->Ctx.getClangModuleLoader());
clang::TargetOptions &ClangOpts = Clang->getTargetInfo().getTargetOpts();
// Set up TargetOptions. // Set up TargetOptions.
// Things that maybe we should collect from the command line: // Things that maybe we should collect from the command line:
// - relocation model // - relocation model
// - code model // - code model
// FIXME: We should do this entirely through Clang, for consistency.
TargetOptions TargetOpts; TargetOptions TargetOpts;
TargetOpts.NoFramePointerElim = Opts.DisableFPElim; TargetOpts.NoFramePointerElim = Opts.DisableFPElim;
// Create the target features string. // Create the target features string.
std::string targetFeatures; std::string targetFeatures;
if (!Opts.TargetFeatures.empty()) { if (!ClangOpts.Features.empty()) {
llvm::SubtargetFeatures features; llvm::SubtargetFeatures features;
for (std::string &feature : Opts.TargetFeatures) for (std::string &feature : ClangOpts.Features)
features.AddFeature(feature); features.AddFeature(feature);
targetFeatures = features.getString(); targetFeatures = features.getString();
} }
// Create a target machine. // Create a target machine.
llvm::TargetMachine *TargetMachine llvm::TargetMachine *TargetMachine
= Target->createTargetMachine(Opts.Triple, Opts.TargetCPU, = Target->createTargetMachine(Opts.Triple, ClangOpts.CPU,
std::move(targetFeatures), std::move(targetFeatures),
TargetOpts, Reloc::PIC_, TargetOpts, Reloc::PIC_,
CodeModel::Default, OptLevel); CodeModel::Default, OptLevel);

View File

@@ -17,7 +17,7 @@
// LLDB: lldb{{"?}} "--repl= // LLDB: lldb{{"?}} "--repl=
// LLDB-NOT: -module-name // LLDB-NOT: -module-name
// LLDB: -target {{[^ ]+}} // LLDB: -target {{[^ "]+}}
// LLDB-NOT: -module-name // LLDB-NOT: -module-name
// LLDB: " // LLDB: "

View File

@@ -57,22 +57,6 @@
// RUN: not %swiftc_driver -import-objc-header fake.h -import-underlying-module -c %s 2>&1 | FileCheck -check-prefix=FRAMEWORK_BRIDGING_HEADER %s // RUN: not %swiftc_driver -import-objc-header fake.h -import-underlying-module -c %s 2>&1 | FileCheck -check-prefix=FRAMEWORK_BRIDGING_HEADER %s
// FRAMEWORK_BRIDGING_HEADER: error: using bridging headers with framework targets is unsupported // FRAMEWORK_BRIDGING_HEADER: error: using bridging headers with framework targets is unsupported
// RUN: %swiftc_driver -target arm64-apple-ios7 -### %s | FileCheck -check-prefix=TARGETCPU1 %s
// RUN: %swift_driver -target arm64-apple-ios7 -### | FileCheck -check-prefix=TARGETCPU1 %s
// TARGETCPU1: -target-cpu cyclone
// RUN: %swiftc_driver -target armv7s-apple-ios7 -### %s | FileCheck -check-prefix=TARGETCPU2 %s
// TARGETCPU2: -target-cpu swift
// RUN: %swiftc_driver -target armv7-apple-ios7 -### %s | FileCheck -check-prefix=TARGETCPU3 %s
// TARGETCPU3: -target-cpu cortex-a8
// RUN: %swiftc_driver -target i386-apple-ios7 -### %s | FileCheck -check-prefix=SIMULATOR_CPU %s
// SIMULATOR_CPU: -target-cpu yonah
// RUN: %swiftc_driver -target x86_64-apple-ios7 -### %s | FileCheck -check-prefix=SIMULATOR64_CPU %s
// SIMULATOR64_CPU: -target-cpu core2
// RUN: %swift_driver -### | FileCheck -check-prefix=DEFAULT_REPL %s // RUN: %swift_driver -### | FileCheck -check-prefix=DEFAULT_REPL %s
// DEFAULT_REPL: -repl // DEFAULT_REPL: -repl
// RUN: not %swiftc_driver 2>&1 | FileCheck -check-prefix=DEFAULT_EXEC_ERR %s // RUN: not %swiftc_driver 2>&1 | FileCheck -check-prefix=DEFAULT_EXEC_ERR %s

View File

@@ -0,0 +1,14 @@
// RUN: not %swift -parse -target arm64-apple-ios7 -Xcc -### %s 2>&1 | FileCheck -check-prefix=TARGETCPU1 %s
// TARGETCPU1: "-target-cpu" "cyclone"
// RUN: not %swift -parse -target armv7s-apple-ios7 -Xcc -### %s 2>&1 | FileCheck -check-prefix=TARGETCPU2 %s
// TARGETCPU2: "-target-cpu" "swift"
// RUN: not %swift -parse -target armv7-apple-ios7 -Xcc -### %s 2>&1 | FileCheck -check-prefix=TARGETCPU3 %s
// TARGETCPU3: "-target-cpu" "cortex-a8"
// RUN: not %swift -parse -target i386-apple-ios7 -Xcc -### %s 2>&1 | FileCheck -check-prefix=SIMULATOR_CPU %s
// SIMULATOR_CPU: "-target-cpu" "yonah"
// RUN: not %swift -parse -target x86_64-apple-ios7 -Xcc -### %s 2>&1 | FileCheck -check-prefix=SIMULATOR64_CPU %s
// SIMULATOR64_CPU: "-target-cpu" "core2"