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 
11 #include "mlir/IR/DialectInterface.h"
12 #include "mlir/Support/LogicalResult.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/SmallVector.h"
15 
16 namespace mlir {
17 class Attribute;
18 class OpFoldResult;
19 class Region;
20 
21 /// Define a fold interface to allow for dialects to control specific aspects
22 /// of the folding behavior for operations they define.
23 class DialectFoldInterface
24     : public DialectInterface::Base<DialectFoldInterface> {
25 public:
DialectFoldInterface(Dialect * dialect)26   DialectFoldInterface(Dialect *dialect) : Base(dialect) {}
27 
28   /// Registered fallback fold for the dialect. Like the fold hook of each
29   /// operation, it attempts to fold the operation with the specified constant
30   /// operand values - the elements in "operands" will correspond directly to
31   /// the operands of the operation, but may be null if non-constant.  If
32   /// folding is successful, this fills in the `results` vector.  If not, this
33   /// returns failure and `results` is unspecified.
fold(Operation * op,ArrayRef<Attribute> operands,SmallVectorImpl<OpFoldResult> & results)34   virtual LogicalResult fold(Operation *op, ArrayRef<Attribute> operands,
35                              SmallVectorImpl<OpFoldResult> &results) const {
36     return failure();
37   }
38 
39   /// Registered hook to check if the given region, which is attached to an
40   /// operation that is *not* isolated from above, should be used when
41   /// materializing constants. The folder will generally materialize constants
42   /// into the top-level isolated region, this allows for materializing into a
43   /// lower level ancestor region if it is more profitable/correct.
shouldMaterializeInto(Region * region)44   virtual bool shouldMaterializeInto(Region *region) const { return false; }
45 };
46 
47 } // namespace mlir
48 
49 #endif // MLIR_INTERFACES_FOLDINTERFACES_H_
50