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 };
35
36 enum ac_float_mode
37 {
38 AC_FLOAT_MODE_DEFAULT,
39 AC_FLOAT_MODE_DEFAULT_OPENGL,
40 AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO,
41 };
42
43 /* Per-thread persistent LLVM objects. */
44 struct ac_llvm_compiler {
45 /* Default compiler. */
46 LLVMTargetMachineRef tm;
47 struct ac_midend_optimizer *meo;
48 struct ac_backend_optimizer *beo;
49 };
50
51 LLVMTargetRef ac_get_llvm_target(const char *triple);
52 void ac_llvm_run_atexit_for_destructors(void);
53 bool ac_is_llvm_processor_supported(LLVMTargetMachineRef tm, const char *processor);
54 void ac_reset_llvm_all_options_occurrences();
55 void ac_add_attr_dereferenceable(LLVMValueRef val, uint64_t bytes);
56 void ac_add_attr_alignment(LLVMValueRef val, uint64_t bytes);
57 LLVMAttributeRef ac_get_llvm_attribute(LLVMContextRef ctx, const char *str);
58 void ac_add_function_attr(LLVMContextRef ctx, LLVMValueRef function, int attr_idx,
59 const char *attr);
60 void ac_dump_module(LLVMModuleRef module);
61 LLVMModuleRef ac_create_module(LLVMTargetMachineRef tm, LLVMContextRef ctx);
62 LLVMBuilderRef ac_create_builder(LLVMContextRef ctx, enum ac_float_mode float_mode);
63 void ac_enable_signed_zeros(struct ac_llvm_context *ctx);
64 void ac_disable_signed_zeros(struct ac_llvm_context *ctx);
65
66 void ac_llvm_add_target_dep_function_attr(LLVMValueRef F, const char *name, unsigned value);
67 void ac_llvm_set_workgroup_size(LLVMValueRef F, unsigned size);
68 void ac_llvm_set_target_features(LLVMValueRef F, struct ac_llvm_context *ctx, bool wgp_mode);
69
70 PUBLIC void ac_init_shared_llvm_once(void); /* Do not use directly, use ac_init_llvm_once */
71 void ac_init_llvm_once(void);
72
73 bool ac_init_llvm_compiler(struct ac_llvm_compiler *compiler, enum radeon_family family,
74 enum ac_target_machine_options tm_options);
75 void ac_destroy_llvm_compiler(struct ac_llvm_compiler *compiler);
76
77 struct ac_midend_optimizer *ac_create_midend_optimizer(LLVMTargetMachineRef tm,
78 bool check_ir);
79 void ac_destroy_midend_optimiser(struct ac_midend_optimizer *meo);
80 bool ac_llvm_optimize_module(struct ac_midend_optimizer *meo, LLVMModuleRef module);
81
82 struct ac_backend_optimizer *ac_create_backend_optimizer(LLVMTargetMachineRef tm);
83 void ac_destroy_backend_optimizer(struct ac_backend_optimizer *beo);
84 bool ac_compile_module_to_elf(struct ac_backend_optimizer *beo, LLVMModuleRef module,
85 char **pelf_buffer, size_t *pelf_size);
86
ac_has_vec3_support(enum amd_gfx_level chip,bool use_format)87 static inline bool ac_has_vec3_support(enum amd_gfx_level chip, bool use_format)
88 {
89 /* GFX6 only supports vec3 with load/store format. */
90 return chip != GFX6 || use_format;
91 }
92
93 #ifdef __cplusplus
94 }
95 #endif
96
97 #endif /* AC_LLVM_UTIL_H */
98