| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207 |
- // Copyright (c) 2020 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 "source/fuzz/transformation_flatten_conditional_branch.h"
- #include "gtest/gtest.h"
- #include "source/fuzz/counter_overflow_id_source.h"
- #include "source/fuzz/fuzzer_util.h"
- #include "source/fuzz/instruction_descriptor.h"
- #include "test/fuzz/fuzz_test_util.h"
- namespace spvtools {
- namespace fuzz {
- namespace {
- protobufs::SideEffectWrapperInfo MakeSideEffectWrapperInfo(
- const protobufs::InstructionDescriptor& instruction,
- uint32_t merge_block_id, uint32_t execute_block_id,
- uint32_t actual_result_id, uint32_t alternative_block_id,
- uint32_t placeholder_result_id, uint32_t value_to_copy_id) {
- protobufs::SideEffectWrapperInfo result;
- *result.mutable_instruction() = instruction;
- result.set_merge_block_id(merge_block_id);
- result.set_execute_block_id(execute_block_id);
- result.set_actual_result_id(actual_result_id);
- result.set_alternative_block_id(alternative_block_id);
- result.set_placeholder_result_id(placeholder_result_id);
- result.set_value_to_copy_id(value_to_copy_id);
- return result;
- }
- protobufs::SideEffectWrapperInfo MakeSideEffectWrapperInfo(
- const protobufs::InstructionDescriptor& instruction,
- uint32_t merge_block_id, uint32_t execute_block_id) {
- return MakeSideEffectWrapperInfo(instruction, merge_block_id,
- execute_block_id, 0, 0, 0, 0);
- }
- TEST(TransformationFlattenConditionalBranchTest, Inapplicable) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- OpName %2 "main"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypeInt 32 0
- %8 = OpConstant %7 0
- %9 = OpTypeBool
- %10 = OpConstantTrue %9
- %11 = OpTypePointer Function %6
- %12 = OpTypePointer Workgroup %6
- %3 = OpVariable %12 Workgroup
- %13 = OpConstant %6 2
- %2 = OpFunction %4 None %5
- %14 = OpLabel
- OpBranch %15
- %15 = OpLabel
- OpSelectionMerge %16 None
- OpSwitch %13 %17 2 %18
- %17 = OpLabel
- OpBranch %16
- %18 = OpLabel
- OpBranch %16
- %16 = OpLabel
- OpLoopMerge %19 %16 None
- OpBranchConditional %10 %16 %19
- %19 = OpLabel
- OpSelectionMerge %20 None
- OpBranchConditional %10 %21 %20
- %21 = OpLabel
- OpReturn
- %20 = OpLabel
- OpSelectionMerge %22 None
- OpBranchConditional %10 %23 %22
- %23 = OpLabel
- OpSelectionMerge %24 None
- OpBranchConditional %10 %25 %24
- %25 = OpLabel
- OpBranch %24
- %24 = OpLabel
- OpBranch %22
- %22 = OpLabel
- OpSelectionMerge %26 None
- OpBranchConditional %10 %26 %27
- %27 = OpLabel
- OpBranch %28
- %28 = OpLabel
- OpLoopMerge %29 %28 None
- OpBranchConditional %10 %28 %29
- %29 = OpLabel
- OpBranch %26
- %26 = OpLabel
- OpSelectionMerge %30 None
- OpBranchConditional %10 %30 %31
- %31 = OpLabel
- OpBranch %32
- %32 = OpLabel
- %33 = OpAtomicLoad %6 %3 %8 %8
- OpBranch %30
- %30 = OpLabel
- OpSelectionMerge %34 None
- OpBranchConditional %10 %35 %34
- %35 = OpLabel
- OpMemoryBarrier %8 %8
- OpBranch %34
- %34 = OpLabel
- OpLoopMerge %40 %39 None
- OpBranchConditional %10 %36 %40
- %36 = OpLabel
- OpSelectionMerge %38 None
- OpBranchConditional %10 %37 %38
- %37 = OpLabel
- OpBranch %40
- %38 = OpLabel
- OpBranch %39
- %39 = OpLabel
- OpBranch %34
- %40 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- // Block %15 does not end with OpBranchConditional.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(15, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context));
- // Block %17 is not a selection header.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(17, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context));
- // Block %16 is a loop header, not a selection header.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(16, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context));
- // Block %19 and the corresponding merge block do not describe a single-entry,
- // single-exit region, because there is a return instruction in %21.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(19, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context));
- // Block %20 is the header of a construct containing an inner selection
- // construct.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(20, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context));
- // Block %22 is the header of a construct containing an inner loop.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(22, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context));
- // Block %30 is the header of a construct containing a barrier instruction.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(30, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context));
- // %33 is not a block.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(33, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context));
- // Block %36 and the corresponding merge block do not describe a single-entry,
- // single-exit region, because block %37 breaks out of the outer loop.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(36, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context));
- }
- TEST(TransformationFlattenConditionalBranchTest, Simple) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- OpName %2 "main"
- %3 = OpTypeBool
- %4 = OpConstantTrue %3
- %5 = OpTypeVoid
- %6 = OpTypeFunction %5
- %2 = OpFunction %5 None %6
- %7 = OpLabel
- OpSelectionMerge %8 None
- OpBranchConditional %4 %9 %10
- %10 = OpLabel
- %26 = OpPhi %3 %4 %7
- OpBranch %8
- %9 = OpLabel
- %27 = OpPhi %3 %4 %7
- %11 = OpCopyObject %3 %4
- OpBranch %8
- %8 = OpLabel
- %12 = OpPhi %3 %11 %9 %4 %10
- %23 = OpPhi %3 %4 %9 %4 %10
- OpBranch %13
- %13 = OpLabel
- %14 = OpCopyObject %3 %4
- OpSelectionMerge %15 None
- OpBranchConditional %4 %16 %17
- %16 = OpLabel
- %28 = OpPhi %3 %4 %13
- OpBranch %18
- %18 = OpLabel
- OpBranch %19
- %17 = OpLabel
- %29 = OpPhi %3 %4 %13
- %20 = OpCopyObject %3 %4
- OpBranch %19
- %19 = OpLabel
- %21 = OpPhi %3 %4 %18 %20 %17
- OpBranch %15
- %15 = OpLabel
- OpSelectionMerge %22 None
- OpBranchConditional %4 %22 %22
- %22 = OpLabel
- %30 = OpPhi %3 %4 %15
- OpSelectionMerge %25 None
- OpBranchConditional %4 %24 %24
- %24 = OpLabel
- OpBranch %25
- %25 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation1 =
- TransformationFlattenConditionalBranch(7, true, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation1.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation1, context.get(),
- &transformation_context);
- auto transformation2 =
- TransformationFlattenConditionalBranch(13, false, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation2.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation2, context.get(),
- &transformation_context);
- auto transformation3 =
- TransformationFlattenConditionalBranch(15, true, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation3.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation3, context.get(),
- &transformation_context);
- auto transformation4 =
- TransformationFlattenConditionalBranch(22, false, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation4.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation4, context.get(),
- &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformations = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- OpName %2 "main"
- %3 = OpTypeBool
- %4 = OpConstantTrue %3
- %5 = OpTypeVoid
- %6 = OpTypeFunction %5
- %2 = OpFunction %5 None %6
- %7 = OpLabel
- OpBranch %9
- %9 = OpLabel
- %27 = OpPhi %3 %4 %7
- %11 = OpCopyObject %3 %4
- OpBranch %10
- %10 = OpLabel
- %26 = OpPhi %3 %4 %9
- OpBranch %8
- %8 = OpLabel
- %12 = OpSelect %3 %4 %11 %4
- %23 = OpSelect %3 %4 %4 %4
- OpBranch %13
- %13 = OpLabel
- %14 = OpCopyObject %3 %4
- OpBranch %17
- %17 = OpLabel
- %29 = OpPhi %3 %4 %13
- %20 = OpCopyObject %3 %4
- OpBranch %16
- %16 = OpLabel
- %28 = OpPhi %3 %4 %17
- OpBranch %18
- %18 = OpLabel
- OpBranch %19
- %19 = OpLabel
- %21 = OpSelect %3 %4 %4 %20
- OpBranch %15
- %15 = OpLabel
- OpBranch %22
- %22 = OpLabel
- %30 = OpPhi %3 %4 %15
- OpBranch %24
- %24 = OpLabel
- OpBranch %25
- %25 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformations, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest, LoadStoreFunctionCall) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %9 = OpTypeVoid
- %10 = OpTypeFunction %9
- %11 = OpTypeInt 32 1
- %12 = OpTypeVector %11 4
- %13 = OpTypeFunction %11
- %70 = OpConstant %11 0
- %14 = OpConstant %11 1
- %15 = OpTypeFloat 32
- %16 = OpTypeVector %15 2
- %17 = OpConstant %15 1
- %18 = OpConstantComposite %16 %17 %17
- %19 = OpTypeBool
- %20 = OpConstantTrue %19
- %21 = OpTypePointer Function %11
- %22 = OpTypeSampler
- %23 = OpTypeImage %9 2D 2 0 0 1 Unknown
- %24 = OpTypeSampledImage %23
- %25 = OpTypePointer Function %23
- %26 = OpTypePointer Function %22
- %27 = OpTypeInt 32 0
- %28 = OpConstant %27 2
- %29 = OpTypeArray %11 %28
- %30 = OpTypePointer Function %29
- %2 = OpFunction %9 None %10
- %31 = OpLabel
- %4 = OpVariable %21 Function
- %5 = OpVariable %30 Function
- %32 = OpVariable %25 Function
- %33 = OpVariable %26 Function
- %34 = OpLoad %23 %32
- %35 = OpLoad %22 %33
- OpSelectionMerge %36 None
- OpBranchConditional %20 %37 %36
- %37 = OpLabel
- %6 = OpLoad %11 %4
- %7 = OpIAdd %11 %6 %14
- OpStore %4 %7
- OpBranch %36
- %36 = OpLabel
- %42 = OpPhi %11 %14 %37 %14 %31
- OpSelectionMerge %43 None
- OpBranchConditional %20 %44 %45
- %44 = OpLabel
- %8 = OpFunctionCall %11 %3
- OpStore %4 %8
- OpBranch %46
- %45 = OpLabel
- %47 = OpAccessChain %21 %5 %14
- OpStore %47 %14
- OpBranch %46
- %46 = OpLabel
- OpStore %4 %14
- OpBranch %43
- %43 = OpLabel
- OpStore %4 %14
- OpSelectionMerge %48 None
- OpBranchConditional %20 %49 %48
- %49 = OpLabel
- OpBranch %48
- %48 = OpLabel
- OpSelectionMerge %50 None
- OpBranchConditional %20 %51 %50
- %51 = OpLabel
- %52 = OpSampledImage %24 %34 %35
- %53 = OpLoad %11 %4
- %54 = OpImageSampleImplicitLod %12 %52 %18
- OpBranch %50
- %50 = OpLabel
- OpReturn
- OpFunctionEnd
- %3 = OpFunction %11 None %13
- %55 = OpLabel
- OpReturnValue %14
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- #ifndef NDEBUG
- // The following checks lead to assertion failures, since some entries
- // requiring fresh ids are not present in the map, and the transformation
- // context does not have a source overflow ids.
- ASSERT_DEATH(TransformationFlattenConditionalBranch(31, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context),
- "Bad attempt to query whether overflow ids are available.");
- ASSERT_DEATH(TransformationFlattenConditionalBranch(
- 31, true, 0, 0, 0,
- {{MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(6, spv::Op::OpLoad, 0), 100,
- 101, 102, 103, 104, 14)}})
- .IsApplicable(context.get(), transformation_context),
- "Bad attempt to query whether overflow ids are available.");
- #endif
- // The map maps from an instruction to a list with not enough fresh ids.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(
- 31, true, 0, 0, 0,
- {{MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(6, spv::Op::OpLoad, 0), 100,
- 101, 102, 103, 0, 0)}})
- .IsApplicable(context.get(), transformation_context));
- // Not all fresh ids given are distinct.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(
- 31, true, 0, 0, 0,
- {{MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(6, spv::Op::OpLoad, 0), 100,
- 100, 102, 103, 104, 0)}})
- .IsApplicable(context.get(), transformation_context));
- // %48 heads a construct containing an OpSampledImage instruction.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(
- 48, true, 0, 0, 0,
- {{MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(53, spv::Op::OpLoad, 0), 100,
- 101, 102, 103, 104, 0)}})
- .IsApplicable(context.get(), transformation_context));
- // %0 is not a valid id.
- ASSERT_FALSE(
- TransformationFlattenConditionalBranch(
- 31, true, 0, 0, 0,
- {MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(6, spv::Op::OpLoad, 0), 104, 100, 101,
- 102, 103, 0),
- MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(6, spv::Op::OpStore, 0), 106, 105)})
- .IsApplicable(context.get(), transformation_context));
- // %17 is a float constant, while %6 has int type.
- ASSERT_FALSE(
- TransformationFlattenConditionalBranch(
- 31, true, 0, 0, 0,
- {MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(6, spv::Op::OpLoad, 0), 104, 100, 101,
- 102, 103, 17),
- MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(6, spv::Op::OpStore, 0), 106, 105)})
- .IsApplicable(context.get(), transformation_context));
- auto transformation1 = TransformationFlattenConditionalBranch(
- 31, true, 0, 0, 0,
- {MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(6, spv::Op::OpLoad, 0), 104, 100, 101, 102,
- 103, 70),
- MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(6, spv::Op::OpStore, 0), 106, 105)});
- ASSERT_TRUE(
- transformation1.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation1, context.get(),
- &transformation_context);
- // Check that the placeholder id was marked as irrelevant.
- ASSERT_TRUE(transformation_context.GetFactManager()->IdIsIrrelevant(103));
- // Make a new transformation context with a source of overflow ids.
- auto overflow_ids_unique_ptr = MakeUnique<CounterOverflowIdSource>(1000);
- auto overflow_ids_ptr = overflow_ids_unique_ptr.get();
- TransformationContext new_transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options,
- std::move(overflow_ids_unique_ptr));
- auto transformation2 = TransformationFlattenConditionalBranch(
- 36, false, 0, 0, 0,
- {MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(8, spv::Op::OpStore, 0), 114, 113)});
- ASSERT_TRUE(
- transformation2.IsApplicable(context.get(), new_transformation_context));
- ApplyAndCheckFreshIds(transformation2, context.get(),
- &new_transformation_context,
- overflow_ids_ptr->GetIssuedOverflowIds());
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformations = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %9 = OpTypeVoid
- %10 = OpTypeFunction %9
- %11 = OpTypeInt 32 1
- %12 = OpTypeVector %11 4
- %13 = OpTypeFunction %11
- %70 = OpConstant %11 0
- %14 = OpConstant %11 1
- %15 = OpTypeFloat 32
- %16 = OpTypeVector %15 2
- %17 = OpConstant %15 1
- %18 = OpConstantComposite %16 %17 %17
- %19 = OpTypeBool
- %20 = OpConstantTrue %19
- %21 = OpTypePointer Function %11
- %22 = OpTypeSampler
- %23 = OpTypeImage %9 2D 2 0 0 1 Unknown
- %24 = OpTypeSampledImage %23
- %25 = OpTypePointer Function %23
- %26 = OpTypePointer Function %22
- %27 = OpTypeInt 32 0
- %28 = OpConstant %27 2
- %29 = OpTypeArray %11 %28
- %30 = OpTypePointer Function %29
- %2 = OpFunction %9 None %10
- %31 = OpLabel
- %4 = OpVariable %21 Function
- %5 = OpVariable %30 Function
- %32 = OpVariable %25 Function
- %33 = OpVariable %26 Function
- %34 = OpLoad %23 %32
- %35 = OpLoad %22 %33
- OpBranch %37
- %37 = OpLabel
- OpSelectionMerge %104 None
- OpBranchConditional %20 %100 %102
- %100 = OpLabel
- %101 = OpLoad %11 %4
- OpBranch %104
- %102 = OpLabel
- %103 = OpCopyObject %11 %70
- OpBranch %104
- %104 = OpLabel
- %6 = OpPhi %11 %101 %100 %103 %102
- %7 = OpIAdd %11 %6 %14
- OpSelectionMerge %106 None
- OpBranchConditional %20 %105 %106
- %105 = OpLabel
- OpStore %4 %7
- OpBranch %106
- %106 = OpLabel
- OpBranch %36
- %36 = OpLabel
- %42 = OpSelect %11 %20 %14 %14
- OpBranch %45
- %45 = OpLabel
- %47 = OpAccessChain %21 %5 %14
- OpSelectionMerge %1005 None
- OpBranchConditional %20 %1005 %1006
- %1006 = OpLabel
- OpStore %47 %14
- OpBranch %1005
- %1005 = OpLabel
- OpBranch %44
- %44 = OpLabel
- OpSelectionMerge %1000 None
- OpBranchConditional %20 %1001 %1003
- %1001 = OpLabel
- %1002 = OpFunctionCall %11 %3
- OpBranch %1000
- %1003 = OpLabel
- %1004 = OpCopyObject %11 %70
- OpBranch %1000
- %1000 = OpLabel
- %8 = OpPhi %11 %1002 %1001 %1004 %1003
- OpSelectionMerge %114 None
- OpBranchConditional %20 %113 %114
- %113 = OpLabel
- OpStore %4 %8
- OpBranch %114
- %114 = OpLabel
- OpBranch %46
- %46 = OpLabel
- OpStore %4 %14
- OpBranch %43
- %43 = OpLabel
- OpStore %4 %14
- OpSelectionMerge %48 None
- OpBranchConditional %20 %49 %48
- %49 = OpLabel
- OpBranch %48
- %48 = OpLabel
- OpSelectionMerge %50 None
- OpBranchConditional %20 %51 %50
- %51 = OpLabel
- %52 = OpSampledImage %24 %34 %35
- %53 = OpLoad %11 %4
- %54 = OpImageSampleImplicitLod %12 %52 %18
- OpBranch %50
- %50 = OpLabel
- OpReturn
- OpFunctionEnd
- %3 = OpFunction %11 None %13
- %55 = OpLabel
- OpReturnValue %14
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformations, context.get()));
- } // namespace
- TEST(TransformationFlattenConditionalBranchTest, EdgeCases) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeBool
- %5 = OpConstantTrue %4
- %6 = OpTypeFunction %3
- %2 = OpFunction %3 None %6
- %7 = OpLabel
- OpSelectionMerge %8 None
- OpBranchConditional %5 %9 %8
- %9 = OpLabel
- %10 = OpFunctionCall %3 %11
- OpBranch %8
- %8 = OpLabel
- OpSelectionMerge %12 None
- OpBranchConditional %5 %13 %12
- %13 = OpLabel
- %14 = OpFunctionCall %3 %11
- OpBranch %12
- %12 = OpLabel
- OpReturn
- %16 = OpLabel
- OpSelectionMerge %17 None
- OpBranchConditional %5 %18 %17
- %18 = OpLabel
- OpBranch %17
- %17 = OpLabel
- OpReturn
- OpFunctionEnd
- %11 = OpFunction %3 None %6
- %19 = OpLabel
- OpBranch %20
- %20 = OpLabel
- OpSelectionMerge %25 None
- OpBranchConditional %5 %21 %22
- %21 = OpLabel
- OpBranch %22
- %22 = OpLabel
- OpSelectionMerge %24 None
- OpBranchConditional %5 %24 %23
- %23 = OpLabel
- OpBranch %24
- %24 = OpLabel
- OpBranch %25
- %25 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- #ifndef NDEBUG
- // The selection construct headed by %7 requires fresh ids because it contains
- // a function call. This causes an assertion failure because transformation
- // context does not have a source of overflow ids.
- ASSERT_DEATH(TransformationFlattenConditionalBranch(7, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context),
- "Bad attempt to query whether overflow ids are available.");
- #endif
- auto transformation1 = TransformationFlattenConditionalBranch(
- 7, true, 0, 0, 0,
- {{MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(10, spv::Op::OpFunctionCall, 0), 100,
- 101)}});
- ASSERT_TRUE(
- transformation1.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation1, context.get(),
- &transformation_context);
- // The selection construct headed by %8 cannot be flattened because it
- // contains a function call returning void, whose result id is used.
- ASSERT_FALSE(
- TransformationFlattenConditionalBranch(
- 7, true, 0, 0, 0,
- {{MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(14, spv::Op::OpFunctionCall, 0), 102,
- 103)}})
- .IsApplicable(context.get(), transformation_context));
- // Block %16 is unreachable.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(16, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context));
- auto transformation2 =
- TransformationFlattenConditionalBranch(20, false, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation2.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation2, context.get(),
- &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeBool
- %5 = OpConstantTrue %4
- %6 = OpTypeFunction %3
- %2 = OpFunction %3 None %6
- %7 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpSelectionMerge %100 None
- OpBranchConditional %5 %101 %100
- %101 = OpLabel
- %10 = OpFunctionCall %3 %11
- OpBranch %100
- %100 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpSelectionMerge %12 None
- OpBranchConditional %5 %13 %12
- %13 = OpLabel
- %14 = OpFunctionCall %3 %11
- OpBranch %12
- %12 = OpLabel
- OpReturn
- %16 = OpLabel
- OpSelectionMerge %17 None
- OpBranchConditional %5 %18 %17
- %18 = OpLabel
- OpBranch %17
- %17 = OpLabel
- OpReturn
- OpFunctionEnd
- %11 = OpFunction %3 None %6
- %19 = OpLabel
- OpBranch %20
- %20 = OpLabel
- OpBranch %21
- %21 = OpLabel
- OpBranch %22
- %22 = OpLabel
- OpSelectionMerge %24 None
- OpBranchConditional %5 %24 %23
- %23 = OpLabel
- OpBranch %24
- %24 = OpLabel
- OpBranch %25
- %25 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest, PhiToSelect1) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeBool
- %5 = OpConstantTrue %4
- %10 = OpConstantFalse %4
- %6 = OpTypeFunction %3
- %2 = OpFunction %3 None %6
- %7 = OpLabel
- OpSelectionMerge %8 None
- OpBranchConditional %5 %9 %8
- %9 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %11 = OpPhi %4 %5 %9 %10 %7
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation =
- TransformationFlattenConditionalBranch(7, true, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeBool
- %5 = OpConstantTrue %4
- %10 = OpConstantFalse %4
- %6 = OpTypeFunction %3
- %2 = OpFunction %3 None %6
- %7 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %11 = OpSelect %4 %5 %5 %10
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest, PhiToSelect2) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeBool
- %5 = OpConstantTrue %4
- %10 = OpConstantFalse %4
- %6 = OpTypeFunction %3
- %2 = OpFunction %3 None %6
- %7 = OpLabel
- OpSelectionMerge %8 None
- OpBranchConditional %5 %9 %8
- %9 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %11 = OpPhi %4 %10 %7 %5 %9
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation =
- TransformationFlattenConditionalBranch(7, true, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeBool
- %5 = OpConstantTrue %4
- %10 = OpConstantFalse %4
- %6 = OpTypeFunction %3
- %2 = OpFunction %3 None %6
- %7 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %11 = OpSelect %4 %5 %5 %10
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest, PhiToSelect3) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeBool
- %5 = OpConstantTrue %4
- %10 = OpConstantFalse %4
- %6 = OpTypeFunction %3
- %2 = OpFunction %3 None %6
- %7 = OpLabel
- OpSelectionMerge %8 None
- OpBranchConditional %5 %9 %12
- %9 = OpLabel
- OpBranch %8
- %12 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %11 = OpPhi %4 %10 %12 %5 %9
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation =
- TransformationFlattenConditionalBranch(7, true, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeBool
- %5 = OpConstantTrue %4
- %10 = OpConstantFalse %4
- %6 = OpTypeFunction %3
- %2 = OpFunction %3 None %6
- %7 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpBranch %12
- %12 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %11 = OpSelect %4 %5 %5 %10
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest, PhiToSelect4) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeBool
- %5 = OpConstantTrue %4
- %10 = OpConstantFalse %4
- %6 = OpTypeFunction %3
- %2 = OpFunction %3 None %6
- %7 = OpLabel
- OpSelectionMerge %8 None
- OpBranchConditional %5 %9 %12
- %9 = OpLabel
- OpBranch %8
- %12 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %11 = OpPhi %4 %5 %9 %10 %12
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation =
- TransformationFlattenConditionalBranch(7, true, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeBool
- %5 = OpConstantTrue %4
- %10 = OpConstantFalse %4
- %6 = OpTypeFunction %3
- %2 = OpFunction %3 None %6
- %7 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpBranch %12
- %12 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %11 = OpSelect %4 %5 %5 %10
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest, PhiToSelect5) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeBool
- %5 = OpConstantTrue %4
- %10 = OpConstantFalse %4
- %6 = OpTypeFunction %3
- %100 = OpTypePointer Function %4
- %2 = OpFunction %3 None %6
- %7 = OpLabel
- %101 = OpVariable %100 Function
- %102 = OpVariable %100 Function
- OpSelectionMerge %470 None
- OpBranchConditional %5 %454 %462
- %454 = OpLabel
- %522 = OpLoad %4 %101
- OpBranch %470
- %462 = OpLabel
- %466 = OpLoad %4 %102
- OpBranch %470
- %470 = OpLabel
- %534 = OpPhi %4 %522 %454 %466 %462
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation = TransformationFlattenConditionalBranch(
- 7, true, 0, 0, 0,
- {MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(522, spv::Op::OpLoad, 0), 200, 201, 202,
- 203, 204, 5),
- MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(466, spv::Op::OpLoad, 0), 300, 301, 302,
- 303, 304, 5)});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeBool
- %5 = OpConstantTrue %4
- %10 = OpConstantFalse %4
- %6 = OpTypeFunction %3
- %100 = OpTypePointer Function %4
- %2 = OpFunction %3 None %6
- %7 = OpLabel
- %101 = OpVariable %100 Function
- %102 = OpVariable %100 Function
- OpBranch %454
- %454 = OpLabel
- OpSelectionMerge %200 None
- OpBranchConditional %5 %201 %203
- %201 = OpLabel
- %202 = OpLoad %4 %101
- OpBranch %200
- %203 = OpLabel
- %204 = OpCopyObject %4 %5
- OpBranch %200
- %200 = OpLabel
- %522 = OpPhi %4 %202 %201 %204 %203
- OpBranch %462
- %462 = OpLabel
- OpSelectionMerge %300 None
- OpBranchConditional %5 %303 %301
- %301 = OpLabel
- %302 = OpLoad %4 %102
- OpBranch %300
- %303 = OpLabel
- %304 = OpCopyObject %4 %5
- OpBranch %300
- %300 = OpLabel
- %466 = OpPhi %4 %302 %301 %304 %303
- OpBranch %470
- %470 = OpLabel
- %534 = OpSelect %4 %5 %522 %466
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest,
- LoadFromBufferBlockDecoratedStruct) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- OpMemberDecorate %11 0 Offset 0
- OpDecorate %11 BufferBlock
- OpDecorate %13 DescriptorSet 0
- OpDecorate %13 Binding 0
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantTrue %6
- %10 = OpTypeInt 32 1
- %11 = OpTypeStruct %10
- %12 = OpTypePointer Uniform %11
- %13 = OpVariable %12 Uniform
- %21 = OpUndef %11
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %9 None
- OpBranchConditional %7 %8 %9
- %8 = OpLabel
- %20 = OpLoad %11 %13
- OpBranch %9
- %9 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation = TransformationFlattenConditionalBranch(
- 5, true, 0, 0, 0,
- {MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(20, spv::Op::OpLoad, 0), 100, 101, 102, 103,
- 104, 21)});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- }
- TEST(TransformationFlattenConditionalBranchTest, InapplicableSampledImageLoad) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main" %12 %96
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- OpDecorate %12 BuiltIn FragCoord
- OpDecorate %91 DescriptorSet 0
- OpDecorate %91 Binding 0
- OpDecorate %96 Location 0
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypeVector %6 2
- %10 = OpTypeVector %6 4
- %11 = OpTypePointer Input %10
- %12 = OpVariable %11 Input
- %21 = OpConstant %6 2
- %24 = OpTypeInt 32 1
- %33 = OpTypeBool
- %35 = OpConstantTrue %33
- %88 = OpTypeImage %6 2D 0 0 0 1 Unknown
- %89 = OpTypeSampledImage %88
- %90 = OpTypePointer UniformConstant %89
- %91 = OpVariable %90 UniformConstant
- %95 = OpTypePointer Output %10
- %96 = OpVariable %95 Output
- %200 = OpUndef %89
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %28
- %28 = OpLabel
- OpSelectionMerge %38 None
- OpBranchConditional %35 %32 %37
- %32 = OpLabel
- %40 = OpLoad %89 %91
- OpBranch %38
- %37 = OpLabel
- OpBranch %38
- %38 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- ASSERT_FALSE(TransformationFlattenConditionalBranch(
- 28, true, 0, 0, 0,
- {MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(40, spv::Op::OpLoad, 0), 100,
- 101, 102, 103, 104, 200)})
- .IsApplicable(context.get(), transformation_context));
- }
- TEST(TransformationFlattenConditionalBranchTest,
- InapplicablePhiToSelectVector) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantTrue %6
- %10 = OpTypeInt 32 1
- %11 = OpTypeVector %10 3
- %12 = OpUndef %11
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %20 None
- OpBranchConditional %7 %8 %9
- %8 = OpLabel
- OpBranch %20
- %9 = OpLabel
- OpBranch %20
- %20 = OpLabel
- %21 = OpPhi %11 %12 %8 %12 %9
- OpReturn
- OpFunctionEnd
- )";
- for (auto env :
- {SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1, SPV_ENV_UNIVERSAL_1_2,
- SPV_ENV_UNIVERSAL_1_3, SPV_ENV_VULKAN_1_0, SPV_ENV_VULKAN_1_1}) {
- const auto consumer = nullptr;
- const auto context =
- BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
- context.get(), validator_options, kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation =
- TransformationFlattenConditionalBranch(5, true, 0, 0, 0, {});
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- }
- }
- TEST(TransformationFlattenConditionalBranchTest,
- InapplicablePhiToSelectVector2) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %30 = OpTypeVector %6 3
- %31 = OpTypeVector %6 2
- %7 = OpConstantTrue %6
- %10 = OpTypeInt 32 1
- %11 = OpTypeVector %10 3
- %40 = OpTypeFloat 32
- %41 = OpTypeVector %40 4
- %12 = OpUndef %11
- %60 = OpUndef %41
- %61 = OpConstantComposite %31 %7 %7
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %20 None
- OpBranchConditional %7 %8 %9
- %8 = OpLabel
- OpBranch %20
- %9 = OpLabel
- OpBranch %20
- %20 = OpLabel
- %21 = OpPhi %11 %12 %8 %12 %9
- %22 = OpPhi %11 %12 %8 %12 %9
- %23 = OpPhi %41 %60 %8 %60 %9
- %24 = OpPhi %31 %61 %8 %61 %9
- %25 = OpPhi %41 %60 %8 %60 %9
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation =
- TransformationFlattenConditionalBranch(5, true, 101, 102, 103, {});
- // bvec4 is not present in the module.
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- }
- TEST(TransformationFlattenConditionalBranchTest,
- InapplicablePhiToSelectMatrix) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantTrue %6
- %10 = OpTypeFloat 32
- %30 = OpTypeVector %10 3
- %11 = OpTypeMatrix %30 3
- %12 = OpUndef %11
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %20 None
- OpBranchConditional %7 %8 %9
- %8 = OpLabel
- OpBranch %20
- %9 = OpLabel
- OpBranch %20
- %20 = OpLabel
- %21 = OpPhi %11 %12 %8 %12 %9
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation =
- TransformationFlattenConditionalBranch(5, true, 0, 0, 0, {});
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- }
- TEST(TransformationFlattenConditionalBranchTest, ApplicablePhiToSelectVector) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantTrue %6
- %10 = OpTypeInt 32 1
- %11 = OpTypeVector %10 3
- %12 = OpUndef %11
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %20 None
- OpBranchConditional %7 %8 %9
- %8 = OpLabel
- OpBranch %20
- %9 = OpLabel
- OpBranch %20
- %20 = OpLabel
- %21 = OpPhi %11 %12 %8 %12 %9
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation =
- TransformationFlattenConditionalBranch(5, true, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string expected_shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantTrue %6
- %10 = OpTypeInt 32 1
- %11 = OpTypeVector %10 3
- %12 = OpUndef %11
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpBranch %20
- %20 = OpLabel
- %21 = OpSelect %11 %7 %12 %12
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest, ApplicablePhiToSelectVector2) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %30 = OpTypeVector %6 3
- %31 = OpTypeVector %6 2
- %32 = OpTypeVector %6 4
- %7 = OpConstantTrue %6
- %10 = OpTypeInt 32 1
- %11 = OpTypeVector %10 3
- %40 = OpTypeFloat 32
- %41 = OpTypeVector %40 4
- %12 = OpUndef %11
- %60 = OpUndef %41
- %61 = OpConstantComposite %31 %7 %7
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %20 None
- OpBranchConditional %7 %8 %9
- %8 = OpLabel
- OpBranch %20
- %9 = OpLabel
- OpBranch %20
- %20 = OpLabel
- %21 = OpPhi %11 %12 %8 %12 %9
- %22 = OpPhi %11 %12 %8 %12 %9
- %23 = OpPhi %41 %60 %8 %60 %9
- %24 = OpPhi %31 %61 %8 %61 %9
- %25 = OpPhi %41 %60 %8 %60 %9
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- // No id for the 2D vector case is provided.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 0, 102, 103, {})
- .IsApplicable(context.get(), transformation_context));
- // No id for the 3D vector case is provided.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 101, 0, 103, {})
- .IsApplicable(context.get(), transformation_context));
- // No id for the 4D vector case is provided.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 101, 102, 0, {})
- .IsApplicable(context.get(), transformation_context));
- // %10 is not fresh
- ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 10, 102, 103, {})
- .IsApplicable(context.get(), transformation_context));
- // %10 is not fresh
- ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 101, 10, 103, {})
- .IsApplicable(context.get(), transformation_context));
- // %10 is not fresh
- ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 101, 102, 10, {})
- .IsApplicable(context.get(), transformation_context));
- // Duplicate "fresh" ids used for boolean vector constructors
- ASSERT_FALSE(
- TransformationFlattenConditionalBranch(5, true, 101, 102, 102, {})
- .IsApplicable(context.get(), transformation_context));
- auto transformation =
- TransformationFlattenConditionalBranch(5, true, 101, 102, 103, {});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string expected_shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %30 = OpTypeVector %6 3
- %31 = OpTypeVector %6 2
- %32 = OpTypeVector %6 4
- %7 = OpConstantTrue %6
- %10 = OpTypeInt 32 1
- %11 = OpTypeVector %10 3
- %40 = OpTypeFloat 32
- %41 = OpTypeVector %40 4
- %12 = OpUndef %11
- %60 = OpUndef %41
- %61 = OpConstantComposite %31 %7 %7
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpBranch %20
- %20 = OpLabel
- %103 = OpCompositeConstruct %32 %7 %7 %7 %7
- %102 = OpCompositeConstruct %30 %7 %7 %7
- %101 = OpCompositeConstruct %31 %7 %7
- %21 = OpSelect %11 %102 %12 %12
- %22 = OpSelect %11 %102 %12 %12
- %23 = OpSelect %41 %103 %60 %60
- %24 = OpSelect %31 %101 %61 %61
- %25 = OpSelect %41 %103 %60 %60
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest, ApplicablePhiToSelectVector3) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %30 = OpTypeVector %6 3
- %31 = OpTypeVector %6 2
- %32 = OpTypeVector %6 4
- %7 = OpConstantTrue %6
- %10 = OpTypeInt 32 1
- %11 = OpTypeVector %10 3
- %40 = OpTypeFloat 32
- %41 = OpTypeVector %40 4
- %12 = OpUndef %11
- %60 = OpUndef %41
- %61 = OpConstantComposite %31 %7 %7
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %20 None
- OpBranchConditional %7 %8 %9
- %8 = OpLabel
- OpBranch %20
- %9 = OpLabel
- OpBranch %20
- %20 = OpLabel
- %21 = OpPhi %11 %12 %8 %12 %9
- %22 = OpPhi %11 %12 %8 %12 %9
- %23 = OpPhi %41 %60 %8 %60 %9
- %24 = OpPhi %31 %61 %8 %61 %9
- %25 = OpPhi %41 %60 %8 %60 %9
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation =
- TransformationFlattenConditionalBranch(5, true, 101, 0, 103, {});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Check that the in operands of any OpSelect instructions all have the
- // appropriate operand type.
- context->module()->ForEachInst([](opt::Instruction* inst) {
- if (inst->opcode() == spv::Op::OpSelect) {
- ASSERT_EQ(SPV_OPERAND_TYPE_ID, inst->GetInOperand(0).type);
- ASSERT_EQ(SPV_OPERAND_TYPE_ID, inst->GetInOperand(1).type);
- ASSERT_EQ(SPV_OPERAND_TYPE_ID, inst->GetInOperand(2).type);
- }
- });
- std::string expected_shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %30 = OpTypeVector %6 3
- %31 = OpTypeVector %6 2
- %32 = OpTypeVector %6 4
- %7 = OpConstantTrue %6
- %10 = OpTypeInt 32 1
- %11 = OpTypeVector %10 3
- %40 = OpTypeFloat 32
- %41 = OpTypeVector %40 4
- %12 = OpUndef %11
- %60 = OpUndef %41
- %61 = OpConstantComposite %31 %7 %7
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpBranch %20
- %20 = OpLabel
- %103 = OpCompositeConstruct %32 %7 %7 %7 %7
- %101 = OpCompositeConstruct %31 %7 %7
- %21 = OpSelect %11 %7 %12 %12
- %22 = OpSelect %11 %7 %12 %12
- %23 = OpSelect %41 %103 %60 %60
- %24 = OpSelect %31 %101 %61 %61
- %25 = OpSelect %41 %103 %60 %60
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest, ApplicablePhiToSelectMatrix) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantTrue %6
- %10 = OpTypeFloat 32
- %30 = OpTypeVector %10 3
- %11 = OpTypeMatrix %30 3
- %12 = OpUndef %11
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %20 None
- OpBranchConditional %7 %8 %9
- %8 = OpLabel
- OpBranch %20
- %9 = OpLabel
- OpBranch %20
- %20 = OpLabel
- %21 = OpPhi %11 %12 %8 %12 %9
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation =
- TransformationFlattenConditionalBranch(5, true, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string expected_shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantTrue %6
- %10 = OpTypeFloat 32
- %30 = OpTypeVector %10 3
- %11 = OpTypeMatrix %30 3
- %12 = OpUndef %11
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpBranch %20
- %20 = OpLabel
- %21 = OpSelect %11 %7 %12 %12
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest,
- InapplicableConditionIsIrrelevant) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantTrue %6
- %10 = OpTypeInt 32 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %9 None
- OpBranchConditional %7 %8 %9
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- transformation_context.GetFactManager()->AddFactIdIsIrrelevant(7);
- // Inapplicable because the branch condition, %7, is irrelevant.
- ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context));
- }
- TEST(TransformationFlattenConditionalBranchTest,
- OpPhiWhenTrueBranchIsConvergenceBlock) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantTrue %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %9 None
- OpBranchConditional %7 %9 %8
- %8 = OpLabel
- %10 = OpCopyObject %6 %7
- OpBranch %9
- %9 = OpLabel
- %11 = OpPhi %6 %10 %8 %7 %5
- %12 = OpPhi %6 %7 %5 %10 %8
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- TransformationFlattenConditionalBranch transformation(5, true, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string expected = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantTrue %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %10 = OpCopyObject %6 %7
- OpBranch %9
- %9 = OpLabel
- %11 = OpSelect %6 %7 %7 %10
- %12 = OpSelect %6 %7 %7 %10
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, expected, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest,
- OpPhiWhenFalseBranchIsConvergenceBlock) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantTrue %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %9 None
- OpBranchConditional %7 %8 %9
- %8 = OpLabel
- %10 = OpCopyObject %6 %7
- OpBranch %9
- %9 = OpLabel
- %11 = OpPhi %6 %10 %8 %7 %5
- %12 = OpPhi %6 %7 %5 %10 %8
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- TransformationFlattenConditionalBranch transformation(5, true, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string expected = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantTrue %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %10 = OpCopyObject %6 %7
- OpBranch %9
- %9 = OpLabel
- %11 = OpSelect %6 %7 %10 %7
- %12 = OpSelect %6 %7 %10 %7
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, expected, context.get()));
- }
- TEST(TransformationFlattenConditionalBranchTest, ContainsDeadBlocksTest) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantFalse %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %9 None
- OpBranchConditional %7 %8 %9
- %8 = OpLabel
- %10 = OpCopyObject %6 %7
- OpBranch %9
- %9 = OpLabel
- %11 = OpPhi %6 %10 %8 %7 %5
- %12 = OpPhi %6 %7 %5 %10 %8
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- TransformationFlattenConditionalBranch transformation(5, true, 0, 0, 0, {});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- transformation_context.GetFactManager()->AddFactBlockIsDead(8);
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- }
- TEST(TransformationFlattenConditionalBranchTest, ContainsContinueBlockTest) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantFalse %6
- %4 = OpFunction %2 None %3
- %12 = OpLabel
- OpBranch %13
- %13 = OpLabel
- OpLoopMerge %15 %14 None
- OpBranchConditional %7 %5 %15
- %5 = OpLabel
- OpSelectionMerge %11 None
- OpBranchConditional %7 %9 %10
- %9 = OpLabel
- OpBranch %11
- %10 = OpLabel
- OpBranch %14
- %11 = OpLabel
- OpBranch %14
- %14 = OpLabel
- OpBranch %13
- %15 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 0, 0, 0, {})
- .IsApplicable(context.get(), transformation_context));
- }
- TEST(TransformationFlattenConditionalBranchTest, ContainsSynonymCreation) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %7 = OpConstantFalse %6
- %8 = OpTypeInt 32 0
- %9 = OpTypePointer Function %8
- %10 = OpConstant %8 42
- %80 = OpConstant %8 0
- %4 = OpFunction %2 None %3
- %11 = OpLabel
- %20 = OpVariable %9 Function
- OpBranch %12
- %12 = OpLabel
- OpSelectionMerge %31 None
- OpBranchConditional %7 %30 %31
- %30 = OpLabel
- OpStore %20 %10
- %21 = OpLoad %8 %20
- OpBranch %31
- %31 = OpLabel
- OpBranch %14
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- transformation_context.GetFactManager()->AddFactDataSynonym(
- MakeDataDescriptor(10, {}), MakeDataDescriptor(21, {}));
- ASSERT_FALSE(
- TransformationFlattenConditionalBranch(
- 12, true, 0, 0, 0,
- {MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(30, spv::Op::OpStore, 0), 100, 101),
- MakeSideEffectWrapperInfo(
- MakeInstructionDescriptor(21, spv::Op::OpLoad, 0), 102, 103, 104,
- 105, 106, 80)})
- .IsApplicable(context.get(), transformation_context));
- }
- } // namespace
- } // namespace fuzz
- } // namespace spvtools
|