| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412 |
- // Copyright (c) 2017 Valve Corporation
- // Copyright (c) 2017 LunarG Inc.
- //
- // 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 {
- using LocalAccessChainConvertTest = PassTest<::testing::Test>;
- TEST_F(LocalAccessChainConvertTest, StructOfVecsOfFloatConverted) {
- // #version 140
- //
- // in vec4 BaseColor;
- //
- // struct S_t {
- // vec4 v0;
- // vec4 v1;
- // };
- //
- // void main()
- // {
- // S_t s0;
- // s0.v1 = BaseColor;
- // gl_FragColor = s0.v1;
- // }
- const std::string predefs_before =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
- OpExecutionMode %main OriginUpperLeft
- OpSource GLSL 140
- OpName %main "main"
- OpName %S_t "S_t"
- OpMemberName %S_t 0 "v0"
- OpMemberName %S_t 1 "v1"
- OpName %s0 "s0"
- OpName %BaseColor "BaseColor"
- OpName %gl_FragColor "gl_FragColor"
- %void = OpTypeVoid
- %8 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %S_t = OpTypeStruct %v4float %v4float
- %_ptr_Function_S_t = OpTypePointer Function %S_t
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %_ptr_Input_v4float = OpTypePointer Input %v4float
- %BaseColor = OpVariable %_ptr_Input_v4float Input
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %gl_FragColor = OpVariable %_ptr_Output_v4float Output
- )";
- const std::string before =
- R"(
- ; CHECK: [[st_id:%\w+]] = OpLoad %v4float %BaseColor
- ; CHECK: [[ld1:%\w+]] = OpLoad %S_t %s0
- ; CHECK: [[ex1:%\w+]] = OpCompositeInsert %S_t [[st_id]] [[ld1]] 1
- ; CHECK: OpStore %s0 [[ex1]]
- ; CHECK: [[ld2:%\w+]] = OpLoad %S_t %s0
- ; CHECK: [[ex2:%\w+]] = OpCompositeExtract %v4float [[ld2]] 1
- ; CHECK: OpStore %gl_FragColor [[ex2]]
- %main = OpFunction %void None %8
- %17 = OpLabel
- %s0 = OpVariable %_ptr_Function_S_t Function
- %18 = OpLoad %v4float %BaseColor
- %19 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- OpStore %19 %18
- %20 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- %21 = OpLoad %v4float %20
- OpStore %gl_FragColor %21
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(predefs_before + before,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, DebugScopeAndLineInfoForNewInstructions) {
- // #version 140
- //
- // in vec4 BaseColor;
- //
- // struct S_t {
- // vec4 v0;
- // vec4 v1;
- // };
- //
- // void main()
- // {
- // S_t s0;
- // s0.v1 = BaseColor;
- // gl_FragColor = s0.v1;
- // }
- const std::string predefs_before =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- %ext = OpExtInstImport "OpenCL.DebugInfo.100"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
- OpExecutionMode %main OriginUpperLeft
- OpSource GLSL 140
- OpName %main "main"
- OpName %S_t "S_t"
- OpMemberName %S_t 0 "v0"
- OpMemberName %S_t 1 "v1"
- OpName %s0 "s0"
- OpName %BaseColor "BaseColor"
- OpName %gl_FragColor "gl_FragColor"
- %5 = OpString "ps.hlsl"
- %6 = OpString "float"
- %var_name = OpString "s0"
- %main_name = OpString "main"
- %void = OpTypeVoid
- %8 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %S_t = OpTypeStruct %v4float %v4float
- %_ptr_Function_S_t = OpTypePointer Function %S_t
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %int_32 = OpConstant %int 32
- %_ptr_Input_v4float = OpTypePointer Input %v4float
- %BaseColor = OpVariable %_ptr_Input_v4float Input
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %gl_FragColor = OpVariable %_ptr_Output_v4float Output
- %20 = OpExtInst %void %ext DebugSource %5
- %21 = OpExtInst %void %ext DebugCompilationUnit 1 4 %20 HLSL
- %22 = OpExtInst %void %ext DebugTypeBasic %6 %int_32 Float
- %23 = OpExtInst %void %ext DebugTypeVector %22 4
- %24 = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %23
- %dbg_main = OpExtInst %void %ext DebugFunction %main_name %24 %20 4 1 %21 %main_name FlagIsProtected|FlagIsPrivate 4 %main
- %25 = OpExtInst %void %ext DebugLocalVariable %var_name %23 %20 0 0 %dbg_main FlagIsLocal
- )";
- const std::string before =
- R"(
- ; CHECK: [[st_id:%\w+]] = OpLoad %v4float %BaseColor
- ; CHECK: OpLine {{%\w+}} 1 0
- ; CHECK: [[ld1:%\w+]] = OpLoad %S_t %s0
- ; CHECK: [[ex1:%\w+]] = OpCompositeInsert %S_t [[st_id]] [[ld1]] 1
- ; CHECK: OpStore %s0 [[ex1]]
- ; CHECK: OpLine {{%\w+}} 3 0
- ; CHECK: [[ld2:%\w+]] = OpLoad %S_t %s0
- ; CHECK: [[ex2:%\w+]] = OpCompositeExtract %v4float [[ld2]] 1
- ; CHECK: OpLine {{%\w+}} 4 0
- ; CHECK: OpStore %gl_FragColor [[ex2]]
- %main = OpFunction %void None %8
- %17 = OpLabel
- %26 = OpExtInst %void %ext DebugScope %dbg_main
- %s0 = OpVariable %_ptr_Function_S_t Function
- %18 = OpLoad %v4float %BaseColor
- OpLine %5 0 0
- %19 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- OpLine %5 1 0
- OpStore %19 %18
- OpLine %5 2 0
- %27 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- OpLine %5 3 0
- %28 = OpLoad %v4float %27
- OpLine %5 4 0
- OpStore %gl_FragColor %28
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(predefs_before + before,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, TestTargetsReferencedByDebugValue) {
- // #version 140
- //
- // in vec4 BaseColor;
- //
- // struct S_t {
- // vec4 v0;
- // vec4 v1;
- // };
- //
- // void main()
- // {
- // S_t s0;
- // s0.v1 = BaseColor;
- // gl_FragColor = s0.v1;
- // }
- const std::string predefs_before =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- %ext = OpExtInstImport "OpenCL.DebugInfo.100"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
- OpExecutionMode %main OriginUpperLeft
- OpSource GLSL 140
- OpName %main "main"
- OpName %S_t "S_t"
- OpMemberName %S_t 0 "v0"
- OpMemberName %S_t 1 "v1"
- OpName %s0 "s0"
- OpName %BaseColor "BaseColor"
- OpName %gl_FragColor "gl_FragColor"
- %5 = OpString "ps.hlsl"
- %6 = OpString "float"
- %var_name = OpString "s0"
- %main_name = OpString "main"
- %void = OpTypeVoid
- %8 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %S_t = OpTypeStruct %v4float %v4float
- %_ptr_Function_S_t = OpTypePointer Function %S_t
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %int_32 = OpConstant %int 32
- %_ptr_Input_v4float = OpTypePointer Input %v4float
- %BaseColor = OpVariable %_ptr_Input_v4float Input
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %gl_FragColor = OpVariable %_ptr_Output_v4float Output
- %deref = OpExtInst %void %ext DebugOperation Deref
- %deref_expr = OpExtInst %void %ext DebugExpression %deref
- %null_expr = OpExtInst %void %ext DebugExpression
- %20 = OpExtInst %void %ext DebugSource %5
- %21 = OpExtInst %void %ext DebugCompilationUnit 1 4 %20 HLSL
- %22 = OpExtInst %void %ext DebugTypeBasic %6 %int_32 Float
- %23 = OpExtInst %void %ext DebugTypeVector %22 4
- %24 = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %23
- %dbg_main = OpExtInst %void %ext DebugFunction %main_name %24 %20 4 1 %21 %main_name FlagIsProtected|FlagIsPrivate 4 %main
- %25 = OpExtInst %void %ext DebugLocalVariable %var_name %23 %20 0 0 %dbg_main FlagIsLocal
- )";
- const std::string before =
- R"(
- ; CHECK: [[st_id:%\w+]] = OpLoad %v4float %BaseColor
- ; CHECK: OpLine {{%\w+}} 0 0
- ; CHECK: [[s0_1_ptr:%\w+]] = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- ; CHECK: DebugValue [[dbg_s0:%\w+]] [[s0_1_ptr]]
- ; CHECK: OpLine {{%\w+}} 1 0
- ; CHECK: [[s0:%\w+]] = OpLoad %S_t %s0
- ; CHECK: [[comp:%\w+]] = OpCompositeInsert %S_t [[st_id]] [[s0]] 1
- ; CHECK: OpStore %s0 [[comp]]
- ; CHECK: OpLine {{%\w+}} 2 0
- ; CHECK: [[s0_2_ptr:%\w+]] = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- ; CHECK: OpLine {{%\w+}} 3 0
- ; CHECK: [[s0:%\w+]] = OpLoad %S_t %s0
- ; CHECK: [[s0_2_val:%\w+]] = OpCompositeExtract %v4float [[s0]] 1
- ; CHECK: DebugValue [[dbg_s0]] [[s0_2_val]]
- ; CHECK: OpLine {{%\w+}} 4 0
- ; CHECK: OpStore %gl_FragColor [[s0_2_val]]
- %main = OpFunction %void None %8
- %17 = OpLabel
- %26 = OpExtInst %void %ext DebugScope %dbg_main
- %s0 = OpVariable %_ptr_Function_S_t Function
- %18 = OpLoad %v4float %BaseColor
- OpLine %5 0 0
- %19 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- %29 = OpExtInst %void %ext DebugValue %25 %19 %deref_expr %int_1
- OpLine %5 1 0
- OpStore %19 %18
- OpLine %5 2 0
- %27 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- OpLine %5 3 0
- %28 = OpLoad %v4float %27
- %30 = OpExtInst %void %ext DebugValue %25 %28 %null_expr %int_1
- OpLine %5 4 0
- OpStore %gl_FragColor %28
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(predefs_before + before,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, InBoundsAccessChainsConverted) {
- // #version 140
- //
- // in vec4 BaseColor;
- //
- // struct S_t {
- // vec4 v0;
- // vec4 v1;
- // };
- //
- // void main()
- // {
- // S_t s0;
- // s0.v1 = BaseColor;
- // gl_FragColor = s0.v1;
- // }
- const std::string predefs_before =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
- OpExecutionMode %main OriginUpperLeft
- OpSource GLSL 140
- OpName %main "main"
- OpName %S_t "S_t"
- OpMemberName %S_t 0 "v0"
- OpMemberName %S_t 1 "v1"
- OpName %s0 "s0"
- OpName %BaseColor "BaseColor"
- OpName %gl_FragColor "gl_FragColor"
- %void = OpTypeVoid
- %8 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %S_t = OpTypeStruct %v4float %v4float
- %_ptr_Function_S_t = OpTypePointer Function %S_t
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %_ptr_Input_v4float = OpTypePointer Input %v4float
- %BaseColor = OpVariable %_ptr_Input_v4float Input
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %gl_FragColor = OpVariable %_ptr_Output_v4float Output
- )";
- const std::string before =
- R"(
- ; CHECK: [[st_id:%\w+]] = OpLoad %v4float %BaseColor
- ; CHECK: [[ld1:%\w+]] = OpLoad %S_t %s0
- ; CHECK: [[ex1:%\w+]] = OpCompositeInsert %S_t [[st_id]] [[ld1]] 1
- ; CHECK: OpStore %s0 [[ex1]]
- ; CHECK: [[ld2:%\w+]] = OpLoad %S_t %s0
- ; CHECK: [[ex2:%\w+]] = OpCompositeExtract %v4float [[ld2]] 1
- ; CHECK: OpStore %gl_FragColor [[ex2]]
- %main = OpFunction %void None %8
- %17 = OpLabel
- %s0 = OpVariable %_ptr_Function_S_t Function
- %18 = OpLoad %v4float %BaseColor
- %19 = OpInBoundsAccessChain %_ptr_Function_v4float %s0 %int_1
- OpStore %19 %18
- %20 = OpInBoundsAccessChain %_ptr_Function_v4float %s0 %int_1
- %21 = OpLoad %v4float %20
- OpStore %gl_FragColor %21
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(predefs_before + before,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, TwoUsesofSingleChainConverted) {
- // #version 140
- //
- // in vec4 BaseColor;
- //
- // struct S_t {
- // vec4 v0;
- // vec4 v1;
- // };
- //
- // void main()
- // {
- // S_t s0;
- // s0.v1 = BaseColor;
- // gl_FragColor = s0.v1;
- // }
- const std::string predefs_before =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
- OpExecutionMode %main OriginUpperLeft
- OpSource GLSL 140
- OpName %main "main"
- OpName %S_t "S_t"
- OpMemberName %S_t 0 "v0"
- OpMemberName %S_t 1 "v1"
- OpName %s0 "s0"
- OpName %BaseColor "BaseColor"
- OpName %gl_FragColor "gl_FragColor"
- %void = OpTypeVoid
- %8 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %S_t = OpTypeStruct %v4float %v4float
- %_ptr_Function_S_t = OpTypePointer Function %S_t
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %_ptr_Input_v4float = OpTypePointer Input %v4float
- %BaseColor = OpVariable %_ptr_Input_v4float Input
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %gl_FragColor = OpVariable %_ptr_Output_v4float Output
- )";
- const std::string before =
- R"(
- ; CHECK: [[st_id:%\w+]] = OpLoad %v4float %BaseColor
- ; CHECK: [[ld1:%\w+]] = OpLoad %S_t %s0
- ; CHECK: [[ex1:%\w+]] = OpCompositeInsert %S_t [[st_id]] [[ld1]] 1
- ; CHECK: OpStore %s0 [[ex1]]
- ; CHECK: [[ld2:%\w+]] = OpLoad %S_t %s0
- ; CHECK: [[ex2:%\w+]] = OpCompositeExtract %v4float [[ld2]] 1
- ; CHECK: OpStore %gl_FragColor [[ex2]]
- %main = OpFunction %void None %8
- %17 = OpLabel
- %s0 = OpVariable %_ptr_Function_S_t Function
- %18 = OpLoad %v4float %BaseColor
- %19 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- OpStore %19 %18
- %20 = OpLoad %v4float %19
- OpStore %gl_FragColor %20
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(predefs_before + before,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, OpaqueConverted) {
- // SPIR-V not representable in GLSL; not generatable from HLSL
- // at the moment
- const std::string predefs =
- R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %outColor %texCoords
- OpExecutionMode %main OriginUpperLeft
- OpSource GLSL 140
- OpName %main "main"
- OpName %S_t "S_t"
- OpMemberName %S_t 0 "v0"
- OpMemberName %S_t 1 "v1"
- OpMemberName %S_t 2 "smp"
- OpName %foo_struct_S_t_vf2_vf21_ "foo(struct-S_t-vf2-vf21;"
- OpName %s "s"
- OpName %outColor "outColor"
- OpName %sampler15 "sampler15"
- OpName %s0 "s0"
- OpName %texCoords "texCoords"
- OpName %param "param"
- OpDecorate %sampler15 DescriptorSet 0
- %void = OpTypeVoid
- %12 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v2float = OpTypeVector %float 2
- %v4float = OpTypeVector %float 4
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %outColor = OpVariable %_ptr_Output_v4float Output
- %17 = OpTypeImage %float 2D 0 0 0 1 Unknown
- %18 = OpTypeSampledImage %17
- %S_t = OpTypeStruct %v2float %v2float %18
- %_ptr_Function_S_t = OpTypePointer Function %S_t
- %20 = OpTypeFunction %void %_ptr_Function_S_t
- %_ptr_UniformConstant_18 = OpTypePointer UniformConstant %18
- %_ptr_Function_18 = OpTypePointer Function %18
- %sampler15 = OpVariable %_ptr_UniformConstant_18 UniformConstant
- %int = OpTypeInt 32 1
- %int_0 = OpConstant %int 0
- %int_2 = OpConstant %int 2
- %_ptr_Function_v2float = OpTypePointer Function %v2float
- %_ptr_Input_v2float = OpTypePointer Input %v2float
- %texCoords = OpVariable %_ptr_Input_v2float Input
- )";
- const std::string before =
- R"(
- ; CHECK: [[l1:%\w+]] = OpLoad %S_t %param
- ; CHECK: [[e1:%\w+]] = OpCompositeExtract {{%\w+}} [[l1]] 2
- ; CHECK: [[l2:%\w+]] = OpLoad %S_t %param
- ; CHECK: [[e2:%\w+]] = OpCompositeExtract {{%\w+}} [[l2]] 0
- ; CHECK: OpImageSampleImplicitLod {{%\w+}} [[e1]] [[e2]]
- %main = OpFunction %void None %12
- %28 = OpLabel
- %s0 = OpVariable %_ptr_Function_S_t Function
- %param = OpVariable %_ptr_Function_S_t Function
- %29 = OpLoad %v2float %texCoords
- %30 = OpAccessChain %_ptr_Function_v2float %s0 %int_0
- OpStore %30 %29
- %31 = OpLoad %18 %sampler15
- %32 = OpAccessChain %_ptr_Function_18 %s0 %int_2
- OpStore %32 %31
- %33 = OpLoad %S_t %s0
- OpStore %param %33
- %34 = OpAccessChain %_ptr_Function_18 %param %int_2
- %35 = OpLoad %18 %34
- %36 = OpAccessChain %_ptr_Function_v2float %param %int_0
- %37 = OpLoad %v2float %36
- %38 = OpImageSampleImplicitLod %v4float %35 %37
- OpStore %outColor %38
- OpReturn
- OpFunctionEnd
- )";
- const std::string remain =
- R"(%foo_struct_S_t_vf2_vf21_ = OpFunction %void None %20
- %s = OpFunctionParameter %_ptr_Function_S_t
- %39 = OpLabel
- %40 = OpAccessChain %_ptr_Function_18 %s %int_2
- %41 = OpLoad %18 %40
- %42 = OpAccessChain %_ptr_Function_v2float %s %int_0
- %43 = OpLoad %v2float %42
- %44 = OpImageSampleImplicitLod %v4float %41 %43
- OpStore %outColor %44
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(predefs + before + remain,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, NestedStructsConverted) {
- // #version 140
- //
- // in vec4 BaseColor;
- //
- // struct S1_t {
- // vec4 v1;
- // };
- //
- // struct S2_t {
- // vec4 v2;
- // S1_t s1;
- // };
- //
- // void main()
- // {
- // S2_t s2;
- // s2.s1.v1 = BaseColor;
- // gl_FragColor = s2.s1.v1;
- // }
- const std::string predefs_before =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
- OpExecutionMode %main OriginUpperLeft
- OpSource GLSL 140
- OpName %main "main"
- OpName %S1_t "S1_t"
- OpMemberName %S1_t 0 "v1"
- OpName %S2_t "S2_t"
- OpMemberName %S2_t 0 "v2"
- OpMemberName %S2_t 1 "s1"
- OpName %s2 "s2"
- OpName %BaseColor "BaseColor"
- OpName %gl_FragColor "gl_FragColor"
- %void = OpTypeVoid
- %9 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %S1_t = OpTypeStruct %v4float
- %S2_t = OpTypeStruct %v4float %S1_t
- %_ptr_Function_S2_t = OpTypePointer Function %S2_t
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %int_0 = OpConstant %int 0
- %_ptr_Input_v4float = OpTypePointer Input %v4float
- %BaseColor = OpVariable %_ptr_Input_v4float Input
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %gl_FragColor = OpVariable %_ptr_Output_v4float Output
- )";
- const std::string before =
- R"(
- ; CHECK: [[st_id:%\w+]] = OpLoad %v4float %BaseColor
- ; CHECK: [[ld1:%\w+]] = OpLoad %S2_t %s2
- ; CHECK: [[ex1:%\w+]] = OpCompositeInsert %S2_t [[st_id]] [[ld1]] 1 0
- ; CHECK: OpStore %s2 [[ex1]]
- ; CHECK: [[ld2:%\w+]] = OpLoad %S2_t %s2
- ; CHECK: [[ex2:%\w+]] = OpCompositeExtract %v4float [[ld2]] 1 0
- ; CHECK: OpStore %gl_FragColor [[ex2]]
- %main = OpFunction %void None %9
- %19 = OpLabel
- %s2 = OpVariable %_ptr_Function_S2_t Function
- %20 = OpLoad %v4float %BaseColor
- %21 = OpAccessChain %_ptr_Function_v4float %s2 %int_1 %int_0
- OpStore %21 %20
- %22 = OpAccessChain %_ptr_Function_v4float %s2 %int_1 %int_0
- %23 = OpLoad %v4float %22
- OpStore %gl_FragColor %23
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(predefs_before + before,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, SomeAccessChainsHaveNoUse) {
- // Based on HLSL source code:
- // struct S {
- // float f;
- // };
- // float main(float input : A) : B {
- // S local = { input };
- // return local.f;
- // }
- const std::string predefs = R"(OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %main "main" %in_var_A %out_var_B
- OpName %main "main"
- OpName %in_var_A "in.var.A"
- OpName %out_var_B "out.var.B"
- OpName %S "S"
- OpName %local "local"
- %int = OpTypeInt 32 1
- %void = OpTypeVoid
- %8 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %_ptr_Function_float = OpTypePointer Function %float
- %_ptr_Input_float = OpTypePointer Input %float
- %_ptr_Output_float = OpTypePointer Output %float
- %S = OpTypeStruct %float
- %_ptr_Function_S = OpTypePointer Function %S
- %int_0 = OpConstant %int 0
- %in_var_A = OpVariable %_ptr_Input_float Input
- %out_var_B = OpVariable %_ptr_Output_float Output
- %main = OpFunction %void None %8
- %15 = OpLabel
- %local = OpVariable %_ptr_Function_S Function
- %16 = OpLoad %float %in_var_A
- %17 = OpCompositeConstruct %S %16
- OpStore %local %17
- )";
- const std::string before =
- R"(
- ; CHECK: [[ld:%\w+]] = OpLoad %S %local
- ; CHECK: [[ex:%\w+]] = OpCompositeExtract %float [[ld]] 0
- ; CHECK: OpStore %out_var_B [[ex]]
- %18 = OpAccessChain %_ptr_Function_float %local %int_0
- %19 = OpAccessChain %_ptr_Function_float %local %int_0
- %20 = OpLoad %float %18
- OpStore %out_var_B %20
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(predefs + before, true);
- }
- TEST_F(LocalAccessChainConvertTest,
- StructOfVecsOfFloatConvertedWithDecorationOnLoad) {
- // #version 140
- //
- // in vec4 BaseColor;
- //
- // struct S_t {
- // vec4 v0;
- // vec4 v1;
- // };
- //
- // void main()
- // {
- // S_t s0;
- // s0.v1 = BaseColor;
- // gl_FragColor = s0.v1;
- // }
- const std::string predefs_before =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
- OpExecutionMode %main OriginUpperLeft
- OpSource GLSL 140
- OpName %main "main"
- OpName %S_t "S_t"
- OpMemberName %S_t 0 "v0"
- OpMemberName %S_t 1 "v1"
- OpName %s0 "s0"
- OpName %BaseColor "BaseColor"
- OpName %gl_FragColor "gl_FragColor"
- OpDecorate %21 RelaxedPrecision
- %void = OpTypeVoid
- %8 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %S_t = OpTypeStruct %v4float %v4float
- %_ptr_Function_S_t = OpTypePointer Function %S_t
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %_ptr_Input_v4float = OpTypePointer Input %v4float
- %BaseColor = OpVariable %_ptr_Input_v4float Input
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %gl_FragColor = OpVariable %_ptr_Output_v4float Output
- )";
- const std::string before =
- R"(
- ; CHECK: OpDecorate
- ; CHECK: OpDecorate [[ld2:%\w+]] RelaxedPrecision
- ; CHECK-NOT: OpDecorate
- ; CHECK: [[st_id:%\w+]] = OpLoad %v4float %BaseColor
- ; CHECK: [[ld1:%\w+]] = OpLoad %S_t %s0
- ; CHECK: [[ins:%\w+]] = OpCompositeInsert %S_t [[st_id]] [[ld1]] 1
- ; CHECK: OpStore %s0 [[ins]]
- ; CHECK: [[ld2]] = OpLoad %S_t %s0
- ; CHECK: [[ex2:%\w+]] = OpCompositeExtract %v4float [[ld2]] 1
- ; CHECK: OpStore %gl_FragColor [[ex2]]
- %main = OpFunction %void None %8
- %17 = OpLabel
- %s0 = OpVariable %_ptr_Function_S_t Function
- %18 = OpLoad %v4float %BaseColor
- %19 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- OpStore %19 %18
- %20 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- %21 = OpLoad %v4float %20
- OpStore %gl_FragColor %21
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(predefs_before + before,
- true);
- }
- TEST_F(LocalAccessChainConvertTest,
- StructOfVecsOfFloatConvertedWithDecorationOnStore) {
- // #version 140
- //
- // in vec4 BaseColor;
- //
- // struct S_t {
- // vec4 v0;
- // vec4 v1;
- // };
- //
- // void main()
- // {
- // S_t s0;
- // s0.v1 = BaseColor;
- // gl_FragColor = s0.v1;
- // }
- const std::string predefs_before =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
- OpExecutionMode %main OriginUpperLeft
- OpSource GLSL 140
- OpName %main "main"
- OpName %S_t "S_t"
- OpMemberName %S_t 0 "v0"
- OpMemberName %S_t 1 "v1"
- OpName %s0 "s0"
- OpName %BaseColor "BaseColor"
- OpName %gl_FragColor "gl_FragColor"
- OpDecorate %s0 RelaxedPrecision
- %void = OpTypeVoid
- %8 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %S_t = OpTypeStruct %v4float %v4float
- %_ptr_Function_S_t = OpTypePointer Function %S_t
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %_ptr_Input_v4float = OpTypePointer Input %v4float
- %BaseColor = OpVariable %_ptr_Input_v4float Input
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %gl_FragColor = OpVariable %_ptr_Output_v4float Output
- )";
- const std::string before =
- R"(
- ; CHECK: OpDecorate
- ; CHECK: OpDecorate [[ld1:%\w+]] RelaxedPrecision
- ; CHECK: OpDecorate [[ins:%\w+]] RelaxedPrecision
- ; CHECK-NOT: OpDecorate
- ; CHECK: [[st_id:%\w+]] = OpLoad %v4float %BaseColor
- ; CHECK: [[ld1]] = OpLoad %S_t %s0
- ; CHECK: [[ins]] = OpCompositeInsert %S_t [[st_id]] [[ld1]] 1
- ; CHECK: OpStore %s0 [[ins]]
- ; CHECK: [[ld2:%\w+]] = OpLoad %S_t %s0
- ; CHECK: [[ex2:%\w+]] = OpCompositeExtract %v4float [[ld2]] 1
- ; CHECK: OpStore %gl_FragColor [[ex2]]
- %main = OpFunction %void None %8
- %17 = OpLabel
- %s0 = OpVariable %_ptr_Function_S_t Function
- %18 = OpLoad %v4float %BaseColor
- %19 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- OpStore %19 %18
- %20 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- %21 = OpLoad %v4float %20
- OpStore %gl_FragColor %21
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(predefs_before + before,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, DynamicallyIndexedVarNotConverted) {
- // #version 140
- //
- // in vec4 BaseColor;
- // flat in int Idx;
- // in float Bi;
- //
- // struct S_t {
- // vec4 v0;
- // vec4 v1;
- // };
- //
- // void main()
- // {
- // S_t s0;
- // s0.v1 = BaseColor;
- // s0.v1[Idx] = Bi;
- // gl_FragColor = s0.v1;
- // }
- const std::string assembly =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %BaseColor %Idx %Bi %gl_FragColor
- OpExecutionMode %main OriginUpperLeft
- OpSource GLSL 140
- OpName %main "main"
- OpName %S_t "S_t"
- OpMemberName %S_t 0 "v0"
- OpMemberName %S_t 1 "v1"
- OpName %s0 "s0"
- OpName %BaseColor "BaseColor"
- OpName %Idx "Idx"
- OpName %Bi "Bi"
- OpName %gl_FragColor "gl_FragColor"
- OpDecorate %Idx Flat
- %void = OpTypeVoid
- %10 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %S_t = OpTypeStruct %v4float %v4float
- %_ptr_Function_S_t = OpTypePointer Function %S_t
- %int = OpTypeInt 32 1
- %int_1 = OpConstant %int 1
- %_ptr_Input_v4float = OpTypePointer Input %v4float
- %BaseColor = OpVariable %_ptr_Input_v4float Input
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %_ptr_Input_int = OpTypePointer Input %int
- %Idx = OpVariable %_ptr_Input_int Input
- %_ptr_Input_float = OpTypePointer Input %float
- %Bi = OpVariable %_ptr_Input_float Input
- %_ptr_Function_float = OpTypePointer Function %float
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %gl_FragColor = OpVariable %_ptr_Output_v4float Output
- %main = OpFunction %void None %10
- %22 = OpLabel
- %s0 = OpVariable %_ptr_Function_S_t Function
- %23 = OpLoad %v4float %BaseColor
- %24 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- OpStore %24 %23
- %25 = OpLoad %int %Idx
- %26 = OpLoad %float %Bi
- %27 = OpAccessChain %_ptr_Function_float %s0 %int_1 %25
- OpStore %27 %26
- %28 = OpAccessChain %_ptr_Function_v4float %s0 %int_1
- %29 = OpLoad %v4float %28
- OpStore %gl_FragColor %29
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndCheck<LocalAccessChainConvertPass>(assembly, assembly, false,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, VariablePointersStorageBuffer) {
- // A case with a storage buffer variable pointer. We should still convert
- // the access chain on the function scope symbol.
- const std::string test =
- R"(
- ; CHECK: OpFunction
- ; CHECK: [[var:%\w+]] = OpVariable {{%\w+}} Function
- ; CHECK: [[ld:%\w+]] = OpLoad {{%\w+}} [[var]]
- ; CHECK: OpCompositeExtract {{%\w+}} [[ld]] 0 0
- OpCapability Shader
- OpCapability VariablePointersStorageBuffer
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint GLCompute %2 "main"
- OpExecutionMode %2 LocalSize 1 1 1
- OpSource GLSL 450
- OpMemberDecorate %_struct_3 0 Offset 0
- OpDecorate %_struct_3 Block
- OpDecorate %4 DescriptorSet 0
- OpDecorate %4 Binding 0
- OpDecorate %_ptr_StorageBuffer_int ArrayStride 4
- OpDecorate %_arr_int_int_128 ArrayStride 4
- %void = OpTypeVoid
- %8 = OpTypeFunction %void
- %int = OpTypeInt 32 1
- %int_128 = OpConstant %int 128
- %_arr_int_int_128 = OpTypeArray %int %int_128
- %_struct_3 = OpTypeStruct %_arr_int_int_128
- %_ptr_StorageBuffer__struct_3 = OpTypePointer StorageBuffer %_struct_3
- %_ptr_Function__struct_3 = OpTypePointer Function %_struct_3
- %4 = OpVariable %_ptr_StorageBuffer__struct_3 StorageBuffer
- %bool = OpTypeBool
- %true = OpConstantTrue %bool
- %int_0 = OpConstant %int 0
- %int_1 = OpConstant %int 1
- %_ptr_StorageBuffer_int = OpTypePointer StorageBuffer %int
- %_ptr_Function_int = OpTypePointer Function %int
- %2 = OpFunction %void None %8
- %18 = OpLabel
- %19 = OpVariable %_ptr_Function__struct_3 Function
- %20 = OpAccessChain %_ptr_StorageBuffer_int %4 %int_0 %int_0
- OpBranch %21
- %21 = OpLabel
- %22 = OpPhi %_ptr_StorageBuffer_int %20 %18 %23 %24
- OpLoopMerge %25 %24 None
- OpBranchConditional %true %26 %25
- %26 = OpLabel
- OpStore %22 %int_0
- OpBranch %24
- %24 = OpLabel
- %23 = OpPtrAccessChain %_ptr_StorageBuffer_int %22 %int_1
- OpBranch %21
- %25 = OpLabel
- %27 = OpAccessChain %_ptr_Function_int %19 %int_0 %int_0
- %28 = OpLoad %int %27
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(test, true);
- }
- TEST_F(LocalAccessChainConvertTest, VariablePointers) {
- // A case with variable pointer capability. We should not convert
- // the access chain on the function scope symbol because the variable pointer
- // could the analysis to miss references to function scope symbols.
- const std::string test =
- R"(OpCapability Shader
- OpCapability VariablePointers
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint GLCompute %2 "main"
- OpExecutionMode %2 LocalSize 1 1 1
- OpSource GLSL 450
- OpMemberDecorate %_struct_3 0 Offset 0
- OpDecorate %_struct_3 Block
- OpDecorate %4 DescriptorSet 0
- OpDecorate %4 Binding 0
- OpDecorate %_ptr_StorageBuffer_int ArrayStride 4
- OpDecorate %_arr_int_int_128 ArrayStride 4
- %void = OpTypeVoid
- %8 = OpTypeFunction %void
- %int = OpTypeInt 32 1
- %int_128 = OpConstant %int 128
- %_arr_int_int_128 = OpTypeArray %int %int_128
- %_struct_3 = OpTypeStruct %_arr_int_int_128
- %_ptr_StorageBuffer__struct_3 = OpTypePointer StorageBuffer %_struct_3
- %_ptr_Function__struct_3 = OpTypePointer Function %_struct_3
- %4 = OpVariable %_ptr_StorageBuffer__struct_3 StorageBuffer
- %bool = OpTypeBool
- %true = OpConstantTrue %bool
- %int_0 = OpConstant %int 0
- %int_1 = OpConstant %int 1
- %_ptr_StorageBuffer_int = OpTypePointer StorageBuffer %int
- %_ptr_Function_int = OpTypePointer Function %int
- %2 = OpFunction %void None %8
- %18 = OpLabel
- %19 = OpVariable %_ptr_Function__struct_3 Function
- %20 = OpAccessChain %_ptr_StorageBuffer_int %4 %int_0 %int_0
- OpBranch %21
- %21 = OpLabel
- %22 = OpPhi %_ptr_StorageBuffer_int %20 %18 %23 %24
- OpLoopMerge %25 %24 None
- OpBranchConditional %true %26 %25
- %26 = OpLabel
- OpStore %22 %int_0
- OpBranch %24
- %24 = OpLabel
- %23 = OpPtrAccessChain %_ptr_StorageBuffer_int %22 %int_1
- OpBranch %21
- %25 = OpLabel
- %27 = OpAccessChain %_ptr_Function_int %19 %int_0 %int_0
- %28 = OpLoad %int %27
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndCheck<LocalAccessChainConvertPass>(test, test, false, true);
- }
- TEST_F(LocalAccessChainConvertTest, IdOverflowReplacingLoad) {
- const std::string text =
- R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "PSMain"
- OpExecutionMode %4 OriginUpperLeft
- OpDecorate %10 Location 47360
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %_struct_8 = OpTypeStruct %v4float
- %_ptr_Function__struct_8 = OpTypePointer Function %_struct_8
- %int = OpTypeInt 32 1
- %int_0 = OpConstant %int 0
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %4 = OpFunction %void None %3
- %5 = OpLabel
- %10 = OpVariable %_ptr_Function__struct_8 Function
- %4194301 = OpAccessChain %_ptr_Function_v4float %10 %int_0
- %4194302 = OpLoad %v4float %4194301
- OpReturn
- OpFunctionEnd
- )";
- SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- std::vector<Message> messages = {
- {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."}};
- SetMessageConsumer(GetTestMessageConsumer(messages));
- auto result = SinglePassRunToBinary<LocalAccessChainConvertPass>(text, true);
- EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
- }
- TEST_F(LocalAccessChainConvertTest, IdOverflowReplacingStore1) {
- const std::string text =
- R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "PSMain"
- OpExecutionMode %4 OriginUpperLeft
- OpDecorate %10 Location 47360
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %_struct_7 = OpTypeStruct %v4float
- %_ptr_Function__struct_7 = OpTypePointer Function %_struct_7
- %int = OpTypeInt 32 1
- %int_0 = OpConstant %int 0
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %13 = OpConstantNull %v4float
- %4 = OpFunction %void None %3
- %5 = OpLabel
- %10 = OpVariable %_ptr_Function__struct_7 Function
- %4194302 = OpAccessChain %_ptr_Function_v4float %10 %int_0
- OpStore %4194302 %13
- OpReturn
- OpFunctionEnd
- )";
- SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- std::vector<Message> messages = {
- {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."}};
- SetMessageConsumer(GetTestMessageConsumer(messages));
- auto result = SinglePassRunToBinary<LocalAccessChainConvertPass>(text, true);
- EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
- }
- TEST_F(LocalAccessChainConvertTest, IdOverflowReplacingStore2) {
- const std::string text =
- R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "PSMain"
- OpExecutionMode %4 OriginUpperLeft
- OpDecorate %10 Location 47360
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %_struct_7 = OpTypeStruct %v4float
- %_ptr_Function__struct_7 = OpTypePointer Function %_struct_7
- %int = OpTypeInt 32 1
- %int_0 = OpConstant %int 0
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %13 = OpConstantNull %v4float
- %4 = OpFunction %void None %3
- %5 = OpLabel
- %10 = OpVariable %_ptr_Function__struct_7 Function
- %4194301 = OpAccessChain %_ptr_Function_v4float %10 %int_0
- OpStore %4194301 %13
- OpReturn
- OpFunctionEnd
- )";
- SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- std::vector<Message> messages = {
- {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."}};
- SetMessageConsumer(GetTestMessageConsumer(messages));
- auto result = SinglePassRunToBinary<LocalAccessChainConvertPass>(text, true);
- EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
- }
- TEST_F(LocalAccessChainConvertTest, AccessChainWithNoIndex) {
- const std::string before =
- R"(
- ; CHECK: OpFunction
- ; CHECK: [[var:%\w+]] = OpVariable
- ; CHECK: OpStore [[var]] %true
- ; CHECK: OpLoad %bool [[var]]
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %void = OpTypeVoid
- %4 = OpTypeFunction %void
- %bool = OpTypeBool
- %true = OpConstantTrue %bool
- %_ptr_Function_bool = OpTypePointer Function %bool
- %2 = OpFunction %void None %4
- %8 = OpLabel
- %9 = OpVariable %_ptr_Function_bool Function
- %10 = OpAccessChain %_ptr_Function_bool %9
- OpStore %10 %true
- %11 = OpLoad %bool %10
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(before, true);
- }
- TEST_F(LocalAccessChainConvertTest, AccessChainWithLongIndex) {
- // The access chain take a value that is larger than 32-bit. The index cannot
- // be encoded in an OpCompositeExtract, so nothing should be done.
- const std::string before =
- R"(OpCapability Shader
- OpCapability Int64
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main_0004f4d4_85b2f584"
- OpExecutionMode %2 OriginUpperLeft
- %ulong = OpTypeInt 64 0
- %ulong_8589934592 = OpConstant %ulong 8589934592
- %ulong_8589934591 = OpConstant %ulong 8589934591
- %_arr_ulong_ulong_8589934592 = OpTypeArray %ulong %ulong_8589934592
- %_ptr_Function__arr_ulong_ulong_8589934592 = OpTypePointer Function %_arr_ulong_ulong_8589934592
- %_ptr_Function_ulong = OpTypePointer Function %ulong
- %void = OpTypeVoid
- %10 = OpTypeFunction %void
- %2 = OpFunction %void None %10
- %11 = OpLabel
- %12 = OpVariable %_ptr_Function__arr_ulong_ulong_8589934592 Function
- %13 = OpAccessChain %_ptr_Function_ulong %12 %ulong_8589934591
- %14 = OpLoad %ulong %13
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndCheck<LocalAccessChainConvertPass>(before, before, false,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, AccessChainWith32BitIndexInLong) {
- // The access chain has a value that is 32-bits, but it is stored in a 64-bit
- // variable. This access change can be converted to an extract.
- const std::string before =
- R"(
- ; CHECK: OpFunction
- ; CHECK: [[var:%\w+]] = OpVariable
- ; CHECK: [[ld:%\w+]] = OpLoad {{%\w+}} [[var]]
- ; CHECK: OpCompositeExtract %ulong [[ld]] 3
- OpCapability Shader
- OpCapability Int64
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main_0004f4d4_85b2f584"
- OpExecutionMode %2 OriginUpperLeft
- %ulong = OpTypeInt 64 0
- %ulong_8589934592 = OpConstant %ulong 8589934592
- %ulong_3 = OpConstant %ulong 3
- %_arr_ulong_ulong_8589934592 = OpTypeArray %ulong %ulong_8589934592
- %_ptr_Function__arr_ulong_ulong_8589934592 = OpTypePointer Function %_arr_ulong_ulong_8589934592
- %_ptr_Function_ulong = OpTypePointer Function %ulong
- %void = OpTypeVoid
- %10 = OpTypeFunction %void
- %2 = OpFunction %void None %10
- %11 = OpLabel
- %12 = OpVariable %_ptr_Function__arr_ulong_ulong_8589934592 Function
- %13 = OpAccessChain %_ptr_Function_ulong %12 %ulong_3
- %14 = OpLoad %ulong %13
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(before, true);
- }
- TEST_F(LocalAccessChainConvertTest, AccessChainWithVarIndex) {
- // The access chain has a value that is not constant, so there should not be
- // any changes.
- const std::string before =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main_0004f4d4_85b2f584"
- OpExecutionMode %2 OriginUpperLeft
- %uint = OpTypeInt 32 0
- %uint_5 = OpConstant %uint 5
- %_arr_uint_uint_5 = OpTypeArray %uint %uint_5
- %_ptr_Function__arr_uint_uint_5 = OpTypePointer Function %_arr_uint_uint_5
- %_ptr_Function_uint = OpTypePointer Function %uint
- %8 = OpUndef %uint
- %void = OpTypeVoid
- %10 = OpTypeFunction %void
- %2 = OpFunction %void None %10
- %11 = OpLabel
- %12 = OpVariable %_ptr_Function__arr_uint_uint_5 Function
- %13 = OpAccessChain %_ptr_Function_uint %12 %8
- %14 = OpLoad %uint %13
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndCheck<LocalAccessChainConvertPass>(before, before, false,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, OutOfBoundsAccess) {
- // The access chain indexes element 12 in an array of size 10. Nothing should
- // be done.
- const std::string assembly =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- %void = OpTypeVoid
- %5 = OpTypeFunction %void
- %int = OpTypeInt 32 1
- %int_10 = OpConstant %int 10
- %_arr_int_int_10 = OpTypeArray %int %int_10
- %_ptr_Function_int = OpTypePointer Function %int
- %int_12 = OpConstant %int 12
- %_ptr_Output_int = OpTypePointer Output %int
- %3 = OpVariable %_ptr_Output_int Output
- %_ptr_Function__arr_int_int_10 = OpTypePointer Function %_arr_int_int_10
- %2 = OpFunction %void None %5
- %13 = OpLabel
- %14 = OpVariable %_ptr_Function__arr_int_int_10 Function
- %15 = OpAccessChain %_ptr_Function_int %14 %int_12
- %16 = OpLoad %int %15
- OpStore %3 %16
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndCheck<LocalAccessChainConvertPass>(assembly, assembly, false,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, OutOfBoundsAccessAtBoundary) {
- // The access chain indexes element 10 in an array of size 10. Nothing should
- // be done.
- const std::string assembly =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- %void = OpTypeVoid
- %5 = OpTypeFunction %void
- %int = OpTypeInt 32 1
- %int_10 = OpConstant %int 10
- %_arr_int_int_10 = OpTypeArray %int %int_10
- %_ptr_Function_int = OpTypePointer Function %int
- %_ptr_Output_int = OpTypePointer Output %int
- %3 = OpVariable %_ptr_Output_int Output
- %_ptr_Function__arr_int_int_10 = OpTypePointer Function %_arr_int_int_10
- %2 = OpFunction %void None %5
- %12 = OpLabel
- %13 = OpVariable %_ptr_Function__arr_int_int_10 Function
- %14 = OpAccessChain %_ptr_Function_int %13 %int_10
- %15 = OpLoad %int %14
- OpStore %3 %15
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndCheck<LocalAccessChainConvertPass>(assembly, assembly, false,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, NegativeIndex) {
- // The access chain has a negative index and should not be converted because
- // the extract instruction cannot hold a negative number.
- const std::string assembly =
- R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- %void = OpTypeVoid
- %4 = OpTypeFunction %void
- %int = OpTypeInt 32 1
- %uint = OpTypeInt 32 0
- %uint_3808428041 = OpConstant %uint 3808428041
- %_arr_int_uint_3808428041 = OpTypeArray %int %uint_3808428041
- %_ptr_Function__arr_int_uint_3808428041 = OpTypePointer Function %_arr_int_uint_3808428041
- %_ptr_Function_int = OpTypePointer Function %int
- %int_n1272971256 = OpConstant %int -1272971256
- %2 = OpFunction %void None %4
- %12 = OpLabel
- %13 = OpVariable %_ptr_Function__arr_int_uint_3808428041 Function
- %14 = OpAccessChain %_ptr_Function_int %13 %int_n1272971256
- %15 = OpLoad %int %14
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndCheck<LocalAccessChainConvertPass>(assembly, assembly, false,
- true);
- }
- TEST_F(LocalAccessChainConvertTest, VkMemoryModelTest) {
- const std::string text =
- R"(
- ; CHECK: OpCapability Shader
- ; CHECK: OpCapability VulkanMemoryModel
- ; CHECK: OpExtension "SPV_KHR_vulkan_memory_model"
- OpCapability Shader
- OpCapability VulkanMemoryModel
- OpExtension "SPV_KHR_vulkan_memory_model"
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical Vulkan
- OpEntryPoint GLCompute %main "main"
- OpExecutionMode %main LocalSize 1 1 1
- OpSource GLSL 450
- OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
- OpSourceExtension "GL_GOOGLE_include_directive"
- OpName %main "main"
- OpName %a "a"
- %void = OpTypeVoid
- %3 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %uint = OpTypeInt 32 0
- %uint_0 = OpConstant %uint 0
- %_ptr_Function_float = OpTypePointer Function %float
- %float_1 = OpConstant %float 1
- ; CHECK: OpFunction
- ; CHECK-NEXT: OpLabel
- ; CHECK-NEXT: [[a:%\w+]] = OpVariable
- ; Make sure the access chains were removed.
- ; CHECK: [[ld:%\w+]] = OpLoad {{%\w+}} [[a]]
- ; CHECK: [[ex:%\w+]] = OpCompositeExtract {{%\w+}} [[ld]] 0
- ; CHECK: [[ld2:%\w+]] = OpLoad {{%\w+}} [[a]]
- ; CHECK: [[v:%\w+]] = OpCompositeInsert {{%\w+}} [[ex]] [[ld2]] 0
- ; CHECK: OpStore [[a]] [[v]]
- %main = OpFunction %void None %3
- %5 = OpLabel
- %a = OpVariable %_ptr_Function_v4float Function
- %13 = OpAccessChain %_ptr_Function_float %a %uint_0
- %14 = OpLoad %float %13
- %17 = OpAccessChain %_ptr_Function_float %a %uint_0
- OpStore %17 %14
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndMatch<LocalAccessChainConvertPass>(text, false);
- }
- // TODO(greg-lunarg): Add tests to verify handling of these cases:
- //
- // Assorted vector and matrix types
- // Assorted struct array types
- // Assorted scalar types
- // Assorted non-target types
- // OpInBoundsAccessChain
- // Others?
- } // namespace
- } // namespace opt
- } // namespace spvtools
|