r312524 - [AMDGPU] Implement infrastructure to set options in AMDGPUToolChain

Andrey Kasaurov via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 5 03:24:39 PDT 2017


Author: kasaurov
Date: Tue Sep  5 03:24:38 2017
New Revision: 312524

URL: http://llvm.org/viewvc/llvm-project?rev=312524&view=rev
Log:
[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

Added:
    cfe/trunk/test/Driver/amdgpu-toolchain-opencl.cl
Modified:
    cfe/trunk/lib/Driver/ToolChains/AMDGPU.cpp
    cfe/trunk/lib/Driver/ToolChains/AMDGPU.h

Modified: cfe/trunk/lib/Driver/ToolChains/AMDGPU.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains/AMDGPU.cpp?rev=312524&r1=312523&r2=312524&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ToolChains/AMDGPU.cpp (original)
+++ cfe/trunk/lib/Driver/ToolChains/AMDGPU.cpp Tue Sep  5 03:24:38 2017
@@ -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(Compil
 /// 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;
+}

Modified: cfe/trunk/lib/Driver/ToolChains/AMDGPU.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains/AMDGPU.h?rev=312524&r1=312523&r2=312524&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ToolChains/AMDGPU.h (original)
+++ cfe/trunk/lib/Driver/ToolChains/AMDGPU.h Tue Sep  5 03:24:38 2017
@@ -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

Added: cfe/trunk/test/Driver/amdgpu-toolchain-opencl.cl
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/amdgpu-toolchain-opencl.cl?rev=312524&view=auto
==============================================================================
--- cfe/trunk/test/Driver/amdgpu-toolchain-opencl.cl (added)
+++ cfe/trunk/test/Driver/amdgpu-toolchain-opencl.cl Tue Sep  5 03:24:38 2017
@@ -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"
+




More information about the cfe-commits mailing list