Coverage Report

Created: 2025-07-23 06:18

/src/spirv-tools/source/assembly_grammar.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2015-2016 The Khronos Group Inc.
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 "source/assembly_grammar.h"
16
17
#include <algorithm>
18
#include <cassert>
19
#include <cstring>
20
21
#include "source/ext_inst.h"
22
#include "source/opcode.h"
23
#include "source/operand.h"
24
#include "source/spirv_target_env.h"
25
#include "source/table.h"
26
#include "source/table2.h"
27
28
namespace spvtools {
29
namespace {
30
31
/// @brief Parses a mask expression string for the given operand type.
32
///
33
/// A mask expression is a sequence of one or more terms separated by '|',
34
/// where each term a named enum value for the given type.  No whitespace
35
/// is permitted.
36
///
37
/// On success, the value is written to pValue.
38
///
39
/// @param[in] type of the operand
40
/// @param[in] textValue word of text to be parsed
41
/// @param[out] pValue where the resulting value is written
42
///
43
/// @return result code
44
spv_result_t spvTextParseMaskOperand(const spv_operand_type_t type,
45
151k
                                     const char* textValue, uint32_t* pValue) {
46
151k
  if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT;
47
151k
  size_t text_length = strlen(textValue);
48
151k
  if (text_length == 0) return SPV_ERROR_INVALID_TEXT;
49
151k
  const char* text_end = textValue + text_length;
50
51
  // We only support mask expressions in ASCII, so the separator value is a
52
  // char.
53
151k
  const char separator = '|';
54
55
  // Accumulate the result by interpreting one word at a time, scanning
56
  // from left to right.
57
151k
  uint32_t value = 0;
58
151k
  const char* begin = textValue;  // The left end of the current word.
59
151k
  const char* end = nullptr;  // One character past the end of the current word.
60
153k
  do {
61
153k
    end = std::find(begin, text_end, separator);
62
63
153k
    const spvtools::OperandDesc* entry = nullptr;
64
153k
    if (auto error =
65
153k
            spvtools::LookupOperand(type, begin, end - begin, &entry)) {
66
128
      return error;
67
128
    }
68
152k
    value |= entry->value;
69
70
    // Advance to the next word by skipping over the separator.
71
152k
    begin = end + 1;
72
152k
  } while (end != text_end);
73
74
151k
  *pValue = value;
75
151k
  return SPV_SUCCESS;
76
151k
}
77
78
// Associates an opcode with its name.
79
struct SpecConstantOpcodeEntry {
80
  spv::Op opcode;
81
  const char* name;
82
};
83
84
// All the opcodes allowed as the operation for OpSpecConstantOp.
85
// The name does not have the usual "Op" prefix. For example opcode
86
// spv::Op::IAdd is associated with the name "IAdd".
87
//
88
// clang-format off
89
#define CASE(NAME) { spv::Op::Op##NAME, #NAME }
90
const SpecConstantOpcodeEntry kOpSpecConstantOpcodes[] = {
91
    // Conversion
92
    CASE(SConvert),
93
    CASE(FConvert),
94
    CASE(ConvertFToS),
95
    CASE(ConvertSToF),
96
    CASE(ConvertFToU),
97
    CASE(ConvertUToF),
98
    CASE(UConvert),
99
    CASE(ConvertPtrToU),
100
    CASE(ConvertUToPtr),
101
    CASE(GenericCastToPtr),
102
    CASE(PtrCastToGeneric),
103
    CASE(Bitcast),
104
    CASE(QuantizeToF16),
105
    // Arithmetic
106
    CASE(SNegate),
107
    CASE(Not),
108
    CASE(IAdd),
109
    CASE(ISub),
110
    CASE(IMul),
111
    CASE(UDiv),
112
    CASE(SDiv),
113
    CASE(UMod),
114
    CASE(SRem),
115
    CASE(SMod),
116
    CASE(ShiftRightLogical),
117
    CASE(ShiftRightArithmetic),
118
    CASE(ShiftLeftLogical),
119
    CASE(BitwiseOr),
120
    CASE(BitwiseAnd),
121
    CASE(BitwiseXor),
122
    CASE(FNegate),
123
    CASE(FAdd),
124
    CASE(FSub),
125
    CASE(FMul),
126
    CASE(FDiv),
127
    CASE(FRem),
128
    CASE(FMod),
129
    // Composite
130
    CASE(VectorShuffle),
131
    CASE(CompositeExtract),
132
    CASE(CompositeInsert),
133
    // Logical
134
    CASE(LogicalOr),
135
    CASE(LogicalAnd),
136
    CASE(LogicalNot),
137
    CASE(LogicalEqual),
138
    CASE(LogicalNotEqual),
139
    CASE(Select),
140
    // Comparison
141
    CASE(IEqual),
142
    CASE(INotEqual),
143
    CASE(ULessThan),
144
    CASE(SLessThan),
145
    CASE(UGreaterThan),
146
    CASE(SGreaterThan),
147
    CASE(ULessThanEqual),
148
    CASE(SLessThanEqual),
149
    CASE(UGreaterThanEqual),
150
    CASE(SGreaterThanEqual),
151
    // Memory
152
    CASE(AccessChain),
153
    CASE(InBoundsAccessChain),
154
    CASE(PtrAccessChain),
155
    CASE(InBoundsPtrAccessChain),
156
    CASE(CooperativeMatrixLengthNV),
157
    CASE(CooperativeMatrixLengthKHR)
158
};
159
160
// The 60 is determined by counting the opcodes listed in the spec.
161
static_assert(61 == sizeof(kOpSpecConstantOpcodes)/sizeof(kOpSpecConstantOpcodes[0]),
162
              "OpSpecConstantOp opcode table is incomplete");
163
#undef CASE
164
// clang-format on
165
166
const size_t kNumOpSpecConstantOpcodes =
167
    sizeof(kOpSpecConstantOpcodes) / sizeof(kOpSpecConstantOpcodes[0]);
168
169
}  // namespace
170
171
CapabilitySet AssemblyGrammar::filterCapsAgainstTargetEnv(
172
13.6M
    const spv::Capability* cap_array, uint32_t count) const {
173
13.6M
  CapabilitySet cap_set;
174
13.6M
  const auto version = spvVersionForTargetEnv(target_env_);
175
15.2M
  for (uint32_t i = 0; i < count; ++i) {
176
1.60M
    const spvtools::OperandDesc* entry = nullptr;
177
1.60M
    if (SPV_SUCCESS ==
178
1.60M
        spvtools::LookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
179
1.60M
                                static_cast<uint32_t>(cap_array[i]), &entry)) {
180
      // This token is visible in this environment if it's in an appropriate
181
      // core version, or it is enabled by a capability or an extension.
182
1.60M
      if ((version >= entry->minVersion && version <= entry->lastVersion) ||
183
1.60M
          entry->extensions_range.count() > 0u ||
184
1.60M
          entry->capabilities_range.count() > 0u) {
185
1.60M
        cap_set.insert(cap_array[i]);
186
1.60M
      }
187
1.60M
    }
188
1.60M
  }
189
13.6M
  return cap_set;
190
13.6M
}
191
192
const char* AssemblyGrammar::lookupOperandName(spv_operand_type_t type,
193
9
                                               uint32_t operand) const {
194
9
  const spvtools::OperandDesc* desc = nullptr;
195
9
  if (spvtools::LookupOperand(type, operand, &desc) != SPV_SUCCESS || !desc) {
196
0
    return "Unknown";
197
0
  }
198
9
  return desc->name().data();
199
9
}
200
201
spv_result_t AssemblyGrammar::lookupSpecConstantOpcode(const char* name,
202
1.81k
                                                       spv::Op* opcode) const {
203
1.81k
  const auto* last = kOpSpecConstantOpcodes + kNumOpSpecConstantOpcodes;
204
1.81k
  const auto* found =
205
1.81k
      std::find_if(kOpSpecConstantOpcodes, last,
206
106k
                   [name](const SpecConstantOpcodeEntry& entry) {
207
106k
                     return 0 == strcmp(name, entry.name);
208
106k
                   });
209
1.81k
  if (found == last) return SPV_ERROR_INVALID_LOOKUP;
210
1.60k
  *opcode = found->opcode;
211
1.60k
  return SPV_SUCCESS;
212
1.81k
}
213
214
6.47k
spv_result_t AssemblyGrammar::lookupSpecConstantOpcode(spv::Op opcode) const {
215
6.47k
  const auto* last = kOpSpecConstantOpcodes + kNumOpSpecConstantOpcodes;
216
6.47k
  const auto* found =
217
6.47k
      std::find_if(kOpSpecConstantOpcodes, last,
218
319k
                   [opcode](const SpecConstantOpcodeEntry& entry) {
219
319k
                     return opcode == entry.opcode;
220
319k
                   });
221
6.47k
  if (found == last) return SPV_ERROR_INVALID_LOOKUP;
222
2.65k
  return SPV_SUCCESS;
223
6.47k
}
224
225
spv_result_t AssemblyGrammar::parseMaskOperand(const spv_operand_type_t type,
226
                                               const char* textValue,
227
151k
                                               uint32_t* pValue) const {
228
151k
  return spvTextParseMaskOperand(type, textValue, pValue);
229
151k
}
230
231
void AssemblyGrammar::pushOperandTypesForMask(
232
    const spv_operand_type_t type, const uint32_t mask,
233
151k
    spv_operand_pattern_t* pattern) const {
234
151k
  spvPushOperandTypesForMask(type, mask, pattern);
235
151k
}
236
237
}  // namespace spvtools