Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spirv-opt: A pass to removed unused input on OpEntryPoint instruction…
…s. (#4275) The new pass will removed interface variable on the OpEntryPoint instruction when they are not statically referenced in the call tree of the entry point. It can be enabled on the command line using the options `remove-unused-interface-variables`.
- Loading branch information
Showing
11 changed files
with
331 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) 2021 ZHOU He | ||
// | ||
// 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 "remove_unused_interface_variables_pass.h" | ||
#include "source/spirv_constant.h" | ||
namespace spvtools { | ||
namespace opt { | ||
|
||
class RemoveUnusedInterfaceVariablesContext { | ||
RemoveUnusedInterfaceVariablesPass& parent_; | ||
Instruction& entry_; | ||
std::unordered_set<uint32_t> used_variables_; | ||
IRContext::ProcessFunction pfn_ = | ||
std::bind(&RemoveUnusedInterfaceVariablesContext::processFunction, this, | ||
std::placeholders::_1); | ||
|
||
bool processFunction(Function* func) { | ||
for (const auto& basic_block : *func) | ||
for (const auto& instruction : basic_block) | ||
instruction.ForEachInId([&](const uint32_t* id) { | ||
if (used_variables_.count(*id)) return; | ||
auto* var = parent_.get_def_use_mgr()->GetDef(*id); | ||
if (!var || var->opcode() != SpvOpVariable) return; | ||
auto storage_class = var->GetSingleWordInOperand(0); | ||
if (storage_class != SpvStorageClassFunction && | ||
(parent_.get_module()->version() >= | ||
SPV_SPIRV_VERSION_WORD(1, 4) || | ||
storage_class == SpvStorageClassInput || | ||
storage_class == SpvStorageClassOutput)) | ||
used_variables_.insert(*id); | ||
}); | ||
return false; | ||
} | ||
|
||
public: | ||
RemoveUnusedInterfaceVariablesContext( | ||
RemoveUnusedInterfaceVariablesPass& parent, Instruction& entry) | ||
: parent_(parent), entry_(entry) {} | ||
|
||
void CollectUsedVariables() { | ||
std::queue<uint32_t> roots; | ||
roots.push(entry_.GetSingleWordInOperand(1)); | ||
parent_.context()->ProcessCallTreeFromRoots(pfn_, &roots); | ||
} | ||
|
||
bool ShouldModify() { | ||
std::unordered_set<uint32_t> old_variables; | ||
for (int i = entry_.NumInOperands() - 1; i >= 3; --i) { | ||
auto variable = entry_.GetInOperand(i).words[0]; | ||
if (!used_variables_.count(variable)) return true; // It is unused. | ||
if (old_variables.count(variable)) return true; // It is duplicate. | ||
old_variables.insert(variable); | ||
} | ||
if (old_variables.size() != used_variables_.size()) // Missing IDs. | ||
return true; | ||
return false; | ||
} | ||
|
||
void Modify() { | ||
for (int i = entry_.NumInOperands() - 1; i >= 3; --i) | ||
entry_.RemoveInOperand(i); | ||
for (auto id : used_variables_) { | ||
entry_.AddOperand(Operand(SPV_OPERAND_TYPE_ID, {id})); | ||
} | ||
} | ||
}; | ||
|
||
RemoveUnusedInterfaceVariablesPass::Status | ||
RemoveUnusedInterfaceVariablesPass::Process() { | ||
bool modified = false; | ||
for (auto& entry : get_module()->entry_points()) { | ||
RemoveUnusedInterfaceVariablesContext context(*this, entry); | ||
context.CollectUsedVariables(); | ||
if (context.ShouldModify()) { | ||
context.Modify(); | ||
modified = true; | ||
} | ||
} | ||
return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange); | ||
} | ||
} // namespace opt | ||
} // namespace spvtools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) 2021 ZHOU He | ||
// | ||
// 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 "source/opt/pass.h" | ||
namespace spvtools { | ||
namespace opt { | ||
|
||
class RemoveUnusedInterfaceVariablesPass : public Pass { | ||
const char* name() const override { | ||
return "remove-unused-interface-variables-pass"; | ||
} | ||
Status Process() override; | ||
}; | ||
} // namespace opt | ||
} // namespace spvtools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
// Copyright (c) 2021 ZHOU He | ||
// | ||
// 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 "gmock/gmock.h" | ||
#include "test/opt/assembly_builder.h" | ||
#include "test/opt/pass_fixture.h" | ||
#include "test/opt/pass_utils.h" | ||
|
||
namespace spvtools { | ||
namespace opt { | ||
namespace { | ||
|
||
using RemoveUnusedInterfaceVariablesTest = PassTest<::testing::Test>; | ||
|
||
static const std::string expected = R"(OpCapability Shader | ||
OpMemoryModel Logical GLSL450 | ||
OpEntryPoint Fragment %_Z5func1v "_Z5func1v" %out_var_SV_TARGET | ||
OpEntryPoint Fragment %_Z5func2v "_Z5func2v" %out_var_SV_TARGET_0 | ||
OpExecutionMode %_Z5func1v OriginUpperLeft | ||
OpExecutionMode %_Z5func2v OriginUpperLeft | ||
OpSource HLSL 630 | ||
OpName %type_cba "type.cba" | ||
OpMemberName %type_cba 0 "color" | ||
OpName %cba "cba" | ||
OpName %out_var_SV_TARGET "out.var.SV_TARGET" | ||
OpName %out_var_SV_TARGET_0 "out.var.SV_TARGET" | ||
OpName %_Z5func1v "_Z5func1v" | ||
OpName %_Z5func2v "_Z5func2v" | ||
OpDecorate %out_var_SV_TARGET Location 0 | ||
OpDecorate %out_var_SV_TARGET_0 Location 0 | ||
OpDecorate %cba DescriptorSet 0 | ||
OpDecorate %cba Binding 0 | ||
OpMemberDecorate %type_cba 0 Offset 0 | ||
OpDecorate %type_cba Block | ||
%int = OpTypeInt 32 1 | ||
%int_0 = OpConstant %int 0 | ||
%float = OpTypeFloat 32 | ||
%v4float = OpTypeVector %float 4 | ||
%type_cba = OpTypeStruct %v4float | ||
%_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba | ||
%_ptr_Output_v4float = OpTypePointer Output %v4float | ||
%void = OpTypeVoid | ||
%14 = OpTypeFunction %void | ||
%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float | ||
%cba = OpVariable %_ptr_Uniform_type_cba Uniform | ||
%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output | ||
%out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output | ||
%_Z5func1v = OpFunction %void None %14 | ||
%16 = OpLabel | ||
%17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0 | ||
%18 = OpLoad %v4float %17 | ||
OpStore %out_var_SV_TARGET %18 | ||
OpReturn | ||
OpFunctionEnd | ||
%_Z5func2v = OpFunction %void None %14 | ||
%19 = OpLabel | ||
%20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0 | ||
%21 = OpLoad %v4float %20 | ||
OpStore %out_var_SV_TARGET_0 %21 | ||
OpReturn | ||
OpFunctionEnd | ||
)"; | ||
|
||
TEST_F(RemoveUnusedInterfaceVariablesTest, RemoveUnusedVariable) { | ||
const std::string text = R"(OpCapability Shader | ||
OpMemoryModel Logical GLSL450 | ||
OpEntryPoint Fragment %_Z5func1v "_Z5func1v" %out_var_SV_TARGET %out_var_SV_TARGET_0 | ||
OpEntryPoint Fragment %_Z5func2v "_Z5func2v" %out_var_SV_TARGET %out_var_SV_TARGET_0 | ||
OpExecutionMode %_Z5func1v OriginUpperLeft | ||
OpExecutionMode %_Z5func2v OriginUpperLeft | ||
OpSource HLSL 630 | ||
OpName %type_cba "type.cba" | ||
OpMemberName %type_cba 0 "color" | ||
OpName %cba "cba" | ||
OpName %out_var_SV_TARGET "out.var.SV_TARGET" | ||
OpName %out_var_SV_TARGET_0 "out.var.SV_TARGET" | ||
OpName %_Z5func1v "_Z5func1v" | ||
OpName %_Z5func2v "_Z5func2v" | ||
OpDecorate %out_var_SV_TARGET Location 0 | ||
OpDecorate %out_var_SV_TARGET_0 Location 0 | ||
OpDecorate %cba DescriptorSet 0 | ||
OpDecorate %cba Binding 0 | ||
OpMemberDecorate %type_cba 0 Offset 0 | ||
OpDecorate %type_cba Block | ||
%int = OpTypeInt 32 1 | ||
%int_0 = OpConstant %int 0 | ||
%float = OpTypeFloat 32 | ||
%v4float = OpTypeVector %float 4 | ||
%type_cba = OpTypeStruct %v4float | ||
%_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba | ||
%_ptr_Output_v4float = OpTypePointer Output %v4float | ||
%void = OpTypeVoid | ||
%14 = OpTypeFunction %void | ||
%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float | ||
%cba = OpVariable %_ptr_Uniform_type_cba Uniform | ||
%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output | ||
%out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output | ||
%_Z5func1v = OpFunction %void None %14 | ||
%16 = OpLabel | ||
%17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0 | ||
%18 = OpLoad %v4float %17 | ||
OpStore %out_var_SV_TARGET %18 | ||
OpReturn | ||
OpFunctionEnd | ||
%_Z5func2v = OpFunction %void None %14 | ||
%19 = OpLabel | ||
%20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0 | ||
%21 = OpLoad %v4float %20 | ||
OpStore %out_var_SV_TARGET_0 %21 | ||
OpReturn | ||
OpFunctionEnd | ||
)"; | ||
|
||
SinglePassRunAndCheck<RemoveUnusedInterfaceVariablesPass>(text, expected, | ||
true, true); | ||
} | ||
|
||
TEST_F(RemoveUnusedInterfaceVariablesTest, FixMissingVariable) { | ||
const std::string text = R"(OpCapability Shader | ||
OpMemoryModel Logical GLSL450 | ||
OpEntryPoint Fragment %_Z5func1v "_Z5func1v" | ||
OpEntryPoint Fragment %_Z5func2v "_Z5func2v" | ||
OpExecutionMode %_Z5func1v OriginUpperLeft | ||
OpExecutionMode %_Z5func2v OriginUpperLeft | ||
OpSource HLSL 630 | ||
OpName %type_cba "type.cba" | ||
OpMemberName %type_cba 0 "color" | ||
OpName %cba "cba" | ||
OpName %out_var_SV_TARGET "out.var.SV_TARGET" | ||
OpName %out_var_SV_TARGET_0 "out.var.SV_TARGET" | ||
OpName %_Z5func1v "_Z5func1v" | ||
OpName %_Z5func2v "_Z5func2v" | ||
OpDecorate %out_var_SV_TARGET Location 0 | ||
OpDecorate %out_var_SV_TARGET_0 Location 0 | ||
OpDecorate %cba DescriptorSet 0 | ||
OpDecorate %cba Binding 0 | ||
OpMemberDecorate %type_cba 0 Offset 0 | ||
OpDecorate %type_cba Block | ||
%int = OpTypeInt 32 1 | ||
%int_0 = OpConstant %int 0 | ||
%float = OpTypeFloat 32 | ||
%v4float = OpTypeVector %float 4 | ||
%type_cba = OpTypeStruct %v4float | ||
%_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba | ||
%_ptr_Output_v4float = OpTypePointer Output %v4float | ||
%void = OpTypeVoid | ||
%14 = OpTypeFunction %void | ||
%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float | ||
%cba = OpVariable %_ptr_Uniform_type_cba Uniform | ||
%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output | ||
%out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output | ||
%_Z5func1v = OpFunction %void None %14 | ||
%16 = OpLabel | ||
%17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0 | ||
%18 = OpLoad %v4float %17 | ||
OpStore %out_var_SV_TARGET %18 | ||
OpReturn | ||
OpFunctionEnd | ||
%_Z5func2v = OpFunction %void None %14 | ||
%19 = OpLabel | ||
%20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0 | ||
%21 = OpLoad %v4float %20 | ||
OpStore %out_var_SV_TARGET_0 %21 | ||
OpReturn | ||
OpFunctionEnd | ||
)"; | ||
|
||
SinglePassRunAndCheck<RemoveUnusedInterfaceVariablesPass>(text, expected, | ||
true, true); | ||
} | ||
|
||
} // namespace | ||
} // namespace opt | ||
} // namespace spvtools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.