• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2022 Advanced Micro Devices, 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 "gmock/gmock.h"
16 #include "test/opt/pass_fixture.h"
17 #include "test/opt/pass_utils.h"
18 
19 namespace spvtools {
20 namespace opt {
21 namespace {
22 
23 using FixFuncCallArgumentsTest = PassTest<::testing::Test>;
TEST_F(FixFuncCallArgumentsTest,Simple)24 TEST_F(FixFuncCallArgumentsTest, Simple) {
25   const std::string text = R"(
26 ;
27 ; CHECK: [[v0:%\w+]] = OpVariable %_ptr_Function_float Function
28 ; CHECK: [[v1:%\w+]] = OpVariable %_ptr_Function_float Function
29 ; CHECK: [[v2:%\w+]] = OpVariable %_ptr_Function_T Function
30 ; CHECK: [[ac0:%\w+]] = OpAccessChain %_ptr_Function_float %t %int_0
31 ; CHECK: [[ac1:%\w+]] = OpAccessChain %_ptr_Uniform_float %r1 %int_0 %uint_0
32 ; CHECK: [[ld0:%\w+]] = OpLoad %float [[ac0]]
33 ; CHECK:                OpStore [[v1]] [[ld0]]
34 ; CHECK: [[ld1:%\w+]] = OpLoad %float [[ac1]]
35 ; CHECK:                OpStore [[v0]] [[ld1]]
36 ; CHECK: [[func:%\w+]] = OpFunctionCall %void %fn [[v1]] [[v0]]
37 ; CHECK: [[ld2:%\w+]] = OpLoad %float [[v0]]
38 ; CHECK: OpStore [[ac1]] [[ld2]]
39 ; CHECK: [[ld3:%\w+]] = OpLoad %float [[v1]]
40 ; CHECK: OpStore [[ac0]] [[ld3]]
41 ;
42 OpCapability Shader
43 OpCapability Linkage
44 OpMemoryModel Logical GLSL450
45 OpSource HLSL 630
46 OpName %type_RWStructuredBuffer_float "type.RWStructuredBuffer.float"
47 OpName %r1 "r1"
48 OpName %type_ACSBuffer_counter "type.ACSBuffer.counter"
49 OpMemberName %type_ACSBuffer_counter 0 "counter"
50 OpName %counter_var_r1 "counter.var.r1"
51 OpName %main "main"
52 OpName %bb_entry "bb.entry"
53 OpName %T "T"
54 OpMemberName %T 0 "t0"
55 OpName %t "t"
56 OpName %fn "fn"
57 OpName %p0 "p0"
58 OpName %p2 "p2"
59 OpName %bb_entry_0 "bb.entry"
60 OpDecorate %main LinkageAttributes "main" Export
61 OpDecorate %r1 DescriptorSet 0
62 OpDecorate %r1 Binding 0
63 OpDecorate %counter_var_r1 DescriptorSet 0
64 OpDecorate %counter_var_r1 Binding 1
65 OpDecorate %_runtimearr_float ArrayStride 4
66 OpMemberDecorate %type_RWStructuredBuffer_float 0 Offset 0
67 OpDecorate %type_RWStructuredBuffer_float BufferBlock
68 OpMemberDecorate %type_ACSBuffer_counter 0 Offset 0
69 OpDecorate %type_ACSBuffer_counter BufferBlock
70 %int = OpTypeInt 32 1
71 %int_0 = OpConstant %int 0
72 %uint = OpTypeInt 32 0
73 %uint_0 = OpConstant %uint 0
74 %int_1 = OpConstant %int 1
75 %float = OpTypeFloat 32
76 %_runtimearr_float = OpTypeRuntimeArray %float
77 %type_RWStructuredBuffer_float = OpTypeStruct %_runtimearr_float
78 %_ptr_Uniform_type_RWStructuredBuffer_float = OpTypePointer Uniform %type_RWStructuredBuffer_float
79 %type_ACSBuffer_counter = OpTypeStruct %int
80 %_ptr_Uniform_type_ACSBuffer_counter = OpTypePointer Uniform %type_ACSBuffer_counter
81 %15 = OpTypeFunction %int
82 %T = OpTypeStruct %float
83 %_ptr_Function_T = OpTypePointer Function %T
84 %_ptr_Function_float = OpTypePointer Function %float
85 %_ptr_Uniform_float = OpTypePointer Uniform %float
86 %void = OpTypeVoid
87 %27 = OpTypeFunction %void %_ptr_Function_float %_ptr_Function_float
88 %r1 = OpVariable %_ptr_Uniform_type_RWStructuredBuffer_float Uniform
89 %counter_var_r1 = OpVariable %_ptr_Uniform_type_ACSBuffer_counter Uniform
90 %main = OpFunction %int None %15
91 %bb_entry = OpLabel
92 %t = OpVariable %_ptr_Function_T Function
93 %21 = OpAccessChain %_ptr_Function_float %t %int_0
94 %23 = OpAccessChain %_ptr_Uniform_float %r1 %int_0 %uint_0
95 %25 = OpFunctionCall %void %fn %21 %23
96 OpReturnValue %int_1
97 OpFunctionEnd
98 %fn = OpFunction %void DontInline %27
99 %p0 = OpFunctionParameter %_ptr_Function_float
100 %p2 = OpFunctionParameter %_ptr_Function_float
101 %bb_entry_0 = OpLabel
102 OpReturn
103 OpFunctionEnd
104 )";
105 
106   SinglePassRunAndMatch<FixFuncCallArgumentsPass>(text, true);
107 }
108 
TEST_F(FixFuncCallArgumentsTest,NotAccessChainInput)109 TEST_F(FixFuncCallArgumentsTest, NotAccessChainInput) {
110   const std::string text = R"(
111 ;
112 ; CHECK: [[o:%\w+]] = OpCopyObject %_ptr_Function_float %t
113 ; CHECK: [[func:%\w+]] = OpFunctionCall %void %fn [[o]]
114 ;
115 OpCapability Shader
116 OpCapability Linkage
117 OpMemoryModel Logical GLSL450
118 OpSource HLSL 630
119 OpName %main "main"
120 OpName %bb_entry "bb.entry"
121 OpName %t "t"
122 OpName %fn "fn"
123 OpName %p0 "p0"
124 OpName %bb_entry_0 "bb.entry"
125 OpDecorate %main LinkageAttributes "main" Export
126 %int = OpTypeInt 32 1
127 %int_1 = OpConstant %int 1
128 %4 = OpTypeFunction %int
129 %float = OpTypeFloat 32
130 %_ptr_Function_float = OpTypePointer Function %float
131 %void = OpTypeVoid
132 %12 = OpTypeFunction %void %_ptr_Function_float
133 %main = OpFunction %int None %4
134 %bb_entry = OpLabel
135 %t = OpVariable %_ptr_Function_float Function
136 %t1 = OpCopyObject %_ptr_Function_float %t
137 %10 = OpFunctionCall %void %fn %t1
138 OpReturnValue %int_1
139 OpFunctionEnd
140 %fn = OpFunction %void DontInline %12
141 %p0 = OpFunctionParameter %_ptr_Function_float
142 %bb_entry_0 = OpLabel
143 OpReturn
144 OpFunctionEnd
145 )";
146 
147   SinglePassRunAndMatch<FixFuncCallArgumentsPass>(text, false);
148 }
149 
150 }  // namespace
151 }  // namespace opt
152 }  // namespace spvtools