Index: lib/Driver/ToolChains/AMDGPU.h =================================================================== --- lib/Driver/ToolChains/AMDGPU.h +++ lib/Driver/ToolChains/AMDGPU.h @@ -11,8 +11,10 @@ #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H #include "Gnu.h" +#include "clang/Driver/Options.h" #include "clang/Driver/Tool.h" #include "clang/Driver/ToolChain.h" +#include namespace clang { namespace driver { @@ -37,14 +39,27 @@ namespace toolchains { class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF { + +private: + const std::map OptionsDefault = { + {options::OPT_O, "3"}, + {options::OPT_cl_std_EQ, "CL1.2"}}; + protected: Tool *buildLinker() const override; + const std::string getOptionDefault(options::ID OptID) const { + auto opt = OptionsDefault.find(OptID); + return opt->second; + } public: AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple, - const llvm::opt::ArgList &Args); + const llvm::opt::ArgList &Args); unsigned GetDefaultDwarfVersion() const override { return 2; } bool IsIntegratedAssemblerDefault() const override { return true; } + llvm::opt::DerivedArgList * + TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, + Action::OffloadKind DeviceOffloadKind) const override; }; } // end namespace toolchains Index: lib/Driver/ToolChains/AMDGPU.cpp =================================================================== --- lib/Driver/ToolChains/AMDGPU.cpp +++ lib/Driver/ToolChains/AMDGPU.cpp @@ -8,8 +8,8 @@ //===----------------------------------------------------------------------===// #include "AMDGPU.h" -#include "InputInfo.h" #include "CommonArgs.h" +#include "InputInfo.h" #include "clang/Driver/Compilation.h" #include "llvm/Option/ArgList.h" @@ -38,8 +38,44 @@ /// AMDGPU Toolchain AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) - : Generic_ELF(D, Triple, Args) { } + : Generic_ELF(D, Triple, Args) {} Tool *AMDGPUToolChain::buildLinker() const { return new tools::amdgpu::Linker(*this); } + +DerivedArgList * +AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch, + Action::OffloadKind DeviceOffloadKind) const { + + DerivedArgList *DAL = + Generic_ELF::TranslateArgs(Args, BoundArch, DeviceOffloadKind); + + // Do nothing if not OpenCL (-x cl) + if (!Args.getLastArgValue(options::OPT_x).equals("cl")) + return DAL; + + if (!DAL) + DAL = new DerivedArgList(Args.getBaseArgs()); + for (auto *A : Args) { + DAL->append(A); + } + + const OptTable &Opts = getDriver().getOpts(); + + // Phase 1 (.cl -> .bc) + if (Args.hasArg(options::OPT_c) && Args.hasArg(options::OPT_emit_llvm)) { + DAL->AddFlagArg(nullptr, Opts.getOption(getTriple().isArch64Bit() + ? options::OPT_m64 + : options::OPT_m32)); + + // Have to check OPT_O4, OPT_O0 & OPT_Ofast separately + // as they defined that way in Options.td + if (!Args.hasArg(options::OPT_O) && !Args.hasArg(options::OPT_O0) && + !Args.hasArg(options::OPT_O4) && !Args.hasArg(options::OPT_Ofast)) + DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), + getOptionDefault(options::OPT_O)); + } + + return DAL; +}