1// Copyright (c) 2021 ZHOU He 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 "test/opt/assembly_builder.h" 16#include "test/opt/pass_fixture.h" 17#include "test/opt/pass_utils.h" 18 19namespace spvtools { 20namespace opt { 21namespace { 22 23using RemoveUnusedInterfaceVariablesTest = PassTest<::testing::Test>; 24 25static const std::string expected = R"(OpCapability Shader 26OpMemoryModel Logical GLSL450 27OpEntryPoint Fragment %_Z5func1v "_Z5func1v" %out_var_SV_TARGET 28OpEntryPoint Fragment %_Z5func2v "_Z5func2v" %out_var_SV_TARGET_0 29OpExecutionMode %_Z5func1v OriginUpperLeft 30OpExecutionMode %_Z5func2v OriginUpperLeft 31OpSource HLSL 630 32OpName %type_cba "type.cba" 33OpMemberName %type_cba 0 "color" 34OpName %cba "cba" 35OpName %out_var_SV_TARGET "out.var.SV_TARGET" 36OpName %out_var_SV_TARGET_0 "out.var.SV_TARGET" 37OpName %_Z5func1v "_Z5func1v" 38OpName %_Z5func2v "_Z5func2v" 39OpDecorate %out_var_SV_TARGET Location 0 40OpDecorate %out_var_SV_TARGET_0 Location 0 41OpDecorate %cba DescriptorSet 0 42OpDecorate %cba Binding 0 43OpMemberDecorate %type_cba 0 Offset 0 44OpDecorate %type_cba Block 45%int = OpTypeInt 32 1 46%int_0 = OpConstant %int 0 47%float = OpTypeFloat 32 48%v4float = OpTypeVector %float 4 49%type_cba = OpTypeStruct %v4float 50%_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba 51%_ptr_Output_v4float = OpTypePointer Output %v4float 52%void = OpTypeVoid 53%14 = OpTypeFunction %void 54%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float 55%cba = OpVariable %_ptr_Uniform_type_cba Uniform 56%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output 57%out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output 58%_Z5func1v = OpFunction %void None %14 59%16 = OpLabel 60%17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0 61%18 = OpLoad %v4float %17 62OpStore %out_var_SV_TARGET %18 63OpReturn 64OpFunctionEnd 65%_Z5func2v = OpFunction %void None %14 66%19 = OpLabel 67%20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0 68%21 = OpLoad %v4float %20 69OpStore %out_var_SV_TARGET_0 %21 70OpReturn 71OpFunctionEnd 72)"; 73 74TEST_F(RemoveUnusedInterfaceVariablesTest, RemoveUnusedVariable) { 75 const std::string text = R"(OpCapability Shader 76OpMemoryModel Logical GLSL450 77OpEntryPoint Fragment %_Z5func1v "_Z5func1v" %out_var_SV_TARGET %out_var_SV_TARGET_0 78OpEntryPoint Fragment %_Z5func2v "_Z5func2v" %out_var_SV_TARGET %out_var_SV_TARGET_0 79OpExecutionMode %_Z5func1v OriginUpperLeft 80OpExecutionMode %_Z5func2v OriginUpperLeft 81OpSource HLSL 630 82OpName %type_cba "type.cba" 83OpMemberName %type_cba 0 "color" 84OpName %cba "cba" 85OpName %out_var_SV_TARGET "out.var.SV_TARGET" 86OpName %out_var_SV_TARGET_0 "out.var.SV_TARGET" 87OpName %_Z5func1v "_Z5func1v" 88OpName %_Z5func2v "_Z5func2v" 89OpDecorate %out_var_SV_TARGET Location 0 90OpDecorate %out_var_SV_TARGET_0 Location 0 91OpDecorate %cba DescriptorSet 0 92OpDecorate %cba Binding 0 93OpMemberDecorate %type_cba 0 Offset 0 94OpDecorate %type_cba Block 95%int = OpTypeInt 32 1 96%int_0 = OpConstant %int 0 97%float = OpTypeFloat 32 98%v4float = OpTypeVector %float 4 99%type_cba = OpTypeStruct %v4float 100%_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba 101%_ptr_Output_v4float = OpTypePointer Output %v4float 102%void = OpTypeVoid 103%14 = OpTypeFunction %void 104%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float 105%cba = OpVariable %_ptr_Uniform_type_cba Uniform 106%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output 107%out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output 108%_Z5func1v = OpFunction %void None %14 109%16 = OpLabel 110%17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0 111%18 = OpLoad %v4float %17 112OpStore %out_var_SV_TARGET %18 113OpReturn 114OpFunctionEnd 115%_Z5func2v = OpFunction %void None %14 116%19 = OpLabel 117%20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0 118%21 = OpLoad %v4float %20 119OpStore %out_var_SV_TARGET_0 %21 120OpReturn 121OpFunctionEnd 122)"; 123 124 SinglePassRunAndCheck<RemoveUnusedInterfaceVariablesPass>(text, expected, 125 true, true); 126} 127 128TEST_F(RemoveUnusedInterfaceVariablesTest, FixMissingVariable) { 129 const std::string text = R"(OpCapability Shader 130OpMemoryModel Logical GLSL450 131OpEntryPoint Fragment %_Z5func1v "_Z5func1v" 132OpEntryPoint Fragment %_Z5func2v "_Z5func2v" 133OpExecutionMode %_Z5func1v OriginUpperLeft 134OpExecutionMode %_Z5func2v OriginUpperLeft 135OpSource HLSL 630 136OpName %type_cba "type.cba" 137OpMemberName %type_cba 0 "color" 138OpName %cba "cba" 139OpName %out_var_SV_TARGET "out.var.SV_TARGET" 140OpName %out_var_SV_TARGET_0 "out.var.SV_TARGET" 141OpName %_Z5func1v "_Z5func1v" 142OpName %_Z5func2v "_Z5func2v" 143OpDecorate %out_var_SV_TARGET Location 0 144OpDecorate %out_var_SV_TARGET_0 Location 0 145OpDecorate %cba DescriptorSet 0 146OpDecorate %cba Binding 0 147OpMemberDecorate %type_cba 0 Offset 0 148OpDecorate %type_cba Block 149%int = OpTypeInt 32 1 150%int_0 = OpConstant %int 0 151%float = OpTypeFloat 32 152%v4float = OpTypeVector %float 4 153%type_cba = OpTypeStruct %v4float 154%_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba 155%_ptr_Output_v4float = OpTypePointer Output %v4float 156%void = OpTypeVoid 157%14 = OpTypeFunction %void 158%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float 159%cba = OpVariable %_ptr_Uniform_type_cba Uniform 160%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output 161%out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output 162%_Z5func1v = OpFunction %void None %14 163%16 = OpLabel 164%17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0 165%18 = OpLoad %v4float %17 166OpStore %out_var_SV_TARGET %18 167OpReturn 168OpFunctionEnd 169%_Z5func2v = OpFunction %void None %14 170%19 = OpLabel 171%20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0 172%21 = OpLoad %v4float %20 173OpStore %out_var_SV_TARGET_0 %21 174OpReturn 175OpFunctionEnd 176)"; 177 178 SinglePassRunAndCheck<RemoveUnusedInterfaceVariablesPass>(text, expected, 179 true, true); 180} 181 182} // namespace 183} // namespace opt 184} // namespace spvtools 185