File: hoist_access_chains.cpp

package info (click to toggle)
spirv-tools 2025.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,588 kB
  • sloc: cpp: 470,407; javascript: 5,893; python: 3,326; ansic: 488; sh: 450; ruby: 88; makefile: 18; lisp: 9
file content (157 lines) | stat: -rw-r--r-- 4,610 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) 2023 The Khronos Group 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 <string>

#include "gmock/gmock.h"
#include "source/opt/licm_pass.h"
#include "test/opt/pass_fixture.h"

namespace spvtools {
namespace opt {
namespace {

using PassClassTest = PassTest<::testing::Test>;

/*
  Tests for the LICM pass to check it handles access chains correctly

  Generated from the following GLSL fragment shader
--eliminate-local-multi-store has also been run on the spv binary
#version 460
void main() {
  for (uint i = 0; i < 123u; ++i) {
    vec2 do_not_hoist_store = vec2(0.0f);
    float do_not_hoist_access_chain_load = do_not_hoist_store.x;
  }
}
*/

TEST_F(PassClassTest, HoistAccessChains) {
  const std::string before_hoist = R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main"
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 460
OpName %main "main"
OpName %i "i"
OpName %do_not_hoist_store "do_not_hoist_store"
OpName %do_not_hoist_access_chain_load "do_not_hoist_access_chain_load"
%void = OpTypeVoid
%7 = OpTypeFunction %void
%uint = OpTypeInt 32 0
%_ptr_Function_uint = OpTypePointer Function %uint
%uint_0 = OpConstant %uint 0
%uint_123 = OpConstant %uint 123
%bool = OpTypeBool
%float = OpTypeFloat 32
%v2float = OpTypeVector %float 2
%_ptr_Function_v2float = OpTypePointer Function %v2float
%float_0 = OpConstant %float 0
%17 = OpConstantComposite %v2float %float_0 %float_0
%_ptr_Function_float = OpTypePointer Function %float
%int = OpTypeInt 32 1
%int_1 = OpConstant %int 1
%main = OpFunction %void None %7
%21 = OpLabel
%i = OpVariable %_ptr_Function_uint Function
%do_not_hoist_store = OpVariable %_ptr_Function_v2float Function
%do_not_hoist_access_chain_load = OpVariable %_ptr_Function_float Function
OpStore %i %uint_0
OpBranch %22
%22 = OpLabel
OpLoopMerge %23 %24 None
OpBranch %25
%25 = OpLabel
%26 = OpLoad %uint %i
%27 = OpULessThan %bool %26 %uint_123
OpBranchConditional %27 %28 %23
%28 = OpLabel
OpStore %do_not_hoist_store %17
%29 = OpAccessChain %_ptr_Function_float %do_not_hoist_store %uint_0
%30 = OpLoad %float %29
OpStore %do_not_hoist_access_chain_load %30
OpBranch %24
%24 = OpLabel
%31 = OpLoad %uint %i
%32 = OpIAdd %uint %31 %int_1
OpStore %i %32
OpBranch %22
%23 = OpLabel
OpReturn
OpFunctionEnd
)";

  const std::string after_hoist = R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main"
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 460
OpName %main "main"
OpName %i "i"
OpName %do_not_hoist_store "do_not_hoist_store"
OpName %do_not_hoist_access_chain_load "do_not_hoist_access_chain_load"
%void = OpTypeVoid
%7 = OpTypeFunction %void
%uint = OpTypeInt 32 0
%_ptr_Function_uint = OpTypePointer Function %uint
%uint_0 = OpConstant %uint 0
%uint_123 = OpConstant %uint 123
%bool = OpTypeBool
%float = OpTypeFloat 32
%v2float = OpTypeVector %float 2
%_ptr_Function_v2float = OpTypePointer Function %v2float
%float_0 = OpConstant %float 0
%17 = OpConstantComposite %v2float %float_0 %float_0
%_ptr_Function_float = OpTypePointer Function %float
%int = OpTypeInt 32 1
%int_1 = OpConstant %int 1
%main = OpFunction %void None %7
%21 = OpLabel
%i = OpVariable %_ptr_Function_uint Function
%do_not_hoist_store = OpVariable %_ptr_Function_v2float Function
%do_not_hoist_access_chain_load = OpVariable %_ptr_Function_float Function
OpStore %i %uint_0
%29 = OpAccessChain %_ptr_Function_float %do_not_hoist_store %uint_0
OpBranch %22
%22 = OpLabel
OpLoopMerge %23 %24 None
OpBranch %25
%25 = OpLabel
%26 = OpLoad %uint %i
%27 = OpULessThan %bool %26 %uint_123
OpBranchConditional %27 %28 %23
%28 = OpLabel
OpStore %do_not_hoist_store %17
%30 = OpLoad %float %29
OpStore %do_not_hoist_access_chain_load %30
OpBranch %24
%24 = OpLabel
%31 = OpLoad %uint %i
%32 = OpIAdd %uint %31 %int_1
OpStore %i %32
OpBranch %22
%23 = OpLabel
OpReturn
OpFunctionEnd
)";

  SinglePassRunAndCheck<LICMPass>(before_hoist, after_hoist, true);
}

}  // namespace
}  // namespace opt
}  // namespace spvtools