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 #include <llvm-c/Transforms/Utils.h>
34 #include "c11/threads.h"
35 #include "gallivm/lp_bld_misc.h"
36 #include "util/u_math.h"
42 static void ac_init_llvm_target()
44 LLVMInitializeAMDGPUTargetInfo();
45 LLVMInitializeAMDGPUTarget();
46 LLVMInitializeAMDGPUTargetMC();
47 LLVMInitializeAMDGPUAsmPrinter();
49 /* For inline assembly. */
50 LLVMInitializeAMDGPUAsmParser();
52 /* Workaround for bug in llvm 4.0 that causes image intrinsics
54 * https://reviews.llvm.org/D26348
56 * "mesa" is the prefix for error messages.
58 * -global-isel-abort=2 is a no-op unless global isel has been enabled.
59 * This option tells the backend to fall-back to SelectionDAG and print
60 * a diagnostic message if global isel fails.
62 const char *argv
[] = {
64 "-simplifycfg-sink-common=false",
65 "-global-isel-abort=2",
66 #if LLVM_VERSION_MAJOR >= 10
67 /* Atomic optimizations require LLVM 10.0 for gfx10 support. */
68 "-amdgpu-atomic-optimizations=true",
71 LLVMParseCommandLineOptions(ARRAY_SIZE(argv
), argv
, NULL
);
74 static once_flag ac_init_llvm_target_once_flag
= ONCE_FLAG_INIT
;
76 void ac_init_llvm_once(void)
78 call_once(&ac_init_llvm_target_once_flag
, ac_init_llvm_target
);
81 static LLVMTargetRef
ac_get_llvm_target(const char *triple
)
83 LLVMTargetRef target
= NULL
;
84 char *err_message
= NULL
;
86 if (LLVMGetTargetFromTriple(triple
, &target
, &err_message
)) {
87 fprintf(stderr
, "Cannot find target for triple %s ", triple
);
89 fprintf(stderr
, "%s\n", err_message
);
91 LLVMDisposeMessage(err_message
);
97 const char *ac_get_llvm_processor_name(enum radeon_family family
)
158 static LLVMTargetMachineRef
ac_create_target_machine(enum radeon_family family
,
159 enum ac_target_machine_options tm_options
,
160 LLVMCodeGenOptLevel level
,
161 const char **out_triple
)
163 assert(family
>= CHIP_TAHITI
);
165 const char *triple
= (tm_options
& AC_TM_SUPPORTS_SPILL
) ? "amdgcn-mesa-mesa3d" : "amdgcn--";
166 LLVMTargetRef target
= ac_get_llvm_target(triple
);
168 snprintf(features
, sizeof(features
),
169 "+DumpCode,-fp32-denormals,+fp64-denormals%s%s%s%s%s%s",
170 family
>= CHIP_NAVI10
&& !(tm_options
& AC_TM_WAVE32
) ?
171 ",+wavefrontsize64,-wavefrontsize32" : "",
172 tm_options
& AC_TM_SISCHED
? ",+si-scheduler" : "",
173 tm_options
& AC_TM_FORCE_ENABLE_XNACK
? ",+xnack" : "",
174 tm_options
& AC_TM_FORCE_DISABLE_XNACK
? ",-xnack" : "",
175 tm_options
& AC_TM_PROMOTE_ALLOCA_TO_SCRATCH
? ",-promote-alloca" : "",
176 tm_options
& AC_TM_NO_LOAD_STORE_OPT
? ",-load-store-opt" : "");
178 LLVMTargetMachineRef tm
= LLVMCreateTargetMachine(
181 ac_get_llvm_processor_name(family
),
185 LLVMCodeModelDefault
);
188 *out_triple
= triple
;
189 if (tm_options
& AC_TM_ENABLE_GLOBAL_ISEL
)
190 ac_enable_global_isel(tm
);
194 static LLVMPassManagerRef
ac_create_passmgr(LLVMTargetLibraryInfoRef target_library_info
,
197 LLVMPassManagerRef passmgr
= LLVMCreatePassManager();
201 if (target_library_info
)
202 LLVMAddTargetLibraryInfo(target_library_info
,
206 LLVMAddVerifierPass(passmgr
);
207 LLVMAddAlwaysInlinerPass(passmgr
);
208 /* Normally, the pass manager runs all passes on one function before
209 * moving onto another. Adding a barrier no-op pass forces the pass
210 * manager to run the inliner on all functions first, which makes sure
211 * that the following passes are only run on the remaining non-inline
212 * function, so it removes useless work done on dead inline functions.
214 ac_llvm_add_barrier_noop_pass(passmgr
);
215 /* This pass should eliminate all the load and store instructions. */
216 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
217 LLVMAddScalarReplAggregatesPass(passmgr
);
218 LLVMAddLICMPass(passmgr
);
219 LLVMAddAggressiveDCEPass(passmgr
);
220 LLVMAddCFGSimplificationPass(passmgr
);
221 /* This is recommended by the instruction combining pass. */
222 LLVMAddEarlyCSEMemSSAPass(passmgr
);
223 LLVMAddInstructionCombiningPass(passmgr
);
227 static const char *attr_to_str(enum ac_func_attr attr
)
230 case AC_FUNC_ATTR_ALWAYSINLINE
: return "alwaysinline";
231 case AC_FUNC_ATTR_INREG
: return "inreg";
232 case AC_FUNC_ATTR_NOALIAS
: return "noalias";
233 case AC_FUNC_ATTR_NOUNWIND
: return "nounwind";
234 case AC_FUNC_ATTR_READNONE
: return "readnone";
235 case AC_FUNC_ATTR_READONLY
: return "readonly";
236 case AC_FUNC_ATTR_WRITEONLY
: return "writeonly";
237 case AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
: return "inaccessiblememonly";
238 case AC_FUNC_ATTR_CONVERGENT
: return "convergent";
240 fprintf(stderr
, "Unhandled function attribute: %x\n", attr
);
246 ac_add_function_attr(LLVMContextRef ctx
, LLVMValueRef function
,
247 int attr_idx
, enum ac_func_attr attr
)
249 const char *attr_name
= attr_to_str(attr
);
250 unsigned kind_id
= LLVMGetEnumAttributeKindForName(attr_name
,
252 LLVMAttributeRef llvm_attr
= LLVMCreateEnumAttribute(ctx
, kind_id
, 0);
254 if (LLVMIsAFunction(function
))
255 LLVMAddAttributeAtIndex(function
, attr_idx
, llvm_attr
);
257 LLVMAddCallSiteAttribute(function
, attr_idx
, llvm_attr
);
260 void ac_add_func_attributes(LLVMContextRef ctx
, LLVMValueRef function
,
261 unsigned attrib_mask
)
263 attrib_mask
|= AC_FUNC_ATTR_NOUNWIND
;
264 attrib_mask
&= ~AC_FUNC_ATTR_LEGACY
;
266 while (attrib_mask
) {
267 enum ac_func_attr attr
= 1u << u_bit_scan(&attrib_mask
);
268 ac_add_function_attr(ctx
, function
, -1, attr
);
273 ac_dump_module(LLVMModuleRef module
)
275 char *str
= LLVMPrintModuleToString(module
);
276 fprintf(stderr
, "%s", str
);
277 LLVMDisposeMessage(str
);
281 ac_llvm_add_target_dep_function_attr(LLVMValueRef F
,
282 const char *name
, unsigned value
)
286 snprintf(str
, sizeof(str
), "0x%x", value
);
287 LLVMAddTargetDependentFunctionAttr(F
, name
, str
);
290 void ac_llvm_set_workgroup_size(LLVMValueRef F
, unsigned size
)
296 snprintf(str
, sizeof(str
), "%u,%u", size
, size
);
297 LLVMAddTargetDependentFunctionAttr(F
, "amdgpu-flat-work-group-size", str
);
301 ac_count_scratch_private_memory(LLVMValueRef function
)
303 unsigned private_mem_vgprs
= 0;
305 /* Process all LLVM instructions. */
306 LLVMBasicBlockRef bb
= LLVMGetFirstBasicBlock(function
);
308 LLVMValueRef next
= LLVMGetFirstInstruction(bb
);
311 LLVMValueRef inst
= next
;
312 next
= LLVMGetNextInstruction(next
);
314 if (LLVMGetInstructionOpcode(inst
) != LLVMAlloca
)
317 LLVMTypeRef type
= LLVMGetElementType(LLVMTypeOf(inst
));
318 /* No idea why LLVM aligns allocas to 4 elements. */
319 unsigned alignment
= LLVMGetAlignment(inst
);
320 unsigned dw_size
= align(ac_get_type_size(type
) / 4, alignment
);
321 private_mem_vgprs
+= dw_size
;
323 bb
= LLVMGetNextBasicBlock(bb
);
326 return private_mem_vgprs
;
330 ac_init_llvm_compiler(struct ac_llvm_compiler
*compiler
,
331 enum radeon_family family
,
332 enum ac_target_machine_options tm_options
)
335 memset(compiler
, 0, sizeof(*compiler
));
337 compiler
->tm
= ac_create_target_machine(family
, tm_options
,
338 LLVMCodeGenLevelDefault
,
343 if (tm_options
& AC_TM_CREATE_LOW_OPT
) {
344 compiler
->low_opt_tm
=
345 ac_create_target_machine(family
, tm_options
,
346 LLVMCodeGenLevelLess
, NULL
);
347 if (!compiler
->low_opt_tm
)
351 if (family
>= CHIP_NAVI10
) {
352 assert(!(tm_options
& AC_TM_CREATE_LOW_OPT
));
353 compiler
->tm_wave32
= ac_create_target_machine(family
,
354 tm_options
| AC_TM_WAVE32
,
355 LLVMCodeGenLevelDefault
,
357 if (!compiler
->tm_wave32
)
361 compiler
->target_library_info
=
362 ac_create_target_library_info(triple
);
363 if (!compiler
->target_library_info
)
366 compiler
->passmgr
= ac_create_passmgr(compiler
->target_library_info
,
367 tm_options
& AC_TM_CHECK_IR
);
368 if (!compiler
->passmgr
)
373 ac_destroy_llvm_compiler(compiler
);
378 ac_destroy_llvm_compiler(struct ac_llvm_compiler
*compiler
)
380 ac_destroy_llvm_passes(compiler
->passes
);
381 ac_destroy_llvm_passes(compiler
->passes_wave32
);
382 ac_destroy_llvm_passes(compiler
->low_opt_passes
);
384 if (compiler
->passmgr
)
385 LLVMDisposePassManager(compiler
->passmgr
);
386 if (compiler
->target_library_info
)
387 ac_dispose_target_library_info(compiler
->target_library_info
);
388 if (compiler
->low_opt_tm
)
389 LLVMDisposeTargetMachine(compiler
->low_opt_tm
);
391 LLVMDisposeTargetMachine(compiler
->tm
);
392 if (compiler
->tm_wave32
)
393 LLVMDisposeTargetMachine(compiler
->tm_wave32
);