Skip to content

Commit

Permalink
Retry: [Driver] Compute effective target triples once per job (NFCI)
Browse files Browse the repository at this point in the history
Compute an effective triple once per job. Cache the triple in the
prevailing ToolChain for the duration of the job.

Clients which need effective triples now look them up in the ToolChain.
This eliminates wasteful re-computation of effective triples (e.g in
getARMFloatABI()).

While we're at it, delete MachO::ComputeEffectiveClangTriple. It was a
no-op override.

Differential Revision: https://reviews.llvm.org/D22596

llvm-svn: 276937
  • Loading branch information
vedantk committed Jul 27, 2016
1 parent 5fb00e4 commit 18286cf
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 26 deletions.
28 changes: 28 additions & 0 deletions clang/include/clang/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace driver {
class Compilation;
class Driver;
class JobAction;
class RegisterEffectiveTriple;
class SanitizerArgs;
class Tool;

Expand Down Expand Up @@ -91,6 +92,14 @@ class ToolChain {

mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;

/// The effective clang triple for the current Job.
mutable llvm::Triple EffectiveTriple;

/// Set the toolchain's effective clang triple.
void setEffectiveTriple(llvm::Triple ET) const { EffectiveTriple = ET; }

friend class RegisterEffectiveTriple;

protected:
MultilibSet Multilibs;
const char *DefaultLinker = "ld";
Expand Down Expand Up @@ -141,6 +150,12 @@ class ToolChain {
return Triple.getTriple();
}

/// Get the toolchain's effective clang triple.
const llvm::Triple &getEffectiveTriple() const {
assert(!EffectiveTriple.getTriple().empty() && "No effective triple");
return EffectiveTriple;
}

path_list &getFilePaths() { return FilePaths; }
const path_list &getFilePaths() const { return FilePaths; }

Expand Down Expand Up @@ -432,6 +447,19 @@ class ToolChain {
virtual VersionTuple getMSVCVersionFromExe() const { return VersionTuple(); }
};

/// Set a ToolChain's effective triple. Reset it when the registration object
/// is destroyed.
class RegisterEffectiveTriple {
const ToolChain &TC;

public:
RegisterEffectiveTriple(const ToolChain &TC, llvm::Triple T) : TC(TC) {
TC.setEffectiveTriple(T);
}

~RegisterEffectiveTriple() { TC.setEffectiveTriple(llvm::Triple()); }
};

} // end namespace driver
} // end namespace clang

Expand Down
13 changes: 13 additions & 0 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2247,6 +2247,19 @@ InputInfo Driver::BuildJobsForActionNoCache(
InputInfos.append(OffloadDependencesInputInfo.begin(),
OffloadDependencesInputInfo.end());

// Set the effective triple of the toolchain for the duration of this job.
llvm::Triple EffectiveTriple;
const ToolChain &ToolTC = T->getToolChain();
const ArgList &Args = C.getArgsForToolChain(TC, BoundArch);
if (InputInfos.size() != 1) {
EffectiveTriple = llvm::Triple(ToolTC.ComputeEffectiveClangTriple(Args));
} else {
// Pass along the input type if it can be unambiguously determined.
EffectiveTriple = llvm::Triple(
ToolTC.ComputeEffectiveClangTriple(Args, InputInfos[0].getType()));
}
RegisterEffectiveTriple TripleRAII(ToolTC, EffectiveTriple);

// Determine the place to write output to, if any.
InputInfo Result;
if (JA->getType() == types::TY_Nothing)
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Driver/ToolChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
const ArgList &Args)
: D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) {
CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)),
EffectiveTriple() {
if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
if (!isThreadModelSupported(A->getValue()))
D.Diag(diag::err_drv_invalid_thread_model_for_target)
Expand Down
7 changes: 0 additions & 7 deletions clang/lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,6 @@ Darwin::~Darwin() {}

MachO::~MachO() {}

std::string MachO::ComputeEffectiveClangTriple(const ArgList &Args,
types::ID InputType) const {
llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));

