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 "c11/threads.h"
32 #include "util/u_math.h"
38 static void ac_init_llvm_target()
40 LLVMInitializeAMDGPUTargetInfo();
41 LLVMInitializeAMDGPUTarget();
42 LLVMInitializeAMDGPUTargetMC();
43 LLVMInitializeAMDGPUAsmPrinter();
45 /* For inline assembly. */
46 LLVMInitializeAMDGPUAsmParser();
48 /* Workaround for bug in llvm 4.0 that causes image intrinsics
50 * https://reviews.llvm.org/D26348
52 * "mesa" is the prefix for error messages.
54 const char *argv
[2] = { "mesa", "-simplifycfg-sink-common=false" };
55 LLVMParseCommandLineOptions(2, argv
, NULL
);
58 static once_flag ac_init_llvm_target_once_flag
= ONCE_FLAG_INIT
;
60 LLVMTargetRef
ac_get_llvm_target(const char *triple
)
62 LLVMTargetRef target
= NULL
;
63 char *err_message
= NULL
;
65 call_once(&ac_init_llvm_target_once_flag
, ac_init_llvm_target
);
67 if (LLVMGetTargetFromTriple(triple
, &target
, &err_message
)) {
68 fprintf(stderr
, "Cannot find target for triple %s ", triple
);
70 fprintf(stderr
, "%s\n", err_message
);
72 LLVMDisposeMessage(err_message
);
78 const char *ac_get_llvm_processor_name(enum radeon_family family
)
124 LLVMTargetMachineRef
ac_create_target_machine(enum radeon_family family
, enum ac_target_machine_options tm_options
)
126 assert(family
>= CHIP_TAHITI
);
128 const char *triple
= (tm_options
& AC_TM_SUPPORTS_SPILL
) ? "amdgcn-mesa-mesa3d" : "amdgcn--";
129 LLVMTargetRef target
= ac_get_llvm_target(triple
);
131 snprintf(features
, sizeof(features
),
132 "+DumpCode,+vgpr-spilling,-fp32-denormals,+fp64-denormals%s%s%s%s",
133 tm_options
& AC_TM_SISCHED
? ",+si-scheduler" : "",
134 tm_options
& AC_TM_FORCE_ENABLE_XNACK
? ",+xnack" : "",
135 tm_options
& AC_TM_FORCE_DISABLE_XNACK
? ",-xnack" : "",
136 tm_options
& AC_TM_PROMOTE_ALLOCA_TO_SCRATCH
? ",-promote-alloca" : "");
138 LLVMTargetMachineRef tm
= LLVMCreateTargetMachine(
141 ac_get_llvm_processor_name(family
),
143 LLVMCodeGenLevelDefault
,
145 LLVMCodeModelDefault
);
150 static const char *attr_to_str(enum ac_func_attr attr
)
153 case AC_FUNC_ATTR_ALWAYSINLINE
: return "alwaysinline";
154 case AC_FUNC_ATTR_INREG
: return "inreg";
155 case AC_FUNC_ATTR_NOALIAS
: return "noalias";
156 case AC_FUNC_ATTR_NOUNWIND
: return "nounwind";
157 case AC_FUNC_ATTR_READNONE
: return "readnone";
158 case AC_FUNC_ATTR_READONLY
: return "readonly";
159 case AC_FUNC_ATTR_WRITEONLY
: return "writeonly";
160 case AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
: return "inaccessiblememonly";
161 case AC_FUNC_ATTR_CONVERGENT
: return "convergent";
163 fprintf(stderr
, "Unhandled function attribute: %x\n", attr
);
169 ac_add_function_attr(LLVMContextRef ctx
, LLVMValueRef function
,
170 int attr_idx
, enum ac_func_attr attr
)
172 const char *attr_name
= attr_to_str(attr
);
173 unsigned kind_id
= LLVMGetEnumAttributeKindForName(attr_name
,
175 LLVMAttributeRef llvm_attr
= LLVMCreateEnumAttribute(ctx
, kind_id
, 0);
177 if (LLVMIsAFunction(function
))
178 LLVMAddAttributeAtIndex(function
, attr_idx
, llvm_attr
);
180 LLVMAddCallSiteAttribute(function
, attr_idx
, llvm_attr
);
183 void ac_add_func_attributes(LLVMContextRef ctx
, LLVMValueRef function
,
184 unsigned attrib_mask
)
186 attrib_mask
|= AC_FUNC_ATTR_NOUNWIND
;
187 attrib_mask
&= ~AC_FUNC_ATTR_LEGACY
;
189 while (attrib_mask
) {
190 enum ac_func_attr attr
= 1u << u_bit_scan(&attrib_mask
);
191 ac_add_function_attr(ctx
, function
, -1, attr
);
196 ac_dump_module(LLVMModuleRef module
)
198 char *str
= LLVMPrintModuleToString(module
);
199 fprintf(stderr
, "%s", str
);
200 LLVMDisposeMessage(str
);
204 ac_llvm_add_target_dep_function_attr(LLVMValueRef F
,
205 const char *name
, int value
)
209 snprintf(str
, sizeof(str
), "%i", value
);
210 LLVMAddTargetDependentFunctionAttr(F
, name
, str
);
214 ac_count_scratch_private_memory(LLVMValueRef function
)
216 unsigned private_mem_vgprs
= 0;
218 /* Process all LLVM instructions. */
219 LLVMBasicBlockRef bb
= LLVMGetFirstBasicBlock(function
);
221 LLVMValueRef next
= LLVMGetFirstInstruction(bb
);
224 LLVMValueRef inst
= next
;
225 next
= LLVMGetNextInstruction(next
);
227 if (LLVMGetInstructionOpcode(inst
) != LLVMAlloca
)
230 LLVMTypeRef type
= LLVMGetElementType(LLVMTypeOf(inst
));
231 /* No idea why LLVM aligns allocas to 4 elements. */
232 unsigned alignment
= LLVMGetAlignment(inst
);
233 unsigned dw_size
= align(ac_get_type_size(type
) / 4, alignment
);
234 private_mem_vgprs
+= dw_size
;
236 bb
= LLVMGetNextBasicBlock(bb
);
239 return private_mem_vgprs
;