Debian Codegraph @

test/operand_lookup_test.cpp

raw
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/operand.h"
20 #include "source/table2.h"
21 #include "spirv-tools/libspirv.h"
22 #include "test/unit_spirv.h"
23
24 using ::testing::ContainerEq;
25 using ::testing::ValuesIn;
26
27 namespace spvtools {
28 namespace {
29
30 struct OperandLookupCase {
31 spv_operand_type_t type;
32 std::string name;
33 size_t name_length;
34 uint32_t value;
35 bool expect_pass = true;
36 };
37
38 std::ostream& operator<<(std::ostream& os, const OperandLookupCase& olc) {
39 os << "OLC('" << spvOperandTypeStr(olc.type) << " '" << olc.name
40 << "', len:" << olc.name_length << ", value:" << olc.value
41 << ", expect pass? " << olc.expect_pass << ")";
42 return os;
43 }
44
45 using OperandLookupTest = ::testing::TestWithParam<OperandLookupCase>;
46
47 TEST_P(OperandLookupTest, OperandLookup_ByName) {
48 const OperandDesc* desc = nullptr;
49 auto status = LookupOperand(GetParam().type, GetParam().name.data(),
50 GetParam().name_length, &desc);
51 if (GetParam().expect_pass) {
52 EXPECT_EQ(status, SPV_SUCCESS);
53 ASSERT_NE(desc, nullptr);
54 EXPECT_EQ(desc->value, GetParam().value);
55 } else {
56 EXPECT_NE(status, SPV_SUCCESS);
57 EXPECT_EQ(desc, nullptr);
58 }
59 }
60
61 TEST_P(OperandLookupTest, OperandLookup_ByValue_Success) {
62 const OperandDesc* desc = nullptr;
63 if (GetParam().expect_pass) {
64 const auto value = GetParam().value;
65 auto status = LookupOperand(GetParam().type, GetParam().value, &desc);
66 EXPECT_EQ(status, SPV_SUCCESS);
67 ASSERT_NE(desc, nullptr);
68 EXPECT_EQ(desc->value, value);
69 }
70 }
71
72 INSTANTIATE_TEST_SUITE_P(
73 Samples, OperandLookupTest,
74 ValuesIn(std::vector<OperandLookupCase>{
75 {SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID, "Relaxed", 7, 0},
76 // "None" is an alias for "Relaxed"
77 {SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID, "None", 4, 0},
78 // "NonPrivatePointer" is the canonical name that appeared
79 // in an extension and incorporated in SPIR-V 1.5.
80 {SPV_OPERAND_TYPE_MEMORY_ACCESS, "NonPrivatePointer", 17, 32},
81 // "NonPrivatePointerKHR" is the name from the extension.
82 {SPV_OPERAND_TYPE_MEMORY_ACCESS, "NonPrivatePointerKHR", 20, 32},
83 // "NoAliasINTELMask" is only in an extension
84 {SPV_OPERAND_TYPE_MEMORY_ACCESS, "NoAliasINTELMask", 16, 0x20000},
85 {SPV_OPERAND_TYPE_RAY_FLAGS, "TerminateOnFirstHitKHR", 22, 4},
86 {SPV_OPERAND_TYPE_FPENCODING, "BFloat16KHR", 11, 0},
87 // Lookup on An optional operand type should match the base lookup.
88 {SPV_OPERAND_TYPE_OPTIONAL_FPENCODING, "BFloat16KHR", 11, 0},
89 // Lookup is type-specific.
90 {SPV_OPERAND_TYPE_OPTIONAL_FPENCODING, "Relaxed", 7, 0, false},
91 // Invalid string
92 {SPV_OPERAND_TYPE_RAY_FLAGS, "does_not_exist", 14, 0, false},
93 // Check lengths
94 {SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID, "Relaxed", 6, 0, false},
95 {SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID, "Relaxed", 7, 0, true},
96 {SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID, "Relaxed|", 7, 0, true},
97 {SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID, "Relaxed|", 8, 0, false},
98 }));
99
100 TEST(OperandLookupSingleTest, OperandLookup_ByValue_Fails) {
101 // This list may need adjusting over time.
102 std::array<spv_operand_type_t, 3> types = {
103 {SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID, SPV_OPERAND_TYPE_RAY_FLAGS,
104 SPV_OPERAND_TYPE_FPENCODING}};
105 std::array<uint32_t, 3> bad_values = {{99999, 37737, 110101}};
106 for (auto type : types) {
107 for (auto bad_value : bad_values) {
108 const OperandDesc* desc = nullptr;
109 auto status = LookupOperand(type, bad_value, &desc);
110 EXPECT_NE(status, SPV_SUCCESS);
111 ASSERT_EQ(desc, nullptr);
112 }
113 }
114 }
115
116 TEST(OperandLookupOperands, Sample) {
117 // Check the operand list for a valid operand lookup.
118 const OperandDesc* desc = nullptr;
119 auto status = LookupOperand(SPV_OPERAND_TYPE_IMAGE, "Grad", 4, &desc);
120 EXPECT_EQ(status, SPV_SUCCESS);
121 ASSERT_NE(desc, nullptr);
122
123 EXPECT_EQ(desc->operands_range.count(), 2u);
124
125 auto operands = desc->operands();
126 using vtype = std::vector<spv_operand_type_t>;
127
128 EXPECT_THAT(vtype(operands.begin(), operands.end()),
129 ContainerEq(vtype{SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID}));
130 }
131
132 } // namespace
133 } // namespace spvtools