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::TypeManager* type_mgr = context()->get_type_mgr();
87 analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
88
89 uint32_t original_type_id = object_to_copy->type_id();
90 if (original_type_id == new_type_id) {
91 return object_to_copy->result_id();
92 }
93
94 InstructionBuilder ir_builder(
95 context(), insertion_position,
96 IRContext::kAnalysisInstrToBlockMapping | IRContext::kAnalysisDefUse);
97
98 analysis::Type* original_type = type_mgr->GetType(original_type_id);
99 analysis::Type* new_type = type_mgr->GetType(new_type_id);
100
101 if (const analysis::Array* original_array_type = original_type->AsArray()) {
102 uint32_t original_element_type_id =
103 type_mgr->GetId(original_array_type->element_type());
104
105 analysis::Array* new_array_type = new_type->AsArray();
106 assert(new_array_type != nullptr && "Can't copy an array to a non-array.");
107 uint32_t new_element_type_id =
108 type_mgr->GetId(new_array_type->element_type());
109
110 std::vector<uint32_t> element_ids;
111 const analysis::Constant* length_const =
112 const_mgr->FindDeclaredConstant(original_array_type->LengthId());
113 assert(length_const->AsIntConstant());
114 uint32_t array_length = length_const->AsIntConstant()->GetU32();
115 for (uint32_t i = 0; i < array_length; i++) {
116 Instruction* extract = ir_builder.AddCompositeExtract(
117 original_element_type_id, object_to_copy->result_id(), {i});
118 element_ids.push_back(
119 GenerateCopy(extract, new_element_type_id, insertion_position));
120 }
121
122 return ir_builder.AddCompositeConstruct(new_type_id, element_ids)
123 ->result_id();
124 } else if (const analysis::Struct* original_struct_type =
125 original_type->AsStruct()) {
126 analysis::Struct* new_struct_type = new_type->AsStruct();
127
128 const std::vector<const analysis::Type*>& original_types =
129 original_struct_type->element_types();
130 const std::vector<const analysis::Type*>& new_types =
131 new_struct_type->element_types();
132 std::vector<uint32_t> element_ids;
133 for (uint32_t i = 0; i < original_types.size(); i++) {
134 Instruction* extract = ir_builder.AddCompositeExtract(
135 type_mgr->GetId(original_types[i]), object_to_copy->result_id(), {i});
136 element_ids.push_back(GenerateCopy(extract, type_mgr->GetId(new_types[i]),
137 insertion_position));
138 }
139 return ir_builder.AddCompositeConstruct(new_type_id, element_ids)
140 ->result_id();
141 } else {
142 // If we do not have an aggregate type, then we have a problem. Either we
143 // found multiple instances of the same type, or we are copying to an
144 // incompatible type. Either way the code is illegal.
145 assert(false &&
146 "Don't know how to copy this type. Code is likely illegal.");
147 }
148 return 0;
149 }
150
151 } // namespace opt
152 } // namespace spvtools
153