MLIR  19.0.0git
FoldInterfaces.h
Go to the documentation of this file.
1 //===- FoldInterfaces.h - Folding Interfaces --------------------*- 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 #ifndef MLIR_INTERFACES_FOLDINTERFACES_H_
9 #define MLIR_INTERFACES_FOLDINTERFACES_H_
10 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/SmallVector.h"
14 
15 namespace mlir {
16 class Attribute;
17 class OpFoldResult;
18 class Region;
19 
20 /// Define a fold interface to allow for dialects to control specific aspects
21 /// of the folding behavior for operations they define.
23  : public DialectInterface::Base<DialectFoldInterface> {
24 public:
25  DialectFoldInterface(Dialect *dialect) : Base(dialect) {}
26 
27  /// Registered fallback fold for the dialect. Like the fold hook of each
28  /// operation, it attempts to fold the operation with the specified constant
29  /// operand values - the elements in "operands" will correspond directly to
30  /// the operands of the operation, but may be null if non-constant. If
31  /// folding is successful, this fills in the `results` vector. If not, this
32  /// returns failure and `results` is unspecified.
33  virtual LogicalResult fold(Operation *op, ArrayRef<Attribute> operands,
34  SmallVectorImpl<OpFoldResult> &results) const {
35  return failure();
36  }
37 
38  /// Registered hook to check if the given region, which is attached to an
39  /// operation that is *not* isolated from above, should be used when
40  /// materializing constants. The folder will generally materialize constants
41  /// into the top-level isolated region, this allows for materializing into a
42  /// lower level ancestor region if it is more profitable/correct.
43  virtual bool shouldMaterializeInto(Region *region) const { return false; }
44 };
45 
46 } // namespace mlir
47 
48 #endif // MLIR_INTERFACES_FOLDINTERFACES_H_
Define a fold interface to allow for dialects to control specific aspects of the folding behavior for...
virtual LogicalResult fold(Operation *op, ArrayRef< Attribute > operands, SmallVectorImpl< OpFoldResult > &results) const
Registered fallback fold for the dialect.
DialectFoldInterface(Dialect *dialect)
virtual bool shouldMaterializeInto(Region *region) const
Registered hook to check if the given region, which is attached to an operation that is not isolated ...
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:38
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
The base class used for all derived interface types.
Include the generated interface declarations.