1// Copyright (c) 2019 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/decompose_initialized_variables_pass.h"
16
17#include "source/opt/ir_context.h"
18
19namespace spvtools {
20namespace opt {
21
22using inst_iterator = InstructionList::iterator;
23
24namespace {
25
26bool HasInitializer(Instruction* inst) {
27 if (inst->opcode() != SpvOpVariable) return false;
28 if (inst->NumOperands() < 4) return false;
29
30 return true;
31}
32
33} // namespace
34
35Pass::Status DecomposeInitializedVariablesPass::Process() {
36 auto* module = context()->module();
37 std::unordered_set<Instruction*> changed;
38
39 std::vector<std::tuple<uint32_t, uint32_t>> global_stores;
40 for (auto iter = module->types_values_begin();
41 iter != module->types_values_end(); ++iter) {
42 Instruction* inst = &(*iter);
43 if (!HasInitializer(inst)) continue;
44
45 auto var_id = inst->result_id();
46 auto val_id = inst->GetOperand(3).words[0];
47 global_stores.push_back(std::make_tuple(var_id, val_id));
48 iter->RemoveOperand(3);
49 changed.insert(&*iter);
50 }
51
52 std::unordered_set<uint32_t> entry_ids;
53 for (auto entry = module->entry_points().begin();
54 entry != module->entry_points().end(); ++entry) {
55 entry_ids.insert(entry->GetSingleWordInOperand(1));
56 }
57
58 for (auto func = module->begin(); func != module->end(); ++func) {
59 std::vector<Instruction*> function_stores;
60 auto first_block = func->entry().get();
61 inst_iterator insert_point = first_block->begin();
62 for (auto iter = first_block->begin();
63 iter != first_block->end() && iter->opcode() == SpvOpVariable;
64 ++iter) {
65 // For valid SPIRV-V, there is guaranteed to be at least one instruction
66 // after the OpVariable instructions.
67 insert_point = (*iter).NextNode();
68 Instruction* inst = &(*iter);
69 if (!HasInitializer(inst)) continue;
70
71 auto var_id = inst->result_id();
72 auto val_id = inst->GetOperand(3).words[0];
73 Instruction* store_inst = new Instruction(
74 context(), SpvOpStore, 0, 0,
75 {{SPV_OPERAND_TYPE_ID, {var_id}}, {SPV_OPERAND_TYPE_ID, {val_id}}});
76 function_stores.push_back(store_inst);
77 iter->RemoveOperand(3);
78 changed.insert(&*iter);
79 }
80
81 if (entry_ids.find(func->result_id()) != entry_ids.end()) {
82 for (auto store_ids : global_stores) {
83 uint32_t var_id;
84 uint32_t val_id;
85 std::tie(var_id, val_id) = store_ids;
86 auto* store_inst = new Instruction(
87 context(), SpvOpStore, 0, 0,
88 {{SPV_OPERAND_TYPE_ID, {var_id}}, {SPV_OPERAND_TYPE_ID, {val_id}}});
89 context()->set_instr_block(store_inst, &*first_block);
90 first_block->AddInstruction(std::unique_ptr<Instruction>(store_inst));
91 store_inst->InsertBefore(&*insert_point);
92 changed.insert(store_inst);
93 }
94 }
95
96 for (auto store = function_stores.begin(); store != function_stores.end();
97 ++store) {
98 context()->set_instr_block(*store, first_block);
99 (*store)->InsertBefore(&*insert_point);
100 changed.insert(*store);
101 }
102 }
103
104 auto* def_use_mgr = get_def_use_mgr();
105 for (auto* inst : changed) def_use_mgr->UpdateDefUse(inst);
106
107 return !changed.empty() ? Pass::Status::SuccessWithChange
108 : Pass::Status::SuccessWithoutChange;
109}
110
111} // namespace opt
112} // namespace spvtools
113