From 3c8c817ae71907611685513522c9494b4ab92797 Mon Sep 17 00:00:00 2001 From: Lionel Landwerlin Date: Fri, 30 Jul 2021 14:04:24 +0300 Subject: [PATCH] clc: add allowed extension for compile parameter The LLVM-SPIRV translator can include a bunch of capabilities into the generated SPIRV which is not what you always want. That include internal Intel specific capabilities from the translator. v2: Rename options Fixup checks (Jesse) Signed-off-by: Lionel Landwerlin Reviewed-by: Jesse Natalie Part-of: --- src/compiler/clc/clc.h | 6 ++++++ src/compiler/clc/clc_helpers.cpp | 23 ++++++++++++++++++++--- src/microsoft/clc/compute_test.cpp | 3 ++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/compiler/clc/clc.h b/src/compiler/clc/clc.h index 7ce661c9a68..0c0d7a3be41 100644 --- a/src/compiler/clc/clc.h +++ b/src/compiler/clc/clc.h @@ -46,6 +46,12 @@ struct clc_compile_args { struct clc_named_value source; const char * const *args; unsigned num_args; + + /* Allowed extensions SPIRV extensions the OpenCL->SPIRV translation can + * enable. A pointer to a NULL terminated array of strings, allow any + * extension if NULL. + */ + const char * const *allowed_spirv_extensions; }; struct clc_binary { diff --git a/src/compiler/clc/clc_helpers.cpp b/src/compiler/clc/clc_helpers.cpp index d66e45fcba0..2bc798461dc 100644 --- a/src/compiler/clc/clc_helpers.cpp +++ b/src/compiler/clc/clc_helpers.cpp @@ -858,12 +858,29 @@ clc_compile_to_llvm_module(const struct clc_compile_args *args, static int llvm_mod_to_spirv(std::unique_ptr<::llvm::Module> mod, std::unique_ptr context, + const struct clc_compile_args *args, const struct clc_logger *logger, struct clc_binary *out_spirv) { std::string log; + + SPIRV::TranslatorOpts spirv_opts; + if (!args || !args->allowed_spirv_extensions) { + spirv_opts.enableAllExtensions(); + } else { + SPIRV::TranslatorOpts::ExtensionsStatusMap ext_map; + for (int i = 0; args->allowed_spirv_extensions[i]; i++) { +#define EXT(X) \ + if (strcmp(#X, args->allowed_spirv_extensions[i]) == 0) \ + ext_map.insert(std::make_pair(SPIRV::ExtensionID::X, true)); +#include "LLVMSPIRVLib/LLVMSPIRVExtensions.inc" +#undef EXT + } + spirv_opts = SPIRV::TranslatorOpts(SPIRV::VersionNumber::MaximumVersion, ext_map); + } + std::ostringstream spv_stream; - if (!::llvm::writeSpirv(mod.get(), spv_stream, log)) { + if (!::llvm::writeSpirv(mod.get(), spirv_opts, spv_stream, log)) { clc_error(logger, "%sTranslation from LLVM IR to SPIR-V failed.\n", log.c_str()); return -1; @@ -905,7 +922,7 @@ clc_c_to_spirv(const struct clc_compile_args *args, auto pair = clc_compile_to_llvm_module(args, logger); if (!pair.first) return -1; - return llvm_mod_to_spirv(std::move(pair.first), std::move(pair.second), logger, out_spirv); + return llvm_mod_to_spirv(std::move(pair.first), std::move(pair.second), args, logger, out_spirv); } int @@ -924,7 +941,7 @@ clc_spir_to_spirv(const struct clc_binary *in_spir, if (!mod) return -1; - return llvm_mod_to_spirv(std::move(mod.get()), std::move(llvm_ctx), logger, out_spirv); + return llvm_mod_to_spirv(std::move(mod.get()), std::move(llvm_ctx), NULL, logger, out_spirv); } class SPIRVMessageConsumer { diff --git a/src/microsoft/clc/compute_test.cpp b/src/microsoft/clc/compute_test.cpp index 1a51a32bfd6..2232abe78cc 100644 --- a/src/microsoft/clc/compute_test.cpp +++ b/src/microsoft/clc/compute_test.cpp @@ -793,7 +793,8 @@ ComputeTest::compile(const std::vector &sources, const std::vector &compile_args, bool create_library) { - struct clc_compile_args args = { 0 }; + struct clc_compile_args args = { + }; args.args = compile_args.data(); args.num_args = (unsigned)compile_args.size(); ComputeTest::Shader shader;