• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017 The Khronos Group Inc.
2 // Copyright (c) 2017 Valve Corporation
3 // Copyright (c) 2017 LunarG Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #include "source/opt/pass.h"
18 
19 #include "source/opt/ir_builder.h"
20 #include "source/opt/iterator.h"
21 
22 namespace spvtools {
23 namespace opt {
24 namespace {
25 constexpr uint32_t kTypePointerTypeIdInIdx = 1;
26 }  // namespace
27 
Pass()28 Pass::Pass() : consumer_(nullptr), context_(nullptr), already_run_(false) {}
29 
Run(IRContext * ctx)30 Pass::Status Pass::Run(IRContext* ctx) {
31   if (already_run_) {
32     return Status::Failure;
33   }
34   already_run_ = true;
35 
36   context_ = ctx;
37   Pass::Status status = Process();
38   context_ = nullptr;
39 
40   if (status == Status::SuccessWithChange) {
41     ctx->InvalidateAnalysesExceptFor(GetPreservedAnalyses());
42   }
43   if (!(status == Status::Failure || ctx->IsConsistent()))
44     assert(false && "An analysis in the context is out of date.");
45   return status;
46 }
47 
GetPointeeTypeId(const Instruction * ptrInst) const48 uint32_t Pass::GetPointeeTypeId(const Instruction* ptrInst) const {
49   const uint32_t ptrTypeId = ptrInst->type_id();
50   const Instruction* ptrTypeInst = get_def_use_mgr()->GetDef(ptrTypeId);
51   return ptrTypeInst->GetSingleWordInOperand(kTypePointerTypeIdInIdx);
52 }
53 
GetBaseType(uint32_t ty_id)54 Instruction* Pass::GetBaseType(uint32_t ty_id) {
55   Instruction* ty_inst = get_def_use_mgr()->GetDef(ty_id);
56   if (ty_inst->opcode() == spv::Op::OpTypeMatrix) {
57     uint32_t vty_id = ty_inst->GetSingleWordInOperand(0);
58     ty_inst = get_def_use_mgr()->GetDef(vty_id);
59   }
60   if (ty_inst->opcode() == spv::Op::OpTypeVector) {
61     uint32_t cty_id = ty_inst->GetSingleWordInOperand(0);
62     ty_inst = get_def_use_mgr()->GetDef(cty_id);
63   }
64   return ty_inst;
65 }
66 
IsFloat(uint32_t ty_id,uint32_t width)67 bool Pass::IsFloat(uint32_t ty_id, uint32_t width) {
68   Instruction* ty_inst = GetBaseType(ty_id);
69   if (ty_inst->opcode() != spv::Op::OpTypeFloat) return false;
70   return ty_inst->GetSingleWordInOperand(0) == width;
71 }
72 
GetNullId(uint32_t type_id)73 uint32_t Pass::GetNullId(uint32_t type_id) {
74   if (IsFloat(type_id, 16)) context()->AddCapability(spv::Capability::Float16);
75   analysis::TypeManager* type_mgr = context()->get_type_mgr();
76   analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
77   const analysis::Type* type = type_mgr->GetType(type_id);
78   const analysis::Constant* null_const = const_mgr->GetConstant(type, {});
79   Instruction* null_inst =
80       const_mgr->GetDefiningInstruction(null_const, type_id);
81   return null_inst->result_id();
82 }
83 
GenerateCopy(Instruction * object_to_copy,uint32_t new_type_id,Instruction * insertion_position)84 uint32_t Pass::GenerateCopy(Instruction* object_to_copy, uint32_t new_type_id,
85                             Instruction* insertion_position) {
86   analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
87 
88   uint32_t original_type_id = object_to_copy->type_id();
89   if (original_type_id == new_type_id) {
90     return object_to_copy->result_id();
91   }
92 
93   InstructionBuilder ir_builder(
94       context(), insertion_position,
95       IRContext::kAnalysisInstrToBlockMapping | IRContext::kAnalysisDefUse);
96 
97   Instruction* original_type = get_def_use_mgr()->GetDef(original_type_id);
98   Instruction* new_type = get_def_use_mgr()->GetDef(new_type_id);
99 
100   if (new_type->opcode() != original_type->opcode()) {
101     return 0;
102   }
103 
104   switch (original_type->opcode()) {
105     case spv::Op::OpTypeArray: {
106       uint32_t original_element_type_id =
107           original_type->GetSingleWordInOperand(0);
108       uint32_t new_element_type_id = new_type->GetSingleWordInOperand(0);
109 
110       std::vector<uint32_t> element_ids;
111       uint32_t length_id = original_type->GetSingleWordInOperand(1);
112       const analysis::Constant* length_const =
113           const_mgr->FindDeclaredConstant(length_id);
114       assert(length_const->AsIntConstant());
115       uint32_t array_length = length_const->AsIntConstant()->GetU32();
116       for (uint32_t i = 0; i < array_length; i++) {
117         Instruction* extract = ir_builder.AddCompositeExtract(
118             original_element_type_id, object_to_copy->result_id(), {i});
119         uint32_t new_id =
120             GenerateCopy(extract, new_element_type_id, insertion_position);
121         if (new_id == 0) {
122           return 0;
123         }
124         element_ids.push_back(new_id);
125       }
126 
127       return ir_builder.AddCompositeConstruct(new_type_id, element_ids)
128           ->result_id();
129     }
130     case spv::Op::OpTypeStruct: {
131       std::vector<uint32_t> element_ids;
132       for (uint32_t i = 0; i < original_type->NumInOperands(); i++) {
133         uint32_t orig_member_type_id = original_type->GetSingleWordInOperand(i);
134         uint32_t new_member_type_id = new_type->GetSingleWordInOperand(i);
135         Instruction* extract = ir_builder.AddCompositeExtract(
136             orig_member_type_id, object_to_copy->result_id(), {i});
137         uint32_t new_id =
138             GenerateCopy(extract, new_member_type_id, insertion_position);
139         if (new_id == 0) {
140           return 0;
141         }
142         element_ids.push_back(new_id);
143       }
144       return ir_builder.AddCompositeConstruct(new_type_id, element_ids)
145           ->result_id();
146     }
147     default:
148       // If we do not have an aggregate type, then we have a problem.  Either we
149       // found multiple instances of the same type, or we are copying to an
150       // incompatible type.  Either way the code is illegal. Leave the code as
151       // is and let the caller deal with it.
152       return 0;
153   }
154 }
155 
156 }  // namespace opt
157 }  // namespace spvtools
158