• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2018 Google LLC.
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 "source/val/validate_scopes.h"
16 
17 #include "source/diagnostic.h"
18 #include "source/spirv_target_env.h"
19 #include "source/val/instruction.h"
20 #include "source/val/validation_state.h"
21 
22 namespace spvtools {
23 namespace val {
24 
IsValidScope(uint32_t scope)25 bool IsValidScope(uint32_t scope) {
26   // Deliberately avoid a default case so we have to update the list when the
27   // scopes list changes.
28   switch (static_cast<SpvScope>(scope)) {
29     case SpvScopeCrossDevice:
30     case SpvScopeDevice:
31     case SpvScopeWorkgroup:
32     case SpvScopeSubgroup:
33     case SpvScopeInvocation:
34     case SpvScopeQueueFamilyKHR:
35     case SpvScopeShaderCallKHR:
36       return true;
37     case SpvScopeMax:
38       break;
39   }
40   return false;
41 }
42 
ValidateScope(ValidationState_t & _,const Instruction * inst,uint32_t scope)43 spv_result_t ValidateScope(ValidationState_t& _, const Instruction* inst,
44                            uint32_t scope) {
45   SpvOp opcode = inst->opcode();
46   bool is_int32 = false, is_const_int32 = false;
47   uint32_t value = 0;
48   std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
49 
50   if (!is_int32) {
51     return _.diag(SPV_ERROR_INVALID_DATA, inst)
52            << spvOpcodeString(opcode) << ": expected scope to be a 32-bit int";
53   }
54 
55   if (!is_const_int32) {
56     if (_.HasCapability(SpvCapabilityShader) &&
57         !_.HasCapability(SpvCapabilityCooperativeMatrixNV)) {
58       return _.diag(SPV_ERROR_INVALID_DATA, inst)
59              << "Scope ids must be OpConstant when Shader capability is "
60              << "present";
61     }
62     if (_.HasCapability(SpvCapabilityShader) &&
63         _.HasCapability(SpvCapabilityCooperativeMatrixNV) &&
64         !spvOpcodeIsConstant(_.GetIdOpcode(scope))) {
65       return _.diag(SPV_ERROR_INVALID_DATA, inst)
66              << "Scope ids must be constant or specialization constant when "
67              << "CooperativeMatrixNV capability is present";
68     }
69   }
70 
71   if (is_const_int32 && !IsValidScope(value)) {
72     return _.diag(SPV_ERROR_INVALID_DATA, inst)
73            << "Invalid scope value:\n " << _.Disassemble(*_.FindDef(scope));
74   }
75 
76   return SPV_SUCCESS;
77 }
78 
ValidateExecutionScope(ValidationState_t & _,const Instruction * inst,uint32_t scope)79 spv_result_t ValidateExecutionScope(ValidationState_t& _,
80                                     const Instruction* inst, uint32_t scope) {
81   SpvOp opcode = inst->opcode();
82   bool is_int32 = false, is_const_int32 = false;
83   uint32_t value = 0;
84   std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
85 
86   if (auto error = ValidateScope(_, inst, scope)) {
87     return error;
88   }
89 
90   if (!is_const_int32) {
91     return SPV_SUCCESS;
92   }
93 
94   // Vulkan specific rules
95   if (spvIsVulkanEnv(_.context()->target_env)) {
96     // Vulkan 1.1 specific rules
97     if (_.context()->target_env != SPV_ENV_VULKAN_1_0) {
98       // Scope for Non Uniform Group Operations must be limited to Subgroup
99       if (spvOpcodeIsNonUniformGroupOperation(opcode) &&
100           value != SpvScopeSubgroup) {
101         return _.diag(SPV_ERROR_INVALID_DATA, inst)
102                << _.VkErrorID(4642) << spvOpcodeString(opcode)
103                << ": in Vulkan environment Execution scope is limited to "
104                << "Subgroup";
105       }
106     }
107 
108     // If OpControlBarrier is used in fragment, vertex, tessellation evaluation,
109     // or geometry stages, the execution Scope must be Subgroup.
110     if (opcode == SpvOpControlBarrier && value != SpvScopeSubgroup) {
111       _.function(inst->function()->id())
112           ->RegisterExecutionModelLimitation([](SpvExecutionModel model,
113                                                 std::string* message) {
114             if (model == SpvExecutionModelFragment ||
115                 model == SpvExecutionModelVertex ||
116                 model == SpvExecutionModelGeometry ||
117                 model == SpvExecutionModelTessellationEvaluation) {
118               if (message) {
119                 *message =
120                     "in Vulkan evironment, OpControlBarrier execution scope "
121                     "must be Subgroup for Fragment, Vertex, Geometry and "
122                     "TessellationEvaluation execution models";
123               }
124               return false;
125             }
126             return true;
127           });
128     }
129 
130     // Vulkan generic rules
131     // Scope for execution must be limited to Workgroup or Subgroup
132     if (value != SpvScopeWorkgroup && value != SpvScopeSubgroup) {
133       return _.diag(SPV_ERROR_INVALID_DATA, inst)
134              << spvOpcodeString(opcode)
135              << ": in Vulkan environment Execution Scope is limited to "
136              << "Workgroup and Subgroup";
137     }
138   }
139 
140   // TODO(atgoo@github.com) Add checks for OpenCL and OpenGL environments.
141 
142   // General SPIRV rules
143   // Scope for execution must be limited to Workgroup or Subgroup for
144   // non-uniform operations
145   if (spvOpcodeIsNonUniformGroupOperation(opcode) &&
146       value != SpvScopeSubgroup && value != SpvScopeWorkgroup) {
147     return _.diag(SPV_ERROR_INVALID_DATA, inst)
148            << spvOpcodeString(opcode)
149            << ": Execution scope is limited to Subgroup or Workgroup";
150   }
151 
152   return SPV_SUCCESS;
153 }
154 
ValidateMemoryScope(ValidationState_t & _,const Instruction * inst,uint32_t scope)155 spv_result_t ValidateMemoryScope(ValidationState_t& _, const Instruction* inst,
156                                  uint32_t scope) {
157   const SpvOp opcode = inst->opcode();
158   bool is_int32 = false, is_const_int32 = false;
159   uint32_t value = 0;
160   std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
161 
162   if (auto error = ValidateScope(_, inst, scope)) {
163     return error;
164   }
165 
166   if (!is_const_int32) {
167     return SPV_SUCCESS;
168   }
169 
170   if (value == SpvScopeQueueFamilyKHR) {
171     if (_.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
172       return SPV_SUCCESS;
173     } else {
174       return _.diag(SPV_ERROR_INVALID_DATA, inst)
175              << spvOpcodeString(opcode)
176              << ": Memory Scope QueueFamilyKHR requires capability "
177              << "VulkanMemoryModelKHR";
178     }
179   }
180 
181   if (value == SpvScopeDevice &&
182       _.HasCapability(SpvCapabilityVulkanMemoryModelKHR) &&
183       !_.HasCapability(SpvCapabilityVulkanMemoryModelDeviceScopeKHR)) {
184     return _.diag(SPV_ERROR_INVALID_DATA, inst)
185            << "Use of device scope with VulkanKHR memory model requires the "
186            << "VulkanMemoryModelDeviceScopeKHR capability";
187   }
188 
189   // Vulkan Specific rules
190   if (spvIsVulkanEnv(_.context()->target_env)) {
191     if (value == SpvScopeCrossDevice) {
192       return _.diag(SPV_ERROR_INVALID_DATA, inst)
193              << _.VkErrorID(4638) << spvOpcodeString(opcode)
194              << ": in Vulkan environment, Memory Scope cannot be CrossDevice";
195     }
196     // Vulkan 1.0 specifc rules
197     if (_.context()->target_env == SPV_ENV_VULKAN_1_0 &&
198         value != SpvScopeDevice && value != SpvScopeWorkgroup &&
199         value != SpvScopeInvocation) {
200       return _.diag(SPV_ERROR_INVALID_DATA, inst)
201              << _.VkErrorID(4638) << spvOpcodeString(opcode)
202              << ": in Vulkan 1.0 environment Memory Scope is limited to "
203              << "Device, Workgroup and Invocation";
204     }
205     // Vulkan 1.1 specifc rules
206     if ((_.context()->target_env == SPV_ENV_VULKAN_1_1 ||
207          _.context()->target_env == SPV_ENV_VULKAN_1_2) &&
208         value != SpvScopeDevice && value != SpvScopeWorkgroup &&
209         value != SpvScopeSubgroup && value != SpvScopeInvocation &&
210         value != SpvScopeShaderCallKHR) {
211       return _.diag(SPV_ERROR_INVALID_DATA, inst)
212              << _.VkErrorID(4638) << spvOpcodeString(opcode)
213              << ": in Vulkan 1.1 and 1.2 environment Memory Scope is limited "
214              << "to Device, Workgroup, Invocation, and ShaderCall";
215     }
216 
217     if (value == SpvScopeShaderCallKHR) {
218       std::string errorVUID = _.VkErrorID(4640);
219       _.function(inst->function()->id())
220           ->RegisterExecutionModelLimitation(
221               [errorVUID](SpvExecutionModel model, std::string* message) {
222                 if (model != SpvExecutionModelRayGenerationKHR &&
223                     model != SpvExecutionModelIntersectionKHR &&
224                     model != SpvExecutionModelAnyHitKHR &&
225                     model != SpvExecutionModelClosestHitKHR &&
226                     model != SpvExecutionModelMissKHR &&
227                     model != SpvExecutionModelCallableKHR) {
228                   if (message) {
229                     *message =
230                         errorVUID +
231                         "ShaderCallKHR Memory Scope requires a ray tracing "
232                         "execution model";
233                   }
234                   return false;
235                 }
236                 return true;
237               });
238     }
239 
240     if (value == SpvScopeWorkgroup) {
241       std::string errorVUID = _.VkErrorID(4639);
242       _.function(inst->function()->id())
243           ->RegisterExecutionModelLimitation(
244               [errorVUID](SpvExecutionModel model, std::string* message) {
245                 if (model != SpvExecutionModelGLCompute &&
246                     model != SpvExecutionModelTaskNV &&
247                     model != SpvExecutionModelMeshNV) {
248                   if (message) {
249                     *message = errorVUID +
250                                "Workgroup Memory Scope is limited to MeshNV, "
251                                "TaskNV, and GLCompute execution model";
252                   }
253                   return false;
254                 }
255                 return true;
256               });
257     }
258   }
259 
260   // TODO(atgoo@github.com) Add checks for OpenCL and OpenGL environments.
261 
262   return SPV_SUCCESS;
263 }
264 
265 }  // namespace val
266 }  // namespace spvtools
267