MLIR 22.0.0git
FormExpressions.cpp
Go to the documentation of this file.
1//===- FormExpressions.cpp - Form C-style expressions --------*- C++ -*-===//
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// This file implements a pass that forms EmitC operations modeling C operators
10// into C-style expressions using the emitc.expression op.
11//
12//===----------------------------------------------------------------------===//
13
18
19namespace mlir {
20namespace emitc {
21#define GEN_PASS_DEF_FORMEXPRESSIONSPASS
22#include "mlir/Dialect/EmitC/Transforms/Passes.h.inc"
23} // namespace emitc
24} // namespace mlir
25
26using namespace mlir;
27using namespace emitc;
29namespace {
30struct FormExpressionsPass
31 : public emitc::impl::FormExpressionsPassBase<FormExpressionsPass> {
32 void runOnOperation() override {
34 MLIRContext *context = rootOp->getContext();
36 // Wrap each C operator op with an expression op.
37 OpBuilder builder(context);
38 auto matchFun = [&](Operation *op) {
39 if (isa<emitc::CExpressionInterface>(*op) &&
40 !op->getParentOfType<emitc::ExpressionOp>() &&
41 op->getNumResults() == 1)
42 createExpression(op, builder);
43 };
44 rootOp->walk(matchFun);
45
46 // Fold expressions where possible.
50 if (failed(applyPatternsGreedily(rootOp, std::move(patterns))))
51 return signalPassFailure();
52 }
53
54 void getDependentDialects(DialectRegistry &registry) const override {
55 registry.insert<emitc::EmitCDialect>();
56 }
57};
58} // namespace
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
This class helps build Operations.
Definition Builders.h:207
OpT getOperation()
Return the current operation being transformed.
Definition Pass.h:378
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
Definition Operation.h:797
MLIRContext * getContext()
Return the context this operation is associated with.
Definition Operation.h:216
virtual void runOnOperation()=0
The polymorphic API that runs the pass over the currently held operation.
void signalPassFailure()
Signal that some invariant was broken when running.
Definition Pass.h:218
void populateExpressionPatterns(RewritePatternSet &patterns)
Populates patterns with expression-related patterns.
ExpressionOp createExpression(Operation *op, OpBuilder &builder)
Include the generated interface declarations.
LogicalResult applyPatternsGreedily(Region &region, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
const FrozenRewritePatternSet & patterns