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
22namespace spvtools {
23namespace opt {
24
25namespace {
26
27const uint32_t kTypePointerTypeIdInIdx = 1;
28
29} // namespace
30
31Pass::Pass() : consumer_(nullptr), context_(nullptr), already_run_(false) {}
32
33Pass::Status Pass::Run(IRContext* ctx) {
34 if (already_run_) {
35 return Status::Failure;
36 }
37 already_run_ = true;
38
39 context_ = ctx;
40 Pass::Status status = Process();
41 context_ = nullptr;
42
43 if (status == Status::SuccessWithChange) {
44 ctx->InvalidateAnalysesExceptFor(GetPreservedAnalyses());
45 }
46 assert((status == Status::Failure || ctx->IsConsistent()) &&
47 "An analysis in the context is out of date.");
48 return status;
49}
50
51uint32_t Pass::GetPointeeTypeId(const Instruction* ptrInst) const {
52 const uint32_t ptrTypeId = ptrInst->type_id();
53 const Instruction* ptrTypeInst = get_def_use_mgr()->GetDef(ptrTypeId);
54 return ptrTypeInst->GetSingleWordInOperand(kTypePointerTypeIdInIdx);
55}
56
57Instruction* Pass::GetBaseType(uint32_t ty_id) {
58 Instruction* ty_inst = get_def_use_mgr()->GetDef(ty_id);
59 if (ty_inst->opcode() == SpvOpTypeMatrix) {
60 uint32_t vty_id = ty_inst->GetSingleWordInOperand(0);
61 ty_inst = get_def_use_mgr()->GetDef(vty_id);
62 }
63 if (ty_inst->opcode() == SpvOpTypeVector) {
64 uint32_t cty_id = ty_inst->GetSingleWordInOperand(0);
65 ty_inst = get_def_use_mgr()->GetDef(cty_id);
66 }
67 return ty_inst;
68}
69
70bool Pass::IsFloat(uint32_t ty_id, uint32_t width) {
71 Instruction* ty_inst = GetBaseType(ty_id);
72 if (ty_inst->opcode() != SpvOpTypeFloat) return false;
73 return ty_inst->GetSingleWordInOperand(0) == width;
74}
75
76uint32_t Pass::GetNullId(uint32_t type_id) {
77 if (IsFloat(type_id, 16)) context()->AddCapability(SpvCapabilityFloat16);
78 analysis::TypeManager* type_mgr = context()->get_type_mgr();
79 analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
80 const analysis::Type* type = type_mgr->GetType(type_id);
81 const analysis::Constant* null_const = const_mgr->GetConstant(type, {});
82 Instruction* null_inst =
83 const_mgr->GetDefiningInstruction(null_const, type_id);
84 return null_inst->result_id();
85}
86
87uint32_t Pass::GenerateCopy(Instruction* object_to_copy, uint32_t new_type_id,
88 Instruction* insertion_position) {
89 analysis::TypeManager* type_mgr = context()->get_type_mgr();
90 analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
91
92 uint32_t original_type_id = object_to_copy->type_id();
93 if (original_type_id == new_type_id) {
94 return object_to_copy->result_id();
95 }
96
97 InstructionBuilder ir_builder(
98 context(), insertion_position,
99 IRContext::kAnalysisInstrToBlockMapping | IRContext::kAnalysisDefUse);
100
101 analysis::Type* original_type = type_mgr->GetType(original_type_id);
102 analysis::Type* new_type = type_mgr->GetType(new_type_id);
103
104 if (const analysis::Array* original_array_type = original_type->AsArray()) {
105 uint32_t original_element_type_id =
106 type_mgr->GetId(original_array_type->element_type());
107
108 analysis::Array* new_array_type = new_type->AsArray();
109 assert(new_array_type != nullptr && "Can't copy an array to a non-array.");
110 uint32_t new_element_type_id =
111 type_mgr->GetId(new_array_type->element_type());
112
113 std::vector<uint32_t> element_ids;
114 const analysis::Constant* length_const =
115 const_mgr->FindDeclaredConstant(original_array_type->LengthId());
116 assert(length_const->AsIntConstant());
117 uint32_t array_length = length_const->AsIntConstant()->GetU32();
118 for (uint32_t i = 0; i < array_length; i++) {
119 Instruction* extract = ir_builder.AddCompositeExtract(
120 original_element_type_id, object_to_copy->result_id(), {i});
121 element_ids.push_back(
122 GenerateCopy(extract, new_element_type_id, insertion_position));
123 }
124
125 return ir_builder.AddCompositeConstruct(new_type_id, element_ids)
126 ->result_id();
127 } else if (const analysis::Struct* original_struct_type =
128 original_type->AsStruct()) {
129 analysis::Struct* new_struct_type = new_type->AsStruct();
130
131 const std::vector<const analysis::Type*>& original_types =
132 original_struct_type->element_types();
133 const std::vector<const analysis::Type*>& new_types =
134 new_struct_type->element_types();
135 std::vector<uint32_t> element_ids;
136 for (uint32_t i = 0; i < original_types.size(); i++) {
137 Instruction* extract = ir_builder.AddCompositeExtract(
138 type_mgr->GetId(original_types[i]), object_to_copy->result_id(), {i});
139 element_ids.push_back(GenerateCopy(extract, type_mgr->GetId(new_types[i]),
140 insertion_position));
141 }
142 return ir_builder.AddCompositeConstruct(new_type_id, element_ids)
143 ->result_id();
144 } else {
145 // If we do not have an aggregate type, then we have a problem. Either we
146 // found multiple instances of the same type, or we are copying to an
147 // incompatible type. Either way the code is illegal.
148 assert(false &&
149 "Don't know how to copy this type. Code is likely illegal.");
150 }
151 return 0;
152}
153
154} // namespace opt
155} // namespace spvtools
156