#include "gpu/intel/compute/utils.hpp"
namespace dnnl {
namespace impl {
namespace gpu {
namespace intel {
namespace compute {
status_t preprocess_headers(stringstream_t &pp_code, const char *code,
const compute::kernel_ctx_t &kernel_ctx) {
stringstream_t code_stream(code);
for (std::string line; std::getline(code_stream, line);) {
const size_t include_pos = line.find("#include");
if (include_pos != std::string::npos) {
static constexpr size_t include_len = 8;
const size_t first_quote_pos
= line.find("\"", include_pos + include_len);
const size_t second_quote_pos
= line.find("\"", first_quote_pos + 1);
const size_t kernel_name_len
= second_quote_pos - first_quote_pos - 1;
const auto header_name
= line.substr(first_quote_pos + 1, kernel_name_len);
const char *header_source
= kernel_ctx.get_custom_header(header_name);
if (!header_source) header_source = get_kernel_header(header_name);
CHECK(preprocess_headers(pp_code, header_source, kernel_ctx));
} else {
pp_code << line << std::endl;
}
}
return status::success;
}
void debugdump_processed_source(const std::string &source,
const std::string &options, const std::string &ocl_options) {
#if defined(__linux__) && defined(DNNL_DEV_MODE)
if (get_verbose(verbose_t::debuginfo) >= 10) {
auto get_defines = [](const std::string &from) {
std::string ret;
size_t pos = 0;
while (pos < from.length()) {
pos = from.find("-D", pos);
char quote_parity = true;
while (pos < from.length()) {
if (quote_parity
&& utils::one_of(from[pos], '~', '#', '$', '&', '*',
'(', ')', '\\', '|', '[', ']', '{', '}',
';', '\'', '<', '>', '/', '?', '!')) {
ret += '\\';
}
ret += from[pos];
if (from[pos] == '"') quote_parity ^= true;
if (from[pos] == ' ' && quote_parity) break;
pos++;
}
}
return ret;
};
auto execute_command
= [](const std::string &cmd, const std::string &stdin) {
std::string result;
std::array<char, 256> buffer;
FILE *pipe = popen(cmd.c_str(), "w");
fputs(stdin.c_str(), pipe);
if (pipe) {
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
result += buffer.data();
}
}
pclose(pipe);
return result;
};
auto o = get_defines(options) + get_defines(ocl_options);
std::string preprocess_cmd
= std::string() + "cpp -P " + o + " | clang-format";
execute_command(preprocess_cmd, source);
std::cout << "OCL_ARCH_OPTIONS: " << ocl_options << std::endl;
}
#endif
}
} } } } }