2017-05-26 16:33:11 +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.
|
|
|
|
|
|
|
|
#include "insert_extract_elim.h"
|
|
|
|
|
2017-10-30 15:13:24 +00:00
|
|
|
#include "ir_context.h"
|
2017-11-08 17:40:02 +00:00
|
|
|
#include "iterator.h"
|
2017-05-26 16:33:11 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
2017-08-10 22:42:16 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const uint32_t kExtractCompositeIdInIdx = 0;
|
|
|
|
const uint32_t kInsertObjectIdInIdx = 0;
|
|
|
|
const uint32_t kInsertCompositeIdInIdx = 1;
|
|
|
|
|
2017-11-08 17:40:02 +00:00
|
|
|
} // anonymous namespace
|
2017-08-10 22:42:16 +00:00
|
|
|
|
2017-05-26 16:33:11 +00:00
|
|
|
bool InsertExtractElimPass::ExtInsMatch(const ir::Instruction* extInst,
|
2017-11-17 23:47:11 +00:00
|
|
|
const ir::Instruction* insInst,
|
|
|
|
const uint32_t extOffset) const {
|
|
|
|
if (extInst->NumInOperands() - extOffset != insInst->NumInOperands() - 1)
|
|
|
|
return false;
|
|
|
|
uint32_t numIdx = extInst->NumInOperands() - 1 - extOffset;
|
2017-05-26 16:33:11 +00:00
|
|
|
for (uint32_t i = 0; i < numIdx; ++i)
|
2017-11-17 23:47:11 +00:00
|
|
|
if (extInst->GetSingleWordInOperand(i + 1 + extOffset) !=
|
2017-05-26 16:33:11 +00:00
|
|
|
insInst->GetSingleWordInOperand(i + 2))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
bool InsertExtractElimPass::ExtInsConflict(const ir::Instruction* extInst,
|
|
|
|
const ir::Instruction* insInst,
|
|
|
|
const uint32_t extOffset) const {
|
2017-11-17 23:47:11 +00:00
|
|
|
if (extInst->NumInOperands() - extOffset == insInst->NumInOperands() - 1)
|
|
|
|
return false;
|
|
|
|
uint32_t extNumIdx = extInst->NumInOperands() - 1 - extOffset;
|
2017-05-26 16:33:11 +00:00
|
|
|
uint32_t insNumIdx = insInst->NumInOperands() - 2;
|
|
|
|
uint32_t numIdx = std::min(extNumIdx, insNumIdx);
|
|
|
|
for (uint32_t i = 0; i < numIdx; ++i)
|
2017-11-17 23:47:11 +00:00
|
|
|
if (extInst->GetSingleWordInOperand(i + 1 + extOffset) !=
|
2017-05-26 16:33:11 +00:00
|
|
|
insInst->GetSingleWordInOperand(i + 2))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-13 00:15:09 +00:00
|
|
|
bool InsertExtractElimPass::IsVectorType(uint32_t typeId) {
|
2017-10-25 17:26:25 +00:00
|
|
|
ir::Instruction* typeInst = get_def_use_mgr()->GetDef(typeId);
|
2017-09-13 00:15:09 +00:00
|
|
|
return typeInst->opcode() == SpvOpTypeVector;
|
|
|
|
}
|
|
|
|
|
2017-05-26 16:33:11 +00:00
|
|
|
bool InsertExtractElimPass::EliminateInsertExtract(ir::Function* func) {
|
|
|
|
bool modified = false;
|
|
|
|
for (auto bi = func->begin(); bi != func->end(); ++bi) {
|
2017-11-21 19:47:46 +00:00
|
|
|
ir::Instruction* inst = &*bi->begin();
|
|
|
|
while (inst) {
|
|
|
|
switch (inst->opcode()) {
|
2017-05-26 16:33:11 +00:00
|
|
|
case SpvOpCompositeExtract: {
|
2017-11-21 19:47:46 +00:00
|
|
|
uint32_t cid = inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
|
2017-10-25 17:26:25 +00:00
|
|
|
ir::Instruction* cinst = get_def_use_mgr()->GetDef(cid);
|
2017-05-26 16:33:11 +00:00
|
|
|
uint32_t replId = 0;
|
2017-11-17 23:47:11 +00:00
|
|
|
// Offset of extract indices being compared to insert indices.
|
|
|
|
// Offset increases as indices are matched.
|
|
|
|
uint32_t extOffset = 0;
|
2017-05-26 16:33:11 +00:00
|
|
|
while (cinst->opcode() == SpvOpCompositeInsert) {
|
2017-11-21 19:47:46 +00:00
|
|
|
if (ExtInsMatch(inst, cinst, extOffset)) {
|
2017-11-17 23:47:11 +00:00
|
|
|
// Match! Use inserted value as replacement
|
2017-08-10 22:42:16 +00:00
|
|
|
replId = cinst->GetSingleWordInOperand(kInsertObjectIdInIdx);
|
2017-05-26 16:33:11 +00:00
|
|
|
break;
|
2017-11-21 19:47:46 +00:00
|
|
|
} else if (ExtInsConflict(inst, cinst, extOffset)) {
|
2017-11-17 23:47:11 +00:00
|
|
|
// If extract has fewer indices than the insert, stop searching.
|
|
|
|
// Otherwise increment offset of extract indices considered and
|
|
|
|
// continue searching through the inserted value
|
2017-11-21 19:47:46 +00:00
|
|
|
if (inst->NumInOperands() - extOffset <
|
2017-11-17 23:47:11 +00:00
|
|
|
cinst->NumInOperands() - 1) {
|
|
|
|
break;
|
2017-11-27 15:16:41 +00:00
|
|
|
} else {
|
2017-11-17 23:47:11 +00:00
|
|
|
extOffset += cinst->NumInOperands() - 2;
|
|
|
|
cid = cinst->GetSingleWordInOperand(kInsertObjectIdInIdx);
|
|
|
|
}
|
2017-11-27 15:16:41 +00:00
|
|
|
} else {
|
2017-11-17 23:47:11 +00:00
|
|
|
// Consider next composite in insert chain
|
|
|
|
cid = cinst->GetSingleWordInOperand(kInsertCompositeIdInIdx);
|
|
|
|
}
|
2017-10-25 17:26:25 +00:00
|
|
|
cinst = get_def_use_mgr()->GetDef(cid);
|
2017-05-26 16:33:11 +00:00
|
|
|
}
|
2017-09-13 00:15:09 +00:00
|
|
|
// If search ended with CompositeConstruct or ConstantComposite
|
|
|
|
// and the extract has one index, return the appropriate component.
|
|
|
|
// If a vector CompositeConstruct we make sure all preceding
|
|
|
|
// components are of component type (not vector composition).
|
|
|
|
// TODO(greg-lunarg): Handle multiple-indices, ConstantNull, special
|
|
|
|
// vector composition, and additional CompositeInsert.
|
|
|
|
if ((cinst->opcode() == SpvOpCompositeConstruct ||
|
|
|
|
cinst->opcode() == SpvOpConstantComposite) &&
|
2017-11-21 19:47:46 +00:00
|
|
|
inst->NumInOperands() - extOffset == 2) {
|
|
|
|
uint32_t compIdx = inst->GetSingleWordInOperand(extOffset + 1);
|
2017-09-13 00:15:09 +00:00
|
|
|
if (IsVectorType(cinst->type_id())) {
|
|
|
|
if (compIdx < cinst->NumInOperands()) {
|
|
|
|
uint32_t i = 0;
|
|
|
|
for (; i <= compIdx; i++) {
|
|
|
|
uint32_t compId = cinst->GetSingleWordInOperand(i);
|
2017-10-25 17:26:25 +00:00
|
|
|
ir::Instruction* compInst = get_def_use_mgr()->GetDef(compId);
|
2017-11-21 19:47:46 +00:00
|
|
|
if (compInst->type_id() != inst->type_id()) break;
|
2017-09-13 00:15:09 +00:00
|
|
|
}
|
|
|
|
if (i > compIdx)
|
|
|
|
replId = cinst->GetSingleWordInOperand(compIdx);
|
|
|
|
}
|
2017-11-08 17:40:02 +00:00
|
|
|
} else {
|
2017-09-13 00:15:09 +00:00
|
|
|
replId = cinst->GetSingleWordInOperand(compIdx);
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 16:33:11 +00:00
|
|
|
if (replId != 0) {
|
2017-11-21 19:47:46 +00:00
|
|
|
const uint32_t extId = inst->result_id();
|
2017-11-02 18:25:48 +00:00
|
|
|
(void)context()->ReplaceAllUsesWith(extId, replId);
|
2017-11-21 19:47:46 +00:00
|
|
|
inst = context()->KillInst(inst);
|
2017-05-26 16:33:11 +00:00
|
|
|
modified = true;
|
2017-11-21 19:47:46 +00:00
|
|
|
} else {
|
|
|
|
inst = inst->NextNode();
|
2017-05-26 16:33:11 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default:
|
2017-11-21 19:47:46 +00:00
|
|
|
inst = inst->NextNode();
|
2017-05-26 16:33:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:13:24 +00:00
|
|
|
void InsertExtractElimPass::Initialize(ir::IRContext* c) {
|
|
|
|
InitializeProcessing(c);
|
2017-07-19 00:57:26 +00:00
|
|
|
|
|
|
|
// Initialize extension whitelist
|
|
|
|
InitExtensions();
|
2017-05-26 16:33:11 +00:00
|
|
|
};
|
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
bool InsertExtractElimPass::AllExtensionsSupported() const {
|
|
|
|
// If any extension not in whitelist, return false
|
2017-10-25 17:26:25 +00:00
|
|
|
for (auto& ei : get_module()->extensions()) {
|
2017-11-08 17:40:02 +00:00
|
|
|
const char* extName =
|
|
|
|
reinterpret_cast<const char*>(&ei.GetInOperand(0).words[0]);
|
2017-07-19 00:57:26 +00:00
|
|
|
if (extensions_whitelist_.find(extName) == extensions_whitelist_.end())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-26 16:33:11 +00:00
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
Pass::Status InsertExtractElimPass::ProcessImpl() {
|
|
|
|
// Do not process if any disallowed extensions are enabled
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!AllExtensionsSupported()) return Status::SuccessWithoutChange;
|
2017-05-26 16:33:11 +00:00
|
|
|
// Process all entry point functions.
|
2017-08-10 22:42:16 +00:00
|
|
|
ProcessFunction pfn = [this](ir::Function* fp) {
|
|
|
|
return EliminateInsertExtract(fp);
|
|
|
|
};
|
2017-10-25 17:26:25 +00:00
|
|
|
bool modified = ProcessEntryPointCallTree(pfn, get_module());
|
2017-05-26 16:33:11 +00:00
|
|
|
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:26:25 +00:00
|
|
|
InsertExtractElimPass::InsertExtractElimPass() {}
|
2017-05-26 16:33:11 +00:00
|
|
|
|
2017-10-30 15:13:24 +00:00
|
|
|
Pass::Status InsertExtractElimPass::Process(ir::IRContext* c) {
|
|
|
|
Initialize(c);
|
2017-05-26 16:33:11 +00:00
|
|
|
return ProcessImpl();
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
void InsertExtractElimPass::InitExtensions() {
|
|
|
|
extensions_whitelist_.clear();
|
|
|
|
extensions_whitelist_.insert({
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_AMD_shader_explicit_vertex_parameter",
|
|
|
|
"SPV_AMD_shader_trinary_minmax",
|
|
|
|
"SPV_AMD_gcn_shader",
|
|
|
|
"SPV_KHR_shader_ballot",
|
|
|
|
"SPV_AMD_shader_ballot",
|
|
|
|
"SPV_AMD_gpu_shader_half_float",
|
|
|
|
"SPV_KHR_shader_draw_parameters",
|
|
|
|
"SPV_KHR_subgroup_vote",
|
|
|
|
"SPV_KHR_16bit_storage",
|
|
|
|
"SPV_KHR_device_group",
|
|
|
|
"SPV_KHR_multiview",
|
|
|
|
"SPV_NVX_multiview_per_view_attributes",
|
|
|
|
"SPV_NV_viewport_array2",
|
|
|
|
"SPV_NV_stereo_view_rendering",
|
|
|
|
"SPV_NV_sample_mask_override_coverage",
|
|
|
|
"SPV_NV_geometry_shader_passthrough",
|
|
|
|
"SPV_AMD_texture_gather_bias_lod",
|
|
|
|
"SPV_KHR_storage_buffer_storage_class",
|
|
|
|
"SPV_KHR_variable_pointers",
|
|
|
|
"SPV_AMD_gpu_shader_int16",
|
|
|
|
"SPV_KHR_post_depth_coverage",
|
|
|
|
"SPV_KHR_shader_atomic_counter_ops",
|
2017-07-19 00:57:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-26 16:33:11 +00:00
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|