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 <ahmed.hesham@arm.com>
Signed-off-by: Xing Huang <xing.huang@arm.com>
This commit is contained in:
Starla Huang
2025-09-09 16:50:19 +01:00
committed by GitHub
parent df61cad39f
commit 4fc861358e
7 changed files with 103 additions and 134 deletions

View File

@@ -17,10 +17,31 @@ set(${MODULE_NAME}_SOURCES
include(../CMakeCommon.txt) 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. # Copy the required test include directories into the build directory.
if(NOT DEFINED COMPILER_TEST_RESOURCES) if(NOT DEFINED COMPILER_TEST_RESOURCES)
set(COMPILER_TEST_RESOURCES $<TARGET_FILE_DIR:${${MODULE_NAME}_OUT}>) set(COMPILER_TEST_RESOURCES $<TARGET_FILE_DIR:${${MODULE_NAME}_OUT}>)
endif() 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( add_custom_command(
COMMENT "Copying compiler test resources..." COMMENT "Copying compiler test resources..."
TARGET ${${MODULE_NAME}_OUT} TARGET ${${MODULE_NAME}_OUT}
@@ -30,7 +51,10 @@ add_custom_command(
${COMPILER_TEST_RESOURCES}/includeTestDirectory ${COMPILER_TEST_RESOURCES}/includeTestDirectory
COMMAND ${CMAKE_COMMAND} -E copy_directory COMMAND ${CMAKE_COMMAND} -E copy_directory
${CLConform_SOURCE_DIR}/test_conformance/compiler/secondIncludeTestDirectory ${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) include(GNUInstallDirs)

View File

@@ -0,0 +1,4 @@
set(COMPILER_SPIRV_NEW_ASM
compiler_spirv_asm/write_kernel.spvasm32
compiler_spirv_asm/write_kernel.spvasm64
)

View File

@@ -14,6 +14,9 @@
// limitations under the License. // limitations under the License.
// //
#include "testBase.h" #include "testBase.h"
#include <filesystem>
#if defined(_WIN32) #if defined(_WIN32)
#include <time.h> #include <time.h>
#elif defined(__linux__) || defined(__APPLE__) #elif defined(__linux__) || defined(__APPLE__)
@@ -3020,15 +3023,6 @@ REGISTER_TEST(execute_after_embedded_header_link)
return 0; 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 <direct.h>
#endif
REGISTER_TEST(execute_after_included_header_link) REGISTER_TEST(execute_after_included_header_link)
{ {
int error; int error;
@@ -3047,100 +3041,60 @@ REGISTER_TEST(execute_after_included_header_link)
} }
/* setup */ /* setup */
#if (defined(__linux__) || defined(__APPLE__)) && (!defined(__ANDROID__)) std::error_code ec;
/* Some tests systems doesn't allow one to write in the test directory */ auto temp_dir_path = std::filesystem::temp_directory_path(ec);
if (_chdir("/tmp") != 0) if (ec)
{ {
log_error("ERROR: Unable to remove directory foo/bar! (in %s:%d)\n", log_error("ERROR: Unable to get the temporary directory path\n");
__FILE__, __LINE__);
return -1; return -1;
} }
#endif temp_dir_path = temp_dir_path / "foo" / "bar";
if (_mkdir("foo") != 0) std::filesystem::create_directories(temp_dir_path, ec);
if (ec)
{ {
log_error("ERROR: Unable to create directory foo! (in %s:%d)\n", log_error("ERROR: Unable to create directory: %s, error: %d (%s)\n",
__FILE__, __LINE__); temp_dir_path.u8string().c_str(), ec.value(),
ec.message().c_str());
return -1; return -1;
} }
if (_mkdir("foo/bar") != 0)
{ const auto simple_header_path = temp_dir_path / simple_header_name;
log_error("ERROR: Unable to create directory foo/bar! (in %s:%d)\n", FILE *simple_header_file =
__FILE__, __LINE__); fopen(simple_header_path.u8string().c_str(), "w");
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");
if (simple_header_file == NULL) if (simple_header_file == NULL)
{ {
log_error("ERROR: Unable to create simple header file %s! (in %s:%d)\n", 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; return -1;
} }
if (fprintf(simple_header_file, "%s", simple_header) < 0) if (fprintf(simple_header_file, "%s", simple_header) < 0)
{ {
log_error( log_error(
"ERROR: Unable to write to simple header file %s! (in %s:%d)\n", "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; return -1;
} }
if (fclose(simple_header_file) != 0) if (fclose(simple_header_file) != 0)
{ {
log_error("ERROR: Unable to close simple header file %s! (in %s:%d)\n", 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; return -1;
} }
if (_chdir("../..") != 0)
{ const std::string include_path =
log_error("ERROR: Unable to change to original working directory! (in " std::string("-I") + temp_dir_path.generic_u8string();
"%s:%d)\n", error = clCompileProgram(program, 1, &device, include_path.c_str(), 0, NULL,
__FILE__, __LINE__);
return -1;
}
#if (defined(__linux__) || defined(__APPLE__)) && (!defined(__ANDROID__))
error = clCompileProgram(program, 1, &device, "-I/tmp/foo/bar", 0, NULL,
NULL, NULL, NULL); NULL, NULL, NULL);
#else
error = clCompileProgram(program, 1, &device, "-Ifoo/bar", 0, NULL, NULL,
NULL, NULL);
#endif
test_error(error, test_error(error,
"Unable to compile a simple program with included header"); "Unable to compile a simple program with included header");
/* cleanup */ /* 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", log_error("ERROR: Unable to delete directory: %s, error: %d (%s)",
__FILE__, __LINE__); temp_dir_path.u8string().c_str(), ec.value(),
return -1; ec.message().c_str());
}
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__);
return -1; return -1;
} }

View File

@@ -25,6 +25,43 @@
#include <string> #include <string>
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <fstream>
#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<unsigned char> readBinary(const char *file_name)
{
using namespace std;
ifstream file(file_name, ios::in | ios::binary | ios::ate);
std::vector<char> 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<unsigned char> result(tmpBuffer.begin(), tmpBuffer.end());
return result;
}
namespace { namespace {
@@ -299,18 +336,12 @@ public:
throw unload_test_failure("Failure getting device address bits"); throw unload_test_failure("Failure getting device address bits");
} }
switch (address_bits) std::vector<unsigned char> kernel_buffer;
{
case 32: std::string file_name =
m_spirv_binary = write_kernel_32_spv.data(); compilerSpvBinaries + std::to_string(address_bits);
m_spirv_size = write_kernel_32_spv.size(); m_spirv_binary = readBinary(file_name.c_str());
break; m_spirv_size = m_spirv_binary.size();
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");
}
} }
void create() final void create() final
@@ -320,7 +351,7 @@ public:
assert(nullptr == m_program); assert(nullptr == m_program);
cl_int err = CL_INVALID_PLATFORM; 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); m_spirv_size, &err);
if (CL_SUCCESS != err) if (CL_SUCCESS != err)
throw unload_test_failure("clCreateProgramWithIL()", err); throw unload_test_failure("clCreateProgramWithIL()", err);
@@ -347,7 +378,7 @@ public:
} }
private: private:
void *m_spirv_binary; std::vector<unsigned char> m_spirv_binary;
size_t m_spirv_size; size_t m_spirv_size;
bool m_enabled; bool m_enabled;

View File

@@ -4,47 +4,3 @@ static const char write_kernel_source[] = R"(
kernel void write_kernel(global unsigned int *p) { kernel void write_kernel(global unsigned int *p) {
*p = 42; *p = 42;
})"; })";
/* Assembled SPIR-V 1.0 binary from write_kernel.spvasm64 */
static std::array<unsigned char, 216> 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<unsigned char, 216> 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 }
};