Skip to content
Merged

. #2

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions include/clspv/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#include <string>
#include <vector>
#else
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#endif

#ifdef __cplusplus
Expand Down Expand Up @@ -67,14 +67,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
Expand All @@ -90,27 +82,23 @@ 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. `output_log` can be NULL
// in which case no log will be generated.
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
Expand Down
31 changes: 21 additions & 10 deletions lib/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -1206,6 +1206,7 @@ int CompilePrograms(const std::vector<std::string> &programs,
ProgramToModule(context, "source", program, output_log, &error));
if (error != 0)
return error;

}
assert(modules.size() > 0 && modules.back() != nullptr);

Expand Down Expand Up @@ -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) {
Expand All @@ -1377,14 +1378,18 @@ ClspvError clspvCompileFromSourcesString(
err =
clspv::CompileFromSourcesString(vPrograms, sOptions, &binary, &buildLog);

if (!buildLog.empty()) {
// Alloc and copy backing mem for build log
*output_log = static_cast<char *>(std::malloc(buildLog.size() + 1));
if (*output_log == NULL) {
return CLSPV_OUT_OF_HOST_MEM;
if (output_log != NULL) {
if (!buildLog.empty()) {
// Alloc and copy backing mem for build log
*output_log = static_cast<char *>(std::malloc(buildLog.size() + 1));
if (*output_log == NULL) {
return CLSPV_OUT_OF_HOST_MEM;
}
std::memcpy(static_cast<void *>(*output_log), buildLog.c_str(),
buildLog.size() + 1);
} else {
*(output_log) = static_cast<char *>(NULL);
}
std::memcpy(static_cast<void *>(*output_log), buildLog.c_str(),
buildLog.size() + 1);
}

if (err != 0) {
Expand All @@ -1393,6 +1398,12 @@ ClspvError clspvCompileFromSourcesString(

// Alloc and copy backing mem for spv output
size_t spv_bytes = binary.size() * sizeof(uint32_t);
if (spv_bytes == 0) {
/** Early return: when allocation is not needed. */
*output_binary = static_cast<char *>(NULL);
return CLSPV_SUCCESS;
}

*output_binary = static_cast<char *>(std::malloc(spv_bytes));
if (*output_binary == NULL) {
return CLSPV_OUT_OF_HOST_MEM;
Expand Down