Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir] share argument attributes interface between calls and callables #123176

Merged
merged 7 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion flang/include/flang/Optimizer/Dialect/CUF/CUFOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ def cuf_KernelLaunchOp : cuf_Op<"kernel_launch", [CallOpInterface,
I32:$block_z,
Optional<I32>:$bytes,
Optional<I32>:$stream,
Variadic<AnyType>:$args
Variadic<AnyType>:$args,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs
);

let assemblyFormat = [{
Expand Down
4 changes: 4 additions & 0 deletions flang/include/flang/Optimizer/Dialect/FIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -2432,6 +2432,8 @@ def fir_CallOp : fir_Op<"call",
let arguments = (ins
OptionalAttr<SymbolRefAttr>:$callee,
Variadic<AnyType>:$args,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs,
OptionalAttr<fir_FortranProcedureFlagsAttr>:$procedure_attrs,
DefaultValuedAttr<Arith_FastMathAttr,
"::mlir::arith::FastMathFlags::none">:$fastmath
Expand Down Expand Up @@ -2518,6 +2520,8 @@ def fir_DispatchOp : fir_Op<"dispatch", []> {
fir_ClassType:$object,
Variadic<AnyType>:$args,
OptionalAttr<I32Attr>:$pass_arg_pos,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs,
OptionalAttr<fir_FortranProcedureFlagsAttr>:$procedure_attrs
);

Expand Down
12 changes: 8 additions & 4 deletions flang/lib/Lower/ConvertCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,8 @@ Fortran::lower::genCallOpAndResult(

builder.create<cuf::KernelLaunchOp>(
loc, funcType.getResults(), funcSymbolAttr, grid_x, grid_y, grid_z,
block_x, block_y, block_z, bytes, stream, operands);
block_x, block_y, block_z, bytes, stream, operands,
/*arg_attrs=*/nullptr, /*res_attrs=*/nullptr);
callNumResults = 0;
} else if (caller.requireDispatchCall()) {
// Procedure call requiring a dynamic dispatch. Call is created with
Expand All @@ -621,7 +622,8 @@ Fortran::lower::genCallOpAndResult(
dispatch = builder.create<fir::DispatchOp>(
loc, funcType.getResults(), builder.getStringAttr(procName),
caller.getInputs()[*passArg], operands,
builder.getI32IntegerAttr(*passArg), procAttrs);
builder.getI32IntegerAttr(*passArg), /*arg_attrs=*/nullptr,
/*res_attrs=*/nullptr, procAttrs);
} else {
// NOPASS
const Fortran::evaluate::Component *component =
Expand All @@ -636,15 +638,17 @@ Fortran::lower::genCallOpAndResult(
passObject = builder.create<fir::LoadOp>(loc, passObject);
dispatch = builder.create<fir::DispatchOp>(
loc, funcType.getResults(), builder.getStringAttr(procName),
passObject, operands, nullptr, procAttrs);
passObject, operands, nullptr, /*arg_attrs=*/nullptr,
/*res_attrs=*/nullptr, procAttrs);
}
callNumResults = dispatch.getNumResults();
if (callNumResults != 0)
callResult = dispatch.getResult(0);
} else {
// Standard procedure call with fir.call.
auto call = builder.create<fir::CallOp>(
loc, funcType.getResults(), funcSymbolAttr, operands, procAttrs);
loc, funcType.getResults(), funcSymbolAttr, operands,
/*arg_attrs=*/nullptr, /*res_attrs=*/nullptr, procAttrs);

callNumResults = call.getNumResults();
if (callNumResults != 0)
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Optimizer/CodeGen/TargetRewrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
newOpers.insert(newOpers.end(), trailingOpers.begin(), trailingOpers.end());

llvm::SmallVector<mlir::Value, 1> newCallResults;
// TODO propagate/update call argument and result attributes.
if constexpr (std::is_same_v<std::decay_t<A>, mlir::gpu::LaunchFuncOp>) {
auto newCall = rewriter->create<A>(
loc, callOp.getKernel(), callOp.getGridSizeOperandValues(),
Expand Down Expand Up @@ -557,6 +558,7 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
loc, newResTys, rewriter->getStringAttr(callOp.getMethod()),
callOp.getOperands()[0], newOpers,
rewriter->getI32IntegerAttr(*callOp.getPassArgPos() + passArgShift),
/*arg_attrs=*/nullptr, /*res_attrs=*/nullptr,
callOp.getProcedureAttrsAttr());
if (wrap)
newCallResults.push_back((*wrap)(dispatchOp.getOperation()));
Expand Down
3 changes: 3 additions & 0 deletions flang/lib/Optimizer/Transforms/AbstractResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class CallConversion : public mlir::OpRewritePattern<Op> {
newResultTypes.emplace_back(getVoidPtrType(result.getContext()));

Op newOp;
// TODO: propagate argument and result attributes (need to be shifted).
// fir::CallOp specific handling.
if constexpr (std::is_same_v<Op, fir::CallOp>) {
if (op.getCallee()) {
Expand Down Expand Up @@ -189,9 +190,11 @@ class CallConversion : public mlir::OpRewritePattern<Op> {
if (op.getPassArgPos())
passArgPos =
rewriter.getI32IntegerAttr(*op.getPassArgPos() + passArgShift);
// TODO: propagate argument and result attributes (need to be shifted).
newOp = rewriter.create<fir::DispatchOp>(
loc, newResultTypes, rewriter.getStringAttr(op.getMethod()),
op.getOperands()[0], newOperands, passArgPos,
/*arg_attrs=*/nullptr, /*res_attrs=*/nullptr,
op.getProcedureAttrsAttr());
}

Expand Down
5 changes: 3 additions & 2 deletions flang/lib/Optimizer/Transforms/PolymorphicOpConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ struct DispatchOpConv : public OpConversionPattern<fir::DispatchOp> {
// Make the call.
llvm::SmallVector<mlir::Value> args{funcPtr};
args.append(dispatch.getArgs().begin(), dispatch.getArgs().end());
rewriter.replaceOpWithNewOp<fir::CallOp>(dispatch, resTypes, nullptr, args,
dispatch.getProcedureAttrsAttr());
rewriter.replaceOpWithNewOp<fir::CallOp>(
dispatch, resTypes, nullptr, args, dispatch.getArgAttrsAttr(),
dispatch.getResAttrsAttr(), dispatch.getProcedureAttrsAttr());
return mlir::success();
}

Expand Down
7 changes: 6 additions & 1 deletion mlir/docs/Interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -753,10 +753,15 @@ interface section goes as follows:
- (`C++ class` -- `ODS class`(if applicable))

##### CallInterfaces

* `CallOpInterface` - Used to represent operations like 'call'
- `CallInterfaceCallable getCallableForCallee()`
- `void setCalleeFromCallable(CallInterfaceCallable)`
- `ArrayAttr getArgAttrsAttr()`
- `ArrayAttr getResAttrsAttr()`
- `void setArgAttrsAttr(ArrayAttr)`
- `void setResAttrsAttr(ArrayAttr)`
- `Attribute removeArgAttrsAttr()`
- `Attribute removeResAttrsAttr()`
* `CallableOpInterface` - Used to represent the target callee of call.
- `Region * getCallableRegion()`
- `ArrayRef<Type> getArgumentTypes()`
Expand Down
7 changes: 6 additions & 1 deletion mlir/examples/toy/Ch4/include/toy/Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ def GenericCallOp : Toy_Op<"generic_call",

// The generic call operation takes a symbol reference attribute as the
// callee, and inputs for the call.
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
let arguments = (ins
FlatSymbolRefAttr:$callee,
Variadic<F64Tensor>:$inputs,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs
);

// The generic call operation returns a single value of TensorType.
let results = (outs F64Tensor);
Expand Down
7 changes: 6 additions & 1 deletion mlir/examples/toy/Ch5/include/toy/Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ def GenericCallOp : Toy_Op<"generic_call",

// The generic call operation takes a symbol reference attribute as the
// callee, and inputs for the call.
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
let arguments = (ins
FlatSymbolRefAttr:$callee,
Variadic<F64Tensor>:$inputs,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs
);

// The generic call operation returns a single value of TensorType.
let results = (outs F64Tensor);
Expand Down
7 changes: 6 additions & 1 deletion mlir/examples/toy/Ch6/include/toy/Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ def GenericCallOp : Toy_Op<"generic_call",

// The generic call operation takes a symbol reference attribute as the
// callee, and inputs for the call.
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
let arguments = (ins
FlatSymbolRefAttr:$callee,
Variadic<F64Tensor>:$inputs,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs
);

// The generic call operation returns a single value of TensorType.
let results = (outs F64Tensor);
Expand Down
10 changes: 8 additions & 2 deletions mlir/examples/toy/Ch7/include/toy/Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,12 @@ def GenericCallOp : Toy_Op<"generic_call",

// The generic call operation takes a symbol reference attribute as the
// callee, and inputs for the call.
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<Toy_Type>:$inputs);
let arguments = (ins
FlatSymbolRefAttr:$callee,
Variadic<Toy_Type>:$inputs,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs
);

// The generic call operation returns a single value of TensorType or
// StructType.
Expand All @@ -250,7 +255,8 @@ def GenericCallOp : Toy_Op<"generic_call",

// Add custom build methods for the generic call operation.
let builders = [
OpBuilder<(ins "StringRef":$callee, "ArrayRef<Value>":$arguments)>
OpBuilder<(ins "Type":$result_type, "StringRef":$callee,
"ArrayRef<Value>":$arguments)>
];
}

Expand Down
5 changes: 3 additions & 2 deletions mlir/examples/toy/Ch7/mlir/Dialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,10 @@ void FuncOp::print(mlir::OpAsmPrinter &p) {
//===----------------------------------------------------------------------===//

void GenericCallOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
StringRef callee, ArrayRef<mlir::Value> arguments) {
mlir::Type resultType, StringRef callee,
ArrayRef<mlir::Value> arguments) {
// Generic call always returns an unranked Tensor initially.
state.addTypes(UnrankedTensorType::get(builder.getF64Type()));
state.addTypes(resultType);
state.addOperands(arguments);
state.addAttribute("callee",
mlir::SymbolRefAttr::get(builder.getContext(), callee));
Expand Down
3 changes: 1 addition & 2 deletions mlir/examples/toy/Ch7/mlir/MLIRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,7 @@ class MLIRGenImpl {
}
mlir::toy::FuncOp calledFunc = calledFuncIt->second;
return builder.create<GenericCallOp>(
location, calledFunc.getFunctionType().getResult(0),
mlir::SymbolRefAttr::get(builder.getContext(), callee), operands);
location, calledFunc.getFunctionType().getResult(0), callee, operands);
}

/// Emit a print expression. It emits specific operations for two builtins:
Expand Down
8 changes: 7 additions & 1 deletion mlir/include/mlir/Dialect/Async/IR/AsyncOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,13 @@ def Async_CallOp : Async_Op<"call",
```
}];

let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyType>:$operands);
let arguments = (ins
FlatSymbolRefAttr:$callee,
Variadic<AnyType>:$operands,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs
);

let results = (outs Variadic<Async_AnyValueOrTokenType>);

let builders = [
Expand Down
8 changes: 7 additions & 1 deletion mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,13 @@ def EmitC_CallOp : EmitC_Op<"call",
%2 = emitc.call @my_add(%0, %1) : (f32, f32) -> f32
```
}];
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<EmitCType>:$operands);
let arguments = (ins
FlatSymbolRefAttr:$callee,
Variadic<EmitCType>:$operands,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs
);

