Coverage Report

Created: 2025-07-18 06:38

/src/shaderc/third_party/spirv-tools/source/opt/fold.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2017 Google 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/opt/fold.h"
16
17
#include <cassert>
18
#include <cstdint>
19
#include <vector>
20
21
#include "source/opt/const_folding_rules.h"
22
#include "source/opt/def_use_manager.h"
23
#include "source/opt/folding_rules.h"
24
#include "source/opt/ir_context.h"
25
26
namespace spvtools {
27
namespace opt {
28
namespace {
29
30
#ifndef INT32_MIN
31
#define INT32_MIN (-2147483648)
32
#endif
33
34
#ifndef INT32_MAX
35
#define INT32_MAX 2147483647
36
#endif
37
38
#ifndef UINT32_MAX
39
#define UINT32_MAX 0xffffffff /* 4294967295U */
40
#endif
41
42
}  // namespace
43
44
uint32_t InstructionFolder::UnaryOperate(spv::Op opcode,
45
4
                                         uint32_t operand) const {
46
4
  switch (opcode) {
47
    // Arthimetics
48
0
    case spv::Op::OpSNegate: {
49
0
      int32_t s_operand = static_cast<int32_t>(operand);
50
0
      if (s_operand == std::numeric_limits<int32_t>::min()) {
51
0
        return s_operand;
52
0
      }
53
0
      return static_cast<uint32_t>(-s_operand);
54
0
    }
55
4
    case spv::Op::OpNot:
56
4
      return ~operand;
57
0
    case spv::Op::OpLogicalNot:
58
0
      return !static_cast<bool>(operand);
59
0
    case spv::Op::OpUConvert:
60
0
      return operand;
61
0
    case spv::Op::OpSConvert:
62
0
      return operand;
63
0
    default:
64
0
      assert(false &&
65
0
             "Unsupported unary operation for OpSpecConstantOp instruction");
66
0
      return 0u;
67
4
  }
68
4
}
69
70
uint32_t InstructionFolder::BinaryOperate(spv::Op opcode, uint32_t a,
71
918
                                          uint32_t b) const {
72
918
  switch (opcode) {
73
    // Shifting
74
0
    case spv::Op::OpShiftRightLogical:
75
0
      if (b >= 32) {
76
        // This is undefined behaviour when |b| > 32.  Choose 0 for consistency.
77
        // When |b| == 32, doing the shift in C++ in undefined, but the result
78
        // will be 0, so just return that value.
79
0
        return 0;
80
0
      }
81
0
      return a >> b;
82
0
    case spv::Op::OpShiftRightArithmetic:
83
0
      if (b > 32) {
84
        // This is undefined behaviour.  Choose 0 for consistency.
85
0
        return 0;
86
0
      }
87
0
      if (b == 32) {
88
        // Doing the shift in C++ is undefined, but the result is defined in the
89
        // spir-v spec.  Find that value another way.
90
0
        if (static_cast<int32_t>(a) >= 0) {
91
0
          return 0;
92
0
        } else {
93
0
          return static_cast<uint32_t>(-1);
94
0
        }
95
0
      }
96
0
      return (static_cast<int32_t>(a)) >> b;
97
0
    case spv::Op::OpShiftLeftLogical:
98
0
      if (b >= 32) {
99
        // This is undefined behaviour when |b| > 32.  Choose 0 for consistency.
100
        // When |b| == 32, doing the shift in C++ in undefined, but the result
101
        // will be 0, so just return that value.
102
0
        return 0;
103
0
      }
104
0
      return a << b;
105
106
    // Bitwise operations
107
0
    case spv::Op::OpBitwiseOr:
108
0
      return a | b;
109
0
    case spv::Op::OpBitwiseAnd:
110
0
      return a & b;
111
0
    case spv::Op::OpBitwiseXor:
112
0
      return a ^ b;
113
114
    // Logical
115
0
    case spv::Op::OpLogicalEqual:
116
0
      return (static_cast<bool>(a)) == (static_cast<bool>(b));
117
0
    case spv::Op::OpLogicalNotEqual:
118
0
      return (static_cast<bool>(a)) != (static_cast<bool>(b));
119
0
    case spv::Op::OpLogicalOr:
120
0
      return (static_cast<bool>(a)) || (static_cast<bool>(b));
121
0
    case spv::Op::OpLogicalAnd:
122
0
      return (static_cast<bool>(a)) && (static_cast<bool>(b));
123
124
    // Comparison
125
0
    case spv::Op::OpIEqual:
126
0
      return a == b;
127
0
    case spv::Op::OpINotEqual:
128
0
      return a != b;
129
0
    case spv::Op::OpULessThan:
130
0
      return a < b;
131
438
    case spv::Op::OpSLessThan:
132
438
      return (static_cast<int32_t>(a)) < (static_cast<int32_t>(b));
133
0
    case spv::Op::OpUGreaterThan:
134
0
      return a > b;
135
0
    case spv::Op::OpSGreaterThan:
136
0
      return (static_cast<int32_t>(a)) > (static_cast<int32_t>(b));
137
0
    case spv::Op::OpULessThanEqual:
138
0
      return a <= b;
139
480
    case spv::Op::OpSLessThanEqual:
140
480
      return (static_cast<int32_t>(a)) <= (static_cast<int32_t>(b));
141
0
    case spv::Op::OpUGreaterThanEqual:
142
0
      return a >= b;
143
0
    case spv::Op::OpSGreaterThanEqual:
144
0
      return (static_cast<int32_t>(a)) >= (static_cast<int32_t>(b));
145
0
    default:
146
0
      assert(false &&
147
0
             "Unsupported binary operation for OpSpecConstantOp instruction");
148
0
      return 0u;
149
918
  }
150
918
}
151
152
uint32_t InstructionFolder::TernaryOperate(spv::Op opcode, uint32_t a,
153
12
                                           uint32_t b, uint32_t c) const {
154
12
  switch (opcode) {
155
12
    case spv::Op::OpSelect:
156
12
      return (static_cast<bool>(a)) ? b : c;
157
0
    default:
158
0
      assert(false &&
159
0
             "Unsupported ternary operation for OpSpecConstantOp instruction");
160
0
      return 0u;
161
12
  }
162
12
}
163
164
uint32_t InstructionFolder::OperateWords(
165
934
    spv::Op opcode, const std::vector<uint32_t>& operand_words) const {
166
934
  switch (operand_words.size()) {
167
4
    case 1:
168
4
      return UnaryOperate(opcode, operand_words.front());
169
918
    case 2:
170
918
      return BinaryOperate(opcode, operand_words.front(), operand_words.back());
171
12
    case 3:
172
12
      return TernaryOperate(opcode, operand_words[0], operand_words[1],
173
12
                            operand_words[2]);
174
0
    default:
175
0
      assert(false && "Invalid number of operands");
176
0
      return 0;
177
934
  }
178
934
}
179
180
198k
bool InstructionFolder::FoldInstructionInternal(Instruction* inst) const {
181
198k
  auto identity_map = [](uint32_t id) { return id; };
182
198k
  Instruction* folded_inst = FoldInstructionToConstant(inst, identity_map);
183
198k
  if (folded_inst != nullptr) {
184
232
    inst->SetOpcode(spv::Op::OpCopyObject);
185
232
    inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {folded_inst->result_id()}}});
186
232
    return true;
187
232
  }
