| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267 |
- // 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 "gmock/gmock.h"
- #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 RayGenerationNV {{%\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 RayGenerationNV {{%\w+}} "RayGeneration" [[var:%\w+]]
- ; CHECK: OpEntryPoint ClosestHitNV {{%\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
|