Coverage Report

Created: 2025-11-19 06:41

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(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
300k
    const spv::Capability* cap_array, uint32_t count) const {
173
300k
  CapabilitySet cap_set;
174
300k
  const auto version = spvVersionForTargetEnv(target_env_);
175
334k
  for (uint32_t i = 0; i < count; ++i) {
176
34.5k
    const spvtools::OperandDesc* entry = nullptr;
177
34.5k
    if (SPV_SUCCESS ==
178
34.5k
        spvtools::LookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
179
34.5k
                                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
34.5k
      if ((version >= entry->minVersion && version <= entry->lastVersion) ||
183
10.8k
          entry->extensions_range.count() > 0u ||
184
34.5k
          entry->capabilities_range.count() > 0u) {
185
34.5k
        cap_set.insert(cap_array[i]);
186
34.5k
      }
187
34.5k
    }
188
34.5k
  }
189
300k
  return cap_set;
190
300k
}
191
192
const char* AssemblyGrammar::lookupOperandName(spv_operand_type_t type,
193
16
                                               uint32_t operand) const {
194
16
  const spvtools::OperandDesc* desc = nullptr;
195
16
  if (spvtools::LookupOperand(type, operand, &desc) != SPV_SUCCESS || !desc) {
196
0
    return "Unknown";
197
0
  }
198
16
  return desc->name().data();
199
16
}
200
201
spv_result_t AssemblyGrammar::lookupSpecConstantOpcode(const char* name,
202
0
                                                       spv::Op* opcode) const {
203
0
  const auto* last = kOpSpecConstantOpcodes + kNumOpSpecConstantOpcodes;
204
0
  const auto* found =
205
0
      std::find_if(kOpSpecConstantOpcodes, last,
206
0
                   [name](const SpecConstantOpcodeEntry& entry) {
207
0
                     return 0 == strcmp(name, entry.name);
208
0
                   });
209
0
  if (found == last) return SPV_ERROR_INVALID_LOOKUP;
210
0
  *opcode = found->opcode;
211
0
  return SPV_SUCCESS;
212
0
}
213
214
596
spv_result_t AssemblyGrammar::lookupSpecConstantOpcode(spv::Op opcode) const {
215
596
  const auto* last = kOpSpecConstantOpcodes + kNumOpSpecConstantOpcodes;
216
596
  const auto* found =
217
596
      std::find_if(kOpSpecConstantOpcodes, last,
218
10.3k
                   [opcode](const SpecConstantOpcodeEntry& entry) {
219
10.3k
                     return opcode == entry.opcode;
220
10.3k
                   });
221
596
  if (found == last) return SPV_ERROR_INVALID_LOOKUP;
222
596
  return SPV_SUCCESS;
223
596
}
224
225
spv_result_t AssemblyGrammar::parseMaskOperand(const spv_operand_type_t type,
226
                                               const char* textValue,
227
0
                                               uint32_t* pValue) const {
228
0
  return spvTextParseMaskOperand(type, textValue, pValue);
229
0
}
230
231
void AssemblyGrammar::pushOperandTypesForMask(
232
    const spv_operand_type_t type, const uint32_t mask,
233
0
    spv_operand_pattern_t* pattern) const {
234
0
  spvPushOperandTypesForMask(type, mask, pattern);
235
0
}
236
237
}  // namespace spvtools