[Mesa-dev] [PATCH 08/10] amd/common: add ac_compile_module_to_elf
Nicolai Hähnle
nhaehnle at gmail.com
Fri May 3 11:18:27 UTC 2019
From: Nicolai Hähnle <nicolai.haehnle at amd.com>
A new variant of ac_compile_module_to_binary that allows us to
keep the entire ELF around.
---
src/amd/common/ac_llvm_helper.cpp | 88 ++++++++++++++++++++++++++++---
src/amd/common/ac_llvm_util.h | 2 +
2 files changed, 83 insertions(+), 7 deletions(-)
diff --git a/src/amd/common/ac_llvm_helper.cpp b/src/amd/common/ac_llvm_helper.cpp
index dcfb8008546..834c5d7f94e 100644
--- a/src/amd/common/ac_llvm_helper.cpp
+++ b/src/amd/common/ac_llvm_helper.cpp
@@ -22,23 +22,27 @@
* of the Software.
*
*/
/* based on Marek's patch to lp_bld_misc.cpp */
// Workaround http://llvm.org/PR23628
#pragma push_macro("DEBUG")
#undef DEBUG
+#include <cstring>
+
#include "ac_binary.h"
#include "ac_llvm_util.h"
+#include "util/macros.h"
+
#include <llvm-c/Core.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/Analysis/TargetLibraryInfo.h>
#include <llvm/Transforms/IPO.h>
#include <llvm/IR/LegacyPassManager.h>
void ac_add_attr_dereferenceable(LLVMValueRef val, uint64_t bytes)
{
@@ -102,28 +106,90 @@ ac_create_target_library_info(const char *triple)
{
return reinterpret_cast<LLVMTargetLibraryInfoRef>(new llvm::TargetLibraryInfoImpl(llvm::Triple(triple)));
}
void
ac_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info)
{
delete reinterpret_cast<llvm::TargetLibraryInfoImpl *>(library_info);
}
+/* Implementation of raw_pwrite_stream that works on malloc()ed memory for
+ * better compatibility with C code. */
+struct raw_memory_ostream : public llvm::raw_pwrite_stream {
+ char *buffer;
+ size_t written;
+ size_t bufsize;
+
+ raw_memory_ostream()
+ {
+ buffer = NULL;
+ written = 0;
+ bufsize = 0;
+ SetUnbuffered();
+ }
+
+ ~raw_memory_ostream()
+ {
+ free(buffer);
+ }
+
+ void clear()
+ {
+ written = 0;
+ }
+
+ void take(char *&out_buffer, size_t &out_size)
+ {
+ out_buffer = buffer;
+ out_size = written;
+ buffer = NULL;
+ written = 0;
+ bufsize = 0;
+ }
+
+ void flush() = delete;
+
+ void write_impl(const char *ptr, size_t size) override
+ {
+ if (unlikely(written + size < written))
+ abort();
+ if (written + size > bufsize) {
+ bufsize = MAX3(1024, written + size, bufsize / 3 * 4);
+ buffer = (char *)realloc(buffer, bufsize);
+ if (!buffer) {
+ fprintf(stderr, "amd: out of memory allocating ELF buffer\n");
+ abort();
+ }
+ }
+ memcpy(buffer + written, ptr, size);
+ written += size;
+ }
+
+ void pwrite_impl(const char *ptr, size_t size, uint64_t offset) override
+ {
+ assert(offset == (size_t)offset &&
+ offset + size >= offset && offset + size <= written);
+ memcpy(buffer + offset, ptr, size);
+ }
+
+ uint64_t current_pos() const override
+ {
+ return written;
+ }
+};
+
/* 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 */
+ raw_memory_ostream ostream; /* ELF shader binary stream */
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);
@@ -142,28 +208,36 @@ 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 */
+ bool success = ac_elf_read(p->ostream.buffer, p->ostream.written, binary);
+ p->ostream.clear();
if (!success)
fprintf(stderr, "amd: cannot read an ELF shader binary\n");
return success;
}
+/* This returns false on failure. */
+bool ac_compile_module_to_elf(struct ac_compiler_passes *p, LLVMModuleRef module,
+ char **pelf_buffer, size_t *pelf_size)
+{
+ p->passmgr.run(*llvm::unwrap(module));
+ p->ostream.take(*pelf_buffer, *pelf_size);
+ return true;
+}
+
void ac_llvm_add_barrier_noop_pass(LLVMPassManagerRef passmgr)
{
llvm::unwrap(passmgr)->add(llvm::createBarrierNoopPass());
}
void ac_enable_global_isel(LLVMTargetMachineRef tm)
{
reinterpret_cast<llvm::TargetMachine*>(tm)->setGlobalISel(true);
}
diff --git a/src/amd/common/ac_llvm_util.h b/src/amd/common/ac_llvm_util.h
index 6d961c06f8a..7447d349bd5 100644
--- a/src/amd/common/ac_llvm_util.h
+++ b/src/amd/common/ac_llvm_util.h
@@ -135,18 +135,20 @@ void ac_init_llvm_once(void);
bool ac_init_llvm_compiler(struct ac_llvm_compiler *compiler,
enum radeon_family family,
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);
+bool ac_compile_module_to_elf(struct ac_compiler_passes *p, LLVMModuleRef module,
+ char **pelf_buffer, size_t *pelf_size);
void ac_llvm_add_barrier_noop_pass(LLVMPassManagerRef passmgr);
void ac_enable_global_isel(LLVMTargetMachineRef tm);
#ifdef __cplusplus
}
#endif
#endif /* AC_LLVM_UTIL_H */
--
2.20.1
More information about the mesa-dev
mailing list