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

Aaron Watry awatry at gmail.com
Sat Jul 22 04:19:50 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>
---
 .../state_trackers/clover/llvm/invocation.cpp      | 48 ++++++++++++++++++++--
 1 file changed, 45 insertions(+), 3 deletions(-)

diff --git a/src/gallium/state_trackers/clover/llvm/invocation.cpp b/src/gallium/state_trackers/clover/llvm/invocation.cpp
index 364aaf1517..92d72e5b73 100644
--- a/src/gallium/state_trackers/clover/llvm/invocation.cpp
+++ b/src/gallium/state_trackers/clover/llvm/invocation.cpp
@@ -93,6 +93,48 @@ namespace {
       return ctx;
    }
 
+   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
+        */
+       if (version_str == "1.1")
+          return clang::LangStandard::lang_opencl11;
+       if (version_str == "1.2")
+          return clang::LangStandard::lang_opencl12;
+       if (version_str == "2.0"){
+          if (is_opt) return clang::LangStandard::lang_opencl20;
+          else return clang::LangStandard::lang_opencl12;
+       }
+
+       /*
+        * At this point, it's not a recognized language version option or
+        * 1.1+ device version, which just leaves 1.0 as a possible device
+        * version (or an invalid version string).
+        */
+       return clang::LangStandard::lang_opencl10;
+  }
+
+   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 +171,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),
@@ -211,7 +253,7 @@ clover::llvm::compile_program(const std::string &source,
 
    auto ctx = create_context(r_log);
    auto c = create_compiler_instance(target, tokenize(opts + " input.cl"),
-                                     r_log);
+                                     device_version, r_log);
    auto mod = compile(*ctx, *c, "input.cl", source, headers, target, opts,
                       r_log);
 
@@ -280,7 +322,7 @@ clover::llvm::link_program(const std::vector<module> &modules,
    erase_if(equals("-create-library"), options);
 
    auto ctx = create_context(r_log);
-   auto c = create_compiler_instance(target, options, r_log);
+   auto c = create_compiler_instance(target, options, device_version, r_log);
    auto mod = link(*ctx, *c, modules, r_log);
 
    optimize(*mod, c->getCodeGenOpts().OptimizationLevel, !create_library);
-- 
2.11.0



More information about the mesa-dev mailing list