Skip to content

Commit

Permalink
[mlir][transform] Add an op for replacing values with function calls (#…
Browse files Browse the repository at this point in the history
…78398)

Adds `transform.func.cast_and_call` that takes a set of inputs and
outputs and replaces the uses of those outputs with a call to a function
at a specified insertion point.

The idea with this operation is to allow users to author independent IR
outside of a to-be-compiled module, and then match and replace a slice
of the program with a call to the external function.

Additionally adds a mechanism for populating a type converter with a set
of conversion materialization functions that allow insertion of
casts on the inputs/outputs to and from the types of the function
signature.
  • Loading branch information
qedawkins authored Jan 19, 2024
1 parent 0784b1e commit 42b1603
Show file tree
Hide file tree
Showing 10 changed files with 538 additions and 5 deletions.
72 changes: 72 additions & 0 deletions mlir/include/mlir/Dialect/Func/TransformOps/FuncTransformOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
include "mlir/Dialect/Transform/IR/TransformDialect.td"
include "mlir/Dialect/Transform/IR/TransformInterfaces.td"
include "mlir/Dialect/Transform/IR/TransformTypes.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/IR/RegionKindInterface.td"
include "mlir/IR/OpBase.td"

def ApplyFuncToLLVMConversionPatternsOp : Op<Transform_Dialect,
Expand All @@ -26,4 +28,74 @@ def ApplyFuncToLLVMConversionPatternsOp : Op<Transform_Dialect,
let assemblyFormat = "attr-dict";
}

def CastAndCallOp : Op<Transform_Dialect,
"func.cast_and_call",
[DeclareOpInterfaceMethods<TransformOpInterface>,
DeclareOpInterfaceMethods<MemoryEffectsOpInterface>,
AttrSizedOperandSegments,
ReportTrackingListenerFailuresOpTrait]
# GraphRegionNoTerminator.traits> {
let summary = "Casts values to the signature of a function and replaces them "
"with a call";
let description = [{
This transform takes value handles to a set of `inputs` and `outputs` and
attempts to cast them to the function signature of the attached function
op, then builds a call to the function and replaces the users of the
outputs. It is the responsibility of the user to ensure that the slice of
the program replaced by this operation makes sense, i.e. there is no
verification that the inputs to this operation have any relation to the
outputs outside of basic dominance requirements needed for the call.

The casting materialization functions are specified in the graph region of
this op. They must implement the `TypeConverterBuilderOpInterface`. The
order of ops within the region is irrelevant.

The target function can be specified by a symbol name or by a handle to the
operation.

This transform only reads the operand handles and only replaces the users of
the outputs with the results of the call. No handles are consumed and no
operations are removed. Users are expected to run cleanup separately if
desired.

Warning: The replacement of the uses of the outputs could invalidate certain
restricted value handle types (e.g. `transform.block_arg` if it existed, by
replacing the use with something not coming from a block argument). The
value will still exist in such cases but wouldn't verify against the type.
See the discussion here for more information:
https://github.com/llvm/llvm-project/pull/78398#discussion_r1455070087

This transform will emit a silenceable failure if:
- The set of outputs isn't unique
- The handle for the insertion point does not include exactly one operation
- The insertion point op does not dominate any of the output users
- The insertion point op is not dominated by any of the inputs
- The function signature does not match the number of inputs/outputs

This transform will emit a definite failure if it fails to resolve the
target function, or if it fails to materialize the conversion casts of
either the inputs to the function argument types, or the call results to
the output types.
}];

let arguments = (ins
TransformHandleTypeInterface:$insertion_point,
UnitAttr:$insert_after,
Optional<TransformValueHandleTypeInterface>:$inputs,
Optional<TransformValueHandleTypeInterface>:$outputs,
OptionalAttr<SymbolRefAttr>:$function_name,
Optional<TransformHandleTypeInterface>:$function);
let results = (outs TransformHandleTypeInterface:$result);
let regions = (region MaxSizedRegion<1>:$conversions);

let assemblyFormat = [{
($function_name^)? ($function^)?
( `(` $inputs^ `)` )?
( `->` $outputs^ )?
(`after` $insert_after^):(`before`)? $insertion_point
($conversions^)? attr-dict `:` functional-type(operands, results)
}];
let hasVerifier = 1;
}

#endif // FUNC_TRANSFORM_OPS
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ include "mlir/IR/OpBase.td"
def MemrefToLLVMTypeConverterOp : Op<Transform_Dialect,
"apply_conversion_patterns.memref.memref_to_llvm_type_converter",
[DeclareOpInterfaceMethods<TypeConverterBuilderOpInterface,
["getTypeConverterType"]>]> {
["getTypeConverter",
"getTypeConverterType"]>]> {
let description = [{
This operation provides an "LLVMTypeConverter" that lowers memref types to
LLVM types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,22 @@ def MakeLoopIndependentOp
}];
}

