Re: [Mesa-dev] [PATCH] clover: Add support for compiling to native object code v2

2014-10-24 Thread Tom Stellard
On Wed, Oct 08, 2014 at 12:06:20PM +0300, Francisco Jerez wrote:
 Tom Stellard thomas.stell...@amd.com writes:
 
  v2:
- Split build_module_native() into three separate functions.
- Code cleanups.
  ---
   .../state_trackers/clover/llvm/invocation.cpp  | 200 
  -
   src/gallium/targets/opencl/Makefile.am |   1 +
   2 files changed, 192 insertions(+), 9 deletions(-)
 
  diff --git a/src/gallium/state_trackers/clover/llvm/invocation.cpp 
  b/src/gallium/state_trackers/clover/llvm/invocation.cpp
  index 3b6b6a4..d9c3d11 100644
  --- a/src/gallium/state_trackers/clover/llvm/invocation.cpp
  +++ b/src/gallium/state_trackers/clover/llvm/invocation.cpp
  @@ -45,12 +45,18 @@
   #include llvm/Support/SourceMgr.h
   #include llvm/IRReader/IRReader.h
   #endif
  +#if HAVE_LLVM  0x0305
  +#include llvm/ADT/OwningPtr.h
  +#endif
   #include llvm/PassManager.h
  +#include llvm/Support/CodeGen.h
   #include llvm/Support/TargetSelect.h
   #include llvm/Support/MemoryBuffer.h
   #if HAVE_LLVM  0x0303
   #include llvm/Support/PathV1.h
   #endif
  +#include llvm/Support/FormattedStream.h
  +#include llvm/Support/TargetRegistry.h
   #include llvm/Transforms/IPO.h
   #include llvm/Transforms/IPO/PassManagerBuilder.h
   
  @@ -61,6 +67,13 @@
   #else
   #include llvm/IR/DataLayout.h
   #endif
  +#include llvm/Target/TargetLibraryInfo.h
  +#include llvm/Target/TargetMachine.h
  +#include llvm/Target/TargetOptions.h
  +
  +#include llvm-c/Target.h
  +#include llvm-c/TargetMachine.h
  +#include llvm-c/Core.h
   
   #include pipe/p_state.h
   #include util/u_memory.h
  @@ -71,6 +84,8 @@
   #include fstream
   #include cstdio
   #include sstream
  +#include libelf.h
  +#include gelf.h
   
   using namespace clover;
   
  @@ -117,10 +132,11 @@ namespace {
   #endif
   
  llvm::Module *
  -   compile(llvm::LLVMContext llvm_ctx, const std::string source,
  +   compile_llvm(llvm::LLVMContext llvm_ctx, const std::string source,
  const std::string name, const std::string triple,
  const std::string processor, const std::string opts,
  -   clang::LangAS::Map address_spaces, compat::string r_log) {
  +   clang::LangAS::Map address_spaces, unsigned 
  optimization_level,
  +  compat::string r_log) {
   
 clang::CompilerInstance c;
 clang::EmitLLVMOnlyAction act(llvm_ctx);
  @@ -228,6 +244,8 @@ namespace {
 // that is no executed by all threads) during its optimizaton passes.
 c.getCodeGenOpts().LinkBitcodeFile = libclc_path;
   
  +  optimization_level = c.getCodeGenOpts().OptimizationLevel;
  +
 // Compile the code
 bool ExecSuccess = c.ExecuteAction(act);
 r_log = log;
  @@ -264,8 +282,8 @@ namespace {
  }
   
  void
  -   internalize_functions(llvm::Module *mod,
  -const std::vectorllvm::Function * kernels) {
  +   optimize(llvm::Module *mod, unsigned optimization_level,
  +const std::vectorllvm::Function * kernels) {
   
 llvm::PassManager PM;
 // Add a function internalizer pass.
  @@ -289,7 +307,14 @@ namespace {
llvm::Function *kernel = *I;
export_list.push_back(kernel-getName().data());
 }
  +  PM.add(new llvm::DataLayoutPass());
 PM.add(llvm::createInternalizePass(export_list));
  +
  +  llvm::PassManagerBuilder PMB;
  +  PMB.OptLevel = optimization_level;
  +  PMB.LibraryInfo = new llvm::TargetLibraryInfo(
  +llvm::Triple(mod-getTargetTriple()));
  +  PMB.populateModulePassManager(PM);
 PM.run(*mod);
  }
   
  @@ -404,6 +429,150 @@ namespace {
   
 return m;
  }
  +
  +   std::vectorchar
  +   compile_native(const llvm::Module *mod, std::string triple,
  +  std::string processor, compat::string r_log) {
  +
 
 triple and processor should be const references.
 
  +  std::string log;
  +  LLVMTargetRef target;
  +  char *error_message;
  +  LLVMMemoryBufferRef out_buffer;
  +  unsigned buffer_size;
  +  const char *buffer_data;
  +  LLVMBool err;
  +  LLVMModuleRef mod_ref = wrap(mod);
  +
  +  if (LLVMGetTargetFromTriple(triple.c_str(), target, 
  error_message)) {
  + r_log = std::string(error_message);
  + LLVMDisposeMessage(error_message);
  + throw build_error();
  +  }
  +
  +  LLVMTargetMachineRef tm = LLVMCreateTargetMachine(
  +target, triple.c_str(), processor.c_str(), ,
  +LLVMCodeGenLevelDefault, LLVMRelocDefault, 
  LLVMCodeModelDefault);
  +
  +  if (!tm) {
  + r_log = Could not create TargetMachine:  + triple;
  + throw build_error();
  +  }
  +
  +  err = LLVMTargetMachineEmitToMemoryBuffer(tm, mod_ref, 
  LLVMObjectFile,
  +error_message, 
  out_buffer);
  +
  +  if (err) {
  + LLVMDisposeTargetMachine(tm);
  + r_log = 

Re: [Mesa-dev] [PATCH] clover: Add support for compiling to native object code v2

2014-10-08 Thread Francisco Jerez
Tom Stellard thomas.stell...@amd.com writes:

 v2:
   - Split build_module_native() into three separate functions.
   - Code cleanups.
 ---
  .../state_trackers/clover/llvm/invocation.cpp  | 200 
 -
  src/gallium/targets/opencl/Makefile.am |   1 +
  2 files changed, 192 insertions(+), 9 deletions(-)

 diff --git a/src/gallium/state_trackers/clover/llvm/invocation.cpp 
 b/src/gallium/state_trackers/clover/llvm/invocation.cpp
 index 3b6b6a4..d9c3d11 100644
 --- a/src/gallium/state_trackers/clover/llvm/invocation.cpp
 +++ b/src/gallium/state_trackers/clover/llvm/invocation.cpp
 @@ -45,12 +45,18 @@
  #include llvm/Support/SourceMgr.h
  #include llvm/IRReader/IRReader.h
  #endif
 +#if HAVE_LLVM  0x0305
 +#include llvm/ADT/OwningPtr.h
 +#endif
  #include llvm/PassManager.h
 +#include llvm/Support/CodeGen.h
  #include llvm/Support/TargetSelect.h
  #include llvm/Support/MemoryBuffer.h
  #if HAVE_LLVM  0x0303
  #include llvm/Support/PathV1.h
  #endif
 +#include llvm/Support/FormattedStream.h
 +#include llvm/Support/TargetRegistry.h
  #include llvm/Transforms/IPO.h
  #include llvm/Transforms/IPO/PassManagerBuilder.h
  
 @@ -61,6 +67,13 @@
  #else
  #include llvm/IR/DataLayout.h
  #endif
 +#include llvm/Target/TargetLibraryInfo.h
 +#include llvm/Target/TargetMachine.h
 +#include llvm/Target/TargetOptions.h
 +
 +#include llvm-c/Target.h
 +#include llvm-c/TargetMachine.h
 +#include llvm-c/Core.h
  
  #include pipe/p_state.h
  #include util/u_memory.h
 @@ -71,6 +84,8 @@
  #include fstream
  #include cstdio
  #include sstream
 +#include libelf.h
 +#include gelf.h
  
  using namespace clover;
  
 @@ -117,10 +132,11 @@ namespace {
  #endif
  
 llvm::Module *
 -   compile(llvm::LLVMContext llvm_ctx, const std::string source,
 +   compile_llvm(llvm::LLVMContext llvm_ctx, const std::string source,
 const std::string name, const std::string triple,
 const std::string processor, const std::string opts,
 -   clang::LangAS::Map address_spaces, compat::string r_log) {
 +   clang::LangAS::Map address_spaces, unsigned optimization_level,
 +compat::string r_log) {
  
clang::CompilerInstance c;
clang::EmitLLVMOnlyAction act(llvm_ctx);
 @@ -228,6 +244,8 @@ namespace {
// that is no executed by all threads) during its optimizaton passes.
c.getCodeGenOpts().LinkBitcodeFile = libclc_path;
  
 +  optimization_level = c.getCodeGenOpts().OptimizationLevel;
 +
// Compile the code
bool ExecSuccess = c.ExecuteAction(act);
r_log = log;
 @@ -264,8 +282,8 @@ namespace {
 }
  
 void
 -   internalize_functions(llvm::Module *mod,
 -const std::vectorllvm::Function * kernels) {
 +   optimize(llvm::Module *mod, unsigned optimization_level,
 +const std::vectorllvm::Function * kernels) {
  
llvm::PassManager PM;
// Add a function internalizer pass.
 @@ -289,7 +307,14 @@ namespace {
   llvm::Function *kernel = *I;
   export_list.push_back(kernel-getName().data());
}
 +  PM.add(new llvm::DataLayoutPass());
PM.add(llvm::createInternalizePass(export_list));
 +
 +  llvm::PassManagerBuilder PMB;
 +  PMB.OptLevel = optimization_level;
 +  PMB.LibraryInfo = new llvm::TargetLibraryInfo(
 +llvm::Triple(mod-getTargetTriple()));
 +  PMB.populateModulePassManager(PM);
PM.run(*mod);
 }
  
 @@ -404,6 +429,150 @@ namespace {
  
return m;
 }
 +
 +   std::vectorchar
 +   compile_native(const llvm::Module *mod, std::string triple,
 +  std::string processor, compat::string r_log) {
 +

triple and processor should be const references.

 +  std::string log;
 +  LLVMTargetRef target;
 +  char *error_message;
 +  LLVMMemoryBufferRef out_buffer;
 +  unsigned buffer_size;
 +  const char *buffer_data;
 +  LLVMBool err;
 +  LLVMModuleRef mod_ref = wrap(mod);
 +
 +  if (LLVMGetTargetFromTriple(triple.c_str(), target, error_message)) {
 + r_log = std::string(error_message);
 + LLVMDisposeMessage(error_message);
 + throw build_error();
 +  }
 +
 +  LLVMTargetMachineRef tm = LLVMCreateTargetMachine(
 +target, triple.c_str(), processor.c_str(), ,
 +LLVMCodeGenLevelDefault, LLVMRelocDefault, LLVMCodeModelDefault);
 +
 +  if (!tm) {
 + r_log = Could not create TargetMachine:  + triple;
 + throw build_error();
 +  }
 +
 +  err = LLVMTargetMachineEmitToMemoryBuffer(tm, mod_ref, LLVMObjectFile,
 +error_message, out_buffer);
 +
 +  if (err) {
 + LLVMDisposeTargetMachine(tm);
 + r_log = std::string(error_message);
 + LLVMDisposeMessage(error_message);
 + throw build_error();
 +  }
 +
 +  buffer_size = LLVMGetBufferSize(out_buffer);
 +  buffer_data = LLVMGetBufferStart(out_buffer);
 +
 +  

[Mesa-dev] [PATCH] clover: Add support for compiling to native object code v2

2014-10-07 Thread Tom Stellard
v2:
  - Split build_module_native() into three separate functions.
  - Code cleanups.
---
 .../state_trackers/clover/llvm/invocation.cpp  | 200 -
 src/gallium/targets/opencl/Makefile.am |   1 +
 2 files changed, 192 insertions(+), 9 deletions(-)

diff --git a/src/gallium/state_trackers/clover/llvm/invocation.cpp 
b/src/gallium/state_trackers/clover/llvm/invocation.cpp
index 3b6b6a4..d9c3d11 100644
--- a/src/gallium/state_trackers/clover/llvm/invocation.cpp
+++ b/src/gallium/state_trackers/clover/llvm/invocation.cpp
@@ -45,12 +45,18 @@
 #include llvm/Support/SourceMgr.h
 #include llvm/IRReader/IRReader.h
 #endif
+#if HAVE_LLVM  0x0305
+#include llvm/ADT/OwningPtr.h
+#endif
 #include llvm/PassManager.h
+#include llvm/Support/CodeGen.h
 #include llvm/Support/TargetSelect.h
 #include llvm/Support/MemoryBuffer.h
 #if HAVE_LLVM  0x0303
 #include llvm/Support/PathV1.h
 #endif
+#include llvm/Support/FormattedStream.h
+#include llvm/Support/TargetRegistry.h
 #include llvm/Transforms/IPO.h
 #include llvm/Transforms/IPO/PassManagerBuilder.h
 
@@ -61,6 +67,13 @@
 #else
 #include llvm/IR/DataLayout.h
 #endif
+#include llvm/Target/TargetLibraryInfo.h
+#include llvm/Target/TargetMachine.h
+#include llvm/Target/TargetOptions.h
+
+#include llvm-c/Target.h
+#include llvm-c/TargetMachine.h
+#include llvm-c/Core.h
 
 #include pipe/p_state.h
 #include util/u_memory.h
@@ -71,6 +84,8 @@
 #include fstream
 #include cstdio
 #include sstream
+#include libelf.h
+#include gelf.h
 
 using namespace clover;
 
@@ -117,10 +132,11 @@ namespace {
 #endif
 
llvm::Module *
-   compile(llvm::LLVMContext llvm_ctx, const std::string source,
+   compile_llvm(llvm::LLVMContext llvm_ctx, const std::string source,
const std::string name, const std::string triple,
const std::string processor, const std::string opts,
-   clang::LangAS::Map address_spaces, compat::string r_log) {
+   clang::LangAS::Map address_spaces, unsigned optimization_level,
+  compat::string r_log) {
 
   clang::CompilerInstance c;
   clang::EmitLLVMOnlyAction act(llvm_ctx);
@@ -228,6 +244,8 @@ namespace {
   // that is no executed by all threads) during its optimizaton passes.
   c.getCodeGenOpts().LinkBitcodeFile = libclc_path;
 
+  optimization_level = c.getCodeGenOpts().OptimizationLevel;
+
   // Compile the code
   bool ExecSuccess = c.ExecuteAction(act);
   r_log = log;
@@ -264,8 +282,8 @@ namespace {
}
 
void
-   internalize_functions(llvm::Module *mod,
-const std::vectorllvm::Function * kernels) {
+   optimize(llvm::Module *mod, unsigned optimization_level,
+const std::vectorllvm::Function * kernels) {
 
   llvm::PassManager PM;
   // Add a function internalizer pass.
@@ -289,7 +307,14 @@ namespace {
  llvm::Function *kernel = *I;
  export_list.push_back(kernel-getName().data());
   }
+  PM.add(new llvm::DataLayoutPass());
   PM.add(llvm::createInternalizePass(export_list));
+
+  llvm::PassManagerBuilder PMB;
+  PMB.OptLevel = optimization_level;
+  PMB.LibraryInfo = new llvm::TargetLibraryInfo(
+llvm::Triple(mod-getTargetTriple()));
+  PMB.populateModulePassManager(PM);
   PM.run(*mod);
}
 
@@ -404,6 +429,150 @@ namespace {
 
   return m;
}
+
+   std::vectorchar
+   compile_native(const llvm::Module *mod, std::string triple,
+  std::string processor, compat::string r_log) {
+
+  std::string log;
+  LLVMTargetRef target;
+  char *error_message;
+  LLVMMemoryBufferRef out_buffer;
+  unsigned buffer_size;
+  const char *buffer_data;
+  LLVMBool err;
+  LLVMModuleRef mod_ref = wrap(mod);
+
+  if (LLVMGetTargetFromTriple(triple.c_str(), target, error_message)) {
+ r_log = std::string(error_message);
+ LLVMDisposeMessage(error_message);
+ throw build_error();
+  }
+
+  LLVMTargetMachineRef tm = LLVMCreateTargetMachine(
+target, triple.c_str(), processor.c_str(), ,
+LLVMCodeGenLevelDefault, LLVMRelocDefault, LLVMCodeModelDefault);
+
+  if (!tm) {
+ r_log = Could not create TargetMachine:  + triple;
+ throw build_error();
+  }
+
+  err = LLVMTargetMachineEmitToMemoryBuffer(tm, mod_ref, LLVMObjectFile,
+error_message, out_buffer);
+
+  if (err) {
+ LLVMDisposeTargetMachine(tm);
+ r_log = std::string(error_message);
+ LLVMDisposeMessage(error_message);
+ throw build_error();
+  }
+
+  buffer_size = LLVMGetBufferSize(out_buffer);
+  buffer_data = LLVMGetBufferStart(out_buffer);
+
+  std::vectorchar code(buffer_data, buffer_data + buffer_size);
+
+  LLVMDisposeMemoryBuffer(out_buffer);
+  LLVMDisposeTargetMachine(tm);
+
+  return code;
+   }
+
+   std::mapstd::string, unsigned
+