From 400b306833d1e9095e8dcf1443653a079c657905 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 31 Mar 2022 20:27:59 -0700 Subject: [PATCH] ircd::gpt::pipe: Split pipe::code into separate unit. --- ircd/gpt_pipe.cc | 215 +--------------------------- ircd/gpt_pipe_code.cc | 318 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 319 insertions(+), 214 deletions(-) create mode 100644 ircd/gpt_pipe_code.cc diff --git a/ircd/gpt_pipe.cc b/ircd/gpt_pipe.cc index d44591cd4..ebd084f7f 100644 --- a/ircd/gpt_pipe.cc +++ b/ircd/gpt_pipe.cc @@ -1308,218 +1308,5 @@ ircd::gpt::pipe::model::matrix::matrix(cl::data *const master, }, } { -} - -/////////////////////////////////////////////////////////////////////////////// -// -// pipe::code -// - -decltype(ircd::gpt::pipe::code::default_path) -ircd::gpt::pipe::code::default_path -{ - { "name", "ircd.gpt.pipe.code.path" }, -}; - -decltype(ircd::gpt::pipe::code::cache_path) -ircd::gpt::pipe::code::cache_path -{ - { "name", "ircd.gpt.pipe.code.cache.path" }, -}; - -decltype(ircd::gpt::pipe::code::default_opts) -ircd::gpt::pipe::code::default_opts -{ - { "name", "ircd.gpt.pipe.code.opts" }, - { "default", string_view - { - " -cl-strict-aliasing" - " -cl-no-signed-zeros" - " -cl-finite-math-only" - " -cl-unsafe-math-optimizations" - " -cl-fast-relaxed-math" - " -cl-mad-enable" - " -cl-single-precision-constant" - //" -cl-fp32-correctly-rounded-divide-sqrt" - - " -cl-kernel-arg-info" - - " -O3" - }} -}; - -ircd::gpt::pipe::code::code() -:cl::code{[] -{ - const auto opts - { - fmt::snstringf - { - 4096, "%s -I%s", - string_view{default_opts}, - string_view{fs::base::include}, - } - }; - - const auto cache_path - { - make_cache_path(fs::path_scratch) - }; - - if(cache_path) - if(fs::is_reg(cache_path)) - return from_cache(opts, cache_path); - - return from_source(opts); -}()} -{ - put_cache(); -} - -ircd::gpt::pipe::code::~code() -noexcept -{ -} - -bool -ircd::gpt::pipe::code::put_cache() -{ - const auto cache_path - { - make_cache_path(fs::path_scratch) - }; - - if(!cache_path) - return false; - - if(fs::is_reg(cache_path)) - return false; - - set_cache(cache_path); - return true; -} - -void -ircd::gpt::pipe::code::set_cache(const string_view &path) -{ - const unique_mutable_buffer cache_buf - { - this->bins_size() - }; - - mutable_buffer _cache_bufs[1] - { - cache_buf - }; - - const auto cache_bufs - { - this->bin(_cache_bufs) - }; - - const fs::fd fd - { - path, std::ios::out - }; - - const auto written - { - fs::write(fd, cache_bufs[0]) - }; - - assert(!empty(written)); -} - -ircd::cl::code -ircd::gpt::pipe::code::from_source(const string_view &opts) -{ - const string_view code_path - { - default_path - }; - - const fs::fd fd - { - code_path - }; - - const std::string read - { - fs::read(fd) - }; - - const string_view src - { - read - }; - - const vector_view srcs - ( - &src, 1 - ); - - return cl::code - { - srcs, opts - }; -} - -ircd::cl::code -ircd::gpt::pipe::code::from_cache(const string_view &opts, - const string_view &cache_path) -{ - const fs::fd fd - { - cache_path - }; - - const std::string read - { - fs::read(fd) - }; - - const const_buffer bin - { - read - }; - - const vector_view bins - ( - &bin, 1 - ); - - return cl::code - { - bins, opts - }; -} - -ircd::string_view -ircd::gpt::pipe::code::make_cache_path(const mutable_buffer &buf) -{ - if(!cache_path) - return {}; - - const string_view &src_path - { - default_path - }; - - const string_view &src_fname - { - fs::filename(fs::path_scratch, src_path) - }; - - const string_view &cache_fname - { - fs::extension(fs::name_scratch, src_fname, ".nir") - }; - - const string_view parts[] - { - string_view{cache_path}, - string_view{cache_fname} - }; - - return fs::path(buf, fs::path_views{parts}); + assert(aligned(offset, 4096)); } diff --git a/ircd/gpt_pipe_code.cc b/ircd/gpt_pipe_code.cc new file mode 100644 index 000000000..7d2be19fd --- /dev/null +++ b/ircd/gpt_pipe_code.cc @@ -0,0 +1,318 @@ +// Tensor Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2022 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +decltype(ircd::gpt::pipe::code::default_path) +ircd::gpt::pipe::code::default_path +{ + { "name", "ircd.gpt.pipe.code.path" }, +}; + +decltype(ircd::gpt::pipe::code::cache_path) +ircd::gpt::pipe::code::cache_path +{ + { "name", "ircd.gpt.pipe.code.cache.path" }, +}; + +decltype(ircd::gpt::pipe::code::default_compile_opts) +ircd::gpt::pipe::code::default_compile_opts +{ + { "name", "ircd.gpt.pipe.code.opts.comp" }, + { "default", string_view + { + " -fident" + " -fno-builtin" + " -ffast-math" + " -O3" + + " -cl-no-signed-zeros" + " -cl-finite-math-only" + " -cl-fp32-correctly-rounded-divide-sqrt" + " -cl-single-precision-constant" + + " -cl-kernel-arg-info" + }} +}; + +decltype(ircd::gpt::pipe::code::default_link_opts) +ircd::gpt::pipe::code::default_link_opts +{ + { "name", "ircd.gpt.pipe.code.opts.link" }, + { "default", string_view + { + " -fident" + " -fno-builtin" + " -ffast-math" + " -O3" + + " -cl-no-signed-zeros" + " -cl-finite-math-only" + " -cl-fp32-correctly-rounded-divide-sqrt" + " -cl-single-precision-constant" + }} +}; + +// +// code::code +// + +ircd::gpt::pipe::code::code() +:cl::code{[]() -> cl::code +{ + const auto comp_opts + { + fmt::snstringf + { + 4096, "%s -I%s", + string_view{default_compile_opts}, + string_view{fs::base::include}, + } + }; + + const string_view link_opts + { + default_link_opts + }; + + cl::code ret; + + if(!ret) + ret = from_cache(); + + if(!ret) + ret = from_bitcode(link_opts); + + if(!ret) + ret = from_source(comp_opts, link_opts); + + return ret; +}()} +{ + put_cache(); +} + +ircd::gpt::pipe::code::~code() +noexcept +{ +} + +bool +ircd::gpt::pipe::code::put_cache() +{ + const auto cache_path + { + make_cache_path(fs::path_scratch) + }; + + if(!cache_path) + return false; + + if(fs::is_reg(cache_path)) + return false; + + set_cache(cache_path); + return true; +} + +void +ircd::gpt::pipe::code::set_cache(const string_view &path) +{ + const unique_mutable_buffer cache_buf + { + this->bins_size() + }; + + mutable_buffer _cache_bufs[1] + { + cache_buf + }; + + const auto cache_bufs + { + this->bin(_cache_bufs) + }; + + const fs::fd fd + { + path, std::ios::out + }; + + const auto written + { + fs::write(fd, cache_bufs[0]) + }; + + assert(!empty(written)); +} + +extern const uint8_t +gpt_gpu_r600_barts_bc[]; + +extern const uint +gpt_gpu_r600_barts_bc_len; + +ircd::cl::code +ircd::gpt::pipe::code::from_bitcode(const string_view &link_opts) +{ + const const_buffer bitcode + { + reinterpret_cast(gpt_gpu_r600_barts_bc), + gpt_gpu_r600_barts_bc_len + }; + + char pbuf[1][48]; + log::debug + { + log, "bitcode %p %s link_opts:%zu attempting...", + data(bitcode), + pretty(pbuf[0], si(size(bitcode))), + size(link_opts), + }; + + cl::code ret + { + bitcode + }; + + ret.link(link_opts); + return ret; +} + +ircd::cl::code +ircd::gpt::pipe::code::from_source(const string_view &comp_opts, + const string_view &link_opts) +{ + const string_view code_path + { + default_path + }; + + if(!code_path) + return {}; + + if(!fs::is_reg(code_path)) + return {}; + + const fs::fd fd + { + code_path + }; + + const std::string read + { + fs::read(fd) + }; + + const string_view src + { + read + }; + + const vector_view srcs + ( + &src, 1 + ); + + char pbuf[1][48]; + log::debug + { + log, "source code `%s' %s comp_opts:%zu link_opts:%zu attempting...", + code_path, + pretty(pbuf[0], si(size(read))), + size(comp_opts), + size(link_opts), + }; + + cl::code ret + { + srcs + }; + + ret.compile(comp_opts); + ret.link(link_opts); + return ret; +} + +ircd::cl::code +ircd::gpt::pipe::code::from_cache() +{ + const auto cache_path + { + make_cache_path(fs::path_scratch) + }; + + if(!cache_path) + return {}; + + if(!fs::is_reg(cache_path)) + return {}; + + const fs::fd fd + { + cache_path + }; + + const std::string read + { + fs::read(fd) + }; + + const const_buffer bin + { + read + }; + + const vector_view bins + ( + &bin, 1 + ); + + char pbuf[1][48]; + log::debug + { + log, "cached nir `%s' %s attempting...", + cache_path, + pretty(pbuf[0], si(size(read))), + }; + + return cl::code + { + bins + }; +} + +ircd::string_view +ircd::gpt::pipe::code::make_cache_path(const mutable_buffer &buf) +{ + if(!cache_path) + return {}; + + const string_view &src_path + { + default_path + }; + + const string_view &src_fname + { + fs::filename(fs::path_scratch, src_path) + }; + + const string_view &cache_fname + { + fs::extension(fs::name_scratch, src_fname, ".r600_barts.bc") + }; + + const string_view parts[] + { + string_view{cache_path}, + string_view{cache_fname} + }; + + return fs::path(buf, fs::path_views{parts}); +}