Coverage Report

Created: 2025-12-03 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc/third_party/spirv-tools/source/opt/pass.h
Line
Count
Source
1
// Copyright (c) 2016 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_PASS_H_
16
#define SOURCE_OPT_PASS_H_
17
18
#include <algorithm>
19
#include <map>
20
#include <unordered_map>
21
#include <unordered_set>
22
#include <utility>
23
24
#include "source/opt/basic_block.h"
25
#include "source/opt/def_use_manager.h"
26
#include "source/opt/ir_context.h"
27
#include "source/opt/module.h"
28
#include "source/util/status.h"
29
#include "spirv-tools/libspirv.hpp"
30
#include "types.h"
31
32
// Avoid unused variable warning/error on Linux
33
#ifndef NDEBUG
34
#define USE_ASSERT(x) assert(x)
35
#else
36
0
#define USE_ASSERT(x) ((void)(x))
37
#endif
38
39
namespace spvtools {
40
namespace opt {
41
42
// Abstract class of a pass. All passes should implement this abstract class
43
// and all analysis and transformation is done via the Process() method.
44
class Pass {
45
 public:
46
  // The status of processing a module using a pass.
47
  //
48
  // The numbers for the cases are assigned to make sure that Failure & anything
49
  // is Failure, SuccessWithChange & any success is SuccessWithChange.
50
  using Status = utils::Status;
51
52
  using ProcessFunction = std::function<bool(Function*)>;
53
54
  // Destructs the pass.
55
46.1k
  virtual ~Pass() = default;
56
57
  // Returns a descriptive name for this pass.
58
  //
59
  // NOTE: When deriving a new pass class, make sure you make the name
60
  // compatible with the corresponding spirv-opt command-line flag. For example,
61
  // if you add the flag --my-pass to spirv-opt, make this function return
62
  // "my-pass" (no leading hyphens).
63
  virtual const char* name() const = 0;
64
65
  // Sets the message consumer to the given |consumer|. |consumer| which will be
66
  // invoked every time there is a message to be communicated to the outside.
67
46.1k
  void SetMessageConsumer(MessageConsumer c) { consumer_ = std::move(c); }
68
69
  // Returns the reference to the message consumer for this pass.
70
0
  const MessageConsumer& consumer() const { return consumer_; }
71
72
  // Returns the def-use manager used for this pass. TODO(dnovillo): This should
73
  // be handled by the pass manager.
74
4.26M
  analysis::DefUseManager* get_def_use_mgr() const {
75
4.26M
    return context()->get_def_use_mgr();
76
4.26M
  }
77
78
1.23M
  analysis::DecorationManager* get_decoration_mgr() const {
79
1.23M
    return context()->get_decoration_mgr();
80
1.23M
  }
81
82
0
  FeatureManager* get_feature_mgr() const {
83
0
    return context()->get_feature_mgr();
84
0
  }
85
86
  // Returns a pointer to the current module for this pass.
87
125k
  Module* get_module() const { return context_->module(); }
88
89
  // Sets the pointer to the current context for this pass.
90
0
  void SetContextForTesting(IRContext* ctx) { context_ = ctx; }
91
92
  // Returns a pointer to the current context for this pass.
93
12.6M
  IRContext* context() const { return context_; }
94
95
  // Returns a pointer to the CFG for current module.
96
382k
  CFG* cfg() const { return context()->cfg(); }
97
98
  // Run the pass on the given |module|. Returns Status::Failure if errors occur
99
  // when processing. Returns the corresponding Status::Success if processing is
100
  // successful to indicate whether changes are made to the module.  If there
101
  // were any changes it will also invalidate the analyses in the IRContext
102
  // that are not preserved.
103
  //
104
  // It is an error if |Run| is called twice with the same instance of the pass.
105
  // If this happens the return value will be |Failure|.
106
  Status Run(IRContext* ctx);
107
108
  // Returns the set of analyses that the pass is guaranteed to preserve.
109
1.62k
  virtual IRContext::Analysis GetPreservedAnalyses() {
110
1.62k
    return IRContext::kAnalysisNone;
111
1.62k
  }
112
113
  // Return type id for |ptrInst|'s pointee
114
  uint32_t GetPointeeTypeId(const Instruction* ptrInst) const;
115
116
  // Return base type of |ty_id| type
117
  Instruction* GetBaseType(uint32_t ty_id);
118
119
  // Return true if |inst| returns scalar, vector or matrix type with base
120
  // float and |width|
121
  bool IsFloat(uint32_t ty_id, uint32_t width);
122
123
  // Return the id of OpConstantNull of type |type_id|. Create if necessary.
124
  uint32_t GetNullId(uint32_t type_id);
125
126
 protected:
127
  // Constructs a new pass.
128
  //
129
  // The constructed instance will have an empty message consumer, which just
130
  // ignores all messages from the library. Use SetMessageConsumer() to supply
131
  // one if messages are of concern.
132
  Pass();
133
134
  // Processes the given |module|. Returns Status::Failure if errors occur when
135
  // processing. Returns the corresponding Status::Success if processing is
136
  // successful to indicate whether changes are made to the module.
137
  virtual Status Process() = 0;
138
139
  // Return the next available SSA id and increment it.
140
  // TODO(1841): Handle id overflow.
141
7.59k
  uint32_t TakeNextId() { return context_->TakeNextId(); }
142
143
  // Returns the id whose value is the same as |object_to_copy| except its type
144
  // is |new_type_id|.  Any instructions needed to generate this value will be
145
  // inserted before |insertion_position|. Returns 0 if a copy could not be
146
  // done.
147
  uint32_t GenerateCopy(Instruction* object_to_copy, uint32_t new_type_id,
148
                        Instruction* insertion_position);
149
150
 private:
151
  MessageConsumer consumer_;  // Message consumer.
152
153
  // The context that this pass belongs to.
154
  IRContext* context_;
155
156
  // An instance of a pass can only be run once because it is too hard to
157
  // enforce proper resetting of internal state for each instance.  This member
158
  // is used to check that we do not run the same instance twice.
159
  bool already_run_;
160
};
161
162
4.12k
inline Pass::Status CombineStatus(Pass::Status a, Pass::Status b) {
163
4.12k
  return std::min(a, b);
164
4.12k
}
165
166
}  // namespace opt
167
}  // namespace spvtools
168
169
#endif  // SOURCE_OPT_PASS_H_