[Mesa-dev] [PATCH 1/4] ac: add reusable helpers for direct LLVM compilation

Dave Airlie airlied at gmail.com
Wed Jul 4 06:26:55 UTC 2018


On 4 July 2018 at 16:02, Marek Olšák <maraeo at gmail.com> wrote:
> From: Marek Olšák <marek.olsak at amd.com>
>
> 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.

Nice, I'll go rebase myself on this when you land it, this looks
pretty good to me.

For the series:
Reviewed-by: Dave Airlie <airlied at redhat.com>


> ---
>  src/amd/common/ac_binary.h        |  8 ++++
>  src/amd/common/ac_llvm_helper.cpp | 63 +++++++++++++++++++++++++++++--
>  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
> @@ -20,20 +20,24 @@
>   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
>   * SOFTWARE.
>   */
>
>  #ifndef AC_BINARY_H
>  #define AC_BINARY_H
>
>  #include <stdint.h>
>  #include <stdbool.h>
>
> +#ifdef __cplusplus
> +extern "C" {
> +#endif
> +
>  struct ac_shader_reloc {
>         char name[32];
>         uint64_t offset;
>  };
>
>  struct ac_shader_binary {
>         unsigned code_size;
>         unsigned config_size;
>         /** The number of bytes of config information for each global symbol.
>          */
> @@ -91,11 +95,15 @@ bool ac_elf_read(const char *elf_data, unsigned elf_size,
>  const unsigned char *ac_shader_binary_config_start(
>         const struct ac_shader_binary *binary,
>         uint64_t symbol_offset);
>
>  void ac_shader_binary_read_config(struct ac_shader_binary *binary,
>                                   struct ac_shader_config *conf,
>                                   unsigned symbol_offset,
>                                   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 d4eaaffa124..a1358755a58 100644
> --- a/src/amd/common/ac_llvm_helper.cpp
> +++ b/src/amd/common/ac_llvm_helper.cpp
> @@ -22,29 +22,33 @@
>   * 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 "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);
>     A->addAttr(llvm::Attribute::getWithDereferenceableBytes(A->getContext(), bytes));
>  }
>
>  bool ac_is_sgpr_param(LLVMValueRef arg)
>  {
>         llvm::Argument *A = llvm::unwrap<llvm::Argument>(arg);
>         llvm::AttributeList AS = A->getParent()->getAttributes();
> @@ -99,10 +103,61 @@ LLVMTargetLibraryInfoRef
>  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);
>  }
> +
> +/* 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
> @@ -28,20 +28,23 @@
>
>  #include <stdbool.h>
>  #include <llvm-c/TargetMachine.h>
>
>  #include "amd_family.h"
>
>  #ifdef __cplusplus
>  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),
>         AC_FUNC_ATTR_NOALIAS      = (1 << 3),
>         AC_FUNC_ATTR_NOUNWIND     = (1 << 4),
>         AC_FUNC_ATTR_READNONE     = (1 << 5),
>         AC_FUNC_ATTR_READONLY     = (1 << 6),
>         AC_FUNC_ATTR_WRITEONLY    = (1 << 7),
>         AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY = (1 << 8),
>         AC_FUNC_ATTR_CONVERGENT = (1 << 9),
> @@ -66,20 +69,21 @@ enum ac_float_mode {
>         AC_FLOAT_MODE_DEFAULT,
>         AC_FLOAT_MODE_NO_SIGNED_ZEROS_FP_MATH,
>         AC_FLOAT_MODE_UNSAFE_FP_MATH,
>  };
>
>  /* Per-thread persistent LLVM objects. */
>  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);
>  void ac_add_attr_dereferenceable(LLVMValueRef val, uint64_t bytes);
>  bool ac_is_sgpr_param(LLVMValueRef param);
>  void ac_add_function_attr(LLVMContextRef ctx, LLVMValueRef function,
>                            int attr_idx, enum ac_func_attr attr);
>  void ac_add_func_attributes(LLVMContextRef ctx, LLVMValueRef function,
>                             unsigned attrib_mask);
>  void ac_dump_module(LLVMModuleRef module);
> @@ -118,15 +122,20 @@ LLVMTargetLibraryInfoRef ac_create_target_library_info(const char *triple);
>  void ac_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info);
>  void ac_init_llvm_once(void);
>
>
>  bool ac_init_llvm_compiler(struct ac_llvm_compiler *compiler,
>                            bool okay_to_leak_target_library_info,
>                            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);
> +
>  #ifdef __cplusplus
>  }
>  #endif
>
>  #endif /* AC_LLVM_UTIL_H */
> --
> 2.17.1
>
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev


More information about the mesa-dev mailing list