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@intel.com>
Reviewed-by: Jesse Natalie <jenatali@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13113>
This commit is contained in:
Lionel Landwerlin 2021-09-24 11:00:16 +03:00 committed by Marge Bot
parent 72fd81d0ac
commit 445996379b
3 changed files with 46 additions and 2 deletions

View file

@ -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.

View file

@ -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;

View file

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