clover: Use core libclc loader

v2 (Jason Ekstrand):
 - Use the newly added nir_can_find_libclc() helper

Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Jesse Natalie <jenatali@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7034>
This commit is contained in:
Dave Airlie 2020-10-07 09:01:43 +10:00 committed by Marge Bot
parent ef453f5439
commit 43390a546d
6 changed files with 14 additions and 81 deletions

View file

@ -55,9 +55,9 @@ device::device(clover::platform &platform, pipe_loader_device *ldev) :
return;
#ifdef HAVE_CLOVER_SPIRV
if (supports_ir(PIPE_SHADER_IR_NIR_SERIALIZED)) {
nir::check_for_libclc(*this);
clc_cache = nir::create_clc_disk_cache();
clc = spirv::load_clc(*this);
clc_nir = lazy<std::shared_ptr<nir_shader>>([&] () { std::string log; return std::shared_ptr<nir_shader>(nir::libclc_spirv_to_nir(clc, *this, log), ralloc_free); });
clc_nir = lazy<std::shared_ptr<nir_shader>>([&] () { std::string log; return std::shared_ptr<nir_shader>(nir::load_libclc_nir(*this, log), ralloc_free); });
return;
}
#endif

View file

@ -106,7 +106,6 @@ namespace clover {
return svm_support() & CL_DEVICE_SVM_FINE_GRAIN_SYSTEM;
}
module clc;
lazy<std::shared_ptr<nir_shader>> clc_nir;
disk_cache *clc_cache;
private:

View file

@ -200,60 +200,19 @@ struct disk_cache *clover::nir::create_clc_disk_cache(void)
return disk_cache_create("clover-clc", cache_id, 0);
}
nir_shader *clover::nir::libclc_spirv_to_nir(const module &mod, const device &dev,
std::string &r_log)
void clover::nir::check_for_libclc(const device &dev)
{
if (!nir_can_find_libclc(dev.address_bits()))
throw error(CL_COMPILER_NOT_AVAILABLE);
}
nir_shader *clover::nir::load_libclc_nir(const device &dev, std::string &r_log)
{
spirv_to_nir_options spirv_options = create_spirv_options(dev, r_log);
spirv_options.create_library = true;
auto &section = mod.secs[0];
const auto *binary =
reinterpret_cast<const pipe_binary_program_header *>(section.data.data());
const uint32_t *data = reinterpret_cast<const uint32_t *>(binary->blob);
const size_t num_words = binary->num_bytes / 4;
auto *compiler_options = dev_get_nir_compiler_options(dev);
unsigned char clc_cache_key[20];
unsigned char sha1[CACHE_KEY_SIZE];
/* caching ftw. */
struct mesa_sha1 ctx;
size_t binary_size = 0;
uint8_t *buffer = NULL;
if (dev.clc_cache) {
_mesa_sha1_init(&ctx);
_mesa_sha1_update(&ctx, data, num_words * 4);
_mesa_sha1_final(&ctx, clc_cache_key);
disk_cache_compute_key(dev.clc_cache, clc_cache_key, 20, sha1);
buffer = (uint8_t *)disk_cache_get(dev.clc_cache, sha1, &binary_size);
}
nir_shader *nir;
if (!buffer) {
nir = spirv_to_nir(data, num_words, nullptr, 0,
MESA_SHADER_KERNEL, "clcspirv",
&spirv_options, compiler_options);
nir_validate_shader(nir, "clover-libclc");
nir->info.internal = true;
NIR_PASS_V(nir, nir_lower_variable_initializers, nir_var_function_temp);
NIR_PASS_V(nir, nir_lower_returns);
if (dev.clc_cache) {
struct blob blob = { 0 };
blob_init(&blob);
nir_serialize(&blob, nir, true);
disk_cache_put(dev.clc_cache, sha1, blob.data, blob.size, NULL);
blob_finish(&blob);
}
} else {
struct blob_reader blob_read;
blob_reader_init(&blob_read, buffer, binary_size);
nir = nir_deserialize(NULL, compiler_options, &blob_read);
free(buffer);
}
return nir;
return nir_load_libclc_shader(dev.address_bits(), dev.clc_cache,
&spirv_options, compiler_options);
}
module clover::nir::spirv_to_nir(const module &mod, const device &dev,

View file

@ -31,9 +31,10 @@ struct nir_shader;
namespace clover {
class device;
namespace nir {
void check_for_libclc(const device &dev);
// converts libclc spirv into nir
nir_shader *libclc_spirv_to_nir(const module &mod, const device &dev,
std::string &r_log);
nir_shader *load_libclc_nir(const device &dev, std::string &r_log);
struct disk_cache *create_clc_disk_cache(void);

View file

@ -876,26 +876,3 @@ clover::spirv::supported_versions() {
return {};
}
#endif
module
clover::spirv::load_clc(const device &dev)
{
std::vector<char> ilfile;
std::ifstream file;
std::string name32 = "spirv-mesa3d-.spv";
std::string name64 = "spirv64-mesa3d-.spv";
file.open(LIBCLC_LIBEXECDIR + (dev.address_bits() == 64 ? name64 : name32), std::ifstream::in | std::ifstream::binary);
if (!file.good())
throw error(CL_COMPILER_NOT_AVAILABLE);
file.seekg(0, std::ios::end);
std::streampos length(file.tellg());
if (length) {
file.seekg(0, std::ios::beg);
ilfile.resize(static_cast<std::size_t>(length));
file.read(&ilfile.front(), static_cast<std::size_t>(length));
}
std::string log;
return spirv::compile_program(ilfile, dev, log, false);
}

View file

@ -60,9 +60,6 @@ namespace clover {
// Returns a vector (sorted in increasing order) of supported SPIR-V
// versions.
std::vector<uint32_t> supported_versions();
// Load the SPIR-V for the CLC module.
module load_clc(const device &dev);
}
}