MLIR  19.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 
19 namespace mlir {
20 namespace emitc {
21 #define GEN_PASS_DEF_FORMEXPRESSIONS
22 #include "mlir/Dialect/EmitC/Transforms/Passes.h.inc"
23 } // namespace emitc
24 } // namespace mlir
25 
26 using namespace mlir;
27 using namespace emitc;
28 
29 namespace {
30 struct FormExpressionsPass
31  : public emitc::impl::FormExpressionsBase<FormExpressionsPass> {
32  void runOnOperation() override {
33  Operation *rootOp = getOperation();
34  MLIRContext *context = rootOp->getContext();
35 
36  // Wrap each C operator op with an expression op.
37  OpBuilder builder(context);
38  auto matchFun = [&](Operation *op) {
40  !op->getParentOfType<emitc::ExpressionOp>())
41  createExpression(op, builder);
42  };
43  rootOp->walk(matchFun);
44 
45  // Fold expressions where possible.
46  RewritePatternSet patterns(context);
48 
49  if (failed(applyPatternsAndFoldGreedily(rootOp, std::move(patterns))))
50  return signalPassFailure();
51  }
52 
53  void getDependentDialects(DialectRegistry &registry) const override {
54  registry.insert<emitc::EmitCDialect>();
55  }
56 };
57 } // namespace
58 
59 std::unique_ptr<Pass> mlir::emitc::createFormExpressionsPass() {
60  return std::make_unique<FormExpressionsPass>();
61 }
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:60
This class helps build Operations.
Definition: Builders.h:209
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition: Operation.h:745
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:793
MLIRContext * getContext()
Return the context this operation is associated with.
Definition: Operation.h:216
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition: Operation.h:238
std::unique_ptr< Pass > createFormExpressionsPass()
Creates an instance of the C-style expressions forming pass.
void populateExpressionPatterns(RewritePatternSet &patterns)
Populates patterns with expression-related patterns.
Definition: Transforms.cpp:110
ExpressionOp createExpression(Operation *op, OpBuilder &builder)
Definition: Transforms.cpp:18
Include the generated interface declarations.
LogicalResult applyPatternsAndFoldGreedily(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...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72