2017-07-27 19:30:12 +00:00
|
|
|
// Copyright (c) 2017 The Khronos Group Inc.
|
|
|
|
// Copyright (c) 2017 Valve Corporation
|
|
|
|
// Copyright (c) 2017 LunarG Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/mem_pass.h"
|
2017-07-27 19:30:12 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "source/cfa.h"
|
|
|
|
#include "source/opt/basic_block.h"
|
|
|
|
#include "source/opt/dominator_analysis.h"
|
|
|
|
#include "source/opt/ir_context.h"
|
|
|
|
#include "source/opt/iterator.h"
|
2017-07-27 19:30:12 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const uint32_t kCopyObjectOperandInIdx = 0;
|
|
|
|
const uint32_t kTypePointerStorageClassInIdx = 0;
|
|
|
|
const uint32_t kTypePointerTypeIdInIdx = 1;
|
|
|
|
|
2017-10-25 17:26:25 +00:00
|
|
|
} // namespace
|
2017-07-27 19:30:12 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool MemPass::IsBaseTargetType(const Instruction* typeInst) const {
|
2017-07-27 19:30:12 +00:00
|
|
|
switch (typeInst->opcode()) {
|
2017-10-25 17:26:25 +00:00
|
|
|
case SpvOpTypeInt:
|
|
|
|
case SpvOpTypeFloat:
|
|
|
|
case SpvOpTypeBool:
|
|
|
|
case SpvOpTypeVector:
|
|
|
|
case SpvOpTypeMatrix:
|
|
|
|
case SpvOpTypeImage:
|
|
|
|
case SpvOpTypeSampler:
|
|
|
|
case SpvOpTypeSampledImage:
|
2017-12-11 18:10:24 +00:00
|
|
|
case SpvOpTypePointer:
|
2017-10-25 17:26:25 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
2017-07-27 19:30:12 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool MemPass::IsTargetType(const Instruction* typeInst) const {
|
2017-10-25 17:26:25 +00:00
|
|
|
if (IsBaseTargetType(typeInst)) return true;
|
2017-12-11 18:10:24 +00:00
|
|
|
if (typeInst->opcode() == SpvOpTypeArray) {
|
|
|
|
if (!IsTargetType(
|
|
|
|
get_def_use_mgr()->GetDef(typeInst->GetSingleWordOperand(1)))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-25 17:26:25 +00:00
|
|
|
if (typeInst->opcode() != SpvOpTypeStruct) return false;
|
2017-07-27 19:30:12 +00:00
|
|
|
// All struct members must be math type
|
2018-01-12 20:05:53 +00:00
|
|
|
return typeInst->WhileEachInId([this](const uint32_t* tid) {
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* compTypeInst = get_def_use_mgr()->GetDef(*tid);
|
2018-01-12 20:05:53 +00:00
|
|
|
if (!IsTargetType(compTypeInst)) return false;
|
|
|
|
return true;
|
2017-07-27 19:30:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemPass::IsNonPtrAccessChain(const SpvOp opcode) const {
|
|
|
|
return opcode == SpvOpAccessChain || opcode == SpvOpInBoundsAccessChain;
|
|
|
|
}
|
|
|
|
|
2017-08-04 21:04:37 +00:00
|
|
|
bool MemPass::IsPtr(uint32_t ptrId) {
|
|
|
|
uint32_t varId = ptrId;
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* ptrInst = get_def_use_mgr()->GetDef(varId);
|
2017-08-04 21:04:37 +00:00
|
|
|
while (ptrInst->opcode() == SpvOpCopyObject) {
|
|
|
|
varId = ptrInst->GetSingleWordInOperand(kCopyObjectOperandInIdx);
|
2017-10-25 17:26:25 +00:00
|
|
|
ptrInst = get_def_use_mgr()->GetDef(varId);
|
2017-08-04 21:04:37 +00:00
|
|
|
}
|
|
|
|
const SpvOp op = ptrInst->opcode();
|
2017-10-25 17:26:25 +00:00
|
|
|
if (op == SpvOpVariable || IsNonPtrAccessChain(op)) return true;
|
|
|
|
if (op != SpvOpFunctionParameter) return false;
|
2017-08-10 22:42:16 +00:00
|
|
|
const uint32_t varTypeId = ptrInst->type_id();
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId);
|
2017-08-10 22:42:16 +00:00
|
|
|
return varTypeInst->opcode() == SpvOpTypePointer;
|
2017-08-04 21:04:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* MemPass::GetPtr(uint32_t ptrId, uint32_t* varId) {
|
2017-08-04 21:04:37 +00:00
|
|
|
*varId = ptrId;
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* ptrInst = get_def_use_mgr()->GetDef(*varId);
|
|
|
|
Instruction* varInst;
|
2017-12-11 18:10:24 +00:00
|
|
|
|
2020-05-20 16:00:51 +00:00
|
|
|
if (ptrInst->opcode() == SpvOpConstantNull) {
|
|
|
|
*varId = 0;
|
|
|
|
return ptrInst;
|
|
|
|
}
|
|
|
|
|
2017-12-11 18:10:24 +00:00
|
|
|
if (ptrInst->opcode() != SpvOpVariable &&
|
|
|
|
ptrInst->opcode() != SpvOpFunctionParameter) {
|
|
|
|
varInst = ptrInst->GetBaseAddress();
|
|
|
|
} else {
|
|
|
|
varInst = ptrInst;
|
2017-07-27 19:30:12 +00:00
|
|
|
}
|
2017-12-11 18:10:24 +00:00
|
|
|
if (varInst->opcode() == SpvOpVariable) {
|
|
|
|
*varId = varInst->result_id();
|
|
|
|
} else {
|
|
|
|
*varId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ptrInst->opcode() == SpvOpCopyObject) {
|
|
|
|
uint32_t temp = ptrInst->GetSingleWordInOperand(0);
|
|
|
|
ptrInst = get_def_use_mgr()->GetDef(temp);
|
2017-07-27 19:30:12 +00:00
|
|
|
}
|
2017-12-11 18:10:24 +00:00
|
|
|
|
2017-07-27 19:30:12 +00:00
|
|
|
return ptrInst;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* MemPass::GetPtr(Instruction* ip, uint32_t* varId) {
|
2018-04-02 17:40:53 +00:00
|
|
|
assert(ip->opcode() == SpvOpStore || ip->opcode() == SpvOpLoad ||
|
2018-10-12 12:46:35 +00:00
|
|
|
ip->opcode() == SpvOpImageTexelPointer || ip->IsAtomicWithLoad());
|
2018-04-02 17:40:53 +00:00
|
|
|
|
|
|
|
// All of these opcode place the pointer in position 0.
|
|
|
|
const uint32_t ptrId = ip->GetSingleWordInOperand(0);
|
2017-08-04 21:04:37 +00:00
|
|
|
return GetPtr(ptrId, varId);
|
|
|
|
}
|
|
|
|
|
2017-07-27 19:30:12 +00:00
|
|
|
bool MemPass::HasOnlyNamesAndDecorates(uint32_t id) const {
|
2018-07-12 19:14:43 +00:00
|
|
|
return get_def_use_mgr()->WhileEachUser(id, [this](Instruction* user) {
|
2018-01-12 20:05:53 +00:00
|
|
|
SpvOp op = user->opcode();
|
|
|
|
if (op != SpvOpName && !IsNonTypeDecorate(op)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2017-07-27 19:30:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void MemPass::KillAllInsts(BasicBlock* bp, bool killLabel) {
|
2018-02-12 21:42:15 +00:00
|
|
|
bp->KillAllInsts(killLabel);
|
2017-10-17 22:33:43 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 19:30:12 +00:00
|
|
|
bool MemPass::HasLoads(uint32_t varId) const {
|
2018-07-12 19:14:43 +00:00
|
|
|
return !get_def_use_mgr()->WhileEachUser(varId, [this](Instruction* user) {
|
2017-11-14 19:11:50 +00:00
|
|
|
SpvOp op = user->opcode();
|
2017-07-27 19:30:12 +00:00
|
|
|
// TODO(): The following is slightly conservative. Could be
|
|
|
|
// better handling of non-store/name.
|
|
|
|
if (IsNonPtrAccessChain(op) || op == SpvOpCopyObject) {
|
2017-11-14 19:11:50 +00:00
|
|
|
if (HasLoads(user->result_id())) {
|
2018-01-12 20:05:53 +00:00
|
|
|
return false;
|
2017-11-14 19:11:50 +00:00
|
|
|
}
|
|
|
|
} else if (op != SpvOpStore && op != SpvOpName && !IsNonTypeDecorate(op)) {
|
2018-01-12 20:05:53 +00:00
|
|
|
return false;
|
2017-11-14 19:11:50 +00:00
|
|
|
}
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
2017-11-14 19:11:50 +00:00
|
|
|
});
|
2017-07-27 19:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MemPass::IsLiveVar(uint32_t varId) const {
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* varInst = get_def_use_mgr()->GetDef(varId);
|
2017-08-10 22:42:16 +00:00
|
|
|
// assume live if not a variable eg. function parameter
|
2017-10-25 17:26:25 +00:00
|
|
|
if (varInst->opcode() != SpvOpVariable) return true;
|
2017-08-10 22:42:16 +00:00
|
|
|
// non-function scope vars are live
|
2017-07-27 19:30:12 +00:00
|
|
|
const uint32_t varTypeId = varInst->type_id();
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId);
|
2017-07-27 19:30:12 +00:00
|
|
|
if (varTypeInst->GetSingleWordInOperand(kTypePointerStorageClassInIdx) !=
|
|
|
|
SpvStorageClassFunction)
|
|
|
|
return true;
|
|
|
|
// test if variable is loaded from
|
|
|
|
return HasLoads(varId);
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void MemPass::AddStores(uint32_t ptr_id, std::queue<Instruction*>* insts) {
|
|
|
|
get_def_use_mgr()->ForEachUser(ptr_id, [this, insts](Instruction* user) {
|
2017-11-14 19:11:50 +00:00
|
|
|
SpvOp op = user->opcode();
|
|
|
|
if (IsNonPtrAccessChain(op)) {
|
|
|
|
AddStores(user->result_id(), insts);
|
|
|
|
} else if (op == SpvOpStore) {
|
|
|
|
insts->push(user);
|
2017-07-27 19:30:12 +00:00
|
|
|
}
|
2017-11-14 19:11:50 +00:00
|
|
|
});
|
2017-07-27 19:30:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void MemPass::DCEInst(Instruction* inst,
|
2018-08-01 18:58:12 +00:00
|
|
|
const std::function<void(Instruction*)>& call_back) {
|
2018-07-12 19:14:43 +00:00
|
|
|
std::queue<Instruction*> deadInsts;
|
2017-07-27 19:30:12 +00:00
|
|
|
deadInsts.push(inst);
|
|
|
|
while (!deadInsts.empty()) {
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* di = deadInsts.front();
|
2017-07-27 19:30:12 +00:00
|
|
|
// Don't delete labels
|
|
|
|
if (di->opcode() == SpvOpLabel) {
|
|
|
|
deadInsts.pop();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Remember operands
|
2017-12-04 17:29:51 +00:00
|
|
|
std::set<uint32_t> ids;
|
|
|
|
di->ForEachInId([&ids](uint32_t* iid) { ids.insert(*iid); });
|
2017-07-27 19:30:12 +00:00
|
|
|
uint32_t varId = 0;
|
|
|
|
// Remember variable if dead load
|
2017-10-25 17:26:25 +00:00
|
|
|
if (di->opcode() == SpvOpLoad) (void)GetPtr(di, &varId);
|
2017-11-21 19:47:46 +00:00
|
|
|
if (call_back) {
|
|
|
|
call_back(di);
|
|
|
|
}
|
2017-11-02 18:25:48 +00:00
|
|
|
context()->KillInst(di);
|
2017-07-27 19:30:12 +00:00
|
|
|
// For all operands with no remaining uses, add their instruction
|
|
|
|
// to the dead instruction queue.
|
|
|
|
for (auto id : ids)
|
2018-02-21 23:43:33 +00:00
|
|
|
if (HasOnlyNamesAndDecorates(id)) {
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* odi = get_def_use_mgr()->GetDef(id);
|
2018-02-21 23:43:33 +00:00
|
|
|
if (context()->IsCombinatorInstruction(odi)) deadInsts.push(odi);
|
|
|
|
}
|
2017-07-27 19:30:12 +00:00
|
|
|
// if a load was deleted and it was the variable's
|
|
|
|
// last load, add all its stores to dead queue
|
2017-10-25 17:26:25 +00:00
|
|
|
if (varId != 0 && !IsLiveVar(varId)) AddStores(varId, &deadInsts);
|
2017-07-27 19:30:12 +00:00
|
|
|
deadInsts.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:26:25 +00:00
|
|
|
MemPass::MemPass() {}
|
|
|
|
|
|
|
|
bool MemPass::HasOnlySupportedRefs(uint32_t varId) {
|
2018-07-12 19:14:43 +00:00
|
|
|
return get_def_use_mgr()->WhileEachUser(varId, [this](Instruction* user) {
|
2018-01-12 20:05:53 +00:00
|
|
|
SpvOp op = user->opcode();
|
|
|
|
if (op != SpvOpStore && op != SpvOpLoad && op != SpvOpName &&
|
|
|
|
!IsNonTypeDecorate(op)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2017-10-25 17:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t MemPass::Type2Undef(uint32_t type_id) {
|
|
|
|
const auto uitr = type2undefs_.find(type_id);
|
|
|
|
if (uitr != type2undefs_.end()) return uitr->second;
|
|
|
|
const uint32_t undefId = TakeNextId();
|
2019-09-10 13:38:23 +00:00
|
|
|
if (undefId == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<Instruction> undef_inst(
|
|
|
|
new Instruction(context(), SpvOpUndef, type_id, undefId, {}));
|
2017-10-25 17:26:25 +00:00
|
|
|
get_def_use_mgr()->AnalyzeInstDefUse(&*undef_inst);
|
|
|
|
get_module()->AddGlobalValue(std::move(undef_inst));
|
|
|
|
type2undefs_[type_id] = undefId;
|
|
|
|
return undefId;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemPass::IsTargetVar(uint32_t varId) {
|
2017-12-11 18:10:24 +00:00
|
|
|
if (varId == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:26:25 +00:00
|
|
|
if (seen_non_target_vars_.find(varId) != seen_non_target_vars_.end())
|
|
|
|
return false;
|
|
|
|
if (seen_target_vars_.find(varId) != seen_target_vars_.end()) return true;
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* varInst = get_def_use_mgr()->GetDef(varId);
|
2017-10-25 17:26:25 +00:00
|
|
|
if (varInst->opcode() != SpvOpVariable) return false;
|
|
|
|
const uint32_t varTypeId = varInst->type_id();
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId);
|
2017-10-25 17:26:25 +00:00
|
|
|
if (varTypeInst->GetSingleWordInOperand(kTypePointerStorageClassInIdx) !=
|
|
|
|
SpvStorageClassFunction) {
|
|
|
|
seen_non_target_vars_.insert(varId);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const uint32_t varPteTypeId =
|
|
|
|
varTypeInst->GetSingleWordInOperand(kTypePointerTypeIdInIdx);
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* varPteTypeInst = get_def_use_mgr()->GetDef(varPteTypeId);
|
2017-10-25 17:26:25 +00:00
|
|
|
if (!IsTargetType(varPteTypeInst)) {
|
|
|
|
seen_non_target_vars_.insert(varId);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
seen_target_vars_.insert(varId);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-17 22:33:43 +00:00
|
|
|
// Remove all |phi| operands coming from unreachable blocks (i.e., blocks not in
|
|
|
|
// |reachable_blocks|). There are two types of removal that this function can
|
|
|
|
// perform:
|
|
|
|
//
|
|
|
|
// 1- Any operand that comes directly from an unreachable block is completely
|
|
|
|
// removed. Since the block is unreachable, the edge between the unreachable
|
|
|
|
// block and the block holding |phi| has been removed.
|
|
|
|
//
|
|
|
|
// 2- Any operand that comes via a live block and was defined at an unreachable
|
|
|
|
// block gets its value replaced with an OpUndef value. Since the argument
|
|
|
|
// was generated in an unreachable block, it no longer exists, so it cannot
|
|
|
|
// be referenced. However, since the value does not reach |phi| directly
|
|
|
|
// from the unreachable block, the operand cannot be removed from |phi|.
|
|
|
|
// Therefore, we replace the argument value with OpUndef.
|
|
|
|
//
|
|
|
|
// For example, in the switch() below, assume that we want to remove the
|
|
|
|
// argument with value %11 coming from block %41.
|
|
|
|
//
|
|
|
|
// [ ... ]
|
|
|
|
// %41 = OpLabel <--- Unreachable block
|
|
|
|
// %11 = OpLoad %int %y
|
|
|
|
// [ ... ]
|
|
|
|
// OpSelectionMerge %16 None
|
|
|
|
// OpSwitch %12 %16 10 %13 13 %14 18 %15
|
|
|
|
// %13 = OpLabel
|
|
|
|
// OpBranch %16
|
|
|
|
// %14 = OpLabel
|
|
|
|
// OpStore %outparm %int_14
|
|
|
|
// OpBranch %16
|
|
|
|
// %15 = OpLabel
|
|
|
|
// OpStore %outparm %int_15
|
|
|
|
// OpBranch %16
|
|
|
|
// %16 = OpLabel
|
|
|
|
// %30 = OpPhi %int %11 %41 %int_42 %13 %11 %14 %11 %15
|
|
|
|
//
|
|
|
|
// Since %41 is now an unreachable block, the first operand of |phi| needs to
|
|
|
|
// be removed completely. But the operands (%11 %14) and (%11 %15) cannot be
|
|
|
|
// removed because %14 and %15 are reachable blocks. Since %11 no longer exist,
|
|
|
|
// in those arguments, we replace all references to %11 with an OpUndef value.
|
|
|
|
// This results in |phi| looking like:
|
|
|
|
//
|
|
|
|
// %50 = OpUndef %int
|
|
|
|
// [ ... ]
|
|
|
|
// %30 = OpPhi %int %int_42 %13 %50 %14 %50 %15
|
|
|
|
void MemPass::RemovePhiOperands(
|
2018-08-01 18:58:12 +00:00
|
|
|
Instruction* phi, const std::unordered_set<BasicBlock*>& reachable_blocks) {
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Operand> keep_operands;
|
2017-10-17 22:33:43 +00:00
|
|
|
uint32_t type_id = 0;
|
|
|
|
// The id of an undefined value we've generated.
|
|
|
|
uint32_t undef_id = 0;
|
|
|
|
|
|
|
|
// Traverse all the operands in |phi|. Build the new operand vector by adding
|
|
|
|
// all the original operands from |phi| except the unwanted ones.
|
|
|
|
for (uint32_t i = 0; i < phi->NumOperands();) {
|
|
|
|
if (i < 2) {
|
|
|
|
// The first two arguments are always preserved.
|
|
|
|
keep_operands.push_back(phi->GetOperand(i));
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The remaining Phi arguments come in pairs. Index 'i' contains the
|
|
|
|
// variable id, index 'i + 1' is the originating block id.
|
|
|
|
assert(i % 2 == 0 && i < phi->NumOperands() - 1 &&
|
|
|
|
"malformed Phi arguments");
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* in_block = cfg()->block(phi->GetSingleWordOperand(i + 1));
|
2017-10-17 22:33:43 +00:00
|
|
|
if (reachable_blocks.find(in_block) == reachable_blocks.end()) {
|
|
|
|
// If the incoming block is unreachable, remove both operands as this
|
|
|
|
// means that the |phi| has lost an incoming edge.
|
|
|
|
i += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In all other cases, the operand must be kept but may need to be changed.
|
|
|
|
uint32_t arg_id = phi->GetSingleWordOperand(i);
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* arg_def_instr = get_def_use_mgr()->GetDef(arg_id);
|
|
|
|
BasicBlock* def_block = context()->get_instr_block(arg_def_instr);
|
2017-10-17 22:33:43 +00:00
|
|
|
if (def_block &&
|
2017-11-10 14:39:00 +00:00
|
|
|
reachable_blocks.find(def_block) == reachable_blocks.end()) {
|
2017-10-17 22:33:43 +00:00
|
|
|
// If the current |phi| argument was defined in an unreachable block, it
|
|
|
|
// means that this |phi| argument is no longer defined. Replace it with
|
|
|
|
// |undef_id|.
|
|
|
|
if (!undef_id) {
|
2017-11-10 14:39:00 +00:00
|
|
|
type_id = arg_def_instr->type_id();
|
2017-10-17 22:33:43 +00:00
|
|
|
undef_id = Type2Undef(type_id);
|
|
|
|
}
|
|
|
|
keep_operands.push_back(
|
2018-07-12 19:14:43 +00:00
|
|
|
Operand(spv_operand_type_t::SPV_OPERAND_TYPE_ID, {undef_id}));
|
2017-10-17 22:33:43 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, the argument comes from a reachable block or from no block
|
|
|
|
// at all (meaning that it was defined in the global section of the
|
|
|
|
// program). In both cases, keep the argument intact.
|
|
|
|
keep_operands.push_back(phi->GetOperand(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
keep_operands.push_back(phi->GetOperand(i + 1));
|
|
|
|
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
|
2017-11-09 16:24:41 +00:00
|
|
|
context()->ForgetUses(phi);
|
2017-10-17 22:33:43 +00:00
|
|
|
phi->ReplaceOperands(keep_operands);
|
2017-11-09 16:24:41 +00:00
|
|
|
context()->AnalyzeUses(phi);
|
2017-10-17 22:33:43 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void MemPass::RemoveBlock(Function::iterator* bi) {
|
2017-10-17 22:33:43 +00:00
|
|
|
auto& rm_block = **bi;
|
|
|
|
|
|
|
|
// Remove instructions from the block.
|
2018-07-12 19:14:43 +00:00
|
|
|
rm_block.ForEachInst([&rm_block, this](Instruction* inst) {
|
2017-10-17 22:33:43 +00:00
|
|
|
// Note that we do not kill the block label instruction here. The label
|
|
|
|
// instruction is needed to identify the block, which is needed by the
|
|
|
|
// removal of phi operands.
|
|
|
|
if (inst != rm_block.GetLabelInst()) {
|
2017-11-02 18:25:48 +00:00
|
|
|
context()->KillInst(inst);
|
2017-10-17 22:33:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove the label instruction last.
|
|
|
|
auto label = rm_block.GetLabelInst();
|
2017-11-02 18:25:48 +00:00
|
|
|
context()->KillInst(label);
|
2017-10-17 22:33:43 +00:00
|
|
|
|
|
|
|
*bi = bi->Erase();
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool MemPass::RemoveUnreachableBlocks(Function* func) {
|
2017-10-17 22:33:43 +00:00
|
|
|
bool modified = false;
|
|
|
|
|
|
|
|
// Mark reachable all blocks reachable from the function's entry block.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_set<BasicBlock*> reachable_blocks;
|
|
|
|
std::unordered_set<BasicBlock*> visited_blocks;
|
|
|
|
std::queue<BasicBlock*> worklist;
|
2017-10-17 22:33:43 +00:00
|
|
|
reachable_blocks.insert(func->entry().get());
|
|
|
|
|
|
|
|
// Initially mark the function entry point as reachable.
|
|
|
|
worklist.push(func->entry().get());
|
|
|
|
|
|
|
|
auto mark_reachable = [&reachable_blocks, &visited_blocks, &worklist,
|
|
|
|
this](uint32_t label_id) {
|
2017-10-30 21:42:26 +00:00
|
|
|
auto successor = cfg()->block(label_id);
|
2017-10-17 22:33:43 +00:00
|
|
|
if (visited_blocks.count(successor) == 0) {
|
|
|
|
reachable_blocks.insert(successor);
|
|
|
|
worklist.push(successor);
|
|
|
|
visited_blocks.insert(successor);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Transitively mark all blocks reachable from the entry as reachable.
|
|
|
|
while (!worklist.empty()) {
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* block = worklist.front();
|
2017-10-17 22:33:43 +00:00
|
|
|
worklist.pop();
|
|
|
|
|
|
|
|
// All the successors of a live block are also live.
|
2018-07-12 19:14:43 +00:00
|
|
|
static_cast<const BasicBlock*>(block)->ForEachSuccessorLabel(
|
2018-02-02 22:17:42 +00:00
|
|
|
mark_reachable);
|
2017-10-17 22:33:43 +00:00
|
|
|
|
|
|
|
// All the Merge and ContinueTarget blocks of a live block are also live.
|
|
|
|
block->ForMergeAndContinueLabel(mark_reachable);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update operands of Phi nodes that reference unreachable blocks.
|
|
|
|
for (auto& block : *func) {
|
|
|
|
// If the block is about to be removed, don't bother updating its
|
|
|
|
// Phi instructions.
|
|
|
|
if (reachable_blocks.count(&block) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the block is reachable and has Phi instructions, remove all
|
|
|
|
// operands from its Phi instructions that reference unreachable blocks.
|
|
|
|
// If the block has no Phi instructions, this is a no-op.
|
2018-07-12 19:14:43 +00:00
|
|
|
block.ForEachPhiInst([&reachable_blocks, this](Instruction* phi) {
|
2017-11-30 21:32:44 +00:00
|
|
|
RemovePhiOperands(phi, reachable_blocks);
|
|
|
|
});
|
2017-10-17 22:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Erase unreachable blocks.
|
|
|
|
for (auto ebi = func->begin(); ebi != func->end();) {
|
|
|
|
if (reachable_blocks.count(&*ebi) == 0) {
|
|
|
|
RemoveBlock(&ebi);
|
|
|
|
modified = true;
|
|
|
|
} else {
|
|
|
|
++ebi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool MemPass::CFGCleanup(Function* func) {
|
2017-10-17 22:33:43 +00:00
|
|
|
bool modified = false;
|
|
|
|
modified |= RemoveUnreachableBlocks(func);
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void MemPass::CollectTargetVars(Function* func) {
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
seen_target_vars_.clear();
|
|
|
|
seen_non_target_vars_.clear();
|
|
|
|
type2undefs_.clear();
|
|
|
|
|
|
|
|
// Collect target (and non-) variable sets. Remove variables with
|
|
|
|
// non-load/store refs from target variable set
|
|
|
|
for (auto& blk : *func) {
|
|
|
|
for (auto& inst : blk) {
|
|
|
|
switch (inst.opcode()) {
|
|
|
|
case SpvOpStore:
|
|
|
|
case SpvOpLoad: {
|
|
|
|
uint32_t varId;
|
|
|
|
(void)GetPtr(&inst, &varId);
|
|
|
|
if (!IsTargetVar(varId)) break;
|
|
|
|
if (HasOnlySupportedRefs(varId)) break;
|
|
|
|
seen_non_target_vars_.insert(varId);
|
|
|
|
seen_target_vars_.erase(varId);
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 19:30:12 +00:00
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|