clang 23.0.0git
CIRSimplify.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "PassDetail.h"
10#include "mlir/Dialect/Func/IR/FuncOps.h"
11#include "mlir/IR/Block.h"
12#include "mlir/IR/Operation.h"
13#include "mlir/IR/PatternMatch.h"
14#include "mlir/IR/Region.h"
15#include "mlir/Support/LogicalResult.h"
16#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
19#include "llvm/ADT/SmallVector.h"
20
21using namespace mlir;
22using namespace cir;
23
24namespace mlir {
25#define GEN_PASS_DEF_CIRSIMPLIFY
26#include "clang/CIR/Dialect/Passes.h.inc"
27} // namespace mlir
28
29//===----------------------------------------------------------------------===//
30// Rewrite patterns
31//===----------------------------------------------------------------------===//
32
33namespace {
34
35/// Simplify suitable ternary operations into select operations.
36///
37/// For now we only simplify those ternary operations whose true and false
38/// branches directly yield a value or a constant. That is, both of the true and
39/// the false branch must either contain a cir.yield operation as the only
40/// operation in the branch, or contain a cir.const operation followed by a
41/// cir.yield operation that yields the constant value.
42///
43/// For example, we will simplify the following ternary operation:
44///
45/// %0 = ...
46/// %1 = cir.ternary (%condition, true {
47/// %2 = cir.const ...
48/// cir.yield %2
49/// } false {
50/// cir.yield %0
51///
52/// into the following sequence of operations:
53///
54/// %1 = cir.const ...
55/// %0 = cir.select if %condition then %1 else %2
56struct SimplifyTernary final : public OpRewritePattern<TernaryOp> {
57 using OpRewritePattern<TernaryOp>::OpRewritePattern;
58
59 LogicalResult matchAndRewrite(TernaryOp op,
60 PatternRewriter &rewriter) const override {
61 if (op->getNumResults() != 1)
62 return mlir::failure();
63
64 if (!isSimpleTernaryBranch(op.getTrueRegion()) ||
65 !isSimpleTernaryBranch(op.getFalseRegion()))
66 return mlir::failure();
67
68 cir::YieldOp trueBranchYieldOp =
69 mlir::cast<cir::YieldOp>(op.getTrueRegion().front().getTerminator());
70 cir::YieldOp falseBranchYieldOp =
71 mlir::cast<cir::YieldOp>(op.getFalseRegion().front().getTerminator());
72 mlir::Value trueValue = trueBranchYieldOp.getArgs()[0];
73 mlir::Value falseValue = falseBranchYieldOp.getArgs()[0];
74
75 rewriter.inlineBlockBefore(&op.getTrueRegion().front(), op);
76 rewriter.inlineBlockBefore(&op.getFalseRegion().front(), op);
77 rewriter.eraseOp(trueBranchYieldOp);
78 rewriter.eraseOp(falseBranchYieldOp);
79 rewriter.replaceOpWithNewOp<cir::SelectOp>(op, op.getCond(), trueValue,
80 falseValue);
81
82 return mlir::success();
83 }
84
85private:
86 bool isSimpleTernaryBranch(mlir::Region &region) const {
87 if (!region.hasOneBlock())
88 return false;
89
90 mlir::Block &onlyBlock = region.front();
91 mlir::Block::OpListType &ops = onlyBlock.getOperations();
92
93 // The region/block could only contain at most 2 operations.
94 if (ops.size() > 2)
95 return false;
96
97 if (ops.size() == 1) {
98 // The region/block only contain a cir.yield operation.
99 return true;
100 }
101
102 // Check whether the region/block contains a cir.const followed by a
103 // cir.yield that yields the value.
104 auto yieldOp = mlir::cast<cir::YieldOp>(onlyBlock.getTerminator());
105 auto yieldValueDefOp =
106 yieldOp.getArgs()[0].getDefiningOp<cir::ConstantOp>();
107 return yieldValueDefOp && yieldValueDefOp->getBlock() == &onlyBlock;
108 }
109};
110
111/// Simplify select operations with boolean constants into simpler forms.
112///
113/// This pattern simplifies select operations where both true and false values
114/// are boolean constants. Two specific cases are handled:
115///
116/// 1. When selecting between true and false based on a condition,
117/// the operation simplifies to just the condition itself:
118///
119/// %0 = cir.select if %condition then true else false
120/// ->
121/// (replaced with %condition directly)
122///
123/// 2. When selecting between false and true based on a condition,
124/// the operation simplifies to the logical negation of the condition:
125///
126/// %0 = cir.select if %condition then false else true
127/// ->
128/// %0 = cir.not %condition
129struct SimplifySelect : public OpRewritePattern<SelectOp> {
130 using OpRewritePattern<SelectOp>::OpRewritePattern;
131
132 LogicalResult matchAndRewrite(SelectOp op,
133 PatternRewriter &rewriter) const final {
134 auto trueValueOp = op.getTrueValue().getDefiningOp<cir::ConstantOp>();
135 auto falseValueOp = op.getFalseValue().getDefiningOp<cir::ConstantOp>();
136 if (!trueValueOp || !falseValueOp)
137 return mlir::failure();
138
139 auto trueValue = trueValueOp.getValueAttr<cir::BoolAttr>();
140 auto falseValue = falseValueOp.getValueAttr<cir::BoolAttr>();
141 if (!trueValue || !falseValue)
142 return mlir::failure();
143
144 // cir.select if %0 then #true else #false -> %0
145 if (trueValue.getValue() && !falseValue.getValue()) {
146 rewriter.replaceAllUsesWith(op, op.getCondition());
147 rewriter.eraseOp(op);
148 return mlir::success();
149 }
150
151 // cir.select if %0 then #false else #true -> cir.not %0
152 if (!trueValue.getValue() && falseValue.getValue()) {
153 rewriter.replaceOpWithNewOp<cir::NotOp>(op, op.getCondition());
154 return mlir::success();
155 }
156
157 return mlir::failure();
158 }
159};
160
161/// Simplify `cir.switch` operations by folding cascading cases
162/// into a single `cir.case` with the `anyof` kind.
163///
164/// This pattern identifies cascading cases within a `cir.switch` operation.
165/// Cascading cases are defined as consecutive `cir.case` operations of kind
166/// `equal`, each containing a single `cir.yield` operation in their body.
167///
168/// The pattern merges these cascading cases into a single `cir.case` operation
169/// with kind `anyof`, aggregating all the case values.
170///
171/// The merging process continues until a `cir.case` with a different body
172/// (e.g., containing `cir.break` or compound stmt) is encountered, which
173/// breaks the chain.
174///
175/// Example:
176///
177/// Before:
178/// cir.case equal, [#cir.int<0> : !s32i] {
179/// cir.yield
180/// }
181/// cir.case equal, [#cir.int<1> : !s32i] {
182/// cir.yield
183/// }
184/// cir.case equal, [#cir.int<2> : !s32i] {
185/// cir.break
186/// }
187///
188/// After applying SimplifySwitch:
189/// cir.case anyof, [#cir.int<0> : !s32i, #cir.int<1> : !s32i, #cir.int<2> :
190/// !s32i] {
191/// cir.break
192/// }
193struct SimplifySwitch : public OpRewritePattern<SwitchOp> {
194 using OpRewritePattern<SwitchOp>::OpRewritePattern;
195 LogicalResult matchAndRewrite(SwitchOp op,
196 PatternRewriter &rewriter) const override {
197
198 LogicalResult changed = mlir::failure();
199 SmallVector<CaseOp, 8> cases;
200 SmallVector<CaseOp, 4> cascadingCases;
201 SmallVector<mlir::Attribute, 4> cascadingCaseValues;
202
203 op.collectCases(cases);
204 if (cases.empty())
205 return mlir::failure();
206
207 auto flushMergedOps = [&]() {
208 for (CaseOp &c : cascadingCases)
209 rewriter.eraseOp(c);
210 cascadingCases.clear();
211 cascadingCaseValues.clear();
212 };
213
214 auto mergeCascadingInto = [&](CaseOp &target) {
215 rewriter.modifyOpInPlace(target, [&]() {
216 target.setValueAttr(rewriter.getArrayAttr(cascadingCaseValues));
217 target.setKind(CaseOpKind::Anyof);
218 });
219 changed = mlir::success();
220 };
221
222 for (CaseOp c : cases) {
223 cir::CaseOpKind kind = c.getKind();
224 if (kind == cir::CaseOpKind::Equal &&
225 isa<YieldOp>(c.getCaseRegion().front().front())) {
226 // If the case contains only a YieldOp, collect it for cascading merge
227 cascadingCases.push_back(c);
228 cascadingCaseValues.push_back(c.getValue()[0]);
229 } else if (kind == cir::CaseOpKind::Equal && !cascadingCases.empty()) {
230 // merge previously collected cascading cases
231 cascadingCaseValues.push_back(c.getValue()[0]);
232 mergeCascadingInto(c);
233 flushMergedOps();
234 } else if (kind != cir::CaseOpKind::Equal && cascadingCases.size() > 1) {
235 // If a Default, Anyof or Range case is found and there are previous
236 // cascading cases, merge all of them into the last cascading case.
237 // We don't currently fold case range statements with other case
238 // statements.
240 CaseOp lastCascadingCase = cascadingCases.back();
241 mergeCascadingInto(lastCascadingCase);
242 cascadingCases.pop_back();
243 flushMergedOps();
244 } else {
245 cascadingCases.clear();
246 cascadingCaseValues.clear();
247 }
248 }
249
250 // Edge case: all cases are simple cascading cases
251 if (cascadingCases.size() == cases.size()) {
252 CaseOp lastCascadingCase = cascadingCases.back();
253 mergeCascadingInto(lastCascadingCase);
254 cascadingCases.pop_back();
255 flushMergedOps();
256 }
257
258 return changed;
259 }
260};
261
262struct SimplifyVecSplat : public OpRewritePattern<VecSplatOp> {
263 using OpRewritePattern<VecSplatOp>::OpRewritePattern;
264 LogicalResult matchAndRewrite(VecSplatOp op,
265 PatternRewriter &rewriter) const override {
266 mlir::Value splatValue = op.getValue();
267 auto constant = splatValue.getDefiningOp<cir::ConstantOp>();
268 if (!constant)
269 return mlir::failure();
270
271 auto value = constant.getValue();
272 if (!mlir::isa_and_nonnull<cir::IntAttr>(value) &&
273 !mlir::isa_and_nonnull<cir::FPAttr>(value))
274 return mlir::failure();
275
276 cir::VectorType resultType = op.getResult().getType();
277 SmallVector<mlir::Attribute, 16> elements(resultType.getSize(), value);
278 auto constVecAttr = cir::ConstVectorAttr::get(
279 resultType, mlir::ArrayAttr::get(getContext(), elements));
280
281 rewriter.replaceOpWithNewOp<cir::ConstantOp>(op, constVecAttr);
282 return mlir::success();
283 }
284};
285
286//===----------------------------------------------------------------------===//
287// CIRSimplifyPass
288//===----------------------------------------------------------------------===//
289
290struct CIRSimplifyPass : public impl::CIRSimplifyBase<CIRSimplifyPass> {
291 using CIRSimplifyBase::CIRSimplifyBase;
292
293 void runOnOperation() override;
294};
295
296void populateMergeCleanupPatterns(RewritePatternSet &patterns) {
297 // clang-format off
298 patterns.add<
299 SimplifyTernary,
300 SimplifySelect,
301 SimplifySwitch,
302 SimplifyVecSplat
303 >(patterns.getContext());
304 // clang-format on
305}
306
307void CIRSimplifyPass::runOnOperation() {
308 // Collect rewrite patterns.
309 RewritePatternSet patterns(&getContext());
310 populateMergeCleanupPatterns(patterns);
311
312 // Collect operations to apply patterns.
313 llvm::SmallVector<Operation *, 16> ops;
314 getOperation()->walk([&](Operation *op) {
315 if (isa<TernaryOp, SelectOp, SwitchOp, VecSplatOp>(op))
316 ops.push_back(op);
317 });
318
319 // Apply patterns.
320 if (applyOpPatternsGreedily(ops, std::move(patterns)).failed())
321 signalPassFailure();
322}
323
324} // namespace
325
326std::unique_ptr<Pass> mlir::createCIRSimplifyPass() {
327 return std::make_unique<CIRSimplifyPass>();
328}
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
__device__ __2f16 float c
std::unique_ptr< Pass > createCIRSimplifyPass()
static bool foldRangeCase()