| 1 |
// Copyright 2025 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include <array> |
| 16 |
#include <iostream> |
| 17 |
|
| 18 |
#include "gmock/gmock.h" |
| 19 |
#include "source/spirv_target_env.h" |
| 20 |
#include "source/table2.h" |
| 21 |
#include "test/unit_spirv.h" |
| 22 |
|
| 23 |
using ::testing::ContainerEq; |
| 24 |
using ::testing::ValuesIn; |
| 25 |
|
| 26 |
namespace spvtools { |
| 27 |
namespace { |
| 28 |
|
| 29 |
struct OpcodeLookupCase { |
| 30 |
std::string name; |
| 31 |
uint32_t opcode; |
| 32 |
bool expect_pass = true; |
| 33 |
}; |
| 34 |
|
| 35 |
std::ostream& operator<<(std::ostream& os, const OpcodeLookupCase& olc) { |
| 36 |
os << "OLC('" << olc.name << "', " << olc.opcode << ", expect pass? " |
| 37 |
<< olc.expect_pass << ")"; |
| 38 |
return os; |
| 39 |
} |
| 40 |
|
| 41 |
using OpcodeLookupTest = ::testing::TestWithParam<OpcodeLookupCase>; |
| 42 |
|
| 43 |
TEST_P(OpcodeLookupTest, OpcodeLookup_ByName) { |
| 44 |
const InstructionDesc* desc = nullptr; |
| 45 |
auto status = LookupOpcode(GetParam().name.data(), &desc); |
| 46 |
if (GetParam().expect_pass) { |
| 47 |
EXPECT_EQ(status, SPV_SUCCESS); |
| 48 |
ASSERT_NE(desc, nullptr); |
| 49 |
EXPECT_EQ(static_cast<uint32_t>(desc->opcode), GetParam().opcode); |
| 50 |
} else { |
| 51 |
EXPECT_NE(status, SPV_SUCCESS); |
| 52 |
EXPECT_EQ(desc, nullptr); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
TEST_P(OpcodeLookupTest, OpcodeLookup_ByOpcode_Success) { |
| 57 |
const InstructionDesc* desc = nullptr; |
| 58 |
if (GetParam().expect_pass) { |
| 59 |
spv::Op opcode = static_cast<spv::Op>(GetParam().opcode); |
| 60 |
auto status = LookupOpcode(opcode, &desc); |
| 61 |
EXPECT_EQ(status, SPV_SUCCESS); |
| 62 |
ASSERT_NE(desc, nullptr); |
| 63 |
EXPECT_EQ(desc->opcode, opcode); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
INSTANTIATE_TEST_SUITE_P(Samples, OpcodeLookupTest, |
| 68 |
ValuesIn(std::vector<OpcodeLookupCase>{ |
| 69 |
{"Nop", 0}, |
| 70 |
{"WritePipe", 275}, |
| 71 |
{"TypeAccelerationStructureKHR", 5341}, |
| 72 |
{"TypeAccelerationStructureNV", 5341}, |
| 73 |
{"does not exist", 0, false}, |
| 74 |
{"CopyLogical", 400}, |
| 75 |
{"FPGARegINTEL", 5949}, |
| 76 |
{"SubgroupMatrixMultiplyAccumulateINTEL", 6237}, |
| 77 |
})); |
| 78 |
|
| 79 |
TEST(OpcodeLookupSingleTest, OpcodeLookup_ByOpcode_Fails) { |
| 80 |
// This list may need adjusting over time. |
| 81 |
std::array<uint32_t, 3> bad_opcodes = {{99999, 37737, 110101}}; |
| 82 |
for (auto bad_opcode : bad_opcodes) { |
| 83 |
const InstructionDesc* desc = nullptr; |
| 84 |
spv::Op opcode = static_cast<spv::Op>(bad_opcode); |
| 85 |
auto status = LookupOpcode(opcode, &desc); |
| 86 |
EXPECT_NE(status, SPV_SUCCESS); |
| 87 |
ASSERT_EQ(desc, nullptr); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
struct OpcodeLookupEnvCase { |
| 92 |
std::string name; |
| 93 |
uint32_t opcode; |
| 94 |
spv_target_env env = SPV_ENV_UNIVERSAL_1_0; |
| 95 |
bool expect_pass = true; |
| 96 |
}; |
| 97 |
|
| 98 |
std::ostream& operator<<(std::ostream& os, const OpcodeLookupEnvCase& olec) { |
| 99 |
os << "OLC('" << olec.name << "', " << olec.opcode << ", env " |
| 100 |
<< spvTargetEnvDescription(olec.env) << ", expect pass? " |
| 101 |
<< olec.expect_pass << ")"; |
| 102 |
return os; |
| 103 |
} |
| 104 |
|
| 105 |
using OpcodeLookupEnvTest = ::testing::TestWithParam<OpcodeLookupEnvCase>; |
| 106 |
|
| 107 |
TEST_P(OpcodeLookupEnvTest, OpcodeLookupForEnv_ByName) { |
| 108 |
const InstructionDesc* desc = nullptr; |
| 109 |
auto status = |
| 110 |
LookupOpcodeForEnv(GetParam().env, GetParam().name.data(), &desc); |
| 111 |
if (GetParam().expect_pass) { |
| 112 |
EXPECT_EQ(status, SPV_SUCCESS); |
| 113 |
ASSERT_NE(desc, nullptr); |
| 114 |
EXPECT_EQ(static_cast<uint32_t>(desc->opcode), GetParam().opcode); |
| 115 |
} else { |
| 116 |
EXPECT_NE(status, SPV_SUCCESS); |
| 117 |
EXPECT_EQ(desc, nullptr); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
TEST_P(OpcodeLookupEnvTest, OpcodeLookupForEnv_ByOpcode) { |
| 122 |
const InstructionDesc* desc = nullptr; |
| 123 |
spv::Op opcode = static_cast<spv::Op>(GetParam().opcode); |
| 124 |
auto status = LookupOpcodeForEnv(GetParam().env, opcode, &desc); |
| 125 |
if (GetParam().expect_pass) { |
| 126 |
EXPECT_EQ(status, SPV_SUCCESS); |
| 127 |
ASSERT_NE(desc, nullptr); |
| 128 |
EXPECT_EQ(desc->opcode, opcode); |
| 129 |
} else { |
| 130 |
// Skip nonsense cases created for the lookup-by-name case. |
| 131 |
if (GetParam().name != "does not exist") { |
| 132 |
EXPECT_NE(status, SPV_SUCCESS); |
| 133 |
EXPECT_EQ(desc, nullptr); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
INSTANTIATE_TEST_SUITE_P(Samples, OpcodeLookupEnvTest, |
| 139 |
ValuesIn(std::vector<OpcodeLookupEnvCase>{ |
| 140 |
{"Nop", 0}, |
| 141 |
{"WritePipe", 275}, |
| 142 |
{"TypeAccelerationStructureKHR", 5341}, |
| 143 |
{"TypeAccelerationStructureNV", 5341}, |
| 144 |
{"does not exist", 0, SPV_ENV_UNIVERSAL_1_0, |
| 145 |
false}, |
| 146 |
{"CopyLogical", 400, SPV_ENV_UNIVERSAL_1_0, false}, |
| 147 |
{"CopyLogical", 400, SPV_ENV_UNIVERSAL_1_3, false}, |
| 148 |
{"CopyLogical", 400, SPV_ENV_UNIVERSAL_1_4, true}, |
| 149 |
{"FPGARegINTEL", 5949}, |
| 150 |
{"SubgroupMatrixMultiplyAccumulateINTEL", 6237}, |
| 151 |
})); |
| 152 |
|
| 153 |
TEST(OpcodeLookupExtInstTest, Operands) { |
| 154 |
// The SPIR-V spec grammar has a single rule for OpExtInst, where the last |
| 155 |
// item is "sequence of Ids". SPIRV-Tools handles it differently. It drops |
| 156 |
// that last item, and instead specifies those operands as operands of the |
| 157 |
// extended instruction enum, such as 'cos'. |
| 158 |
// See https://github.com/KhronosGroup/SPIRV-Tools/issues/233 |
| 159 |
// Test the exact sequence of operand types extracted for OpExtInst. |
| 160 |
const InstructionDesc* desc = nullptr; |
| 161 |
auto status = LookupOpcode("ExtInst", &desc); |
| 162 |
EXPECT_EQ(status, SPV_SUCCESS); |
| 163 |
ASSERT_NE(desc, nullptr); |
| 164 |
|
| 165 |
EXPECT_EQ(desc->operands_range.count(), 4u); |
| 166 |
|
| 167 |
auto operands = desc->operands(); |
| 168 |
using vtype = std::vector<spv_operand_type_t>; |
| 169 |
|
| 170 |
EXPECT_THAT( |
| 171 |
vtype(operands.begin(), operands.end()), |
| 172 |
ContainerEq(vtype{SPV_OPERAND_TYPE_TYPE_ID, SPV_OPERAND_TYPE_RESULT_ID, |
| 173 |
SPV_OPERAND_TYPE_ID, |
| 174 |
SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER})); |
| 175 |
} |
| 176 |
|
| 177 |
// Test printingClass |
| 178 |
|
| 179 |
struct OpcodePrintingClassCase { |
| 180 |
std::string name; |
| 181 |
PrintingClass expected; |
| 182 |
}; |
| 183 |
|
| 184 |
std::ostream& operator<<(std::ostream& os, |
| 185 |
const OpcodePrintingClassCase& opcc) { |
| 186 |
os << "OPCC('" << opcc.name << "', " << static_cast<int>(opcc.expected) |
| 187 |
<< ")"; |
| 188 |
return os; |
| 189 |
} |
| 190 |
|
| 191 |
using OpcodePrintingClassTest = |
| 192 |
::testing::TestWithParam<OpcodePrintingClassCase>; |
| 193 |
|
| 194 |
TEST_P(OpcodePrintingClassTest, OpcodeLookup_ByName) { |
| 195 |
const InstructionDesc* desc = nullptr; |
| 196 |
auto status = LookupOpcode(GetParam().name.data(), &desc); |
| 197 |
EXPECT_EQ(status, SPV_SUCCESS); |
| 198 |
ASSERT_NE(desc, nullptr); |
| 199 |
EXPECT_EQ(desc->printingClass, GetParam().expected); |
| 200 |
} |
| 201 |
|
| 202 |
INSTANTIATE_TEST_SUITE_P( |
| 203 |
Samples, OpcodePrintingClassTest, |
| 204 |
ValuesIn(std::vector<OpcodePrintingClassCase>{ |
| 205 |
{"ConstantFunctionPointerINTEL", PrintingClass::k_exclude}, |
| 206 |
{"Nop", PrintingClass::kMiscellaneous}, |
| 207 |
{"SourceContinued", PrintingClass::kDebug}, |
| 208 |
{"Decorate", PrintingClass::kAnnotation}, |
| 209 |
{"Extension", PrintingClass::kExtension}, |
| 210 |
{"MemoryModel", PrintingClass::kMode_Setting}, |
| 211 |
{"Variable", PrintingClass::kMemory}, |
| 212 |
{"CooperativeMatrixPerElementOpNV", PrintingClass::kFunction}, |
| 213 |
{"SampledImage", PrintingClass::kImage}, |
| 214 |
{"ConvertFToU", PrintingClass::kConversion}, |
| 215 |
{"VectorExtractDynamic", PrintingClass::kComposite}, |
| 216 |
{"IAdd", PrintingClass::kArithmetic}, |
| 217 |
{"ShiftRightLogical", PrintingClass::kBit}, |
| 218 |
{"Any", PrintingClass::kRelational_and_Logical}, |
| 219 |
{"DPdx", PrintingClass::kDerivative}, |
| 220 |
{"Branch", PrintingClass::kControl_Flow}, |
| 221 |
{"AtomicLoad", PrintingClass::kAtomic}, |
| 222 |
{"ControlBarrier", PrintingClass::kBarrier}, |
| 223 |
{"GroupAll", PrintingClass::kGroup}, |
| 224 |
{"EnqueueMarker", PrintingClass::kDevice_Side_Enqueue}, |
| 225 |
{"ReadPipe", PrintingClass::kPipe}, |
| 226 |
{"GroupNonUniformElect", PrintingClass::kNon_Uniform}, |
| 227 |
// Skipping "Reserved" because it's probably an |
| 228 |
// unstable class. |
| 229 |
})); |
| 230 |
|
| 231 |
} // namespace |
| 232 |
} // namespace spvtools |