Coverage Report

Created: 2026-02-14 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spirv-tools/source/assembly_grammar.cpp
Line
Count
Source
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
114k
                                     const char* textValue, uint32_t* pValue) {
46
114k
  if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT;
47
114k
  size_t text_length = strlen(textValue);
48
114k
  if (text_length == 0) return SPV_ERROR_INVALID_TEXT;
49
114k
  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
114k
  const char separator = '|';
54
55
  // Accumulate the result by interpreting one word at a time, scanning
56
  // from left to right.
57
114k
  uint32_t value = 0;
58
114k
  const char* begin = textValue;  // The left end of the current word.
59
114k
  const char* end = nullptr;  // One character past the end of the current word.
60
121k
  do {
61
121k
    end = std::find(begin, text_end, separator);
62
63
121k
    const spvtools::OperandDesc* entry = nullptr;
64
121k
    if (auto error =
65
121k
            spvtools::LookupOperand(type, begin, end - begin, &entry)) {
66
132
      return error;
67
132
    }
68
121k
    value |= entry->value;
69
70
    // Advance to the next word by skipping over the separator.
71
121k
    begin = end + 1;
72
121k
  } while (end != text_end);
73
74
113k
  *pValue = value;
75
113k
  return SPV_SUCCESS;
76
114k
}
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
15.0M
    const spv::Capability* cap_array, uint32_t count) const {
173
15.0M
  CapabilitySet cap_set;
174
15.0M
  const auto version = spvVersionForTargetEnv(target_env_);
175
15.7M
  for (uint32_t i = 0; i < count; ++i) {
176
625k
    const spvtools::OperandDesc* entry = nullptr;
177
625k
    if (SPV_SUCCESS ==
178
625k
        spvtools::LookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
179
625k
                                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
625k
      if ((version >= entry->minVersion && version <= entry->lastVersion) ||
183
36.1k
          entry->extensions_range.count() > 0u ||
184
623k
          entry->capabilities_range.count() > 0u) {
185
623k
        cap_set.insert(cap_array[i]);
186
623k
      }
187
625k
    }
188
625k
  }
189
15.0M
  return cap_set;
190
15.0M
}
191
192
const char* AssemblyGrammar::lookupOperandName(spv_operand_type_t type,
193
6
                                               uint32_t operand) const {
194
6
  const spvtools::OperandDesc* desc = nullptr;
195
6
  if (spvtools::LookupOperand(type, operand, &desc) != SPV_SUCCESS || !desc) {
196
0
    return "Unknown";
197
0
  }
198
6
  return desc->name().data();
199
6
}
200
201
spv_result_t AssemblyGrammar::lookupSpecConstantOpcode(const char* name,
202
2.27k
                                                       spv::Op* opcode) const {
203
2.27k
  const auto* last = kOpSpecConstantOpcodes + kNumOpSpecConstantOpcodes;
204
2.27k
  const auto* found =
205
2.27k
      std::find_if(kOpSpecConstantOpcodes, last,
206
97.3k
                   [name](const SpecConstantOpcodeEntry& entry) {
207
97.3k
                     return 0 == strcmp(name, entry.name);
208
97.3k
                   });
209
2.27k
  if (found == last) return SPV_ERROR_INVALID_LOOKUP;
210
2.04k
  *opcode = found->opcode;
211
2.04k
  return SPV_SUCCESS;
212
2.27k
}
213
214
8.68k
spv_result_t AssemblyGrammar::lookupSpecConstantOpcode(spv::Op opcode) const {
215
8.68k
  const auto* last = kOpSpecConstantOpcodes + kNumOpSpecConstantOpcodes;
216
8.68k
  const auto* found =
217
8.68k
      std::find_if(kOpSpecConstantOpcodes, last,
218
459k
                   [opcode](const SpecConstantOpcodeEntry& entry) {
219
459k
                     return opcode == entry.opcode;
220
459k
                   });
221
8.68k
  if (found == last) return SPV_ERROR_INVALID_LOOKUP;
222
2.96k
  return SPV_SUCCESS;
223
8.68k
}
224
225
spv_result_t AssemblyGrammar::parseMaskOperand(const spv_operand_type_t type,
226
                                               const char* textValue,
227
114k
                                               uint32_t* pValue) const {
228
114k
  return spvTextParseMaskOperand(type, textValue, pValue);
229
114k
}
230
231
void AssemblyGrammar::pushOperandTypesForMask(
232
    const spv_operand_type_t type, const uint32_t mask,
233
113k
    spv_operand_pattern_t* pattern) const {
234
113k
  spvPushOperandTypesForMask(type, mask, pattern);
235
113k
}
236
237
}  // namespace spvtools