[Mesa-dev] [PATCH 4/8] clover/llvm: Use -cl-std and device version to select language defaults

Aaron Watry awatry at gmail.com
Mon Jul 31 01:26:08 UTC 2017


According to section 5.8.4.5 of the 2.0 spec, the CL C version is chosen by:
 1) If you have -cl-std=CL1.1+ use the version specified
 2) If not, use the highest 1.x version that the device supports

Curiously, there is no valid value for -cl-std=CL1.0

Signed-off-by: Aaron Watry <awatry at gmail.com>
Cc: Pierre Moreau <pierre.morrow at free.fr>

v2: (Pierre) Move create_compiler_instance changes to correct patch
    to prevent temporary build breakage.
    Convert version_str into unsigned and use it to find language version
    Add build_error for unknown language version string
    Whitespace fixes
---
 .../state_trackers/clover/llvm/invocation.cpp      | 61 +++++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)

diff --git a/src/gallium/state_trackers/clover/llvm/invocation.cpp b/src/gallium/state_trackers/clover/llvm/invocation.cpp
index 7c8d0e738d..ca75596b05 100644
--- a/src/gallium/state_trackers/clover/llvm/invocation.cpp
+++ b/src/gallium/state_trackers/clover/llvm/invocation.cpp
@@ -93,6 +93,65 @@ namespace {
       return ctx;
    }
 
+   unsigned get_language_version_from_string(const std::string &version_str){
+      if (version_str == "1.0"){
+         return 100;
+      }
+      if (version_str == "1.1"){
+         return 110;
+      }
+      if (version_str == "1.2"){
+         return 120;
+      }
+      if (version_str == "2.0"){
+         return 200;
+      }
+      throw build_error("Unknown/Unsupported language version");
+   }
+
+   clang::LangStandard::Kind
+   get_language_from_version_str(const std::string &version_str,
+                                 bool is_opt = false) {
+       /**
+        * Per CL 2.0 spec, section 5.8.4.5:
+        * If it's an option, use the value directly.
+        * If it's a device version, clamp to max 1.x version, a.k.a. 1.2
+        */
+      unsigned version = get_language_version_from_string(version_str);
+      if (!is_opt && version > 120 ){
+         version = 120;
+      }
+      switch (version){
+         case 100:
+            return clang::LangStandard::lang_opencl10;
+         case 110:
+            return clang::LangStandard::lang_opencl11;
+         case 120:
+            return clang::LangStandard::lang_opencl12;
+         case 200:
+            return clang::LangStandard::lang_opencl20;
+         default:
+            throw build_error("Unknown/Unsupported language version");
+      }
+   }
+
+   clang::LangStandard::Kind
+   get_language_version(const std::vector<std::string> &opts,
+                        const std::string &device_version) {
+
+      const std::string search = "-cl-std=CL";
+
+      for(auto opt: opts){
+         auto pos = opt.find(search);
+         if (pos == 0){
+            auto ver = opt.substr(pos+search.size());
+            return get_language_from_version_str(ver, true);
+         }
+      }
+
+      return get_language_from_version_str(device_version);
+   }
+
    std::unique_ptr<clang::CompilerInstance>
    create_compiler_instance(const target &target,
                             const std::vector<std::string> &opts,
@@ -129,7 +188,7 @@ namespace {
       compat::set_lang_defaults(c->getInvocation(), c->getLangOpts(),
                                 compat::ik_opencl, ::llvm::Triple(target.triple),
                                 c->getPreprocessorOpts(),
-                                clang::LangStandard::lang_opencl11);
+                                get_language_version(opts, device_version));
 
       c->createDiagnostics(new clang::TextDiagnosticPrinter(
                               *new raw_string_ostream(r_log),
-- 
2.11.0



More information about the mesa-dev mailing list