Coverage Report

Created: 2025-11-11 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc/third_party/spirv-tools/source/opt/fold.h
Line
Count
Source
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
#ifndef SOURCE_OPT_FOLD_H_
16
#define SOURCE_OPT_FOLD_H_
17
18
#include <cstdint>
19
#include <vector>
20
21
#include "source/opt/const_folding_rules.h"
22
#include "source/opt/constants.h"
23
#include "source/opt/def_use_manager.h"
24
#include "source/opt/folding_rules.h"
25
26
namespace spvtools {
27
namespace opt {
28
29
class InstructionFolder {
30
 public:
31
  explicit InstructionFolder(IRContext* context)
32
990
      : context_(context),
33
990
        const_folding_rules_(new ConstantFoldingRules(context)),
34
990
        folding_rules_(new FoldingRules(context)) {
35
990
    folding_rules_->AddFoldingRules();
36
990
    const_folding_rules_->AddFoldingRules();
37
990
  }
38
39
  explicit InstructionFolder(
40
      IRContext* context, std::unique_ptr<FoldingRules>&& folding_rules,
41
      std::unique_ptr<ConstantFoldingRules>&& constant_folding_rules)
42
90
      : context_(context),
43
90
        const_folding_rules_(std::move(constant_folding_rules)),
44
90
        folding_rules_(std::move(folding_rules)) {
45
90
    folding_rules_->AddFoldingRules();
46
90
    const_folding_rules_->AddFoldingRules();
47
90
  }
48
49
  // Returns the result of folding a scalar instruction with the given |opcode|
50
  // and |operands|. Each entry in |operands| is a pointer to an
51
  // analysis::Constant instance, which should've been created with the constant
52
  // manager (See IRContext::get_constant_mgr).
53
  //
54
  // It is an error to call this function with an opcode that does not pass the
55
  // IsFoldableOpcode test. If any error occurs during folding, the folder will
56
  // fail with a call to assert.
57
  uint32_t FoldScalars(
58
      spv::Op opcode,
59
      const std::vector<const analysis::Constant*>& operands) const;
60
61
  // Returns the result of performing an operation with the given |opcode| over
62
  // constant vectors with |num_dims| dimensions.  Each entry in |operands| is a
63
  // pointer to an analysis::Constant instance, which should've been created
64
  // with the constant manager (See IRContext::get_constant_mgr).
65
  //
66
  // This function iterates through the given vector type constant operands and
67
  // calculates the result for each element of the result vector to return.
68
  // Vectors with longer than 32-bit scalar components are not accepted in this
69
  // function.
70
  //
71
  // It is an error to call this function with an opcode that does not pass the
72
  // IsFoldableOpcode test. If any error occurs during folding, the folder will
73
  // fail with a call to assert.
74
  std::vector<uint32_t> FoldVectors(
75
      spv::Op opcode, uint32_t num_dims,
76
      const std::vector<const analysis::Constant*>& operands) const;
77
78
  // Returns true if |opcode| represents an operation handled by FoldScalars or
79
  // FoldVectors.
80
  bool IsFoldableOpcode(spv::Op opcode) const;
81
82
  // Returns true if |cst| is supported by FoldScalars and FoldVectors.
83
  bool IsFoldableConstant(const analysis::Constant* cst) const;
84
85
  // Returns true if |FoldInstructionToConstant| could fold an instruction whose
86
  // result type is |type_inst|.
87
  bool IsFoldableType(Instruction* type_inst) const;
88
89
  // Returns true if |FoldInstructionToConstant| could fold an instruction whose
90
  // result type is |type_inst|.
91
  bool IsFoldableScalarType(Instruction* type_inst) const;
92
93
  // Returns true if |FoldInstructionToConstant| could fold an instruction whose
94
  // result type is |type_inst|.
95
  bool IsFoldableVectorType(Instruction* type_inst) const;
96
97
  // Tries to fold |inst| to a single constant, when the input ids to |inst|
98
  // have been substituted using |id_map|.  Returns a pointer to the OpConstant*
99
  // instruction if successful.  If necessary, a new constant instruction is
100
  // created and placed in the global values section.
101
  //
102
  // |id_map| is a function that takes one result id and returns another.  It
103
  // can be used for things like CCP where it is known that some ids contain a
104
  // constant, but the instruction itself has not been updated yet.  This can
105
  // map those ids to the appropriate constants.
106
  Instruction* FoldInstructionToConstant(
107
      Instruction* inst, std::function<uint32_t(uint32_t)> id_map) const;
108
  // Returns true if |inst| can be folded into a simpler instruction.
109
  // If |inst| can be simplified, |inst| is overwritten with the simplified
110
  // instruction reusing the same result id.
111
  //
112
  // If |inst| is simplified, it is possible that the resulting code in invalid
113
  // because the instruction is in a bad location.  Callers of this function
114
  // have to handle the following cases:
115
  //
116
  // 1) An OpPhi becomes and OpCopyObject - If there are OpPhi instruction after
117
  //    |inst| in a basic block then this is invalid.  The caller must fix this
118
  //    up.
119
  bool FoldInstruction(Instruction* inst) const;
120
121
  // Return true if this opcode has a const folding rule associtated with it.
122
76.8k
  bool HasConstFoldingRule(const Instruction* inst) const {
123
76.8k
    return GetConstantFoldingRules().HasFoldingRule(inst);
124
76.8k
  }
125
126
 private:
127
  // Returns a reference to the ConstnatFoldingRules instance.
128
500k
  const ConstantFoldingRules& GetConstantFoldingRules() const {
129
500k
    return *const_folding_rules_;
130
500k
  }
131
132
  // Returns a reference to the FoldingRules instance.
133
262k
  const FoldingRules& GetFoldingRules() const { return *folding_rules_; }
134
135
  // Returns the single-word result from performing the given unary operation on
136
  // the operand value which is passed in as a 32-bit word.
137
  uint32_t UnaryOperate(spv::Op opcode, uint32_t operand) const;
138
139
  // Returns the single-word result from performing the given binary operation
140
  // on the operand values which are passed in as two 32-bit word.
141
  uint32_t BinaryOperate(spv::Op opcode, uint32_t a, uint32_t b) const;
142
143
  // Returns the single-word result from performing the given ternary operation
144
  // on the operand values which are passed in as three 32-bit word.
145
  uint32_t TernaryOperate(spv::Op opcode, uint32_t a, uint32_t b,
146
                          uint32_t c) const;
147
148
  // Returns the single-word result from performing the given operation on the
149
  // operand words. This only works with 32-bit operations and uses boolean
150
  // convention that 0u is false, and anything else is boolean true.
151
  // TODO(qining): Support operands other than 32-bit wide.
152
  uint32_t OperateWords(spv::Op opcode,
153
                        const std::vector<uint32_t>& operand_words) const;
154
155
  bool FoldInstructionInternal(Instruction* inst) const;
156
157
  // Returns true if |inst| is a binary operation that takes two integers as
158
  // parameters and folds to a constant that can be represented as an unsigned
159
  // 32-bit value when the ids have been replaced by |id_map|.  If |inst| can be
160
  // folded, the resulting value is returned in |*result|.  Valid result types
161
  // for the instruction are any integer (signed or unsigned) with 32-bits or
162
  // less, or a boolean value.
163
  bool FoldBinaryIntegerOpToConstant(
164
      Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
165
      uint32_t* result) const;
166
167
  // Returns true if |inst| is a binary operation on two boolean values, and
168
  // folds
169
  // to a constant boolean value when the ids have been replaced using |id_map|.
170
  // If |inst| can be folded, the result value is returned in |*result|.
171
  bool FoldBinaryBooleanOpToConstant(
172
      Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
173
      uint32_t* result) const;
174
175
  // Returns true if |inst| can be folded to an constant when the ids have been
176
  // substituted using id_map.  If it can, the value is returned in |result|. If
177
  // not, |result| is unchanged.  It is assumed that not all operands are
178
  // constant.  Those cases are handled by |FoldScalar|.
179
  bool FoldIntegerOpToConstant(Instruction* inst,
180
                               const std::function<uint32_t(uint32_t)>& id_map,
181
                               uint32_t* result) const;
182
183
  IRContext* context_;
184
185
  // Folding rules used by |FoldInstructionToConstant| and |FoldInstruction|.
186
  std::unique_ptr<ConstantFoldingRules> const_folding_rules_;
187
188
  // Folding rules used by |FoldInstruction|.
189
  std::unique_ptr<FoldingRules> folding_rules_;
190
};
191
192
}  // namespace opt
193
}  // namespace spvtools
194
195
#endif  // SOURCE_OPT_FOLD_H_