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 "util/bitscan.h"
28 #include <llvm-c/Core.h>
29 #include <llvm-c/Support.h>
30 #include "c11/threads.h"
36 static void ac_init_llvm_target()
38 LLVMInitializeAMDGPUTargetInfo();
39 LLVMInitializeAMDGPUTarget();
40 LLVMInitializeAMDGPUTargetMC();
41 LLVMInitializeAMDGPUAsmPrinter();
43 /* For inline assembly. */
44 LLVMInitializeAMDGPUAsmParser();
46 /* Workaround for bug in llvm 4.0 that causes image intrinsics
48 * https://reviews.llvm.org/D26348
50 if (HAVE_LLVM
>= 0x0400) {
51 /* "mesa" is the prefix for error messages */
52 const char *argv
[2] = { "mesa", "-simplifycfg-sink-common=false" };
53 LLVMParseCommandLineOptions(2, argv
, NULL
);
57 static once_flag ac_init_llvm_target_once_flag
= ONCE_FLAG_INIT
;
59 LLVMTargetRef
ac_get_llvm_target(const char *triple
)
61 LLVMTargetRef target
= NULL
;
62 char *err_message
= NULL
;
64 call_once(&ac_init_llvm_target_once_flag
, ac_init_llvm_target
);
66 if (LLVMGetTargetFromTriple(triple
, &target
, &err_message
)) {
67 fprintf(stderr
, "Cannot find target for triple %s ", triple
);
69 fprintf(stderr
, "%s\n", err_message
);
71 LLVMDisposeMessage(err_message
);
77 static const char *ac_get_llvm_processor_name(enum radeon_family family
)
123 LLVMTargetMachineRef
ac_create_target_machine(enum radeon_family family
, enum ac_target_machine_options tm_options
)
125 assert(family
>= CHIP_TAHITI
);
127 const char *triple
= (tm_options
& AC_TM_SUPPORTS_SPILL
) ? "amdgcn-mesa-mesa3d" : "amdgcn--";
128 LLVMTargetRef target
= ac_get_llvm_target(triple
);
130 snprintf(features
, sizeof(features
),
131 "+DumpCode,+vgpr-spilling,-fp32-denormals%s",
132 tm_options
& AC_TM_SISCHED
? ",+si-scheduler" : "");
134 LLVMTargetMachineRef tm
= LLVMCreateTargetMachine(
137 ac_get_llvm_processor_name(family
),
139 LLVMCodeGenLevelDefault
,
141 LLVMCodeModelDefault
);
147 #if HAVE_LLVM < 0x0400
148 static LLVMAttribute
ac_attr_to_llvm_attr(enum ac_func_attr attr
)
151 case AC_FUNC_ATTR_ALWAYSINLINE
: return LLVMAlwaysInlineAttribute
;
152 case AC_FUNC_ATTR_BYVAL
: return LLVMByValAttribute
;
153 case AC_FUNC_ATTR_INREG
: return LLVMInRegAttribute
;
154 case AC_FUNC_ATTR_NOALIAS
: return LLVMNoAliasAttribute
;
155 case AC_FUNC_ATTR_NOUNWIND
: return LLVMNoUnwindAttribute
;
156 case AC_FUNC_ATTR_READNONE
: return LLVMReadNoneAttribute
;
157 case AC_FUNC_ATTR_READONLY
: return LLVMReadOnlyAttribute
;
159 fprintf(stderr
, "Unhandled function attribute: %x\n", attr
);
166 static const char *attr_to_str(enum ac_func_attr attr
)
169 case AC_FUNC_ATTR_ALWAYSINLINE
: return "alwaysinline";
170 case AC_FUNC_ATTR_BYVAL
: return "byval";
171 case AC_FUNC_ATTR_INREG
: return "inreg";
172 case AC_FUNC_ATTR_NOALIAS
: return "noalias";
173 case AC_FUNC_ATTR_NOUNWIND
: return "nounwind";
174 case AC_FUNC_ATTR_READNONE
: return "readnone";
175 case AC_FUNC_ATTR_READONLY
: return "readonly";
176 case AC_FUNC_ATTR_WRITEONLY
: return "writeonly";
177 case AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
: return "inaccessiblememonly";
178 case AC_FUNC_ATTR_CONVERGENT
: return "convergent";
180 fprintf(stderr
, "Unhandled function attribute: %x\n", attr
);
188 ac_add_function_attr(LLVMContextRef ctx
, LLVMValueRef function
,
189 int attr_idx
, enum ac_func_attr attr
)
191 #if HAVE_LLVM < 0x0400
192 LLVMAttribute llvm_attr
= ac_attr_to_llvm_attr(attr
);
193 if (attr_idx
== -1) {
194 LLVMAddFunctionAttr(function
, llvm_attr
);
196 LLVMAddAttribute(LLVMGetParam(function
, attr_idx
- 1), llvm_attr
);
199 const char *attr_name
= attr_to_str(attr
);
200 unsigned kind_id
= LLVMGetEnumAttributeKindForName(attr_name
,
202 LLVMAttributeRef llvm_attr
= LLVMCreateEnumAttribute(ctx
, kind_id
, 0);
204 if (LLVMIsAFunction(function
))
205 LLVMAddAttributeAtIndex(function
, attr_idx
, llvm_attr
);
207 LLVMAddCallSiteAttribute(function
, attr_idx
, llvm_attr
);
211 void ac_add_func_attributes(LLVMContextRef ctx
, LLVMValueRef function
,
212 unsigned attrib_mask
)
214 attrib_mask
|= AC_FUNC_ATTR_NOUNWIND
;
215 attrib_mask
&= ~AC_FUNC_ATTR_LEGACY
;
217 while (attrib_mask
) {
218 enum ac_func_attr attr
= 1u << u_bit_scan(&attrib_mask
);
219 ac_add_function_attr(ctx
, function
, -1, attr
);
224 ac_dump_module(LLVMModuleRef module
)
226 char *str
= LLVMPrintModuleToString(module
);
227 fprintf(stderr
, "%s", str
);
228 LLVMDisposeMessage(str
);
232 ac_llvm_add_target_dep_function_attr(LLVMValueRef F
,
233 const char *name
, int value
)
237 snprintf(str
, sizeof(str
), "%i", value
);
238 LLVMAddTargetDependentFunctionAttr(F
, name
, str
);