xref: /llvm-project/mlir/include/mlir/Interfaces/CallInterfaces.td (revision d1cad2290c10712ea27509081f50769ed597ee0f)
1//===- CallInterfaces.td - Call Interfaces for ops ---------*- tablegen -*-===//
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 file contains a set of interfaces that can be used to define information
10// related to call-like and callable operations. Each of which are defined along
11// with the respective interface below.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef MLIR_INTERFACES_CALLINTERFACES
16#define MLIR_INTERFACES_CALLINTERFACES
17
18include "mlir/IR/OpBase.td"
19
20// `CallInterfaceCallable`: This is a type used to represent a single callable
21// region. A callable is either a symbol, or an SSA value, that is referenced by
22// a call-like operation. This represents the destination of the call.
23
24/// Interface for call-like operations.
25def CallOpInterface : OpInterface<"CallOpInterface"> {
26  let description = [{
27    A call-like operation is one that transfers control from one sub-routine to
28    another. These operations may be traditional direct calls `call @foo`, or
29    indirect calls to other operations `call_indirect %foo`. An operation that
30    uses this interface, must *not* also provide the `CallableOpInterface`.
31  }];
32  let cppNamespace = "::mlir";
33
34  let methods = [
35    InterfaceMethod<[{
36        Returns the callee of this call-like operation. A `callee` is either a
37        reference to a symbol, via SymbolRefAttr, or a reference to a defined
38        SSA value. If the reference is an SSA value, the SSA value corresponds
39        to a region of a lambda-like operation.
40      }],
41      "::mlir::CallInterfaceCallable", "getCallableForCallee"
42    >,
43    InterfaceMethod<[{
44        Sets the callee of this call-like operation. A `callee` is either a
45        reference to a symbol, via SymbolRefAttr, or a reference to a defined
46        SSA value. The type of the `callee` is expected to be the same as the
47        return type of `getCallableForCallee`, e.g., `callee` should be
48        SymbolRefAttr for `func.call`.
49      }],
50      "void", "setCalleeFromCallable", (ins "::mlir::CallInterfaceCallable":$callee)
51    >,
52    InterfaceMethod<[{
53        Returns the operands within this call that are used as arguments to the
54        callee.
55      }],
56      "::mlir::Operation::operand_range", "getArgOperands"
57    >,
58    InterfaceMethod<[{
59        Returns the operands within this call that are used as arguments to the
60        callee as a mutable range.
61      }],
62      "::mlir::MutableOperandRange", "getArgOperandsMutable"
63    >,
64    InterfaceMethod<[{
65        Resolve the callable operation for given callee to a
66        CallableOpInterface, or nullptr if a valid callable was not resolved.
67        `symbolTable` parameter allow for using a cached symbol table for symbol
68        lookups instead of performing an O(N) scan.
69      }],
70      "::mlir::Operation *", "resolveCallableInTable", (ins "::mlir::SymbolTableCollection *":$symbolTable),
71      /*methodBody=*/[{}], /*defaultImplementation=*/[{
72        return ::mlir::call_interface_impl::resolveCallable($_op, symbolTable);
73      }]
74    >,
75    InterfaceMethod<[{
76        Resolve the callable operation for given callee to a
77        CallableOpInterface, or nullptr if a valid callable was not resolved.
78    }],
79      "::mlir::Operation *", "resolveCallable", (ins),
80      /*methodBody=*/[{}], /*defaultImplementation=*/[{
81        return ::mlir::call_interface_impl::resolveCallable($_op);
82      }]
83    >
84  ];
85}
86
87/// Interface for callable operations.
88def CallableOpInterface : OpInterface<"CallableOpInterface"> {
89  let description = [{
90    A callable operation is one who represents a potential sub-routine, and may
91    be a target for a call-like operation (those providing the CallOpInterface
92    above). These operations may be traditional functional operation
93    `func @foo(...)`, as well as function producing operations
94    `%foo = dialect.create_function(...)`. These operations may only contain a
95    single region, or subroutine.
96  }];
97  let cppNamespace = "::mlir";
98
99  let methods = [
100    InterfaceMethod<[{
101        Returns the region on the current operation that is callable. This may
102        return null in the case of an external callable object, e.g. an external
103        function.
104      }],
105      "::mlir::Region *", "getCallableRegion">,
106    InterfaceMethod<[{
107      Returns the callable's argument types based exclusively on the type (to
108      allow for this method may be called on function declarations).
109    }],
110    "::llvm::ArrayRef<::mlir::Type>", "getArgumentTypes">,
111    InterfaceMethod<[{
112      Returns the callable's result types based exclusively on the type (to
113      allow for this method may be called on function declarations).
114    }],
115    "::llvm::ArrayRef<::mlir::Type>", "getResultTypes">,
116
117    InterfaceMethod<[{
118        Get the array of argument attribute dictionaries. The method should
119        return an array attribute containing only dictionary attributes equal in
120        number to the number of region arguments. Alternatively, the method can
121        return null to indicate that the region has no argument attributes.
122      }],
123      "::mlir::ArrayAttr", "getArgAttrsAttr", (ins),
124      /*methodBody=*/[{}], /*defaultImplementation=*/[{ return nullptr; }]>,
125    InterfaceMethod<[{
126        Get the array of result attribute dictionaries. The method should return
127        an array attribute containing only dictionary attributes equal in number
128        to the number of region results. Alternatively, the method can return
129        null to indicate that the region has no result attributes.
130      }],
131      "::mlir::ArrayAttr", "getResAttrsAttr", (ins),
132      /*methodBody=*/[{}], /*defaultImplementation=*/[{ return nullptr; }]>,
133    InterfaceMethod<[{
134      Set the array of argument attribute dictionaries.
135    }],
136    "void", "setArgAttrsAttr", (ins "::mlir::ArrayAttr":$attrs),
137      /*methodBody=*/[{}], /*defaultImplementation=*/[{ return; }]>,
138    InterfaceMethod<[{
139      Set the array of result attribute dictionaries.
140    }],
141    "void", "setResAttrsAttr", (ins "::mlir::ArrayAttr":$attrs),
142      /*methodBody=*/[{}], /*defaultImplementation=*/[{ return; }]>,
143    InterfaceMethod<[{
144      Remove the array of argument attribute dictionaries. This is the same as
145      setting all argument attributes to an empty dictionary. The method should
146      return the removed attribute.
147    }],
148    "::mlir::Attribute", "removeArgAttrsAttr", (ins),
149      /*methodBody=*/[{}], /*defaultImplementation=*/[{ return nullptr; }]>,
150    InterfaceMethod<[{
151      Remove the array of result attribute dictionaries. This is the same as
152      setting all result attributes to an empty dictionary. The method should
153      return the removed attribute.
154    }],
155    "::mlir::Attribute", "removeResAttrsAttr", (ins),
156      /*methodBody=*/[{}], /*defaultImplementation=*/[{ return nullptr; }]>,
157  ];
158}
159
160#endif // MLIR_INTERFACES_CALLINTERFACES
161