def TypeConversionCastShapeDynamicDimsOp : Op<Transform_Dialect,
"type_conversion.tensor.cast_shape_dynamic_dims",
[DeclareOpInterfaceMethods<TypeConverterBuilderOpInterface,
["populateTypeMaterializations"]>]> {
let description = [{
Populates a type converter with conversion materialization functions that
cast a tensor value between two cast-compatible tensors. See `tensor.cast`
for more information on cast compatibility between tensors.

If `ignore_dynamic_info` is not set, this will set an additional constraint
that source materializations do not cast dynamic dimensions to static ones.
}];
let arguments = (ins UnitAttr:$ignore_dynamic_info);

let assemblyFormat =
"(`ignore_dynamic_info` $ignore_dynamic_info^)? attr-dict";
}

#endif // TENSOR_TRANSFORM_OPS
27 changes: 24 additions & 3 deletions mlir/include/mlir/Dialect/Transform/IR/TransformInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,14 @@ def TypeConverterBuilderOpInterface
: OpInterface<"TypeConverterBuilderOpInterface"> {
let description = [{
This interface should be implemented by ops that specify a type converter
for a dialect conversion. Such ops can be used with
"apply_conversion_patterns".
for a dialect conversion, or to populate a type converter with
conversions.

When such ops are intended to be used with "apply_conversion_patterns" or
other operations that expect a type converter, a non-default implementation
of `getTypeConverter` should be implemented. For use with "cast_and_call"
like ops that construct a type converter iteratively, non-default
`populateTypeMaterializations` should be implemented.
}];

let cppNamespace = "::mlir::transform";
Expand All @@ -297,7 +303,11 @@ def TypeConverterBuilderOpInterface
}],
/*returnType=*/"std::unique_ptr<::mlir::TypeConverter>",
/*name=*/"getTypeConverter",
/*arguments=*/(ins)
/*arguments=*/(ins),
/*methodBody=*/"",
/*defaultImplementation=*/[{
return std::make_unique<::mlir::TypeConverter>();
}]
>,
StaticInterfaceMethod<
/*desc=*/[{
Expand All @@ -310,6 +320,17 @@ def TypeConverterBuilderOpInterface
/*methodBody=*/"",
/*defaultImplementation=*/[{ return "TypeConverter"; }]
>,
InterfaceMethod<
/*desc=*/[{
Populate the given type converter with source/target materialization
functions.
}],
/*returnType=*/"void",
/*name=*/"populateTypeMaterializations",
/*arguments=*/(ins "::mlir::TypeConverter &":$converter),
/*methodBody=*/"",
/*defaultImplementation=*/[{ return; }]
>,
];
}

Expand Down
191 changes: 191 additions & 0 deletions mlir/lib/Dialect/Func/TransformOps/FuncTransformOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "mlir/Dialect/Transform/IR/TransformDialect.h"
#include "mlir/Dialect/Transform/IR/TransformInterfaces.h"
#include "mlir/Dialect/Transform/IR/TransformOps.h"
#include "mlir/Transforms/DialectConversion.h"

using namespace mlir;

Expand All @@ -36,6 +37,196 @@ transform::ApplyFuncToLLVMConversionPatternsOp::verifyTypeConverter(
return success();
}

//===----------------------------------------------------------------------===//
// CastAndCallOp
//===----------------------------------------------------------------------===//

