clover/nir: support int64 atomics if the device supports it
[mesa.git] / src / gallium / frontends / clover / nir / invocation.cpp
1 //
2 // Copyright 2019 Karol Herbst
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22
23 #include "invocation.hpp"
24
25 #include <tuple>
26
27 #include "core/device.hpp"
28 #include "core/error.hpp"
29 #include "pipe/p_state.h"
30 #include "util/algorithm.hpp"
31 #include "util/functional.hpp"
32
33 #include <compiler/glsl_types.h>
34 #include <compiler/nir/nir_serialize.h>
35 #include <compiler/spirv/nir_spirv.h>
36 #include <util/u_math.h>
37
38 using namespace clover;
39
40 #ifdef HAVE_CLOVER_SPIRV
41
42 // Refs and unrefs the glsl_type_singleton.
43 static class glsl_type_ref {
44 public:
45 glsl_type_ref() {
46 glsl_type_singleton_init_or_ref();
47 }
48
49 ~glsl_type_ref() {
50 glsl_type_singleton_decref();
51 }
52 } glsl_type_ref;
53
54 static const nir_shader_compiler_options *
55 dev_get_nir_compiler_options(const device &dev)
56 {
57 const void *co = dev.get_compiler_options(PIPE_SHADER_IR_NIR);
58 return static_cast<const nir_shader_compiler_options*>(co);
59 }
60
61 module clover::nir::spirv_to_nir(const module &mod, const device &dev,
62 std::string &r_log)
63 {
64 struct spirv_to_nir_options spirv_options = {};
65 spirv_options.environment = NIR_SPIRV_OPENCL;
66 if (dev.address_bits() == 32u) {
67 spirv_options.shared_addr_format = nir_address_format_32bit_global;
68 spirv_options.global_addr_format = nir_address_format_32bit_global;
69 spirv_options.temp_addr_format = nir_address_format_32bit_global;
70 spirv_options.ubo_addr_format = nir_address_format_32bit_global;
71 } else {
72 spirv_options.shared_addr_format = nir_address_format_64bit_global;
73 spirv_options.global_addr_format = nir_address_format_64bit_global;
74 spirv_options.temp_addr_format = nir_address_format_64bit_global;
75 spirv_options.ubo_addr_format = nir_address_format_32bit_index_offset;
76 }
77 spirv_options.caps.address = true;
78 spirv_options.caps.float64 = true;
79 spirv_options.caps.int8 = true;
80 spirv_options.caps.int16 = true;
81 spirv_options.caps.int64 = true;
82 spirv_options.caps.kernel = true;
83 spirv_options.caps.int64_atomics = dev.has_int64_atomics();
84 spirv_options.constant_as_global = true;
85
86 module m;
87 // We only insert one section.
88 assert(mod.secs.size() == 1);
89 auto &section = mod.secs[0];
90
91 module::resource_id section_id = 0;
92 for (const auto &sym : mod.syms) {
93 assert(sym.section == 0);
94
95 const auto *binary =
96 reinterpret_cast<const pipe_binary_program_header *>(section.data.data());
97 const uint32_t *data = reinterpret_cast<const uint32_t *>(binary->blob);
98 const size_t num_words = binary->num_bytes / 4;
99 const char *name = sym.name.c_str();
100 auto *compiler_options = dev_get_nir_compiler_options(dev);
101
102 nir_shader *nir = spirv_to_nir(data, num_words, nullptr, 0,
103 MESA_SHADER_KERNEL, name,
104 &spirv_options, compiler_options);
105 if (!nir) {
106 r_log += "Translation from SPIR-V to NIR for kernel \"" + sym.name +
107 "\" failed.\n";
108 throw build_error();
109 }
110
111 nir->info.cs.local_size_variable = true;
112 nir_validate_shader(nir, "clover");
113
114 // Calculate input offsets.
115 unsigned offset = 0;
116 nir_foreach_shader_in_variable_safe(var, nir) {
117 offset = align(offset, glsl_get_cl_alignment(var->type));
118 var->data.driver_location = offset;
119 offset += glsl_get_cl_size(var->type);
120 }
121
122 // Inline all functions first.
123 // according to the comment on nir_inline_functions
124 NIR_PASS_V(nir, nir_lower_variable_initializers, nir_var_function_temp);
125 NIR_PASS_V(nir, nir_lower_returns);
126 NIR_PASS_V(nir, nir_inline_functions);
127 NIR_PASS_V(nir, nir_copy_prop);
128 NIR_PASS_V(nir, nir_opt_deref);
129
130 // Pick off the single entrypoint that we want.
131 foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
132 if (!func->is_entrypoint)
133 exec_node_remove(&func->node);
134 }
135 assert(exec_list_length(&nir->functions) == 1);
136
137 nir_validate_shader(nir, "clover after function inlining");
138
139 NIR_PASS_V(nir, nir_lower_variable_initializers,
140 static_cast<nir_variable_mode>(~nir_var_function_temp));
141
142 // copy propagate to prepare for lower_explicit_io
143 NIR_PASS_V(nir, nir_split_var_copies);
144 NIR_PASS_V(nir, nir_opt_copy_prop_vars);
145 NIR_PASS_V(nir, nir_lower_var_copies);
146 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
147 NIR_PASS_V(nir, nir_opt_dce);
148
149 NIR_PASS_V(nir, nir_lower_vars_to_explicit_types, nir_var_mem_shared,
150 glsl_get_cl_type_size_align);
151
152 /* use offsets for shader_in and shared memory */
153 nir_variable_mode modes = (nir_variable_mode)(
154 nir_var_shader_in |
155 nir_var_mem_shared);
156 NIR_PASS_V(nir, nir_lower_explicit_io, modes, nir_address_format_32bit_offset);
157
158 /* use global format for global memory */
159 nir_address_format format = nir->info.cs.ptr_size == 64 ?
160 nir_address_format_64bit_global : nir_address_format_32bit_global;
161 NIR_PASS_V(nir, nir_lower_explicit_io, nir_var_mem_global, format);
162
163 NIR_PASS_V(nir, nir_lower_system_values);
164 if (compiler_options->lower_int64_options)
165 NIR_PASS_V(nir, nir_lower_int64);
166
167 NIR_PASS_V(nir, nir_opt_dce);
168
169 struct blob blob;
170 blob_init(&blob);
171 nir_serialize(&blob, nir, false);
172
173 const pipe_binary_program_header header { uint32_t(blob.size) };
174 module::section text { section_id, module::section::text_executable, header.num_bytes, {} };
175 text.data.insert(text.data.end(), reinterpret_cast<const char *>(&header),
176 reinterpret_cast<const char *>(&header) + sizeof(header));
177 text.data.insert(text.data.end(), blob.data, blob.data + blob.size);
178
179 m.syms.emplace_back(sym.name, section_id, 0, sym.args);
180 m.secs.push_back(text);
181 section_id++;
182 }
183 return m;
184 }
185 #else
186 module clover::nir::spirv_to_nir(const module &mod, const device &dev, std::string &r_log)
187 {
188 r_log += "SPIR-V support in clover is not enabled.\n";
189 throw error(CL_LINKER_NOT_AVAILABLE);
190 }
191 #endif