From 8dbe382a8dde8478512f8ebc8aa88ee5af932f77 Mon Sep 17 00:00:00 2001 From: Andrey Kasaurov <Andrey.Kasaurov@amd.com> Date: Tue, 5 Sep 2017 10:24:38 +0000 Subject: [PATCH] [AMDGPU] Implement infrastructure to set options in AMDGPUToolChain In current OpenCL implementation some options are set in OpenCL RT/Driver, which causes discrepancy between online and offline paths. Implement infrastructure to move options from OpenCL RT/Driver to AMDGPUToolChain using overloaded TranslateArgs() method. Create map for default options values, as Options.td doesn't support default values (in contrast with OPTIONS.def). Add two driver options: -On and -mNN (like -O3, -m64). Some minor formatting changes to follow the clang-format style. Differential Revision: https://reviews.llvm.org/D37386 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@312524 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Driver/ToolChains/AMDGPU.cpp | 41 ++++++++++++++++++++++++-- lib/Driver/ToolChains/AMDGPU.h | 16 +++++++++- test/Driver/amdgpu-toolchain-opencl.cl | 19 ++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 test/Driver/amdgpu-toolchain-opencl.cl diff --git a/lib/Driver/ToolChains/AMDGPU.cpp b/lib/Driver/ToolChains/AMDGPU.cpp index 63e1749e008..ce7f4e852a7 100644 --- a/lib/Driver/ToolChains/AMDGPU.cpp +++ b/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,45 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA, /// AMDGPU Toolchain AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) - : Generic_ELF(D, Triple, Args) { } + : Generic_ELF(D, Triple, Args), + OptionsDefault({{options::OPT_O, "3"}, + {options::OPT_cl_std_EQ, "CL1.2"}}) {} 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, options::OPT_O0, options::OPT_O4, + options::OPT_Ofast)) + DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), + getOptionDefault(options::OPT_O)); + } + + return DAL; +} diff --git a/lib/Driver/ToolChains/AMDGPU.h b/lib/Driver/ToolChains/AMDGPU.h index 9af1e96489e..e2a12d46823 100644 --- a/lib/Driver/ToolChains/AMDGPU.h +++ b/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 <map> namespace clang { namespace driver { @@ -37,14 +39,26 @@ public: namespace toolchains { class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF { + +private: + const std::map<options::ID, const StringRef> OptionsDefault; + protected: Tool *buildLinker() const override; + const StringRef getOptionDefault(options::ID OptID) const { + auto opt = OptionsDefault.find(OptID); + assert(opt != OptionsDefault.end() && "No Default for Option"); + 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 diff --git a/test/Driver/amdgpu-toolchain-opencl.cl b/test/Driver/amdgpu-toolchain-opencl.cl new file mode 100644 index 00000000000..3994387f3ea --- /dev/null +++ b/test/Driver/amdgpu-toolchain-opencl.cl @@ -0,0 +1,19 @@ +// RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm -mcpu=fiji -O0 %s 2>&1 | FileCheck -check-prefix=CHECK_O0 %s +// RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm -mcpu=fiji -O1 %s 2>&1 | FileCheck -check-prefix=CHECK_O1 %s +// RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm -mcpu=fiji -O2 %s 2>&1 | FileCheck -check-prefix=CHECK_O2 %s +// RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm -mcpu=fiji -O3 %s 2>&1 | FileCheck -check-prefix=CHECK_O3 %s +// RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm -mcpu=fiji -O4 %s 2>&1 | FileCheck -check-prefix=CHECK_O4 %s +// RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm -mcpu=fiji -O5 %s 2>&1 | FileCheck -check-prefix=CHECK_O5 %s +// RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm -mcpu=fiji -Og %s 2>&1 | FileCheck -check-prefix=CHECK_Og %s +// RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm -mcpu=fiji -Ofast %s 2>&1 | FileCheck -check-prefix=CHECK_Ofast %s +// RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm -mcpu=fiji %s 2>&1 | FileCheck -check-prefix=CHECK_O_DEFAULT %s +// CHECK_O0: clang{{.*}} "-O0" +// CHECK_O1: clang{{.*}} "-O1" +// CHECK_O2: clang{{.*}} "-O2" +// CHECK_O3: clang{{.*}} "-O3" +// CHECK_O4: clang{{.*}} "-O3" +// CHECK_O5: clang{{.*}} "-O5" +// CHECK_Og: clang{{.*}} "-Og" +// CHECK_Ofast: {{.*}}clang{{.*}} "-Ofast" +// CHECK_O_DEFAULT: clang{{.*}} "-O3" + -- GitLab