| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545 |
- // Copyright (c) 2018 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 <memory>
- #include <vector>
- #include "gmock/gmock.h"
- #include "source/opt/loop_unroller.h"
- #include "source/opt/loop_utils.h"
- #include "source/opt/pass.h"
- #include "test/opt/assembly_builder.h"
- #include "test/opt/function_utils.h"
- #include "test/opt/pass_fixture.h"
- #include "test/opt/pass_utils.h"
- namespace spvtools {
- namespace opt {
- namespace {
- using ::testing::UnorderedElementsAre;
- using PassClassTest = PassTest<::testing::Test>;
- template <int factor>
- class PartialUnrollerTestPass : public Pass {
- public:
- PartialUnrollerTestPass() : Pass() {}
- const char* name() const override { return "Loop unroller"; }
- Status Process() override {
- bool changed = false;
- for (Function& f : *context()->module()) {
- if (f.IsDeclaration()) {
- continue;
- }
- LoopDescriptor& loop_descriptor = *context()->GetLoopDescriptor(&f);
- for (auto& loop : loop_descriptor) {
- LoopUtils loop_utils{context(), &loop};
- if (loop_utils.PartiallyUnroll(factor)) {
- changed = true;
- }
- }
- }
- if (changed) return Pass::Status::SuccessWithChange;
- return Pass::Status::SuccessWithoutChange;
- }
- };
- /*
- Generated from the following GLSL
- #version 410 core
- layout(location = 0) flat in int in_upper_bound;
- void main() {
- for (int i = 0; i < in_upper_bound; ++i) {
- x[i] = 1.0f;
- }
- }
- */
- TEST_F(PassClassTest, CheckUpperBound) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %3 "in_upper_bound"
- OpName %4 "x"
- OpDecorate %3 Flat
- OpDecorate %3 Location 0
- %5 = OpTypeVoid
- %6 = OpTypeFunction %5
- %7 = OpTypeInt 32 1
- %8 = OpTypePointer Function %7
- %9 = OpConstant %7 0
- %10 = OpTypePointer Input %7
- %3 = OpVariable %10 Input
- %11 = OpTypeBool
- %12 = OpTypeFloat 32
- %13 = OpTypeInt 32 0
- %14 = OpConstant %13 10
- %15 = OpTypeArray %12 %14
- %16 = OpTypePointer Function %15
- %17 = OpConstant %12 1
- %18 = OpTypePointer Function %12
- %19 = OpConstant %7 1
- %2 = OpFunction %5 None %6
- %20 = OpLabel
- %4 = OpVariable %16 Function
- OpBranch %21
- %21 = OpLabel
- %22 = OpPhi %7 %9 %20 %23 %24
- OpLoopMerge %25 %24 Unroll
- OpBranch %26
- %26 = OpLabel
- %27 = OpLoad %7 %3
- %28 = OpSLessThan %11 %22 %27
- OpBranchConditional %28 %29 %25
- %29 = OpLabel
- %30 = OpAccessChain %18 %4 %22
- OpStore %30 %17
- OpBranch %24
- %24 = OpLabel
- %23 = OpIAdd %7 %22 %19
- OpBranch %21
- %25 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // Make sure the pass doesn't run
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, text, false);
- }
- /*
- Generated from the following GLSL
- #version 410 core
- void main() {
- float out_array[10];
- for (uint i = 0; i < 2; i++) {
- for (float x = 0; x < 5; ++x) {
- out_array[x + i*5] = i;
- }
- }
- }
- */
- TEST_F(PassClassTest, UnrollNestedLoopsInvalid) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %3 "out_array"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 0
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 0
- %9 = OpConstant %6 2
- %10 = OpTypeBool
- %11 = OpTypeInt 32 1
- %12 = OpTypePointer Function %11
- %13 = OpConstant %11 0
- %14 = OpConstant %11 5
- %15 = OpTypeFloat 32
- %16 = OpConstant %6 10
- %17 = OpTypeArray %15 %16
- %18 = OpTypePointer Function %17
- %19 = OpConstant %6 5
- %20 = OpTypePointer Function %15
- %21 = OpConstant %11 1
- %22 = OpUndef %11
- %2 = OpFunction %4 None %5
- %23 = OpLabel
- %3 = OpVariable %18 Function
- OpBranch %24
- %24 = OpLabel
- %25 = OpPhi %6 %8 %23 %26 %27
- %28 = OpPhi %11 %22 %23 %29 %27
- OpLoopMerge %30 %27 Unroll
- OpBranch %31
- %31 = OpLabel
- %32 = OpULessThan %10 %25 %9
- OpBranchConditional %32 %33 %30
- %33 = OpLabel
- OpBranch %34
- %34 = OpLabel
- %29 = OpPhi %11 %13 %33 %35 %36
- OpLoopMerge %37 %36 None
- OpBranch %38
- %38 = OpLabel
- %39 = OpSLessThan %10 %29 %14
- OpBranchConditional %39 %40 %37
- %40 = OpLabel
- %41 = OpBitcast %6 %29
- %42 = OpIMul %6 %25 %19
- %43 = OpIAdd %6 %41 %42
- %44 = OpConvertUToF %15 %25
- %45 = OpAccessChain %20 %3 %43
- OpStore %45 %44
- OpBranch %36
- %36 = OpLabel
- %35 = OpIAdd %11 %29 %21
- OpBranch %34
- %37 = OpLabel
- OpBranch %27
- %27 = OpLabel
- %26 = OpIAdd %6 %25 %21
- OpBranch %24
- %30 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- }
- /*
- Generated from the following GLSL
- #version 440 core
- void main(){
- float x[10];
- for (int i = 0; i < 10; i++) {
- if (i == 5) {
- break;
- }
- x[i] = i;
- }
- }
- */
- TEST_F(PassClassTest, BreakInBody) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 440
- OpName %2 "main"
- OpName %3 "x"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 0
- %9 = OpConstant %6 10
- %10 = OpTypeBool
- %11 = OpConstant %6 5
- %12 = OpTypeFloat 32
- %13 = OpTypeInt 32 0
- %14 = OpConstant %13 10
- %15 = OpTypeArray %12 %14
- %16 = OpTypePointer Function %15
- %17 = OpTypePointer Function %12
- %18 = OpConstant %6 1
- %2 = OpFunction %4 None %5
- %19 = OpLabel
- %3 = OpVariable %16 Function
- OpBranch %20
- %20 = OpLabel
- %21 = OpPhi %6 %8 %19 %22 %23
- OpLoopMerge %24 %23 Unroll
- OpBranch %25
- %25 = OpLabel
- %26 = OpSLessThan %10 %21 %9
- OpBranchConditional %26 %27 %24
- %27 = OpLabel
- %28 = OpIEqual %10 %21 %11
- OpSelectionMerge %29 None
- OpBranchConditional %28 %30 %29
- %30 = OpLabel
- OpBranch %24
- %29 = OpLabel
- %31 = OpConvertSToF %12 %21
- %32 = OpAccessChain %17 %3 %21
- OpStore %32 %31
- OpBranch %23
- %23 = OpLabel
- %22 = OpIAdd %6 %21 %18
- OpBranch %20
- %24 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- }
- /*
- Generated from the following GLSL
- #version 440 core
- void main(){
- float x[10];
- for (int i = 0; i < 10; i++) {
- if (i == 5) {
- continue;
- }
- x[i] = i;
- }
- }
- */
- TEST_F(PassClassTest, ContinueInBody) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 440
- OpName %2 "main"
- OpName %3 "x"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 0
- %9 = OpConstant %6 10
- %10 = OpTypeBool
- %11 = OpConstant %6 5
- %12 = OpTypeFloat 32
- %13 = OpTypeInt 32 0
- %14 = OpConstant %13 10
- %15 = OpTypeArray %12 %14
- %16 = OpTypePointer Function %15
- %17 = OpTypePointer Function %12
- %18 = OpConstant %6 1
- %2 = OpFunction %4 None %5
- %19 = OpLabel
- %3 = OpVariable %16 Function
- OpBranch %20
- %20 = OpLabel
- %21 = OpPhi %6 %8 %19 %22 %23
- OpLoopMerge %24 %23 Unroll
- OpBranch %25
- %25 = OpLabel
- %26 = OpSLessThan %10 %21 %9
- OpBranchConditional %26 %27 %24
- %27 = OpLabel
- %28 = OpIEqual %10 %21 %11
- OpSelectionMerge %29 None
- OpBranchConditional %28 %30 %29
- %30 = OpLabel
- OpBranch %23
- %29 = OpLabel
- %31 = OpConvertSToF %12 %21
- %32 = OpAccessChain %17 %3 %21
- OpStore %32 %31
- OpBranch %23
- %23 = OpLabel
- %22 = OpIAdd %6 %21 %18
- OpBranch %20
- %24 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- }
- /*
- Generated from the following GLSL
- #version 440 core
- void main(){
- float x[10];
- for (int i = 0; i < 10; i++) {
- if (i == 5) {
- return;
- }
- x[i] = i;
- }
- }
- */
- TEST_F(PassClassTest, ReturnInBody) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 440
- OpName %2 "main"
- OpName %3 "x"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 0
- %9 = OpConstant %6 10
- %10 = OpTypeBool
- %11 = OpConstant %6 5
- %12 = OpTypeFloat 32
- %13 = OpTypeInt 32 0
- %14 = OpConstant %13 10
- %15 = OpTypeArray %12 %14
- %16 = OpTypePointer Function %15
- %17 = OpTypePointer Function %12
- %18 = OpConstant %6 1
- %2 = OpFunction %4 None %5
- %19 = OpLabel
- %3 = OpVariable %16 Function
- OpBranch %20
- %20 = OpLabel
- %21 = OpPhi %6 %8 %19 %22 %23
- OpLoopMerge %24 %23 Unroll
- OpBranch %25
- %25 = OpLabel
- %26 = OpSLessThan %10 %21 %9
- OpBranchConditional %26 %27 %24
- %27 = OpLabel
- %28 = OpIEqual %10 %21 %11
- OpSelectionMerge %29 None
- OpBranchConditional %28 %30 %29
- %30 = OpLabel
- OpReturn
- %29 = OpLabel
- %31 = OpConvertSToF %12 %21
- %32 = OpAccessChain %17 %3 %21
- OpStore %32 %31
- OpBranch %23
- %23 = OpLabel
- %22 = OpIAdd %6 %21 %18
- OpBranch %20
- %24 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- }
- TEST_F(PassClassTest, KillInBody) {
- const std::string text = R"(OpCapability Shader
- OpMemoryModel Logical Simple
- OpEntryPoint Fragment %1 "main"
- OpExecutionMode %1 OriginUpperLeft
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %4 = OpTypeBool
- %5 = OpTypeInt 32 0
- %6 = OpConstant %5 0
- %7 = OpConstant %5 1
- %8 = OpConstant %5 5
- %1 = OpFunction %2 None %3
- %9 = OpLabel
- OpBranch %10
- %10 = OpLabel
- %11 = OpPhi %5 %6 %9 %12 %13
- %14 = OpULessThan %4 %11 %8
- OpLoopMerge %15 %13 Unroll
- OpBranchConditional %14 %16 %15
- %16 = OpLabel
- OpKill
- %13 = OpLabel
- %12 = OpIAdd %5 %11 %7
- OpBranch %10
- %15 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- }
- TEST_F(PassClassTest, TerminateInvocationInBody) {
- const std::string text = R"(OpCapability Shader
- OpExtension "SPV_KHR_terminate_invocation"
- OpMemoryModel Logical Simple
- OpEntryPoint Fragment %1 "main"
- OpExecutionMode %1 OriginUpperLeft
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %4 = OpTypeBool
- %5 = OpTypeInt 32 0
- %6 = OpConstant %5 0
- %7 = OpConstant %5 1
- %8 = OpConstant %5 5
- %1 = OpFunction %2 None %3
- %9 = OpLabel
- OpBranch %10
- %10 = OpLabel
- %11 = OpPhi %5 %6 %9 %12 %13
- %14 = OpULessThan %4 %11 %8
- OpLoopMerge %15 %13 Unroll
- OpBranchConditional %14 %16 %15
- %16 = OpLabel
- OpTerminateInvocation
- %13 = OpLabel
- %12 = OpIAdd %5 %11 %7
- OpBranch %10
- %15 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- }
- /*
- Generated from the following GLSL
- #version 440 core
- void main() {
- int j = 0;
- for (int i = 0; i < 10 && i > 0; i++) {
- j++;
- }
- }
- */
- TEST_F(PassClassTest, MultipleConditionsSingleVariable) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 440
- OpName %2 "main"
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeInt 32 1
- %6 = OpTypePointer Function %5
- %7 = OpConstant %5 0
- %8 = OpConstant %5 10
- %9 = OpTypeBool
- %10 = OpConstant %5 1
- %2 = OpFunction %3 None %4
- %11 = OpLabel
- OpBranch %12
- %12 = OpLabel
- %13 = OpPhi %5 %7 %11 %14 %15
- %16 = OpPhi %5 %7 %11 %17 %15
- OpLoopMerge %18 %15 Unroll
- OpBranch %19
- %19 = OpLabel
- %20 = OpSLessThan %9 %16 %8
- %21 = OpSGreaterThan %9 %16 %7
- %22 = OpLogicalAnd %9 %20 %21
- OpBranchConditional %22 %23 %18
- %23 = OpLabel
- %14 = OpIAdd %5 %13 %10
- OpBranch %15
- %15 = OpLabel
- %17 = OpIAdd %5 %16 %10
- OpBranch %12
- %18 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // Make sure the pass doesn't run
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, text, false);
- }
- /*
- Generated from the following GLSL
- #version 440 core
- void main() {
- int i = 0;
- int j = 0;
- int k = 0;
- for (; i < 10 && j > 0; i++, j++) {
- k++;
- }
- }
- */
- TEST_F(PassClassTest, MultipleConditionsMultipleVariables) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 440
- OpName %2 "main"
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeInt 32 1
- %6 = OpTypePointer Function %5
- %7 = OpConstant %5 0
- %8 = OpConstant %5 10
- %9 = OpTypeBool
- %10 = OpConstant %5 1
- %2 = OpFunction %3 None %4
- %11 = OpLabel
- OpBranch %12
- %12 = OpLabel
- %13 = OpPhi %5 %7 %11 %14 %15
- %16 = OpPhi %5 %7 %11 %17 %15
- %18 = OpPhi %5 %7 %11 %19 %15
- OpLoopMerge %20 %15 Unroll
- OpBranch %21
- %21 = OpLabel
- %22 = OpSLessThan %9 %13 %8
- %23 = OpSGreaterThan %9 %16 %7
- %24 = OpLogicalAnd %9 %22 %23
- OpBranchConditional %24 %25 %20
- %25 = OpLabel
- %19 = OpIAdd %5 %18 %10
- OpBranch %15
- %15 = OpLabel
- %14 = OpIAdd %5 %13 %10
- %17 = OpIAdd %5 %16 %10
- OpBranch %12
- %20 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // Make sure the pass doesn't run
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, text, false);
- }
- /*
- Generated from the following GLSL
- #version 440 core
- void main() {
- float i = 0.0;
- int j = 0;
- for (; i < 10; i++) {
- j++;
- }
- }
- */
- TEST_F(PassClassTest, FloatingPointLoop) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 440
- OpName %2 "main"
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeFloat 32
- %6 = OpTypePointer Function %5
- %7 = OpConstant %5 0
- %8 = OpTypeInt 32 1
- %9 = OpTypePointer Function %8
- %10 = OpConstant %8 0
- %11 = OpConstant %5 10
- %12 = OpTypeBool
- %13 = OpConstant %8 1
- %14 = OpConstant %5 1
- %2 = OpFunction %3 None %4
- %15 = OpLabel
- OpBranch %16
- %16 = OpLabel
- %17 = OpPhi %5 %7 %15 %18 %19
- %20 = OpPhi %8 %10 %15 %21 %19
- OpLoopMerge %22 %19 Unroll
- OpBranch %23
- %23 = OpLabel
- %24 = OpFOrdLessThan %12 %17 %11
- OpBranchConditional %24 %25 %22
- %25 = OpLabel
- %21 = OpIAdd %8 %20 %13
- OpBranch %19
- %19 = OpLabel
- %18 = OpFAdd %5 %17 %14
- OpBranch %16
- %22 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // Make sure the pass doesn't run
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, text, false);
- }
- /*
- Generated from the following GLSL
- #version 440 core
- void main() {
- int i = 2;
- int j = 0;
- if (j == 0) { i = 5; }
- for (; i < 3; ++i) {
- j++;
- }
- }
- */
- TEST_F(PassClassTest, InductionPhiOutsideLoop) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 440
- OpName %2 "main"
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeInt 32 1
- %6 = OpTypePointer Function %5
- %7 = OpConstant %5 2
- %8 = OpConstant %5 0
- %9 = OpTypeBool
- %10 = OpConstant %5 5
- %11 = OpConstant %5 3
- %12 = OpConstant %5 1
- %2 = OpFunction %3 None %4
- %13 = OpLabel
- %14 = OpIEqual %9 %8 %8
- OpSelectionMerge %15 None
- OpBranchConditional %14 %16 %15
- %16 = OpLabel
- OpBranch %15
- %15 = OpLabel
- %17 = OpPhi %5 %7 %13 %10 %16
- OpBranch %18
- %18 = OpLabel
- %19 = OpPhi %5 %17 %15 %20 %21
- %22 = OpPhi %5 %8 %15 %23 %21
- OpLoopMerge %24 %21 Unroll
- OpBranch %25
- %25 = OpLabel
- %26 = OpSLessThan %9 %19 %11
- OpBranchConditional %26 %27 %24
- %27 = OpLabel
- %23 = OpIAdd %5 %22 %12
- OpBranch %21
- %21 = OpLabel
- %20 = OpIAdd %5 %19 %12
- OpBranch %18
- %24 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // Make sure the pass doesn't run
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, text, false);
- }
- /*
- Generated from the following GLSL
- #version 440 core
- void main() {
- int j = 0;
- for (int i = 0; i == 0; ++i) {
- ++j;
- }
- for (int i = 0; i != 3; ++i) {
- ++j;
- }
- for (int i = 0; i < 3; i *= 2) {
- ++j;
- }
- for (int i = 10; i > 3; i /= 2) {
- ++j;
- }
- for (int i = 10; i > 3; i |= 2) {
- ++j;
- }
- for (int i = 10; i > 3; i &= 2) {
- ++j;
- }
- for (int i = 10; i > 3; i ^= 2) {
- ++j;
- }
- for (int i = 0; i < 3; i << 2) {
- ++j;
- }
- for (int i = 10; i > 3; i >> 2) {
- ++j;
- }
- }
- */
- TEST_F(PassClassTest, UnsupportedLoopTypes) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 440
- OpName %2 "main"
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeInt 32 1
- %6 = OpTypePointer Function %5
- %7 = OpConstant %5 0
- %8 = OpTypeBool
- %9 = OpConstant %5 1
- %10 = OpConstant %5 3
- %11 = OpConstant %5 2
- %12 = OpConstant %5 10
- %2 = OpFunction %3 None %4
- %13 = OpLabel
- OpBranch %14
- %14 = OpLabel
- %15 = OpPhi %5 %7 %13 %16 %17
- %18 = OpPhi %5 %7 %13 %19 %17
- OpLoopMerge %20 %17 Unroll
- OpBranch %21
- %21 = OpLabel
- %22 = OpIEqual %8 %18 %7
- OpBranchConditional %22 %23 %20
- %23 = OpLabel
- %16 = OpIAdd %5 %15 %9
- OpBranch %17
- %17 = OpLabel
- %19 = OpIAdd %5 %18 %9
- OpBranch %14
- %20 = OpLabel
- OpBranch %24
- %24 = OpLabel
- %25 = OpPhi %5 %15 %20 %26 %27
- %28 = OpPhi %5 %7 %20 %29 %27
- OpLoopMerge %30 %27 Unroll
- OpBranch %31
- %31 = OpLabel
- %32 = OpINotEqual %8 %28 %10
- OpBranchConditional %32 %33 %30
- %33 = OpLabel
- %26 = OpIAdd %5 %25 %9
- OpBranch %27
- %27 = OpLabel
- %29 = OpIAdd %5 %28 %9
- OpBranch %24
- %30 = OpLabel
- OpBranch %34
- %34 = OpLabel
- %35 = OpPhi %5 %25 %30 %36 %37
- %38 = OpPhi %5 %7 %30 %39 %37
- OpLoopMerge %40 %37 Unroll
- OpBranch %41
- %41 = OpLabel
- %42 = OpSLessThan %8 %38 %10
- OpBranchConditional %42 %43 %40
- %43 = OpLabel
- %36 = OpIAdd %5 %35 %9
- OpBranch %37
- %37 = OpLabel
- %39 = OpIMul %5 %38 %11
- OpBranch %34
- %40 = OpLabel
- OpBranch %44
- %44 = OpLabel
- %45 = OpPhi %5 %35 %40 %46 %47
- %48 = OpPhi %5 %12 %40 %49 %47
- OpLoopMerge %50 %47 Unroll
- OpBranch %51
- %51 = OpLabel
- %52 = OpSGreaterThan %8 %48 %10
- OpBranchConditional %52 %53 %50
- %53 = OpLabel
- %46 = OpIAdd %5 %45 %9
- OpBranch %47
- %47 = OpLabel
- %49 = OpSDiv %5 %48 %11
- OpBranch %44
- %50 = OpLabel
- OpBranch %54
- %54 = OpLabel
- %55 = OpPhi %5 %45 %50 %56 %57
- %58 = OpPhi %5 %12 %50 %59 %57
- OpLoopMerge %60 %57 Unroll
- OpBranch %61
- %61 = OpLabel
- %62 = OpSGreaterThan %8 %58 %10
- OpBranchConditional %62 %63 %60
- %63 = OpLabel
- %56 = OpIAdd %5 %55 %9
- OpBranch %57
- %57 = OpLabel
- %59 = OpBitwiseOr %5 %58 %11
- OpBranch %54
- %60 = OpLabel
- OpBranch %64
- %64 = OpLabel
- %65 = OpPhi %5 %55 %60 %66 %67
- %68 = OpPhi %5 %12 %60 %69 %67
- OpLoopMerge %70 %67 Unroll
- OpBranch %71
- %71 = OpLabel
- %72 = OpSGreaterThan %8 %68 %10
- OpBranchConditional %72 %73 %70
- %73 = OpLabel
- %66 = OpIAdd %5 %65 %9
- OpBranch %67
- %67 = OpLabel
- %69 = OpBitwiseAnd %5 %68 %11
- OpBranch %64
- %70 = OpLabel
- OpBranch %74
- %74 = OpLabel
- %75 = OpPhi %5 %65 %70 %76 %77
- %78 = OpPhi %5 %12 %70 %79 %77
- OpLoopMerge %80 %77 Unroll
- OpBranch %81
- %81 = OpLabel
- %82 = OpSGreaterThan %8 %78 %10
- OpBranchConditional %82 %83 %80
- %83 = OpLabel
- %76 = OpIAdd %5 %75 %9
- OpBranch %77
- %77 = OpLabel
- %79 = OpBitwiseXor %5 %78 %11
- OpBranch %74
- %80 = OpLabel
- OpBranch %84
- %84 = OpLabel
- %85 = OpPhi %5 %75 %80 %86 %87
- OpLoopMerge %88 %87 Unroll
- OpBranch %89
- %89 = OpLabel
- %90 = OpSLessThan %8 %7 %10
- OpBranchConditional %90 %91 %88
- %91 = OpLabel
- %86 = OpIAdd %5 %85 %9
- OpBranch %87
- %87 = OpLabel
- %92 = OpShiftLeftLogical %5 %7 %11
- OpBranch %84
- %88 = OpLabel
- OpBranch %93
- %93 = OpLabel
- %94 = OpPhi %5 %85 %88 %95 %96
- OpLoopMerge %97 %96 Unroll
- OpBranch %98
- %98 = OpLabel
- %99 = OpSGreaterThan %8 %12 %10
- OpBranchConditional %99 %100 %97
- %100 = OpLabel
- %95 = OpIAdd %5 %94 %9
- OpBranch %96
- %96 = OpLabel
- %101 = OpShiftRightArithmetic %5 %12 %11
- OpBranch %93
- %97 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // Make sure the pass doesn't run
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, text, false);
- }
- /*
- #version 430
- layout(location = 0) out float o;
- void main(void) {
- for (int j = 2; j < 0; j += 1) {
- o += 1.0;
- }
- }
- */
- TEST_F(PassClassTest, NegativeNumberOfIterations) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %2 "main"
- OpName %3 "o"
- OpDecorate %3 Location 0
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 2
- %9 = OpConstant %6 0
- %10 = OpTypeBool
- %11 = OpTypeFloat 32
- %12 = OpTypePointer Output %11
- %3 = OpVariable %12 Output
- %13 = OpConstant %11 1
- %14 = OpConstant %6 1
- %2 = OpFunction %4 None %5
- %15 = OpLabel
- OpBranch %16
- %16 = OpLabel
- %17 = OpPhi %6 %8 %15 %18 %19
- OpLoopMerge %20 %19 None
- OpBranch %21
- %21 = OpLabel
- %22 = OpSLessThan %10 %17 %9
- OpBranchConditional %22 %23 %20
- %23 = OpLabel
- %24 = OpLoad %11 %3
- %25 = OpFAdd %11 %24 %13
- OpStore %3 %25
- OpBranch %19
- %19 = OpLabel
- %18 = OpIAdd %6 %17 %14
- OpBranch %16
- %20 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // Make sure the pass doesn't run
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, text, false);
- }
- /*
- #version 430
- layout(location = 0) out float o;
- void main(void) {
- float s = 0.0;
- for (int j = 0; j < 3; j += 1) {
- s += 1.0;
- j += 1;
- }
- o = s;
- }
- */
- TEST_F(PassClassTest, MultipleStepOperations) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %2 "main"
- OpName %3 "o"
- OpDecorate %3 Location 0
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 0
- %9 = OpTypeInt 32 1
- %10 = OpTypePointer Function %9
- %11 = OpConstant %9 0
- %12 = OpConstant %9 3
- %13 = OpTypeBool
- %14 = OpConstant %6 1
- %15 = OpConstant %9 1
- %16 = OpTypePointer Output %6
- %3 = OpVariable %16 Output
- %2 = OpFunction %4 None %5
- %17 = OpLabel
- OpBranch %18
- %18 = OpLabel
- %19 = OpPhi %6 %8 %17 %20 %21
- %22 = OpPhi %9 %11 %17 %23 %21
- OpLoopMerge %24 %21 Unroll
- OpBranch %25
- %25 = OpLabel
- %26 = OpSLessThan %13 %22 %12
- OpBranchConditional %26 %27 %24
- %27 = OpLabel
- %20 = OpFAdd %6 %19 %14
- %28 = OpIAdd %9 %22 %15
- OpBranch %21
- %21 = OpLabel
- %23 = OpIAdd %9 %28 %15
- OpBranch %18
- %24 = OpLabel
- OpStore %3 %19
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // Make sure the pass doesn't run
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, text, false);
- }
- /*
- #version 430
- layout(location = 0) out float o;
- void main(void) {
- float s = 0.0;
- for (int j = 10; j > 20; j -= 1) {
- s += 1.0;
- }
- o = s;
- }
- */
- TEST_F(PassClassTest, ConditionFalseFromStartGreaterThan) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %2 "main"
- OpName %3 "o"
- OpDecorate %3 Location 0
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 0
- %9 = OpTypeInt 32 1
- %10 = OpTypePointer Function %9
- %11 = OpConstant %9 10
- %12 = OpConstant %9 20
- %13 = OpTypeBool
- %14 = OpConstant %6 1
- %15 = OpConstant %9 1
- %16 = OpTypePointer Output %6
- %3 = OpVariable %16 Output
- %2 = OpFunction %4 None %5
- %17 = OpLabel
- OpBranch %18
- %18 = OpLabel
- %19 = OpPhi %6 %8 %17 %20 %21
- %22 = OpPhi %9 %11 %17 %23 %21
- OpLoopMerge %24 %21 Unroll
- OpBranch %25
- %25 = OpLabel
- %26 = OpSGreaterThan %13 %22 %12
- OpBranchConditional %26 %27 %24
- %27 = OpLabel
- %20 = OpFAdd %6 %19 %14
- OpBranch %21
- %21 = OpLabel
- %23 = OpISub %9 %22 %15
- OpBranch %18
- %24 = OpLabel
- OpStore %3 %19
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // Make sure the pass doesn't run
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, text, false);
- }
- /*
- #version 430
- layout(location = 0) out float o;
- void main(void) {
- float s = 0.0;
- for (int j = 10; j >= 20; j -= 1) {
- s += 1.0;
- }
- o = s;
- }
- */
- TEST_F(PassClassTest, ConditionFalseFromStartGreaterThanOrEqual) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %2 "main"
- OpName %3 "o"
- OpDecorate %3 Location 0
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 0
- %9 = OpTypeInt 32 1
- %10 = OpTypePointer Function %9
- %11 = OpConstant %9 10
- %12 = OpConstant %9 20
- %13 = OpTypeBool
- %14 = OpConstant %6 1
- %15 = OpConstant %9 1
- %16 = OpTypePointer Output %6
- %3 = OpVariable %16 Output
- %2 = OpFunction %4 None %5
- %17 = OpLabel
- OpBranch %18
- %18 = OpLabel
- %19 = OpPhi %6 %8 %17 %20 %21
- %22 = OpPhi %9 %11 %17 %23 %21
- OpLoopMerge %24 %21 Unroll
- OpBranch %25
- %25 = OpLabel
- %26 = OpSGreaterThanEqual %13 %22 %12
- OpBranchConditional %26 %27 %24
- %27 = OpLabel
- %20 = OpFAdd %6 %19 %14
- OpBranch %21
- %21 = OpLabel
- %23 = OpISub %9 %22 %15
- OpBranch %18
- %24 = OpLabel
- OpStore %3 %19
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // Make sure the pass doesn't run
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, text, false);
- }
- /*
- #version 430
- layout(location = 0) out float o;
- void main(void) {
- float s = 0.0;
- for (int j = 20; j < 10; j -= 1) {
- s += 1.0;
- }
- o = s;
- }
- */
- TEST_F(PassClassTest, ConditionFalseFromStartLessThan) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %2 "main"
- OpName %3 "o"
- OpDecorate %3 Location 0
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 0
- %9 = OpTypeInt 32 1
- %10 = OpTypePointer Function %9
- %11 = OpConstant %9 20
- %12 = OpConstant %9 10
- %13 = OpTypeBool
- %14 = OpConstant %6 1
- %15 = OpConstant %9 1
- %16 = OpTypePointer Output %6
- %3 = OpVariable %16 Output
- %2 = OpFunction %4 None %5
- %17 = OpLabel
- OpBranch %18
- %18 = OpLabel
- %19 = OpPhi %6 %8 %17 %20 %21
- %22 = OpPhi %9 %11 %17 %23 %21
- OpLoopMerge %24 %21 Unroll
- OpBranch %25
- %25 = OpLabel
- %26 = OpSLessThan %13 %22 %12
- OpBranchConditional %26 %27 %24
- %27 = OpLabel
- %20 = OpFAdd %6 %19 %14
- OpBranch %21
- %21 = OpLabel
- %23 = OpISub %9 %22 %15
- OpBranch %18
- %24 = OpLabel
- OpStore %3 %19
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // Make sure the pass doesn't run
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, text, false);
- }
- /*
- #version 430
- layout(location = 0) out float o;
- void main(void) {
- float s = 0.0;
- for (int j = 20; j <= 10; j -= 1) {
- s += 1.0;
- }
- o = s;
- }
- */
- TEST_F(PassClassTest, ConditionFalseFromStartLessThanEqual) {
- // clang-format off
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %2 "main"
- OpName %3 "o"
- OpDecorate %3 Location 0
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 0
- %9 = OpTypeInt 32 1
- %10 = OpTypePointer Function %9
- %11 = OpConstant %9 20
- %12 = OpConstant %9 10
- %13 = OpTypeBool
- %14 = OpConstant %6 1
- %15 = OpConstant %9 1
- %16 = OpTypePointer Output %6
- %3 = OpVariable %16 Output
- %2 = OpFunction %4 None %5
- %17 = OpLabel
- OpBranch %18
- %18 = OpLabel
- %19 = OpPhi %6 %8 %17 %20 %21
- %22 = OpPhi %9 %11 %17 %23 %21
- OpLoopMerge %24 %21 Unroll
- OpBranch %25
- %25 = OpLabel
- %26 = OpSLessThanEqual %13 %22 %12
- OpBranchConditional %26 %27 %24
- %27 = OpLabel
- %20 = OpFAdd %6 %19 %14
- OpBranch %21
- %21 = OpLabel
- %23 = OpISub %9 %22 %15
- OpBranch %18
- %24 = OpLabel
- OpStore %3 %19
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // Make sure the pass doesn't run
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, text, false);
- }
- TEST_F(PassClassTest, FunctionDeclaration) {
- // Make sure the pass works with a function declaration that is called.
- const std::string text = R"(OpCapability Addresses
- OpCapability Linkage
- OpCapability Kernel
- OpCapability Int8
- %1 = OpExtInstImport "OpenCL.std"
- OpMemoryModel Physical64 OpenCL
- OpEntryPoint Kernel %2 "_Z23julia__1166_kernel_77094Bool"
- OpExecutionMode %2 ContractionOff
- OpSource Unknown 0
- OpDecorate %3 LinkageAttributes "julia_error_7712" Import
- %void = OpTypeVoid
- %5 = OpTypeFunction %void
- %3 = OpFunction %void None %5
- OpFunctionEnd
- %2 = OpFunction %void None %5
- %6 = OpLabel
- %7 = OpFunctionCall %void %3
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- SinglePassRunAndCheck<PartialUnrollerTestPass<1>>(text, text, false);
- }
- } // namespace
- } // namespace opt
- } // namespace spvtools
|