• 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 
32 }  // namespace
33 
34 namespace descsroautil {
35 
IsDescriptorArray(IRContext * context,Instruction * var)36 bool IsDescriptorArray(IRContext* context, Instruction* var) {
37   if (var->opcode() != spv::Op::OpVariable) {
38     return false;
39   }
40 
41   uint32_t ptr_type_id = var->type_id();
42   Instruction* ptr_type_inst = context->get_def_use_mgr()->GetDef(ptr_type_id);
43   if (ptr_type_inst->opcode() != spv::Op::OpTypePointer) {
44     return false;
45   }
46 
47   uint32_t var_type_id = ptr_type_inst->GetSingleWordInOperand(1);
48   Instruction* var_type_inst = context->get_def_use_mgr()->GetDef(var_type_id);
49   if (var_type_inst->opcode() != spv::Op::OpTypeArray &&
50       var_type_inst->opcode() != spv::Op::OpTypeStruct) {
51     return false;
52   }
53 
54   // All structures with descriptor assignments must be replaced by variables,
55   // one for each of their members - with the exceptions of buffers.
56   if (IsTypeOfStructuredBuffer(context, var_type_inst)) {
57     return false;
58   }
59 
60   if (!context->get_decoration_mgr()->HasDecoration(
61           var->result_id(), uint32_t(spv::Decoration::DescriptorSet))) {
62     return false;
63   }
64 
65   return context->get_decoration_mgr()->HasDecoration(
66       var->result_id(), uint32_t(spv::Decoration::Binding));
67 }
68 
IsTypeOfStructuredBuffer(IRContext * context,const Instruction * type)69 bool IsTypeOfStructuredBuffer(IRContext* context, const Instruction* type) {
70   if (type->opcode() != spv::Op::OpTypeStruct) {
71     return false;
72   }
73 
74   // All buffers have offset decorations for members of their structure types.
75   // This is how we distinguish it from a structure of descriptors.
76   return context->get_decoration_mgr()->HasDecoration(
77       type->result_id(), uint32_t(spv::Decoration::Offset));
78 }
79 
GetAccessChainIndexAsConst(IRContext * context,Instruction * access_chain)80 const analysis::Constant* GetAccessChainIndexAsConst(
81     IRContext* context, Instruction* access_chain) {
82   if (access_chain->NumInOperands() <= 1) {
83     return nullptr;
84   }
85   uint32_t idx_id = GetFirstIndexOfAccessChain(access_chain);
86   const analysis::Constant* idx_const =
87       context->get_constant_mgr()->FindDeclaredConstant(idx_id);
88   return idx_const;
89 }
90 
GetFirstIndexOfAccessChain(Instruction * access_chain)91 uint32_t GetFirstIndexOfAccessChain(Instruction* access_chain) {
92   assert(access_chain->NumInOperands() > 1 &&
93          "OpAccessChain does not have Indexes operand");
94   return access_chain->GetSingleWordInOperand(kOpAccessChainInOperandIndexes);
95 }
96 
GetNumberOfElementsForArrayOrStruct(IRContext * context,Instruction * var)97 uint32_t GetNumberOfElementsForArrayOrStruct(IRContext* context,
98                                              Instruction* var) {
99   uint32_t ptr_type_id = var->type_id();
100   Instruction* ptr_type_inst = context->get_def_use_mgr()->GetDef(ptr_type_id);
101   assert(ptr_type_inst->opcode() == spv::Op::OpTypePointer &&
102          "Variable should be a pointer to an array or structure.");
103   uint32_t pointee_type_id = ptr_type_inst->GetSingleWordInOperand(1);
104   Instruction* pointee_type_inst =
105       context->get_def_use_mgr()->GetDef(pointee_type_id);
106   if (pointee_type_inst->opcode() == spv::Op::OpTypeArray) {
107     return GetLengthOfArrayType(context, pointee_type_inst);
108   }
109   assert(pointee_type_inst->opcode() == spv::Op::OpTypeStruct &&
110          "Variable should be a pointer to an array or structure.");
111   return pointee_type_inst->NumInOperands();
112 }
113 
114 }  // namespace descsroautil
115 }  // namespace opt
116 }  // namespace spvtools
117