• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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.
22 class DialectFoldInterface
23     : public DialectInterface::Base<DialectFoldInterface> {
24 public:
DialectFoldInterface(Dialect * dialect)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.
fold(Operation * op,ArrayRef<Attribute> operands,SmallVectorImpl<OpFoldResult> & results)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.
shouldMaterializeInto(Region * region)43   virtual bool shouldMaterializeInto(Region *region) const { return false; }
44 };
45 
46 } // end namespace mlir
47 
48 #endif // MLIR_INTERFACES_FOLDINTERFACES_H_
49