e022e12c7f320a5d883c6cce532ac8352f297392
[mesa.git] / src / amd / common / ac_llvm_helper.cpp
1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
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:
11 *
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.
19 *
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
22 * of the Software.
23 *
24 */
25
26 /* based on Marek's patch to lp_bld_misc.cpp */
27
28 // Workaround http://llvm.org/PR23628
29 #pragma push_macro("DEBUG")
30 #undef DEBUG
31
32 #include "ac_binary.h"
33 #include "ac_llvm_util.h"
34
35 #include <llvm-c/Core.h>
36 #include <llvm/Target/TargetMachine.h>
37 #include <llvm/IR/IRBuilder.h>
38 #include <llvm/Analysis/TargetLibraryInfo.h>
39 #include <llvm/Transforms/IPO.h>
40
41 #include <llvm/IR/LegacyPassManager.h>
42 #if HAVE_LLVM < 0x0700
43 #include "llvm/Support/raw_ostream.h"
44 #endif
45
46 void ac_add_attr_dereferenceable(LLVMValueRef val, uint64_t bytes)
47 {
48 llvm::Argument *A = llvm::unwrap<llvm::Argument>(val);
49 A->addAttr(llvm::Attribute::getWithDereferenceableBytes(A->getContext(), bytes));
50 }
51
52 bool ac_is_sgpr_param(LLVMValueRef arg)
53 {
54 llvm::Argument *A = llvm::unwrap<llvm::Argument>(arg);
55 llvm::AttributeList AS = A->getParent()->getAttributes();
56 unsigned ArgNo = A->getArgNo();
57 return AS.hasAttribute(ArgNo + 1, llvm::Attribute::InReg);
58 }
59
60 LLVMValueRef ac_llvm_get_called_value(LLVMValueRef call)
61 {
62 return LLVMGetCalledValue(call);
63 }
64
65 bool ac_llvm_is_function(LLVMValueRef v)
66 {
67 return LLVMGetValueKind(v) == LLVMFunctionValueKind;
68 }
69
70 LLVMModuleRef ac_create_module(LLVMTargetMachineRef tm, LLVMContextRef ctx)
71 {
72 llvm::TargetMachine *TM = reinterpret_cast<llvm::TargetMachine*>(tm);
73 LLVMModuleRef module = LLVMModuleCreateWithNameInContext("mesa-shader", ctx);
74
75 llvm::unwrap(module)->setTargetTriple(TM->getTargetTriple().getTriple());
76 llvm::unwrap(module)->setDataLayout(TM->createDataLayout());
77 return module;
78 }
79
80 LLVMBuilderRef ac_create_builder(LLVMContextRef ctx,
81 enum ac_float_mode float_mode)
82 {
83 LLVMBuilderRef builder = LLVMCreateBuilderInContext(ctx);
84
85 llvm::FastMathFlags flags;
86
87 switch (float_mode) {
88 case AC_FLOAT_MODE_DEFAULT:
89 break;
90 case AC_FLOAT_MODE_NO_SIGNED_ZEROS_FP_MATH:
91 flags.setNoSignedZeros();
92 llvm::unwrap(builder)->setFastMathFlags(flags);
93 break;
94 case AC_FLOAT_MODE_UNSAFE_FP_MATH:
95 flags.setFast();
96 llvm::unwrap(builder)->setFastMathFlags(flags);
97 break;
98 }
99
100 return builder;
101 }
102
103 LLVMTargetLibraryInfoRef
104 ac_create_target_library_info(const char *triple)
105 {
106 return reinterpret_cast<LLVMTargetLibraryInfoRef>(new llvm::TargetLibraryInfoImpl(llvm::Triple(triple)));
107 }
108
109 void
110 ac_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info)
111 {
112 delete reinterpret_cast<llvm::TargetLibraryInfoImpl *>(library_info);
113 }
114
115 /* The LLVM compiler is represented as a pass manager containing passes for
116 * optimizations, instruction selection, and code generation.
117 */
118 struct ac_compiler_passes {
119 ac_compiler_passes(): ostream(code_string) {}
120
121 llvm::SmallString<0> code_string; /* ELF shader binary */
122 llvm::raw_svector_ostream ostream; /* stream for appending data to the binary */
123 llvm::legacy::PassManager passmgr; /* list of passes */
124 };
125
126 struct ac_compiler_passes *ac_create_llvm_passes(LLVMTargetMachineRef tm)
127 {
128 struct ac_compiler_passes *p = new ac_compiler_passes();
129 if (!p)
130 return NULL;
131
132 llvm::TargetMachine *TM = reinterpret_cast<llvm::TargetMachine*>(tm);
133
134 if (TM->addPassesToEmitFile(p->passmgr, p->ostream,
135 #if HAVE_LLVM >= 0x0700
136 nullptr,
137 #endif
138 llvm::TargetMachine::CGFT_ObjectFile)) {
139 fprintf(stderr, "amd: TargetMachine can't emit a file of this type!\n");
140 delete p;
141 return NULL;
142 }
143 return p;
144 }
145
146 void ac_destroy_llvm_passes(struct ac_compiler_passes *p)
147 {
148 delete p;
149 }
150
151 /* This returns false on failure. */
152 bool ac_compile_module_to_binary(struct ac_compiler_passes *p, LLVMModuleRef module,
153 struct ac_shader_binary *binary)
154 {
155 p->passmgr.run(*llvm::unwrap(module));
156
157 llvm::StringRef data = p->ostream.str();
158 bool success = ac_elf_read(data.data(), data.size(), binary);
159 p->code_string = ""; /* release the ELF shader binary */
160
161 if (!success)
162 fprintf(stderr, "amd: cannot read an ELF shader binary\n");
163 return success;
164 }
165
166 void ac_llvm_add_barrier_noop_pass(LLVMPassManagerRef passmgr)
167 {
168 llvm::unwrap(passmgr)->add(llvm::createBarrierNoopPass());
169 }
170
171 void ac_enable_global_isel(LLVMTargetMachineRef tm)
172 {
173 #if HAVE_LLVM >= 0x0700
174 reinterpret_cast<llvm::TargetMachine*>(tm)->setGlobalISel(true);
175 #endif
176 }