1 // Copyright (c) 2017 Pierre Moreau
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/decoration_manager.h"
16
17 #include <algorithm>
18 #include <memory>
19 #include <set>
20 #include <stack>
21 #include <utility>
22
23 #include "source/opt/ir_context.h"
24
25 namespace {
26 using InstructionVector = std::vector<const spvtools::opt::Instruction*>;
27 using DecorationSet = std::set<std::u32string>;
28
29 // Returns true if |a| is a subet of |b|.
IsSubset(const DecorationSet & a,const DecorationSet & b)30 bool IsSubset(const DecorationSet& a, const DecorationSet& b) {
31 auto it1 = a.begin();
32 auto it2 = b.begin();
33
34 while (it1 != a.end()) {
35 if (it2 == b.end() || *it1 < *it2) {
36 // |*it1| is in |a|, but not in |b|.
37 return false;
38 }
39 if (*it1 == *it2) {
40 // Found the element move to the next one.
41 it1++;
42 it2++;
43 } else /* *it1 > *it2 */ {
44 // Did not find |*it1| yet, check the next element in |b|.
45 it2++;
46 }
47 }
48 return true;
49 }
50 } // namespace
51
52 namespace spvtools {
53 namespace opt {
54 namespace analysis {
55
RemoveDecorationsFrom(uint32_t id,std::function<bool (const Instruction &)> pred)56 bool DecorationManager::RemoveDecorationsFrom(
57 uint32_t id, std::function<bool(const Instruction&)> pred) {
58 bool was_modified = false;
59 const auto ids_iter = id_to_decoration_insts_.find(id);
60 if (ids_iter == id_to_decoration_insts_.end()) {
61 return was_modified;
62 }
63
64 TargetData& decorations_info = ids_iter->second;
65 auto context = module_->context();
66 std::vector<Instruction*> insts_to_kill;
67 const bool is_group = !decorations_info.decorate_insts.empty();
68
69 // Schedule all direct decorations for removal if instructed as such by
70 // |pred|.
71 for (Instruction* inst : decorations_info.direct_decorations)
72 if (pred(*inst)) insts_to_kill.push_back(inst);
73
74 // For all groups being directly applied to |id|, remove |id| (and the
75 // literal if |inst| is an OpGroupMemberDecorate) from the instruction
76 // applying the group.
77 std::unordered_set<const Instruction*> indirect_decorations_to_remove;
78 for (Instruction* inst : decorations_info.indirect_decorations) {
79 assert(inst->opcode() == SpvOpGroupDecorate ||
80 inst->opcode() == SpvOpGroupMemberDecorate);
81
82 std::vector<Instruction*> group_decorations_to_keep;
83 const uint32_t group_id = inst->GetSingleWordInOperand(0u);
84 const auto group_iter = id_to_decoration_insts_.find(group_id);
85 assert(group_iter != id_to_decoration_insts_.end() &&
86 "Unknown decoration group");
87 const auto& group_decorations = group_iter->second.direct_decorations;
88 for (Instruction* decoration : group_decorations) {
89 if (!pred(*decoration)) group_decorations_to_keep.push_back(decoration);
90 }
91
92 // If all decorations should be kept, then we can keep |id| part of the
93 // group. However, if the group itself has no decorations, we should remove
94 // the id from the group. This is needed to make |KillNameAndDecorate| work
95 // correctly when a decoration group has no decorations.
96 if (group_decorations_to_keep.size() == group_decorations.size() &&
97 group_decorations.size() != 0) {
98 continue;
99 }
100
101 // Otherwise, remove |id| from the targets of |group_id|
102 const uint32_t stride = inst->opcode() == SpvOpGroupDecorate ? 1u : 2u;
103 for (uint32_t i = 1u; i < inst->NumInOperands();) {
104 if (inst->GetSingleWordInOperand(i) != id) {
105 i += stride;
106 continue;
107 }
108
109 const uint32_t last_operand_index = inst->NumInOperands() - stride;
110 if (i < last_operand_index)
111 inst->GetInOperand(i) = inst->GetInOperand(last_operand_index);
112 // Remove the associated literal, if it exists.
113 if (stride == 2u) {
114 if (i < last_operand_index)
115 inst->GetInOperand(i + 1u) =
116 inst->GetInOperand(last_operand_index + 1u);
117 inst->RemoveInOperand(last_operand_index + 1u);
118 }
119 inst->RemoveInOperand(last_operand_index);
120 was_modified = true;
121 }
122
123 // If the instruction has no targets left, remove the instruction
124 // altogether.
125 if (inst->NumInOperands() == 1u) {
126 indirect_decorations_to_remove.emplace(inst);
127 insts_to_kill.push_back(inst);
128 } else if (was_modified) {
129 context->ForgetUses(inst);
130 indirect_decorations_to_remove.emplace(inst);
131 context->AnalyzeUses(inst);
132 }
133
134 // If only some of the decorations should be kept, clone them and apply
135 // them directly to |id|.
136 if (!group_decorations_to_keep.empty()) {
137 for (Instruction* decoration : group_decorations_to_keep) {
138 // simply clone decoration and change |group_id| to |id|
139 std::unique_ptr<Instruction> new_inst(
140 decoration->Clone(module_->context()));
141 new_inst->SetInOperand(0, {id});
142 module_->AddAnnotationInst(std::move(new_inst));
143 auto decoration_iter = --module_->annotation_end();
144 context->AnalyzeUses(&*decoration_iter);
145 }
146 }
147 }
148
149 auto& indirect_decorations = decorations_info.indirect_decorations;
150 indirect_decorations.erase(
151 std::remove_if(
152 indirect_decorations.begin(), indirect_decorations.end(),
153 [&indirect_decorations_to_remove](const Instruction* inst) {
154 return indirect_decorations_to_remove.count(inst);
155 }),
156 indirect_decorations.end());
157
158 was_modified |= !insts_to_kill.empty();
159 for (Instruction* inst : insts_to_kill) context->KillInst(inst);
160 insts_to_kill.clear();
161
162 // Schedule all instructions applying the group for removal if this group no
163 // longer applies decorations, either directly or indirectly.
164 if (is_group && decorations_info.direct_decorations.empty() &&
165 decorations_info.indirect_decorations.empty()) {
166 for (Instruction* inst : decorations_info.decorate_insts)
167 insts_to_kill.push_back(inst);
168 }
169 was_modified |= !insts_to_kill.empty();
170 for (Instruction* inst : insts_to_kill) context->KillInst(inst);
171
172 if (decorations_info.direct_decorations.empty() &&
173 decorations_info.indirect_decorations.empty() &&
174 decorations_info.decorate_insts.empty()) {
175 id_to_decoration_insts_.erase(ids_iter);
176 }
177 return was_modified;
178 }
179
GetDecorationsFor(uint32_t id,bool include_linkage)180 std::vector<Instruction*> DecorationManager::GetDecorationsFor(
181 uint32_t id, bool include_linkage) {
182 return InternalGetDecorationsFor<Instruction*>(id, include_linkage);
183 }
184
GetDecorationsFor(uint32_t id,bool include_linkage) const185 std::vector<const Instruction*> DecorationManager::GetDecorationsFor(
186 uint32_t id, bool include_linkage) const {
187 return const_cast<DecorationManager*>(this)
188 ->InternalGetDecorationsFor<const Instruction*>(id, include_linkage);
189 }
190
HaveTheSameDecorations(uint32_t id1,uint32_t id2) const191 bool DecorationManager::HaveTheSameDecorations(uint32_t id1,
192 uint32_t id2) const {
193 const InstructionVector decorations_for1 = GetDecorationsFor(id1, false);
194 const InstructionVector decorations_for2 = GetDecorationsFor(id2, false);
195
196 // This function splits the decoration instructions into different sets,
197 // based on their opcode; only OpDecorate, OpDecorateId,
198 // OpDecorateStringGOOGLE, and OpMemberDecorate are considered, the other
199 // opcodes are ignored.
200 const auto fillDecorationSets =
201 [](const InstructionVector& decoration_list, DecorationSet* decorate_set,
202 DecorationSet* decorate_id_set, DecorationSet* decorate_string_set,
203 DecorationSet* member_decorate_set) {
204 for (const Instruction* inst : decoration_list) {
205 std::u32string decoration_payload;
206 // Ignore the opcode and the target as we do not want them to be
207 // compared.
208 for (uint32_t i = 1u; i < inst->NumInOperands(); ++i) {
209 for (uint32_t word : inst->GetInOperand(i).words) {
210 decoration_payload.push_back(word);
211 }
212 }
213
214 switch (inst->opcode()) {
215 case SpvOpDecorate:
216 decorate_set->emplace(std::move(decoration_payload));
217 break;
218 case SpvOpMemberDecorate:
219 member_decorate_set->emplace(std::move(decoration_payload));
220 break;
221 case SpvOpDecorateId:
222 decorate_id_set->emplace(std::move(decoration_payload));
223 break;
224 case SpvOpDecorateStringGOOGLE:
225 decorate_string_set->emplace(std::move(decoration_payload));
226 break;
227 default:
228 break;
229 }
230 }
231 };
232
233 DecorationSet decorate_set_for1;
234 DecorationSet decorate_id_set_for1;
235 DecorationSet decorate_string_set_for1;
236 DecorationSet member_decorate_set_for1;
237 fillDecorationSets(decorations_for1, &decorate_set_for1,
238 &decorate_id_set_for1, &decorate_string_set_for1,
239 &member_decorate_set_for1);
240
241 DecorationSet decorate_set_for2;
242 DecorationSet decorate_id_set_for2;
243 DecorationSet decorate_string_set_for2;
244 DecorationSet member_decorate_set_for2;
245 fillDecorationSets(decorations_for2, &decorate_set_for2,
246 &decorate_id_set_for2, &decorate_string_set_for2,
247 &member_decorate_set_for2);
248
249 const bool result = decorate_set_for1 == decorate_set_for2 &&
250 decorate_id_set_for1 == decorate_id_set_for2 &&
251 member_decorate_set_for1 == member_decorate_set_for2 &&
252 // Compare string sets last in case the strings are long.
253 decorate_string_set_for1 == decorate_string_set_for2;
254 return result;
255 }
256
HaveSubsetOfDecorations(uint32_t id1,uint32_t id2) const257 bool DecorationManager::HaveSubsetOfDecorations(uint32_t id1,
258 uint32_t id2) const {
259 const InstructionVector decorations_for1 = GetDecorationsFor(id1, false);
260 const InstructionVector decorations_for2 = GetDecorationsFor(id2, false);
261
262 // This function splits the decoration instructions into different sets,
263 // based on their opcode; only OpDecorate, OpDecorateId,
264 // OpDecorateStringGOOGLE, and OpMemberDecorate are considered, the other
265 // opcodes are ignored.
266 const auto fillDecorationSets =
267 [](const InstructionVector& decoration_list, DecorationSet* decorate_set,
268 DecorationSet* decorate_id_set, DecorationSet* decorate_string_set,
269 DecorationSet* member_decorate_set) {
270 for (const Instruction* inst : decoration_list) {
271 std::u32string decoration_payload;
272 // Ignore the opcode and the target as we do not want them to be
273 // compared.
274 for (uint32_t i = 1u; i < inst->NumInOperands(); ++i) {
275 for (uint32_t word : inst->GetInOperand(i).words) {
276 decoration_payload.push_back(word);
277 }
278 }
279
280 switch (inst->opcode()) {
281 case SpvOpDecorate:
282 decorate_set->emplace(std::move(decoration_payload));
283 break;
284 case SpvOpMemberDecorate:
285 member_decorate_set->emplace(std::move(decoration_payload));
286 break;
287 case SpvOpDecorateId:
288 decorate_id_set->emplace(std::move(decoration_payload));
289 break;
290 case SpvOpDecorateStringGOOGLE:
291 decorate_string_set->emplace(std::move(decoration_payload));
292 break;
293 default:
294 break;
295 }
296 }
297 };
298
299 DecorationSet decorate_set_for1;
300 DecorationSet decorate_id_set_for1;
301 DecorationSet decorate_string_set_for1;
302 DecorationSet member_decorate_set_for1;
303 fillDecorationSets(decorations_for1, &decorate_set_for1,
304 &decorate_id_set_for1, &decorate_string_set_for1,
305 &member_decorate_set_for1);
306
307 DecorationSet decorate_set_for2;
308 DecorationSet decorate_id_set_for2;
309 DecorationSet decorate_string_set_for2;
310 DecorationSet member_decorate_set_for2;
311 fillDecorationSets(decorations_for2, &decorate_set_for2,
312 &decorate_id_set_for2, &decorate_string_set_for2,
313 &member_decorate_set_for2);
314
315 const bool result =
316 IsSubset(decorate_set_for1, decorate_set_for2) &&
317 IsSubset(decorate_id_set_for1, decorate_id_set_for2) &&
318 IsSubset(member_decorate_set_for1, member_decorate_set_for2) &&
319 // Compare string sets last in case the strings are long.
320 IsSubset(decorate_string_set_for1, decorate_string_set_for2);
321 return result;
322 }
323
324 // TODO(pierremoreau): If OpDecorateId is referencing an OpConstant, one could
325 // check that the constants are the same rather than just
326 // looking at the constant ID.
AreDecorationsTheSame(const Instruction * inst1,const Instruction * inst2,bool ignore_target) const327 bool DecorationManager::AreDecorationsTheSame(const Instruction* inst1,
328 const Instruction* inst2,
329 bool ignore_target) const {
330 switch (inst1->opcode()) {
331 case SpvOpDecorate:
332 case SpvOpMemberDecorate:
333 case SpvOpDecorateId:
334 case SpvOpDecorateStringGOOGLE:
335 break;
336 default:
337 return false;
338 }
339
340 if (inst1->opcode() != inst2->opcode() ||
341 inst1->NumInOperands() != inst2->NumInOperands())
342 return false;
343
344 for (uint32_t i = ignore_target ? 1u : 0u; i < inst1->NumInOperands(); ++i)
345 if (inst1->GetInOperand(i) != inst2->GetInOperand(i)) return false;
346
347 return true;
348 }
349
AnalyzeDecorations()350 void DecorationManager::AnalyzeDecorations() {
351 if (!module_) return;
352
353 // For each group and instruction, collect all their decoration instructions.
354 for (Instruction& inst : module_->annotations()) {
355 AddDecoration(&inst);
356 }
357 }
358
AddDecoration(Instruction * inst)359 void DecorationManager::AddDecoration(Instruction* inst) {
360 switch (inst->opcode()) {
361 case SpvOpDecorate:
362 case SpvOpDecorateId:
363 case SpvOpDecorateStringGOOGLE:
364 case SpvOpMemberDecorate: {
365 const auto target_id = inst->GetSingleWordInOperand(0u);
366 id_to_decoration_insts_[target_id].direct_decorations.push_back(inst);
367 break;
368 }
369 case SpvOpGroupDecorate:
370 case SpvOpGroupMemberDecorate: {
371 const uint32_t start = inst->opcode() == SpvOpGroupDecorate ? 1u : 2u;
372 const uint32_t stride = start;
373 for (uint32_t i = start; i < inst->NumInOperands(); i += stride) {
374 const auto target_id = inst->GetSingleWordInOperand(i);
375 TargetData& target_data = id_to_decoration_insts_[target_id];
376 target_data.indirect_decorations.push_back(inst);
377 }
378 const auto target_id = inst->GetSingleWordInOperand(0u);
379 id_to_decoration_insts_[target_id].decorate_insts.push_back(inst);
380 break;
381 }
382 default:
383 break;
384 }
385 }
386
AddDecoration(SpvOp opcode,std::vector<Operand> opnds)387 void DecorationManager::AddDecoration(SpvOp opcode,
388 std::vector<Operand> opnds) {
389 IRContext* ctx = module_->context();
390 std::unique_ptr<Instruction> newDecoOp(
391 new Instruction(ctx, opcode, 0, 0, opnds));
392 ctx->AddAnnotationInst(std::move(newDecoOp));
393 }
394
AddDecoration(uint32_t inst_id,uint32_t decoration)395 void DecorationManager::AddDecoration(uint32_t inst_id, uint32_t decoration) {
396 AddDecoration(
397 SpvOpDecorate,
398 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {inst_id}},
399 {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {decoration}}});
400 }
401
AddDecorationVal(uint32_t inst_id,uint32_t decoration,uint32_t decoration_value)402 void DecorationManager::AddDecorationVal(uint32_t inst_id, uint32_t decoration,
403 uint32_t decoration_value) {
404 AddDecoration(
405 SpvOpDecorate,
406 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {inst_id}},
407 {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {decoration}},
408 {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER,
409 {decoration_value}}});
410 }
411
AddMemberDecoration(uint32_t inst_id,uint32_t member,uint32_t decoration,uint32_t decoration_value)412 void DecorationManager::AddMemberDecoration(uint32_t inst_id, uint32_t member,
413 uint32_t decoration,
414 uint32_t decoration_value) {
415 AddDecoration(
416 SpvOpMemberDecorate,
417 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {inst_id}},
418 {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {member}},
419 {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {decoration}},
420 {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER,
421 {decoration_value}}});
422 }
423
424 template <typename T>
InternalGetDecorationsFor(uint32_t id,bool include_linkage)425 std::vector<T> DecorationManager::InternalGetDecorationsFor(
426 uint32_t id, bool include_linkage) {
427 std::vector<T> decorations;
428
429 const auto ids_iter = id_to_decoration_insts_.find(id);
430 // |id| has no decorations
431 if (ids_iter == id_to_decoration_insts_.end()) return decorations;
432
433 const TargetData& target_data = ids_iter->second;
434
435 const auto process_direct_decorations =
436 [include_linkage,
437 &decorations](const std::vector<Instruction*>& direct_decorations) {
438 for (Instruction* inst : direct_decorations) {
439 const bool is_linkage = inst->opcode() == SpvOpDecorate &&
440 inst->GetSingleWordInOperand(1u) ==
441 SpvDecorationLinkageAttributes;
442 if (include_linkage || !is_linkage) decorations.push_back(inst);
443 }
444 };
445
446 // Process |id|'s decorations.
447 process_direct_decorations(ids_iter->second.direct_decorations);
448
449 // Process the decorations of all groups applied to |id|.
450 for (const Instruction* inst : target_data.indirect_decorations) {
451 const uint32_t group_id = inst->GetSingleWordInOperand(0u);
452 const auto group_iter = id_to_decoration_insts_.find(group_id);
453 assert(group_iter != id_to_decoration_insts_.end() && "Unknown group ID");
454 process_direct_decorations(group_iter->second.direct_decorations);
455 }
456
457 return decorations;
458 }
459
WhileEachDecoration(uint32_t id,uint32_t decoration,std::function<bool (const Instruction &)> f)460 bool DecorationManager::WhileEachDecoration(
461 uint32_t id, uint32_t decoration,
462 std::function<bool(const Instruction&)> f) {
463 for (const Instruction* inst : GetDecorationsFor(id, true)) {
464 switch (inst->opcode()) {
465 case SpvOpMemberDecorate:
466 if (inst->GetSingleWordInOperand(2) == decoration) {
467 if (!f(*inst)) return false;
468 }
469 break;
470 case SpvOpDecorate:
471 case SpvOpDecorateId:
472 case SpvOpDecorateStringGOOGLE:
473 if (inst->GetSingleWordInOperand(1) == decoration) {
474 if (!f(*inst)) return false;
475 }
476 break;
477 default:
478 assert(false && "Unexpected decoration instruction");
479 }
480 }
481 return true;
482 }
483
ForEachDecoration(uint32_t id,uint32_t decoration,std::function<void (const Instruction &)> f)484 void DecorationManager::ForEachDecoration(
485 uint32_t id, uint32_t decoration,
486 std::function<void(const Instruction&)> f) {
487 WhileEachDecoration(id, decoration, [&f](const Instruction& inst) {
488 f(inst);
489 return true;
490 });
491 }
492
HasDecoration(uint32_t id,uint32_t decoration)493 bool DecorationManager::HasDecoration(uint32_t id, uint32_t decoration) {
494 bool has_decoration = false;
495 ForEachDecoration(id, decoration, [&has_decoration](const Instruction&) {
496 has_decoration = true;
497 });
498 return has_decoration;
499 }
500
FindDecoration(uint32_t id,uint32_t decoration,std::function<bool (const Instruction &)> f)501 bool DecorationManager::FindDecoration(
502 uint32_t id, uint32_t decoration,
503 std::function<bool(const Instruction&)> f) {
504 return !WhileEachDecoration(
505 id, decoration, [&f](const Instruction& inst) { return !f(inst); });
506 }
507
CloneDecorations(uint32_t from,uint32_t to)508 void DecorationManager::CloneDecorations(uint32_t from, uint32_t to) {
509 const auto decoration_list = id_to_decoration_insts_.find(from);
510 if (decoration_list == id_to_decoration_insts_.end()) return;
511 auto context = module_->context();
512 for (Instruction* inst : decoration_list->second.direct_decorations) {
513 // simply clone decoration and change |target-id| to |to|
514 std::unique_ptr<Instruction> new_inst(inst->Clone(module_->context()));
515 new_inst->SetInOperand(0, {to});
516 module_->AddAnnotationInst(std::move(new_inst));
517 auto decoration_iter = --module_->annotation_end();
518 context->AnalyzeUses(&*decoration_iter);
519 }
520 // We need to copy the list of instructions as ForgetUses and AnalyzeUses are
521 // going to modify it.
522 std::vector<Instruction*> indirect_decorations =
523 decoration_list->second.indirect_decorations;
524 for (Instruction* inst : indirect_decorations) {
525 switch (inst->opcode()) {
526 case SpvOpGroupDecorate:
527 context->ForgetUses(inst);
528 // add |to| to list of decorated id's
529 inst->AddOperand(
530 Operand(spv_operand_type_t::SPV_OPERAND_TYPE_ID, {to}));
531 context->AnalyzeUses(inst);
532 break;
533 case SpvOpGroupMemberDecorate: {
534 context->ForgetUses(inst);
535 // for each (id == from), add (to, literal) as operands
536 const uint32_t num_operands = inst->NumOperands();
537 for (uint32_t i = 1; i < num_operands; i += 2) {
538 Operand op = inst->GetOperand(i);
539 if (op.words[0] == from) { // add new pair of operands: (to, literal)
540 inst->AddOperand(
541 Operand(spv_operand_type_t::SPV_OPERAND_TYPE_ID, {to}));
542 op = inst->GetOperand(i + 1);
543 inst->AddOperand(std::move(op));
544 }
545 }
546 context->AnalyzeUses(inst);
547 break;
548 }
549 default:
550 assert(false && "Unexpected decoration instruction");
551 }
552 }
553 }
554
CloneDecorations(uint32_t from,uint32_t to,const std::vector<SpvDecoration> & decorations_to_copy)555 void DecorationManager::CloneDecorations(
556 uint32_t from, uint32_t to,
557 const std::vector<SpvDecoration>& decorations_to_copy) {
558 const auto decoration_list = id_to_decoration_insts_.find(from);
559 if (decoration_list == id_to_decoration_insts_.end()) return;
560 auto context = module_->context();
561 for (Instruction* inst : decoration_list->second.direct_decorations) {
562 if (std::find(decorations_to_copy.begin(), decorations_to_copy.end(),
563 inst->GetSingleWordInOperand(1)) ==
564 decorations_to_copy.end()) {
565 continue;
566 }
567
568 // Clone decoration and change |target-id| to |to|.
569 std::unique_ptr<Instruction> new_inst(inst->Clone(module_->context()));
570 new_inst->SetInOperand(0, {to});
571 module_->AddAnnotationInst(std::move(new_inst));
572 auto decoration_iter = --module_->annotation_end();
573 context->AnalyzeUses(&*decoration_iter);
574 }
575
576 // We need to copy the list of instructions as ForgetUses and AnalyzeUses are
577 // going to modify it.
578 std::vector<Instruction*> indirect_decorations =
579 decoration_list->second.indirect_decorations;
580 for (Instruction* inst : indirect_decorations) {
581 switch (inst->opcode()) {
582 case SpvOpGroupDecorate:
583 CloneDecorations(inst->GetSingleWordInOperand(0), to,
584 decorations_to_copy);
585 break;
586 case SpvOpGroupMemberDecorate: {
587 assert(false && "The source id is not suppose to be a type.");
588 break;
589 }
590 default:
591 assert(false && "Unexpected decoration instruction");
592 }
593 }
594 }
595
RemoveDecoration(Instruction * inst)596 void DecorationManager::RemoveDecoration(Instruction* inst) {
597 const auto remove_from_container = [inst](std::vector<Instruction*>& v) {
598 v.erase(std::remove(v.begin(), v.end(), inst), v.end());
599 };
600
601 switch (inst->opcode()) {
602 case SpvOpDecorate:
603 case SpvOpDecorateId:
604 case SpvOpDecorateStringGOOGLE:
605 case SpvOpMemberDecorate: {
606 const auto target_id = inst->GetSingleWordInOperand(0u);
607 auto const iter = id_to_decoration_insts_.find(target_id);
608 if (iter == id_to_decoration_insts_.end()) return;
609 remove_from_container(iter->second.direct_decorations);
610 } break;
611 case SpvOpGroupDecorate:
612 case SpvOpGroupMemberDecorate: {
613 const uint32_t stride = inst->opcode() == SpvOpGroupDecorate ? 1u : 2u;
614 for (uint32_t i = 1u; i < inst->NumInOperands(); i += stride) {
615 const auto target_id = inst->GetSingleWordInOperand(i);
616 auto const iter = id_to_decoration_insts_.find(target_id);
617 if (iter == id_to_decoration_insts_.end()) continue;
618 remove_from_container(iter->second.indirect_decorations);
619 }
620 const auto group_id = inst->GetSingleWordInOperand(0u);
621 auto const iter = id_to_decoration_insts_.find(group_id);
622 if (iter == id_to_decoration_insts_.end()) return;
623 remove_from_container(iter->second.decorate_insts);
624 } break;
625 default:
626 break;
627 }
628 }
629
operator ==(const DecorationManager & lhs,const DecorationManager & rhs)630 bool operator==(const DecorationManager& lhs, const DecorationManager& rhs) {
631 return lhs.id_to_decoration_insts_ == rhs.id_to_decoration_insts_;
632 }
633
634 } // namespace analysis
635 } // namespace opt
636 } // namespace spvtools
637