| 1 |
// Copyright (c) 2023 The Khronos Group Inc. |
| 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 <string> |
| 16 |
|
| 17 |
#include "gmock/gmock.h" |
| 18 |
#include "source/opt/licm_pass.h" |
| 19 |
#include "test/opt/pass_fixture.h" |
| 20 |
|
| 21 |
namespace spvtools { |
| 22 |
namespace opt { |
| 23 |
namespace { |
| 24 |
|
| 25 |
using PassClassTest = PassTest<::testing::Test>; |
| 26 |
|
| 27 |
/* |
| 28 |
Tests for the LICM pass to check it handles access chains correctly |
| 29 |
|
| 30 |
Generated from the following GLSL fragment shader |
| 31 |
--eliminate-local-multi-store has also been run on the spv binary |
| 32 |
#version 460 |
| 33 |
void main() { |
| 34 |
for (uint i = 0; i < 123u; ++i) { |
| 35 |
vec2 do_not_hoist_store = vec2(0.0f); |
| 36 |
float do_not_hoist_access_chain_load = do_not_hoist_store.x; |
| 37 |
} |
| 38 |
} |
| 39 |
*/ |
| 40 |
|
| 41 |
TEST_F(PassClassTest, HoistAccessChains) { |
| 42 |
const std::string before_hoist = R"(OpCapability Shader |
| 43 |
%1 = OpExtInstImport "GLSL.std.450" |
| 44 |
OpMemoryModel Logical GLSL450 |
| 45 |
OpEntryPoint Fragment %main "main" |
| 46 |
OpExecutionMode %main OriginUpperLeft |
| 47 |
OpSource GLSL 460 |
| 48 |
OpName %main "main" |
| 49 |
OpName %i "i" |
| 50 |
OpName %do_not_hoist_store "do_not_hoist_store" |
| 51 |
OpName %do_not_hoist_access_chain_load "do_not_hoist_access_chain_load" |
| 52 |
%void = OpTypeVoid |
| 53 |
%7 = OpTypeFunction %void |
| 54 |
%uint = OpTypeInt 32 0 |
| 55 |
%_ptr_Function_uint = OpTypePointer Function %uint |
| 56 |
%uint_0 = OpConstant %uint 0 |
| 57 |
%uint_123 = OpConstant %uint 123 |
| 58 |
%bool = OpTypeBool |
| 59 |
%float = OpTypeFloat 32 |
| 60 |
%v2float = OpTypeVector %float 2 |
| 61 |
%_ptr_Function_v2float = OpTypePointer Function %v2float |
| 62 |
%float_0 = OpConstant %float 0 |
| 63 |
%17 = OpConstantComposite %v2float %float_0 %float_0 |
| 64 |
%_ptr_Function_float = OpTypePointer Function %float |
| 65 |
%int = OpTypeInt 32 1 |
| 66 |
%int_1 = OpConstant %int 1 |
| 67 |
%main = OpFunction %void None %7 |
| 68 |
%21 = OpLabel |
| 69 |
%i = OpVariable %_ptr_Function_uint Function |
| 70 |
%do_not_hoist_store = OpVariable %_ptr_Function_v2float Function |
| 71 |
%do_not_hoist_access_chain_load = OpVariable %_ptr_Function_float Function |
| 72 |
OpStore %i %uint_0 |
| 73 |
OpBranch %22 |
| 74 |
%22 = OpLabel |
| 75 |
OpLoopMerge %23 %24 None |
| 76 |
OpBranch %25 |
| 77 |
%25 = OpLabel |
| 78 |
%26 = OpLoad %uint %i |
| 79 |
%27 = OpULessThan %bool %26 %uint_123 |
| 80 |
OpBranchConditional %27 %28 %23 |
| 81 |
%28 = OpLabel |
| 82 |
OpStore %do_not_hoist_store %17 |
| 83 |
%29 = OpAccessChain %_ptr_Function_float %do_not_hoist_store %uint_0 |
| 84 |
%30 = OpLoad %float %29 |
| 85 |
OpStore %do_not_hoist_access_chain_load %30 |
| 86 |
OpBranch %24 |
| 87 |
%24 = OpLabel |
| 88 |
%31 = OpLoad %uint %i |
| 89 |
%32 = OpIAdd %uint %31 %int_1 |
| 90 |
OpStore %i %32 |
| 91 |
OpBranch %22 |
| 92 |
%23 = OpLabel |
| 93 |
OpReturn |
| 94 |
OpFunctionEnd |
| 95 |
)"; |
| 96 |
|
| 97 |
const std::string after_hoist = R"(OpCapability Shader |
| 98 |
%1 = OpExtInstImport "GLSL.std.450" |
| 99 |
OpMemoryModel Logical GLSL450 |
| 100 |
OpEntryPoint Fragment %main "main" |
| 101 |
OpExecutionMode %main OriginUpperLeft |
| 102 |
OpSource GLSL 460 |
| 103 |
OpName %main "main" |
| 104 |
OpName %i "i" |
| 105 |
OpName %do_not_hoist_store "do_not_hoist_store" |
| 106 |
OpName %do_not_hoist_access_chain_load "do_not_hoist_access_chain_load" |
| 107 |
%void = OpTypeVoid |
| 108 |
%7 = OpTypeFunction %void |
| 109 |
%uint = OpTypeInt 32 0 |
| 110 |
%_ptr_Function_uint = OpTypePointer Function %uint |
| 111 |
%uint_0 = OpConstant %uint 0 |
| 112 |
%uint_123 = OpConstant %uint 123 |
| 113 |
%bool = OpTypeBool |
| 114 |
%float = OpTypeFloat 32 |
| 115 |
%v2float = OpTypeVector %float 2 |
| 116 |
%_ptr_Function_v2float = OpTypePointer Function %v2float |
| 117 |
%float_0 = OpConstant %float 0 |
| 118 |
%17 = OpConstantComposite %v2float %float_0 %float_0 |
| 119 |
%_ptr_Function_float = OpTypePointer Function %float |
| 120 |
%int = OpTypeInt 32 1 |
| 121 |
%int_1 = OpConstant %int 1 |
| 122 |
%main = OpFunction %void None %7 |
| 123 |
%21 = OpLabel |
| 124 |
%i = OpVariable %_ptr_Function_uint Function |
| 125 |
%do_not_hoist_store = OpVariable %_ptr_Function_v2float Function |
| 126 |
%do_not_hoist_access_chain_load = OpVariable %_ptr_Function_float Function |
| 127 |
OpStore %i %uint_0 |
| 128 |
%29 = OpAccessChain %_ptr_Function_float %do_not_hoist_store %uint_0 |
| 129 |
OpBranch %22 |
| 130 |
%22 = OpLabel |
| 131 |
OpLoopMerge %23 %24 None |
| 132 |
OpBranch %25 |
| 133 |
%25 = OpLabel |
| 134 |
%26 = OpLoad %uint %i |
| 135 |
%27 = OpULessThan %bool %26 %uint_123 |
| 136 |
OpBranchConditional %27 %28 %23 |
| 137 |
%28 = OpLabel |
| 138 |
OpStore %do_not_hoist_store %17 |
| 139 |
%30 = OpLoad %float %29 |
| 140 |
OpStore %do_not_hoist_access_chain_load %30 |
| 141 |
OpBranch %24 |
| 142 |
%24 = OpLabel |
| 143 |
%31 = OpLoad %uint %i |
| 144 |
%32 = OpIAdd %uint %31 %int_1 |
| 145 |
OpStore %i %32 |
| 146 |
OpBranch %22 |
| 147 |
%23 = OpLabel |
| 148 |
OpReturn |
| 149 |
OpFunctionEnd |
| 150 |
)"; |
| 151 |
|
| 152 |
SinglePassRunAndCheck<LICMPass>(before_hoist, after_hoist, true); |
| 153 |
} |
| 154 |
|
| 155 |
} // namespace |
| 156 |
} // namespace opt |
| 157 |
} // namespace spvtools |