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
14#include "mlir/Dialect/EmitC/IR/EmitC.h"
15#include "mlir/Dialect/EmitC/Transforms/Passes.h"
16#include "mlir/Dialect/EmitC/Transforms/Transforms.h"
17#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
18
19namespace mlir {
20namespace emitc {
21#define GEN_PASS_DEF_FORMEXPRESSIONS
22#include "mlir/Dialect/EmitC/Transforms/Passes.h.inc"
23} // namespace emitc
24} // namespace mlir
25
26using namespace mlir;
27using namespace emitc;
28
29namespace {
30struct 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) {
39 if (op->hasTrait<OpTrait::emitc::CExpression>() &&
40 !op->getParentOfType<emitc::ExpressionOp>())
41 createExpression(op, builder);
42 };
43 rootOp->walk(matchFun);
44
45 // Fold expressions where possible.
46 RewritePatternSet patterns(context);
47 populateExpressionPatterns(patterns);
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
59std::unique_ptr<Pass> mlir::emitc::createFormExpressionsPass() {
60 return std::make_unique<FormExpressionsPass>();
61}
62

source code of mlir/lib/Dialect/EmitC/Transforms/FormExpressions.cpp