Mesa (main): clc: let user specify the targetted SPIRV version

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Sun Oct 3 20:45:58 UTC 2021


Module: Mesa
Branch: main
Commit: 445996379b1abe745453184fb55d4446b46b1646
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=445996379b1abe745453184fb55d4446b46b1646

Author: Lionel Landwerlin <lionel.g.landwerlin at intel.com>
Date:   Fri Sep 24 11:00:16 2021 +0300

clc: let user specify the targetted SPIRV version

This version is given to the LLVM-SPIRV translator. On the SPIRV-Tools
side of things, we want to use the highest available version to be
sure to be able to parse back what was generated.

Signed-off-by: Lionel Landwerlin <lionel.g.landwerlin at intel.com>
Reviewed-by: Jesse Natalie <jenatali at microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13113>

---

 src/compiler/clc/clc.h           | 12 ++++++++++++
 src/compiler/clc/clc_helpers.cpp | 31 +++++++++++++++++++++++++++++--
 src/compiler/clc/meson.build     |  5 +++++
 3 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/src/compiler/clc/clc.h b/src/compiler/clc/clc.h
index 0c0d7a3be41..68366340ced 100644
--- a/src/compiler/clc/clc.h
+++ b/src/compiler/clc/clc.h
@@ -40,6 +40,15 @@ struct clc_named_value {
    const char *value;
 };
 
+enum clc_spirv_version {
+   CLC_SPIRV_VERSION_MAX = 0,
+   CLC_SPIRV_VERSION_1_0,
+   CLC_SPIRV_VERSION_1_1,
+   CLC_SPIRV_VERSION_1_2,
+   CLC_SPIRV_VERSION_1_3,
+   CLC_SPIRV_VERSION_1_4,
+};
+
 struct clc_compile_args {
    const struct clc_named_value *headers;
    unsigned num_headers;
@@ -47,6 +56,9 @@ struct clc_compile_args {
    const char * const *args;
    unsigned num_args;
 
+   /* SPIRV version to target. */
+   enum clc_spirv_version spirv_version;
+
    /* Allowed extensions SPIRV extensions the OpenCL->SPIRV translation can
     * enable. A pointer to a NULL terminated array of strings, allow any
     * extension if NULL.
diff --git a/src/compiler/clc/clc_helpers.cpp b/src/compiler/clc/clc_helpers.cpp
index f251eab4a22..1cd35cadcb5 100644
--- a/src/compiler/clc/clc_helpers.cpp
+++ b/src/compiler/clc/clc_helpers.cpp
@@ -60,7 +60,10 @@
 
 #include "clc_helpers.h"
 
-constexpr spv_target_env spirv_target = SPV_ENV_UNIVERSAL_1_0;
+/* Use the highest version of SPIRV supported by SPIRV-Tools. */
+constexpr spv_target_env spirv_target = SPV_ENV_UNIVERSAL_1_5;
+
+constexpr SPIRV::VersionNumber invalid_spirv_trans_version = static_cast<SPIRV::VersionNumber>(0);
 
 using ::llvm::Function;
 using ::llvm::LLVMContext;
@@ -855,6 +858,22 @@ clc_compile_to_llvm_module(const struct clc_compile_args *args,
    return { act.takeModule(), std::move(llvm_ctx) };
 }
 
+static SPIRV::VersionNumber
+spirv_version_to_llvm_spirv_translator_version(enum clc_spirv_version version)
+{
+   switch (version) {
+   case CLC_SPIRV_VERSION_MAX: return SPIRV::VersionNumber::MaximumVersion;
+   case CLC_SPIRV_VERSION_1_0: return SPIRV::VersionNumber::SPIRV_1_0;
+   case CLC_SPIRV_VERSION_1_1: return SPIRV::VersionNumber::SPIRV_1_1;
+   case CLC_SPIRV_VERSION_1_2: return SPIRV::VersionNumber::SPIRV_1_2;
+   case CLC_SPIRV_VERSION_1_3: return SPIRV::VersionNumber::SPIRV_1_3;
+#ifdef HAS_SPIRV_1_4
+   case CLC_SPIRV_VERSION_1_4: return SPIRV::VersionNumber::SPIRV_1_4;
+#endif
+   default:      return invalid_spirv_trans_version;
+   }
+}
+
 static int
 llvm_mod_to_spirv(std::unique_ptr<::llvm::Module> mod,
                   std::unique_ptr<LLVMContext> context,
@@ -864,8 +883,16 @@ llvm_mod_to_spirv(std::unique_ptr<::llvm::Module> mod,
 {
    std::string log;
 
+   SPIRV::VersionNumber version =
+      spirv_version_to_llvm_spirv_translator_version(args->spirv_version);
+   if (version == invalid_spirv_trans_version) {
+      clc_error(logger, "Invalid/unsupported SPIRV specified.\n");
+      return -1;
+   }
+
    SPIRV::TranslatorOpts spirv_opts;
    if (!args || !args->allowed_spirv_extensions) {
+      spirv_opts = SPIRV::TranslatorOpts(version);
       spirv_opts.enableAllExtensions();
    } else {
       SPIRV::TranslatorOpts::ExtensionsStatusMap ext_map;
@@ -876,7 +903,7 @@ llvm_mod_to_spirv(std::unique_ptr<::llvm::Module> mod,
 #include "LLVMSPIRVLib/LLVMSPIRVExtensions.inc"
 #undef EXT
       }
-      spirv_opts = SPIRV::TranslatorOpts(SPIRV::VersionNumber::MaximumVersion, ext_map);
+      spirv_opts = SPIRV::TranslatorOpts(version, ext_map);
    }
 
    std::ostringstream spv_stream;
diff --git a/src/compiler/clc/meson.build b/src/compiler/clc/meson.build
index d46f67c3e79..d3c3286a7af 100644
--- a/src/compiler/clc/meson.build
+++ b/src/compiler/clc/meson.build
@@ -44,6 +44,11 @@ if with_microsoft_clc
   _libclc_cpp_args += ['-DUSE_STATIC_OPENCL_C_H=1']
 endif
 
+# Supported added for SPIRV 1.4 in a version that required LLVM 14.
+if dep_llvm.version().version_compare('>= 14.0')
+  _libclc_cpp_args += ['-DHAS_SPIRV_1_4=1']
+endif
+
 _libclc = static_library(
   'libclc',
   files_libclc,



More information about the mesa-commit mailing list