• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2021 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/opt/desc_sroa_util.h"
16 
17 namespace spvtools {
18 namespace opt {
19 namespace {
20 constexpr uint32_t kOpAccessChainInOperandIndexes = 1;
21 
22 // Returns the length of array type |type|.
GetLengthOfArrayType(IRContext * context,Instruction * type)23 uint32_t GetLengthOfArrayType(IRContext* context, Instruction* type) {
24   assert(type->opcode() == spv::Op::OpTypeArray && "type must be array");
25   uint32_t length_id = type->GetSingleWordInOperand(1);
26   const analysis::Constant* length_const =
27       context->get_constant_mgr()->FindDeclaredConstant(length_id);
28   assert(length_const != nullptr);
29   return length_const->GetU32();
30 }
31 
HasDescriptorDecorations(IRContext * context,Instruction * var)32 bool HasDescriptorDecorations(IRContext* context, Instruction* var) {
33   const auto& decoration_mgr = context->get_decoration_mgr();
34   return decoration_mgr->HasDecoration(
35              var->result_id(), uint32_t(spv::Decoration::DescriptorSet)) &&
36          decoration_mgr->HasDecoration(var->result_id(),
37                                        uint32_t(spv::Decoration::Binding));
38 }
39 
GetVariableType(IRContext * context,Instruction * var)40 Instruction* GetVariableType(IRContext* context, Instruction* var) {
41   if (var->opcode() != spv::Op::OpVariable) {
42     return nullptr;
43   }
44 
45   uint32_t ptr_type_id = var->type_id();
46   Instruction* ptr_type_inst = context->get_def_use_mgr()->GetDef(ptr_type_id);
47   if (ptr_type_inst->opcode() != spv::Op::OpTypePointer) {
48     return nullptr;
49   }
50 
51   uint32_t var_type_id = ptr_type_inst->GetSingleWordInOperand(1);
52   return context->get_def_use_mgr()->GetDef(var_type_id);
53 }
54 
55 }  // namespace
56 
57 namespace descsroautil {
58 
IsDescriptorArray(IRContext * context,Instruction * var)59 bool IsDescriptorArray(IRContext* context, Instruction* var) {
60   Instruction* var_type_inst = GetVariableType(context, var);
61   if (var_type_inst == nullptr) return false;
62   return var_type_inst->opcode() == spv::Op::OpTypeArray &&
63          HasDescriptorDecorations(context, var);
64 }
65 
IsDescriptorStruct(IRContext * context,Instruction * var)66 bool IsDescriptorStruct(IRContext* context, Instruction* var) {
67   Instruction* var_type_inst = GetVariableType(context, var);
68   if (var_type_inst == nullptr) return false;
69 
70   while (var_type_inst->opcode() == spv::Op::OpTypeArray) {
71     var_type_inst = context->get_def_use_mgr()->GetDef(
72         var_type_inst->GetInOperand(0).AsId());
73   }
74 
75   if (var_type_inst->opcode() != spv::Op::OpTypeStruct) return false;
76 
77   // All structures with descriptor assignments must be replaced by variables,
78   // one for each of their members - with the exceptions of buffers.
79   if (IsTypeOfStructuredBuffer(context, var_type_inst)) {
80     return false;
81   }
82 
83   return HasDescriptorDecorations(context, var);
84 }
85 
IsTypeOfStructuredBuffer(IRContext * context,const Instruction * type)86 bool IsTypeOfStructuredBuffer(IRContext* context, const Instruction* type) {
87   if (type->opcode() != spv::Op::OpTypeStruct) {
88     return false;
89   }
90 
91   // All buffers have offset decorations for members of their structure types.
92   // This is how we distinguish it from a structure of descriptors.
93   return context->get_decoration_mgr()->HasDecoration(
94       type->result_id(), uint32_t(spv::Decoration::Offset));
95 }
96 
GetAccessChainIndexAsConst(IRContext * context,Instruction * access_chain)97 const analysis::Constant* GetAccessChainIndexAsConst(
98     IRContext* context, Instruction* access_chain) {
99   if (access_chain->NumInOperands() <= 1) {
100     return nullptr;
101   }
102   uint32_t idx_id = GetFirstIndexOfAccessChain(access_chain);
103   const analysis::Constant* idx_const =
104       context->get_constant_mgr()->FindDeclaredConstant(idx_id);
105   return idx_const;
106 }
107 
GetFirstIndexOfAccessChain(Instruction * access_chain)108 uint32_t GetFirstIndexOfAccessChain(Instruction* access_chain) {
109   assert(access_chain->NumInOperands() > 1 &&
110          "OpAccessChain does not have Indexes operand");
111   return access_chain->GetSingleWordInOperand(kOpAccessChainInOperandIndexes);
112 }
113 
GetNumberOfElementsForArrayOrStruct(IRContext * context,Instruction * var)114 uint32_t GetNumberOfElementsForArrayOrStruct(IRContext* context,
115                                              Instruction* var) {
116   uint32_t ptr_type_id = var->type_id();
117   Instruction* ptr_type_inst = context->get_def_use_mgr()->GetDef(ptr_type_id);
118   assert(ptr_type_inst->opcode() == spv::Op::OpTypePointer &&
119          "Variable should be a pointer to an array or structure.");
120   uint32_t pointee_type_id = ptr_type_inst->GetSingleWordInOperand(1);
121   Instruction* pointee_type_inst =
122       context->get_def_use_mgr()->GetDef(pointee_type_id);
123   if (pointee_type_inst->opcode() == spv::Op::OpTypeArray) {
124     return GetLengthOfArrayType(context, pointee_type_inst);
125   }
126   assert(pointee_type_inst->opcode() == spv::Op::OpTypeStruct &&
127          "Variable should be a pointer to an array or structure.");
128   return pointee_type_inst->NumInOperands();
129 }
130 
131 }  // namespace descsroautil
132 }  // namespace opt
133 }  // namespace spvtools
134