| 1 |
// Copyright (c) 2016 Google 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 "test/opt/assembly_builder.h" |
| 16 |
|
| 17 |
#include "test/opt/pass_fixture.h" |
| 18 |
#include "test/opt/pass_utils.h" |
| 19 |
|
| 20 |
namespace spvtools { |
| 21 |
namespace opt { |
| 22 |
namespace { |
| 23 |
|
| 24 |
using AssemblyBuilderTest = PassTest<::testing::Test>; |
| 25 |
|
| 26 |
TEST_F(AssemblyBuilderTest, MinimalShader) { |
| 27 |
AssemblyBuilder builder; |
| 28 |
std::vector<const char*> expected = { |
| 29 |
// clang-format off |
| 30 |
"OpCapability Shader", |
| 31 |
"OpCapability Float64", |
| 32 |
"%1 = OpExtInstImport \"GLSL.std.450\"", |
| 33 |
"OpMemoryModel Logical GLSL450", |
| 34 |
"OpEntryPoint Vertex %main \"main\"", |
| 35 |
"OpName %void \"void\"", |
| 36 |
"OpName %main_func_type \"main_func_type\"", |
| 37 |
"OpName %main \"main\"", |
| 38 |
"OpName %main_func_entry_block \"main_func_entry_block\"", |
| 39 |
"%void = OpTypeVoid", |
| 40 |
"%main_func_type = OpTypeFunction %void", |
| 41 |
"%main = OpFunction %void None %main_func_type", |
| 42 |
"%main_func_entry_block = OpLabel", |
| 43 |
"OpReturn", |
| 44 |
"OpFunctionEnd", |
| 45 |
// clang-format on |
| 46 |
}; |
| 47 |
|
| 48 |
SinglePassRunAndCheck<NullPass>(builder.GetCode(), JoinAllInsts(expected), |
| 49 |
/* skip_nop = */ false); |
| 50 |
} |
| 51 |
|
| 52 |
TEST_F(AssemblyBuilderTest, ShaderWithConstants) { |
| 53 |
AssemblyBuilder builder; |
| 54 |
builder |
| 55 |
.AppendTypesConstantsGlobals({ |
| 56 |
// clang-format off |
| 57 |
"%bool = OpTypeBool", |
| 58 |
"%_PF_bool = OpTypePointer Function %bool", |
| 59 |
"%bt = OpConstantTrue %bool", |
| 60 |
"%bf = OpConstantFalse %bool", |
| 61 |
"%int = OpTypeInt 32 1", |
| 62 |
"%_PF_int = OpTypePointer Function %int", |
| 63 |
"%si = OpConstant %int 1", |
| 64 |
"%uint = OpTypeInt 32 0", |
| 65 |
"%_PF_uint = OpTypePointer Function %uint", |
| 66 |
"%ui = OpConstant %uint 2", |
| 67 |
"%float = OpTypeFloat 32", |
| 68 |
"%_PF_float = OpTypePointer Function %float", |
| 69 |
"%f = OpConstant %float 3.1415", |
| 70 |
"%double = OpTypeFloat 64", |
| 71 |
"%_PF_double = OpTypePointer Function %double", |
| 72 |
"%d = OpConstant %double 3.14159265358979", |
| 73 |
// clang-format on |
| 74 |
}) |
| 75 |
.AppendInMain({ |
| 76 |
// clang-format off |
| 77 |
"%btv = OpVariable %_PF_bool Function", |
| 78 |
"%bfv = OpVariable %_PF_bool Function", |
| 79 |
"%iv = OpVariable %_PF_int Function", |
| 80 |
"%uv = OpVariable %_PF_uint Function", |
| 81 |
"%fv = OpVariable %_PF_float Function", |
| 82 |
"%dv = OpVariable %_PF_double Function", |
| 83 |
"OpStore %btv %bt", |
| 84 |
"OpStore %bfv %bf", |
| 85 |
"OpStore %iv %si", |
| 86 |
"OpStore %uv %ui", |
| 87 |
"OpStore %fv %f", |
| 88 |
"OpStore %dv %d", |
| 89 |
// clang-format on |
| 90 |
}); |
| 91 |
|
| 92 |
std::vector<const char*> expected = { |
| 93 |
// clang-format off |
| 94 |
"OpCapability Shader", |
| 95 |
"OpCapability Float64", |
| 96 |
"%1 = OpExtInstImport \"GLSL.std.450\"", |
| 97 |
"OpMemoryModel Logical GLSL450", |
| 98 |
"OpEntryPoint Vertex %main \"main\"", |
| 99 |
"OpName %void \"void\"", |
| 100 |
"OpName %main_func_type \"main_func_type\"", |
| 101 |
"OpName %main \"main\"", |
| 102 |
"OpName %main_func_entry_block \"main_func_entry_block\"", |
| 103 |
"OpName %bool \"bool\"", |
| 104 |
"OpName %_PF_bool \"_PF_bool\"", |
| 105 |
"OpName %bt \"bt\"", |
| 106 |
"OpName %bf \"bf\"", |
| 107 |
"OpName %int \"int\"", |
| 108 |
"OpName %_PF_int \"_PF_int\"", |
| 109 |
"OpName %si \"si\"", |
| 110 |
"OpName %uint \"uint\"", |
| 111 |
"OpName %_PF_uint \"_PF_uint\"", |
| 112 |
"OpName %ui \"ui\"", |
| 113 |
"OpName %float \"float\"", |
| 114 |
"OpName %_PF_float \"_PF_float\"", |
| 115 |
"OpName %f \"f\"", |
| 116 |
"OpName %double \"double\"", |
| 117 |
"OpName %_PF_double \"_PF_double\"", |
| 118 |
"OpName %d \"d\"", |
| 119 |
"OpName %btv \"btv\"", |
| 120 |
"OpName %bfv \"bfv\"", |
| 121 |
"OpName %iv \"iv\"", |
| 122 |
"OpName %uv \"uv\"", |
| 123 |
"OpName %fv \"fv\"", |
| 124 |
"OpName %dv \"dv\"", |
| 125 |
"%void = OpTypeVoid", |
| 126 |
"%main_func_type = OpTypeFunction %void", |
| 127 |
"%bool = OpTypeBool", |
| 128 |
"%_PF_bool = OpTypePointer Function %bool", |
| 129 |
"%bt = OpConstantTrue %bool", |
| 130 |
"%bf = OpConstantFalse %bool", |
| 131 |
"%int = OpTypeInt 32 1", |
| 132 |
"%_PF_int = OpTypePointer Function %int", |
| 133 |
"%si = OpConstant %int 1", |
| 134 |
"%uint = OpTypeInt 32 0", |
| 135 |
"%_PF_uint = OpTypePointer Function %uint", |
| 136 |
"%ui = OpConstant %uint 2", |
| 137 |
"%float = OpTypeFloat 32", |
| 138 |
"%_PF_float = OpTypePointer Function %float", |
| 139 |
"%f = OpConstant %float 3.1415", |
| 140 |
"%double = OpTypeFloat 64", |
| 141 |
"%_PF_double = OpTypePointer Function %double", |
| 142 |
"%d = OpConstant %double 3.14159265358979", |
| 143 |
"%main = OpFunction %void None %main_func_type", |
| 144 |
"%main_func_entry_block = OpLabel", |
| 145 |
"%btv = OpVariable %_PF_bool Function", |
| 146 |
"%bfv = OpVariable %_PF_bool Function", |
| 147 |
"%iv = OpVariable %_PF_int Function", |
| 148 |
"%uv = OpVariable %_PF_uint Function", |
| 149 |
"%fv = OpVariable %_PF_float Function", |
| 150 |
"%dv = OpVariable %_PF_double Function", |
| 151 |
"OpStore %btv %bt", |
| 152 |
"OpStore %bfv %bf", |
| 153 |
"OpStore %iv %si", |
| 154 |
"OpStore %uv %ui", |
| 155 |
"OpStore %fv %f", |
| 156 |
"OpStore %dv %d", |
| 157 |
"OpReturn", |
| 158 |
"OpFunctionEnd", |
| 159 |
// clang-format on |
| 160 |
}; |
| 161 |
SinglePassRunAndCheck<NullPass>(builder.GetCode(), JoinAllInsts(expected), |
| 162 |
/* skip_nop = */ false); |
| 163 |
} |
| 164 |
|
| 165 |
TEST_F(AssemblyBuilderTest, SpecConstants) { |
| 166 |
AssemblyBuilder builder; |
| 167 |
builder.AppendTypesConstantsGlobals({ |
| 168 |
"%bool = OpTypeBool", |
| 169 |
"%uint = OpTypeInt 32 0", |
| 170 |
"%int = OpTypeInt 32 1", |
| 171 |
"%float = OpTypeFloat 32", |
| 172 |
"%double = OpTypeFloat 64", |
| 173 |
"%v2int = OpTypeVector %int 2", |
| 174 |
|
| 175 |
"%spec_true = OpSpecConstantTrue %bool", |
| 176 |
"%spec_false = OpSpecConstantFalse %bool", |
| 177 |
"%spec_uint = OpSpecConstant %uint 1", |
| 178 |
"%spec_int = OpSpecConstant %int 1", |
| 179 |
"%spec_float = OpSpecConstant %float 1.25", |
| 180 |
"%spec_double = OpSpecConstant %double 1.2345678", |
| 181 |
|
| 182 |
// Spec constants defined below should not have SpecID. |
| 183 |
"%spec_add_op = OpSpecConstantOp %int IAdd %spec_int %spec_int", |
| 184 |
"%spec_vec = OpSpecConstantComposite %v2int %spec_int %spec_int", |
| 185 |
"%spec_vec_x = OpSpecConstantOp %int CompositeExtract %spec_vec 0", |
| 186 |
}); |
| 187 |
std::vector<const char*> expected = { |
| 188 |
// clang-format off |
| 189 |
"OpCapability Shader", |
| 190 |
"OpCapability Float64", |
| 191 |
"%1 = OpExtInstImport \"GLSL.std.450\"", |
| 192 |
"OpMemoryModel Logical GLSL450", |
| 193 |
"OpEntryPoint Vertex %main \"main\"", |
| 194 |
"OpName %void \"void\"", |
| 195 |
"OpName %main_func_type \"main_func_type\"", |
| 196 |
"OpName %main \"main\"", |
| 197 |
"OpName %main_func_entry_block \"main_func_entry_block\"", |
| 198 |
"OpName %bool \"bool\"", |
| 199 |
"OpName %uint \"uint\"", |
| 200 |
"OpName %int \"int\"", |
| 201 |
"OpName %float \"float\"", |
| 202 |
"OpName %double \"double\"", |
| 203 |
"OpName %v2int \"v2int\"", |
| 204 |
"OpName %spec_true \"spec_true\"", |
| 205 |
"OpName %spec_false \"spec_false\"", |
| 206 |
"OpName %spec_uint \"spec_uint\"", |
| 207 |
"OpName %spec_int \"spec_int\"", |
| 208 |
"OpName %spec_float \"spec_float\"", |
| 209 |
"OpName %spec_double \"spec_double\"", |
| 210 |
"OpName %spec_add_op \"spec_add_op\"", |
| 211 |
"OpName %spec_vec \"spec_vec\"", |
| 212 |
"OpName %spec_vec_x \"spec_vec_x\"", |
| 213 |
"OpDecorate %spec_true SpecId 200", |
| 214 |
"OpDecorate %spec_false SpecId 201", |
| 215 |
"OpDecorate %spec_uint SpecId 202", |
| 216 |
"OpDecorate %spec_int SpecId 203", |
| 217 |
"OpDecorate %spec_float SpecId 204", |
| 218 |
"OpDecorate %spec_double SpecId 205", |
| 219 |
"%void = OpTypeVoid", |
| 220 |
"%main_func_type = OpTypeFunction %void", |
| 221 |
"%bool = OpTypeBool", |
| 222 |
"%uint = OpTypeInt 32 0", |
| 223 |
"%int = OpTypeInt 32 1", |
| 224 |
"%float = OpTypeFloat 32", |
| 225 |
"%double = OpTypeFloat 64", |
| 226 |
"%v2int = OpTypeVector %int 2", |
| 227 |
"%spec_true = OpSpecConstantTrue %bool", |
| 228 |
"%spec_false = OpSpecConstantFalse %bool", |
| 229 |
"%spec_uint = OpSpecConstant %uint 1", |
| 230 |
"%spec_int = OpSpecConstant %int 1", |
| 231 |
"%spec_float = OpSpecConstant %float 1.25", |
| 232 |
"%spec_double = OpSpecConstant %double 1.2345678", |
| 233 |
"%spec_add_op = OpSpecConstantOp %int IAdd %spec_int %spec_int", |
| 234 |
"%spec_vec = OpSpecConstantComposite %v2int %spec_int %spec_int", |
| 235 |
"%spec_vec_x = OpSpecConstantOp %int CompositeExtract %spec_vec 0", |
| 236 |
"%main = OpFunction %void None %main_func_type", |
| 237 |
"%main_func_entry_block = OpLabel", |
| 238 |
"OpReturn", |
| 239 |
"OpFunctionEnd", |
| 240 |
|
| 241 |
// clang-format on |
| 242 |
}; |
| 243 |
|
| 244 |
SinglePassRunAndCheck<NullPass>(builder.GetCode(), JoinAllInsts(expected), |
| 245 |
/* skip_nop = */ false); |
| 246 |
} |
| 247 |
|
| 248 |
TEST_F(AssemblyBuilderTest, AppendNames) { |
| 249 |
AssemblyBuilder builder; |
| 250 |
builder.AppendNames({ |
| 251 |
"OpName %void \"another_name_for_void\"", |
| 252 |
"I am an invalid OpName instruction and should not be added", |
| 253 |
"OpName %main \"another name for main\"", |
| 254 |
}); |
| 255 |
std::vector<const char*> expected = { |
| 256 |
// clang-format off |
| 257 |
"OpCapability Shader", |
| 258 |
"OpCapability Float64", |
| 259 |
"%1 = OpExtInstImport \"GLSL.std.450\"", |
| 260 |
"OpMemoryModel Logical GLSL450", |
| 261 |
"OpEntryPoint Vertex %main \"main\"", |
| 262 |
"OpName %void \"void\"", |
| 263 |
"OpName %main_func_type \"main_func_type\"", |
| 264 |
"OpName %main \"main\"", |
| 265 |
"OpName %main_func_entry_block \"main_func_entry_block\"", |
| 266 |
"OpName %void \"another_name_for_void\"", |
| 267 |
"OpName %main \"another name for main\"", |
| 268 |
"%void = OpTypeVoid", |
| 269 |
"%main_func_type = OpTypeFunction %void", |
| 270 |
"%main = OpFunction %void None %main_func_type", |
| 271 |
"%main_func_entry_block = OpLabel", |
| 272 |
"OpReturn", |
| 273 |
"OpFunctionEnd", |
| 274 |
// clang-format on |
| 275 |
}; |
| 276 |
|
| 277 |
SinglePassRunAndCheck<NullPass>(builder.GetCode(), JoinAllInsts(expected), |
| 278 |
/* skip_nop = */ false); |
| 279 |
} |
| 280 |
|
| 281 |
} // namespace |
| 282 |
} // namespace opt |
| 283 |
} // namespace spvtools |