| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450 |
- // Copyright (c) 2015-2016 The Khronos Group 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.
- // Validation tests for SSA
- #include <sstream>
- #include <string>
- #include <utility>
- #include "gmock/gmock.h"
- #include "test/unit_spirv.h"
- #include "test/val/val_fixtures.h"
- namespace spvtools {
- namespace val {
- namespace {
- using ::testing::HasSubstr;
- using ::testing::MatchesRegex;
- using ValidateSSA = spvtest::ValidateBase<std::pair<std::string, bool>>;
- TEST_F(ValidateSSA, Default) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpEntryPoint GLCompute %3 ""
- OpExecutionMode %3 LocalSize 1 1 1
- %1 = OpTypeVoid
- %2 = OpTypeFunction %1
- %3 = OpFunction %1 None %2
- %4 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, IdUndefinedBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %missing "missing"
- %voidt = OpTypeVoid
- %vfunct = OpTypeFunction %voidt
- %func = OpFunction %vfunct None %missing
- %flabel = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- TEST_F(ValidateSSA, IdRedefinedBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %2 "redefined"
- %1 = OpTypeVoid
- %2 = OpTypeFunction %1
- %2 = OpFunction %1 None %2
- %4 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- }
- TEST_F(ValidateSSA, DominateUsageBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %1 "not_dominant"
- %2 = OpTypeFunction %1 ; uses %1 before it's definition
- %1 = OpTypeVoid
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("not_dominant"));
- }
- TEST_F(ValidateSSA, DominateUsageWithinBlockBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %bad "bad"
- %voidt = OpTypeVoid
- %funct = OpTypeFunction %voidt
- %uintt = OpTypeInt 32 0
- %one = OpConstant %uintt 1
- %func = OpFunction %voidt None %funct
- %entry = OpLabel
- %sum = OpIAdd %uintt %one %bad
- %bad = OpCopyObject %uintt %sum
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- MatchesRegex("ID '.\\[%bad\\]' has not been defined\n"
- " %8 = OpIAdd %uint %uint_1 %bad\n"));
- }
- TEST_F(ValidateSSA, DominateUsageSameInstructionBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %sum "sum"
- %voidt = OpTypeVoid
- %funct = OpTypeFunction %voidt
- %uintt = OpTypeInt 32 0
- %one = OpConstant %uintt 1
- %func = OpFunction %voidt None %funct
- %entry = OpLabel
- %sum = OpIAdd %uintt %one %sum
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- MatchesRegex("ID '.\\[%sum\\]' has not been defined\n"
- " %sum = OpIAdd %uint %uint_1 %sum\n"));
- }
- TEST_F(ValidateSSA, ForwardNameGood) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %3 "main"
- %1 = OpTypeVoid
- %2 = OpTypeFunction %1
- %3 = OpFunction %1 None %2
- %4 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, ForwardNameMissingTargetBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %5 "main" ; Target never defined
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("main"));
- }
- TEST_F(ValidateSSA, ForwardMemberNameGood) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpMemberName %struct 0 "value"
- OpMemberName %struct 1 "size"
- %intt = OpTypeInt 32 1
- %uintt = OpTypeInt 32 0
- %struct = OpTypeStruct %intt %uintt
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, ForwardMemberNameMissingTargetBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpMemberName %struct 0 "value"
- OpMemberName %bad 1 "size" ; Target is not defined
- %intt = OpTypeInt 32 1
- %uintt = OpTypeInt 32 0
- %struct = OpTypeStruct %intt %uintt
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- HasSubstr("The following forward referenced IDs have not been "
- "defined:\n'2[%2]'"));
- }
- TEST_F(ValidateSSA, ForwardDecorateGood) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpDecorate %var Restrict
- %intt = OpTypeInt 32 1
- %ptrt = OpTypePointer UniformConstant %intt
- %var = OpVariable %ptrt UniformConstant
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, ForwardDecorateInvalidIDBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %missing "missing"
- OpDecorate %missing Restrict ;Missing ID
- %voidt = OpTypeVoid
- %intt = OpTypeInt 32 1
- %ptrt = OpTypePointer UniformConstant %intt
- %var = OpVariable %ptrt UniformConstant
- %2 = OpTypeFunction %voidt
- %3 = OpFunction %voidt None %2
- %4 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- TEST_F(ValidateSSA, ForwardMemberDecorateGood) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpMemberDecorate %struct 1 RowMajor
- %intt = OpTypeInt 32 1
- %f32 = OpTypeFloat 32
- %vec3 = OpTypeVector %f32 3
- %mat33 = OpTypeMatrix %vec3 3
- %struct = OpTypeStruct %intt %mat33
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, ForwardMemberDecorateInvalidIdBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %missing "missing"
- OpMemberDecorate %missing 1 RowMajor ; Target not defined
- %intt = OpTypeInt 32 1
- %f32 = OpTypeFloat 32
- %vec3 = OpTypeVector %f32 3
- %mat33 = OpTypeMatrix %vec3 3
- %struct = OpTypeStruct %intt %mat33
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- TEST_F(ValidateSSA, ForwardGroupDecorateGood) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpDecorate %dgrp RowMajor
- %dgrp = OpDecorationGroup
- OpGroupDecorate %dgrp %mat33 %mat44
- %f32 = OpTypeFloat 32
- %vec3 = OpTypeVector %f32 3
- %vec4 = OpTypeVector %f32 4
- %mat33 = OpTypeMatrix %vec3 3
- %mat44 = OpTypeMatrix %vec4 4
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, ForwardGroupDecorateMissingGroupBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %missing "missing"
- OpDecorate %dgrp RowMajor
- %dgrp = OpDecorationGroup
- OpGroupDecorate %missing %mat33 %mat44 ; Target not defined
- %intt = OpTypeInt 32 1
- %vec3 = OpTypeVector %intt 3
- %vec4 = OpTypeVector %intt 4
- %mat33 = OpTypeMatrix %vec3 3
- %mat44 = OpTypeMatrix %vec4 4
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- TEST_F(ValidateSSA, ForwardGroupDecorateMissingTargetBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %missing "missing"
- OpDecorate %dgrp RowMajor
- %dgrp = OpDecorationGroup
- OpGroupDecorate %dgrp %missing %mat44 ; Target not defined
- %f32 = OpTypeFloat 32
- %vec3 = OpTypeVector %f32 3
- %vec4 = OpTypeVector %f32 4
- %mat33 = OpTypeMatrix %vec3 3
- %mat44 = OpTypeMatrix %vec4 4
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- TEST_F(ValidateSSA, ForwardGroupDecorateDecorationGroupDominateBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %dgrp "group"
- OpDecorate %dgrp RowMajor
- OpGroupDecorate %dgrp %mat33 %mat44 ; Decoration group does not dominate usage
- %dgrp = OpDecorationGroup
- %intt = OpTypeInt 32 1
- %vec3 = OpTypeVector %intt 3
- %vec4 = OpTypeVector %intt 4
- %mat33 = OpTypeMatrix %vec3 3
- %mat44 = OpTypeMatrix %vec4 4
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("group"));
- }
- TEST_F(ValidateSSA, ForwardDecorateInvalidIdBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %missing "missing"
- OpDecorate %missing Restrict ; Missing target
- %voidt = OpTypeVoid
- %intt = OpTypeInt 32 1
- %ptrt = OpTypePointer UniformConstant %intt
- %var = OpVariable %ptrt UniformConstant
- %2 = OpTypeFunction %voidt
- %3 = OpFunction %voidt None %2
- %4 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- TEST_F(ValidateSSA, FunctionCallGood) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- %1 = OpTypeVoid
- %2 = OpTypeInt 32 1
- %3 = OpTypeInt 32 0
- %4 = OpTypeFunction %1
- %8 = OpTypeFunction %1 %2 %3
- %four = OpConstant %2 4
- %five = OpConstant %3 5
- %9 = OpFunction %1 None %8
- %10 = OpFunctionParameter %2
- %11 = OpFunctionParameter %3
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- %5 = OpFunction %1 None %4
- %6 = OpLabel
- %7 = OpFunctionCall %1 %9 %four %five
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, ForwardFunctionCallGood) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- %1 = OpTypeVoid
- %2 = OpTypeInt 32 1
- %3 = OpTypeInt 32 0
- %four = OpConstant %2 4
- %five = OpConstant %3 5
- %8 = OpTypeFunction %1 %2 %3
- %4 = OpTypeFunction %1
- %5 = OpFunction %1 None %4
- %6 = OpLabel
- %7 = OpFunctionCall %1 %9 %four %five
- OpReturn
- OpFunctionEnd
- %9 = OpFunction %1 None %8
- %10 = OpFunctionParameter %2
- %11 = OpFunctionParameter %3
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, ForwardBranchConditionalGood) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- %voidt = OpTypeVoid
- %boolt = OpTypeBool
- %vfunct = OpTypeFunction %voidt
- %true = OpConstantTrue %boolt
- %main = OpFunction %voidt None %vfunct
- %mainl = OpLabel
- OpSelectionMerge %endl None
- OpBranchConditional %true %truel %falsel
- %truel = OpLabel
- OpNop
- OpBranch %endl
- %falsel = OpLabel
- OpNop
- OpBranch %endl
- %endl = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, ForwardBranchConditionalWithWeightsGood) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- %voidt = OpTypeVoid
- %boolt = OpTypeBool
- %vfunct = OpTypeFunction %voidt
- %true = OpConstantTrue %boolt
- %main = OpFunction %voidt None %vfunct
- %mainl = OpLabel
- OpSelectionMerge %endl None
- OpBranchConditional %true %truel %falsel 1 9
- %truel = OpLabel
- OpNop
- OpBranch %endl
- %falsel = OpLabel
- OpNop
- OpBranch %endl
- %endl = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, ForwardBranchConditionalNonDominantConditionBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %tcpy "conditional"
- %voidt = OpTypeVoid
- %boolt = OpTypeBool
- %vfunct = OpTypeFunction %voidt
- %true = OpConstantTrue %boolt
- %main = OpFunction %voidt None %vfunct
- %mainl = OpLabel
- OpSelectionMerge %endl None
- OpBranchConditional %tcpy %truel %falsel ;
- %truel = OpLabel
- OpNop
- OpBranch %endl
- %falsel = OpLabel
- OpNop
- OpBranch %endl
- %endl = OpLabel
- %tcpy = OpCopyObject %boolt %true
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("conditional"));
- }
- TEST_F(ValidateSSA, ForwardBranchConditionalMissingTargetBad) {
- char str[] = R"(
- OpCapability Shader
- OpCapability Linkage
- OpMemoryModel Logical GLSL450
- OpName %missing "missing"
- %voidt = OpTypeVoid
- %boolt = OpTypeBool
- %vfunct = OpTypeFunction %voidt
- %true = OpConstantTrue %boolt
- %main = OpFunction %voidt None %vfunct
- %mainl = OpLabel
- OpSelectionMerge %endl None
- OpBranchConditional %true %missing %falsel
- %truel = OpLabel
- OpNop
- OpBranch %endl
- %falsel = OpLabel
- OpNop
- OpBranch %endl
- %endl = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- // Since Int8 requires the Kernel capability, the signedness of int types may
- // not be "1".
- const std::string kHeader = R"(
- OpCapability Int8
- OpCapability DeviceEnqueue
- OpCapability Linkage
- OpMemoryModel Logical OpenCL
- )";
- const std::string kBasicTypes = R"(
- %voidt = OpTypeVoid
- %boolt = OpTypeBool
- %int8t = OpTypeInt 8 0
- %uintt = OpTypeInt 32 0
- %vfunct = OpTypeFunction %voidt
- %intptrt = OpTypePointer UniformConstant %uintt
- %zero = OpConstant %uintt 0
- %one = OpConstant %uintt 1
- %ten = OpConstant %uintt 10
- %false = OpConstantFalse %boolt
- )";
- const std::string kKernelTypesAndConstants = R"(
- %queuet = OpTypeQueue
- %three = OpConstant %uintt 3
- %arr3t = OpTypeArray %uintt %three
- %ndt = OpTypeStruct %uintt %arr3t %arr3t %arr3t
- %eventt = OpTypeEvent
- %offset = OpConstant %uintt 0
- %local = OpConstant %uintt 1
- %gl = OpConstant %uintt 1
- %nevent = OpConstant %uintt 0
- %event = OpConstantNull %eventt
- %firstp = OpConstant %int8t 0
- %psize = OpConstant %uintt 0
- %palign = OpConstant %uintt 32
- %lsize = OpConstant %uintt 1
- %flags = OpConstant %uintt 0 ; NoWait
- %kfunct = OpTypeFunction %voidt %intptrt
- )";
- const std::string kKernelSetup = R"(
- %dqueue = OpGetDefaultQueue %queuet
- %ndval = OpBuildNDRange %ndt %gl %local %offset
- %revent = OpUndef %eventt
- )";
- const std::string kKernelDefinition = R"(
- %kfunc = OpFunction %voidt None %kfunct
- %iparam = OpFunctionParameter %intptrt
- %kfuncl = OpLabel
- OpNop
- OpReturn
- OpFunctionEnd
- )";
- TEST_F(ValidateSSA, EnqueueKernelGood) {
- std::string str = kHeader + kBasicTypes + kKernelTypesAndConstants +
- kKernelDefinition + R"(
- %main = OpFunction %voidt None %vfunct
- %mainl = OpLabel
- )" + kKernelSetup + R"(
- %err = OpEnqueueKernel %uintt %dqueue %flags %ndval %nevent
- %event %revent %kfunc %firstp %psize
- %palign %lsize
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, ForwardEnqueueKernelGood) {
- std::string str = kHeader + kBasicTypes + kKernelTypesAndConstants + R"(
- %main = OpFunction %voidt None %vfunct
- %mainl = OpLabel
- )" +
- kKernelSetup + R"(
- %err = OpEnqueueKernel %uintt %dqueue %flags %ndval %nevent
- %event %revent %kfunc %firstp %psize
- %palign %lsize
- OpReturn
- OpFunctionEnd
- )" + kKernelDefinition;
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, EnqueueMissingFunctionBad) {
- std::string str = kHeader + "OpName %kfunc \"kfunc\"" + kBasicTypes +
- kKernelTypesAndConstants + R"(
- %main = OpFunction %voidt None %vfunct
- %mainl = OpLabel
- )" + kKernelSetup + R"(
- %err = OpEnqueueKernel %uintt %dqueue %flags %ndval %nevent
- %event %revent %kfunc %firstp %psize
- %palign %lsize
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("kfunc"));
- }
- std::string forwardKernelNonDominantParameterBaseCode(
- std::string name = std::string()) {
- std::string op_name;
- if (name.empty()) {
- op_name = "";
- } else {
- op_name = "\nOpName %" + name + " \"" + name + "\"\n";
- }
- std::string out = kHeader + op_name + kBasicTypes + kKernelTypesAndConstants +
- kKernelDefinition +
- R"(
- %main = OpFunction %voidt None %vfunct
- %mainl = OpLabel
- )" + kKernelSetup;
- return out;
- }
- TEST_F(ValidateSSA, ForwardEnqueueKernelMissingParameter1Bad) {
- std::string str = forwardKernelNonDominantParameterBaseCode("missing") + R"(
- %err = OpEnqueueKernel %missing %dqueue %flags %ndval
- %nevent %event %revent %kfunc %firstp
- %psize %palign %lsize
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- TEST_F(ValidateSSA, ForwardEnqueueKernelNonDominantParameter2Bad) {
- std::string str = forwardKernelNonDominantParameterBaseCode("dqueue2") + R"(
- %err = OpEnqueueKernel %uintt %dqueue2 %flags %ndval
- %nevent %event %revent %kfunc
- %firstp %psize %palign %lsize
- %dqueue2 = OpGetDefaultQueue %queuet
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("dqueue2"));
- }
- TEST_F(ValidateSSA, ForwardEnqueueKernelNonDominantParameter3Bad) {
- std::string str = forwardKernelNonDominantParameterBaseCode("ndval2") + R"(
- %err = OpEnqueueKernel %uintt %dqueue %flags %ndval2
- %nevent %event %revent %kfunc %firstp
- %psize %palign %lsize
- %ndval2 = OpBuildNDRange %ndt %gl %local %offset
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("ndval2"));
- }
- TEST_F(ValidateSSA, ForwardEnqueueKernelNonDominantParameter4Bad) {
- std::string str = forwardKernelNonDominantParameterBaseCode("nevent2") + R"(
- %err = OpEnqueueKernel %uintt %dqueue %flags %ndval %nevent2
- %event %revent %kfunc %firstp %psize
- %palign %lsize
- %nevent2 = OpCopyObject %uintt %nevent
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("nevent2"));
- }
- TEST_F(ValidateSSA, ForwardEnqueueKernelNonDominantParameter5Bad) {
- std::string str = forwardKernelNonDominantParameterBaseCode("event2") + R"(
- %err = OpEnqueueKernel %uintt %dqueue %flags %ndval %nevent
- %event2 %revent %kfunc %firstp %psize
- %palign %lsize
- %event2 = OpCopyObject %eventt %event
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("event2"));
- }
- TEST_F(ValidateSSA, ForwardEnqueueKernelNonDominantParameter6Bad) {
- std::string str = forwardKernelNonDominantParameterBaseCode("revent2") + R"(
- %err = OpEnqueueKernel %uintt %dqueue %flags %ndval %nevent
- %event %revent2 %kfunc %firstp %psize
- %palign %lsize
- %revent2 = OpCopyObject %eventt %revent
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("revent2"));
- }
- TEST_F(ValidateSSA, ForwardEnqueueKernelNonDominantParameter8Bad) {
- std::string str = forwardKernelNonDominantParameterBaseCode("firstp2") + R"(
- %err = OpEnqueueKernel %uintt %dqueue %flags %ndval %nevent
- %event %revent %kfunc %firstp2 %psize
- %palign %lsize
- %firstp2 = OpCopyObject %int8t %firstp
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("firstp2"));
- }
- TEST_F(ValidateSSA, ForwardEnqueueKernelNonDominantParameter9Bad) {
- std::string str = forwardKernelNonDominantParameterBaseCode("psize2") + R"(
- %err = OpEnqueueKernel %uintt %dqueue %flags %ndval %nevent
- %event %revent %kfunc %firstp %psize2
- %palign %lsize
- %psize2 = OpCopyObject %uintt %psize
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("psize2"));
- }
- TEST_F(ValidateSSA, ForwardEnqueueKernelNonDominantParameter10Bad) {
- std::string str = forwardKernelNonDominantParameterBaseCode("palign2") + R"(
- %err = OpEnqueueKernel %uintt %dqueue %flags %ndval %nevent
- %event %revent %kfunc %firstp %psize
- %palign2 %lsize
- %palign2 = OpCopyObject %uintt %palign
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("palign2"));
- }
- TEST_F(ValidateSSA, ForwardEnqueueKernelNonDominantParameter11Bad) {
- std::string str = forwardKernelNonDominantParameterBaseCode("lsize2") + R"(
- %err = OpEnqueueKernel %uintt %dqueue %flags %ndval %nevent
- %event %revent %kfunc %firstp %psize
- %palign %lsize2
- %lsize2 = OpCopyObject %uintt %lsize
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("lsize2"));
- }
- static const bool kWithNDrange = true;
- static const bool kNoNDrange = false;
- std::pair<std::string, bool> cases[] = {
- {"OpGetKernelNDrangeSubGroupCount", kWithNDrange},
- {"OpGetKernelNDrangeMaxSubGroupSize", kWithNDrange},
- {"OpGetKernelWorkGroupSize", kNoNDrange},
- {"OpGetKernelPreferredWorkGroupSizeMultiple", kNoNDrange}};
- INSTANTIATE_TEST_SUITE_P(KernelArgs, ValidateSSA, ::testing::ValuesIn(cases));
- static const std::string return_instructions = R"(
- OpReturn
- OpFunctionEnd
- )";
- TEST_P(ValidateSSA, GetKernelGood) {
- std::string instruction = GetParam().first;
- bool with_ndrange = GetParam().second;
- std::string ndrange_param = with_ndrange ? " %ndval " : " ";
- std::stringstream ss;
- // clang-format off
- ss << forwardKernelNonDominantParameterBaseCode() + " %numsg = "
- << instruction + " %uintt" + ndrange_param + "%kfunc %firstp %psize %palign"
- << return_instructions;
- // clang-format on
- CompileSuccessfully(ss.str());
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_P(ValidateSSA, ForwardGetKernelGood) {
- std::string instruction = GetParam().first;
- bool with_ndrange = GetParam().second;
- std::string ndrange_param = with_ndrange ? " %ndval " : " ";
- // clang-format off
- std::string str = kHeader + kBasicTypes + kKernelTypesAndConstants +
- R"(
- %main = OpFunction %voidt None %vfunct
- %mainl = OpLabel
- )"
- + kKernelSetup + " %numsg = "
- + instruction + " %uintt" + ndrange_param + "%kfunc %firstp %psize %palign"
- + return_instructions + kKernelDefinition;
- // clang-format on
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_P(ValidateSSA, ForwardGetKernelMissingDefinitionBad) {
- std::string instruction = GetParam().first;
- bool with_ndrange = GetParam().second;
- std::string ndrange_param = with_ndrange ? " %ndval " : " ";
- std::stringstream ss;
- // clang-format off
- ss << forwardKernelNonDominantParameterBaseCode("missing") + " %numsg = "
- << instruction + " %uintt" + ndrange_param + "%missing %firstp %psize %palign"
- << return_instructions;
- // clang-format on
- CompileSuccessfully(ss.str());
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- TEST_P(ValidateSSA, ForwardGetKernelNDrangeSubGroupCountMissingParameter1Bad) {
- std::string instruction = GetParam().first;
- bool with_ndrange = GetParam().second;
- std::string ndrange_param = with_ndrange ? " %ndval " : " ";
- std::stringstream ss;
- // clang-format off
- ss << forwardKernelNonDominantParameterBaseCode("missing") + " %numsg = "
- << instruction + " %missing" + ndrange_param + "%kfunc %firstp %psize %palign"
- << return_instructions;
- // clang-format on
- CompileSuccessfully(ss.str());
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- TEST_P(ValidateSSA,
- ForwardGetKernelNDrangeSubGroupCountNonDominantParameter2Bad) {
- std::string instruction = GetParam().first;
- bool with_ndrange = GetParam().second;
- std::string ndrange_param = with_ndrange ? " %ndval2 " : " ";
- std::stringstream ss;
- // clang-format off
- ss << forwardKernelNonDominantParameterBaseCode("ndval2") + " %numsg = "
- << instruction + " %uintt" + ndrange_param + "%kfunc %firstp %psize %palign"
- << "\n %ndval2 = OpBuildNDRange %ndt %gl %local %offset"
- << return_instructions;
- // clang-format on
- if (GetParam().second) {
- CompileSuccessfully(ss.str());
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("ndval2"));
- }
- }
- TEST_P(ValidateSSA,
- ForwardGetKernelNDrangeSubGroupCountNonDominantParameter4Bad) {
- std::string instruction = GetParam().first;
- bool with_ndrange = GetParam().second;
- std::string ndrange_param = with_ndrange ? " %ndval " : " ";
- std::stringstream ss;
- // clang-format off
- ss << forwardKernelNonDominantParameterBaseCode("firstp2") + " %numsg = "
- << instruction + " %uintt" + ndrange_param + "%kfunc %firstp2 %psize %palign"
- << "\n %firstp2 = OpCopyObject %int8t %firstp"
- << return_instructions;
- // clang-format on
- CompileSuccessfully(ss.str());
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("firstp2"));
- }
- TEST_P(ValidateSSA,
- ForwardGetKernelNDrangeSubGroupCountNonDominantParameter5Bad) {
- std::string instruction = GetParam().first;
- bool with_ndrange = GetParam().second;
- std::string ndrange_param = with_ndrange ? " %ndval " : " ";
- std::stringstream ss;
- // clang-format off
- ss << forwardKernelNonDominantParameterBaseCode("psize2") + " %numsg = "
- << instruction + " %uintt" + ndrange_param + "%kfunc %firstp %psize2 %palign"
- << "\n %psize2 = OpCopyObject %uintt %psize"
- << return_instructions;
- // clang-format on
- CompileSuccessfully(ss.str());
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("psize2"));
- }
- TEST_P(ValidateSSA,
- ForwardGetKernelNDrangeSubGroupCountNonDominantParameter6Bad) {
- std::string instruction = GetParam().first;
- bool with_ndrange = GetParam().second;
- std::string ndrange_param = with_ndrange ? " %ndval " : " ";
- std::stringstream ss;
- // clang-format off
- ss << forwardKernelNonDominantParameterBaseCode("palign2") + " %numsg = "
- << instruction + " %uintt" + ndrange_param + "%kfunc %firstp %psize %palign2"
- << "\n %palign2 = OpCopyObject %uintt %palign"
- << return_instructions;
- // clang-format on
- if (GetParam().second) {
- CompileSuccessfully(ss.str());
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("palign2"));
- }
- }
- TEST_F(ValidateSSA, PhiGood) {
- std::string str = kHeader + kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %preheader = OpLabel
- %init = OpCopyObject %uintt %zero
- OpBranch %loop
- %loop = OpLabel
- %i = OpPhi %uintt %init %preheader %loopi %loop
- %loopi = OpIAdd %uintt %i %one
- OpNop
- %cond = OpSLessThan %boolt %i %ten
- OpLoopMerge %endl %loop None
- OpBranchConditional %cond %loop %endl
- %endl = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, PhiMissingTypeBad) {
- std::string str = kHeader + "OpName %missing \"missing\"" + kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %preheader = OpLabel
- %init = OpCopyObject %uintt %zero
- OpBranch %loop
- %loop = OpLabel
- %i = OpPhi %missing %init %preheader %loopi %loop
- %loopi = OpIAdd %uintt %i %one
- OpNop
- %cond = OpSLessThan %boolt %i %ten
- OpLoopMerge %endl %loop None
- OpBranchConditional %cond %loop %endl
- %endl = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- TEST_F(ValidateSSA, PhiMissingIdBad) {
- std::string str = kHeader + "OpName %missing \"missing\"" + kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %preheader = OpLabel
- %init = OpCopyObject %uintt %zero
- OpBranch %loop
- %loop = OpLabel
- %i = OpPhi %uintt %missing %preheader %loopi %loop
- %loopi = OpIAdd %uintt %i %one
- OpNop
- %cond = OpSLessThan %boolt %i %ten
- OpLoopMerge %endl %loop None
- OpBranchConditional %cond %loop %endl
- %endl = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- TEST_F(ValidateSSA, PhiMissingLabelBad) {
- std::string str = kHeader + "OpName %missing \"missing\"" + kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %preheader = OpLabel
- %init = OpCopyObject %uintt %zero
- OpBranch %loop
- %loop = OpLabel
- %i = OpPhi %uintt %init %missing %loopi %loop
- %loopi = OpIAdd %uintt %i %one
- OpNop
- %cond = OpSLessThan %boolt %i %ten
- OpLoopMerge %endl %loop None
- OpBranchConditional %cond %loop %endl
- %endl = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(), HasSubstr("missing"));
- }
- TEST_F(ValidateSSA, IdDominatesItsUseGood) {
- std::string str = kHeader + kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %entry = OpLabel
- %cond = OpSLessThan %boolt %one %ten
- %eleven = OpIAdd %uintt %one %ten
- OpSelectionMerge %merge None
- OpBranchConditional %cond %t %f
- %t = OpLabel
- %twelve = OpIAdd %uintt %eleven %one
- OpBranch %merge
- %f = OpLabel
- %twentytwo = OpIAdd %uintt %eleven %ten
- OpBranch %merge
- %merge = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, IdDoesNotDominateItsUseBad) {
- std::string str = kHeader +
- "OpName %eleven \"eleven\"\n"
- "OpName %true_block \"true_block\"\n"
- "OpName %false_block \"false_block\"" +
- kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %entry = OpLabel
- %cond = OpSLessThan %boolt %one %ten
- OpSelectionMerge %merge None
- OpBranchConditional %cond %true_block %false_block
- %true_block = OpLabel
- %eleven = OpIAdd %uintt %one %ten
- %twelve = OpIAdd %uintt %eleven %one
- OpBranch %merge
- %false_block = OpLabel
- %twentytwo = OpIAdd %uintt %eleven %ten
- OpBranch %merge
- %merge = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- MatchesRegex("ID '.\\[%eleven\\]' defined in block '.\\[%true_block\\]' "
- "does not dominate its use in block '.\\[%false_block\\]'\n"
- " %false_block = OpLabel\n"));
- }
- TEST_F(ValidateSSA, PhiUseDoesntDominateDefinitionGood) {
- std::string str = kHeader + kBasicTypes +
- R"(
- %funcintptrt = OpTypePointer Function %uintt
- %func = OpFunction %voidt None %vfunct
- %entry = OpLabel
- %var_one = OpVariable %funcintptrt Function %one
- %one_val = OpLoad %uintt %var_one
- OpBranch %loop
- %loop = OpLabel
- %i = OpPhi %uintt %one_val %entry %inew %cont
- %cond = OpSLessThan %boolt %one %ten
- OpLoopMerge %merge %cont None
- OpBranchConditional %cond %body %merge
- %body = OpLabel
- OpBranch %cont
- %cont = OpLabel
- %inew = OpIAdd %uintt %i %one
- OpBranch %loop
- %merge = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA,
- PhiUseDoesntDominateUseOfPhiOperandUsedBeforeDefinitionBad) {
- std::string str = kHeader + "OpName %inew \"inew\"" + kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %entry = OpLabel
- %var_one = OpVariable %intptrt Function %one
- %one_val = OpLoad %uintt %var_one
- OpBranch %loop
- %loop = OpLabel
- %i = OpPhi %uintt %one_val %entry %inew %cont
- %bad = OpIAdd %uintt %inew %one
- %cond = OpSLessThan %boolt %one %ten
- OpLoopMerge %merge %cont None
- OpBranchConditional %cond %body %merge
- %body = OpLabel
- OpBranch %cont
- %cont = OpLabel
- %inew = OpIAdd %uintt %i %one
- OpBranch %loop
- %merge = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- MatchesRegex("ID '.\\[%inew\\]' has not been defined\n"
- " %19 = OpIAdd %uint %inew %uint_1\n"));
- }
- TEST_F(ValidateSSA, PhiUseMayComeFromNonDominatingBlockGood) {
- std::string str = kHeader + "OpName %if_true \"if_true\"\n" +
- "OpName %exit \"exit\"\n" + "OpName %copy \"copy\"\n" +
- kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %entry = OpLabel
- OpBranchConditional %false %if_true %exit
- %if_true = OpLabel
- %copy = OpCopyObject %boolt %false
- OpBranch %exit
- ; The use of %copy here is ok, even though it was defined
- ; in a block that does not dominate %exit. That's the point
- ; of an OpPhi.
- %exit = OpLabel
- %value = OpPhi %boolt %false %entry %copy %if_true
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString();
- }
- TEST_F(ValidateSSA, PhiUsesItsOwnDefinitionGood) {
- // See https://github.com/KhronosGroup/SPIRV-Tools/issues/415
- //
- // Non-phi instructions can't use their own definitions, as
- // already checked in test DominateUsageSameInstructionBad.
- std::string str = kHeader + "OpName %loop \"loop\"\n" +
- "OpName %value \"value\"\n" + kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %entry = OpLabel
- OpBranch %loop
- %loop = OpLabel
- %value = OpPhi %boolt %false %entry %value %loop
- OpBranch %loop
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString();
- }
- TEST_F(ValidateSSA, PhiVariableDefNotDominatedByParentBlockBad) {
- std::string str = kHeader + "OpName %if_true \"if_true\"\n" +
- "OpName %if_false \"if_false\"\n" +
- "OpName %exit \"exit\"\n" + "OpName %value \"phi\"\n" +
- "OpName %true_copy \"true_copy\"\n" +
- "OpName %false_copy \"false_copy\"\n" + kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %entry = OpLabel
- OpBranchConditional %false %if_true %if_false
- %if_true = OpLabel
- %true_copy = OpCopyObject %boolt %false
- OpBranch %exit
- %if_false = OpLabel
- %false_copy = OpCopyObject %boolt %false
- OpBranch %exit
- ; The (variable,Id) pairs are swapped.
- %exit = OpLabel
- %value = OpPhi %boolt %true_copy %if_false %false_copy %if_true
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- MatchesRegex(
- "In OpPhi instruction '.\\[%phi\\]', ID '.\\[%true_copy\\]' "
- "definition does not dominate its parent '.\\[%if_false\\]'\n"
- " %phi = OpPhi %bool %true_copy %if_false %false_copy "
- "%if_true\n"));
- }
- TEST_F(ValidateSSA, PhiVariableDefDominatesButNotDefinedInParentBlock) {
- std::string str = kHeader + "OpName %if_true \"if_true\"\n" + kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %entry = OpLabel
- OpBranchConditional %false %if_true %if_false
- %if_true = OpLabel
- %true_copy = OpCopyObject %boolt %false
- OpBranch %if_tnext
- %if_tnext = OpLabel
- OpBranch %exit
- %if_false = OpLabel
- %false_copy = OpCopyObject %boolt %false
- OpBranch %if_fnext
- %if_fnext = OpLabel
- OpBranch %exit
- %exit = OpLabel
- %value = OpPhi %boolt %true_copy %if_tnext %false_copy %if_fnext
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA,
- DominanceCheckIgnoresUsesInUnreachableBlocksDefInBlockGood) {
- std::string str = kHeader + kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %entry = OpLabel
- %def = OpCopyObject %boolt %false
- OpReturn
- %unreach = OpLabel
- %use = OpCopyObject %boolt %def
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- EXPECT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString();
- }
- TEST_F(ValidateSSA, PhiVariableUnreachableDefNotInParentBlock) {
- std::string str = kHeader + "OpName %unreachable \"unreachable\"\n" +
- kBasicTypes +
- R"(
- %func = OpFunction %voidt None %vfunct
- %entry = OpLabel
- OpBranch %if_false
- %unreachable = OpLabel
- %copy = OpCopyObject %boolt %false
- OpBranch %if_tnext
- %if_tnext = OpLabel
- OpBranch %exit
- %if_false = OpLabel
- %false_copy = OpCopyObject %boolt %false
- OpBranch %if_fnext
- %if_fnext = OpLabel
- OpBranch %exit
- %exit = OpLabel
- %value = OpPhi %boolt %copy %if_tnext %false_copy %if_fnext
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA,
- DominanceCheckIgnoresUsesInUnreachableBlocksDefIsParamGood) {
- std::string str = kHeader + kBasicTypes +
- R"(
- %void_fn_int = OpTypeFunction %voidt %uintt
- %func = OpFunction %voidt None %void_fn_int
- %int_param = OpFunctionParameter %uintt
- %entry = OpLabel
- OpReturn
- %unreach = OpLabel
- %use = OpCopyObject %uintt %int_param
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- EXPECT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString();
- }
- TEST_F(ValidateSSA, UseFunctionParameterFromOtherFunctionBad) {
- std::string str = kHeader +
- "OpName %first \"first\"\n"
- "OpName %func \"func\"\n" +
- "OpName %func2 \"func2\"\n" + kBasicTypes +
- R"(
- %viifunct = OpTypeFunction %voidt %uintt %uintt
- %func = OpFunction %voidt None %viifunct
- %first = OpFunctionParameter %uintt
- %second = OpFunctionParameter %uintt
- OpFunctionEnd
- %func2 = OpFunction %voidt None %viifunct
- %first2 = OpFunctionParameter %uintt
- %second2 = OpFunctionParameter %uintt
- %entry2 = OpLabel
- %baduse = OpIAdd %uintt %first %first2
- OpReturn
- OpFunctionEnd
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- MatchesRegex(
- "ID '.\\[%first\\]' used in function '.\\[%func2\\]' is used "
- "outside of it's defining function '.\\[%func\\]'\n"
- " %func = OpFunction %void None %14\n"));
- }
- TEST_F(ValidateSSA, TypeForwardPointerForwardReference) {
- // See https://github.com/KhronosGroup/SPIRV-Tools/issues/429
- //
- // ForwardPointers can references instructions that have not been defined
- std::string str = R"(
- OpCapability Kernel
- OpCapability Addresses
- OpCapability Linkage
- OpMemoryModel Logical OpenCL
- OpName %intptrt "intptrt"
- OpTypeForwardPointer %intptrt UniformConstant
- %uint = OpTypeInt 32 0
- %struct = OpTypeStruct %uint
- %intptrt = OpTypePointer UniformConstant %struct
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidateSSA, TypeStructForwardReference) {
- std::string str = R"(
- OpCapability Kernel
- OpCapability Addresses
- OpCapability Linkage
- OpMemoryModel Logical OpenCL
- OpName %structptr "structptr"
- OpTypeForwardPointer %structptr UniformConstant
- %uint = OpTypeInt 32 0
- %structt1 = OpTypeStruct %structptr %uint
- %structt2 = OpTypeStruct %uint %structptr
- %structt3 = OpTypeStruct %uint %uint %structptr
- %structt4 = OpTypeStruct %uint %uint %uint %structptr
- %structptr = OpTypePointer UniformConstant %structt1
- )";
- CompileSuccessfully(str);
- ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- // TODO(umar): OpGroupMemberDecorate
- } // namespace
- } // namespace val
- } // namespace spvtools
|