OSDN Git Service

ac: add reusable helpers for direct LLVM compilation
authorMarek Olšák <marek.olsak@amd.com>
Wed, 4 Jul 2018 05:11:47 +0000 (01:11 -0400)
committerMarek Olšák <marek.olsak@amd.com>
Wed, 4 Jul 2018 19:48:18 +0000 (15:48 -0400)
This is basically LLVMTargetMachineEmitToMemoryBuffer inlined and reworked.

struct ac_compiler_passes (opaque type) contains the main pass manager.

ac_create_llvm_passes -- the result can go to thread local storage
ac_destroy_llvm_passes -- can be called by a destructor in TLS
ac_compile_module_to_binary -- from LLVMModuleRef to ac_shader_binary

The motivation is to do the expensive call addPassesToEmitFile once
per context or thread.

Reviewed-by: Dave Airlie <airlied@redhat.com>
src/amd/common/ac_binary.h
src/amd/common/ac_llvm_helper.cpp
src/amd/common/ac_llvm_util.h

index 4bd86b9..735e393 100644 (file)
 #include <stdint.h>
 #include <stdbool.h>
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 struct ac_shader_reloc {
        char name[32];
        uint64_t offset;
@@ -98,4 +102,8 @@ void ac_shader_binary_read_config(struct ac_shader_binary *binary,
                                  bool supports_spill);
 void ac_shader_binary_clean(struct ac_shader_binary *b);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* AC_BINARY_H */
index 48a5a44..4348ebd 100644 (file)
 #pragma push_macro("DEBUG")
 #undef DEBUG
 
+#include "ac_binary.h"
 #include "ac_llvm_util.h"
+
 #include <llvm-c/Core.h>
-#include <llvm/Target/TargetOptions.h>
-#include <llvm/ExecutionEngine/ExecutionEngine.h>
-#include <llvm/IR/Attributes.h>
-#include <llvm/IR/CallSite.h>
+#include <llvm/Target/TargetMachine.h>
 #include <llvm/IR/IRBuilder.h>
 #include <llvm/Analysis/TargetLibraryInfo.h>
 
+#include <llvm/IR/LegacyPassManager.h>
+#if HAVE_LLVM < 0x0700
+#include "llvm/Support/raw_ostream.h"
+#endif
+
 void ac_add_attr_dereferenceable(LLVMValueRef val, uint64_t bytes)
 {
    llvm::Argument *A = llvm::unwrap<llvm::Argument>(val);
@@ -110,3 +114,54 @@ ac_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info)
 {
        delete reinterpret_cast<llvm::TargetLibraryInfoImpl *>(library_info);
 }
+
+/* The LLVM compiler is represented as a pass manager containing passes for
+ * optimizations, instruction selection, and code generation.
+ */
+struct ac_compiler_passes {
+       ac_compiler_passes(): ostream(code_string) {}
+
+       llvm::SmallString<0> code_string;  /* ELF shader binary */
+       llvm::raw_svector_ostream ostream; /* stream for appending data to the binary */
+       llvm::legacy::PassManager passmgr; /* list of passes */
+};
+
+struct ac_compiler_passes *ac_create_llvm_passes(LLVMTargetMachineRef tm)
+{
+       struct ac_compiler_passes *p = new ac_compiler_passes();
+       if (!p)
+               return NULL;
+
+       llvm::TargetMachine *TM = reinterpret_cast<llvm::TargetMachine*>(tm);
+
+       if (TM->addPassesToEmitFile(p->passmgr, p->ostream,
+#if HAVE_LLVM >= 0x0700
+                                   nullptr,
+#endif
+                                   llvm::TargetMachine::CGFT_ObjectFile)) {
+               fprintf(stderr, "amd: TargetMachine can't emit a file of this type!\n");
+               delete p;
+               return NULL;
+       }
+       return p;
+}
+
+void ac_destroy_llvm_passes(struct ac_compiler_passes *p)
+{
+       delete p;
+}
+
+/* This returns false on failure. */
+bool ac_compile_module_to_binary(struct ac_compiler_passes *p, LLVMModuleRef module,
+                                struct ac_shader_binary *binary)
+{
+       p->passmgr.run(*llvm::unwrap(module));
+
+       llvm::StringRef data = p->ostream.str();
+       bool success = ac_elf_read(data.data(), data.size(), binary);
+       p->code_string = ""; /* release the ELF shader binary */
+
+       if (!success)
+               fprintf(stderr, "amd: cannot read an ELF shader binary\n");
+       return success;
+}
index c6ee537..373fd8d 100644 (file)
@@ -35,6 +35,9 @@
 extern "C" {
 #endif
 
+struct ac_shader_binary;
+struct ac_compiler_passes;
+
 enum ac_func_attr {
        AC_FUNC_ATTR_ALWAYSINLINE = (1 << 0),
        AC_FUNC_ATTR_INREG        = (1 << 2),
@@ -73,6 +76,7 @@ struct ac_llvm_compiler {
        LLVMTargetMachineRef            tm;
        LLVMTargetLibraryInfoRef        target_library_info;
        LLVMPassManagerRef              passmgr;
+       struct ac_compiler_passes       *passes;
 };
 
 const char *ac_get_llvm_processor_name(enum radeon_family family);
@@ -125,6 +129,11 @@ bool ac_init_llvm_compiler(struct ac_llvm_compiler *compiler,
                           enum ac_target_machine_options tm_options);
 void ac_destroy_llvm_compiler(struct ac_llvm_compiler *compiler);
 
+struct ac_compiler_passes *ac_create_llvm_passes(LLVMTargetMachineRef tm);
+void ac_destroy_llvm_passes(struct ac_compiler_passes *p);
+bool ac_compile_module_to_binary(struct ac_compiler_passes *p, LLVMModuleRef module,
+                                struct ac_shader_binary *binary);
+
 #ifdef __cplusplus
 }
 #endif