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(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 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,