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
28 #include <llvm-c/Core.h>
29 #include <llvm/Target/TargetMachine.h>
30 #include <llvm/IR/IRBuilder.h>
31 #include <llvm/Analysis/TargetLibraryInfo.h>
32 #include <llvm/Transforms/IPO.h>
34 #include <llvm/IR/LegacyPassManager.h>
36 /* DO NOT REORDER THE HEADERS
37 * The LLVM headers need to all be included before any Mesa header,
38 * as they use the `restrict` keyword in ways that are incompatible
39 * with our #define in include/c99_compat.h
42 #include "ac_binary.h"
43 #include "ac_llvm_util.h"
44 #include "ac_llvm_build.h"
46 #include "util/macros.h"
48 void ac_add_attr_dereferenceable(LLVMValueRef val
, uint64_t bytes
)
50 llvm::Argument
*A
= llvm::unwrap
<llvm::Argument
>(val
);
51 A
->addAttr(llvm::Attribute::getWithDereferenceableBytes(A
->getContext(), bytes
));
54 void ac_add_attr_alignment(LLVMValueRef val
, uint64_t bytes
)
56 #if LLVM_VERSION_MAJOR >= 10
57 llvm::Argument
*A
= llvm::unwrap
<llvm::Argument
>(val
);
58 A
->addAttr(llvm::Attribute::getWithAlignment(A
->getContext(), llvm::Align(bytes
)));
60 /* Avoid unused parameter warnings. */
66 bool ac_is_sgpr_param(LLVMValueRef arg
)
68 llvm::Argument
*A
= llvm::unwrap
<llvm::Argument
>(arg
);
69 llvm::AttributeList AS
= A
->getParent()->getAttributes();
70 unsigned ArgNo
= A
->getArgNo();
71 return AS
.hasAttribute(ArgNo
+ 1, llvm::Attribute::InReg
);
74 LLVMValueRef
ac_llvm_get_called_value(LLVMValueRef call
)
76 return LLVMGetCalledValue(call
);
79 bool ac_llvm_is_function(LLVMValueRef v
)
81 return LLVMGetValueKind(v
) == LLVMFunctionValueKind
;
84 LLVMModuleRef
ac_create_module(LLVMTargetMachineRef tm
, LLVMContextRef ctx
)
86 llvm::TargetMachine
*TM
= reinterpret_cast<llvm::TargetMachine
*>(tm
);
87 LLVMModuleRef module
= LLVMModuleCreateWithNameInContext("mesa-shader", ctx
);
89 llvm::unwrap(module
)->setTargetTriple(TM
->getTargetTriple().getTriple());
90 llvm::unwrap(module
)->setDataLayout(TM
->createDataLayout());
94 LLVMBuilderRef
ac_create_builder(LLVMContextRef ctx
,
95 enum ac_float_mode float_mode
)
97 LLVMBuilderRef builder
= LLVMCreateBuilderInContext(ctx
);
99 llvm::FastMathFlags flags
;
101 switch (float_mode
) {
102 case AC_FLOAT_MODE_DEFAULT
:
103 case AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
:
106 case AC_FLOAT_MODE_DEFAULT_OPENGL
:
107 /* Allow optimizations to treat the sign of a zero argument or
108 * result as insignificant.
110 flags
.setNoSignedZeros(); /* nsz */
112 /* Allow optimizations to use the reciprocal of an argument
113 * rather than perform division.
115 flags
.setAllowReciprocal(); /* arcp */
117 llvm::unwrap(builder
)->setFastMathFlags(flags
);
124 LLVMTargetLibraryInfoRef
125 ac_create_target_library_info(const char *triple
)
127 return reinterpret_cast<LLVMTargetLibraryInfoRef
>(new llvm::TargetLibraryInfoImpl(llvm::Triple(triple
)));
131 ac_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info
)
133 delete reinterpret_cast<llvm::TargetLibraryInfoImpl
*>(library_info
);
136 /* Implementation of raw_pwrite_stream that works on malloc()ed memory for
137 * better compatibility with C code. */
138 struct raw_memory_ostream
: public llvm::raw_pwrite_stream
{
151 ~raw_memory_ostream()
161 void take(char *&out_buffer
, size_t &out_size
)
170 void flush() = delete;
172 void write_impl(const char *ptr
, size_t size
) override
174 if (unlikely(written
+ size
< written
))
176 if (written
+ size
> bufsize
) {
177 bufsize
= MAX3(1024, written
+ size
, bufsize
/ 3 * 4);
178 buffer
= (char *)realloc(buffer
, bufsize
);
180 fprintf(stderr
, "amd: out of memory allocating ELF buffer\n");
184 memcpy(buffer
+ written
, ptr
, size
);
188 void pwrite_impl(const char *ptr
, size_t size
, uint64_t offset
) override
190 assert(offset
== (size_t)offset
&&
191 offset
+ size
>= offset
&& offset
+ size
<= written
);
192 memcpy(buffer
+ offset
, ptr
, size
);
195 uint64_t current_pos() const override
201 /* The LLVM compiler is represented as a pass manager containing passes for
202 * optimizations, instruction selection, and code generation.
204 struct ac_compiler_passes
{
205 raw_memory_ostream ostream
; /* ELF shader binary stream */
206 llvm::legacy::PassManager passmgr
; /* list of passes */
209 struct ac_compiler_passes
*ac_create_llvm_passes(LLVMTargetMachineRef tm
)
211 struct ac_compiler_passes
*p
= new ac_compiler_passes();
215 llvm::TargetMachine
*TM
= reinterpret_cast<llvm::TargetMachine
*>(tm
);
217 if (TM
->addPassesToEmitFile(p
->passmgr
, p
->ostream
,
219 #if LLVM_VERSION_MAJOR >= 10
220 llvm::CGFT_ObjectFile
)) {
222 llvm::TargetMachine::CGFT_ObjectFile
)) {
224 fprintf(stderr
, "amd: TargetMachine can't emit a file of this type!\n");
231 void ac_destroy_llvm_passes(struct ac_compiler_passes
*p
)
236 /* This returns false on failure. */
237 bool ac_compile_module_to_elf(struct ac_compiler_passes
*p
, LLVMModuleRef module
,
238 char **pelf_buffer
, size_t *pelf_size
)
240 p
->passmgr
.run(*llvm::unwrap(module
));
241 p
->ostream
.take(*pelf_buffer
, *pelf_size
);
245 void ac_llvm_add_barrier_noop_pass(LLVMPassManagerRef passmgr
)
247 llvm::unwrap(passmgr
)->add(llvm::createBarrierNoopPass());
250 void ac_enable_global_isel(LLVMTargetMachineRef tm
)
252 reinterpret_cast<llvm::TargetMachine
*>(tm
)->setGlobalISel(true);
255 LLVMValueRef
ac_build_atomic_rmw(struct ac_llvm_context
*ctx
, LLVMAtomicRMWBinOp op
,
256 LLVMValueRef ptr
, LLVMValueRef val
,
257 const char *sync_scope
) {
258 llvm::AtomicRMWInst::BinOp binop
;
260 case LLVMAtomicRMWBinOpXchg
:
261 binop
= llvm::AtomicRMWInst::Xchg
;
263 case LLVMAtomicRMWBinOpAdd
:
264 binop
= llvm::AtomicRMWInst::Add
;
266 case LLVMAtomicRMWBinOpSub
:
267 binop
= llvm::AtomicRMWInst::Sub
;
269 case LLVMAtomicRMWBinOpAnd
:
270 binop
= llvm::AtomicRMWInst::And
;
272 case LLVMAtomicRMWBinOpNand
:
273 binop
= llvm::AtomicRMWInst::Nand
;
275 case LLVMAtomicRMWBinOpOr
:
276 binop
= llvm::AtomicRMWInst::Or
;
278 case LLVMAtomicRMWBinOpXor
:
279 binop
= llvm::AtomicRMWInst::Xor
;
281 case LLVMAtomicRMWBinOpMax
:
282 binop
= llvm::AtomicRMWInst::Max
;
284 case LLVMAtomicRMWBinOpMin
:
285 binop
= llvm::AtomicRMWInst::Min
;
287 case LLVMAtomicRMWBinOpUMax
:
288 binop
= llvm::AtomicRMWInst::UMax
;
290 case LLVMAtomicRMWBinOpUMin
:
291 binop
= llvm::AtomicRMWInst::UMin
;
294 unreachable(!"invalid LLVMAtomicRMWBinOp");
297 unsigned SSID
= llvm::unwrap(ctx
->context
)->getOrInsertSyncScopeID(sync_scope
);
298 return llvm::wrap(llvm::unwrap(ctx
->builder
)->CreateAtomicRMW(
299 binop
, llvm::unwrap(ptr
), llvm::unwrap(val
),
300 llvm::AtomicOrdering::SequentiallyConsistent
, SSID
));
303 LLVMValueRef
ac_build_atomic_cmp_xchg(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
304 LLVMValueRef cmp
, LLVMValueRef val
,
305 const char *sync_scope
) {
306 unsigned SSID
= llvm::unwrap(ctx
->context
)->getOrInsertSyncScopeID(sync_scope
);
307 return llvm::wrap(llvm::unwrap(ctx
->builder
)->CreateAtomicCmpXchg(
308 llvm::unwrap(ptr
), llvm::unwrap(cmp
), llvm::unwrap(val
),
309 llvm::AtomicOrdering::SequentiallyConsistent
,
310 llvm::AtomicOrdering::SequentiallyConsistent
, SSID
));