}
void
-_cl_program::build(const std::vector<clover::device *> &devs) {
+_cl_program::build(const std::vector<clover::device *> &devs,
+ const char *opts) {
for (auto dev : devs) {
__binaries.erase(dev);
__logs.erase(dev);
+ __opts.erase(dev);
+
+ __opts.insert({ dev, opts });
try {
auto module = (dev->ir_format() == PIPE_SHADER_IR_TGSI ?
compile_program_tgsi(__source) :
compile_program_llvm(__source, dev->ir_format(),
- dev->ir_target()));
+ dev->ir_target(), build_opts(dev)));
__binaries.insert({ dev, module });
} catch (build_error &e) {
__logs.insert({ dev, e.what() });
throw error(CL_BUILD_PROGRAM_FAILURE);
+ } catch (invalid_option_error &e) {
+ throw error(CL_INVALID_BUILD_OPTIONS);
}
}
}
std::string
_cl_program::build_opts(clover::device *dev) const {
- return {};
+ return __opts.count(dev) ? __opts.find(dev)->second : "";
}
std::string
const std::vector<clover::device *> &devs,
const std::vector<clover::module> &binaries);
- void build(const std::vector<clover::device *> &devs);
+ void build(const std::vector<clover::device *> &devs, const char *opts);
const std::string &source() const;
const std::map<clover::device *, clover::module> &binaries() const;
private:
std::map<clover::device *, clover::module> __binaries;
std::map<clover::device *, std::string> __logs;
+ std::map<clover::device *, std::string> __opts;
std::string __source;
};
#include "core/compiler.hpp"
#include <clang/Frontend/CompilerInstance.h>
+#include <clang/Frontend/TextDiagnosticBuffer.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/CodeGen/CodeGenAction.h>
#include <llvm/Bitcode/BitstreamWriter.h>
#include <iomanip>
#include <fstream>
#include <cstdio>
+#include <sstream>
using namespace clover;
llvm::Module *
compile(const std::string &source, const std::string &name,
- const std::string &triple) {
+ const std::string &triple, const std::string &opts) {
clang::CompilerInstance c;
+ clang::CompilerInvocation invocation;
clang::EmitLLVMOnlyAction act(&llvm::getGlobalContext());
std::string log;
llvm::raw_string_ostream s_log(log);
- c.getFrontendOpts().Inputs.push_back(
- clang::FrontendInputFile(name, clang::IK_OpenCL));
+ // Parse the compiler options:
+ std::vector<std::string> opts_array;
+ std::istringstream ss(opts);
+
+ while (!ss.eof()) {
+ std::string opt;
+ getline(ss, opt, ' ');
+ opts_array.push_back(opt);
+ }
+
+ opts_array.push_back(name);
+
+ std::vector<const char *> opts_carray;
+ for (unsigned i = 0; i < opts_array.size(); i++) {
+ opts_carray.push_back(opts_array.at(i).c_str());
+ }
+
+ llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID;
+ llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts;
+ clang::TextDiagnosticBuffer *DiagsBuffer;
+
+ DiagID = new clang::DiagnosticIDs();
+ DiagOpts = new clang::DiagnosticOptions();
+ DiagsBuffer = new clang::TextDiagnosticBuffer();
+
+ clang::DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
+ bool Success;
+
+ Success = clang::CompilerInvocation::CreateFromArgs(c.getInvocation(),
+ opts_carray.data(),
+ opts_carray.data() + opts_carray.size(),
+ Diags);
+ if (!Success) {
+ throw invalid_option_error();
+ }
c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly;
c.getHeaderSearchOpts().UseBuiltinIncludes = true;
c.getHeaderSearchOpts().UseStandardSystemIncludes = true;
module
clover::compile_program_llvm(const compat::string &source,
enum pipe_shader_ir ir,
- const compat::string &triple) {
+ const compat::string &triple,
+ const compat::string &opts) {
std::vector<llvm::Function *> kernels;
- llvm::Module *mod = compile(source, "cl_input", triple);
+ // The input file name must have the .cl extension in order for the
+ // CompilerInvocation class to recognize it as an OpenCL source file.
+ llvm::Module *mod = compile(source, "input.cl", triple, opts);
find_kernels(mod, kernels);