Skip to content

Commit cf98e82

Browse files
committed
Revert "[mlir] FunctionOpInterface: make get/setFunctionType interface methods"
and "[mlir] Fix examples build" This reverts commit fbc253f and 96cf183. Which I missed in the first revert in f3379fe.
1 parent f79d941 commit cf98e82

File tree

23 files changed

+117
-119
lines changed

23 files changed

+117
-119
lines changed

mlir/examples/toy/Ch2/mlir/Dialect.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,7 @@ mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser,
211211
std::string &) { return builder.getFunctionType(argTypes, results); };
212212

213213
return mlir::function_interface_impl::parseFunctionOp(
214-
parser, result, /*allowVariadic=*/false,
215-
getFunctionTypeAttrName(result.name), buildFuncType,
216-
getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name));
214+
parser, result, /*allowVariadic=*/false, buildFuncType);
217215
}
218216

219217
void FuncOp::print(mlir::OpAsmPrinter &p) {

mlir/examples/toy/Ch3/mlir/Dialect.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser,
198198
std::string &) { return builder.getFunctionType(argTypes, results); };
199199

200200
return mlir::function_interface_impl::parseFunctionOp(
201-
parser, result, /*allowVariadic=*/false,
202-
getFunctionTypeAttrName(result.name), buildFuncType,
203-
getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name));
201+
parser, result, /*allowVariadic=*/false, buildFuncType);
204202
}
205203

206204
void FuncOp::print(mlir::OpAsmPrinter &p) {

mlir/examples/toy/Ch4/mlir/Dialect.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,7 @@ mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser,
287287
std::string &) { return builder.getFunctionType(argTypes, results); };
288288

289289
return mlir::function_interface_impl::parseFunctionOp(
290-
parser, result, /*allowVariadic=*/false,
291-
getFunctionTypeAttrName(result.name), buildFuncType,
292-
getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name));
290+
parser, result, /*allowVariadic=*/false, buildFuncType);
293291
}
294292

295293
void FuncOp::print(mlir::OpAsmPrinter &p) {

mlir/examples/toy/Ch5/mlir/Dialect.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,7 @@ mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser,
287287
std::string &) { return builder.getFunctionType(argTypes, results); };
288288

289289
return mlir::function_interface_impl::parseFunctionOp(
290-
parser, result, /*allowVariadic=*/false,
291-
getFunctionTypeAttrName(result.name), buildFuncType,
292-
getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name));
290+
parser, result, /*allowVariadic=*/false, buildFuncType);
293291
}
294292

295293
void FuncOp::print(mlir::OpAsmPrinter &p) {

mlir/examples/toy/Ch6/mlir/Dialect.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,7 @@ mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser,
287287
std::string &) { return builder.getFunctionType(argTypes, results); };
288288

289289
return mlir::function_interface_impl::parseFunctionOp(
290-
parser, result, /*allowVariadic=*/false,
291-
getFunctionTypeAttrName(result.name), buildFuncType,
292-
getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name));
290+
parser, result, /*allowVariadic=*/false, buildFuncType);
293291
}
294292

295293
void FuncOp::print(mlir::OpAsmPrinter &p) {

mlir/examples/toy/Ch7/mlir/Dialect.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,7 @@ mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser,
314314
std::string &) { return builder.getFunctionType(argTypes, results); };
315315

316316
return mlir::function_interface_impl::parseFunctionOp(
317-
parser, result, /*allowVariadic=*/false,
318-
getFunctionTypeAttrName(result.name), buildFuncType,
319-
getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name));
317+
parser, result, /*allowVariadic=*/false, buildFuncType);
320318
}
321319

