| 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/strip_atomic_counter_memory_pass.h" |
| 16 | #include "source/opt/ir_context.h" |
| 17 | |
| 18 | namespace spvtools { |
| 19 | namespace opt { |
| 20 | |
| 21 | Pass::Status StripAtomicCounterMemoryPass::Process() { |
| 22 | bool changed = false; |
| 23 | context()->module()->ForEachInst([this, &changed](Instruction* inst) { |
| 24 | auto indices = spvOpcodeMemorySemanticsOperandIndices(inst->opcode()); |
| 25 | if (indices.empty()) return; |
| 26 | |
| 27 | for (auto idx : indices) { |
| 28 | auto mem_sem_id = inst->GetSingleWordOperand(idx); |
| 29 | const auto& mem_sem_inst = |
| 30 | context()->get_def_use_mgr()->GetDef(mem_sem_id); |
| 31 | // The spec explicitly says that this id must be an OpConstant |
| 32 | auto mem_sem_val = mem_sem_inst->GetSingleWordOperand(2); |
| 33 | if (!(mem_sem_val & SpvMemorySemanticsAtomicCounterMemoryMask)) { |
| 34 | continue; |
| 35 | } |
| 36 | mem_sem_val &= ~SpvMemorySemanticsAtomicCounterMemoryMask; |
| 37 | |
| 38 | analysis::Integer int_type(32, false); |
| 39 | const analysis::Type* uint32_type = |
| 40 | context()->get_type_mgr()->GetRegisteredType(&int_type); |
| 41 | auto* new_const = context()->get_constant_mgr()->GetConstant( |
| 42 | uint32_type, {mem_sem_val}); |
| 43 | auto* new_const_inst = |
| 44 | context()->get_constant_mgr()->GetDefiningInstruction(new_const); |
| 45 | auto new_const_id = new_const_inst->result_id(); |
| 46 | |
| 47 | inst->SetOperand(idx, {new_const_id}); |
| 48 | context()->UpdateDefUse(inst); |
| 49 | changed = true; |
| 50 | } |
| 51 | }); |
| 52 | |
| 53 | return changed ? Status::SuccessWithChange : Status::SuccessWithoutChange; |
| 54 | } |
| 55 | |
| 56 | } // namespace opt |
| 57 | } // namespace spvtools |
| 58 |