From 4fc861358ec5e66106195ca2daefa42dd38cdcd6 Mon Sep 17 00:00:00 2001 From: Starla Huang <82885378+starlahuang@users.noreply.github.com> Date: Tue, 9 Sep 2025 16:50:19 +0100 Subject: [PATCH] Resolve #852 issue (#1220) There are two changes in total. The first one to fix a small issue of current working directory so that second change can be applied. And the second one is for resolving #852 Removing hard-coded SPIR-V binaries in clUnloadPlatformCompiler tests. Fixes #852 --------- Signed-off-by: Ahmed Hesham Signed-off-by: Xing Huang --- test_conformance/compiler/CMakeLists.txt | 26 ++++- .../{ => spirv_asm}/write_kernel.spvasm32 | 0 .../{ => spirv_asm}/write_kernel.spvasm64 | 0 test_conformance/compiler/spirv_asm_list.txt | 4 + test_conformance/compiler/test_compile.cpp | 104 +++++------------- .../test_unload_platform_compiler.cpp | 59 +++++++--- ...est_unload_platform_compiler_resources.hpp | 44 -------- 7 files changed, 103 insertions(+), 134 deletions(-) rename test_conformance/compiler/{ => spirv_asm}/write_kernel.spvasm32 (100%) rename test_conformance/compiler/{ => spirv_asm}/write_kernel.spvasm64 (100%) create mode 100644 test_conformance/compiler/spirv_asm_list.txt diff --git a/test_conformance/compiler/CMakeLists.txt b/test_conformance/compiler/CMakeLists.txt index b64d3b31..498c6218 100644 --- a/test_conformance/compiler/CMakeLists.txt +++ b/test_conformance/compiler/CMakeLists.txt @@ -17,10 +17,31 @@ set(${MODULE_NAME}_SOURCES include(../CMakeCommon.txt) +# Include the relative paths to SPV assembly files +configure_file(spirv_asm_list.txt ${CMAKE_CURRENT_BINARY_DIR}/spirv_asm_list.txt) +include(${CMAKE_CURRENT_BINARY_DIR}/spirv_asm_list.txt) + +# Determine the corresponding binary outputs to the SPV assembly input files +set(COMPILER_ASM_REL_PATH spirv_asm) +set(COMPILER_ASM_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${COMPILER_ASM_REL_PATH}") +set(COMPILER_SPV_PATH "${CMAKE_CURRENT_BINARY_DIR}/spirv_bin") + # Copy the required test include directories into the build directory. if(NOT DEFINED COMPILER_TEST_RESOURCES) set(COMPILER_TEST_RESOURCES $) endif() + +set(COMPILER_SPV_EXTRA "") +if(SPIRV_TOOLS_DIR AND IS_ABSOLUTE "${SPIRV_TOOLS_DIR}" AND + IS_DIRECTORY "${SPIRV_TOOLS_DIR}") + message("Using SPIR-V tools from '${SPIRV_TOOLS_DIR}'") + set(COMPILER_SPV_EXTRA "--assembler=${SPIRV_TOOLS_DIR}/spirv-as" "--validator=${SPIRV_TOOLS_DIR}/spirv-val") +endif() +set(COMPILER_ASSEMBLY_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/../spirv_new/spirv_asm/assemble_spirv.py) + +include(CMakePrintHelpers) +cmake_print_variables(COMPILER_ASSEMBLY_SCRIPT) + add_custom_command( COMMENT "Copying compiler test resources..." TARGET ${${MODULE_NAME}_OUT} @@ -30,7 +51,10 @@ add_custom_command( ${COMPILER_TEST_RESOURCES}/includeTestDirectory COMMAND ${CMAKE_COMMAND} -E copy_directory ${CLConform_SOURCE_DIR}/test_conformance/compiler/secondIncludeTestDirectory - ${COMPILER_TEST_RESOURCES}/secondIncludeTestDirectory) + ${COMPILER_TEST_RESOURCES}/secondIncludeTestDirectory + COMMAND ${COMPILER_ASSEMBLY_SCRIPT} --source-dir "${COMPILER_ASM_PATH}" --output-dir "${COMPILER_SPV_PATH}" ${COMPILER_SPV_EXTRA} --verbose + DEPENDS ${COMPILER_ASSEMBLY_SCRIPT} ${COMPILER_ASM} + VERBATIM) include(GNUInstallDirs) diff --git a/test_conformance/compiler/write_kernel.spvasm32 b/test_conformance/compiler/spirv_asm/write_kernel.spvasm32 similarity index 100% rename from test_conformance/compiler/write_kernel.spvasm32 rename to test_conformance/compiler/spirv_asm/write_kernel.spvasm32 diff --git a/test_conformance/compiler/write_kernel.spvasm64 b/test_conformance/compiler/spirv_asm/write_kernel.spvasm64 similarity index 100% rename from test_conformance/compiler/write_kernel.spvasm64 rename to test_conformance/compiler/spirv_asm/write_kernel.spvasm64 diff --git a/test_conformance/compiler/spirv_asm_list.txt b/test_conformance/compiler/spirv_asm_list.txt new file mode 100644 index 00000000..8150ce28 --- /dev/null +++ b/test_conformance/compiler/spirv_asm_list.txt @@ -0,0 +1,4 @@ +set(COMPILER_SPIRV_NEW_ASM +compiler_spirv_asm/write_kernel.spvasm32 +compiler_spirv_asm/write_kernel.spvasm64 +) diff --git a/test_conformance/compiler/test_compile.cpp b/test_conformance/compiler/test_compile.cpp index 907de9cb..70ca9449 100644 --- a/test_conformance/compiler/test_compile.cpp +++ b/test_conformance/compiler/test_compile.cpp @@ -14,6 +14,9 @@ // limitations under the License. // #include "testBase.h" + +#include + #if defined(_WIN32) #include #elif defined(__linux__) || defined(__APPLE__) @@ -3020,15 +3023,6 @@ REGISTER_TEST(execute_after_embedded_header_link) return 0; } -#if defined(__APPLE__) || defined(__linux) -#define _mkdir(x) mkdir(x, S_IRWXU) -#define _chdir chdir -#define _rmdir rmdir -#define _unlink unlink -#else -#include -#endif - REGISTER_TEST(execute_after_included_header_link) { int error; @@ -3047,100 +3041,60 @@ REGISTER_TEST(execute_after_included_header_link) } /* setup */ -#if (defined(__linux__) || defined(__APPLE__)) && (!defined(__ANDROID__)) - /* Some tests systems doesn't allow one to write in the test directory */ - if (_chdir("/tmp") != 0) + std::error_code ec; + auto temp_dir_path = std::filesystem::temp_directory_path(ec); + if (ec) { - log_error("ERROR: Unable to remove directory foo/bar! (in %s:%d)\n", - __FILE__, __LINE__); + log_error("ERROR: Unable to get the temporary directory path\n"); return -1; } -#endif - if (_mkdir("foo") != 0) + temp_dir_path = temp_dir_path / "foo" / "bar"; + std::filesystem::create_directories(temp_dir_path, ec); + if (ec) { - log_error("ERROR: Unable to create directory foo! (in %s:%d)\n", - __FILE__, __LINE__); + log_error("ERROR: Unable to create directory: %s, error: %d (%s)\n", + temp_dir_path.u8string().c_str(), ec.value(), + ec.message().c_str()); return -1; } - if (_mkdir("foo/bar") != 0) - { - log_error("ERROR: Unable to create directory foo/bar! (in %s:%d)\n", - __FILE__, __LINE__); - return -1; - } - if (_chdir("foo/bar") != 0) - { - log_error("ERROR: Unable to change to directory foo/bar! (in %s:%d)\n", - __FILE__, __LINE__); - return -1; - } - FILE *simple_header_file = fopen(simple_header_name, "w"); + + const auto simple_header_path = temp_dir_path / simple_header_name; + FILE *simple_header_file = + fopen(simple_header_path.u8string().c_str(), "w"); if (simple_header_file == NULL) { log_error("ERROR: Unable to create simple header file %s! (in %s:%d)\n", - simple_header_name, __FILE__, __LINE__); + simple_header_path.u8string().c_str(), __FILE__, __LINE__); return -1; } if (fprintf(simple_header_file, "%s", simple_header) < 0) { log_error( "ERROR: Unable to write to simple header file %s! (in %s:%d)\n", - simple_header_name, __FILE__, __LINE__); + simple_header_path.u8string().c_str(), __FILE__, __LINE__); return -1; } if (fclose(simple_header_file) != 0) { log_error("ERROR: Unable to close simple header file %s! (in %s:%d)\n", - simple_header_name, __FILE__, __LINE__); + simple_header_path.u8string().c_str(), __FILE__, __LINE__); return -1; } - if (_chdir("../..") != 0) - { - log_error("ERROR: Unable to change to original working directory! (in " - "%s:%d)\n", - __FILE__, __LINE__); - return -1; - } -#if (defined(__linux__) || defined(__APPLE__)) && (!defined(__ANDROID__)) - error = clCompileProgram(program, 1, &device, "-I/tmp/foo/bar", 0, NULL, + + const std::string include_path = + std::string("-I") + temp_dir_path.generic_u8string(); + error = clCompileProgram(program, 1, &device, include_path.c_str(), 0, NULL, NULL, NULL, NULL); -#else - error = clCompileProgram(program, 1, &device, "-Ifoo/bar", 0, NULL, NULL, - NULL, NULL); -#endif test_error(error, "Unable to compile a simple program with included header"); /* cleanup */ - if (_chdir("foo/bar") != 0) + std::filesystem::remove_all(temp_dir_path, ec); + if (ec) { - log_error("ERROR: Unable to change to directory foo/bar! (in %s:%d)\n", - __FILE__, __LINE__); - return -1; - } - if (_unlink(simple_header_name) != 0) - { - log_error("ERROR: Unable to remove simple header file %s! (in %s:%d)\n", - simple_header_name, __FILE__, __LINE__); - return -1; - } - if (_chdir("../..") != 0) - { - log_error("ERROR: Unable to change to original working directory! (in " - "%s:%d)\n", - __FILE__, __LINE__); - return -1; - } - if (_rmdir("foo/bar") != 0) - { - log_error("ERROR: Unable to remove directory foo/bar! (in %s:%d)\n", - __FILE__, __LINE__); - return -1; - } - if (_rmdir("foo") != 0) - { - log_error("ERROR: Unable to remove directory foo! (in %s:%d)\n", - __FILE__, __LINE__); + log_error("ERROR: Unable to delete directory: %s, error: %d (%s)", + temp_dir_path.u8string().c_str(), ec.value(), + ec.message().c_str()); return -1; } diff --git a/test_conformance/compiler/test_unload_platform_compiler.cpp b/test_conformance/compiler/test_unload_platform_compiler.cpp index 84442080..bb41f64d 100644 --- a/test_conformance/compiler/test_unload_platform_compiler.cpp +++ b/test_conformance/compiler/test_unload_platform_compiler.cpp @@ -25,6 +25,43 @@ #include #include #include +#include + +#if defined(_WIN32) +const std::string slash = "\\"; +#else +const std::string slash = "/"; +#endif +std::string compilerSpvBinaries = "test_conformance" + slash + "compiler" + + slash + "spirv_bin" + slash + "write_kernel.spv"; + +const std::string spvExt = ".spv"; + +std::vector readBinary(const char *file_name) +{ + using namespace std; + + ifstream file(file_name, ios::in | ios::binary | ios::ate); + + std::vector tmpBuffer(0); + + if (file.is_open()) + { + size_t size = file.tellg(); + tmpBuffer.resize(size); + file.seekg(0, ios::beg); + file.read(&tmpBuffer[0], size); + file.close(); + } + else + { + log_error("File %s not found\n", file_name); + } + + std::vector result(tmpBuffer.begin(), tmpBuffer.end()); + + return result; +} namespace { @@ -299,18 +336,12 @@ public: throw unload_test_failure("Failure getting device address bits"); } - switch (address_bits) - { - case 32: - m_spirv_binary = write_kernel_32_spv.data(); - m_spirv_size = write_kernel_32_spv.size(); - break; - case 64: - m_spirv_binary = write_kernel_64_spv.data(); - m_spirv_size = write_kernel_64_spv.size(); - break; - default: throw unload_test_failure("Invalid address bits"); - } + std::vector kernel_buffer; + + std::string file_name = + compilerSpvBinaries + std::to_string(address_bits); + m_spirv_binary = readBinary(file_name.c_str()); + m_spirv_size = m_spirv_binary.size(); } void create() final @@ -320,7 +351,7 @@ public: assert(nullptr == m_program); cl_int err = CL_INVALID_PLATFORM; - m_program = m_CreateProgramWithIL(m_context, m_spirv_binary, + m_program = m_CreateProgramWithIL(m_context, &m_spirv_binary[0], m_spirv_size, &err); if (CL_SUCCESS != err) throw unload_test_failure("clCreateProgramWithIL()", err); @@ -347,7 +378,7 @@ public: } private: - void *m_spirv_binary; + std::vector m_spirv_binary; size_t m_spirv_size; bool m_enabled; diff --git a/test_conformance/compiler/test_unload_platform_compiler_resources.hpp b/test_conformance/compiler/test_unload_platform_compiler_resources.hpp index 82f87ffc..a529c212 100644 --- a/test_conformance/compiler/test_unload_platform_compiler_resources.hpp +++ b/test_conformance/compiler/test_unload_platform_compiler_resources.hpp @@ -4,47 +4,3 @@ static const char write_kernel_source[] = R"( kernel void write_kernel(global unsigned int *p) { *p = 42; })"; - -/* Assembled SPIR-V 1.0 binary from write_kernel.spvasm64 */ -static std::array write_kernel_64_spv{ - { 0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, - 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, - 0x04, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00, - 0x0e, 0x00, 0x03, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, - 0x0f, 0x00, 0x07, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - 0x77, 0x72, 0x69, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x72, 0x6e, 0x65, 0x6c, - 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, - 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, - 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, - 0x13, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, - 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, - 0x21, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, - 0x05, 0x00, 0x00, 0x00, 0x36, 0x00, 0x05, 0x00, 0x04, 0x00, 0x00, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, - 0x37, 0x00, 0x03, 0x00, 0x05, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, - 0xf8, 0x00, 0x02, 0x00, 0x08, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x05, 0x00, - 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00 } -}; - -/* Assembled SPIR-V 1.0 binary from write_kernel.spvasm32 */ -static std::array write_kernel_32_spv{ - { 0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, - 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, - 0x04, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00, - 0x0e, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, - 0x0f, 0x00, 0x07, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - 0x77, 0x72, 0x69, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x72, 0x6e, 0x65, 0x6c, - 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, - 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, - 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, - 0x13, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, - 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, - 0x21, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, - 0x05, 0x00, 0x00, 0x00, 0x36, 0x00, 0x05, 0x00, 0x04, 0x00, 0x00, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, - 0x37, 0x00, 0x03, 0x00, 0x05, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, - 0xf8, 0x00, 0x02, 0x00, 0x08, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x05, 0x00, - 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00 } -};