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