fix_func_call_arguments_test.cpp 5.0 KB

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