diff options
author | Sanjay Patel <spatel@rotateright.com> | 2015-05-22 21:42:10 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2015-05-22 21:42:10 +0000 |
commit | f5019e60c76a248cdeb358d52ce8243f00e6d34d (patch) | |
tree | c346412f010a2701e549019fbfa0eba01b073bdd /clang/lib | |
parent | 236f9040d0f01391c45fd8fa0b0528a21a4ffec8 (diff) | |
download | bcm5719-llvm-f5019e60c76a248cdeb358d52ce8243f00e6d34d.tar.gz bcm5719-llvm-f5019e60c76a248cdeb358d52ce8243f00e6d34d.zip |
add the -mrecip driver flag and process its options
This is the front-end counterpart to D8982 (LLVM r238051).
The -mrecip option interface is based on maintaining compatibility with gcc:
https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/i386-and-x86-64-Options.html#index-mrecip_003dopt-1627
https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/RS_002f6000-and-PowerPC-Options.html#index-mrecip-2289
...while adding more functionality (allowing users to specify the number of refinement steps for each
estimate type).
Differential Revision: http://reviews.llvm.org/D8989
llvm-svn: 238055
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/CodeGen/BackendUtil.cpp | 3 | ||||
-rw-r--r-- | clang/lib/Driver/Tools.cpp | 134 | ||||
-rw-r--r-- | clang/lib/Frontend/CompilerInvocation.cpp | 2 |
3 files changed, 138 insertions, 1 deletions
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 30030050df0..ab41787e9a4 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -480,6 +480,9 @@ TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { llvm::TargetOptions Options; + if (!TargetOpts.Reciprocals.empty()) + Options.Reciprocals = TargetRecip(TargetOpts.Reciprocals); + Options.ThreadModel = llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel) .Case("posix", llvm::ThreadModel::POSIX) diff --git a/clang/lib/Driver/Tools.cpp b/clang/lib/Driver/Tools.cpp index 9b25cab9705..3166201a431 100644 --- a/clang/lib/Driver/Tools.cpp +++ b/clang/lib/Driver/Tools.cpp @@ -1629,6 +1629,138 @@ static void AddGoldPlugin(const ToolChain &ToolChain, const ArgList &Args, CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=mcpu=") + CPU)); } +/// This is a helper function for validating the optional refinement step +/// parameter in reciprocal argument strings. Return false if there is an error +/// parsing the refinement step. Otherwise, return true and set the Position +/// of the refinement step in the input string. +static bool getRefinementStep(const StringRef &In, const Driver &D, + const Arg &A, size_t &Position) { + const char RefinementStepToken = ':'; + Position = In.find(RefinementStepToken); + if (Position != StringRef::npos) { + StringRef Option = A.getOption().getName(); + StringRef RefStep = In.substr(Position + 1); + // Allow exactly one numeric character for the additional refinement + // step parameter. This is reasonable for all currently-supported + // operations and architectures because we would expect that a larger value + // of refinement steps would cause the estimate "optimization" to + // under-perform the native operation. Also, if the estimate does not + // converge quickly, it probably will not ever converge, so further + // refinement steps will not produce a better answer. + if (RefStep.size() != 1) { + D.Diag(diag::err_drv_invalid_value) << Option << RefStep; + return false; + } + char RefStepChar = RefStep[0]; + if (RefStepChar < '0' || RefStepChar > '9') { + D.Diag(diag::err_drv_invalid_value) << Option << RefStep; + return false; + } + } + return true; +} + +/// The -mrecip flag requires processing of many optional parameters. +static void ParseMRecip(const Driver &D, const ArgList &Args, + ArgStringList &OutStrings) { + static const char DisabledPrefixIn = '!'; + static const char DisabledPrefixOut = '!'; + static const char EnabledPrefixOut = '\0'; + StringRef Out = "-mrecip="; + + Arg *A = Args.getLastArg(options::OPT_mrecip, options::OPT_mrecip_EQ); + if (!A) + return; + + unsigned NumOptions = A->getNumValues(); + if (NumOptions == 0) { + // No option is the same as "all". + OutStrings.push_back(Args.MakeArgString(Out + "all")); + return; + } + + // Pass through "all", "none", or "default" with an optional refinement step. + if (NumOptions == 1) { + StringRef Val = A->getValue(0); + size_t RefStepLoc; + if (!getRefinementStep(Val, D, *A, RefStepLoc)) + return; + StringRef ValBase = Val.slice(0, RefStepLoc); + if (ValBase == "all" || ValBase == "none" || ValBase == "default") { + OutStrings.push_back(Args.MakeArgString(Out + Val)); + return; + } + } + + // Each reciprocal type may be enabled or disabled individually. + // Check each input value for validity, concatenate them all back together, + // and pass through. + + llvm::StringMap<bool> OptionStrings; + OptionStrings.insert(std::make_pair("divd", false)); + OptionStrings.insert(std::make_pair("divf", false)); + OptionStrings.insert(std::make_pair("vec-divd", false)); + OptionStrings.insert(std::make_pair("vec-divf", false)); + OptionStrings.insert(std::make_pair("sqrtd", false)); + OptionStrings.insert(std::make_pair("sqrtf", false)); + OptionStrings.insert(std::make_pair("vec-sqrtd", false)); + OptionStrings.insert(std::make_pair("vec-sqrtf", false)); + + for (unsigned i = 0; i != NumOptions; ++i) { + StringRef Val = A->getValue(i); + + bool IsDisabled = Val[0] == DisabledPrefixIn; + // Ignore the disablement token for string matching. + if (IsDisabled) + Val = Val.substr(1); + + size_t RefStep; + if (!getRefinementStep(Val, D, *A, RefStep)) + return; + + StringRef ValBase = Val.slice(0, RefStep); + llvm::StringMap<bool>::iterator OptionIter = OptionStrings.find(ValBase); + if (OptionIter == OptionStrings.end()) { + // Try again specifying float suffix. + OptionIter = OptionStrings.find(ValBase.str() + 'f'); + if (OptionIter == OptionStrings.end()) { + // The input name did not match any known option string. + D.Diag(diag::err_drv_unknown_argument) << Val; + return; + } + // The option was specified without a float or double suffix. + // Make sure that the double entry was not already specified. + // The float entry will be checked below. + if (OptionStrings[ValBase.str() + 'd']) { + D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; + return; + } + } + + if (OptionIter->second == true) { + // Duplicate option specified. + D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; + return; + } + + // Mark the matched option as found. Do not allow duplicate specifiers. + OptionIter->second = true; + + // If the precision was not specified, also mark the double entry as found. + if (ValBase.back() != 'f' && ValBase.back() != 'd') + OptionStrings[ValBase.str() + 'd'] = true; + + // Build the output string. + const char *Prefix = IsDisabled ? + &DisabledPrefixOut : &EnabledPrefixOut; + Out = Args.MakeArgString(Out + Prefix + Val); + if (i != NumOptions - 1) + Out = Args.MakeArgString(Out + ","); + } + + OutStrings.push_back(Args.MakeArgString(Out)); +} + static void getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple, const ArgList &Args, std::vector<const char *> &Features) { @@ -3142,6 +3274,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast")); } } + + ParseMRecip(getToolChain().getDriver(), Args, CmdArgs); // We separately look for the '-ffast-math' and '-ffinite-math-only' flags, // and if we find them, tell the frontend to provide the appropriate diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 8d3d31228a7..5f745d45359 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1846,7 +1846,7 @@ static void ParseTargetArgs(TargetOptions &Opts, ArgList &Args) { Opts.FeaturesAsWritten = Args.getAllArgValues(OPT_target_feature); Opts.LinkerVersion = Args.getLastArgValue(OPT_target_linker_version); Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple)); - + Opts.Reciprocals = Args.getAllArgValues(OPT_mrecip_EQ); // Use the default target triple if unspecified. if (Opts.Triple.empty()) Opts.Triple = llvm::sys::getDefaultTargetTriple(); |