| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266 |
- // Copyright (c) 2022 Google LLC
- //
- // 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 "test/opt/pass_fixture.h"
- #include "test/opt/pass_utils.h"
- namespace spvtools {
- namespace opt {
- namespace {
- struct ExecutionModelAndBuiltIn {
- const char* execution_model;
- const char* built_in;
- const bool use_v4uint;
- };
- using AddVolatileDecorationTest =
- PassTest<::testing::TestWithParam<ExecutionModelAndBuiltIn>>;
- TEST_P(AddVolatileDecorationTest, InMain) {
- const auto& tc = GetParam();
- const std::string execution_model(tc.execution_model);
- const std::string built_in(tc.built_in);
- const std::string var_type =
- tc.use_v4uint ? "%_ptr_Input_v4uint" : "%_ptr_Input_uint";
- const std::string var_load_type = tc.use_v4uint ? "%v4uint" : "%uint";
- const std::string text =
- std::string(R"(OpCapability RuntimeDescriptorArray
- OpCapability RayTracingKHR
- OpCapability SubgroupBallotKHR
- OpExtension "SPV_EXT_descriptor_indexing"
- OpExtension "SPV_KHR_ray_tracing"
- OpExtension "SPV_KHR_shader_ballot"
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint )") +
- execution_model + std::string(R"( %main "main" %var
- OpSource GLSL 460
- OpSourceExtension "GL_EXT_nonuniform_qualifier"
- OpSourceExtension "GL_KHR_ray_tracing"
- OpName %main "main"
- OpName %fn "fn"
- OpName %StorageBuffer "StorageBuffer"
- OpMemberName %StorageBuffer 0 "index"
- OpMemberName %StorageBuffer 1 "red"
- OpName %sbo "sbo"
- OpName %images "images"
- OpMemberDecorate %StorageBuffer 0 Offset 0
- OpMemberDecorate %StorageBuffer 1 Offset 4
- OpDecorate %StorageBuffer BufferBlock
- OpDecorate %sbo DescriptorSet 0
- OpDecorate %sbo Binding 0
- OpDecorate %images DescriptorSet 0
- OpDecorate %images Binding 1
- OpDecorate %images NonWritable
- )") + std::string(R"(
- ; CHECK: OpDecorate [[var:%\w+]] BuiltIn )") +
- built_in + std::string(R"(
- ; CHECK: OpDecorate [[var]] Volatile
- OpDecorate %var BuiltIn )") + built_in + std::string(R"(
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %StorageBuffer = OpTypeStruct %uint %float
- %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
- %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
- %_runtimearr_13 = OpTypeRuntimeArray %13
- %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
- %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
- %_ptr_Input_uint = OpTypePointer Input %uint
- %v4uint = OpTypeVector %uint 4
- %_ptr_Input_v4uint = OpTypePointer Input %v4uint
- %var = OpVariable )") +
- var_type + std::string(R"( Input
- %int_0 = OpConstant %int 0
- %_ptr_Uniform_uint = OpTypePointer Uniform %uint
- %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
- %v2int = OpTypeVector %int 2
- %25 = OpConstantComposite %v2int %int_0 %int_0
- %v4float = OpTypeVector %float 4
- %uint_0 = OpConstant %uint 0
- %_ptr_Uniform_float = OpTypePointer Uniform %float
- %main = OpFunction %void None %3
- %5 = OpLabel
- %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
- %20 = OpLoad %uint %19
- %load = OpLoad )") + var_load_type + std::string(R"( %var
- %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
- %23 = OpLoad %13 %22
- %27 = OpImageRead %v4float %23 %25
- %29 = OpCompositeExtract %float %27 0
- %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
- OpStore %31 %29
- %32 = OpFunctionCall %void %fn
- OpReturn
- OpFunctionEnd
- %fn = OpFunction %void None %3
- %33 = OpLabel
- OpReturn
- OpFunctionEnd
- )");
- SinglePassRunAndMatch<SpreadVolatileSemantics>(text, true);
- }
- INSTANTIATE_TEST_SUITE_P(
- AddVolatileDecoration, AddVolatileDecorationTest,
- ::testing::ValuesIn(std::vector<ExecutionModelAndBuiltIn>{
- {"RayGenerationKHR", "SubgroupSize", false},
- {"RayGenerationKHR", "SubgroupLocalInvocationId", false},
- {"RayGenerationKHR", "SubgroupEqMask", true},
- {"ClosestHitKHR", "SubgroupLocalInvocationId", true},
- {"IntersectionKHR", "SubgroupEqMask", true},
- {"MissKHR", "SubgroupGeMask", true},
- {"CallableKHR", "SubgroupGtMask", true},
- {"RayGenerationKHR", "SubgroupLeMask", true},
- }));
- using SetLoadVolatileTest =
- PassTest<::testing::TestWithParam<ExecutionModelAndBuiltIn>>;
- TEST_P(SetLoadVolatileTest, InMain) {
- const auto& tc = GetParam();
- const std::string execution_model(tc.execution_model);
- const std::string built_in(tc.built_in);
- const std::string var_type =
- tc.use_v4uint ? "%_ptr_Input_v4uint" : "%_ptr_Input_uint";
- const std::string var_value = tc.use_v4uint ? std::string(R"(
- ; CHECK: [[ptr:%\w+]] = OpAccessChain %_ptr_Input_uint [[var]] %int_0
- ; CHECK: OpLoad {{%\w+}} [[ptr]] Volatile
- %ptr = OpAccessChain %_ptr_Input_uint %var %int_0
- %var_value = OpLoad %uint %ptr)")
- : std::string(R"(
- ; CHECK: OpLoad {{%\w+}} [[var]] Volatile
- %var_value = OpLoad %uint %var)");
- const std::string text = std::string(R"(OpCapability RuntimeDescriptorArray
- OpCapability RayTracingKHR
- OpCapability SubgroupBallotKHR
- OpCapability VulkanMemoryModel
- OpExtension "SPV_KHR_vulkan_memory_model"
- OpExtension "SPV_EXT_descriptor_indexing"
- OpExtension "SPV_KHR_ray_tracing"
- OpExtension "SPV_KHR_shader_ballot"
- OpMemoryModel Logical Vulkan
- OpEntryPoint )") + execution_model +
- std::string(R"( %main "main" %var
- OpName %main "main"
- OpName %StorageBuffer "StorageBuffer"
- OpMemberName %StorageBuffer 0 "index"
- OpMemberName %StorageBuffer 1 "red"
- OpName %sbo "sbo"
- OpName %images "images"
- OpMemberDecorate %StorageBuffer 0 Offset 0
- OpMemberDecorate %StorageBuffer 1 Offset 4
- OpDecorate %StorageBuffer BufferBlock
- OpDecorate %sbo DescriptorSet 0
- OpDecorate %sbo Binding 0
- OpDecorate %images DescriptorSet 0
- OpDecorate %images Binding 1
- OpDecorate %images NonWritable
- )") + std::string(R"(
- ; CHECK: OpDecorate [[var:%\w+]] BuiltIn )") +
- built_in + std::string(R"(
- OpDecorate %var BuiltIn )") + built_in +
- std::string(R"(
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %StorageBuffer = OpTypeStruct %uint %float
- %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
- %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
- %_runtimearr_13 = OpTypeRuntimeArray %13
- %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
- %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
- %_ptr_Input_uint = OpTypePointer Input %uint
- %v4uint = OpTypeVector %uint 4
- %_ptr_Input_v4uint = OpTypePointer Input %v4uint
- %var = OpVariable )") + var_type +
- std::string(R"( Input
- %int_0 = OpConstant %int 0
- %_ptr_Uniform_uint = OpTypePointer Uniform %uint
- %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
- %v2int = OpTypeVector %int 2
- %25 = OpConstantComposite %v2int %int_0 %int_0
- %v4float = OpTypeVector %float 4
- %uint_0 = OpConstant %uint 0
- %_ptr_Uniform_float = OpTypePointer Uniform %float
- %main = OpFunction %void None %3
- %5 = OpLabel
- %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
- %20 = OpLoad %uint %19
- )") + var_value + std::string(R"(
- %test = OpIAdd %uint %var_value %20
- %22 = OpAccessChain %_ptr_UniformConstant_13 %images %test
- %23 = OpLoad %13 %22
- %27 = OpImageRead %v4float %23 %25
- %29 = OpCompositeExtract %float %27 0
- %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
- OpStore %31 %29
- OpReturn
- OpFunctionEnd
- )");
- SinglePassRunAndMatch<SpreadVolatileSemantics>(text, true);
- }
- INSTANTIATE_TEST_SUITE_P(
- SetLoadVolatile, SetLoadVolatileTest,
- ::testing::ValuesIn(std::vector<ExecutionModelAndBuiltIn>{
- {"RayGenerationKHR", "SubgroupSize", false},
- {"RayGenerationKHR", "SubgroupLocalInvocationId", false},
- {"RayGenerationKHR", "SubgroupEqMask", true},
- {"ClosestHitKHR", "SubgroupLocalInvocationId", true},
- {"IntersectionKHR", "SubgroupEqMask", true},
- {"MissKHR", "SubgroupGeMask", true},
- {"CallableKHR", "SubgroupGtMask", true},
- {"RayGenerationKHR", "SubgroupLeMask", true},
- }));
- using VolatileSpreadTest = PassTest<::testing::Test>;
- TEST_F(VolatileSpreadTest, SpreadVolatileForHelperInvocation) {
- const std::string text =
- R"(
- OpCapability Shader
- OpCapability DemoteToHelperInvocation
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %var
- OpExecutionMode %main OriginUpperLeft
- ; CHECK: OpDecorate [[var:%\w+]] BuiltIn HelperInvocation
- ; CHECK: OpDecorate [[var]] Volatile
- OpDecorate %var BuiltIn HelperInvocation
- %bool = OpTypeBool
- %void = OpTypeVoid
- %void_fn = OpTypeFunction %void
- %_ptr_Input_bool = OpTypePointer Input %bool
- %var = OpVariable %_ptr_Input_bool Input
- %main = OpFunction %void None %void_fn
- %entry = OpLabel
- %load = OpLoad %bool %var
- OpDemoteToHelperInvocation
- OpReturn
- OpFunctionEnd
- )";
- SetTargetEnv(SPV_ENV_UNIVERSAL_1_6);
- SinglePassRunAndMatch<SpreadVolatileSemantics>(text, true);
- }
- TEST_F(VolatileSpreadTest, MultipleExecutionModel) {
- const std::string text =
- R"(
- OpCapability RuntimeDescriptorArray
- OpCapability RayTracingKHR
- OpCapability SubgroupBallotKHR
- OpExtension "SPV_EXT_descriptor_indexing"
- OpExtension "SPV_KHR_ray_tracing"
- OpExtension "SPV_KHR_shader_ballot"
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint RayGenerationKHR %RayGeneration "RayGeneration" %var
- OpEntryPoint GLCompute %compute "Compute" %gl_LocalInvocationIndex
- OpExecutionMode %compute LocalSize 16 16 1
- OpSource GLSL 460
- OpSourceExtension "GL_EXT_nonuniform_qualifier"
- OpSourceExtension "GL_KHR_ray_tracing"
- OpName %RayGeneration "RayGeneration"
- OpName %StorageBuffer "StorageBuffer"
- OpMemberName %StorageBuffer 0 "index"
- OpMemberName %StorageBuffer 1 "red"
- OpName %sbo "sbo"
- OpName %images "images"
- OpMemberDecorate %StorageBuffer 0 Offset 0
- OpMemberDecorate %StorageBuffer 1 Offset 4
- OpDecorate %gl_LocalInvocationIndex BuiltIn LocalInvocationIndex
- OpDecorate %StorageBuffer BufferBlock
- OpDecorate %sbo DescriptorSet 0
- OpDecorate %sbo Binding 0
- OpDecorate %images DescriptorSet 0
- OpDecorate %images Binding 1
- OpDecorate %images NonWritable
- ; CHECK: OpEntryPoint RayGenerationKHR {{%\w+}} "RayGeneration" [[var:%\w+]]
- ; CHECK: OpDecorate [[var]] BuiltIn SubgroupSize
- ; CHECK: OpDecorate [[var]] Volatile
- ; CHECK-NOT: OpDecorate {{%\w+}} Volatile
- OpDecorate %var BuiltIn SubgroupSize
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %StorageBuffer = OpTypeStruct %uint %float
- %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
- %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
- %_runtimearr_13 = OpTypeRuntimeArray %13
- %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
- %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
- %_ptr_Input_uint = OpTypePointer Input %uint
- %var = OpVariable %_ptr_Input_uint Input
- %int_0 = OpConstant %int 0
- %_ptr_Uniform_uint = OpTypePointer Uniform %uint
- %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
- %v2int = OpTypeVector %int 2
- %25 = OpConstantComposite %v2int %int_0 %int_0
- %v4float = OpTypeVector %float 4
- %uint_0 = OpConstant %uint 0
- %uint_1 = OpConstant %uint 1
- %_ptr_Uniform_float = OpTypePointer Uniform %float
- %gl_LocalInvocationIndex = OpVariable %_ptr_Input_uint Input
- %_ptr_Workgroup_uint = OpTypePointer Workgroup %uint
- %shared = OpVariable %_ptr_Workgroup_uint Workgroup
- %RayGeneration = OpFunction %void None %3
- %5 = OpLabel
- %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
- %20 = OpLoad %uint %var
- %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
- %23 = OpLoad %13 %22
- %27 = OpImageRead %v4float %23 %25
- %29 = OpCompositeExtract %float %27 0
- %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
- OpStore %31 %29
- OpReturn
- OpFunctionEnd
- %compute = OpFunction %void None %3
- %66 = OpLabel
- %62 = OpLoad %uint %gl_LocalInvocationIndex
- %61 = OpAtomicIAdd %uint %shared %uint_1 %uint_0 %62
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<SpreadVolatileSemantics>(text, true);
- }
- TEST_F(VolatileSpreadTest, VarUsedInMultipleEntryPoints) {
- const std::string text =
- R"(
- OpCapability RuntimeDescriptorArray
- OpCapability RayTracingKHR
- OpCapability SubgroupBallotKHR
- OpExtension "SPV_EXT_descriptor_indexing"
- OpExtension "SPV_KHR_ray_tracing"
- OpExtension "SPV_KHR_shader_ballot"
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint RayGenerationKHR %RayGeneration "RayGeneration" %var
- OpEntryPoint ClosestHitKHR %ClosestHit "ClosestHit" %var
- OpSource GLSL 460
- OpSourceExtension "GL_EXT_nonuniform_qualifier"
- OpSourceExtension "GL_KHR_ray_tracing"
- OpName %RayGeneration "RayGeneration"
- OpName %ClosestHit "ClosestHit"
- OpName %StorageBuffer "StorageBuffer"
- OpMemberName %StorageBuffer 0 "index"
- OpMemberName %StorageBuffer 1 "red"
- OpName %sbo "sbo"
- OpName %images "images"
- OpMemberDecorate %StorageBuffer 0 Offset 0
- OpMemberDecorate %StorageBuffer 1 Offset 4
- OpDecorate %gl_LocalInvocationIndex BuiltIn LocalInvocationIndex
- OpDecorate %StorageBuffer BufferBlock
- OpDecorate %sbo DescriptorSet 0
- OpDecorate %sbo Binding 0
- OpDecorate %images DescriptorSet 0
- OpDecorate %images Binding 1
- OpDecorate %images NonWritable
- ; CHECK: OpEntryPoint RayGenerationKHR {{%\w+}} "RayGeneration" [[var:%\w+]]
- ; CHECK: OpEntryPoint ClosestHitKHR {{%\w+}} "ClosestHit" [[var]]
- ; CHECK: OpDecorate [[var]] BuiltIn SubgroupSize
- ; CHECK: OpDecorate [[var]] Volatile
- ; CHECK-NOT: OpDecorate {{%\w+}} Volatile
- OpDecorate %var BuiltIn SubgroupSize
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %StorageBuffer = OpTypeStruct %uint %float
- %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
- %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
- %_runtimearr_13 = OpTypeRuntimeArray %13
- %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
- %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
- %_ptr_Input_uint = OpTypePointer Input %uint
- %var = OpVariable %_ptr_Input_uint Input
- %int_0 = OpConstant %int 0
- %_ptr_Uniform_uint = OpTypePointer Uniform %uint
- %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
- %v2int = OpTypeVector %int 2
- %25 = OpConstantComposite %v2int %int_0 %int_0
- %v4float = OpTypeVector %float 4
- %uint_0 = OpConstant %uint 0
- %uint_1 = OpConstant %uint 1
- %_ptr_Uniform_float = OpTypePointer Uniform %float
- %gl_LocalInvocationIndex = OpVariable %_ptr_Input_uint Input
- %_ptr_Workgroup_uint = OpTypePointer Workgroup %uint
- %shared = OpVariable %_ptr_Workgroup_uint Workgroup
- %RayGeneration = OpFunction %void None %3
- %5 = OpLabel
- %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
- %20 = OpLoad %uint %var
- %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
- %23 = OpLoad %13 %22
- %27 = OpImageRead %v4float %23 %25
- %29 = OpCompositeExtract %float %27 0
- %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
- OpStore %31 %29
- OpReturn
- OpFunctionEnd
- %ClosestHit = OpFunction %void None %3
- %45 = OpLabel
- %49 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
- %40 = OpLoad %uint %var
- %42 = OpAccessChain %_ptr_UniformConstant_13 %images %40
- %43 = OpLoad %13 %42
- %47 = OpImageRead %v4float %43 %25
- %59 = OpCompositeExtract %float %47 0
- %51 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
- OpStore %51 %59
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<SpreadVolatileSemantics>(text, true);
- }
- class VolatileSpreadErrorTest : public PassTest<::testing::Test> {
- public:
- VolatileSpreadErrorTest()
- : consumer_([this](spv_message_level_t level, const char*,
- const spv_position_t& position, const char* message) {
- if (!error_message_.empty()) error_message_ += "\n";
- switch (level) {
- case SPV_MSG_FATAL:
- case SPV_MSG_INTERNAL_ERROR:
- case SPV_MSG_ERROR:
- error_message_ += "ERROR";
- break;
- case SPV_MSG_WARNING:
- error_message_ += "WARNING";
- break;
- case SPV_MSG_INFO:
- error_message_ += "INFO";
- break;
- case SPV_MSG_DEBUG:
- error_message_ += "DEBUG";
- break;
- }
- error_message_ +=
- ": " + std::to_string(position.index) + ": " + message;
- }) {}
- Pass::Status RunPass(const std::string& text) {
- std::unique_ptr<IRContext> context_ =
- spvtools::BuildModule(SPV_ENV_UNIVERSAL_1_2, consumer_, text);
- if (!context_.get()) return Pass::Status::Failure;
- PassManager manager;
- manager.SetMessageConsumer(consumer_);
- manager.AddPass<SpreadVolatileSemantics>();
- return manager.Run(context_.get());
- }
- std::string GetErrorMessage() const { return error_message_; }
- void TearDown() override { error_message_.clear(); }
- private:
- spvtools::MessageConsumer consumer_;
- std::string error_message_;
- };
- TEST_F(VolatileSpreadErrorTest, VarUsedInMultipleExecutionModelError) {
- const std::string text =
- R"(
- OpCapability RuntimeDescriptorArray
- OpCapability RayTracingKHR
- OpCapability SubgroupBallotKHR
- OpExtension "SPV_EXT_descriptor_indexing"
- OpExtension "SPV_KHR_ray_tracing"
- OpExtension "SPV_KHR_shader_ballot"
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint RayGenerationKHR %RayGeneration "RayGeneration" %var
- OpEntryPoint GLCompute %compute "Compute" %gl_LocalInvocationIndex %var
- OpExecutionMode %compute LocalSize 16 16 1
- OpSource GLSL 460
- OpSourceExtension "GL_EXT_nonuniform_qualifier"
- OpSourceExtension "GL_KHR_ray_tracing"
- OpName %RayGeneration "RayGeneration"
- OpName %StorageBuffer "StorageBuffer"
- OpMemberName %StorageBuffer 0 "index"
- OpMemberName %StorageBuffer 1 "red"
- OpName %sbo "sbo"
- OpName %images "images"
- OpMemberDecorate %StorageBuffer 0 Offset 0
- OpMemberDecorate %StorageBuffer 1 Offset 4
- OpDecorate %gl_LocalInvocationIndex BuiltIn LocalInvocationIndex
- OpDecorate %StorageBuffer BufferBlock
- OpDecorate %sbo DescriptorSet 0
- OpDecorate %sbo Binding 0
- OpDecorate %images DescriptorSet 0
- OpDecorate %images Binding 1
- OpDecorate %images NonWritable
- OpDecorate %var BuiltIn SubgroupSize
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %StorageBuffer = OpTypeStruct %uint %float
- %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
- %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
- %_runtimearr_13 = OpTypeRuntimeArray %13
- %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
- %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
- %_ptr_Input_uint = OpTypePointer Input %uint
- %var = OpVariable %_ptr_Input_uint Input
- %int_0 = OpConstant %int 0
- %_ptr_Uniform_uint = OpTypePointer Uniform %uint
- %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
- %v2int = OpTypeVector %int 2
- %25 = OpConstantComposite %v2int %int_0 %int_0
- %v4float = OpTypeVector %float 4
- %uint_0 = OpConstant %uint 0
- %uint_1 = OpConstant %uint 1
- %_ptr_Uniform_float = OpTypePointer Uniform %float
- %gl_LocalInvocationIndex = OpVariable %_ptr_Input_uint Input
- %_ptr_Workgroup_uint = OpTypePointer Workgroup %uint
- %shared = OpVariable %_ptr_Workgroup_uint Workgroup
- %RayGeneration = OpFunction %void None %3
- %5 = OpLabel
- %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
- %20 = OpLoad %uint %var
- %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
- %23 = OpLoad %13 %22
- %27 = OpImageRead %v4float %23 %25
- %29 = OpCompositeExtract %float %27 0
- %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
- OpStore %31 %29
- OpReturn
- OpFunctionEnd
- %compute = OpFunction %void None %3
- %66 = OpLabel
- %62 = OpLoad %uint %gl_LocalInvocationIndex
- %63 = OpLoad %uint %var
- %64 = OpIAdd %uint %62 %63
- %61 = OpAtomicIAdd %uint %shared %uint_1 %uint_0 %64
- OpReturn
- OpFunctionEnd
- )";
- EXPECT_EQ(RunPass(text), Pass::Status::Failure);
- const char expected_error[] =
- "ERROR: 0: Variable is a target for Volatile semantics for an entry "
- "point, but it is not for another entry point";
- EXPECT_STREQ(GetErrorMessage().substr(0, sizeof(expected_error) - 1).c_str(),
- expected_error);
- }
- TEST_F(VolatileSpreadErrorTest,
- VarUsedInMultipleReverseOrderExecutionModelError) {
- const std::string text =
- R"(
- OpCapability RuntimeDescriptorArray
- OpCapability RayTracingKHR
- OpCapability SubgroupBallotKHR
- OpExtension "SPV_EXT_descriptor_indexing"
- OpExtension "SPV_KHR_ray_tracing"
- OpExtension "SPV_KHR_shader_ballot"
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint GLCompute %compute "Compute" %gl_LocalInvocationIndex %var
- OpEntryPoint RayGenerationKHR %RayGeneration "RayGeneration" %var
- OpExecutionMode %compute LocalSize 16 16 1
- OpSource GLSL 460
- OpSourceExtension "GL_EXT_nonuniform_qualifier"
- OpSourceExtension "GL_KHR_ray_tracing"
- OpName %RayGeneration "RayGeneration"
- OpName %StorageBuffer "StorageBuffer"
- OpMemberName %StorageBuffer 0 "index"
- OpMemberName %StorageBuffer 1 "red"
- OpName %sbo "sbo"
- OpName %images "images"
- OpMemberDecorate %StorageBuffer 0 Offset 0
- OpMemberDecorate %StorageBuffer 1 Offset 4
- OpDecorate %gl_LocalInvocationIndex BuiltIn LocalInvocationIndex
- OpDecorate %StorageBuffer BufferBlock
- OpDecorate %sbo DescriptorSet 0
- OpDecorate %sbo Binding 0
- OpDecorate %images DescriptorSet 0
- OpDecorate %images Binding 1
- OpDecorate %images NonWritable
- OpDecorate %var BuiltIn SubgroupSize
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %StorageBuffer = OpTypeStruct %uint %float
- %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
- %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
- %_runtimearr_13 = OpTypeRuntimeArray %13
- %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
- %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
- %_ptr_Input_uint = OpTypePointer Input %uint
- %var = OpVariable %_ptr_Input_uint Input
- %int_0 = OpConstant %int 0
- %_ptr_Uniform_uint = OpTypePointer Uniform %uint
- %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
- %v2int = OpTypeVector %int 2
- %25 = OpConstantComposite %v2int %int_0 %int_0
- %v4float = OpTypeVector %float 4
- %uint_0 = OpConstant %uint 0
- %uint_1 = OpConstant %uint 1
- %_ptr_Uniform_float = OpTypePointer Uniform %float
- %gl_LocalInvocationIndex = OpVariable %_ptr_Input_uint Input
- %_ptr_Workgroup_uint = OpTypePointer Workgroup %uint
- %shared = OpVariable %_ptr_Workgroup_uint Workgroup
- %RayGeneration = OpFunction %void None %3
- %5 = OpLabel
- %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
- %20 = OpLoad %uint %var
- %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
- %23 = OpLoad %13 %22
- %27 = OpImageRead %v4float %23 %25
- %29 = OpCompositeExtract %float %27 0
- %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
- OpStore %31 %29
- OpReturn
- OpFunctionEnd
- %compute = OpFunction %void None %3
- %66 = OpLabel
- %62 = OpLoad %uint %gl_LocalInvocationIndex
- %63 = OpLoad %uint %var
- %64 = OpIAdd %uint %62 %63
- %61 = OpAtomicIAdd %uint %shared %uint_1 %uint_0 %64
- OpReturn
- OpFunctionEnd
- )";
- EXPECT_EQ(RunPass(text), Pass::Status::Failure);
- const char expected_error[] =
- "ERROR: 0: Variable is a target for Volatile semantics for an entry "
- "point, but it is not for another entry point";
- EXPECT_STREQ(GetErrorMessage().substr(0, sizeof(expected_error) - 1).c_str(),
- expected_error);
- }
- TEST_F(VolatileSpreadErrorTest, FunctionNotInlined) {
- const std::string text =
- R"(
- OpCapability RuntimeDescriptorArray
- OpCapability RayTracingKHR
- OpCapability SubgroupBallotKHR
- OpExtension "SPV_EXT_descriptor_indexing"
- OpExtension "SPV_KHR_ray_tracing"
- OpExtension "SPV_KHR_shader_ballot"
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint RayGenerationKHR %RayGeneration "RayGeneration" %var
- OpEntryPoint ClosestHitKHR %ClosestHit "ClosestHit" %var
- OpSource GLSL 460
- OpSourceExtension "GL_EXT_nonuniform_qualifier"
- OpSourceExtension "GL_KHR_ray_tracing"
- OpName %RayGeneration "RayGeneration"
- OpName %ClosestHit "ClosestHit"
- OpName %StorageBuffer "StorageBuffer"
- OpMemberName %StorageBuffer 0 "index"
- OpMemberName %StorageBuffer 1 "red"
- OpName %sbo "sbo"
- OpName %images "images"
- OpMemberDecorate %StorageBuffer 0 Offset 0
- OpMemberDecorate %StorageBuffer 1 Offset 4
- OpDecorate %gl_LocalInvocationIndex BuiltIn LocalInvocationIndex
- OpDecorate %StorageBuffer BufferBlock
- OpDecorate %sbo DescriptorSet 0
- OpDecorate %sbo Binding 0
- OpDecorate %images DescriptorSet 0
- OpDecorate %images Binding 1
- OpDecorate %images NonWritable
- OpDecorate %var BuiltIn SubgroupSize
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %StorageBuffer = OpTypeStruct %uint %float
- %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
- %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
- %_runtimearr_13 = OpTypeRuntimeArray %13
- %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
- %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
- %_ptr_Input_uint = OpTypePointer Input %uint
- %var = OpVariable %_ptr_Input_uint Input
- %int_0 = OpConstant %int 0
- %_ptr_Uniform_uint = OpTypePointer Uniform %uint
- %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
- %v2int = OpTypeVector %int 2
- %25 = OpConstantComposite %v2int %int_0 %int_0
- %v4float = OpTypeVector %float 4
- %uint_0 = OpConstant %uint 0
- %uint_1 = OpConstant %uint 1
- %_ptr_Uniform_float = OpTypePointer Uniform %float
- %gl_LocalInvocationIndex = OpVariable %_ptr_Input_uint Input
- %_ptr_Workgroup_uint = OpTypePointer Workgroup %uint
- %shared = OpVariable %_ptr_Workgroup_uint Workgroup
- %RayGeneration = OpFunction %void None %3
- %5 = OpLabel
- %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
- %20 = OpLoad %uint %19
- %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
- %23 = OpLoad %13 %22
- %27 = OpImageRead %v4float %23 %25
- %29 = OpCompositeExtract %float %27 0
- %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
- OpStore %31 %29
- OpReturn
- OpFunctionEnd
- %NotInlined = OpFunction %void None %3
- %32 = OpLabel
- OpReturn
- OpFunctionEnd
- %ClosestHit = OpFunction %void None %3
- %45 = OpLabel
- %49 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
- %40 = OpLoad %uint %49
- %42 = OpAccessChain %_ptr_UniformConstant_13 %images %40
- %43 = OpLoad %13 %42
- %47 = OpImageRead %v4float %43 %25
- %59 = OpCompositeExtract %float %47 0
- %51 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
- OpStore %51 %59
- OpReturn
- OpFunctionEnd
- )";
- EXPECT_EQ(RunPass(text), Pass::Status::SuccessWithoutChange);
- }
- TEST_F(VolatileSpreadErrorTest, VarNotUsedInEntryPointForVolatile) {
- const std::string text =
- R"(
- OpCapability RuntimeDescriptorArray
- OpCapability RayTracingKHR
- OpCapability SubgroupBallotKHR
- OpExtension "SPV_EXT_descriptor_indexing"
- OpExtension "SPV_KHR_ray_tracing"
- OpExtension "SPV_KHR_shader_ballot"
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint RayGenerationKHR %RayGeneration "RayGeneration" %var
- OpEntryPoint GLCompute %compute "Compute" %gl_LocalInvocationIndex %var
- OpExecutionMode %compute LocalSize 16 16 1
- OpSource GLSL 460
- OpSourceExtension "GL_EXT_nonuniform_qualifier"
- OpSourceExtension "GL_KHR_ray_tracing"
- OpName %RayGeneration "RayGeneration"
- OpName %StorageBuffer "StorageBuffer"
- OpMemberName %StorageBuffer 0 "index"
- OpMemberName %StorageBuffer 1 "red"
- OpName %sbo "sbo"
- OpName %images "images"
- OpMemberDecorate %StorageBuffer 0 Offset 0
- OpMemberDecorate %StorageBuffer 1 Offset 4
- OpDecorate %gl_LocalInvocationIndex BuiltIn LocalInvocationIndex
- OpDecorate %StorageBuffer BufferBlock
- OpDecorate %sbo DescriptorSet 0
- OpDecorate %sbo Binding 0
- OpDecorate %images DescriptorSet 0
- OpDecorate %images Binding 1
- OpDecorate %images NonWritable
- ; CHECK-NOT: OpDecorate {{%\w+}} Volatile
- OpDecorate %var BuiltIn SubgroupSize
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %StorageBuffer = OpTypeStruct %uint %float
- %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
- %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
- %_runtimearr_13 = OpTypeRuntimeArray %13
- %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
- %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
- %_ptr_Input_uint = OpTypePointer Input %uint
- %var = OpVariable %_ptr_Input_uint Input
- %int_0 = OpConstant %int 0
- %_ptr_Uniform_uint = OpTypePointer Uniform %uint
- %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
- %v2int = OpTypeVector %int 2
- %25 = OpConstantComposite %v2int %int_0 %int_0
- %v4float = OpTypeVector %float 4
- %uint_0 = OpConstant %uint 0
- %uint_1 = OpConstant %uint 1
- %_ptr_Uniform_float = OpTypePointer Uniform %float
- %gl_LocalInvocationIndex = OpVariable %_ptr_Input_uint Input
- %_ptr_Workgroup_uint = OpTypePointer Workgroup %uint
- %shared = OpVariable %_ptr_Workgroup_uint Workgroup
- %RayGeneration = OpFunction %void None %3
- %5 = OpLabel
- %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
- %20 = OpLoad %uint %19
- %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
- %23 = OpLoad %13 %22
- %27 = OpImageRead %v4float %23 %25
- %29 = OpCompositeExtract %float %27 0
- %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
- OpStore %31 %29
- OpReturn
- OpFunctionEnd
- %compute = OpFunction %void None %3
- %66 = OpLabel
- %62 = OpLoad %uint %gl_LocalInvocationIndex
- %63 = OpLoad %uint %var
- %64 = OpIAdd %uint %62 %63
- %61 = OpAtomicIAdd %uint %shared %uint_1 %uint_0 %64
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<SpreadVolatileSemantics>(text, true);
- }
- TEST_F(VolatileSpreadTest, RecursivelySpreadVolatile) {
- const std::string text =
- R"(
- OpCapability RuntimeDescriptorArray
- OpCapability RayTracingKHR
- OpCapability SubgroupBallotKHR
- OpCapability VulkanMemoryModel
- OpExtension "SPV_KHR_vulkan_memory_model"
- OpExtension "SPV_EXT_descriptor_indexing"
- OpExtension "SPV_KHR_ray_tracing"
- OpExtension "SPV_KHR_shader_ballot"
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical Vulkan
- OpEntryPoint RayGenerationKHR %RayGeneration "RayGeneration" %var0 %var1
- OpSource GLSL 460
- OpSourceExtension "GL_EXT_nonuniform_qualifier"
- OpSourceExtension "GL_KHR_ray_tracing"
- OpName %RayGeneration "RayGeneration"
- OpName %StorageBuffer "StorageBuffer"
- OpMemberName %StorageBuffer 0 "index"
- OpMemberName %StorageBuffer 1 "red"
- OpName %sbo "sbo"
- OpName %images "images"
- OpMemberDecorate %StorageBuffer 0 Offset 0
- OpMemberDecorate %StorageBuffer 1 Offset 4
- OpDecorate %StorageBuffer BufferBlock
- OpDecorate %sbo DescriptorSet 0
- OpDecorate %sbo Binding 0
- OpDecorate %images DescriptorSet 0
- OpDecorate %images Binding 1
- OpDecorate %images NonWritable
- ; CHECK: OpDecorate [[var0:%\w+]] BuiltIn SubgroupEqMask
- ; CHECK: OpDecorate [[var1:%\w+]] BuiltIn SubgroupGeMask
- OpDecorate %var0 BuiltIn SubgroupEqMask
- OpDecorate %var1 BuiltIn SubgroupGeMask
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %StorageBuffer = OpTypeStruct %uint %float
- %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
- %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
- %_runtimearr_13 = OpTypeRuntimeArray %13
- %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
- %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
- %v4uint = OpTypeVector %uint 4
- %_ptr_Input_v4uint = OpTypePointer Input %v4uint
- %_ptr_Input_uint = OpTypePointer Input %uint
- %var0 = OpVariable %_ptr_Input_v4uint Input
- %var1 = OpVariable %_ptr_Input_v4uint Input
- %int_0 = OpConstant %int 0
- %_ptr_Uniform_uint = OpTypePointer Uniform %uint
- %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
- %v2int = OpTypeVector %int 2
- %25 = OpConstantComposite %v2int %int_0 %int_0
- %v4float = OpTypeVector %float 4
- %uint_0 = OpConstant %uint 0
- %uint_1 = OpConstant %uint 1
- %_ptr_Uniform_float = OpTypePointer Uniform %float
- %RayGeneration = OpFunction %void None %3
- %5 = OpLabel
- ; CHECK: [[ptr0:%\w+]] = OpAccessChain %_ptr_Input_uint [[var0]] %int_0
- ; CHECK: OpLoad {{%\w+}} [[ptr0]] Volatile
- %19 = OpAccessChain %_ptr_Input_uint %var0 %int_0
- %20 = OpLoad %uint %19
- %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
- %23 = OpLoad %13 %22
- %27 = OpImageRead %v4float %23 %25
- %29 = OpCompositeExtract %float %27 0
- %31 = OpAccessChain %_ptr_Uniform_float %sbo %uint_1
- ; CHECK: OpLoad {{%\w+}} [[ptr0]] Volatile
- %24 = OpLoad %uint %19
- ; CHECK: [[var2:%\w+]] = OpCopyObject %_ptr_Input_v4uint [[var0]]
- ; CHECK: [[ptr2:%\w+]] = OpAccessChain %_ptr_Input_uint [[var2]] %int_1
- ; CHECK: OpLoad {{%\w+}} [[ptr2]] Volatile
- %18 = OpCopyObject %_ptr_Input_v4uint %var0
- %21 = OpAccessChain %_ptr_Input_uint %18 %int_1
- %26 = OpLoad %uint %21
- %28 = OpIAdd %uint %24 %26
- %30 = OpConvertUToF %float %28
- ; CHECK: [[ptr1:%\w+]] = OpAccessChain %_ptr_Input_uint [[var1]] %int_1
- ; CHECK: OpLoad {{%\w+}} [[ptr1]] Volatile
- %32 = OpAccessChain %_ptr_Input_uint %var1 %int_1
- %33 = OpLoad %uint %32
- %34 = OpConvertUToF %float %33
- %35 = OpFAdd %float %34 %30
- %36 = OpFAdd %float %35 %29
- OpStore %31 %36
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<SpreadVolatileSemantics>(text, true);
- }
- TEST_F(VolatileSpreadTest, SpreadVolatileOnlyForTargetEntryPoints) {
- const std::string text =
- R"(
- OpCapability RuntimeDescriptorArray
- OpCapability RayTracingKHR
- OpCapability SubgroupBallotKHR
- OpCapability VulkanMemoryModel
- OpCapability VulkanMemoryModelDeviceScopeKHR
- OpExtension "SPV_KHR_vulkan_memory_model"
- OpExtension "SPV_EXT_descriptor_indexing"
- OpExtension "SPV_KHR_ray_tracing"
- OpExtension "SPV_KHR_shader_ballot"
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical Vulkan
- OpEntryPoint RayGenerationKHR %RayGeneration "RayGeneration" %var0 %var1
- OpEntryPoint GLCompute %compute "Compute" %var0 %var1
- OpExecutionMode %compute LocalSize 16 16 1
- OpSource GLSL 460
- OpSourceExtension "GL_EXT_nonuniform_qualifier"
- OpSourceExtension "GL_KHR_ray_tracing"
- OpName %RayGeneration "RayGeneration"
- OpName %StorageBuffer "StorageBuffer"
- OpMemberName %StorageBuffer 0 "index"
- OpMemberName %StorageBuffer 1 "red"
- OpName %sbo "sbo"
- OpName %images "images"
- OpMemberDecorate %StorageBuffer 0 Offset 0
- OpMemberDecorate %StorageBuffer 1 Offset 4
- OpDecorate %StorageBuffer BufferBlock
- OpDecorate %sbo DescriptorSet 0
- OpDecorate %sbo Binding 0
- OpDecorate %images DescriptorSet 0
- OpDecorate %images Binding 1
- OpDecorate %images NonWritable
- ; CHECK: OpDecorate [[var0:%\w+]] BuiltIn SubgroupEqMask
- ; CHECK: OpDecorate [[var1:%\w+]] BuiltIn SubgroupGeMask
- OpDecorate %var0 BuiltIn SubgroupEqMask
- OpDecorate %var1 BuiltIn SubgroupGeMask
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %StorageBuffer = OpTypeStruct %uint %float
- %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
- %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
- %_runtimearr_13 = OpTypeRuntimeArray %13
- %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
- %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
- %v4uint = OpTypeVector %uint 4
- %_ptr_Input_v4uint = OpTypePointer Input %v4uint
- %_ptr_Input_uint = OpTypePointer Input %uint
- %var0 = OpVariable %_ptr_Input_v4uint Input
- %var1 = OpVariable %_ptr_Input_v4uint Input
- %int_0 = OpConstant %int 0
- %_ptr_Uniform_uint = OpTypePointer Uniform %uint
- %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
- %v2int = OpTypeVector %int 2
- %25 = OpConstantComposite %v2int %int_0 %int_0
- %v4float = OpTypeVector %float 4
- %uint_0 = OpConstant %uint 0
- %uint_1 = OpConstant %uint 1
- %_ptr_Uniform_float = OpTypePointer Uniform %float
- %_ptr_Workgroup_uint = OpTypePointer Workgroup %uint
- %shared = OpVariable %_ptr_Workgroup_uint Workgroup
- %RayGeneration = OpFunction %void None %3
- %5 = OpLabel
- ; CHECK: [[ptr0:%\w+]] = OpAccessChain %_ptr_Input_uint [[var0]] %int_0
- ; CHECK: OpLoad {{%\w+}} [[ptr0]] Volatile
- %19 = OpAccessChain %_ptr_Input_uint %var0 %int_0
- %20 = OpLoad %uint %19
- %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
- %23 = OpLoad %13 %22
- %27 = OpImageRead %v4float %23 %25
- %29 = OpCompositeExtract %float %27 0
- %31 = OpAccessChain %_ptr_Uniform_float %sbo %uint_1
- ; CHECK: OpLoad {{%\w+}} [[ptr0]] Volatile
- %24 = OpLoad %uint %19
- ; CHECK: [[var2:%\w+]] = OpCopyObject %_ptr_Input_v4uint [[var0]]
- ; CHECK: [[ptr2:%\w+]] = OpAccessChain %_ptr_Input_uint [[var2]] %int_1
- ; CHECK: OpLoad {{%\w+}} [[ptr2]] Volatile
- %18 = OpCopyObject %_ptr_Input_v4uint %var0
- %21 = OpAccessChain %_ptr_Input_uint %18 %int_1
- %26 = OpLoad %uint %21
- %28 = OpIAdd %uint %24 %26
- %30 = OpConvertUToF %float %28
- ; CHECK: [[ptr1:%\w+]] = OpAccessChain %_ptr_Input_uint [[var1]] %int_1
- ; CHECK: OpLoad {{%\w+}} [[ptr1]] Volatile
- %32 = OpAccessChain %_ptr_Input_uint %var1 %int_1
- %33 = OpLoad %uint %32
- %34 = OpConvertUToF %float %33
- %35 = OpFAdd %float %34 %30
- %36 = OpFAdd %float %35 %29
- OpStore %31 %36
- OpReturn
- OpFunctionEnd
- %compute = OpFunction %void None %3
- %66 = OpLabel
- ; CHECK-NOT: OpLoad {{%\w+}} {{%\w+}} Volatile
- %62 = OpLoad %v4uint %var0
- %63 = OpLoad %v4uint %var1
- %64 = OpIAdd %v4uint %62 %63
- %65 = OpCompositeExtract %uint %64 0
- %61 = OpAtomicIAdd %uint %shared %uint_1 %uint_0 %65
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<SpreadVolatileSemantics>(text, true);
- }
- TEST_F(VolatileSpreadTest, SkipIfItHasNoExecutionModel) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- Pass::Status status;
- std::tie(std::ignore, status) =
- SinglePassRunToBinary<SpreadVolatileSemantics>(text,
- /* skip_nop = */ false);
- EXPECT_EQ(status, Pass::Status::SuccessWithoutChange);
- }
- TEST_F(VolatileSpreadTest, NoInlinedfuncCalls) {
- const std::string text = R"(
- OpCapability RayTracingNV
- OpCapability VulkanMemoryModel
- OpCapability GroupNonUniform
- OpExtension "SPV_NV_ray_tracing"
- OpExtension "SPV_KHR_vulkan_memory_model"
- OpMemoryModel Logical Vulkan
- OpEntryPoint RayGenerationNV %main "main" %SubgroupSize
- OpSource HLSL 630
- OpName %main "main"
- OpName %src_main "src.main"
- OpName %bb_entry "bb.entry"
- OpName %func0 "func0"
- OpName %bb_entry_0 "bb.entry"
- OpName %func2 "func2"
- OpName %bb_entry_1 "bb.entry"
- OpName %param_var_count "param.var.count"
- OpName %func1 "func1"
- OpName %bb_entry_2 "bb.entry"
- OpName %func3 "func3"
- OpName %count "count"
- OpName %bb_entry_3 "bb.entry"
- OpDecorate %SubgroupSize BuiltIn SubgroupSize
- %uint = OpTypeInt 32 0
- %_ptr_Input_uint = OpTypePointer Input %uint
- %void = OpTypeVoid
- %6 = OpTypeFunction %void
- %_ptr_Function_uint = OpTypePointer Function %uint
- %25 = OpTypeFunction %void %_ptr_Function_uint
- %SubgroupSize = OpVariable %_ptr_Input_uint Input
- %main = OpFunction %void None %6
- %7 = OpLabel
- %8 = OpFunctionCall %void %src_main
- OpReturn
- OpFunctionEnd
- %src_main = OpFunction %void None %6
- %bb_entry = OpLabel
- %11 = OpFunctionCall %void %func0
- OpReturn
- OpFunctionEnd
- %func0 = OpFunction %void DontInline %6
- %bb_entry_0 = OpLabel
- %14 = OpFunctionCall %void %func2
- %16 = OpFunctionCall %void %func1
- OpReturn
- OpFunctionEnd
- %func2 = OpFunction %void DontInline %6
- %bb_entry_1 = OpLabel
- %param_var_count = OpVariable %_ptr_Function_uint Function
- ; CHECK: {{%\w+}} = OpLoad %uint %SubgroupSize Volatile
- %21 = OpLoad %uint %SubgroupSize
- OpStore %param_var_count %21
- %22 = OpFunctionCall %void %func3 %param_var_count
- OpReturn
- OpFunctionEnd
- %func1 = OpFunction %void DontInline %6
- %bb_entry_2 = OpLabel
- OpReturn
- OpFunctionEnd
- %func3 = OpFunction %void DontInline %25
- %count = OpFunctionParameter %_ptr_Function_uint
- %bb_entry_3 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<SpreadVolatileSemantics>(text, true);
- }
- TEST_F(VolatileSpreadErrorTest, NoInlinedMultiEntryfuncCalls) {
- const std::string text = R"(
- OpCapability RayTracingNV
- OpCapability SubgroupBallotKHR
- OpExtension "SPV_NV_ray_tracing"
- OpExtension "SPV_KHR_shader_ballot"
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint RayGenerationNV %main "main" %SubgroupSize
- OpEntryPoint GLCompute %main2 "main2" %gl_LocalInvocationIndex %SubgroupSize
- OpSource HLSL 630
- OpName %main "main"
- OpName %bb_entry "bb.entry"
- OpName %main2 "main2"
- OpName %bb_entry_0 "bb.entry"
- OpName %func "func"
- OpName %count "count"
- OpName %bb_entry_1 "bb.entry"
- OpDecorate %gl_LocalInvocationIndex BuiltIn LocalInvocationIndex
- OpDecorate %SubgroupSize BuiltIn SubgroupSize
- %uint = OpTypeInt 32 0
- %uint_0 = OpConstant %uint 0
- %_ptr_Input_uint = OpTypePointer Input %uint
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %void = OpTypeVoid
- %12 = OpTypeFunction %void
- %_ptr_Function_uint = OpTypePointer Function %uint
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %29 = OpTypeFunction %void %_ptr_Function_v4float
- %34 = OpTypeFunction %void %_ptr_Function_uint
- %SubgroupSize = OpVariable %_ptr_Input_uint Input
- %gl_LocalInvocationIndex = OpVariable %_ptr_Input_uint Input
- %main = OpFunction %void None %12
- %bb_entry = OpLabel
- %20 = OpFunctionCall %void %func
- OpReturn
- OpFunctionEnd
- %main2 = OpFunction %void None %12
- %bb_entry_0 = OpLabel
- %33 = OpFunctionCall %void %func
- OpReturn
- OpFunctionEnd
- %func = OpFunction %void DontInline %12
- %bb_entry_1 = OpLabel
- %count = OpVariable %_ptr_Function_uint Function
- %35 = OpLoad %uint %SubgroupSize
- OpStore %count %35
- OpReturn
- OpFunctionEnd
- )";
- EXPECT_EQ(RunPass(text), Pass::Status::Failure);
- const char expected_error[] =
- "ERROR: 0: Variable is a target for Volatile semantics for an entry "
- "point, but it is not for another entry point";
- EXPECT_STREQ(GetErrorMessage().substr(0, sizeof(expected_error) - 1).c_str(),
- expected_error);
- }
- } // namespace
- } // namespace opt
- } // namespace spvtools
|