xref: /llvm-project/mlir/examples/transform/Ch3/include/MyExtension.td (revision 5a9bdd85ee4d8527e2cedf44f3ce26ff414f9b6a)
168ae0d78SAlex Zinenko//===-- MyExtension.td - Transform dialect tutorial --------*- tablegen -*-===//
268ae0d78SAlex Zinenko//
368ae0d78SAlex Zinenko// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468ae0d78SAlex Zinenko// See https://llvm.org/LICENSE.txt for license information.
568ae0d78SAlex Zinenko// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668ae0d78SAlex Zinenko//
768ae0d78SAlex Zinenko//===----------------------------------------------------------------------===//
868ae0d78SAlex Zinenko//
968ae0d78SAlex Zinenko// This file defines Transform dialect extension operations used in the
1068ae0d78SAlex Zinenko// Chapter 3 of the Transform dialect tutorial.
1168ae0d78SAlex Zinenko//
1268ae0d78SAlex Zinenko//===----------------------------------------------------------------------===//
1368ae0d78SAlex Zinenko
1468ae0d78SAlex Zinenko#ifndef MY_EXTENSION
1568ae0d78SAlex Zinenko#define MY_EXTENSION
1668ae0d78SAlex Zinenko
1768ae0d78SAlex Zinenkoinclude "MyExtensionTypes.td"
1868ae0d78SAlex Zinenkoinclude "mlir/Dialect/Transform/IR/TransformDialect.td"
19*5a9bdd85SOleksandr "Alex" Zinenkoinclude "mlir/Dialect/Transform/Interfaces/TransformInterfaces.td"
2068ae0d78SAlex Zinenkoinclude "mlir/Dialect/Transform/IR/TransformTypes.td"
2168ae0d78SAlex Zinenkoinclude "mlir/IR/OpBase.td"
2268ae0d78SAlex Zinenkoinclude "mlir/Interfaces/SideEffectInterfaces.td"
2368ae0d78SAlex Zinenko
2468ae0d78SAlex Zinenko// Define the new operation. By convention, prefix its name with the name of the dialect
2568ae0d78SAlex Zinenko// extension, "my.". The full operation name will be further prefixed with "transform.".
2668ae0d78SAlex Zinenkodef ChangeCallTargetOp : Op<Transform_Dialect, "my.change_call_target",
2768ae0d78SAlex Zinenko    // Indicate that the operation implements the required TransformOpInterface and
2868ae0d78SAlex Zinenko    // MemoryEffectsOpInterface. Use the TransformEach trait to provide the
2968ae0d78SAlex Zinenko    // implementation for TransformOpInterface.
3068ae0d78SAlex Zinenko    [TransformOpInterface, TransformEachOpTrait,
3168ae0d78SAlex Zinenko     DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
3268ae0d78SAlex Zinenko  // Provide a brief and a full description. It is recommended that the latter describes
3368ae0d78SAlex Zinenko  // the effects on the operands and how the operation processes various failure modes.
3468ae0d78SAlex Zinenko  let summary = "Changes the callee of a call operation to the specified one";
3568ae0d78SAlex Zinenko  let description = [{
3668ae0d78SAlex Zinenko    For each `func.call` payload operation associated with the handle, changes its
3768ae0d78SAlex Zinenko    callee to be the symbol whose name is provided as an attribute to this operation.
3868ae0d78SAlex Zinenko
3968ae0d78SAlex Zinenko    Generates a silenceable failure if the operand is associated with payload operations
4068ae0d78SAlex Zinenko    that are not `func.call`.
4168ae0d78SAlex Zinenko    Only reads the operand.
4268ae0d78SAlex Zinenko  }];
4368ae0d78SAlex Zinenko
4468ae0d78SAlex Zinenko  // The arguments include the handle to the payload operations and the attribute that
4568ae0d78SAlex Zinenko  // specifies the new callee. The handle must implement TransformHandleTypeInterface.
4668ae0d78SAlex Zinenko  // We use a string attribute as the symbol may not exist in the transform IR so the
4768ae0d78SAlex Zinenko  // verification may fail.
4868ae0d78SAlex Zinenko  let arguments = (ins
4968ae0d78SAlex Zinenko    // Specify the type constraint on the input accepting only `func.call` payload
5068ae0d78SAlex Zinenko    // operations.
5168ae0d78SAlex Zinenko    Transform_ConcreteOpType<"func.call">:$call,
5268ae0d78SAlex Zinenko    StrAttr:$new_target);
5368ae0d78SAlex Zinenko
5468ae0d78SAlex Zinenko  // The results are empty as the transformation does not produce any new payload.
5568ae0d78SAlex Zinenko  let results = (outs);
5668ae0d78SAlex Zinenko
5768ae0d78SAlex Zinenko  // Provide nice syntax.
5868ae0d78SAlex Zinenko  let assemblyFormat = "$call `,` $new_target attr-dict `:` qualified(type($call))";
5968ae0d78SAlex Zinenko
6068ae0d78SAlex Zinenko  // Declare the function implementing the interface for a single payload operation.
6168ae0d78SAlex Zinenko  let extraClassDeclaration = [{
6268ae0d78SAlex Zinenko    ::mlir::DiagnosedSilenceableFailure applyToOne(
63c63d2b2cSMatthias Springer        ::mlir::transform::TransformRewriter &rewriter,
6468ae0d78SAlex Zinenko        ::mlir::func::CallOp call,
6568ae0d78SAlex Zinenko        ::mlir::transform::ApplyToEachResultList &results,
6668ae0d78SAlex Zinenko        ::mlir::transform::TransformState &state);
6768ae0d78SAlex Zinenko  }];
6868ae0d78SAlex Zinenko}
6968ae0d78SAlex Zinenko
7068ae0d78SAlex Zinenko// Define another transform operation.
7168ae0d78SAlex Zinenkodef CallToOp : Op<Transform_Dialect, "my.call_to_op",
7268ae0d78SAlex Zinenko     // Indicate that the operation implements the required TransformOpInterface.
7368ae0d78SAlex Zinenko     // Use the TransformEach trait to provide implementation of this interface.
7468ae0d78SAlex Zinenko    [TransformOpInterface, TransformEachOpTrait,
7568ae0d78SAlex Zinenko     // Indicate that the operation implements the required MemoryEffectsOpInterface.
7668ae0d78SAlex Zinenko     // Use the FunctionalStyle trait to provide the implementation for this interface.
7768ae0d78SAlex Zinenko     MemoryEffectsOpInterface, FunctionalStyleTransformOpTrait]> {
7868ae0d78SAlex Zinenko  // Summary and description omitted for brevity.
7968ae0d78SAlex Zinenko
8068ae0d78SAlex Zinenko  // The argument is the handle to the payload operations.
8168ae0d78SAlex Zinenko  let arguments = (ins CallOpInterfaceHandle:$call);
8268ae0d78SAlex Zinenko
8368ae0d78SAlex Zinenko  // The result is the handle to the payload operations produced during the
8468ae0d78SAlex Zinenko  // transformation.
8568ae0d78SAlex Zinenko  let results = (outs TransformHandleTypeInterface:$transformed);
8668ae0d78SAlex Zinenko
8768ae0d78SAlex Zinenko  // Provide nice syntax.
8868ae0d78SAlex Zinenko  let assemblyFormat = "$call attr-dict `:` functional-type(operands, results)";
8968ae0d78SAlex Zinenko
9068ae0d78SAlex Zinenko  // Declare the function implementing the interface for a single payload operation.
9168ae0d78SAlex Zinenko  let extraClassDeclaration = [{
9268ae0d78SAlex Zinenko    ::mlir::DiagnosedSilenceableFailure applyToOne(
93c63d2b2cSMatthias Springer        ::mlir::transform::TransformRewriter &rewriter,
9468ae0d78SAlex Zinenko        ::mlir::CallOpInterface call,
9568ae0d78SAlex Zinenko        ::mlir::transform::ApplyToEachResultList &results,
9668ae0d78SAlex Zinenko        ::mlir::transform::TransformState &state);
9768ae0d78SAlex Zinenko  }];
9868ae0d78SAlex Zinenko}
9968ae0d78SAlex Zinenko
10068ae0d78SAlex Zinenko#endif // MY_EXTENSION
101