From 6b41d35aaafb8118a71526ab5bd89eef4ea90eec Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 7 Aug 2025 21:54:29 +0900 Subject: [PATCH 01/10] Revert "Hotfix: stddef.h is needed for size_t" This reverts commit dfb29e67d9e3923b5c86c3e07a6a7f2b5ada7597. --- include/clspv/Compiler.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/clspv/Compiler.h b/include/clspv/Compiler.h index 13594a02b..5bb2a206e 100644 --- a/include/clspv/Compiler.h +++ b/include/clspv/Compiler.h @@ -23,7 +23,6 @@ #else #include #include -#include #endif #ifdef __cplusplus From 09c7f2145372d97eca0063ba9cc2c3dc6dd43584 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 7 Aug 2025 21:54:44 +0900 Subject: [PATCH 02/10] Revert "C interface header" This reverts commit fd091a36944216b3cc5777e9ad8b99dbec9d555d. --- include/clspv/Compiler.h | 40 ++++++++++------------------------------ lib/Compiler.cpp | 7 ++++--- 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/include/clspv/Compiler.h b/include/clspv/Compiler.h index 5bb2a206e..7ce52c1ac 100644 --- a/include/clspv/Compiler.h +++ b/include/clspv/Compiler.h @@ -15,17 +15,11 @@ #ifndef CLSPV_INCLUDE_CLSPV_COMPILER_H_ #define CLSPV_INCLUDE_CLSPV_COMPILER_H_ -#ifdef __cplusplus #include #include #include #include -#else -#include -#include -#endif -#ifdef __cplusplus namespace clspv { // DEPRECATED: This function will be replaced by an expanded API. int Compile(const int argc, const char *const argv[]); @@ -50,7 +44,6 @@ int CompileFromSourcesString(const std::vector &programs, std::vector *output_buffer, std::string *output_log); } // namespace clspv -#endif // C API typedef enum ClspvError { @@ -66,14 +59,6 @@ typedef enum ClspvError { #define EXPORT __attribute__((visibility("default"))) #endif -#ifndef clspv_restrict - -/** Optional restrict qualifier for given C API with C++ implementation where - * restrict is not the thing. */ -#define clspv_restrict - -#endif - #ifdef __cplusplus extern "C" { #endif @@ -89,27 +74,22 @@ extern "C" { // |options| - String of options to pass to Clspv compiler. // |output_binary| - Handle to compiler output/result. // |output_binary_size| - Size of compiler output/result (in bytes). -// |output_log| - Handle to compiler build log, -// will assume log is not needed if param is NULL. -// its value will be NULL when no build log is empty. +// |output_log| - Handle to compiler build log. EXPORT ClspvError clspvCompileFromSourcesString( - const size_t program_count, const size_t *clspv_restrict program_sizes, - const char *const clspv_restrict *clspv_restrict programs, - const char *clspv_restrict options, - char *clspv_restrict *clspv_restrict output_binary, - size_t *clspv_restrict output_binary_size, - char *clspv_restrict *clspv_restrict output_log_opt); + const size_t program_count, const size_t *program_sizes, + const char **programs, const char *options, char **output_binary, + size_t *output_binary_size, char **output_log); // Frees the output memory from clspvCompileFromSourcesString // // |output_binary| - Handle to spv // |output_log| - Handle to compiler build log -static inline void clspvFreeOutputBuildObjs(char *clspv_restrict output_binary, - char *clspv_restrict output_log) { - if (output_binary) - free(output_binary); - if (output_log) - free(output_log); +static inline void clspvFreeOutputBuildObjs(char *output_binary, + char *output_log) { + free(output_binary); + output_binary = NULL; + free(output_log); + output_log = NULL; } #ifdef __cplusplus diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp index 2a050b7a5..c107953f4 100644 --- a/lib/Compiler.cpp +++ b/lib/Compiler.cpp @@ -245,7 +245,7 @@ clang::TargetInfo *PrepareTargetInfo(CompilerInstance &instance) { enabled) { instance.getPreprocessorOpts().addMacroDef(str); } - if (feat == clspv::FeatureMacro::__opencl_c_int64 && !enabled) { + if (feat == clspv::FeatureMacro::__opencl_c_int64 && !enabled){ instance.getPreprocessorOpts().addMacroUndef(str); } } @@ -712,7 +712,7 @@ int RunPassPipeline(llvm::Module &M, llvm::raw_svector_ostream *binaryStream) { pm.addPass(clspv::SimplifyPointerBitcastPass()); pm.addPass(clspv::ReplacePointerBitcastPass()); pm.addPass(llvm::createModuleToFunctionPassAdaptor(llvm::DCEPass())); - + pm.addPass(clspv::UndoTranslateSamplerFoldPass()); if (clspv::Option::ModuleConstantsInStorageBuffer()) { @@ -1206,6 +1206,7 @@ int CompilePrograms(const std::vector &programs, ProgramToModule(context, "source", program, output_log, &error)); if (error != 0) return error; + } assert(modules.size() > 0 && modules.back() != nullptr); @@ -1351,7 +1352,7 @@ int CompileFromSourceString(const std::string &program, // C API ClspvError clspvCompileFromSourcesString( const size_t program_count, const size_t *program_sizes, - const char *const *programs, const char *options, char **output_binary, + const char **programs, const char *options, char **output_binary, size_t *output_binary_size, char **output_log) { if (programs == nullptr || program_count == 0 || output_binary == nullptr || output_binary_size == nullptr) { From c4c8c9a57bdfcd901d212c95eb96e17c59d78ba0 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 7 Aug 2025 22:01:10 +0900 Subject: [PATCH 03/10] Formatting is needed yet. Changes to be committed: modified: include/clspv/Compiler.h modified: lib/Compiler.cpp --- include/clspv/Compiler.h | 8 ++++++++ lib/Compiler.cpp | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/clspv/Compiler.h b/include/clspv/Compiler.h index 7ce52c1ac..d27a4b7ab 100644 --- a/include/clspv/Compiler.h +++ b/include/clspv/Compiler.h @@ -15,11 +15,18 @@ #ifndef CLSPV_INCLUDE_CLSPV_COMPILER_H_ #define CLSPV_INCLUDE_CLSPV_COMPILER_H_ +#ifdef __cplusplus #include #include #include #include +#else +#include +#include +#include +#endif +#ifdef __cplusplus namespace clspv { // DEPRECATED: This function will be replaced by an expanded API. int Compile(const int argc, const char *const argv[]); @@ -44,6 +51,7 @@ int CompileFromSourcesString(const std::vector &programs, std::vector *output_buffer, std::string *output_log); } // namespace clspv +#endif // C API typedef enum ClspvError { diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp index c107953f4..c9f2a7e0f 100644 --- a/lib/Compiler.cpp +++ b/lib/Compiler.cpp @@ -1378,6 +1378,7 @@ ClspvError clspvCompileFromSourcesString( err = clspv::CompileFromSourcesString(vPrograms, sOptions, &binary, &buildLog); + if(output_log) { if (!buildLog.empty()) { // Alloc and copy backing mem for build log *output_log = static_cast(std::malloc(buildLog.size() + 1)); @@ -1386,6 +1387,9 @@ ClspvError clspvCompileFromSourcesString( } std::memcpy(static_cast(*output_log), buildLog.c_str(), buildLog.size() + 1); + } else { + *(output_log) = static_cast(NULL); + } } if (err != 0) { @@ -1394,11 +1398,17 @@ ClspvError clspvCompileFromSourcesString( // Alloc and copy backing mem for spv output size_t spv_bytes = binary.size() * sizeof(uint32_t); + if(!spv_bytes) { + /** Early return: when allocation is not needed. */ + *spv_bytes = static_cast(NULL); + return CLSPV_SUCCESS; + } + *output_binary = static_cast(std::malloc(spv_bytes)); if (*output_binary == NULL) { return CLSPV_OUT_OF_HOST_MEM; } - std::memcpy(static_cast(*output_binary), binary.data(), spv_bytes); + std::memcpy(static_cast(*output_binary), binary.data(), spv_bytes); *output_binary_size = spv_bytes; return CLSPV_SUCCESS; From ebb0084c51e15e6d58f04c88957a6bd762fc3f55 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 7 Aug 2025 22:14:02 +0900 Subject: [PATCH 04/10] Formatted --- include/clspv/Compiler.h | 2 +- lib/Compiler.cpp | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/include/clspv/Compiler.h b/include/clspv/Compiler.h index d27a4b7ab..bb0275912 100644 --- a/include/clspv/Compiler.h +++ b/include/clspv/Compiler.h @@ -21,9 +21,9 @@ #include #include #else +#include #include #include -#include #endif #ifdef __cplusplus diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp index c9f2a7e0f..8d01df831 100644 --- a/lib/Compiler.cpp +++ b/lib/Compiler.cpp @@ -1378,18 +1378,18 @@ ClspvError clspvCompileFromSourcesString( err = clspv::CompileFromSourcesString(vPrograms, sOptions, &binary, &buildLog); - if(output_log) { - if (!buildLog.empty()) { - // Alloc and copy backing mem for build log - *output_log = static_cast(std::malloc(buildLog.size() + 1)); - if (*output_log == NULL) { - return CLSPV_OUT_OF_HOST_MEM; + if (output_log) { + if (!buildLog.empty()) { + // Alloc and copy backing mem for build log + *output_log = static_cast(std::malloc(buildLog.size() + 1)); + if (*output_log == NULL) { + return CLSPV_OUT_OF_HOST_MEM; + } + std::memcpy(static_cast(*output_log), buildLog.c_str(), + buildLog.size() + 1); + } else { + *(output_log) = static_cast(NULL); } - std::memcpy(static_cast(*output_log), buildLog.c_str(), - buildLog.size() + 1); - } else { - *(output_log) = static_cast(NULL); - } } if (err != 0) { @@ -1398,17 +1398,17 @@ ClspvError clspvCompileFromSourcesString( // Alloc and copy backing mem for spv output size_t spv_bytes = binary.size() * sizeof(uint32_t); - if(!spv_bytes) { - /** Early return: when allocation is not needed. */ - *spv_bytes = static_cast(NULL); - return CLSPV_SUCCESS; + if (!spv_bytes) { + /** Early return: when allocation is not needed. */ + *spv_bytes = static_cast(NULL); + return CLSPV_SUCCESS; } *output_binary = static_cast(std::malloc(spv_bytes)); if (*output_binary == NULL) { return CLSPV_OUT_OF_HOST_MEM; } - std::memcpy(static_cast(*output_binary), binary.data(), spv_bytes); + std::memcpy(static_cast(*output_binary), binary.data(), spv_bytes); *output_binary_size = spv_bytes; return CLSPV_SUCCESS; From 4f7702212b3360d886d93b1fe50a0efe68da921d Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 7 Aug 2025 22:19:04 +0900 Subject: [PATCH 05/10] Comment has been added. Changes to be committed: modified: include/clspv/Compiler.h --- include/clspv/Compiler.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/clspv/Compiler.h b/include/clspv/Compiler.h index bb0275912..72c0cc930 100644 --- a/include/clspv/Compiler.h +++ b/include/clspv/Compiler.h @@ -82,7 +82,8 @@ extern "C" { // |options| - String of options to pass to Clspv compiler. // |output_binary| - Handle to compiler output/result. // |output_binary_size| - Size of compiler output/result (in bytes). -// |output_log| - Handle to compiler build log. +// |output_log| - Handle to compiler build log. `output_log` can be NULL +// in which case no log will be generated. EXPORT ClspvError clspvCompileFromSourcesString( const size_t program_count, const size_t *program_sizes, const char **programs, const char *options, char **output_binary, From 840a53928b7bc7d0c0169d4ff25d7800e860e613 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 7 Aug 2025 22:45:09 +0900 Subject: [PATCH 06/10] Fixed a bug and improved readability Changes to be committed: modified: lib/Compiler.cpp --- lib/Compiler.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp index 8d01df831..7b9cf21f9 100644 --- a/lib/Compiler.cpp +++ b/lib/Compiler.cpp @@ -1388,7 +1388,7 @@ ClspvError clspvCompileFromSourcesString( std::memcpy(static_cast(*output_log), buildLog.c_str(), buildLog.size() + 1); } else { - *(output_log) = static_cast(NULL); + *(output_log) = static_cast(NULL); } } @@ -1398,9 +1398,9 @@ ClspvError clspvCompileFromSourcesString( // Alloc and copy backing mem for spv output size_t spv_bytes = binary.size() * sizeof(uint32_t); - if (!spv_bytes) { + if (spv_bytes == 0) { /** Early return: when allocation is not needed. */ - *spv_bytes = static_cast(NULL); + *output_binary = static_cast(NULL); return CLSPV_SUCCESS; } From 6b0faf6afbd9e0908655dca4d7f20fbae14205a3 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 7 Aug 2025 22:46:59 +0900 Subject: [PATCH 07/10] Improved readability Changes to be committed: modified: lib/Compiler.cpp --- lib/Compiler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp index 7b9cf21f9..46e4db764 100644 --- a/lib/Compiler.cpp +++ b/lib/Compiler.cpp @@ -1378,7 +1378,7 @@ ClspvError clspvCompileFromSourcesString( err = clspv::CompileFromSourcesString(vPrograms, sOptions, &binary, &buildLog); - if (output_log) { + if (output_log != NULL) { if (!buildLog.empty()) { // Alloc and copy backing mem for build log *output_log = static_cast(std::malloc(buildLog.size() + 1)); From 41ae26f160f4a029f6c89baa485fca77622a385b Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 7 Aug 2025 22:50:20 +0900 Subject: [PATCH 08/10] Resovled type mismatch Changes to be committed: modified: lib/Compiler.cpp --- lib/Compiler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp index 46e4db764..dd33762c6 100644 --- a/lib/Compiler.cpp +++ b/lib/Compiler.cpp @@ -1388,7 +1388,7 @@ ClspvError clspvCompileFromSourcesString( std::memcpy(static_cast(*output_log), buildLog.c_str(), buildLog.size() + 1); } else { - *(output_log) = static_cast(NULL); + *(output_log) = static_cast(NULL); } } @@ -1400,7 +1400,7 @@ ClspvError clspvCompileFromSourcesString( size_t spv_bytes = binary.size() * sizeof(uint32_t); if (spv_bytes == 0) { /** Early return: when allocation is not needed. */ - *output_binary = static_cast(NULL); + *output_binary = static_cast(NULL); return CLSPV_SUCCESS; } From 9731b47c6508e0b7639cc55151becd4820de5934 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 7 Aug 2025 22:54:33 +0900 Subject: [PATCH 09/10] Clang formatted Changes to be committed: modified: lib/Compiler.cpp --- lib/Compiler.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp index dd33762c6..cdd1cac15 100644 --- a/lib/Compiler.cpp +++ b/lib/Compiler.cpp @@ -1206,7 +1206,6 @@ int CompilePrograms(const std::vector &programs, ProgramToModule(context, "source", program, output_log, &error)); if (error != 0) return error; - } assert(modules.size() > 0 && modules.back() != nullptr); From e57684b789174a37f8d9060edb73bbec655bbea0 Mon Sep 17 00:00:00 2001 From: ae2f Date: Thu, 7 Aug 2025 22:58:39 +0900 Subject: [PATCH 10/10] Revert "Clang formatted" This reverts commit 9731b47c6508e0b7639cc55151becd4820de5934. Changes to be committed: modified: lib/Compiler.cpp --- lib/Compiler.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp index cdd1cac15..dd33762c6 100644 --- a/lib/Compiler.cpp +++ b/lib/Compiler.cpp @@ -1206,6 +1206,7 @@ int CompilePrograms(const std::vector &programs, ProgramToModule(context, "source", program, output_log, &error)); if (error != 0) return error; + } assert(modules.size() > 0 && modules.back() != nullptr);