diff --git a/CMakeLists.txt b/CMakeLists.txt index 43fac54abf..e562f7cada 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -856,6 +856,7 @@ set(BUILTINS_VME_LIB_NAME "builtins_vme") set(SCHEDULER_BINARY_LIB_NAME "scheduler_binary") add_subdirectory_unique(shared/source) +add_subdirectory_unique(shared/generate_cpp_array) macro(generate_runtime_lib LIB_NAME MOCKABLE GENERATE_EXEC) set(NEO_STATIC_LIB_NAME ${LIB_NAME}) diff --git a/opencl/source/built_ins/kernels/CMakeLists.txt b/opencl/source/built_ins/kernels/CMakeLists.txt index 869b9915c5..339564493e 100644 --- a/opencl/source/built_ins/kernels/CMakeLists.txt +++ b/opencl/source/built_ins/kernels/CMakeLists.txt @@ -75,7 +75,7 @@ function(compile_builtin gen_type platform_type builtin bits builtin_options) list(APPEND __cloc__options__ "-cl-kernel-arg-info") add_custom_command( OUTPUT ${OUTPUT_FILES} - COMMAND ${cloc_cmd_prefix} -q -file ${FILENAME} -device ${DEFAULT_SUPPORTED_${gen_type}_${platform_type}_PLATFORM} ${builtin_options} -${bits} -out_dir ${OUTPUTDIR} -cpp_file -options "$" + COMMAND ${cloc_cmd_prefix} -q -file ${FILENAME} -device ${DEFAULT_SUPPORTED_${gen_type}_${platform_type}_PLATFORM} ${builtin_options} -${bits} -out_dir ${OUTPUTDIR} -options "$" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${builtin} ocloc copy_compiler_files ) diff --git a/shared/generate_cpp_array/CMakeLists.txt b/shared/generate_cpp_array/CMakeLists.txt new file mode 100644 index 0000000000..af5c157a45 --- /dev/null +++ b/shared/generate_cpp_array/CMakeLists.txt @@ -0,0 +1,11 @@ +# +# Copyright (C) 2020 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +set(CPP_GENERATE_TOOL_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/source/generate_cpp_array.cpp +) +add_executable (cpp_generate_tool "${CPP_GENERATE_TOOL_SOURCES}") \ No newline at end of file diff --git a/shared/generate_cpp_array/source/generate_cpp_array.cpp b/shared/generate_cpp_array/source/generate_cpp_array.cpp new file mode 100644 index 0000000000..3c1cda32bc --- /dev/null +++ b/shared/generate_cpp_array/source/generate_cpp_array.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include +#include +#include +#include + +constexpr int ARG_COUNT = 9; + +static void show_usage(std::string name) { + std::cerr << "Usage " << name << " - ALL MUST BE SPECIFIED\n" + << "Options :\n" + << "\t -f, --file\t\tA file which content will be parsed into a uint32_t array in a .cpp file\n" + << "\t -o, --out\t\t.Cpp output file name\n" + << "\t -p, --platform\t\tFamily name with type\n" + << "\t -a, --array\t\tName of an uin32_t type array containing parsed input file" << std::endl; +} +std::string parseToCharArray(std::unique_ptr binary, size_t size, std::string &builtinName, std::string &platform, bool isSpirV) { + std::ostringstream out; + + out << "#include \n"; + out << "#include \n\n"; + out << "size_t " << builtinName << "BinarySize_" << platform << " = " << size << ";\n"; + out << "uint32_t " << builtinName << "Binary_" << platform << "[" << (size + 3) / 4 << "] = {" + << std::endl + << " "; + uint32_t *binaryUint = reinterpret_cast(binary.get()); + for (size_t i = 0; i < (size + 3) / 4; i++) { + if (i != 0) { + out << ", "; + if (i % 8 == 0) { + out << std::endl + << " "; + } + } + if (i < size / 4) { + out << "0x" << std::hex << std::setw(8) << std::setfill('0') << binaryUint[i]; + } else { + uint32_t lastBytes = size & 0x3; + uint32_t lastUint = 0; + uint8_t *pLastUint = (uint8_t *)&lastUint; + for (uint32_t j = 0; j < lastBytes; j++) { + pLastUint[sizeof(uint32_t) - 1 - j] = binary.get()[i * 4 + j]; + } + out << "0x" << std::hex << std::setw(8) << std::setfill('0') << lastUint; + } + } + out << "};" << std::endl; + + out << std::endl + << "#include \"shared/source/built_ins/registry/built_ins_registry.h\"\n" + << std::endl; + out << "namespace NEO {" << std::endl; + out << "static RegisterEmbeddedResource register" << builtinName; + isSpirV ? out << "Ir(" : out << "Bin("; + out << std::endl; + out << " \"" << platform << "_0_" << builtinName; + isSpirV ? out << ".builtin_kernel.spv\"," : out << ".builtin_kernel.bin\","; + out << std::endl; + out << " (const char *)" << builtinName << "Binary_" << platform << "," << std::endl; + out << " " << builtinName << "BinarySize_" << platform << ");" << std::endl; + out << "}" << std::endl; + + return out.str(); +} + +int main(int argc, char *argv[]) { + if (argc != ARG_COUNT) { + show_usage(argv[0]); + return 1; + } + std::string fileName; + std::string cppOutputName; + std::string arrayName; + std::string platform; + size_t size = 0; + std::fstream inputFile; + bool isSpirV; + for (int i = 1; i < argc; i++) { + std::string arg = argv[i]; + if ((arg == "-f") || (arg == "--file")) { + fileName = argv[++i]; + } else if ((arg == "-o") || (arg == "--output")) { + cppOutputName = argv[++i]; + } else if ((arg == "-a") || (arg == "--array")) { + arrayName = argv[++i]; + } else if ((arg == "-p") || (arg == "--platform")) { + platform = argv[++i]; + } else { + return 1; + } + } + inputFile.open(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate); + + if (inputFile.is_open()) { + size = static_cast(inputFile.tellg()); + std::unique_ptr memblock = std::make_unique(size); + inputFile.clear(); + inputFile.seekg(0, std::ios::beg); + inputFile.read(reinterpret_cast(memblock.get()), size); + inputFile.close(); + isSpirV = fileName.find(".spv") != std::string::npos; + std::string cpp = parseToCharArray(move(memblock), size, arrayName, platform, isSpirV); + std::fstream(cppOutputName.c_str(), std::ios::out | std::ios::binary).write(cpp.c_str(), cpp.size()); + } else { + std::cerr << "File cannot be opened!" << std::endl; + return 1; + } + return 0; +} \ No newline at end of file diff --git a/shared/source/built_ins/kernels/CMakeLists.txt b/shared/source/built_ins/kernels/CMakeLists.txt index 22f42e2983..308a6cb3bd 100644 --- a/shared/source/built_ins/kernels/CMakeLists.txt +++ b/shared/source/built_ins/kernels/CMakeLists.txt @@ -53,9 +53,11 @@ function(compile_builtin gen_type platform_type builtin bits builtin_options) set(OUTPUT_FILES ${OUTPUTPATH_BASE}.spv ${OUTPUTPATH_BASE}.bin - ${OUTPUTPATH_BASE}.cpp ${OUTPUTPATH_BASE}.gen ) + set(OUTPUT_FILE_CPP + ${OUTPUTPATH_BASE}.cpp + ) # function returns builtin cpp filename unset(BUILTIN_CPP) @@ -76,10 +78,16 @@ function(compile_builtin gen_type platform_type builtin bits builtin_options) list(APPEND __cloc__options__ "-cl-kernel-arg-info") add_custom_command( OUTPUT ${OUTPUT_FILES} - COMMAND ${cloc_cmd_prefix} -q -file ${FILENAME} -device ${DEFAULT_SUPPORTED_${gen_type}_${platform_type}_PLATFORM} ${builtin_options} -${bits} -out_dir ${OUTPUTDIR} -cpp_file -options "$" + COMMAND ${cloc_cmd_prefix} -q -file ${FILENAME} -device ${DEFAULT_SUPPORTED_${gen_type}_${platform_type}_PLATFORM} ${builtin_options} -${bits} -out_dir ${OUTPUTDIR} -options "$" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${builtin} ocloc copy_compiler_files ) + add_custom_command( + OUTPUT ${OUTPUT_FILE_CPP} + COMMAND $ --file ${OUTPUTPATH_BASE}.gen --output ${OUTPUT_FILE_CPP} --array ${BASENAME} --platform ${family_name_with_type} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + DEPENDS ${OUTPUTPATH_BASE}.gen $ + ) endfunction() macro(macro_for_each_gen)