Coverage Report

Created: 2026-05-30 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc/third_party/spirv-tools/source/name_mapper.cpp
Line
Count
Source
1
// Copyright (c) 2016 Google Inc.
2
// Modifications Copyright (C) 2024 Advanced Micro Devices, Inc. All rights
3
// reserved.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
//     http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include "source/name_mapper.h"
18
19
#include <algorithm>
20
#include <cassert>
21
#include <iterator>
22
#include <sstream>
23
#include <string>
24
#include <unordered_map>
25
#include <unordered_set>
26
27
#include "source/binary.h"
28
#include "source/latest_version_spirv_header.h"
29
#include "source/parsed_operand.h"
30
#include "source/table2.h"
31
#include "source/to_string.h"
32
#include "spirv-tools/libspirv.h"
33
34
namespace spvtools {
35
36
362
NameMapper GetTrivialNameMapper() {
37
362
  return [](uint32_t i) { return spvtools::to_string(i); };
38
362
}
39
40
FriendlyNameMapper::FriendlyNameMapper(const spv_const_context context,
41
                                       const uint32_t* code,
42
                                       const size_t wordCount, uint32_t options)
43
168
    : grammar_(AssemblyGrammar(context)) {
44
168
  spv_diagnostic diag = nullptr;
45
  // We don't care if the parse fails.
46
168
  spvBinaryParseWithOptions(context, this, code, wordCount, nullptr,
47
168
                            ParseInstructionForwarder, &diag, options);
48
168
  spvDiagnosticDestroy(diag);
49
168
}
50
51
57.3k
std::string FriendlyNameMapper::NameForId(uint32_t id) {
52
57.3k
  auto iter = name_for_id_.find(id);
53
57.3k
  if (iter == name_for_id_.end()) {
54
    // It must have been an invalid module, so just return a trivial mapping.
55
    // We don't care about uniqueness.
56
0
    return to_string(id);
57
57.3k
  } else {
58
57.3k
    return iter->second;
59
57.3k
  }
60
57.3k
}
61
62
20.8k
std::string FriendlyNameMapper::Sanitize(const std::string& suggested_name) {
63
20.8k
  if (suggested_name.empty()) return "_";
64
  // Otherwise, replace invalid characters by '_'.
65
20.8k
  std::string result;
66
20.8k
  std::string valid =
67
20.8k
      "abcdefghijklmnopqrstuvwxyz"
68
20.8k
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
69
20.8k
      "_0123456789";
70
20.8k
  std::transform(suggested_name.begin(), suggested_name.end(),
71
94.0k
                 std::back_inserter(result), [&valid](const char c) {
72
94.0k
                   return (std::string::npos == valid.find(c)) ? '_' : c;
73
94.0k
                 });
74
20.8k
  return result;
75
20.8k
}
76
77
void FriendlyNameMapper::SaveName(uint32_t id,
78
21.0k
                                  const std::string& suggested_name) {
79
21.0k
  if (name_for_id_.find(id) != name_for_id_.end()) return;
80
81
20.8k
  const std::string sanitized_suggested_name = Sanitize(suggested_name);
82
20.8k
  std::string name = sanitized_suggested_name;
83
20.8k
  auto inserted = used_names_.insert(name);
84
20.8k
  if (!inserted.second) {
85
125
    const std::string base_name = sanitized_suggested_name + "_";
86
425
    for (uint32_t index = 0; !inserted.second; ++index) {
87
300
      name = base_name + to_string(index);
88
300
      inserted = used_names_.insert(name);
89
300
    }
90
125
  }
91
20.8k
  name_for_id_[id] = name;
92
20.8k
}
93
94
void FriendlyNameMapper::SaveBuiltInName(uint32_t target_id,
95
25
                                         uint32_t built_in) {
96
25
#define GLCASE(name)                  \
97
25
  case spv::BuiltIn::name:            \
98
0
    SaveName(target_id, "gl_" #name); \
99
0
    return;
100
25
#define GLCASE2(name, suggested)           \
101
25
  case spv::BuiltIn::name:                 \
102
0
    SaveName(target_id, "gl_" #suggested); \
103
0
    return;
104
25
#define CASE(name)              \
105
25
  case spv::BuiltIn::name:      \
106
0
    SaveName(target_id, #name); \
107
0
    return;
108
25
  switch (spv::BuiltIn(built_in)) {
109
0
    GLCASE(Position)
110
0
    GLCASE(PointSize)
111
0
    GLCASE(ClipDistance)
112
0
    GLCASE(CullDistance)
113
0
    GLCASE2(VertexId, VertexID)
114
0
    GLCASE2(InstanceId, InstanceID)
115
0
    GLCASE2(PrimitiveId, PrimitiveID)
116
0
    GLCASE2(InvocationId, InvocationID)
117
0
    GLCASE(Layer)
118
0
    GLCASE(ViewportIndex)
119
0
    GLCASE(TessLevelOuter)
120
0
    GLCASE(TessLevelInner)
121
0
    GLCASE(TessCoord)
122
0
    GLCASE(PatchVertices)
123
0
    GLCASE(FragCoord)
124
0
    GLCASE(PointCoord)
125
0
    GLCASE(FrontFacing)
126
0
    GLCASE2(SampleId, SampleID)
127
0
    GLCASE(SamplePosition)
128
0
    GLCASE(SampleMask)
129
0
    GLCASE(FragDepth)
130
0
    GLCASE(HelperInvocation)
131
0
    GLCASE2(NumWorkgroups, NumWorkGroups)
132
0
    GLCASE2(WorkgroupSize, WorkGroupSize)
133
0
    GLCASE2(WorkgroupId, WorkGroupID)
134
0
    GLCASE2(LocalInvocationId, LocalInvocationID)
135
0
    GLCASE2(GlobalInvocationId, GlobalInvocationID)
136
0
    GLCASE(LocalInvocationIndex)
137
0
    CASE(WorkDim)
138
0
    CASE(GlobalSize)
139
0
    CASE(EnqueuedWorkgroupSize)
140
0
    CASE(GlobalOffset)
141
0
    CASE(GlobalLinearId)
142
0
    CASE(SubgroupSize)
143
0
    CASE(SubgroupMaxSize)
144
0
    CASE(NumSubgroups)
145
0
    CASE(NumEnqueuedSubgroups)
146
0
    CASE(SubgroupId)
147
0
    CASE(SubgroupLocalInvocationId)
148
0
    GLCASE(VertexIndex)
149
0
    GLCASE(InstanceIndex)
150
0
    GLCASE(BaseInstance)
151
0
    CASE(SubgroupEqMaskKHR)
152
0
    CASE(SubgroupGeMaskKHR)
153
0
    CASE(SubgroupGtMaskKHR)
154
0
    CASE(SubgroupLeMaskKHR)
155
0
    CASE(SubgroupLtMaskKHR)
156
25
    default:
157
25
      break;
158
25
  }
159
25
#undef GLCASE
160
25
#undef GLCASE2
161
25
#undef CASE
162
25
}
163
164
spv_result_t FriendlyNameMapper::ParseInstruction(
165
32.3k
    const spv_parsed_instruction_t& inst) {
166
32.3k
  const auto result_id = inst.result_id;
167
32.3k
  switch (spv::Op(inst.opcode)) {
168
1.46k
    case spv::Op::OpName:
169
1.46k
      SaveName(inst.words[1], spvDecodeLiteralStringOperand(inst, 1));
170
1.46k
      break;
171
2.17k
    case spv::Op::OpDecorate:
172
      // Decorations come after OpName.  So OpName will take precedence over
173
      // decorations.
174
      //
175
      // In theory, we should also handle OpGroupDecorate.  But that's unlikely
176
      // to occur.
177
2.17k
      if (spv::Decoration(inst.words[2]) == spv::Decoration::BuiltIn) {
178
25
        assert(inst.num_words > 3);
179
25
        SaveBuiltInName(inst.words[1], inst.words[3]);
180
25
      }
181
2.17k
      break;
182
167
    case spv::Op::OpTypeVoid:
183
167
      SaveName(result_id, "void");
184
167
      break;
185
52
    case spv::Op::OpTypeBool:
186
52
      SaveName(result_id, "bool");
187
52
      break;
188
299
    case spv::Op::OpTypeInt: {
189
299
      std::string signedness;
190
299
      std::string root;
191
299
      const auto bit_width = inst.words[2];
192
299
      switch (bit_width) {
193
16
        case 8:
194
16
          root = "char";
195
16
          break;
196
18
        case 16:
197
18
          root = "short";
198
18
          break;
199
208
        case 32:
200
208
          root = "int";
201
208
          break;
202
57
        case 64:
203
57
          root = "long";
204
57
          break;
205
0
        default:
206
0
          root = to_string(bit_width);
207
0
          signedness = "i";
208
0
          break;
209
299
      }
210
299
      if (0 == inst.words[3]) signedness = "u";
211
299
      SaveName(result_id, signedness + root);
212
299
    } break;
213
118
    case spv::Op::OpTypeFloat: {
214
118
      const auto bit_width = inst.words[2];
215
118
      if (inst.num_words > 3) {
216
0
        if (spv::FPEncoding(inst.words[3]) == spv::FPEncoding::BFloat16KHR) {
217
0
          SaveName(result_id, "bfloat16");
218
0
          break;
219
0
        }
220
0
        if (spv::FPEncoding(inst.words[3]) == spv::FPEncoding::Float8E4M3EXT) {
221
0
          SaveName(result_id, "fp8e4m3");
222
0
          break;
223
0
        }
224
0
        if (spv::FPEncoding(inst.words[3]) == spv::FPEncoding::Float8E5M2EXT) {
225
0
          SaveName(result_id, "fp8e5m2");
226
0
          break;
227
0
        }
228
0
      }
229
118
      switch (bit_width) {
230
14
        case 16:
231
14
          SaveName(result_id, "half");
232
14
          break;
233
101
        case 32:
234
101
          SaveName(result_id, "float");
235
101
          break;
236
3
        case 64:
237
3
          SaveName(result_id, "double");
238
3
          break;
239
0
        default:
240
0
          SaveName(result_id, std::string("fp") + to_string(bit_width));
241
0
          break;
242
118
      }
243
118
    } break;
244
442
    case spv::Op::OpTypeVector:
245
442
      SaveName(result_id, std::string("v") + to_string(inst.words[3]) +
246
442
                              NameForId(inst.words[2]));
247
442
      break;
248
48
    case spv::Op::OpTypeMatrix:
249
48
      SaveName(result_id, std::string("mat") + to_string(inst.words[3]) +
250
48
                              NameForId(inst.words[2]));
251
48
      break;
252
88
    case spv::Op::OpTypeArray:
253
88
      SaveName(result_id, std::string("_arr_") + NameForId(inst.words[2]) +
254
88
                              "_" + NameForId(inst.words[3]));
255
88
      break;
256
8
    case spv::Op::OpTypeRuntimeArray:
257
8
      SaveName(result_id,
258
8
               std::string("_runtimearr_") + NameForId(inst.words[2]));
259
8
      break;
260
0
    case spv::Op::OpTypeNodePayloadArrayAMDX:
261
0
      SaveName(result_id,
262
0
               std::string("_payloadarr_") + NameForId(inst.words[2]));
263
0
      break;
264
1.21k
    case spv::Op::OpTypePointer:
265
1.21k
      SaveName(result_id, std::string("_ptr_") +
266
1.21k
                              NameForEnumOperand(SPV_OPERAND_TYPE_STORAGE_CLASS,
267
1.21k
                                                 inst.words[2]) +
268
1.21k
                              "_" + NameForId(inst.words[3]));
269
1.21k
      break;
270
0
    case spv::Op::OpTypeUntypedPointerKHR:
271
0
      SaveName(result_id, std::string("_ptr_") +
272
0
                              NameForEnumOperand(SPV_OPERAND_TYPE_STORAGE_CLASS,
273
0
                                                 inst.words[2]));
274
0
      break;
275
0
    case spv::Op::OpTypePipe:
276
0
      SaveName(result_id,
277
0
               std::string("Pipe") +
278
0
                   NameForEnumOperand(SPV_OPERAND_TYPE_ACCESS_QUALIFIER,
279
0
                                      inst.words[2]));
280
0
      break;
281
0
    case spv::Op::OpTypeEvent:
282
0
      SaveName(result_id, "Event");
283
0
      break;
284
0
    case spv::Op::OpTypeDeviceEvent:
285
0
      SaveName(result_id, "DeviceEvent");
286
0
      break;
287
0
    case spv::Op::OpTypeReserveId:
288
0
      SaveName(result_id, "ReserveId");
289
0
      break;
290
0
    case spv::Op::OpTypeQueue:
291
0
      SaveName(result_id, "Queue");
292
0
      break;
293
0
    case spv::Op::OpTypeOpaque:
294
0
      SaveName(result_id, std::string("Opaque_") +
295
0
                              Sanitize(spvDecodeLiteralStringOperand(inst, 1)));
296
0
      break;
297
0
    case spv::Op::OpTypePipeStorage:
298
0
      SaveName(result_id, "PipeStorage");
299
0
      break;
300
0
    case spv::Op::OpTypeNamedBarrier:
301
0
      SaveName(result_id, "NamedBarrier");
302
0
      break;
303
200
    case spv::Op::OpTypeStruct:
304
      // Structs are mapped rather simplisitically. Just indicate that they
305
      // are a struct and then give the raw Id number.
306
200
      SaveName(result_id, std::string("_struct_") + to_string(result_id));
307
200
      break;
308
14
    case spv::Op::OpConstantTrue:
309
14
      SaveName(result_id, "true");
310
14
      break;
311
13
    case spv::Op::OpConstantFalse:
312
13
      SaveName(result_id, "false");
313
13
      break;
314
1.10k
    case spv::Op::OpConstant: {
315
1.10k
      std::ostringstream value;
316
1.10k
      EmitNumericLiteral(&value, inst, inst.operands[2]);
317
1.10k
      auto value_str = value.str();
318
      // Use 'n' to signify negative. Other invalid characters will be mapped
319
      // to underscore.
320
1.10k
      for (auto& c : value_str)
321
2.28k
        if (c == '-') c = 'n';
322
1.10k
      SaveName(result_id, NameForId(inst.type_id) + "_" + value_str);
323
1.10k
    } break;
324
24.9k
    default:
325
      // If this instruction otherwise defines an Id, then save a mapping for
326
      // it.  This is needed to ensure uniqueness in there is an OpName with
327
      // string something like "1" that might collide with this result_id.
328
      // We should only do this if a name hasn't already been registered by some
329
      // previous forward reference.
330
24.9k
      if (result_id && name_for_id_.find(result_id) == name_for_id_.end())
331
15.7k
        SaveName(result_id, to_string(result_id));
332
24.9k
      break;
333
32.3k
  }
334
32.3k
  return SPV_SUCCESS;
335
32.3k
}
336
337
std::string FriendlyNameMapper::NameForEnumOperand(spv_operand_type_t type,
338
1.21k
                                                   uint32_t word) {
339
1.21k
  const spvtools::OperandDesc* desc = nullptr;
340
1.21k
  if (SPV_SUCCESS == spvtools::LookupOperand(type, word, &desc)) {
341
1.21k
    return desc->name().data();
342
1.21k
  } else {
343
    // Invalid input.  Just give something.
344
0
    return std::string("StorageClass") + to_string(word);
345
0
  }
346
1.21k
}
347
348
}  // namespace spvtools