| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536 |
- // Copyright (c) 2016 Google Inc.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #include <memory>
- #include <string>
- #include <utility>
- #include <vector>
- #include "gmock/gmock.h"
- #include "source/opt/instruction.h"
- #include "source/opt/ir_context.h"
- #include "spirv-tools/libspirv.h"
- #include "test/opt/pass_fixture.h"
- #include "test/opt/pass_utils.h"
- #include "test/unit_spirv.h"
- namespace spvtools {
- namespace opt {
- namespace {
- using ::testing::Eq;
- using spvtest::MakeInstruction;
- using DescriptorTypeTest = PassTest<::testing::Test>;
- using OpaqueTypeTest = PassTest<::testing::Test>;
- using GetBaseTest = PassTest<::testing::Test>;
- using ValidBasePointerTest = PassTest<::testing::Test>;
- using VulkanBufferTest = PassTest<::testing::Test>;
- TEST(InstructionTest, CreateTrivial) {
- Instruction empty;
- EXPECT_EQ(SpvOpNop, empty.opcode());
- EXPECT_EQ(0u, empty.type_id());
- EXPECT_EQ(0u, empty.result_id());
- EXPECT_EQ(0u, empty.NumOperands());
- EXPECT_EQ(0u, empty.NumOperandWords());
- EXPECT_EQ(0u, empty.NumInOperandWords());
- EXPECT_EQ(empty.cend(), empty.cbegin());
- EXPECT_EQ(empty.end(), empty.begin());
- }
- TEST(InstructionTest, CreateWithOpcodeAndNoOperands) {
- IRContext context(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction inst(&context, SpvOpReturn);
- EXPECT_EQ(SpvOpReturn, inst.opcode());
- EXPECT_EQ(0u, inst.type_id());
- EXPECT_EQ(0u, inst.result_id());
- EXPECT_EQ(0u, inst.NumOperands());
- EXPECT_EQ(0u, inst.NumOperandWords());
- EXPECT_EQ(0u, inst.NumInOperandWords());
- EXPECT_EQ(inst.cend(), inst.cbegin());
- EXPECT_EQ(inst.end(), inst.begin());
- }
- TEST(InstructionTest, OperandAsCString) {
- Operand::OperandData abcde{0x64636261, 0x65};
- Operand operand(SPV_OPERAND_TYPE_LITERAL_STRING, std::move(abcde));
- EXPECT_STREQ("abcde", operand.AsCString());
- }
- TEST(InstructionTest, OperandAsString) {
- Operand::OperandData abcde{0x64636261, 0x65};
- Operand operand(SPV_OPERAND_TYPE_LITERAL_STRING, std::move(abcde));
- EXPECT_EQ("abcde", operand.AsString());
- }
- TEST(InstructionTest, OperandAsLiteralUint64_32bits) {
- Operand::OperandData words{0x1234};
- Operand operand(SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER, std::move(words));
- EXPECT_EQ(uint64_t(0x1234), operand.AsLiteralUint64());
- }
- TEST(InstructionTest, OperandAsLiteralUint64_64bits) {
- Operand::OperandData words{0x1234, 0x89ab};
- Operand operand(SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER, std::move(words));
- EXPECT_EQ((uint64_t(0x89ab) << 32 | 0x1234), operand.AsLiteralUint64());
- }
- // The words for an OpTypeInt for 32-bit signed integer resulting in Id 44.
- uint32_t kSampleInstructionWords[] = {(4 << 16) | uint32_t(SpvOpTypeInt), 44,
- 32, 1};
- // The operands that would be parsed from kSampleInstructionWords
- spv_parsed_operand_t kSampleParsedOperands[] = {
- {1, 1, SPV_OPERAND_TYPE_RESULT_ID, SPV_NUMBER_NONE, 0},
- {2, 1, SPV_OPERAND_TYPE_LITERAL_INTEGER, SPV_NUMBER_UNSIGNED_INT, 32},
- {3, 1, SPV_OPERAND_TYPE_LITERAL_INTEGER, SPV_NUMBER_UNSIGNED_INT, 1},
- };
- // A valid parse of kSampleParsedOperands.
- spv_parsed_instruction_t kSampleParsedInstruction = {kSampleInstructionWords,
- uint16_t(4),
- uint16_t(SpvOpTypeInt),
- SPV_EXT_INST_TYPE_NONE,
- 0, // type id
- 44, // result id
- kSampleParsedOperands,
- 3};
- // The words for an OpAccessChain instruction.
- uint32_t kSampleAccessChainInstructionWords[] = {
- (7 << 16) | uint32_t(SpvOpAccessChain), 100, 101, 102, 103, 104, 105};
- // The operands that would be parsed from kSampleAccessChainInstructionWords.
- spv_parsed_operand_t kSampleAccessChainOperands[] = {
- {1, 1, SPV_OPERAND_TYPE_RESULT_ID, SPV_NUMBER_NONE, 0},
- {2, 1, SPV_OPERAND_TYPE_TYPE_ID, SPV_NUMBER_NONE, 0},
- {3, 1, SPV_OPERAND_TYPE_ID, SPV_NUMBER_NONE, 0},
- {4, 1, SPV_OPERAND_TYPE_ID, SPV_NUMBER_NONE, 0},
- {5, 1, SPV_OPERAND_TYPE_ID, SPV_NUMBER_NONE, 0},
- {6, 1, SPV_OPERAND_TYPE_ID, SPV_NUMBER_NONE, 0},
- };
- // A valid parse of kSampleAccessChainInstructionWords
- spv_parsed_instruction_t kSampleAccessChainInstruction = {
- kSampleAccessChainInstructionWords,
- uint16_t(7),
- uint16_t(SpvOpAccessChain),
- SPV_EXT_INST_TYPE_NONE,
- 100, // type id
- 101, // result id
- kSampleAccessChainOperands,
- 6};
- // The words for an OpControlBarrier instruction.
- uint32_t kSampleControlBarrierInstructionWords[] = {
- (4 << 16) | uint32_t(SpvOpControlBarrier), 100, 101, 102};
- // The operands that would be parsed from kSampleControlBarrierInstructionWords.
- spv_parsed_operand_t kSampleControlBarrierOperands[] = {
- {1, 1, SPV_OPERAND_TYPE_SCOPE_ID, SPV_NUMBER_NONE, 0}, // Execution
- {2, 1, SPV_OPERAND_TYPE_SCOPE_ID, SPV_NUMBER_NONE, 0}, // Memory
- {3, 1, SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID, SPV_NUMBER_NONE,
- 0}, // Semantics
- };
- // A valid parse of kSampleControlBarrierInstructionWords
- spv_parsed_instruction_t kSampleControlBarrierInstruction = {
- kSampleControlBarrierInstructionWords,
- uint16_t(4),
- uint16_t(SpvOpControlBarrier),
- SPV_EXT_INST_TYPE_NONE,
- 0, // type id
- 0, // result id
- kSampleControlBarrierOperands,
- 3};
- TEST(InstructionTest, CreateWithOpcodeAndOperands) {
- IRContext context(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction inst(&context, kSampleParsedInstruction);
- EXPECT_EQ(SpvOpTypeInt, inst.opcode());
- EXPECT_EQ(0u, inst.type_id());
- EXPECT_EQ(44u, inst.result_id());
- EXPECT_EQ(3u, inst.NumOperands());
- EXPECT_EQ(3u, inst.NumOperandWords());
- EXPECT_EQ(2u, inst.NumInOperandWords());
- }
- TEST(InstructionTest, GetOperand) {
- IRContext context(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction inst(&context, kSampleParsedInstruction);
- EXPECT_THAT(inst.GetOperand(0).words, Eq(std::vector<uint32_t>{44}));
- EXPECT_THAT(inst.GetOperand(1).words, Eq(std::vector<uint32_t>{32}));
- EXPECT_THAT(inst.GetOperand(2).words, Eq(std::vector<uint32_t>{1}));
- }
- TEST(InstructionTest, GetInOperand) {
- IRContext context(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction inst(&context, kSampleParsedInstruction);
- EXPECT_THAT(inst.GetInOperand(0).words, Eq(std::vector<uint32_t>{32}));
- EXPECT_THAT(inst.GetInOperand(1).words, Eq(std::vector<uint32_t>{1}));
- }
- TEST(InstructionTest, OperandConstIterators) {
- IRContext context(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction inst(&context, kSampleParsedInstruction);
- // Spot check iteration across operands.
- auto cbegin = inst.cbegin();
- auto cend = inst.cend();
- EXPECT_NE(cend, inst.cbegin());
- auto citer = inst.cbegin();
- for (int i = 0; i < 3; ++i, ++citer) {
- const auto& operand = *citer;
- EXPECT_THAT(operand.type, Eq(kSampleParsedOperands[i].type));
- EXPECT_THAT(operand.words,
- Eq(std::vector<uint32_t>{kSampleInstructionWords[i + 1]}));
- EXPECT_NE(cend, citer);
- }
- EXPECT_EQ(cend, citer);
- // Check that cbegin and cend have not changed.
- EXPECT_EQ(cbegin, inst.cbegin());
- EXPECT_EQ(cend, inst.cend());
- // Check arithmetic.
- const Operand& operand2 = *(inst.cbegin() + 2);
- EXPECT_EQ(SPV_OPERAND_TYPE_LITERAL_INTEGER, operand2.type);
- }
- TEST(InstructionTest, OperandIterators) {
- IRContext context(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction inst(&context, kSampleParsedInstruction);
- // Spot check iteration across operands, with mutable iterators.
- auto begin = inst.begin();
- auto end = inst.end();
- EXPECT_NE(end, inst.begin());
- auto iter = inst.begin();
- for (int i = 0; i < 3; ++i, ++iter) {
- const auto& operand = *iter;
- EXPECT_THAT(operand.type, Eq(kSampleParsedOperands[i].type));
- EXPECT_THAT(operand.words,
- Eq(std::vector<uint32_t>{kSampleInstructionWords[i + 1]}));
- EXPECT_NE(end, iter);
- }
- EXPECT_EQ(end, iter);
- // Check that begin and end have not changed.
- EXPECT_EQ(begin, inst.begin());
- EXPECT_EQ(end, inst.end());
- // Check arithmetic.
- Operand& operand2 = *(inst.begin() + 2);
- EXPECT_EQ(SPV_OPERAND_TYPE_LITERAL_INTEGER, operand2.type);
- // Check mutation through an iterator.
- operand2.type = SPV_OPERAND_TYPE_TYPE_ID;
- EXPECT_EQ(SPV_OPERAND_TYPE_TYPE_ID, (*(inst.cbegin() + 2)).type);
- }
- TEST(InstructionTest, ForInIdStandardIdTypes) {
- IRContext context(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction inst(&context, kSampleAccessChainInstruction);
- std::vector<uint32_t> ids;
- inst.ForEachInId([&ids](const uint32_t* idptr) { ids.push_back(*idptr); });
- EXPECT_THAT(ids, Eq(std::vector<uint32_t>{102, 103, 104, 105}));
- ids.clear();
- inst.ForEachInId([&ids](uint32_t* idptr) { ids.push_back(*idptr); });
- EXPECT_THAT(ids, Eq(std::vector<uint32_t>{102, 103, 104, 105}));
- }
- TEST(InstructionTest, ForInIdNonstandardIdTypes) {
- IRContext context(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction inst(&context, kSampleControlBarrierInstruction);
- std::vector<uint32_t> ids;
- inst.ForEachInId([&ids](const uint32_t* idptr) { ids.push_back(*idptr); });
- EXPECT_THAT(ids, Eq(std::vector<uint32_t>{100, 101, 102}));
- ids.clear();
- inst.ForEachInId([&ids](uint32_t* idptr) { ids.push_back(*idptr); });
- EXPECT_THAT(ids, Eq(std::vector<uint32_t>{100, 101, 102}));
- }
- TEST(InstructionTest, UniqueIds) {
- IRContext context(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction inst1(&context);
- Instruction inst2(&context);
- EXPECT_NE(inst1.unique_id(), inst2.unique_id());
- }
- TEST(InstructionTest, CloneUniqueIdDifferent) {
- IRContext context(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction inst(&context);
- std::unique_ptr<Instruction> clone(inst.Clone(&context));
- EXPECT_EQ(inst.context(), clone->context());
- EXPECT_NE(inst.unique_id(), clone->unique_id());
- }
- TEST(InstructionTest, CloneDifferentContext) {
- IRContext c1(SPV_ENV_UNIVERSAL_1_2, nullptr);
- IRContext c2(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction inst(&c1);
- std::unique_ptr<Instruction> clone(inst.Clone(&c2));
- EXPECT_EQ(&c1, inst.context());
- EXPECT_EQ(&c2, clone->context());
- EXPECT_NE(&c1, &c2);
- }
- TEST(InstructionTest, CloneDifferentContextDifferentUniqueId) {
- IRContext c1(SPV_ENV_UNIVERSAL_1_2, nullptr);
- IRContext c2(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction inst(&c1);
- Instruction other(&c2);
- std::unique_ptr<Instruction> clone(inst.Clone(&c2));
- EXPECT_EQ(&c2, clone->context());
- EXPECT_NE(other.unique_id(), clone->unique_id());
- }
- TEST(InstructionTest, EqualsEqualsOperator) {
- IRContext context(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction i1(&context);
- Instruction i2(&context);
- std::unique_ptr<Instruction> clone(i1.Clone(&context));
- EXPECT_TRUE(i1 == i1);
- EXPECT_FALSE(i1 == i2);
- EXPECT_FALSE(i1 == *clone);
- EXPECT_FALSE(i2 == *clone);
- }
- TEST(InstructionTest, LessThanOperator) {
- IRContext context(SPV_ENV_UNIVERSAL_1_2, nullptr);
- Instruction i1(&context);
- Instruction i2(&context);
- std::unique_ptr<Instruction> clone(i1.Clone(&context));
- EXPECT_TRUE(i1 < i2);
- EXPECT_TRUE(i1 < *clone);
- EXPECT_TRUE(i2 < *clone);
- }
- TEST_F(DescriptorTypeTest, StorageImage) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %3 "myStorageImage"
- OpDecorate %3 DescriptorSet 0
- OpDecorate %3 Binding 0
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeFloat 32
- %7 = OpTypeImage %6 2D 0 0 0 2 R32f
- %8 = OpTypePointer UniformConstant %7
- %3 = OpVariable %8 UniformConstant
- %2 = OpFunction %4 None %5
- %9 = OpLabel
- %10 = OpCopyObject %8 %3
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* type = context->get_def_use_mgr()->GetDef(8);
- EXPECT_TRUE(type->IsVulkanStorageImage());
- EXPECT_FALSE(type->IsVulkanSampledImage());
- EXPECT_FALSE(type->IsVulkanStorageTexelBuffer());
- EXPECT_FALSE(type->IsVulkanStorageBuffer());
- EXPECT_FALSE(type->IsVulkanUniformBuffer());
- Instruction* variable = context->get_def_use_mgr()->GetDef(3);
- EXPECT_FALSE(variable->IsReadOnlyPointer());
- Instruction* object_copy = context->get_def_use_mgr()->GetDef(10);
- EXPECT_FALSE(object_copy->IsReadOnlyPointer());
- }
- TEST_F(DescriptorTypeTest, SampledImage) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %3 "myStorageImage"
- OpDecorate %3 DescriptorSet 0
- OpDecorate %3 Binding 0
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeFloat 32
- %7 = OpTypeImage %6 2D 0 0 0 1 Unknown
- %8 = OpTypePointer UniformConstant %7
- %3 = OpVariable %8 UniformConstant
- %2 = OpFunction %4 None %5
- %9 = OpLabel
- %10 = OpCopyObject %8 %3
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* type = context->get_def_use_mgr()->GetDef(8);
- EXPECT_FALSE(type->IsVulkanStorageImage());
- EXPECT_TRUE(type->IsVulkanSampledImage());
- EXPECT_FALSE(type->IsVulkanStorageTexelBuffer());
- EXPECT_FALSE(type->IsVulkanStorageBuffer());
- EXPECT_FALSE(type->IsVulkanUniformBuffer());
- Instruction* variable = context->get_def_use_mgr()->GetDef(3);
- EXPECT_TRUE(variable->IsReadOnlyPointer());
- Instruction* object_copy = context->get_def_use_mgr()->GetDef(10);
- EXPECT_TRUE(object_copy->IsReadOnlyPointer());
- }
- TEST_F(DescriptorTypeTest, StorageTexelBuffer) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %3 "myStorageImage"
- OpDecorate %3 DescriptorSet 0
- OpDecorate %3 Binding 0
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeFloat 32
- %7 = OpTypeImage %6 Buffer 0 0 0 2 R32f
- %8 = OpTypePointer UniformConstant %7
- %3 = OpVariable %8 UniformConstant
- %2 = OpFunction %4 None %5
- %9 = OpLabel
- %10 = OpCopyObject %8 %3
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* type = context->get_def_use_mgr()->GetDef(8);
- EXPECT_FALSE(type->IsVulkanStorageImage());
- EXPECT_FALSE(type->IsVulkanSampledImage());
- EXPECT_TRUE(type->IsVulkanStorageTexelBuffer());
- EXPECT_FALSE(type->IsVulkanStorageBuffer());
- EXPECT_FALSE(type->IsVulkanUniformBuffer());
- Instruction* variable = context->get_def_use_mgr()->GetDef(3);
- EXPECT_FALSE(variable->IsReadOnlyPointer());
- Instruction* object_copy = context->get_def_use_mgr()->GetDef(10);
- EXPECT_FALSE(object_copy->IsReadOnlyPointer());
- }
- TEST_F(DescriptorTypeTest, StorageBuffer) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %3 "myStorageImage"
- OpDecorate %3 DescriptorSet 0
- OpDecorate %3 Binding 0
- OpDecorate %9 BufferBlock
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeFloat 32
- %7 = OpTypeVector %6 4
- %8 = OpTypeRuntimeArray %7
- %9 = OpTypeStruct %8
- %10 = OpTypePointer Uniform %9
- %3 = OpVariable %10 Uniform
- %2 = OpFunction %4 None %5
- %11 = OpLabel
- %12 = OpCopyObject %8 %3
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* type = context->get_def_use_mgr()->GetDef(10);
- EXPECT_FALSE(type->IsVulkanStorageImage());
- EXPECT_FALSE(type->IsVulkanSampledImage());
- EXPECT_FALSE(type->IsVulkanStorageTexelBuffer());
- EXPECT_TRUE(type->IsVulkanStorageBuffer());
- EXPECT_FALSE(type->IsVulkanUniformBuffer());
- Instruction* variable = context->get_def_use_mgr()->GetDef(3);
- EXPECT_FALSE(variable->IsReadOnlyPointer());
- Instruction* object_copy = context->get_def_use_mgr()->GetDef(12);
- EXPECT_FALSE(object_copy->IsReadOnlyPointer());
- }
- TEST_F(DescriptorTypeTest, UniformBuffer) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %3 "myStorageImage"
- OpDecorate %3 DescriptorSet 0
- OpDecorate %3 Binding 0
- OpDecorate %9 Block
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeFloat 32
- %7 = OpTypeVector %6 4
- %8 = OpTypeRuntimeArray %7
- %9 = OpTypeStruct %8
- %10 = OpTypePointer Uniform %9
- %3 = OpVariable %10 Uniform
- %2 = OpFunction %4 None %5
- %11 = OpLabel
- %12 = OpCopyObject %10 %3
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* type = context->get_def_use_mgr()->GetDef(10);
- EXPECT_FALSE(type->IsVulkanStorageImage());
- EXPECT_FALSE(type->IsVulkanSampledImage());
- EXPECT_FALSE(type->IsVulkanStorageTexelBuffer());
- EXPECT_FALSE(type->IsVulkanStorageBuffer());
- EXPECT_TRUE(type->IsVulkanUniformBuffer());
- Instruction* variable = context->get_def_use_mgr()->GetDef(3);
- EXPECT_TRUE(variable->IsReadOnlyPointer());
- Instruction* object_copy = context->get_def_use_mgr()->GetDef(12);
- EXPECT_TRUE(object_copy->IsReadOnlyPointer());
- }
- TEST_F(DescriptorTypeTest, NonWritableIsReadOnly) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %3 "myStorageImage"
- OpDecorate %3 DescriptorSet 0
- OpDecorate %3 Binding 0
- OpDecorate %9 BufferBlock
- OpDecorate %3 NonWritable
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeFloat 32
- %7 = OpTypeVector %6 4
- %8 = OpTypeRuntimeArray %7
- %9 = OpTypeStruct %8
- %10 = OpTypePointer Uniform %9
- %3 = OpVariable %10 Uniform
- %2 = OpFunction %4 None %5
- %11 = OpLabel
- %12 = OpCopyObject %8 %3
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* variable = context->get_def_use_mgr()->GetDef(3);
- EXPECT_TRUE(variable->IsReadOnlyPointer());
- // This demonstrates that the check for whether a pointer is read-only is not
- // precise: copying a NonWritable-decorated variable can yield a pointer that
- // the check does not regard as read-only.
- Instruction* object_copy = context->get_def_use_mgr()->GetDef(12);
- EXPECT_FALSE(object_copy->IsReadOnlyPointer());
- }
- TEST_F(DescriptorTypeTest, AccessChainIntoReadOnlyStructIsReadOnly) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 320
- OpMemberDecorate %3 0 Offset 0
- OpMemberDecorate %3 1 Offset 4
- OpDecorate %3 Block
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpTypeFloat 32
- %3 = OpTypeStruct %6 %8
- %9 = OpTypePointer PushConstant %3
- %10 = OpVariable %9 PushConstant
- %11 = OpConstant %6 0
- %12 = OpTypePointer PushConstant %6
- %13 = OpConstant %6 1
- %14 = OpTypePointer PushConstant %8
- %2 = OpFunction %4 None %5
- %15 = OpLabel
- %16 = OpVariable %7 Function
- %17 = OpAccessChain %12 %10 %11
- %18 = OpAccessChain %14 %10 %13
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* push_constant_struct_variable =
- context->get_def_use_mgr()->GetDef(10);
- EXPECT_TRUE(push_constant_struct_variable->IsReadOnlyPointer());
- Instruction* push_constant_struct_field_0 =
- context->get_def_use_mgr()->GetDef(17);
- EXPECT_TRUE(push_constant_struct_field_0->IsReadOnlyPointer());
- Instruction* push_constant_struct_field_1 =
- context->get_def_use_mgr()->GetDef(18);
- EXPECT_TRUE(push_constant_struct_field_1->IsReadOnlyPointer());
- }
- TEST_F(DescriptorTypeTest, ReadOnlyPointerParameter) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 320
- OpMemberDecorate %3 0 Offset 0
- OpMemberDecorate %3 1 Offset 4
- OpDecorate %3 Block
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpTypeFloat 32
- %3 = OpTypeStruct %6 %8
- %9 = OpTypePointer PushConstant %3
- %10 = OpVariable %9 PushConstant
- %11 = OpConstant %6 0
- %12 = OpTypePointer PushConstant %6
- %13 = OpConstant %6 1
- %14 = OpTypePointer PushConstant %8
- %15 = OpTypeFunction %4 %9
- %2 = OpFunction %4 None %5
- %16 = OpLabel
- %17 = OpVariable %7 Function
- %18 = OpAccessChain %12 %10 %11
- %19 = OpAccessChain %14 %10 %13
- OpReturn
- OpFunctionEnd
- %20 = OpFunction %4 None %15
- %21 = OpFunctionParameter %9
- %22 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* push_constant_struct_parameter =
- context->get_def_use_mgr()->GetDef(21);
- EXPECT_TRUE(push_constant_struct_parameter->IsReadOnlyPointer());
- }
- TEST_F(OpaqueTypeTest, BaseOpaqueTypesShader) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeFloat 32
- %6 = OpTypeImage %5 2D 1 0 0 1 Unknown
- %7 = OpTypeSampler
- %8 = OpTypeSampledImage %6
- %9 = OpTypeRuntimeArray %5
- %2 = OpFunction %3 None %4
- %10 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* image_type = context->get_def_use_mgr()->GetDef(6);
- EXPECT_TRUE(image_type->IsOpaqueType());
- Instruction* sampler_type = context->get_def_use_mgr()->GetDef(7);
- EXPECT_TRUE(sampler_type->IsOpaqueType());
- Instruction* sampled_image_type = context->get_def_use_mgr()->GetDef(8);
- EXPECT_TRUE(sampled_image_type->IsOpaqueType());
- Instruction* runtime_array_type = context->get_def_use_mgr()->GetDef(9);
- EXPECT_TRUE(runtime_array_type->IsOpaqueType());
- Instruction* float_type = context->get_def_use_mgr()->GetDef(5);
- EXPECT_FALSE(float_type->IsOpaqueType());
- Instruction* void_type = context->get_def_use_mgr()->GetDef(3);
- EXPECT_FALSE(void_type->IsOpaqueType());
- }
- TEST_F(OpaqueTypeTest, OpaqueStructTypes) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeFloat 32
- %6 = OpTypeRuntimeArray %5
- %7 = OpTypeStruct %6 %6
- %8 = OpTypeStruct %5 %6
- %9 = OpTypeStruct %6 %5
- %10 = OpTypeStruct %7
- %2 = OpFunction %3 None %4
- %11 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- for (int i = 7; i <= 10; i++) {
- Instruction* type = context->get_def_use_mgr()->GetDef(i);
- EXPECT_TRUE(type->IsOpaqueType());
- }
- }
- TEST_F(GetBaseTest, SampleImage) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %3 "myStorageImage"
- OpDecorate %3 DescriptorSet 0
- OpDecorate %3 Binding 0
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeFloat 32
- %7 = OpTypeVector %6 2
- %8 = OpTypeVector %6 4
- %9 = OpConstant %6 0
- %10 = OpConstantComposite %7 %9 %9
- %11 = OpTypeImage %6 2D 0 0 0 1 R32f
- %12 = OpTypePointer UniformConstant %11
- %3 = OpVariable %12 UniformConstant
- %13 = OpTypeSampledImage %11
- %14 = OpTypeSampler
- %15 = OpTypePointer UniformConstant %14
- %16 = OpVariable %15 UniformConstant
- %2 = OpFunction %4 None %5
- %17 = OpLabel
- %18 = OpLoad %11 %3
- %19 = OpLoad %14 %16
- %20 = OpSampledImage %13 %18 %19
- %21 = OpImageSampleImplicitLod %8 %20 %10
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* load = context->get_def_use_mgr()->GetDef(21);
- Instruction* base = context->get_def_use_mgr()->GetDef(20);
- EXPECT_TRUE(load->GetBaseAddress() == base);
- }
- TEST_F(GetBaseTest, PtrAccessChain) {
- const std::string text = R"(
- OpCapability VariablePointers
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "PSMain" %2
- OpExecutionMode %1 OriginUpperLeft
- %void = OpTypeVoid
- %4 = OpTypeFunction %void
- %float = OpTypeFloat 32
- %v4float = OpTypeVector %float 4
- %int = OpTypeInt 32 8388353
- %int_0 = OpConstant %int 0
- %_ptr_Function_v4float = OpTypePointer Function %v4float
- %2 = OpVariable %_ptr_Function_v4float Input
- %1 = OpFunction %void None %4
- %10 = OpLabel
- %11 = OpPtrAccessChain %_ptr_Function_v4float %2 %int_0
- %12 = OpLoad %v4float %11
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* load = context->get_def_use_mgr()->GetDef(12);
- Instruction* base = context->get_def_use_mgr()->GetDef(2);
- EXPECT_TRUE(load->GetBaseAddress() == base);
- }
- TEST_F(GetBaseTest, ImageRead) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %3 "myStorageImage"
- OpDecorate %3 DescriptorSet 0
- OpDecorate %3 Binding 0
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 0
- %7 = OpTypeVector %6 2
- %8 = OpConstant %6 0
- %9 = OpConstantComposite %7 %8 %8
- %10 = OpTypeImage %6 2D 0 0 0 2 R32f
- %11 = OpTypePointer UniformConstant %10
- %3 = OpVariable %11 UniformConstant
- %2 = OpFunction %4 None %5
- %12 = OpLabel
- %13 = OpLoad %10 %3
- %14 = OpImageRead %6 %13 %9
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* load = context->get_def_use_mgr()->GetDef(14);
- Instruction* base = context->get_def_use_mgr()->GetDef(13);
- EXPECT_TRUE(load->GetBaseAddress() == base);
- }
- TEST_F(ValidBasePointerTest, OpSelectBadNoVariablePointersStorageBuffer) {
- const std::string text = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer StorageBuffer %3
- %5 = OpVariable %4 StorageBuffer
- %6 = OpTypeFunction %2
- %7 = OpTypeBool
- %8 = OpConstantTrue %7
- %1 = OpFunction %2 None %6
- %9 = OpLabel
- %10 = OpSelect %4 %8 %5 %5
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* select = context->get_def_use_mgr()->GetDef(10);
- EXPECT_NE(select, nullptr);
- EXPECT_FALSE(select->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpSelectBadNoVariablePointers) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability VariablePointersStorageBuffer
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer Workgroup %3
- %5 = OpVariable %4 Workgroup
- %6 = OpTypeFunction %2
- %7 = OpTypeBool
- %8 = OpConstantTrue %7
- %1 = OpFunction %2 None %6
- %9 = OpLabel
- %10 = OpSelect %4 %8 %5 %5
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* select = context->get_def_use_mgr()->GetDef(10);
- EXPECT_NE(select, nullptr);
- EXPECT_FALSE(select->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpSelectGoodVariablePointersStorageBuffer) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability VariablePointersStorageBuffer
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer StorageBuffer %3
- %5 = OpVariable %4 StorageBuffer
- %6 = OpTypeFunction %2
- %7 = OpTypeBool
- %8 = OpConstantTrue %7
- %1 = OpFunction %2 None %6
- %9 = OpLabel
- %10 = OpSelect %4 %8 %5 %5
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* select = context->get_def_use_mgr()->GetDef(10);
- EXPECT_NE(select, nullptr);
- EXPECT_TRUE(select->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpSelectGoodVariablePointers) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability VariablePointers
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer Workgroup %3
- %5 = OpVariable %4 Workgroup
- %6 = OpTypeFunction %2
- %7 = OpTypeBool
- %8 = OpConstantTrue %7
- %1 = OpFunction %2 None %6
- %9 = OpLabel
- %10 = OpSelect %4 %8 %5 %5
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* select = context->get_def_use_mgr()->GetDef(10);
- EXPECT_NE(select, nullptr);
- EXPECT_TRUE(select->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpConstantNullBadNoVariablePointersStorageBuffer) {
- const std::string text = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer StorageBuffer %3
- %5 = OpConstantNull %4
- %6 = OpTypeFunction %2
- %1 = OpFunction %2 None %6
- %7 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* null_inst = context->get_def_use_mgr()->GetDef(5);
- EXPECT_NE(null_inst, nullptr);
- EXPECT_FALSE(null_inst->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpConstantNullBadNoVariablePointers) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability VariablePointersStorageBuffer
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer Workgroup %3
- %5 = OpConstantNull %4
- %6 = OpTypeFunction %2
- %1 = OpFunction %2 None %6
- %7 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* null_inst = context->get_def_use_mgr()->GetDef(5);
- EXPECT_NE(null_inst, nullptr);
- EXPECT_FALSE(null_inst->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpConstantNullGoodVariablePointersStorageBuffer) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability VariablePointersStorageBuffer
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer StorageBuffer %3
- %5 = OpConstantNull %4
- %6 = OpTypeFunction %2
- %1 = OpFunction %2 None %6
- %9 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* null_inst = context->get_def_use_mgr()->GetDef(5);
- EXPECT_NE(null_inst, nullptr);
- EXPECT_TRUE(null_inst->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpConstantNullGoodVariablePointers) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability VariablePointers
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer Workgroup %3
- %5 = OpConstantNull %4
- %6 = OpTypeFunction %2
- %1 = OpFunction %2 None %6
- %7 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* null_inst = context->get_def_use_mgr()->GetDef(5);
- EXPECT_NE(null_inst, nullptr);
- EXPECT_TRUE(null_inst->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpPhiBadNoVariablePointersStorageBuffer) {
- const std::string text = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer StorageBuffer %3
- %5 = OpVariable %4 StorageBuffer
- %6 = OpTypeFunction %2
- %1 = OpFunction %2 None %6
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %9 = OpPhi %4 %5 %7
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* phi = context->get_def_use_mgr()->GetDef(9);
- EXPECT_NE(phi, nullptr);
- EXPECT_FALSE(phi->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpPhiBadNoVariablePointers) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability VariablePointersStorageBuffer
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer Workgroup %3
- %5 = OpVariable %4 Workgroup
- %6 = OpTypeFunction %2
- %1 = OpFunction %2 None %6
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %9 = OpPhi %4 %5 %7
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* phi = context->get_def_use_mgr()->GetDef(9);
- EXPECT_NE(phi, nullptr);
- EXPECT_FALSE(phi->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpPhiGoodVariablePointersStorageBuffer) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability VariablePointersStorageBuffer
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer StorageBuffer %3
- %5 = OpVariable %4 StorageBuffer
- %6 = OpTypeFunction %2
- %1 = OpFunction %2 None %6
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %9 = OpPhi %4 %5 %7
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* phi = context->get_def_use_mgr()->GetDef(9);
- EXPECT_NE(phi, nullptr);
- EXPECT_TRUE(phi->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpPhiGoodVariablePointers) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability VariablePointers
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer Workgroup %3
- %5 = OpVariable %4 Workgroup
- %6 = OpTypeFunction %2
- %1 = OpFunction %2 None %6
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- %9 = OpPhi %4 %5 %7
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* phi = context->get_def_use_mgr()->GetDef(9);
- EXPECT_NE(phi, nullptr);
- EXPECT_TRUE(phi->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpFunctionCallBadNoVariablePointersStorageBuffer) {
- const std::string text = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer StorageBuffer %3
- %5 = OpConstantNull %4
- %6 = OpTypeFunction %2
- %7 = OpTypeFunction %4
- %1 = OpFunction %2 None %6
- %8 = OpLabel
- %9 = OpFunctionCall %4 %10
- OpReturn
- OpFunctionEnd
- %10 = OpFunction %4 None %7
- %11 = OpLabel
- OpReturnValue %5
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* null_inst = context->get_def_use_mgr()->GetDef(9);
- EXPECT_NE(null_inst, nullptr);
- EXPECT_FALSE(null_inst->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpFunctionCallBadNoVariablePointers) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability VariablePointersStorageBuffer
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer Workgroup %3
- %5 = OpConstantNull %4
- %6 = OpTypeFunction %2
- %7 = OpTypeFunction %4
- %1 = OpFunction %2 None %6
- %8 = OpLabel
- %9 = OpFunctionCall %4 %10
- OpReturn
- OpFunctionEnd
- %10 = OpFunction %4 None %7
- %11 = OpLabel
- OpReturnValue %5
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* null_inst = context->get_def_use_mgr()->GetDef(9);
- EXPECT_NE(null_inst, nullptr);
- EXPECT_FALSE(null_inst->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpFunctionCallGoodVariablePointersStorageBuffer) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability VariablePointersStorageBuffer
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer StorageBuffer %3
- %5 = OpConstantNull %4
- %6 = OpTypeFunction %2
- %7 = OpTypeFunction %4
- %1 = OpFunction %2 None %6
- %8 = OpLabel
- %9 = OpFunctionCall %4 %10
- OpReturn
- OpFunctionEnd
- %10 = OpFunction %4 None %7
- %11 = OpLabel
- OpReturnValue %5
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* null_inst = context->get_def_use_mgr()->GetDef(9);
- EXPECT_NE(null_inst, nullptr);
- EXPECT_TRUE(null_inst->IsValidBasePointer());
- }
- TEST_F(ValidBasePointerTest, OpFunctionCallGoodVariablePointers) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability VariablePointers
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "func"
- %2 = OpTypeVoid
- %3 = OpTypeInt 32 0
- %4 = OpTypePointer Workgroup %3
- %5 = OpConstantNull %4
- %6 = OpTypeFunction %2
- %7 = OpTypeFunction %4
- %1 = OpFunction %2 None %6
- %8 = OpLabel
- %9 = OpFunctionCall %4 %10
- OpReturn
- OpFunctionEnd
- %10 = OpFunction %4 None %7
- %11 = OpLabel
- OpReturnValue %5
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- Instruction* null_inst = context->get_def_use_mgr()->GetDef(9);
- EXPECT_NE(null_inst, nullptr);
- EXPECT_TRUE(null_inst->IsValidBasePointer());
- }
- TEST_F(VulkanBufferTest, VulkanStorageBuffer) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability RuntimeDescriptorArray
- OpMemoryModel Logical GLSL450
- OpEntryPoint GLCompute %1 "main"
- OpExecutionMode %1 LocalSize 1 1 1
- OpDecorate %2 Block
- OpMemberDecorate %2 0 Offset 0
- OpDecorate %3 BufferBlock
- OpMemberDecorate %3 0 Offset 0
- %4 = OpTypeVoid
- %5 = OpTypeInt 32 0
- %2 = OpTypeStruct %5
- %3 = OpTypeStruct %5
- %6 = OpTypePointer StorageBuffer %2
- %7 = OpTypePointer Uniform %2
- %8 = OpTypePointer Uniform %3
- %9 = OpConstant %5 1
- %10 = OpTypeArray %2 %9
- %11 = OpTypeArray %3 %9
- %12 = OpTypePointer StorageBuffer %10
- %13 = OpTypePointer Uniform %10
- %14 = OpTypePointer Uniform %11
- %15 = OpTypeRuntimeArray %2
- %16 = OpTypeRuntimeArray %3
- %17 = OpTypePointer StorageBuffer %15
- %18 = OpTypePointer Uniform %15
- %19 = OpTypePointer Uniform %16
- %50 = OpTypeFunction %4
- %1 = OpFunction %4 None %50
- %51 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- // Standard SSBO and UBO
- Instruction* inst = context->get_def_use_mgr()->GetDef(6);
- EXPECT_EQ(true, inst->IsVulkanStorageBuffer());
- inst = context->get_def_use_mgr()->GetDef(7);
- EXPECT_EQ(false, inst->IsVulkanStorageBuffer());
- inst = context->get_def_use_mgr()->GetDef(8);
- EXPECT_EQ(true, inst->IsVulkanStorageBuffer());
- // Arrayed SSBO and UBO
- inst = context->get_def_use_mgr()->GetDef(12);
- EXPECT_EQ(true, inst->IsVulkanStorageBuffer());
- inst = context->get_def_use_mgr()->GetDef(13);
- EXPECT_EQ(false, inst->IsVulkanStorageBuffer());
- inst = context->get_def_use_mgr()->GetDef(14);
- EXPECT_EQ(true, inst->IsVulkanStorageBuffer());
- // Runtime arrayed SSBO and UBO
- inst = context->get_def_use_mgr()->GetDef(17);
- EXPECT_EQ(true, inst->IsVulkanStorageBuffer());
- inst = context->get_def_use_mgr()->GetDef(18);
- EXPECT_EQ(false, inst->IsVulkanStorageBuffer());
- inst = context->get_def_use_mgr()->GetDef(19);
- EXPECT_EQ(true, inst->IsVulkanStorageBuffer());
- }
- TEST_F(VulkanBufferTest, VulkanUniformBuffer) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability RuntimeDescriptorArray
- OpMemoryModel Logical GLSL450
- OpEntryPoint GLCompute %1 "main"
- OpExecutionMode %1 LocalSize 1 1 1
- OpDecorate %2 Block
- OpMemberDecorate %2 0 Offset 0
- OpDecorate %3 BufferBlock
- OpMemberDecorate %3 0 Offset 0
- %4 = OpTypeVoid
- %5 = OpTypeInt 32 0
- %2 = OpTypeStruct %5
- %3 = OpTypeStruct %5
- %6 = OpTypePointer StorageBuffer %2
- %7 = OpTypePointer Uniform %2
- %8 = OpTypePointer Uniform %3
- %9 = OpConstant %5 1
- %10 = OpTypeArray %2 %9
- %11 = OpTypeArray %3 %9
- %12 = OpTypePointer StorageBuffer %10
- %13 = OpTypePointer Uniform %10
- %14 = OpTypePointer Uniform %11
- %15 = OpTypeRuntimeArray %2
- %16 = OpTypeRuntimeArray %3
- %17 = OpTypePointer StorageBuffer %15
- %18 = OpTypePointer Uniform %15
- %19 = OpTypePointer Uniform %16
- %50 = OpTypeFunction %4
- %1 = OpFunction %4 None %50
- %51 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- // Standard SSBO and UBO
- Instruction* inst = context->get_def_use_mgr()->GetDef(6);
- EXPECT_EQ(false, inst->IsVulkanUniformBuffer());
- inst = context->get_def_use_mgr()->GetDef(7);
- EXPECT_EQ(true, inst->IsVulkanUniformBuffer());
- inst = context->get_def_use_mgr()->GetDef(8);
- EXPECT_EQ(false, inst->IsVulkanUniformBuffer());
- // Arrayed SSBO and UBO
- inst = context->get_def_use_mgr()->GetDef(12);
- EXPECT_EQ(false, inst->IsVulkanUniformBuffer());
- inst = context->get_def_use_mgr()->GetDef(13);
- EXPECT_EQ(true, inst->IsVulkanUniformBuffer());
- inst = context->get_def_use_mgr()->GetDef(14);
- EXPECT_EQ(false, inst->IsVulkanUniformBuffer());
- // Runtime arrayed SSBO and UBO
- inst = context->get_def_use_mgr()->GetDef(17);
- EXPECT_EQ(false, inst->IsVulkanUniformBuffer());
- inst = context->get_def_use_mgr()->GetDef(18);
- EXPECT_EQ(true, inst->IsVulkanUniformBuffer());
- inst = context->get_def_use_mgr()->GetDef(19);
- EXPECT_EQ(false, inst->IsVulkanUniformBuffer());
- }
- TEST_F(VulkanBufferTest, ImageQueries) {
- const std::string text = R"(
- OpCapability Shader
- OpCapability ImageBuffer
- OpCapability RuntimeDescriptorArray
- OpMemoryModel Logical GLSL450
- OpEntryPoint GLCompute %1 "main"
- OpExecutionMode %1 LocalSize 1 1 1
- %2 = OpTypeVoid
- %3 = OpTypeFloat 32
- %4 = OpTypeImage %3 Buffer 0 0 0 1 Rgba32f
- %5 = OpTypeImage %3 Buffer 0 0 0 2 Rgba32f
- %6 = OpTypeImage %3 2D 0 0 0 1 Rgba32f
- %7 = OpTypeImage %3 2D 0 0 0 2 Rgba32f
- %8 = OpTypePointer UniformConstant %4
- %9 = OpTypePointer UniformConstant %5
- %10 = OpTypePointer UniformConstant %6
- %11 = OpTypePointer UniformConstant %7
- %12 = OpTypeInt 32 0
- %13 = OpConstant %12 1
- %14 = OpTypeArray %4 %13
- %15 = OpTypeArray %5 %13
- %16 = OpTypeArray %6 %13
- %17 = OpTypeArray %7 %13
- %18 = OpTypePointer UniformConstant %14
- %19 = OpTypePointer UniformConstant %15
- %20 = OpTypePointer UniformConstant %16
- %21 = OpTypePointer UniformConstant %17
- %22 = OpTypeRuntimeArray %4
- %23 = OpTypeRuntimeArray %5
- %24 = OpTypeRuntimeArray %6
- %25 = OpTypeRuntimeArray %7
- %26 = OpTypePointer UniformConstant %22
- %27 = OpTypePointer UniformConstant %23
- %28 = OpTypePointer UniformConstant %24
- %29 = OpTypePointer UniformConstant %25
- %50 = OpTypeFunction %4
- %1 = OpFunction %4 None %50
- %51 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text);
- EXPECT_NE(context, nullptr);
- // Bare pointers
- Instruction* inst = context->get_def_use_mgr()->GetDef(8);
- EXPECT_EQ(false, inst->IsVulkanStorageImage());
- EXPECT_EQ(false, inst->IsVulkanSampledImage());
- EXPECT_EQ(false, inst->IsVulkanStorageTexelBuffer());
- inst = context->get_def_use_mgr()->GetDef(9);
- EXPECT_EQ(false, inst->IsVulkanStorageImage());
- EXPECT_EQ(false, inst->IsVulkanSampledImage());
- EXPECT_EQ(true, inst->IsVulkanStorageTexelBuffer());
- inst = context->get_def_use_mgr()->GetDef(10);
- EXPECT_EQ(false, inst->IsVulkanStorageImage());
- EXPECT_EQ(true, inst->IsVulkanSampledImage());
- EXPECT_EQ(false, inst->IsVulkanStorageTexelBuffer());
- inst = context->get_def_use_mgr()->GetDef(11);
- EXPECT_EQ(true, inst->IsVulkanStorageImage());
- EXPECT_EQ(false, inst->IsVulkanSampledImage());
- EXPECT_EQ(false, inst->IsVulkanStorageTexelBuffer());
- // Array pointers
- inst = context->get_def_use_mgr()->GetDef(18);
- EXPECT_EQ(false, inst->IsVulkanStorageImage());
- EXPECT_EQ(false, inst->IsVulkanSampledImage());
- EXPECT_EQ(false, inst->IsVulkanStorageTexelBuffer());
- inst = context->get_def_use_mgr()->GetDef(19);
- EXPECT_EQ(false, inst->IsVulkanStorageImage());
- EXPECT_EQ(false, inst->IsVulkanSampledImage());
- EXPECT_EQ(true, inst->IsVulkanStorageTexelBuffer());
- inst = context->get_def_use_mgr()->GetDef(20);
- EXPECT_EQ(false, inst->IsVulkanStorageImage());
- EXPECT_EQ(true, inst->IsVulkanSampledImage());
- EXPECT_EQ(false, inst->IsVulkanStorageTexelBuffer());
- inst = context->get_def_use_mgr()->GetDef(21);
- EXPECT_EQ(true, inst->IsVulkanStorageImage());
- EXPECT_EQ(false, inst->IsVulkanSampledImage());
- EXPECT_EQ(false, inst->IsVulkanStorageTexelBuffer());
- // Runtime array pointers
- inst = context->get_def_use_mgr()->GetDef(26);
- EXPECT_EQ(false, inst->IsVulkanStorageImage());
- EXPECT_EQ(false, inst->IsVulkanSampledImage());
- EXPECT_EQ(false, inst->IsVulkanStorageTexelBuffer());
- inst = context->get_def_use_mgr()->GetDef(27);
- EXPECT_EQ(false, inst->IsVulkanStorageImage());
- EXPECT_EQ(false, inst->IsVulkanSampledImage());
- EXPECT_EQ(true, inst->IsVulkanStorageTexelBuffer());
- inst = context->get_def_use_mgr()->GetDef(28);
- EXPECT_EQ(false, inst->IsVulkanStorageImage());
- EXPECT_EQ(true, inst->IsVulkanSampledImage());
- EXPECT_EQ(false, inst->IsVulkanStorageTexelBuffer());
- inst = context->get_def_use_mgr()->GetDef(29);
- EXPECT_EQ(true, inst->IsVulkanStorageImage());
- EXPECT_EQ(false, inst->IsVulkanSampledImage());
- EXPECT_EQ(false, inst->IsVulkanStorageTexelBuffer());
- }
- } // namespace
- } // namespace opt
- } // namespace spvtools
|