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
20#include "llvm/ADT/ArrayRef.h"
21#include <string>
22
23namespace mlir {
24
25class ModuleOp;
26
27namespace func {
28
29class FuncOp;
30class 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.
40mlir::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.
48mlir::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.
59mlir::FailureOr<std::pair<mlir::func::FuncOp, mlir::func::CallOp>>
60deduplicateArgsOfFuncOp(mlir::RewriterBase &rewriter, mlir::func::FuncOp funcOp,
61 mlir::ModuleOp moduleOp);
62
63/// Look up a FuncOp with signature `resultTypes`(`paramTypes`)` and name
64/// `name`. Return a failure if the FuncOp is found but with a different
65/// signature.
66FailureOr<FuncOp> lookupFnDecl(SymbolOpInterface symTable, StringRef name,
67 FunctionType funcT,
68 SymbolTableCollection *symbolTables = nullptr);
69
70/// Create a FuncOp decl and insert it into `symTable` operation. If
71/// `symbolTables` is provided, then the decl will be inserted into the
72/// SymbolTableCollection.
73FuncOp createFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
74 FunctionType funcT, bool setPrivate,
75 SymbolTableCollection *symbolTables = nullptr);
76
77/// Helper function to look up or create the symbol for a runtime library
78/// function with the given parameter types. Returns an int64_t, unless a
79/// different result type is specified.
80FailureOr<FuncOp>
81lookupOrCreateFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
82 TypeRange paramTypes,
83 SymbolTableCollection *symbolTables = nullptr,
84 Type resultType = {});
85
86} // namespace func
87} // namespace mlir
88
89#endif // MLIR_DIALECT_FUNC_UTILS_H
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
This class helps build Operations.
Definition Builders.h:207
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
This class represents a collection of SymbolTables.
This class provides an abstraction over the various different ranges of value types.
Definition TypeRange.h:37
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
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...
FuncOp createFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name, FunctionType funcT, bool setPrivate, SymbolTableCollection *symbolTables=nullptr)
Look up a FuncOp with signature resultTypes(paramTypes) and name / name`.
Definition Utils.cpp:283
FailureOr< FuncOp > lookupOrCreateFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name, TypeRange paramTypes, SymbolTableCollection *symbolTables=nullptr, Type resultType={})
Helper function to look up or create the symbol for a runtime library function with the given paramet...
Definition Utils.cpp:302
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.