return Triple.getTriple();
}

std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
types::ID InputType) const {
llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Expand Down
3 changes: 0 additions & 3 deletions clang/lib/Driver/ToolChains.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,6 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
/// @name ToolChain Implementation
/// {

std::string ComputeEffectiveClangTriple(const llvm::opt::ArgList &Args,
types::ID InputType) const override;

types::ID LookupTypeForExtension(const char *Ext) const override;

bool HasNativeLLVMSupport() const override;
Expand Down
24 changes: 9 additions & 15 deletions clang/lib/Driver/Tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ static bool useAAPCSForMachO(const llvm::Triple &T) {
// -mfloat-abi=.
arm::FloatABI arm::getARMFloatABI(const ToolChain &TC, const ArgList &Args) {
const Driver &D = TC.getDriver();
const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(Args));
const llvm::Triple &Triple = TC.getEffectiveTriple();
auto SubArch = getARMSubArchVersionNumber(Triple);
arm::FloatABI ABI = FloatABI::Invalid;
if (Arg *A =
Expand Down Expand Up @@ -1165,8 +1165,7 @@ static std::string getAArch64TargetCPU(const ArgList &Args) {

void Clang::AddAArch64TargetArgs(const ArgList &Args,
ArgStringList &CmdArgs) const {
std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
llvm::Triple Triple(TripleStr);
const llvm::Triple &Triple = getToolChain().getEffectiveTriple();

if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
Args.hasArg(options::OPT_mkernel) ||
Expand Down Expand Up @@ -3838,8 +3837,8 @@ static void AddAssemblerKPIC(const ToolChain &ToolChain, const ArgList &Args,
void Clang::ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output, const InputInfoList &Inputs,
const ArgList &Args, const char *LinkingOutput) const {
std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
const llvm::Triple Triple(TripleStr);
const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
const std::string &TripleStr = Triple.getTriple();

bool KernelOrKext =
Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
Expand Down Expand Up @@ -6500,9 +6499,8 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
assert(Inputs.size() == 1 && "Unexpected number of inputs.");
const InputInfo &Input = Inputs[0];

std::string TripleStr =
getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
const llvm::Triple Triple(TripleStr);
const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
const std::string &TripleStr = Triple.getTriple();

// Don't warn about "clang -w -c foo.s"
Args.ClaimAllArgs(options::OPT_w);
Expand Down Expand Up @@ -8878,9 +8876,7 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
break;
case llvm::Triple::armeb:
case llvm::Triple::thumbeb:
arm::appendEBLinkFlags(
Args, CmdArgs,
llvm::Triple(getToolChain().ComputeEffectiveClangTriple(Args)));
arm::appendEBLinkFlags(Args, CmdArgs, getToolChain().getEffectiveTriple());
CmdArgs.push_back("-m");
switch (getToolChain().getTriple().getEnvironment()) {
case llvm::Triple::EABI:
Expand Down Expand Up @@ -9044,8 +9040,7 @@ void gnutools::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
const char *LinkingOutput) const {
claimNoWarnArgs(Args);

std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
llvm::Triple Triple = llvm::Triple(TripleStr);
const llvm::Triple &Triple = getToolChain().getEffectiveTriple();

ArgStringList CmdArgs;

Expand Down Expand Up @@ -9380,8 +9375,7 @@ void gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
static_cast<const toolchains::Linux &>(getToolChain());
const Driver &D = ToolChain.getDriver();

std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
llvm::Triple Triple = llvm::Triple(TripleStr);
const llvm::Triple &Triple = getToolChain().getEffectiveTriple();

const llvm::Triple::ArchType Arch = ToolChain.getArch();
const bool isAndroid = ToolChain.getTriple().isAndroid();
Expand Down

0 comments on commit 18286cf

Please sign in to comment.