* \author Benjamin Segovia <benjamin.segovia@intel.com>
*/
+#include "llvm/Config/config.h"
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 2
+#include "llvm/LLVMContext.h"
+#include "llvm/Module.h"
+#include "llvm/DataLayout.h"
+#else
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/DataLayout.h"
+#endif /* LLVM_VERSION_MINOR <= 2 */
+
#include "backend/program.h"
#include "backend/gen_program.h"
#include "backend/gen_program.hpp"
#include "ir/unit.hpp"
#include "llvm/llvm_to_gen.hpp"
+#include <clang/CodeGen/CodeGenAction.h>
+
#include <cstring>
#include <sstream>
#include <memory>
#endif
}
- GenProgram::~GenProgram(void) {}
+ GenProgram::~GenProgram(void){
+#ifdef GBE_COMPILER_AVAILABLE
+ if(module){
+ delete (llvm::Module*)module;
+ module = NULL;
+ }
+
+ if(llvm_ctx){
+ delete (llvm::LLVMContext*)llvm_ctx;
+ llvm_ctx = NULL;
+ }
+#endif
+ }
/*! We must avoid spilling at all cost with Gen */
static const struct CodeGenStrategy {
static gbe_program genProgramNewFromLLVM(uint32_t deviceID,
const char *fileName,
+ const void* module,
+ const void* llvm_ctx,
size_t stringSize,
char *err,
size_t *errSize,
int optLevel)
{
using namespace gbe;
- GenProgram *program = GBE_NEW(GenProgram, deviceID);
+ GenProgram *program = GBE_NEW(GenProgram, deviceID, module, llvm_ctx);
#ifdef GBE_COMPILER_AVAILABLE
std::string error;
// Try to compile the program
- if (program->buildFromLLVMFile(fileName, error, optLevel) == false) {
+ if (program->buildFromLLVMFile(fileName, module, error, optLevel) == false) {
if (err != NULL && errSize != NULL && stringSize > 0u) {
const size_t msgSize = std::min(error.size(), stringSize-1u);
std::memcpy(err, error.c_str(), msgSize);
#include "llvm/Config/config.h"
#include "llvm/Support/Threading.h"
#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Transforms/Utils/Cloning.h"
+#include "llvm/IR/LLVMContext.h"
#include <cstring>
#include <algorithm>
#include <fstream>
#ifdef GBE_COMPILER_AVAILABLE
BVAR(OCL_OUTPUT_GEN_IR, false);
- bool Program::buildFromLLVMFile(const char *fileName, std::string &error, int optLevel) {
+ bool Program::buildFromLLVMFile(const char *fileName, const void* module, std::string &error, int optLevel) {
ir::Unit *unit = new ir::Unit();
- if (llvmToGen(*unit, fileName, optLevel) == false) {
+ llvm::Module * cloned_module = NULL;
+ if(module){
+ cloned_module = llvm::CloneModule((llvm::Module*)module);
+ }
+ if (llvmToGen(*unit, fileName, module, optLevel) == false) {
error = std::string(fileName) + " not found";
return false;
}
if(!unit->getValid()) {
delete unit; //clear unit
unit = new ir::Unit();
- llvmToGen(*unit, fileName, 0); //suppose file exists and llvmToGen will not return false.
+ if(cloned_module){
+ llvmToGen(*unit, fileName, cloned_module, 0); //suppose file exists and llvmToGen will not return false.
+ }else{
+ llvmToGen(*unit, fileName, module, 0); //suppose file exists and llvmToGen will not return false.
+ }
}
assert(unit->getValid());
this->buildFromUnit(*unit, error);
delete unit;
+ if(cloned_module){
+ delete (llvm::Module*) cloned_module;
+ }
return true;
}
SVAR(OCL_PCH_PATH, PCH_OBJECT_DIR);
SVAR(OCL_PCM_PATH, PCM_OBJECT_DIR);
- static bool buildModuleFromSource(const char* input, const char* output, std::string options,
+ static bool buildModuleFromSource(const char* input, llvm::Module** out_module, llvm::LLVMContext* llvm_ctx, std::string options,
size_t stringSize, char *err, size_t *errSize) {
// Arguments to pass to the clang frontend
vector<const char *> args;
}
// Create an action and make the compiler instance carry it out
- llvm::OwningPtr<clang::CodeGenAction> Act(new clang::EmitLLVMOnlyAction());
+ llvm::OwningPtr<clang::CodeGenAction> Act(new clang::EmitLLVMOnlyAction(llvm_ctx));
std::string dirs = OCL_PCM_PATH;
std::string pcmFileName;
llvm::Module *module = Act->takeModule();
-#if (LLVM_VERSION_MAJOR == 3) && (LLVM_VERSION_MINOR > 3)
- auto mode = llvm::sys::fs::F_Binary;
-#else
- auto mode = llvm::raw_fd_ostream::F_Binary;
-#endif
- llvm::raw_fd_ostream OS(output, ErrorString, mode);
- //still write to temp file for code simply, otherwise need add another function.
- //because gbe_program_new_from_llvm also be used by cl_program_create_from_llvm, can't be removed
- //TODO: Pass module to llvmToGen, if use module, should return Act and use OwningPtr out of this funciton
- llvm::WriteBitcodeToFile(module, OS);
- if (err != NULL && *errSize < stringSize - 1 && ErrorString.size() > 0) {
- size_t errLen;
- errLen = ErrorString.copy(err + *errSize, stringSize - *errSize - 1, 0);
- *errSize += errLen;
- }
-
- if (err == NULL || OCL_OUTPUT_BUILD_LOG) {
- // flush the error messages to the errs() if there is no
- // error string buffer.
- llvm::errs() << ErrorString;
- }
- OS.close();
+ *out_module = module;
return true;
}
extern std::string ocl_stdlib_str;
BVAR(OCL_USE_PCH, true);
- static gbe_program programNewFromSource(uint32_t deviceID,
- const char *source,
- size_t stringSize,
- const char *options,
- char *err,
- size_t *errSize)
+ static void processSourceAndOption(const char *source,
+ const char *options,
+ const char *temp_header_path,
+ std::string& clOpt,
+ std::string& clName,
+ int& optLevel)
{
char clStr[] = "/tmp/XXXXXX.cl";
- char llStr[] = "/tmp/XXXXXX.ll";
int clFd = mkstemps(clStr, 3);
- int llFd = mkstemps(llStr, 3);
- close(llFd);
- const std::string clName = std::string(clStr);
- const std::string llName = std::string(llStr);
- std::string clOpt;
- int optLevel = 1;
+ clName = std::string(clStr);
FILE *clFile = fdopen(clFd, "w");
FATAL_IF(clFile == NULL, "Failed to open temporary file");
} else
fwrite(ocl_stdlib_str.c_str(), strlen(ocl_stdlib_str.c_str()), 1, clFile);
+ //for clCompilerProgram usage.
+ if(temp_header_path){
+ clOpt += " -I ";
+ clOpt += temp_header_path;
+ clOpt += " ";
+ }
+
if (!OCL_STRICT_CONFORMANCE) {
fwrite(ocl_mathfunc_fastpath_str.c_str(), strlen(ocl_mathfunc_fastpath_str.c_str()), 1, clFile);
}
// Write the source to the cl file
fwrite(source, strlen(source), 1, clFile);
fclose(clFile);
+ }
+
+ static gbe_program programNewFromSource(uint32_t deviceID,
+ const char *source,
+ size_t stringSize,
+ const char *options,
+ char *err,
+ size_t *errSize)
+ {
+ int optLevel = 1;
+ std::string clOpt;
+ std::string clName;
+ processSourceAndOption(source, options, NULL, clOpt, clName, optLevel);
gbe_program p;
- if (buildModuleFromSource(clName.c_str(), llName.c_str(), clOpt.c_str(),
+ // will delete the module and act in the destructor of GenProgram.
+ llvm::Module * out_module;
+ llvm::LLVMContext* llvm_ctx = new llvm::LLVMContext;
+ if (buildModuleFromSource(clName.c_str(), &out_module, llvm_ctx, clOpt.c_str(),
stringSize, err, errSize)) {
// Now build the program from llvm
static std::mutex gbe_mutex;
err += *errSize;
clangErrSize = *errSize;
}
- p = gbe_program_new_from_llvm(deviceID, llName.c_str(), stringSize,
+
+ p = gbe_program_new_from_llvm(deviceID, NULL, out_module, llvm_ctx, stringSize,
err, errSize, optLevel);
if (err != NULL)
*errSize += clangErrSize;
gbe_mutex.unlock();
if (OCL_OUTPUT_BUILD_LOG && options)
llvm::errs() << options;
- remove(llName.c_str());
} else
p = NULL;
remove(clName.c_str());
#include "sys/cvar.hpp"
#include "sys/platform.hpp"
+#include <clang/CodeGen/CodeGenAction.h>
+
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
MPM.run(mod);
}
- bool llvmToGen(ir::Unit &unit, const char *fileName, int optLevel)
+ bool llvmToGen(ir::Unit &unit, const char *fileName,const void* module, int optLevel)
{
- // Get the global LLVM context
- llvm::LLVMContext& c = llvm::getGlobalContext();
std::string errInfo;
std::unique_ptr<llvm::raw_fd_ostream> o = NULL;
if (OCL_OUTPUT_LLVM_BEFORE_EXTRA_PASS || OCL_OUTPUT_LLVM)
// Get the module from its file
llvm::SMDiagnostic Err;
std::auto_ptr<Module> M;
- M.reset(ParseIRFile(fileName, Err, c));
- if (M.get() == 0) return false;
- Module &mod = *M.get();
+ if(fileName){
+ // only when module is null, Get the global LLVM context
+ llvm::LLVMContext& c = llvm::getGlobalContext();
+ M.reset(ParseIRFile(fileName, Err, c));
+ if (M.get() == 0) return false;
+ }
+ Module &mod = (module!=NULL)?*(llvm::Module*)module:*M.get();
Triple TargetTriple(mod.getTargetTriple());
TargetLibraryInfo *libraryInfo = new TargetLibraryInfo(TargetTriple);