From 2402466a08c370adb19319162ee67ead9024d57b Mon Sep 17 00:00:00 2001 From: Pierre Moreau Date: Mon, 18 May 2020 18:08:37 +0200 Subject: [PATCH] clover/llvm: Use the highest supported SPIR-V version (v4) v2: a) Move `supported_spirv_verssions()` to `spirv::` (Francisco Jerez) b) Introduce a `SPV_MAKE_VERSION` macro to avoid making mistakes and making it more readable than its uint representation. v3: Replaced an if-statement with a `std::min()` in `spirv::get_spirv_translator_options()` (Karol Herbst) v4: Turn `SPV_MAKE_VERSION()` into a function (Francisco Jerez) Signed-off-by: Pierre Moreau Reviewed-by: Karol Herbst Reviewed-by: Francisco Jerez Part-of: --- src/gallium/frontends/clover/llvm/invocation.cpp | 16 +++++++++++++++- .../frontends/clover/spirv/invocation.cpp | 16 ++++++++++++++++ .../frontends/clover/spirv/invocation.hpp | 4 ++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/gallium/frontends/clover/llvm/invocation.cpp b/src/gallium/frontends/clover/llvm/invocation.cpp index 95a9d036622..04db2f90e03 100644 --- a/src/gallium/frontends/clover/llvm/invocation.cpp +++ b/src/gallium/frontends/clover/llvm/invocation.cpp @@ -307,6 +307,18 @@ namespace { return act.takeModule(); } + +#ifdef HAVE_CLOVER_SPIRV + SPIRV::TranslatorOpts + get_spirv_translator_options(const device &dev) { + const auto supported_versions = spirv::supported_versions(); + const auto maximum_spirv_version = + std::min(static_cast(supported_versions.back()), + SPIRV::VersionNumber::MaximumVersion); + + return SPIRV::TranslatorOpts(maximum_spirv_version); + } +#endif } module @@ -438,6 +450,8 @@ clover::llvm::compile_to_spirv(const std::string &source, if (has_flag(debug::llvm)) debug::log(".ll", print_module_bitcode(*mod)); + const auto spirv_options = get_spirv_translator_options(dev); + std::string error_msg; if (!::llvm::regularizeLlvmForSpirv(mod.get(), error_msg)) { r_log += "Failed to regularize LLVM IR for SPIR-V: " + error_msg + ".\n"; @@ -445,7 +459,7 @@ clover::llvm::compile_to_spirv(const std::string &source, } std::ostringstream os; - if (!::llvm::writeSpirv(mod.get(), os, error_msg)) { + if (!::llvm::writeSpirv(mod.get(), spirv_options, os, error_msg)) { r_log += "Translation from LLVM IR to SPIR-V failed: " + error_msg + ".\n"; throw error(CL_INVALID_VALUE); } diff --git a/src/gallium/frontends/clover/spirv/invocation.cpp b/src/gallium/frontends/clover/spirv/invocation.cpp index e4b1565288f..489bb62dde9 100644 --- a/src/gallium/frontends/clover/spirv/invocation.cpp +++ b/src/gallium/frontends/clover/spirv/invocation.cpp @@ -49,6 +49,12 @@ using namespace clover; #ifdef HAVE_CLOVER_SPIRV namespace { + uint32_t + make_spirv_version(uint8_t major, uint8_t minor) { + return (static_cast(major) << 16u) | + (static_cast(minor) << 8u); + } + template T get(const char *source, size_t index) { const uint32_t *word_ptr = reinterpret_cast(source); @@ -715,6 +721,11 @@ clover::spirv::supported_extensions() { }; } +std::vector +clover::spirv::supported_versions() { + return { make_spirv_version(1u, 0u) }; +} + #else bool clover::spirv::is_valid_spirv(const std::vector &/*binary*/, @@ -748,4 +759,9 @@ std::unordered_set clover::spirv::supported_extensions() { return {}; } + +std::vector +clover::spirv::supported_versions() { + return {}; +} #endif diff --git a/src/gallium/frontends/clover/spirv/invocation.hpp b/src/gallium/frontends/clover/spirv/invocation.hpp index 49a183ffc04..27f8d8c1934 100644 --- a/src/gallium/frontends/clover/spirv/invocation.hpp +++ b/src/gallium/frontends/clover/spirv/invocation.hpp @@ -55,6 +55,10 @@ namespace clover { // Returns a set of supported SPIR-V extensions. std::unordered_set supported_extensions(); + + // Returns a vector (sorted in increasing order) of supported SPIR-V + // versions. + std::vector supported_versions(); } } -- 2.30.2