summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZack Rusin <zack@kde.org>2010-11-26 18:13:06 -0500
committerZack Rusin <zack@kde.org>2010-11-26 18:13:06 -0500
commit13ce356ce8a75fa9af8a6b7859bcb110db4d3b33 (patch)
treebfa6b3ef2fef0ba06f7caa49ed0e91f776c674a5
parent1e018dfcec6e19e48dea6c185fd1e69da21dcc74 (diff)
Hack to finally start generating llvm ir for opencl kernels.
-rw-r--r--src/compiler/compiler.cpp42
-rw-r--r--src/compiler/compiler.h4
-rw-r--r--src/tools/clcompiler.cpp6
3 files changed, 47 insertions, 5 deletions
diff --git a/src/compiler/compiler.cpp b/src/compiler/compiler.cpp
index 3a52843..c41a6cd 100644
--- a/src/compiler/compiler.cpp
+++ b/src/compiler/compiler.cpp
@@ -21,6 +21,9 @@
#include <llvm/System/Path.h>
#include <llvm/Target/TargetSelect.h>
+#include <iostream>
+#include <fstream>
+
using namespace Coal;
using namespace clang;
@@ -30,6 +33,7 @@ setupCodeGenOpts(CodeGenOptions &opts)
opts.DebugInfo = true;
//opts.OptimizationLevel = 2;
//opts.MainFileName = "main.cl";
+ //opts.UnrollLoops = true;
opts.AsmVerbose = true;
}
@@ -48,12 +52,16 @@ setupDiagnosticOpts(DiagnosticOptions &opts)
}
static void
-setupFrontendOpts(FrontendOptions &opts)
+setupFrontendOpts(FrontendOptions &opts, const std::string &fileLoc)
{
opts.ProgramAction = frontend::EmitLLVMOnly;
opts.DisableFree = true;
+
+ /* XXX HACK
+ * just matches whatever garbage Compiler::prepareInput
+ * creates */
opts.Inputs.push_back(
- std::make_pair(IK_OpenCL, "-"));
+ std::make_pair(IK_OpenCL, fileLoc));
}
static void
@@ -108,6 +116,9 @@ Compiler::~Compiler()
bool Compiler::init()
{
+ std::string tempDir = llvm::sys::Path::GetTemporaryDirectory().str();
+ m_tempFileLocation = tempDir + "/main.cl";
+
m_clang.setLLVMContext(new llvm::LLVMContext);
// Create the compilers actual diagnostics engine.
@@ -119,17 +130,40 @@ bool Compiler::init()
setupPreprocessorOpts(m_clang.getPreprocessorOpts());
setupCodeGenOpts(m_clang.getCodeGenOpts());
setupDiagnosticOpts(m_clang.getDiagnosticOpts());
- setupFrontendOpts(m_clang.getFrontendOpts());
+ setupFrontendOpts(m_clang.getFrontendOpts(), m_tempFileLocation);
setupLangOpts(m_clang.getLangOpts());
setupTargetOpts(m_clang.getTargetOpts());
}
llvm::Module * Compiler::compile(const std::string &text)
{
+ llvm::Module *module;
+ /* setup the input file */
+ prepareInput(text);
+
// Create and execute the frontend to generate an LLVM bitcode module.
llvm::OwningPtr<CodeGenAction> act(new EmitLLVMOnlyAction());
if (!m_clang.ExecuteAction(*act))
return 0;
- return act->takeModule();
+ module = act->takeModule();
+ module->dump();
+ return module;
+}
+
+
+/** XXX
+ * this needs to go. it's a nasty, nasty hack.
+ * It's also a serious security threat!
+ */
+void Compiler::prepareInput(const std::string &text)
+{
+ std::string error;
+
+ std::cerr << "Output path = " << m_tempFileLocation.c_str();
+ std::cerr << "Prepare input = "<<text << std::endl;
+
+ std::fstream stream(m_tempFileLocation.c_str(), std::ios::out | std::ios::trunc);
+ stream << text;
+ stream.close();
}
diff --git a/src/compiler/compiler.h b/src/compiler/compiler.h
index 6e94611..69b2779 100644
--- a/src/compiler/compiler.h
+++ b/src/compiler/compiler.h
@@ -18,9 +18,13 @@ namespace Coal {
private:
bool init();
+ void prepareInput(const std::string &text);
private:
clang::CompilerInstance m_clang;
+
+ // XXX HACK
+ std::string m_tempFileLocation;
};
}
diff --git a/src/tools/clcompiler.cpp b/src/tools/clcompiler.cpp
index 9770944..52fed0e 100644
--- a/src/tools/clcompiler.cpp
+++ b/src/tools/clcompiler.cpp
@@ -1,6 +1,7 @@
#include "compiler.h"
#include <iostream>
+#include <fstream>
static
void usage(const char *progName)
@@ -16,8 +17,11 @@ int main(int argc, char **argv)
usage(argv[0]);
return 1;
}
+ std::ifstream stream(argv[1]);
+ std::string contents((std::istreambuf_iterator<char>(stream)),
+ std::istreambuf_iterator<char>());
- compiler.compile(argv[1]);
+ compiler.compile(contents);
return 0;
}