| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570 |
- // 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 "source/opt/instruction.h"
- #include <memory>
- #include <utility>
- #include <vector>
- #include "gmock/gmock.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(spv::Op::OpNop, 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, spv::Op::OpReturn);
- EXPECT_EQ(spv::Op::OpReturn, 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, 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(spv::Op::OpTypeInt),
- 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(spv::Op::OpTypeInt),
- 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(spv::Op::OpAccessChain), 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(spv::Op::OpAccessChain),
- 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(spv::Op::OpControlBarrier), 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(spv::Op::OpControlBarrier),
- 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(spv::Op::OpTypeInt, 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());
- }
- TEST_F(DescriptorTypeTest, GetShader100DebugOpcode) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
- %2 = OpString "ps.hlsl"
- %3 = OpString "#line 1 \"ps.hlsl\""
- %void = OpTypeVoid
- %5 = OpExtInst %void %1 DebugExpression
- %6 = OpExtInst %void %1 DebugSource %2 %3
- )";
- SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
- Instruction* debug_expression = context->get_def_use_mgr()->GetDef(5);
- EXPECT_EQ(debug_expression->GetShader100DebugOpcode(),
- NonSemanticShaderDebugInfo100DebugExpression);
- Instruction* debug_source = context->get_def_use_mgr()->GetDef(6);
- EXPECT_EQ(debug_source->GetShader100DebugOpcode(),
- NonSemanticShaderDebugInfo100DebugSource);
- // Test that an opcode larger than the max will return Max. This instruction
- // cannot be in the assembly above because the assembler expects the string
- // for the opcode, so we cannot use an arbitrary number. However, a binary
- // file could have an arbitrary number.
- std::unique_ptr<Instruction> past_max(debug_expression->Clone(context.get()));
- const uint32_t kExtInstOpcodeInIndex = 1;
- uint32_t large_opcode = NonSemanticShaderDebugInfo100InstructionsMax + 2;
- past_max->SetInOperand(kExtInstOpcodeInIndex, {large_opcode});
- EXPECT_EQ(past_max->GetShader100DebugOpcode(),
- NonSemanticShaderDebugInfo100InstructionsMax);
- // Test that an opcode without a value in the enum, but less than Max returns
- // the same value.
- uint32_t opcode = NonSemanticShaderDebugInfo100InstructionsMax - 2;
- past_max->SetInOperand(kExtInstOpcodeInIndex, {opcode});
- EXPECT_EQ(past_max->GetShader100DebugOpcode(), opcode);
- }
- } // namespace
- } // namespace opt
- } // namespace spvtools
|