322320
void FuncOp::print(mlir::OpAsmPrinter &p) {

mlir/include/mlir/IR/FunctionImplementation.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,17 @@ Type getFunctionType(Builder &builder, ArrayRef<OpAsmParser::Argument> argAttrs,
6969

7070
/// Parser implementation for function-like operations. Uses
7171
/// `funcTypeBuilder` to construct the custom function type given lists of
72-
/// input and output types. The parser sets the `typeAttrName` attribute to the
73-
/// resulting function type. If `allowVariadic` is set, the parser will accept
72+
/// input and output types. If `allowVariadic` is set, the parser will accept
7473
/// trailing ellipsis in the function signature and indicate to the builder
7574
/// whether the function is variadic. If the builder returns a null type,
7675
/// `result` will not contain the `type` attribute. The caller can then add a
7776
/// type, report the error or delegate the reporting to the op's verifier.
7877
ParseResult parseFunctionOp(OpAsmParser &parser, OperationState &result,
79-
bool allowVariadic, StringAttr typeAttrName,
78+
bool allowVariadic,
8079
FuncTypeBuilder funcTypeBuilder);
8180

8281
/// Printer implementation for function-like operations.
83-
void printFunctionOp(OpAsmPrinter &p, FunctionOpInterface op, bool isVariadic,
84-
StringRef typeAttrName);
82+
void printFunctionOp(OpAsmPrinter &p, FunctionOpInterface op, bool isVariadic);
8583

8684
/// Prints the signature of the function-like operation `op`. Assumes `op` has
8785
/// is a FunctionOpInterface and has passed verification.
@@ -94,7 +92,8 @@ void printFunctionSignature(OpAsmPrinter &p, Operation *op,
9492
/// function-like operation internally are not printed. Nothing is printed
9593
/// if all attributes are elided. Assumes `op` is a FunctionOpInterface and
9694
/// has passed verification.
97-
void printFunctionAttributes(OpAsmPrinter &p, Operation *op,
95+
void printFunctionAttributes(OpAsmPrinter &p, Operation *op, unsigned numInputs,
96+
unsigned numResults,
9897
ArrayRef<StringRef> elided = {});
9998

10099
} // namespace function_interface_impl

mlir/include/mlir/IR/FunctionInterfaces.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
#include "llvm/ADT/SmallString.h"
2323

2424
namespace mlir {
25-
class FunctionOpInterface;
2625

2726
namespace function_interface_impl {
2827

28+
/// Return the name of the attribute used for function types.
29+
inline StringRef getTypeAttrName() { return "function_type"; }
30+
2931
/// Return the name of the attribute used for function argument attributes.
3032
inline StringRef getArgDictAttrName() { return "arg_attrs"; }
3133

@@ -70,29 +72,28 @@ inline ArrayRef<NamedAttribute> getResultAttrs(Operation *op, unsigned index) {
7072
}
7173

7274
/// Insert the specified arguments and update the function type attribute.
73-
void insertFunctionArguments(FunctionOpInterface op,
74-
ArrayRef<unsigned> argIndices, TypeRange argTypes,
75+
void insertFunctionArguments(Operation *op, ArrayRef<unsigned> argIndices,
76+
TypeRange argTypes,
7577
ArrayRef<DictionaryAttr> argAttrs,
7678
ArrayRef<Location> argLocs,
7779
unsigned originalNumArgs, Type newType);
7880

7981
/// Insert the specified results and update the function type attribute.
80-
void insertFunctionResults(FunctionOpInterface op,
81-
ArrayRef<unsigned> resultIndices,
82+
void insertFunctionResults(Operation *op, ArrayRef<unsigned> resultIndices,
8283
TypeRange resultTypes,
8384
ArrayRef<DictionaryAttr> resultAttrs,
8485
unsigned originalNumResults, Type newType);
8586

8687
/// Erase the specified arguments and update the function type attribute.
87-
void eraseFunctionArguments(FunctionOpInterface op, const BitVector &argIndices,
88+
void eraseFunctionArguments(Operation *op, const BitVector &argIndices,
8889
Type newType);
8990

9091
/// Erase the specified results and update the function type attribute.
91-
void eraseFunctionResults(FunctionOpInterface op,
92-
const BitVector &resultIndices, Type newType);
92+
void eraseFunctionResults(Operation *op, const BitVector &resultIndices,
93+
Type newType);
9394

9495
/// Set a FunctionOpInterface operation's type signature.
95-
void setFunctionType(FunctionOpInterface op, Type newType);
96+
void setFunctionType(Operation *op, Type newType);
9697

9798
/// Insert a set of `newTypes` into `oldTypes` at the given `indices`. If any
9899
/// types are inserted, `storage` is used to hold the new type list. The new
@@ -206,6 +207,10 @@ Attribute removeResultAttr(ConcreteType op, unsigned index, StringAttr name) {
206207
/// method on FunctionOpInterface::Trait.
207208
template <typename ConcreteOp>
208209
LogicalResult verifyTrait(ConcreteOp op) {
210+
if (!op.getFunctionTypeAttr())
211+
return op.emitOpError("requires a type attribute '")
212+
<< function_interface_impl::getTypeAttrName() << '\'';
213+
209214
if (failed(op.verifyType()))
210215
return failure();
211216

mlir/include/mlir/IR/FunctionInterfaces.td

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,6 @@ def FunctionOpInterface : OpInterface<"FunctionOpInterface"> {
4949
for each of the function results.
5050
}];
5151
let methods = [
52-
InterfaceMethod<[{
53-
Returns the type of the function.
54-
}],
55-
"::mlir::Type", "getFunctionType">,
56-
InterfaceMethod<[{
57-
Set the type of the function. This method should perform an unsafe
58-
modification to the function type; it should not update argument or
59-
result attributes.
60-
}],
61-
"void", "setFunctionTypeAttr", (ins "::mlir::TypeAttr":$type)>,
6252
InterfaceMethod<[{
6353
Returns the function argument types based exclusively on
6454
the type (to allow for this method may be called on function
@@ -149,7 +139,7 @@ def FunctionOpInterface : OpInterface<"FunctionOpInterface"> {
149139
ArrayRef<NamedAttribute> attrs, TypeRange inputTypes) {
150140
state.addAttribute(SymbolTable::getSymbolAttrName(),
151141
builder.getStringAttr(name));
152-
state.addAttribute(ConcreteOp::getFunctionTypeAttrName(state.name),
142+
state.addAttribute(function_interface_impl::getTypeAttrName(),
153143
TypeAttr::get(type));
154144
state.attributes.append(attrs.begin(), attrs.end());
155145

@@ -254,6 +244,11 @@ def FunctionOpInterface : OpInterface<"FunctionOpInterface"> {
254244
// the derived operation, which should already have these defined
255245
// (via ODS).
256246

247+
/// Returns the name of the attribute used for function types.
248+
static StringRef getTypeAttrName() {
249+
return function_interface_impl::getTypeAttrName();
250+
}
251+
257252
/// Returns the name of the attribute used for function argument attributes.
258253
static StringRef getArgDictAttrName() {
259254
return function_interface_impl::getArgDictAttrName();
@@ -264,6 +259,15 @@ def FunctionOpInterface : OpInterface<"FunctionOpInterface"> {
264259
return function_interface_impl::getResultDictAttrName();
265260
}
266261

262+
/// Return the attribute containing the type of this function.
263+
TypeAttr getFunctionTypeAttr() {
264+
return this->getOperation()->template getAttrOfType<TypeAttr>(
265+
getTypeAttrName());
266+
}
267+
268+
/// Return the type of this function.
269+
Type getFunctionType() { return getFunctionTypeAttr().getValue(); }
270+
267271
//===------------------------------------------------------------------===//
268272
// Argument and Result Handling
269273
//===------------------------------------------------------------------===//

mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ using namespace mlir;
5959
/// Only retain those attributes that are not constructed by
6060
/// `LLVMFuncOp::build`. If `filterArgAttrs` is set, also filter out argument
6161
/// attributes.
62-
static void filterFuncAttributes(func::FuncOp func, bool filterArgAndResAttrs,
62+
static void filterFuncAttributes(ArrayRef<NamedAttribute> attrs,
63+
bool filterArgAndResAttrs,
6364
SmallVectorImpl<NamedAttribute> &result) {
64-
for (const NamedAttribute &attr : func->getAttrs()) {
65+
for (const auto &attr : attrs) {
6566
if (attr.getName() == SymbolTable::getSymbolAttrName() ||
66-
attr.getName() == func.getFunctionTypeAttrName() ||
67+
attr.getName() == FunctionOpInterface::getTypeAttrName() ||
6768
attr.getName() == "func.varargs" ||
6869
(filterArgAndResAttrs &&
6970
(attr.getName() == FunctionOpInterface::getArgDictAttrName() ||
@@ -137,7 +138,8 @@ static void wrapForExternalCallers(OpBuilder &rewriter, Location loc,
137138
LLVM::LLVMFuncOp newFuncOp) {
138139
auto type = funcOp.getFunctionType();
139140
SmallVector<NamedAttribute, 4> attributes;
140-
filterFuncAttributes(funcOp, /*filterArgAndResAttrs=*/false, attributes);
141+
filterFuncAttributes(funcOp->getAttrs(), /*filterArgAndResAttrs=*/false,
142+
attributes);
141143
auto [wrapperFuncType, resultIsNowArg] =
142144
typeConverter.convertFunctionTypeCWrapper(type);
143145
if (resultIsNowArg)
@@ -202,7 +204,8 @@ static void wrapExternalFunction(OpBuilder &builder, Location loc,
202204
assert(wrapperType && "unexpected type conversion failure");
203205

204206
SmallVector<NamedAttribute, 4> attributes;
205-
filterFuncAttributes(funcOp, /*filterArgAndResAttrs=*/false, attributes);
207+
filterFuncAttributes(funcOp->getAttrs(), /*filterArgAndResAttrs=*/false,
208+
attributes);
206209

207210
if (resultIsNowArg)
208211
prependResAttrsToArgAttrs(builder, attributes, funcOp.getNumArguments());
@@ -301,7 +304,8 @@ struct FuncOpConversionBase : public ConvertOpToLLVMPattern<func::FuncOp> {
301304
// Propagate argument/result attributes to all converted arguments/result
302305
// obtained after converting a given original argument/result.
303306
SmallVector<NamedAttribute, 4> attributes;
304-
filterFuncAttributes(funcOp, /*filterArgAndResAttrs=*/true, attributes);
307+
filterFuncAttributes(funcOp->getAttrs(), /*filterArgAndResAttrs=*/true,
308+
attributes);
305309
if (ArrayAttr resAttrDicts = funcOp.getAllResultAttrs()) {
306310
assert(!resAttrDicts.empty() && "expected array to be non-empty");
307311
auto newResAttrDicts =

0 commit comments

Comments
 (0)