let results = (outs Variadic<EmitCType>);

let builders = [
Expand Down
31 changes: 27 additions & 4 deletions mlir/include/mlir/Dialect/Func/IR/FuncOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ def CallOp : Func_Op<"call",
```
}];

let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyType>:$operands,
UnitAttr:$no_inline);
let arguments = (ins
FlatSymbolRefAttr:$callee,
Variadic<AnyType>:$operands,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs,
UnitAttr:$no_inline
);

let results = (outs Variadic<AnyType>);

let builders = [
Expand All @@ -73,6 +79,18 @@ def CallOp : Func_Op<"call",
CArg<"ValueRange", "{}">:$operands), [{
build($_builder, $_state, StringAttr::get($_builder.getContext(), callee),
results, operands);
}]>,
OpBuilder<(ins "TypeRange":$results, "FlatSymbolRefAttr":$callee,
CArg<"ValueRange", "{}">:$operands), [{
build($_builder, $_state, callee, results, operands);
}]>,
OpBuilder<(ins "TypeRange":$results, "StringAttr":$callee,
CArg<"ValueRange", "{}">:$operands), [{
build($_builder, $_state, callee, results, operands);
}]>,
OpBuilder<(ins "TypeRange":$results, "StringRef":$callee,
CArg<"ValueRange", "{}">:$operands), [{
build($_builder, $_state, callee, results, operands);
}]>];

let extraClassDeclaration = [{
Expand Down Expand Up @@ -136,8 +154,13 @@ def CallIndirectOp : Func_Op<"call_indirect", [
```
}];