DiagnosedSilenceableFailure
transform::CastAndCallOp::apply(transform::TransformRewriter &rewriter,
transform::TransformResults &results,
transform::TransformState &state) {
SmallVector<Value> inputs;
if (getInputs())
llvm::append_range(inputs, state.getPayloadValues(getInputs()));

SetVector<Value> outputs;
if (getOutputs()) {
for (auto output : state.getPayloadValues(getOutputs()))
outputs.insert(output);

// Verify that the set of output values to be replaced is unique.
if (outputs.size() !=
llvm::range_size(state.getPayloadValues(getOutputs()))) {
return emitSilenceableFailure(getLoc())
<< "cast and call output values must be unique";
}
}

// Get the insertion point for the call.
auto insertionOps = state.getPayloadOps(getInsertionPoint());
if (!llvm::hasSingleElement(insertionOps)) {
return emitSilenceableFailure(getLoc())
<< "Only one op can be specified as an insertion point";
}
bool insertAfter = getInsertAfter();
Operation *insertionPoint = *insertionOps.begin();

// Check that all inputs dominate the insertion point, and the insertion
// point dominates all users of the outputs.
DominanceInfo dom(insertionPoint);
for (Value output : outputs) {
for (Operation *user : output.getUsers()) {
// If we are inserting after the insertion point operation, the
// insertion point operation must properly dominate the user. Otherwise
// basic dominance is enough.
bool doesDominate = insertAfter
? dom.properlyDominates(insertionPoint, user)
: dom.dominates(insertionPoint, user);
if (!doesDominate) {
return emitDefiniteFailure()
<< "User " << user << " is not dominated by insertion point "
<< insertionPoint;
}
}
}

for (Value input : inputs) {
// If we are inserting before the insertion point operation, the
// input must properly dominate the insertion point operation. Otherwise
// basic dominance is enough.
bool doesDominate = insertAfter
? dom.dominates(input, insertionPoint)
: dom.properlyDominates(input, insertionPoint);
if (!doesDominate) {
return emitDefiniteFailure()
<< "input " << input << " does not dominate insertion point "
<< insertionPoint;
}
}

// Get the function to call. This can either be specified by symbol or as a
// transform handle.
func::FuncOp targetFunction = nullptr;
if (getFunctionName()) {
targetFunction = SymbolTable::lookupNearestSymbolFrom<func::FuncOp>(
insertionPoint, *getFunctionName());
if (!targetFunction) {
return emitDefiniteFailure()
<< "unresolved symbol " << *getFunctionName();
}
} else if (getFunction()) {
auto payloadOps = state.getPayloadOps(getFunction());
if (!llvm::hasSingleElement(payloadOps)) {
return emitDefiniteFailure() << "requires a single function to call";
}
targetFunction = dyn_cast<func::FuncOp>(*payloadOps.begin());
if (!targetFunction) {
return emitDefiniteFailure() << "invalid non-function callee";
}
} else {
llvm_unreachable("Invalid CastAndCall op without a function to call");
return emitDefiniteFailure();
}

// Verify that the function argument and result lengths match the inputs and
// outputs given to this op.
if (targetFunction.getNumArguments() != inputs.size()) {
return emitSilenceableFailure(targetFunction.getLoc())
<< "mismatch between number of function arguments "
<< targetFunction.getNumArguments() << " and number of inputs "
<< inputs.size();
}
if (targetFunction.getNumResults() != outputs.size()) {
return emitSilenceableFailure(targetFunction.getLoc())
<< "mismatch between number of function results "
<< targetFunction->getNumResults() << " and number of outputs "
<< outputs.size();
}

// Gather all specified converters.
mlir::TypeConverter converter;
if (!getRegion().empty()) {
for (Operation &op : getRegion().front()) {
cast<transform::TypeConverterBuilderOpInterface>(&op)
.populateTypeMaterializations(converter);
}
}

if (insertAfter)
rewriter.setInsertionPointAfter(insertionPoint);
else
rewriter.setInsertionPoint(insertionPoint);

for (auto [input, type] :
llvm::zip_equal(inputs, targetFunction.getArgumentTypes())) {
if (input.getType() != type) {
Value newInput = converter.materializeSourceConversion(
rewriter, input.getLoc(), type, input);
if (!newInput) {
return emitDefiniteFailure() << "Failed to materialize conversion of "
<< input << " to type " << type;
}
input = newInput;
}
}

auto callOp = rewriter.create<func::CallOp>(insertionPoint->getLoc(),
targetFunction, inputs);

// Cast the call results back to the expected types. If any conversions fail
// this is a definite failure as the call has been constructed at this point.
for (auto [output, newOutput] :
llvm::zip_equal(outputs, callOp.getResults())) {
Value convertedOutput = newOutput;
if (output.getType() != newOutput.getType()) {
convertedOutput = converter.materializeTargetConversion(
rewriter, output.getLoc(), output.getType(), newOutput);
if (!convertedOutput) {
return emitDefiniteFailure()
<< "Failed to materialize conversion of " << newOutput
<< " to type " << output.getType();
}
}
rewriter.replaceAllUsesExcept(output, convertedOutput, callOp);
}
results.set(cast<OpResult>(getResult()), {callOp});
return DiagnosedSilenceableFailure::success();
}

LogicalResult transform::CastAndCallOp::verify() {
if (!getRegion().empty()) {
for (Operation &op : getRegion().front()) {
if (!isa<transform::TypeConverterBuilderOpInterface>(&op)) {
InFlightDiagnostic diag = emitOpError()
<< "expected children ops to implement "
"TypeConverterBuilderOpInterface";
diag.attachNote(op.getLoc()) << "op without interface";
return diag;
}
}
}
if (!getFunction() && !getFunctionName()) {
return emitOpError() << "expected a function handle or name to call";
}
if (getFunction() && getFunctionName()) {
return emitOpError() << "function handle and name are mutually exclusive";
}
return success();
}

void transform::CastAndCallOp::getEffects(
SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
transform::onlyReadsHandle(getInsertionPoint(), effects);
if (getInputs())
transform::onlyReadsHandle(getInputs(), effects);
if (getOutputs())
transform::onlyReadsHandle(getOutputs(), effects);
if (getFunction())
transform::onlyReadsHandle(getFunction(), effects);
transform::producesHandle(getResult(), effects);
transform::modifiesPayload(effects);
}

//===----------------------------------------------------------------------===//
// Transform op registration
//===----------------------------------------------------------------------===//
Expand Down
Loading

0 comments on commit 42b1603

Please sign in to comment.