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
[3] = { "mesa", "-simplifycfg-sink-common=false", "-global-isel-abort=2" };
63 LLVMParseCommandLineOptions(3, argv
, NULL
);
66 static once_flag ac_init_llvm_target_once_flag
= ONCE_FLAG_INIT
;
68 void ac_init_llvm_once(void)
70 call_once(&ac_init_llvm_target_once_flag
, ac_init_llvm_target
);
73 static LLVMTargetRef
ac_get_llvm_target(const char *triple
)
75 LLVMTargetRef target
= NULL
;
76 char *err_message
= NULL
;
78 if (LLVMGetTargetFromTriple(triple
, &target
, &err_message
)) {
79 fprintf(stderr
, "Cannot find target for triple %s ", triple
);
81 fprintf(stderr
, "%s\n", err_message
);
83 LLVMDisposeMessage(err_message
);
89 const char *ac_get_llvm_processor_name(enum radeon_family family
)
137 return HAVE_LLVM
>= 0x0800 ? "gfx909" : "gfx902";
143 static LLVMTargetMachineRef
ac_create_target_machine(enum radeon_family family
,
144 enum ac_target_machine_options tm_options
,
145 LLVMCodeGenOptLevel level
,
146 const char **out_triple
)
148 assert(family
>= CHIP_TAHITI
);
150 const char *triple
= (tm_options
& AC_TM_SUPPORTS_SPILL
) ? "amdgcn-mesa-mesa3d" : "amdgcn--";
151 LLVMTargetRef target
= ac_get_llvm_target(triple
);
153 snprintf(features
, sizeof(features
),
154 "+DumpCode,-fp32-denormals,+fp64-denormals%s%s%s%s%s",
155 HAVE_LLVM
>= 0x0800 ? "" : ",+vgpr-spilling",
156 tm_options
& AC_TM_SISCHED
? ",+si-scheduler" : "",
157 tm_options
& AC_TM_FORCE_ENABLE_XNACK
? ",+xnack" : "",
158 tm_options
& AC_TM_FORCE_DISABLE_XNACK
? ",-xnack" : "",
159 tm_options
& AC_TM_PROMOTE_ALLOCA_TO_SCRATCH
? ",-promote-alloca" : "");
161 LLVMTargetMachineRef tm
= LLVMCreateTargetMachine(
164 ac_get_llvm_processor_name(family
),
168 LLVMCodeModelDefault
);
171 *out_triple
= triple
;
172 if (tm_options
& AC_TM_ENABLE_GLOBAL_ISEL
)
173 ac_enable_global_isel(tm
);
177 static LLVMPassManagerRef
ac_create_passmgr(LLVMTargetLibraryInfoRef target_library_info
,
180 LLVMPassManagerRef passmgr
= LLVMCreatePassManager();
184 if (target_library_info
)
185 LLVMAddTargetLibraryInfo(target_library_info
,
189 LLVMAddVerifierPass(passmgr
);
190 LLVMAddAlwaysInlinerPass(passmgr
);
191 /* Normally, the pass manager runs all passes on one function before
192 * moving onto another. Adding a barrier no-op pass forces the pass
193 * manager to run the inliner on all functions first, which makes sure
194 * that the following passes are only run on the remaining non-inline
195 * function, so it removes useless work done on dead inline functions.
197 ac_llvm_add_barrier_noop_pass(passmgr
);
198 /* This pass should eliminate all the load and store instructions. */
199 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
200 LLVMAddScalarReplAggregatesPass(passmgr
);
201 LLVMAddLICMPass(passmgr
);
202 LLVMAddAggressiveDCEPass(passmgr
);
203 LLVMAddCFGSimplificationPass(passmgr
);
204 /* This is recommended by the instruction combining pass. */
205 LLVMAddEarlyCSEMemSSAPass(passmgr
);
206 LLVMAddInstructionCombiningPass(passmgr
);
210 static const char *attr_to_str(enum ac_func_attr attr
)
213 case AC_FUNC_ATTR_ALWAYSINLINE
: return "alwaysinline";
214 case AC_FUNC_ATTR_INREG
: return "inreg";
215 case AC_FUNC_ATTR_NOALIAS
: return "noalias";
216 case AC_FUNC_ATTR_NOUNWIND
: return "nounwind";
217 case AC_FUNC_ATTR_READNONE
: return "readnone";
218 case AC_FUNC_ATTR_READONLY
: return "readonly";
219 case AC_FUNC_ATTR_WRITEONLY
: return "writeonly";
220 case AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
: return "inaccessiblememonly";
221 case AC_FUNC_ATTR_CONVERGENT
: return "convergent";
223 fprintf(stderr
, "Unhandled function attribute: %x\n", attr
);
229 ac_add_function_attr(LLVMContextRef ctx
, LLVMValueRef function
,
230 int attr_idx
, enum ac_func_attr attr
)
232 const char *attr_name
= attr_to_str(attr
);
233 unsigned kind_id
= LLVMGetEnumAttributeKindForName(attr_name
,
235 LLVMAttributeRef llvm_attr
= LLVMCreateEnumAttribute(ctx
, kind_id
, 0);
237 if (LLVMIsAFunction(function
))
238 LLVMAddAttributeAtIndex(function
, attr_idx
, llvm_attr
);
240 LLVMAddCallSiteAttribute(function
, attr_idx
, llvm_attr
);
243 void ac_add_func_attributes(LLVMContextRef ctx
, LLVMValueRef function
,
244 unsigned attrib_mask
)
246 attrib_mask
|= AC_FUNC_ATTR_NOUNWIND
;
247 attrib_mask
&= ~AC_FUNC_ATTR_LEGACY
;
249 while (attrib_mask
) {
250 enum ac_func_attr attr
= 1u << u_bit_scan(&attrib_mask
);
251 ac_add_function_attr(ctx
, function
, -1, attr
);
256 ac_dump_module(LLVMModuleRef module
)
258 char *str
= LLVMPrintModuleToString(module
);
259 fprintf(stderr
, "%s", str
);
260 LLVMDisposeMessage(str
);
264 ac_llvm_add_target_dep_function_attr(LLVMValueRef F
,
265 const char *name
, unsigned value
)
269 snprintf(str
, sizeof(str
), "0x%x", value
);
270 LLVMAddTargetDependentFunctionAttr(F
, name
, str
);
274 ac_count_scratch_private_memory(LLVMValueRef function
)
276 unsigned private_mem_vgprs
= 0;
278 /* Process all LLVM instructions. */
279 LLVMBasicBlockRef bb
= LLVMGetFirstBasicBlock(function
);
281 LLVMValueRef next
= LLVMGetFirstInstruction(bb
);
284 LLVMValueRef inst
= next
;
285 next
= LLVMGetNextInstruction(next
);
287 if (LLVMGetInstructionOpcode(inst
) != LLVMAlloca
)
290 LLVMTypeRef type
= LLVMGetElementType(LLVMTypeOf(inst
));
291 /* No idea why LLVM aligns allocas to 4 elements. */
292 unsigned alignment
= LLVMGetAlignment(inst
);
293 unsigned dw_size
= align(ac_get_type_size(type
) / 4, alignment
);
294 private_mem_vgprs
+= dw_size
;
296 bb
= LLVMGetNextBasicBlock(bb
);
299 return private_mem_vgprs
;
303 ac_init_llvm_compiler(struct ac_llvm_compiler
*compiler
,
304 enum radeon_family family
,
305 enum ac_target_machine_options tm_options
)
308 memset(compiler
, 0, sizeof(*compiler
));
310 compiler
->tm
= ac_create_target_machine(family
, tm_options
,
311 LLVMCodeGenLevelDefault
,
316 if (tm_options
& AC_TM_CREATE_LOW_OPT
) {
317 compiler
->low_opt_tm
=
318 ac_create_target_machine(family
, tm_options
,
319 LLVMCodeGenLevelLess
, NULL
);
320 if (!compiler
->low_opt_tm
)
324 compiler
->target_library_info
=
325 ac_create_target_library_info(triple
);
326 if (!compiler
->target_library_info
)
329 compiler
->passmgr
= ac_create_passmgr(compiler
->target_library_info
,
330 tm_options
& AC_TM_CHECK_IR
);
331 if (!compiler
->passmgr
)
336 ac_destroy_llvm_compiler(compiler
);
341 ac_destroy_llvm_compiler(struct ac_llvm_compiler
*compiler
)
343 if (compiler
->passmgr
)
344 LLVMDisposePassManager(compiler
->passmgr
);
345 if (compiler
->target_library_info
)
346 ac_dispose_target_library_info(compiler
->target_library_info
);
347 if (compiler
->low_opt_tm
)
348 LLVMDisposeTargetMachine(compiler
->low_opt_tm
);
350 LLVMDisposeTargetMachine(compiler
->tm
);