xref: /aosp_15_r20/external/mesa3d/src/amd/llvm/ac_llvm_util.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2016 Bas Nieuwenhuizen
3  *
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #ifndef AC_LLVM_UTIL_H
8 #define AC_LLVM_UTIL_H
9 
10 #include "amd_family.h"
11 #include "util/macros.h"
12 #include <llvm-c/TargetMachine.h>
13 #include <llvm/Config/llvm-config.h>
14 
15 #include <stdbool.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 struct ac_compiler_passes;
22 struct ac_llvm_context;
23 
24 /* Attributes at call sites of intrinsics. */
25 enum ac_call_site_attr {
26    AC_ATTR_INVARIANT_LOAD = 1 << 0,
27    AC_ATTR_CONVERGENT = 1 << 1,
28 };
29 
30 enum ac_target_machine_options
31 {
32    AC_TM_SUPPORTS_SPILL       = 1 << 0,
33    AC_TM_CHECK_IR             = 1 << 1,
34    AC_TM_CREATE_LOW_OPT       = 1 << 2,
35 };
36 
37 enum ac_float_mode
38 {
39    AC_FLOAT_MODE_DEFAULT,
40    AC_FLOAT_MODE_DEFAULT_OPENGL,
41    AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO,
42 };
43 
44 /* Per-thread persistent LLVM objects. */
45 struct ac_llvm_compiler {
46    LLVMTargetLibraryInfoRef target_library_info;
47    LLVMPassManagerRef passmgr;
48 
49    /* Default compiler. */
50    LLVMTargetMachineRef tm;
51    struct ac_compiler_passes *passes;
52 
53    /* Optional compiler for faster compilation with fewer optimizations.
54     * LLVM modules can be created with "tm" too. There is no difference.
55     */
56    LLVMTargetMachineRef low_opt_tm; /* uses -O1 instead of -O2 */
57    struct ac_compiler_passes *low_opt_passes;
58 };
59 
60 LLVMTargetRef ac_get_llvm_target(const char *triple);
61 void ac_llvm_run_atexit_for_destructors(void);
62 bool ac_is_llvm_processor_supported(LLVMTargetMachineRef tm, const char *processor);
63 void ac_reset_llvm_all_options_occurrences();
64 void ac_add_attr_dereferenceable(LLVMValueRef val, uint64_t bytes);
65 void ac_add_attr_alignment(LLVMValueRef val, uint64_t bytes);
66 bool ac_is_sgpr_param(LLVMValueRef param);
67 LLVMAttributeRef ac_get_llvm_attribute(LLVMContextRef ctx, const char *str);
68 void ac_add_function_attr(LLVMContextRef ctx, LLVMValueRef function, int attr_idx,
69                           const char *attr);
70 void ac_dump_module(LLVMModuleRef module);
71 LLVMModuleRef ac_create_module(LLVMTargetMachineRef tm, LLVMContextRef ctx);
72 LLVMBuilderRef ac_create_builder(LLVMContextRef ctx, enum ac_float_mode float_mode);
73 void ac_enable_signed_zeros(struct ac_llvm_context *ctx);
74 void ac_disable_signed_zeros(struct ac_llvm_context *ctx);
75 
76 void ac_llvm_add_target_dep_function_attr(LLVMValueRef F, const char *name, unsigned value);
77 void ac_llvm_set_workgroup_size(LLVMValueRef F, unsigned size);
78 void ac_llvm_set_target_features(LLVMValueRef F, struct ac_llvm_context *ctx, bool wgp_mode);
79 
80 LLVMTargetLibraryInfoRef ac_create_target_library_info(const char *triple);
81 void ac_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info);
82 PUBLIC void ac_init_shared_llvm_once(void); /* Do not use directly, use ac_init_llvm_once */
83 void ac_init_llvm_once(void);
84 
85 bool ac_init_llvm_compiler(struct ac_llvm_compiler *compiler, enum radeon_family family,
86                            enum ac_target_machine_options tm_options);
87 void ac_destroy_llvm_compiler(struct ac_llvm_compiler *compiler);
88 
89 struct ac_compiler_passes *ac_create_llvm_passes(LLVMTargetMachineRef tm);
90 void ac_destroy_llvm_passes(struct ac_compiler_passes *p);
91 bool ac_compile_module_to_elf(struct ac_compiler_passes *p, LLVMModuleRef module,
92                               char **pelf_buffer, size_t *pelf_size);
93 LLVMPassManagerRef ac_create_passmgr(LLVMTargetLibraryInfoRef target_library_info,
94                                      bool check_ir);
95 
ac_has_vec3_support(enum amd_gfx_level chip,bool use_format)96 static inline bool ac_has_vec3_support(enum amd_gfx_level chip, bool use_format)
97 {
98    /* GFX6 only supports vec3 with load/store format. */
99    return chip != GFX6 || use_format;
100 }
101 
102 #ifdef __cplusplus
103 }
104 #endif
105 
106 #endif /* AC_LLVM_UTIL_H */
107