1 // Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "ComputeProgram.hpp"
16
17 #include "Constants.hpp"
18 #include "System/Debug.hpp"
19 #include "Vulkan/VkDevice.hpp"
20 #include "Vulkan/VkPipelineLayout.hpp"
21
22 #include "marl/defer.h"
23 #include "marl/trace.h"
24 #include "marl/waitgroup.h"
25
26 #include <queue>
27
28 namespace {
29
30 enum
31 {
32 X,
33 Y,
34 Z
35 };
36
37 } // anonymous namespace
38
39 namespace sw {
40
ComputeProgram(vk::Device * device,std::shared_ptr<SpirvShader> shader,vk::PipelineLayout const * pipelineLayout,const vk::DescriptorSet::Bindings & descriptorSets)41 ComputeProgram::ComputeProgram(vk::Device *device, std::shared_ptr<SpirvShader> shader, vk::PipelineLayout const *pipelineLayout, const vk::DescriptorSet::Bindings &descriptorSets)
42 : device(device)
43 , shader(shader)
44 , pipelineLayout(pipelineLayout)
45 , descriptorSets(descriptorSets)
46 {
47 }
48
~ComputeProgram()49 ComputeProgram::~ComputeProgram()
50 {
51 }
52
generate()53 void ComputeProgram::generate()
54 {
55 MARL_SCOPED_EVENT("ComputeProgram::generate");
56
57 SpirvRoutine routine(pipelineLayout);
58 shader->emitProlog(&routine);
59 emit(&routine);
60 shader->emitEpilog(&routine);
61 shader->clearPhis(&routine);
62 }
63
setWorkgroupBuiltins(Pointer<Byte> data,SpirvRoutine * routine,Int workgroupID[3])64 void ComputeProgram::setWorkgroupBuiltins(Pointer<Byte> data, SpirvRoutine *routine, Int workgroupID[3])
65 {
66 // TODO(b/146486064): Consider only assigning these to the SpirvRoutine iff
67 // they are ever going to be read.
68 routine->numWorkgroups = *Pointer<Int4>(data + OFFSET(Data, numWorkgroups));
69 routine->workgroupID = Insert(Insert(Insert(Int4(0), workgroupID[X], X), workgroupID[Y], Y), workgroupID[Z], Z);
70 routine->workgroupSize = *Pointer<Int4>(data + OFFSET(Data, workgroupSize));
71 routine->subgroupsPerWorkgroup = *Pointer<Int>(data + OFFSET(Data, subgroupsPerWorkgroup));
72 routine->invocationsPerSubgroup = *Pointer<Int>(data + OFFSET(Data, invocationsPerSubgroup));
73
74 routine->setInputBuiltin(shader.get(), spv::BuiltInNumWorkgroups, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
75 for(uint32_t component = 0; component < builtin.SizeInComponents; component++)
76 {
77 value[builtin.FirstComponent + component] =
78 As<SIMD::Float>(SIMD::Int(Extract(routine->numWorkgroups, component)));
79 }
80 });
81
82 routine->setInputBuiltin(shader.get(), spv::BuiltInWorkgroupId, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
83 for(uint32_t component = 0; component < builtin.SizeInComponents; component++)
84 {
85 value[builtin.FirstComponent + component] =
86 As<SIMD::Float>(SIMD::Int(workgroupID[component]));
87 }
88 });
89
90 routine->setInputBuiltin(shader.get(), spv::BuiltInWorkgroupSize, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
91 for(uint32_t component = 0; component < builtin.SizeInComponents; component++)
92 {
93 value[builtin.FirstComponent + component] =
94 As<SIMD::Float>(SIMD::Int(Extract(routine->workgroupSize, component)));
95 }
96 });
97
98 routine->setInputBuiltin(shader.get(), spv::BuiltInNumSubgroups, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
99 ASSERT(builtin.SizeInComponents == 1);
100 value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(routine->subgroupsPerWorkgroup));
101 });
102
103 routine->setInputBuiltin(shader.get(), spv::BuiltInSubgroupSize, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
104 ASSERT(builtin.SizeInComponents == 1);
105 value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(routine->invocationsPerSubgroup));
106 });
107
108 routine->setImmutableInputBuiltins(shader.get());
109 }
110
setSubgroupBuiltins(Pointer<Byte> data,SpirvRoutine * routine,Int workgroupID[3],SIMD::Int localInvocationIndex,Int subgroupIndex)111 void ComputeProgram::setSubgroupBuiltins(Pointer<Byte> data, SpirvRoutine *routine, Int workgroupID[3], SIMD::Int localInvocationIndex, Int subgroupIndex)
112 {
113 Int4 numWorkgroups = *Pointer<Int4>(data + OFFSET(Data, numWorkgroups));
114 Int4 workgroupSize = *Pointer<Int4>(data + OFFSET(Data, workgroupSize));
115
116 // TODO: Fix Int4 swizzles so we can just use workgroupSize.x, workgroupSize.y.
117 Int workgroupSizeX = Extract(workgroupSize, X);
118 Int workgroupSizeY = Extract(workgroupSize, Y);
119
120 SIMD::Int localInvocationID[3];
121 {
122 SIMD::Int idx = localInvocationIndex;
123 localInvocationID[Z] = idx / SIMD::Int(workgroupSizeX * workgroupSizeY);
124 idx -= localInvocationID[Z] * SIMD::Int(workgroupSizeX * workgroupSizeY); // modulo
125 localInvocationID[Y] = idx / SIMD::Int(workgroupSizeX);
126 idx -= localInvocationID[Y] * SIMD::Int(workgroupSizeX); // modulo
127 localInvocationID[X] = idx;
128 }
129
130 Int4 wgID = Insert(Insert(Insert(SIMD::Int(0), workgroupID[X], X), workgroupID[Y], Y), workgroupID[Z], Z);
131 auto localBase = workgroupSize * wgID;
132 SIMD::Int globalInvocationID[3];
133 globalInvocationID[X] = SIMD::Int(Extract(localBase, X)) + localInvocationID[X];
134 globalInvocationID[Y] = SIMD::Int(Extract(localBase, Y)) + localInvocationID[Y];
135 globalInvocationID[Z] = SIMD::Int(Extract(localBase, Z)) + localInvocationID[Z];
136
137 routine->localInvocationIndex = localInvocationIndex;
138 routine->subgroupIndex = subgroupIndex;
139 routine->localInvocationID[X] = localInvocationID[X];
140 routine->localInvocationID[Y] = localInvocationID[Y];
141 routine->localInvocationID[Z] = localInvocationID[Z];
142 routine->globalInvocationID[X] = globalInvocationID[X];
143 routine->globalInvocationID[Y] = globalInvocationID[Y];
144 routine->globalInvocationID[Z] = globalInvocationID[Z];
145
146 routine->setInputBuiltin(shader.get(), spv::BuiltInLocalInvocationIndex, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
147 ASSERT(builtin.SizeInComponents == 1);
148 value[builtin.FirstComponent] = As<SIMD::Float>(localInvocationIndex);
149 });
150
151 routine->setInputBuiltin(shader.get(), spv::BuiltInSubgroupId, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
152 ASSERT(builtin.SizeInComponents == 1);
153 value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(subgroupIndex));
154 });
155
156 routine->setInputBuiltin(shader.get(), spv::BuiltInLocalInvocationId, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
157 for(uint32_t component = 0; component < builtin.SizeInComponents; component++)
158 {
159 value[builtin.FirstComponent + component] =
160 As<SIMD::Float>(localInvocationID[component]);
161 }
162 });
163
164 routine->setInputBuiltin(shader.get(), spv::BuiltInGlobalInvocationId, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
165 for(uint32_t component = 0; component < builtin.SizeInComponents; component++)
166 {
167 value[builtin.FirstComponent + component] =
168 As<SIMD::Float>(globalInvocationID[component]);
169 }
170 });
171 }
172
emit(SpirvRoutine * routine)173 void ComputeProgram::emit(SpirvRoutine *routine)
174 {
175 Pointer<Byte> device = Arg<0>();
176 Pointer<Byte> data = Arg<1>();
177 Int workgroupX = Arg<2>();
178 Int workgroupY = Arg<3>();
179 Int workgroupZ = Arg<4>();
180 Pointer<Byte> workgroupMemory = Arg<5>();
181 Int firstSubgroup = Arg<6>();
182 Int subgroupCount = Arg<7>();
183
184 routine->device = device;
185 routine->descriptorSets = data + OFFSET(Data, descriptorSets);
186 routine->descriptorDynamicOffsets = data + OFFSET(Data, descriptorDynamicOffsets);
187 routine->pushConstants = data + OFFSET(Data, pushConstants);
188 routine->constants = device + OFFSET(vk::Device, constants);
189 routine->workgroupMemory = workgroupMemory;
190
191 Int invocationsPerWorkgroup = *Pointer<Int>(data + OFFSET(Data, invocationsPerWorkgroup));
192
193 Int workgroupID[3] = { workgroupX, workgroupY, workgroupZ };
194 setWorkgroupBuiltins(data, routine, workgroupID);
195
196 For(Int i = 0, i < subgroupCount, i++)
197 {
198 auto subgroupIndex = firstSubgroup + i;
199
200 // TODO: Replace SIMD::Int(0, 1, 2, 3) with SIMD-width equivalent
201 auto localInvocationIndex = SIMD::Int(subgroupIndex * SIMD::Width) + SIMD::Int(0, 1, 2, 3);
202
203 // Disable lanes where (invocationIDs >= invocationsPerWorkgroup)
204 auto activeLaneMask = CmpLT(localInvocationIndex, SIMD::Int(invocationsPerWorkgroup));
205
206 setSubgroupBuiltins(data, routine, workgroupID, localInvocationIndex, subgroupIndex);
207
208 shader->emit(routine, activeLaneMask, activeLaneMask, descriptorSets);
209 }
210 }
211
run(vk::DescriptorSet::Array const & descriptorSetObjects,vk::DescriptorSet::Bindings const & descriptorSets,vk::DescriptorSet::DynamicOffsets const & descriptorDynamicOffsets,vk::Pipeline::PushConstantStorage const & pushConstants,uint32_t baseGroupX,uint32_t baseGroupY,uint32_t baseGroupZ,uint32_t groupCountX,uint32_t groupCountY,uint32_t groupCountZ)212 void ComputeProgram::run(
213 vk::DescriptorSet::Array const &descriptorSetObjects,
214 vk::DescriptorSet::Bindings const &descriptorSets,
215 vk::DescriptorSet::DynamicOffsets const &descriptorDynamicOffsets,
216 vk::Pipeline::PushConstantStorage const &pushConstants,
217 uint32_t baseGroupX, uint32_t baseGroupY, uint32_t baseGroupZ,
218 uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ)
219 {
220 auto &executionModes = shader->getExecutionModes();
221
222 auto invocationsPerSubgroup = SIMD::Width;
223 auto invocationsPerWorkgroup = executionModes.WorkgroupSizeX * executionModes.WorkgroupSizeY * executionModes.WorkgroupSizeZ;
224 auto subgroupsPerWorkgroup = (invocationsPerWorkgroup + invocationsPerSubgroup - 1) / invocationsPerSubgroup;
225
226 Data data;
227 data.descriptorSets = descriptorSets;
228 data.descriptorDynamicOffsets = descriptorDynamicOffsets;
229 data.numWorkgroups[X] = groupCountX;
230 data.numWorkgroups[Y] = groupCountY;
231 data.numWorkgroups[Z] = groupCountZ;
232 data.numWorkgroups[3] = 0;
233 data.workgroupSize[X] = executionModes.WorkgroupSizeX;
234 data.workgroupSize[Y] = executionModes.WorkgroupSizeY;
235 data.workgroupSize[Z] = executionModes.WorkgroupSizeZ;
236 data.workgroupSize[3] = 0;
237 data.invocationsPerSubgroup = invocationsPerSubgroup;
238 data.invocationsPerWorkgroup = invocationsPerWorkgroup;
239 data.subgroupsPerWorkgroup = subgroupsPerWorkgroup;
240 data.pushConstants = pushConstants;
241
242 marl::WaitGroup wg;
243 const uint32_t batchCount = 16;
244
245 auto groupCount = groupCountX * groupCountY * groupCountZ;
246
247 for(uint32_t batchID = 0; batchID < batchCount && batchID < groupCount; batchID++)
248 {
249 wg.add(1);
250 marl::schedule([=, &data] {
251 defer(wg.done());
252 std::vector<uint8_t> workgroupMemory(shader->workgroupMemory.size());
253
254 for(uint32_t groupIndex = batchID; groupIndex < groupCount; groupIndex += batchCount)
255 {
256 auto modulo = groupIndex;
257 auto groupOffsetZ = modulo / (groupCountX * groupCountY);
258 modulo -= groupOffsetZ * (groupCountX * groupCountY);
259 auto groupOffsetY = modulo / groupCountX;
260 modulo -= groupOffsetY * groupCountX;
261 auto groupOffsetX = modulo;
262
263 auto groupZ = baseGroupZ + groupOffsetZ;
264 auto groupY = baseGroupY + groupOffsetY;
265 auto groupX = baseGroupX + groupOffsetX;
266 MARL_SCOPED_EVENT("groupX: %d, groupY: %d, groupZ: %d", groupX, groupY, groupZ);
267
268 using Coroutine = std::unique_ptr<rr::Stream<SpirvShader::YieldResult>>;
269 std::queue<Coroutine> coroutines;
270
271 if(shader->getAnalysis().ContainsControlBarriers)
272 {
273 // Make a function call per subgroup so each subgroup
274 // can yield, bringing all subgroups to the barrier
275 // together.
276 for(int subgroupIndex = 0; subgroupIndex < subgroupsPerWorkgroup; subgroupIndex++)
277 {
278 auto coroutine = (*this)(device, &data, groupX, groupY, groupZ, workgroupMemory.data(), subgroupIndex, 1);
279 coroutines.push(std::move(coroutine));
280 }
281 }
282 else
283 {
284 auto coroutine = (*this)(device, &data, groupX, groupY, groupZ, workgroupMemory.data(), 0, subgroupsPerWorkgroup);
285 coroutines.push(std::move(coroutine));
286 }
287
288 while(coroutines.size() > 0)
289 {
290 auto coroutine = std::move(coroutines.front());
291 coroutines.pop();
292
293 SpirvShader::YieldResult result;
294 if(coroutine->await(result))
295 {
296 // TODO: Consider result (when the enum is more than 1 entry).
297 coroutines.push(std::move(coroutine));
298 }
299 }
300 }
301 });
302 }
303
304 wg.wait();
305
306 if(shader->containsImageWrite())
307 {
308 vk::DescriptorSet::ContentsChanged(descriptorSetObjects, pipelineLayout, device);
309 }
310 }
311
312 } // namespace sw
313