diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/amd/common/ac_binary.h | 8 | ||||
-rw-r--r-- | src/amd/common/ac_llvm_helper.cpp | 63 | ||||
-rw-r--r-- | src/amd/common/ac_llvm_util.h | 9 |
3 files changed, 76 insertions, 4 deletions
diff --git a/src/amd/common/ac_binary.h b/src/amd/common/ac_binary.h index 4bd86b939f1..735e3932055 100644 --- a/src/amd/common/ac_binary.h +++ b/src/amd/common/ac_binary.h @@ -27,6 +27,10 @@ #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 */ diff --git a/src/amd/common/ac_llvm_helper.cpp b/src/amd/common/ac_llvm_helper.cpp index 48a5a44e392..4348ebd36ee 100644 --- a/src/amd/common/ac_llvm_helper.cpp +++ b/src/amd/common/ac_llvm_helper.cpp @@ -29,15 +29,19 @@ #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; +} diff --git a/src/amd/common/ac_llvm_util.h b/src/amd/common/ac_llvm_util.h index c6ee537b74c..373fd8d28db 100644 --- a/src/amd/common/ac_llvm_util.h +++ b/src/amd/common/ac_llvm_util.h @@ -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 |