1/* 2 * Copyright 2016 Bas Nieuwenhuizen 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the 6 * "Software"), to deal in the Software without restriction, including 7 * without limitation the rights to use, copy, modify, merge, publish, 8 * distribute, sub license, and/or sell copies of the Software, and to 9 * permit persons to whom the Software is furnished to do so, subject to 10 * the following conditions: 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 18 * USE OR OTHER DEALINGS IN THE SOFTWARE. 19 * 20 * The above copyright notice and this permission notice (including the 21 * next paragraph) shall be included in all copies or substantial portions 22 * of the Software. 23 * 24 */ 25 26#ifndef AC_LLVM_UTIL_H 27#define AC_LLVM_UTIL_H 28 29#include "amd_family.h" 30#include "util/macros.h" 31#include <llvm-c/TargetMachine.h> 32#include <llvm/Config/llvm-config.h> 33 34#include <stdbool.h> 35 36#ifdef __cplusplus 37extern "C" { 38#endif 39 40struct ac_compiler_passes; 41struct ac_llvm_context; 42 43enum ac_func_attr 44{ 45 AC_FUNC_ATTR_ALWAYSINLINE = (1 << 0), 46 AC_FUNC_ATTR_INREG = (1 << 2), 47 AC_FUNC_ATTR_NOALIAS = (1 << 3), 48 AC_FUNC_ATTR_NOUNWIND = (1 << 4), 49 AC_FUNC_ATTR_READNONE = (1 << 5), 50 AC_FUNC_ATTR_READONLY = (1 << 6), 51 AC_FUNC_ATTR_WRITEONLY = (1 << 7), 52 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY = (1 << 8), 53 AC_FUNC_ATTR_CONVERGENT = (1 << 9), 54 55 /* Legacy intrinsic that needs attributes on function declarations 56 * and they must match the internal LLVM definition exactly, otherwise 57 * intrinsic selection fails. 58 */ 59 AC_FUNC_ATTR_LEGACY = (1u << 31), 60}; 61 62enum ac_target_machine_options 63{ 64 AC_TM_SUPPORTS_SPILL = 1 << 0, 65 AC_TM_CHECK_IR = 1 << 1, 66 AC_TM_CREATE_LOW_OPT = 1 << 2, 67}; 68 69enum ac_float_mode 70{ 71 AC_FLOAT_MODE_DEFAULT, 72 AC_FLOAT_MODE_DEFAULT_OPENGL, 73 AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO, 74}; 75 76/* Per-thread persistent LLVM objects. */ 77struct ac_llvm_compiler { 78 LLVMTargetLibraryInfoRef target_library_info; 79 LLVMPassManagerRef passmgr; 80 81 /* Default compiler. */ 82 LLVMTargetMachineRef tm; 83 struct ac_compiler_passes *passes; 84 85 /* Optional compiler for faster compilation with fewer optimizations. 86 * LLVM modules can be created with "tm" too. There is no difference. 87 */ 88 LLVMTargetMachineRef low_opt_tm; /* uses -O1 instead of -O2 */ 89 struct ac_compiler_passes *low_opt_passes; 90}; 91 92LLVMTargetRef ac_get_llvm_target(const char *triple); 93const char *ac_get_llvm_processor_name(enum radeon_family family); 94bool ac_is_llvm_processor_supported(LLVMTargetMachineRef tm, const char *processor); 95void ac_reset_llvm_all_options_occurences(); 96void ac_add_attr_dereferenceable(LLVMValueRef val, uint64_t bytes); 97void ac_add_attr_alignment(LLVMValueRef val, uint64_t bytes); 98bool ac_is_sgpr_param(LLVMValueRef param); 99void ac_add_function_attr(LLVMContextRef ctx, LLVMValueRef function, int attr_idx, 100 enum ac_func_attr attr); 101void ac_add_func_attributes(LLVMContextRef ctx, LLVMValueRef function, unsigned attrib_mask); 102void ac_dump_module(LLVMModuleRef module); 103LLVMModuleRef ac_create_module(LLVMTargetMachineRef tm, LLVMContextRef ctx); 104LLVMBuilderRef ac_create_builder(LLVMContextRef ctx, enum ac_float_mode float_mode); 105void ac_enable_signed_zeros(struct ac_llvm_context *ctx); 106void ac_disable_signed_zeros(struct ac_llvm_context *ctx); 107 108void ac_llvm_add_target_dep_function_attr(LLVMValueRef F, const char *name, unsigned value); 109void ac_llvm_set_workgroup_size(LLVMValueRef F, unsigned size); 110void ac_llvm_set_target_features(LLVMValueRef F, struct ac_llvm_context *ctx); 111 112static inline unsigned ac_get_load_intr_attribs(bool can_speculate) 113{ 114 /* READNONE means writes can't affect it, while READONLY means that 115 * writes can affect it. */ 116 return can_speculate ? AC_FUNC_ATTR_READNONE : AC_FUNC_ATTR_READONLY; 117} 118 119LLVMTargetLibraryInfoRef ac_create_target_library_info(const char *triple); 120void ac_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info); 121PUBLIC void ac_init_shared_llvm_once(void); /* Do not use directly, use ac_init_llvm_once */ 122void ac_init_llvm_once(void); 123 124bool ac_init_llvm_compiler(struct ac_llvm_compiler *compiler, enum radeon_family family, 125 enum ac_target_machine_options tm_options); 126void ac_destroy_llvm_compiler(struct ac_llvm_compiler *compiler); 127 128struct ac_compiler_passes *ac_create_llvm_passes(LLVMTargetMachineRef tm); 129void ac_destroy_llvm_passes(struct ac_compiler_passes *p); 130bool ac_compile_module_to_elf(struct ac_compiler_passes *p, LLVMModuleRef module, 131 char **pelf_buffer, size_t *pelf_size); 132void ac_llvm_add_barrier_noop_pass(LLVMPassManagerRef passmgr); 133 134static inline bool ac_has_vec3_support(enum amd_gfx_level chip, bool use_format) 135{ 136 /* GFX6 only supports vec3 with load/store format. */ 137 return chip != GFX6 || use_format; 138} 139 140#ifdef __cplusplus 141} 142#endif 143 144#endif /* AC_LLVM_UTIL_H */ 145