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