188
189
197k
  analysis::ConstantManager* const_manager = context_->get_constant_mgr();
190
197k
  std::vector<const analysis::Constant*> constants =
191
197k
      const_manager->GetOperandConstants(inst);
192
193
197k
  for (const FoldingRule& rule :
194
230k
       GetFoldingRules().GetRulesForInstruction(inst)) {
195
230k
    if (rule(context_, inst, constants)) {
196
5.52k
      return true;
197
5.52k
    }
198
230k
  }
199
192k
  return false;
200
197k
}
201
202
// Returns the result of performing an operation on scalar constant operands.
203
// This function extracts the operand values as 32 bit words and returns the
204
// result in 32 bit word. Scalar constants with longer than 32-bit width are
205
// not accepted in this function.
206
uint32_t InstructionFolder::FoldScalars(
207
    spv::Op opcode,
208
922
    const std::vector<const analysis::Constant*>& operands) const {
209
922
  assert(IsFoldableOpcode(opcode) &&
210
922
         "Unhandled instruction opcode in FoldScalars");
211
922
  std::vector<uint32_t> operand_values_in_raw_words;
212
1.84k
  for (const auto& operand : operands) {
213
1.84k
    if (const analysis::ScalarConstant* scalar = operand->AsScalarConstant()) {
214
1.84k
      const auto& scalar_words = scalar->words();
215
1.84k
      assert(scalar_words.size() == 1 &&
216
1.84k
             "Scalar constants with longer than 32-bit width are not allowed "
217
1.84k
             "in FoldScalars()");
218
1.84k
      operand_values_in_raw_words.push_back(scalar_words.front());
219
1.84k
    } else if (operand->AsNullConstant()) {
220
0
      operand_values_in_raw_words.push_back(0u);
221
0
    } else {
222
0
      assert(false &&
223
0
             "FoldScalars() only accepts ScalarConst or NullConst type of "
224
0
             "constant");
225
0
    }
226
1.84k
  }
227
922
  return OperateWords(opcode, operand_values_in_raw_words);
228
922
}
229
230
bool InstructionFolder::FoldBinaryIntegerOpToConstant(
231
    Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
232
12.3k
    uint32_t* result) const {
233
12.3k
  spv::Op opcode = inst->opcode();
234
12.3k
  analysis::ConstantManager* const_manger = context_->get_constant_mgr();
235
236
12.3k
  uint32_t ids[2];
237
12.3k
  const analysis::IntConstant* constants[2];
238
37.0k
  for (uint32_t i = 0; i < 2; i++) {
239
24.7k
    const Operand* operand = &inst->GetInOperand(i);
240
24.7k
    if (operand->type != SPV_OPERAND_TYPE_ID) {
241
0
      return false;
242
0
    }
243
24.7k
    ids[i] = id_map(operand->words[0]);
244
24.7k
    const analysis::Constant* constant =
245
24.7k
        const_manger->FindDeclaredConstant(ids[i]);
246
24.7k
    constants[i] = (constant != nullptr ? constant->AsIntConstant() : nullptr);
247
24.7k
  }
248
249
12.3k
  switch (opcode) {
250
    // Arthimetics
251
0
    case spv::Op::OpIMul:
252
0
      for (uint32_t i = 0; i < 2; i++) {
253
0
        if (constants[i] != nullptr && constants[i]->IsZero()) {
254
0
          *result = 0;
255
0
          return true;
256
0
        }
257
0
      }
258
0
      break;
259
0
    case spv::Op::OpUDiv:
260
0
    case spv::Op::OpSDiv:
261
0
    case spv::Op::OpSRem:
262
126
    case spv::Op::OpSMod:
263
158
    case spv::Op::OpUMod:
264
      // This changes undefined behaviour (ie divide by 0) into a 0.
265
474
      for (uint32_t i = 0; i < 2; i++) {
266
316
        if (constants[i] != nullptr && constants[i]->IsZero()) {
267
0
          *result = 0;
268
0
          return true;
269
0
        }
270
316
      }
271
158
      break;
272
273
    // Shifting
274
158
    case spv::Op::OpShiftRightLogical:
275
114
    case spv::Op::OpShiftLeftLogical:
276
114
      if (constants[1] != nullptr) {
277
        // When shifting by a value larger than the size of the result, the
278
        // result is undefined.  We are setting the undefined behaviour to a
279
        // result of 0.  If the shift amount is the same as the size of the
280
        // result, then the result is defined, and it 0.
281
6
        uint32_t shift_amount = constants[1]->GetU32BitValue();
282
6
        if (shift_amount >= 32) {
283
0
          *result = 0;
284
0
          return true;
285
0
        }
286
6
      }
287
114
      break;
288
289
    // Bitwise operations
290
118
    case spv::Op::OpBitwiseOr:
291
354
      for (uint32_t i = 0; i < 2; i++) {
292
236
        if (constants[i] != nullptr) {
293
          // TODO: Change the mask against a value based on the bit width of the
294
          // instruction result type.  This way we can handle say 16-bit values
295
          // as well.
296
4
          uint32_t mask = constants[i]->GetU32BitValue();
297
4
          if (mask == 0xFFFFFFFF) {
298
0
            *result = 0xFFFFFFFF;
299
0
            return true;
300
0
          }
301
4
        }
302
236
      }
303
118
      break;
304
118
    case spv::Op::OpBitwiseAnd:
305
342
      for (uint32_t i = 0; i < 2; i++) {
306
228
        if (constants[i] != nullptr) {
307
0
          if (constants[i]->IsZero()) {
308
0
            *result = 0;
309
0
            return true;
310
0
          }
311
0
        }
312
228
      }
313
114
      break;
314
315
    // Comparison
316
114
    case spv::Op::OpULessThan:
317
0
      if (constants[0] != nullptr &&
318
0
          constants[0]->GetU32BitValue() == UINT32_MAX) {
319
0
        *result = false;
320
0
        return true;
321
0
      }
322
0
      if (constants[1] != nullptr && constants[1]->GetU32BitValue() == 0) {
323
0
        *result = false;
324
0
        return true;
325
0
      }
326
0
      break;
327
1.71k
    case spv::Op::OpSLessThan:
328
1.71k
      if (constants[0] != nullptr &&
329
1.71k
          constants[0]->GetS32BitValue() == INT32_MAX) {
330
0
        *result = false;
331
0
        return true;
332
0
      }
333
1.71k
      if (constants[1] != nullptr &&
334
1.71k
          constants[1]->GetS32BitValue() == INT32_MIN) {
335
0
        *result = false;
336
0
        return true;
337
0
      }
338
1.71k
      break;
339
1.71k
    case spv::Op::OpUGreaterThan:
340
112
      if (constants[0] != nullptr && constants[0]->IsZero()) {
341
0
        *result = false;
342
0
        return true;
343
0
      }
344
112
      if (constants[1] != nullptr &&
345
112
          constants[1]->GetU32BitValue() == UINT32_MAX) {
346
0
        *result = false;
347
0
        return true;
348
0
      }
349
112
      break;
350
1.49k
    case spv::Op::OpSGreaterThan:
351
1.49k
      if (constants[0] != nullptr &&
352
1.49k
          constants[0]->GetS32BitValue() == INT32_MIN) {
353
0
        *result = false;
354
0
        return true;
355
0
      }
356
1.49k
      if (constants[1] != nullptr &&
357
1.49k
          constants[1]->GetS32BitValue() == INT32_MAX) {
358
0
        *result = false;
359
0
        return true;
360
0
      }
361
1.49k
      break;
362
1.49k
    case spv::Op::OpULessThanEqual:
363
0
      if (constants[0] != nullptr && constants[0]->IsZero()) {
364
0
        *result = true;
365
0
        return true;
366
0
      }
367
0
      if (constants[1] != nullptr &&
368
0
          constants[1]->GetU32BitValue() == UINT32_MAX) {
369
0
        *result = true;
370
0
        return true;
371
0
      }
372
0
      break;
373
1.93k
    case spv::Op::OpSLessThanEqual:
374
1.93k
      if (constants[0] != nullptr &&
375
1.93k
          constants[0]->GetS32BitValue() == INT32_MIN) {
376
0
        *result = true;
377
0
        return true;
378
0
      }
379
1.93k
      if (constants[1] != nullptr &&
380
1.93k
          constants[1]->GetS32BitValue() == INT32_MAX) {
381
0
        *result = true;
382
0
        return true;
383
0
      }
384
1.93k
      break;
385
1.93k
    case spv::Op::OpUGreaterThanEqual:
386
0
      if (constants[0] != nullptr &&
387
0
          constants[0]->GetU32BitValue() == UINT32_MAX) {
388
0
        *result = true;
389
0
        return true;
390
0
      }
391
0
      if (constants[1] != nullptr && constants[1]->GetU32BitValue() == 0) {
392
0
        *result = true;
393
0
        return true;
394
0
      }
395
0
      break;
396
0
    case spv::Op::OpSGreaterThanEqual:
397
0
      if (constants[0] != nullptr &&
398
0
          constants[0]->GetS32BitValue() == INT32_MAX) {
399
0
        *result = true;
400
0
        return true;
401
0
      }
402
0
      if (constants[1] != nullptr &&
403
0
          constants[1]->GetS32BitValue() == INT32_MIN) {
404
0
        *result = true;
405
0
        return true;
406
0
      }
407
0
      break;
408
6.60k
    default:
409
6.60k
      break;
410
12.3k
  }
411
12.3k
  return false;
412
12.3k
}
413
414
bool InstructionFolder::FoldBinaryBooleanOpToConstant(
415
    Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
416
12.3k
    uint32_t* result) const {
417
12.3k
  spv::Op opcode = inst->opcode();
418
12.3k
  analysis::ConstantManager* const_manger = context_->get_constant_mgr();
419
420
12.3k
  uint32_t ids[2];
421
12.3k
  const analysis::BoolConstant* constants[2];
422
37.0k
  for (uint32_t i = 0; i < 2; i++) {
423
24.7k
    const Operand* operand = &inst->GetInOperand(i);
424
24.7k
    if (operand->type != SPV_OPERAND_TYPE_ID) {
425
0
      return false;
426
0
    }
427
24.7k
    ids[i] = id_map(operand->words[0]);
428
24.7k
    const analysis::Constant* constant =
429
24.7k
        const_manger->FindDeclaredConstant(ids[i]);
430
24.7k
    constants[i] = (constant != nullptr ? constant->AsBoolConstant() : nullptr);
431
24.7k
  }
432
433
12.3k
  switch (opcode) {
434
    // Logical
435
474
    case spv::Op::OpLogicalOr:
436
1.42k
      for (uint32_t i = 0; i < 2; i++) {
437
948
        if (constants[i] != nullptr) {
438
0
          if (constants[i]->value()) {
439
0
            *result = true;
440
0
            return true;
441
0
          }
442
0
        }
443
948
      }
444
474
      break;
445
870
    case spv::Op::OpLogicalAnd:
446
2.61k
      for (uint32_t i = 0; i < 2; i++) {
447
1.74k
        if (constants[i] != nullptr) {
448
0
          if (!constants[i]->value()) {
449
0
            *result = false;
450
0
            return true;
451
0
          }
452
0
        }
453
1.74k
      }
454
870
      break;
455
456
11.0k
    default:
457
11.0k
      break;
458
12.3k
  }
459
12.3k
  return false;
460
12.3k
}
461
462
bool InstructionFolder::FoldIntegerOpToConstant(
463
    Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
464
12.8k
    uint32_t* result) const {
465
12.8k
  assert(IsFoldableOpcode(inst->opcode()) &&
466
12.8k
         "Unhandled instruction opcode in FoldScalars");
467
12.8k
  switch (inst->NumInOperands()) {
468
12.3k
    case 2:
469
12.3k
      return FoldBinaryIntegerOpToConstant(inst, id_map, result) ||
470
12.3k
             FoldBinaryBooleanOpToConstant(inst, id_map, result);
471
466
    default:
472
466
      return false;
473
12.8k
  }
474
12.8k
}
475
476
std::vector<uint32_t> InstructionFolder::FoldVectors(
477
    spv::Op opcode, uint32_t num_dims,
478
6
    const std::vector<const analysis::Constant*>& operands) const {
479
6
  assert(IsFoldableOpcode(opcode) &&
480
6
         "Unhandled instruction opcode in FoldVectors");
481
6
  std::vector<uint32_t> result;
482
18
  for (uint32_t d = 0; d < num_dims; d++) {
483
12
    std::vector<uint32_t> operand_values_for_one_dimension;
484
36
    for (const auto& operand : operands) {
485
36
      if (const analysis::VectorConstant* vector_operand =
486
36
              operand->AsVectorConstant()) {
487
        // Extract the raw value of the scalar component constants
488
        // in 32-bit words here. The reason of not using FoldScalars() here
489
        // is that we do not create temporary null constants as components
490
        // when the vector operand is a NullConstant because Constant creation
491
        // may need extra checks for the validity and that is not managed in
492
        // here.
493
36
        if (const analysis::ScalarConstant* scalar_component =
494
36
                vector_operand->GetComponents().at(d)->AsScalarConstant()) {
495
36
          const auto& scalar_words = scalar_component->words();
496
36
          assert(
497
36
              scalar_words.size() == 1 &&
498
36
              "Vector components with longer than 32-bit width are not allowed "
499
36
              "in FoldVectors()");
500
36
          operand_values_for_one_dimension.push_back(scalar_words.front());
501
36
        } else if (operand->AsNullConstant()) {
502
0
          operand_values_for_one_dimension.push_back(0u);
503
0
        } else {
504
0
          assert(false &&
505
0
                 "VectorConst should only has ScalarConst or NullConst as "
506
0
                 "components");
507
0
        }
508
36
      } else if (operand->AsNullConstant()) {
509
0
        operand_values_for_one_dimension.push_back(0u);
510
0
      } else {
511
0
        assert(false &&
512
0
               "FoldVectors() only accepts VectorConst or NullConst type of "
513
0
               "constant");
514
0
      }
515
36
    }
516
12
    result.push_back(OperateWords(opcode, operand_values_for_one_dimension));
517
12
  }
518
6
  return result;
519
6
}
520
521
768k
bool InstructionFolder::IsFoldableOpcode(spv::Op opcode) const {
522
  // NOTE: Extend to more opcodes as new cases are handled in the folder
523
  // functions.
524
768k
  switch (opcode) {
525
1.44k
    case spv::Op::OpBitwiseAnd:
526
2.25k
    case spv::Op::OpBitwiseOr:
527
2.81k
    case spv::Op::OpBitwiseXor:
528
18.7k
    case spv::Op::OpIAdd:
529
18.8k
    case spv::Op::OpIEqual:
530
19.3k
    case spv::Op::OpIMul:
531
20.2k
    case spv::Op::OpINotEqual:
532
21.1k
    case spv::Op::OpISub:
533
23.1k
    case spv::Op::OpLogicalAnd:
534
23.1k
    case spv::Op::OpLogicalEqual:
535
23.4k
    case spv::Op::OpLogicalNot:
536
23.7k
    case spv::Op::OpLogicalNotEqual:
537
24.8k
    case spv::Op::OpLogicalOr:
538
26.5k
    case spv::Op::OpNot:
539
26.5k
    case spv::Op::OpSDiv:
540
27.6k
    case spv::Op::OpSelect:
541
31.0k
    case spv::Op::OpSGreaterThan:
542
31.0k
    case spv::Op::OpSGreaterThanEqual:
543
31.8k
    case spv::Op::OpShiftLeftLogical:
544
32.6k
    case spv::Op::OpShiftRightArithmetic:
545
32.6k
    case spv::Op::OpShiftRightLogical:
546
37.7k
    case spv::Op::OpSLessThan:
547
43.6k
    case spv::Op::OpSLessThanEqual:
548
43.8k
    case spv::Op::OpSMod:
549
44.8k
    case spv::Op::OpSNegate:
550
44.8k
    case spv::Op::OpSRem:
551
45.4k
    case spv::Op::OpSConvert:
552
45.6k
    case spv::Op::OpUConvert:
553
46.1k
    case spv::Op::OpUDiv:
554
46.3k
    case spv::Op::OpUGreaterThan:
555
46.3k
    case spv::Op::OpUGreaterThanEqual:
556
46.3k
    case spv::Op::OpULessThan:
557
46.3k
    case spv::Op::OpULessThanEqual:
558
46.9k
    case spv::Op::OpUMod:
559
46.9k
      return true;
560
721k
    default:
561
721k
      return false;
562
768k
  }
563
768k
}
564
565
bool InstructionFolder::IsFoldableConstant(
566
0
    const analysis::Constant* cst) const {
567
  // Currently supported constants are 32-bit values or null constants.
568
0
  if (const analysis::ScalarConstant* scalar = cst->AsScalarConstant())
569
0
    return scalar->words().size() == 1;
570
0
  else
571
0
    return cst->AsNullConstant() != nullptr;
572
0
}
573
574
Instruction* InstructionFolder::FoldInstructionToConstant(
575
230k
    Instruction* inst, std::function<uint32_t(uint32_t)> id_map) const {
576
230k
  analysis::ConstantManager* const_mgr = context_->get_constant_mgr();
577
578
230k
  if (!inst->IsFoldableByFoldScalar() && !inst->IsFoldableByFoldVector() &&
579
230k
      !GetConstantFoldingRules().HasFoldingRule(inst)) {
580
120k
    return nullptr;
581
120k
  }
582
  // Collect the values of the constant parameters.
583
110k
  std::vector<const analysis::Constant*> constants;
584
110k
  bool missing_constants = false;
585
110k
  inst->ForEachInId([&constants, &missing_constants, const_mgr,
586
190k
                     &id_map](uint32_t* op_id) {
587
190k
    uint32_t id = id_map(*op_id);
588
190k
    const analysis::Constant* const_op = const_mgr->FindDeclaredConstant(id);
589
190k
    if (!const_op) {
590
157k
      constants.push_back(nullptr);
591
157k
      missing_constants = true;
592
157k
    } else {
593
33.4k
      constants.push_back(const_op);
594
33.4k
    }
595
190k
  });
596
597
110k
  const analysis::Constant* folded_const = nullptr;
598
110k
  for (auto rule : GetConstantFoldingRules().GetRulesForInstruction(inst)) {
599
105k
    folded_const = rule(context_, inst, constants);
600
105k
    if (folded_const != nullptr) {
601
3.89k
      Instruction* const_inst =
602
3.89k
          const_mgr->GetDefiningInstruction(folded_const, inst->type_id());
603
3.89k
      if (const_inst == nullptr) {
604
0
        return nullptr;
605
0
      }
606
3.89k
      assert(const_inst->type_id() == inst->type_id());
607
      // May be a new instruction that needs to be analysed.
608
3.89k
      context_->UpdateDefUse(const_inst);
609
3.89k
      return const_inst;
610
3.89k
    }
611
105k
  }
612
613
106k
  bool successful = false;
614
615
  // If all parameters are constant, fold the instruction to a constant.
616
106k
  if (inst->IsFoldableByFoldScalar()) {
617
13.7k
    uint32_t result_val = 0;
618
619
13.7k
    if (!missing_constants) {
620
922
      result_val = FoldScalars(inst->opcode(), constants);
621
922
      successful = true;
622
922
    }
623
624
13.7k
    if (!successful) {
625
12.8k
      successful = FoldIntegerOpToConstant(inst, id_map, &result_val);
626
12.8k
    }
627
628
13.7k
    if (successful) {
629
922
      const analysis::Constant* result_const =
630
922
          const_mgr->GetConstant(const_mgr->GetType(inst), {result_val});
631
922
      Instruction* folded_inst =
632
922
          const_mgr->GetDefiningInstruction(result_const, inst->type_id());
633
922
      return folded_inst;
634
922
    }
635
92.8k
  } else if (inst->IsFoldableByFoldVector()) {
636
1.46k
    std::vector<uint32_t> result_val;
637
638
1.46k
    if (!missing_constants) {
639
6
      if (Instruction* inst_type =
640
6
              context_->get_def_use_mgr()->GetDef(inst->type_id())) {
641
6
        result_val = FoldVectors(
642
6
            inst->opcode(), inst_type->GetSingleWordInOperand(1), constants);
643
6
        successful = true;
644
6
      }
645
6
    }
646
647
1.46k
    if (successful) {
648
6
      const analysis::Constant* result_const =
649
6
          const_mgr->GetNumericVectorConstantWithWords(
650
6
              const_mgr->GetType(inst)->AsVector(), result_val);
651
6
      Instruction* folded_inst =
652
6
          const_mgr->GetDefiningInstruction(result_const, inst->type_id());
653
6
      return folded_inst;
654
6
    }
655
1.46k
  }
656
657
105k
  return nullptr;
658
106k
}
659
660
0
bool InstructionFolder::IsFoldableType(Instruction* type_inst) const {
661
0
  return IsFoldableScalarType(type_inst) || IsFoldableVectorType(type_inst);
662
0
}
663
664
119k
bool InstructionFolder::IsFoldableScalarType(Instruction* type_inst) const {
665
  // Support 32-bit integers.
666
119k
  if (type_inst->opcode() == spv::Op::OpTypeInt) {
667
89.0k
    return type_inst->GetSingleWordInOperand(0) == 32;
668
89.0k
  }
669
  // Support booleans.
670
30.7k
  if (type_inst->opcode() == spv::Op::OpTypeBool) {
671
26.7k
    return true;
672
26.7k
  }
673
  // Nothing else yet.
674
3.98k
  return false;
675
30.7k
}
676
677
12.2k
bool InstructionFolder::IsFoldableVectorType(Instruction* type_inst) const {
678
  // Support vectors with foldable components
679
12.2k
  if (type_inst->opcode() == spv::Op::OpTypeVector) {
680
9.98k
    uint32_t component_type_id = type_inst->GetSingleWordInOperand(0);
681
9.98k
    Instruction* def_component_type =
682
9.98k
        context_->get_def_use_mgr()->GetDef(component_type_id);
683
9.98k
    return def_component_type != nullptr &&
684
9.98k
           IsFoldableScalarType(def_component_type);
685
9.98k
  }
686
  // Nothing else yet.
687
2.29k
  return false;
688
12.2k
}
689
690
193k
bool InstructionFolder::FoldInstruction(Instruction* inst) const {
691
193k
  bool modified = false;
692
193k
  Instruction* folded_inst(inst);
693
199k
  while (folded_inst->opcode() != spv::Op::OpCopyObject &&
694
199k
         FoldInstructionInternal(&*folded_inst)) {
695
5.75k
    modified = true;
696
5.75k
  }
697
193k
  return modified;
698
193k
}
699
700
}  // namespace opt
701
}  // namespace spvtools