MLIR  22.0.0git
Utils.h
Go to the documentation of this file.
1 //===- Utils.h - General Func transformation utilities ----*- 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 header file defines prototypes for various transformation utilities for
10 // the Func dialect. These are not passes by themselves but are used
11 // either by passes, optimization sequences, or in turn by other transformation
12 // utilities.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef MLIR_DIALECT_FUNC_UTILS_H
17 #define MLIR_DIALECT_FUNC_UTILS_H
18 
19 #include "mlir/IR/PatternMatch.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include <string>
22 
23 namespace mlir {
24 
25 class ModuleOp;
26 
27 namespace func {
28 
29 class FuncOp;
30 class CallOp;
31 
32 /// Creates a new function operation with the same name as the original
33 /// function operation, but with the arguments mapped according to
34 /// the `oldArgToNewArg` and `oldResToNewRes`.
35 /// The `funcOp` operation must have exactly one block.
36 /// Returns the new function operation or failure if `funcOp` doesn't
37 /// have exactly one block.
38 /// Note: the method asserts that the `oldArgToNewArg` and `oldResToNewRes`
39 /// maps the whole function arguments and results.
40 mlir::FailureOr<mlir::func::FuncOp> replaceFuncWithNewMapping(
41  mlir::RewriterBase &rewriter, mlir::func::FuncOp funcOp,
42  ArrayRef<int> oldArgIdxToNewArgIdx, ArrayRef<int> oldResIdxToNewResIdx);
43 /// Creates a new call operation with the values as the original
44 /// call operation, but with the arguments mapped according to
45 /// the `oldArgToNewArg` and `oldResToNewRes`.
46 /// Note: the method asserts that the `oldArgToNewArg` and `oldResToNewRes`
47 /// maps the whole call operation arguments and results.
48 mlir::func::CallOp replaceCallOpWithNewMapping(
49  mlir::RewriterBase &rewriter, mlir::func::CallOp callOp,
50  ArrayRef<int> oldArgIdxToNewArgIdx, ArrayRef<int> oldResIdxToNewResIdx);
51 
52 /// This utility function examines all call operations within the given
53 /// `moduleOp` that target the specified `funcOp`. It identifies duplicate
54 /// operands in the call operations, creates mappings to deduplicate them, and
55 /// then applies the transformation to both the function and its call sites. For
56 /// now, it only supports one call operation for the function operation. The
57 /// function returns a pair containing the new funcOp and the new callOp. Note:
58 /// after the transformation, the original funcOp and callOp will be erased.
59 mlir::FailureOr<std::pair<mlir::func::FuncOp, mlir::func::CallOp>>
60 deduplicateArgsOfFuncOp(mlir::RewriterBase &rewriter, mlir::func::FuncOp funcOp,
61  mlir::ModuleOp moduleOp);
62 
63 } // namespace func
64 } // namespace mlir
65 
66 #endif // MLIR_DIALECT_FUNC_UTILS_H
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Definition: PatternMatch.h:368
mlir::func::CallOp replaceCallOpWithNewMapping(mlir::RewriterBase &rewriter, mlir::func::CallOp callOp, ArrayRef< int > oldArgIdxToNewArgIdx, ArrayRef< int > oldResIdxToNewResIdx)
Creates a new call operation with the values as the original call operation, but with the arguments m...
mlir::FailureOr< mlir::func::FuncOp > replaceFuncWithNewMapping(mlir::RewriterBase &rewriter, mlir::func::FuncOp funcOp, ArrayRef< int > oldArgIdxToNewArgIdx, ArrayRef< int > oldResIdxToNewResIdx)
Creates a new function operation with the same name as the original function operation,...
mlir::FailureOr< std::pair< mlir::func::FuncOp, mlir::func::CallOp > > deduplicateArgsOfFuncOp(mlir::RewriterBase &rewriter, mlir::func::FuncOp funcOp, mlir::ModuleOp moduleOp)
This utility function examines all call operations within the given moduleOp that target the specifie...
Include the generated interface declarations.