2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
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.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
25 /* based on pieces from si_pipe.c and radeon_llvm_emit.c */
26 #include "ac_llvm_util.h"
27 #include "ac_llvm_build.h"
28 #include "util/bitscan.h"
29 #include <llvm-c/Core.h>
30 #include <llvm-c/Support.h>
31 #include <llvm-c/Transforms/IPO.h>
32 #include <llvm-c/Transforms/Scalar.h>
33 #if HAVE_LLVM >= 0x0700
34 #include <llvm-c/Transforms/Utils.h>
36 #include "c11/threads.h"
37 #include "gallivm/lp_bld_misc.h"
38 #include "util/u_math.h"
44 static void ac_init_llvm_target()
46 LLVMInitializeAMDGPUTargetInfo();
47 LLVMInitializeAMDGPUTarget();
48 LLVMInitializeAMDGPUTargetMC();
49 LLVMInitializeAMDGPUAsmPrinter();
51 /* For inline assembly. */
52 LLVMInitializeAMDGPUAsmParser();
54 /* Workaround for bug in llvm 4.0 that causes image intrinsics
56 * https://reviews.llvm.org/D26348
58 * "mesa" is the prefix for error messages.
60 * -global-isel-abort=2 is a no-op unless global isel has been enabled.
61 * This option tells the backend to fall-back to SelectionDAG and print
62 * a diagnostic message if global isel fails.
64 const char *argv
[3] = { "mesa", "-simplifycfg-sink-common=false", "-global-isel-abort=2" };
65 LLVMParseCommandLineOptions(3, argv
, NULL
);
68 static once_flag ac_init_llvm_target_once_flag
= ONCE_FLAG_INIT
;
70 void ac_init_llvm_once(void)
72 call_once(&ac_init_llvm_target_once_flag
, ac_init_llvm_target
);
75 static LLVMTargetRef
ac_get_llvm_target(const char *triple
)
77 LLVMTargetRef target
= NULL
;
78 char *err_message
= NULL
;
80 if (LLVMGetTargetFromTriple(triple
, &target
, &err_message
)) {
81 fprintf(stderr
, "Cannot find target for triple %s ", triple
);
83 fprintf(stderr
, "%s\n", err_message
);
85 LLVMDisposeMessage(err_message
);
91 const char *ac_get_llvm_processor_name(enum radeon_family family
)
135 return HAVE_LLVM
>= 0x0700 ? "gfx904" : "gfx902";
137 return HAVE_LLVM
>= 0x0700 ? "gfx906" : "gfx902";
139 return "gfx902"; /* TODO: use gfx909 when it's available */
145 static LLVMTargetMachineRef
ac_create_target_machine(enum radeon_family family
,
146 enum ac_target_machine_options tm_options
,
147 LLVMCodeGenOptLevel level
,
148 const char **out_triple
)
150 assert(family
>= CHIP_TAHITI
);
152 const char *triple
= (tm_options
& AC_TM_SUPPORTS_SPILL
) ? "amdgcn-mesa-mesa3d" : "amdgcn--";
153 LLVMTargetRef target
= ac_get_llvm_target(triple
);
155 snprintf(features
, sizeof(features
),
156 "+DumpCode,+vgpr-spilling,-fp32-denormals,+fp64-denormals%s%s%s%s",
157 tm_options
& AC_TM_SISCHED
? ",+si-scheduler" : "",
158 tm_options
& AC_TM_FORCE_ENABLE_XNACK
? ",+xnack" : "",
159 tm_options
& AC_TM_FORCE_DISABLE_XNACK
? ",-xnack" : "",
160 tm_options
& AC_TM_PROMOTE_ALLOCA_TO_SCRATCH
? ",-promote-alloca" : "");
162 LLVMTargetMachineRef tm
= LLVMCreateTargetMachine(
165 ac_get_llvm_processor_name(family
),
169 LLVMCodeModelDefault
);
172 *out_triple
= triple
;
173 if (tm_options
& AC_TM_ENABLE_GLOBAL_ISEL
)
174 ac_enable_global_isel(tm
);
178 static LLVMPassManagerRef
ac_create_passmgr(LLVMTargetLibraryInfoRef target_library_info
,
181 LLVMPassManagerRef passmgr
= LLVMCreatePassManager();
185 if (target_library_info
)
186 LLVMAddTargetLibraryInfo(target_library_info
,
190 LLVMAddVerifierPass(passmgr
);
191 LLVMAddAlwaysInlinerPass(passmgr
);
192 /* Normally, the pass manager runs all passes on one function before
193 * moving onto another. Adding a barrier no-op pass forces the pass
194 * manager to run the inliner on all functions first, which makes sure
195 * that the following passes are only run on the remaining non-inline
196 * function, so it removes useless work done on dead inline functions.
198 ac_llvm_add_barrier_noop_pass(passmgr
);
199 /* This pass should eliminate all the load and store instructions. */
200 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
201 LLVMAddScalarReplAggregatesPass(passmgr
);
202 LLVMAddLICMPass(passmgr
);
203 LLVMAddAggressiveDCEPass(passmgr
);
204 LLVMAddCFGSimplificationPass(passmgr
);
205 /* This is recommended by the instruction combining pass. */
206 LLVMAddEarlyCSEMemSSAPass(passmgr
);
207 LLVMAddInstructionCombiningPass(passmgr
);
211 static const char *attr_to_str(enum ac_func_attr attr
)
214 case AC_FUNC_ATTR_ALWAYSINLINE
: return "alwaysinline";
215 case AC_FUNC_ATTR_INREG
: return "inreg";
216 case AC_FUNC_ATTR_NOALIAS
: return "noalias";
217 case AC_FUNC_ATTR_NOUNWIND
: return "nounwind";
218 case AC_FUNC_ATTR_READNONE
: return "readnone";
219 case AC_FUNC_ATTR_READONLY
: return "readonly";
220 case AC_FUNC_ATTR_WRITEONLY
: return "writeonly";
221 case AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
: return "inaccessiblememonly";
222 case AC_FUNC_ATTR_CONVERGENT
: return "convergent";
224 fprintf(stderr
, "Unhandled function attribute: %x\n", attr
);
230 ac_add_function_attr(LLVMContextRef ctx
, LLVMValueRef function
,
231 int attr_idx
, enum ac_func_attr attr
)
233 const char *attr_name
= attr_to_str(attr
);
234 unsigned kind_id
= LLVMGetEnumAttributeKindForName(attr_name
,
236 LLVMAttributeRef llvm_attr
= LLVMCreateEnumAttribute(ctx
, kind_id
, 0);
238 if (LLVMIsAFunction(function
))
239 LLVMAddAttributeAtIndex(function
, attr_idx
, llvm_attr
);
241 LLVMAddCallSiteAttribute(function
, attr_idx
, llvm_attr
);
244 void ac_add_func_attributes(LLVMContextRef ctx
, LLVMValueRef function
,
245 unsigned attrib_mask
)
247 attrib_mask
|= AC_FUNC_ATTR_NOUNWIND
;
248 attrib_mask
&= ~AC_FUNC_ATTR_LEGACY
;
250 while (attrib_mask
) {
251 enum ac_func_attr attr
= 1u << u_bit_scan(&attrib_mask
);
252 ac_add_function_attr(ctx
, function
, -1, attr
);
257 ac_dump_module(LLVMModuleRef module
)
259 char *str
= LLVMPrintModuleToString(module
);
260 fprintf(stderr
, "%s", str
);
261 LLVMDisposeMessage(str
);
265 ac_llvm_add_target_dep_function_attr(LLVMValueRef F
,
266 const char *name
, unsigned value
)
270 snprintf(str
, sizeof(str
), "0x%x", value
);
271 LLVMAddTargetDependentFunctionAttr(F
, name
, str
);
275 ac_count_scratch_private_memory(LLVMValueRef function
)
277 unsigned private_mem_vgprs
= 0;
279 /* Process all LLVM instructions. */
280 LLVMBasicBlockRef bb
= LLVMGetFirstBasicBlock(function
);
282 LLVMValueRef next
= LLVMGetFirstInstruction(bb
);
285 LLVMValueRef inst
= next
;
286 next
= LLVMGetNextInstruction(next
);
288 if (LLVMGetInstructionOpcode(inst
) != LLVMAlloca
)
291 LLVMTypeRef type
= LLVMGetElementType(LLVMTypeOf(inst
));
292 /* No idea why LLVM aligns allocas to 4 elements. */
293 unsigned alignment
= LLVMGetAlignment(inst
);
294 unsigned dw_size
= align(ac_get_type_size(type
) / 4, alignment
);
295 private_mem_vgprs
+= dw_size
;
297 bb
= LLVMGetNextBasicBlock(bb
);
300 return private_mem_vgprs
;
304 ac_init_llvm_compiler(struct ac_llvm_compiler
*compiler
,
305 bool okay_to_leak_target_library_info
,
306 enum radeon_family family
,
307 enum ac_target_machine_options tm_options
)
310 memset(compiler
, 0, sizeof(*compiler
));
312 compiler
->tm
= ac_create_target_machine(family
, tm_options
,
313 LLVMCodeGenLevelDefault
,
318 if (tm_options
& AC_TM_CREATE_LOW_OPT
) {
319 compiler
->low_opt_tm
=
320 ac_create_target_machine(family
, tm_options
,
321 LLVMCodeGenLevelLess
, NULL
);
322 if (!compiler
->low_opt_tm
)
326 if (okay_to_leak_target_library_info
|| (HAVE_LLVM
>= 0x0700)) {
327 compiler
->target_library_info
=
328 ac_create_target_library_info(triple
);
329 if (!compiler
->target_library_info
)
333 compiler
->passmgr
= ac_create_passmgr(compiler
->target_library_info
,
334 tm_options
& AC_TM_CHECK_IR
);
335 if (!compiler
->passmgr
)
340 ac_destroy_llvm_compiler(compiler
);
345 ac_destroy_llvm_compiler(struct ac_llvm_compiler
*compiler
)
347 if (compiler
->passmgr
)
348 LLVMDisposePassManager(compiler
->passmgr
);
349 #if HAVE_LLVM >= 0x0700
350 /* This crashes on LLVM 5.0 and 6.0 and Ubuntu 18.04, so leak it there. */
351 if (compiler
->target_library_info
)
352 ac_dispose_target_library_info(compiler
->target_library_info
);
354 if (compiler
->low_opt_tm
)
355 LLVMDisposeTargetMachine(compiler
->low_opt_tm
);
357 LLVMDisposeTargetMachine(compiler
->tm
);