let arguments = (ins FunctionType:$callee,
Variadic<AnyType>:$callee_operands);
let arguments = (ins
FunctionType:$callee,
Variadic<AnyType>:$callee_operands,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs
);

let results = (outs Variadic<AnyType>:$results);

let builders = [
Expand Down
6 changes: 5 additions & 1 deletion mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,8 @@ def LLVM_InvokeOp : LLVM_Op<"invoke", [
OptionalAttr<TypeAttrOf<LLVM_FunctionType>>:$var_callee_type,
OptionalAttr<FlatSymbolRefAttr>:$callee,
Variadic<LLVM_Type>:$callee_operands,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs,
Variadic<LLVM_Type>:$normalDestOperands,
Variadic<LLVM_Type>:$unwindDestOperands,
OptionalAttr<DenseI32ArrayAttr>:$branch_weights,
Expand Down Expand Up @@ -755,7 +757,9 @@ def LLVM_CallOp : LLVM_MemAccessOpBase<"call",
VariadicOfVariadic<LLVM_Type,
"op_bundle_sizes">:$op_bundle_operands,
DenseI32ArrayAttr:$op_bundle_sizes,
OptionalAttr<ArrayAttr>:$op_bundle_tags);
OptionalAttr<ArrayAttr>:$op_bundle_tags,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs);
// Append the aliasing related attributes defined in LLVM_MemAccessOpBase.
let arguments = !con(args, aliasAttrs);
let results = (outs Optional<LLVM_Type>:$result);
Expand Down
13 changes: 12 additions & 1 deletion mlir/include/mlir/Dialect/SPIRV/IR/SPIRVControlFlowOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,24 @@ def SPIRV_FunctionCallOp : SPIRV_Op<"FunctionCall", [

let arguments = (ins
FlatSymbolRefAttr:$callee,
Variadic<SPIRV_Type>:$arguments
Variadic<SPIRV_Type>:$arguments,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs
);

let results = (outs
Optional<SPIRV_Type>:$return_value
);

let builders = [
OpBuilder<(ins "Type":$returnType, "FlatSymbolRefAttr":$callee,
"ValueRange":$arguments),
[{
build($_builder, $_state, returnType, callee, arguments,
/*arg_attrs=*/nullptr, /*res_attrs=*/nullptr);
}]>
];

let autogenSerialization = 0;

let assemblyFormat = [{
Expand Down
4 changes: 3 additions & 1 deletion mlir/include/mlir/Dialect/Transform/IR/TransformOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,9 @@ def IncludeOp : TransformDialectOp<"include",

let arguments = (ins SymbolRefAttr:$target,
FailurePropagationMode:$failure_propagation_mode,
Variadic<Transform_AnyHandleOrParamType>:$operands);
Variadic<Transform_AnyHandleOrParamType>:$operands,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs);
let results = (outs Variadic<Transform_AnyHandleOrParamType>:$results);

let assemblyFormat =
Expand Down
Loading
Loading