Coverage Report

Created: 2026-09-01 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc/third_party/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
0
                                     const char* textValue, uint32_t* pValue) {
46
0
  if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT;
47
0
  size_t text_length = strlen(textValue);
48
0
  if (text_length == 0) return SPV_ERROR_INVALID_TEXT;
49
0
  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
0
  const char separator = '|';
54
55
  // Accumulate the result by interpreting one word at a time, scanning
56
  // from left to right.
57
0
  uint32_t value = 0;
58
0
  const char* begin = textValue;  // The left end of the current word.
59
0
  const char* end = nullptr;  // One character past the end of the current word.
60
0
  do {
61
0
    end = std::find(begin, text_end, separator);
62
63
0
    const spvtools::OperandDesc* entry = nullptr;
64
0
    if (auto error =
65
0
            spvtools::LookupOperand(type, begin, end - begin, &entry)) {
66
0
      return error;
67
0
    }
68
0
    value |= entry->value;
69
70
    // Advance to the next word by skipping over the separator.
71
0
    begin = end + 1;
72
0
  } while (end != text_end);
73
74
0
  *pValue = value;
75
0
  return SPV_SUCCESS;
76
0
}
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(UntypedAccessChainKHR),
157
    CASE(UntypedInBoundsAccessChainKHR),
158
    CASE(UntypedPtrAccessChainKHR),
159
    CASE(UntypedInBoundsPtrAccessChainKHR),
160
    CASE(CooperativeMatrixLengthNV),
161
    CASE(CooperativeMatrixLengthKHR)
162
};
163
164
// The count is determined by counting the opcodes listed in the spec.
165
static_assert(65 == sizeof(kOpSpecConstantOpcodes)/sizeof(kOpSpecConstantOpcodes[0]),
166
              "OpSpecConstantOp opcode table is incomplete");
167
#undef CASE
168
// clang-format on
169
170
const size_t kNumOpSpecConstantOpcodes =
171
    sizeof(kOpSpecConstantOpcodes) / sizeof(kOpSpecConstantOpcodes[0]);
172
173
}  // namespace
174
175
CapabilitySet AssemblyGrammar::filterCapsAgainstTargetEnv(
176
53.6k
    const spv::Capability* cap_array, uint32_t count) const {
177
53.6k
  CapabilitySet cap_set;
178
53.6k
  const auto version = spvVersionForTargetEnv(target_env_);
179
63.3k
  for (uint32_t i = 0; i < count; ++i) {
180
9.70k
    const spvtools::OperandDesc* entry = nullptr;
181
9.70k
    if (SPV_SUCCESS ==
182
9.70k
        spvtools::LookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
183
9.70k
                                static_cast<uint32_t>(cap_array[i]), &entry)) {
184
      // This token is visible in this environment if it's in an appropriate
185
      // core version, or it is enabled by a capability or an extension.
186
9.70k
      if ((version >= entry->minVersion && version <= entry->lastVersion) ||
187
4.49k
          entry->extensions_range.count() > 0u ||
188
9.70k
          entry->capabilities_range.count() > 0u) {
189
9.70k
        cap_set.insert(cap_array[i]);
190
9.70k
      }
191
9.70k
    }
192
9.70k
  }
193
53.6k
  return cap_set;
194
53.6k
}
195
196
const char* AssemblyGrammar::lookupOperandName(spv_operand_type_t type,
197
8
                                               uint32_t operand) const {
198
8
  const spvtools::OperandDesc* desc = nullptr;
199
8
  if (spvtools::LookupOperand(type, operand, &desc) != SPV_SUCCESS || !desc) {
200
0
    return "Unknown";
201
0
  }
202
8
  return desc->name().data();
203
8
}
204
205
spv_result_t AssemblyGrammar::lookupSpecConstantOpcode(const char* name,
206
0
                                                       spv::Op* opcode) const {
207
0
  const auto* last = kOpSpecConstantOpcodes + kNumOpSpecConstantOpcodes;
208
0
  const auto* found =
209
0
      std::find_if(kOpSpecConstantOpcodes, last,
210
0
                   [name](const SpecConstantOpcodeEntry& entry) {
211
0
                     return 0 == strcmp(name, entry.name);
212
0
                   });
213
0
  if (found == last) return SPV_ERROR_INVALID_LOOKUP;
214
0
  *opcode = found->opcode;
215
0
  return SPV_SUCCESS;
216
0
}
217
218
470
spv_result_t AssemblyGrammar::lookupSpecConstantOpcode(spv::Op opcode) const {
219
470
  const auto* last = kOpSpecConstantOpcodes + kNumOpSpecConstantOpcodes;
220
470
  const auto* found =
221
470
      std::find_if(kOpSpecConstantOpcodes, last,
222
8.28k
                   [opcode](const SpecConstantOpcodeEntry& entry) {
223
8.28k
                     return opcode == entry.opcode;
224
8.28k
                   });
225
470
  if (found == last) return SPV_ERROR_INVALID_LOOKUP;
226
470
  return SPV_SUCCESS;
227
470
}
228
229
spv_result_t AssemblyGrammar::parseMaskOperand(const spv_operand_type_t type,
230
                                               const char* textValue,
231
0
                                               uint32_t* pValue) const {
232
0
  return spvTextParseMaskOperand(type, textValue, pValue);
233
0
}
234
235
void AssemblyGrammar::pushOperandTypesForMask(
236
    const spv_operand_type_t type, const uint32_t mask,
237
0
    spv_operand_pattern_t* pattern) const {
238
0
  spvPushOperandTypesForMask(type, mask, pattern);
239
0
}
240
241
}  // namespace spvtools