Coverage Report

Created: 2025-06-13 06:37

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