From 59b47651b7040835a93fceeb6241d1d288ff9dbe Mon Sep 17 00:00:00 2001 From: Christian Sigg Date: Mon, 17 Feb 2025 19:26:21 +0100 Subject: [PATCH] [mlir][tensor][linalg] Move RelayoutOpInterface from linalg back to tensor RelayoutOpInterface is used only to identify tensor.pack/tensor.unpack ops. This CL moves this interface back to the tensor dialect, to allow the removal of the linalg dialect dependency from the tensor dialect. This is a partial revert of https://github.com/llvm/llvm-project/pull/125823. --- mlir/include/mlir/Dialect/Linalg/IR/Linalg.h | 1 + .../Dialect/Linalg/IR/LinalgInterfaces.td | 10 ------- .../Dialect/Linalg/IR/LinalgRelayoutOps.td | 3 +- .../mlir/Dialect/Tensor/IR/CMakeLists.txt | 6 ++++ mlir/include/mlir/Dialect/Tensor/IR/Tensor.h | 6 ++++ .../Dialect/Tensor/IR/TensorInterfaces.td | 30 +++++++++++++++++++ mlir/lib/Dialect/Tensor/IR/CMakeLists.txt | 1 + mlir/lib/Dialect/Tensor/IR/TensorOps.cpp | 4 +-- .../llvm-project-overlay/mlir/BUILD.bazel | 21 +++++++++++++ 9 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 mlir/include/mlir/Dialect/Tensor/IR/TensorInterfaces.td diff --git a/mlir/include/mlir/Dialect/Linalg/IR/Linalg.h b/mlir/include/mlir/Dialect/Linalg/IR/Linalg.h index 57bf6305a469d..38a8d18e56b05 100644 --- a/mlir/include/mlir/Dialect/Linalg/IR/Linalg.h +++ b/mlir/include/mlir/Dialect/Linalg/IR/Linalg.h @@ -10,6 +10,7 @@ #define MLIR_DIALECT_LINALG_IR_LINALG_H #include "mlir/Bytecode/BytecodeOpInterface.h" +#include "mlir/Dialect/Tensor/IR/Tensor.h" #include "mlir/Dialect/Utils/ReshapeOpsUtils.h" #include "mlir/Dialect/Utils/StructuredOpsUtils.h" #include "mlir/IR/AffineExpr.h" diff --git a/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td b/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td index 247afc141c180..dbc1ac60e0973 100644 --- a/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td +++ b/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td @@ -178,16 +178,6 @@ def LinalgConvolutionOpInterface : OpInterface<"ConvolutionOpInterface"> { ]; } -def LinalgRelayoutOpInterface : OpInterface<"RelayoutOpInterface"> { - let description = [{ - A Linalg relayout-op is either linalg.pack or linalg.unpack. - - While we could extend this interface with methods from Linalg_RelayoutOp, - this is currently not needed and left as a TODO. - }]; - let cppNamespace = "::mlir::linalg"; -} - def LinalgFillOpInterface : OpInterface<"FillOpInterface"> { let description = [{ A fill operation is defined in general terms: diff --git a/mlir/include/mlir/Dialect/Linalg/IR/LinalgRelayoutOps.td b/mlir/include/mlir/Dialect/Linalg/IR/LinalgRelayoutOps.td index a08a778fc25e1..2b27f2c908c64 100644 --- a/mlir/include/mlir/Dialect/Linalg/IR/LinalgRelayoutOps.td +++ b/mlir/include/mlir/Dialect/Linalg/IR/LinalgRelayoutOps.td @@ -23,6 +23,7 @@ include "mlir/Interfaces/DestinationStyleOpInterface.td" include "mlir/Interfaces/SideEffectInterfaces.td" include "mlir/Interfaces/InferTypeOpInterface.td" include "mlir/Dialect/Linalg/IR/LinalgInterfaces.td" +include "mlir/Dialect/Tensor/IR/TensorInterfaces.td" include "mlir/IR/OpAsmInterface.td" //===----------------------------------------------------------------------===// @@ -32,7 +33,7 @@ include "mlir/IR/OpAsmInterface.td" class Linalg_RelayoutOp traits = []> : Op, - DestinationStyleOpInterface, LinalgRelayoutOpInterface, + DestinationStyleOpInterface, TensorRelayoutOpInterface, ConditionallySpeculatable, NoMemoryEffect, DeclareOpInterfaceMethods, TypesMatchWith<"result type matches type of dest", diff --git a/mlir/include/mlir/Dialect/Tensor/IR/CMakeLists.txt b/mlir/include/mlir/Dialect/Tensor/IR/CMakeLists.txt index cd14fe5c04561..74a05291376b3 100644 --- a/mlir/include/mlir/Dialect/Tensor/IR/CMakeLists.txt +++ b/mlir/include/mlir/Dialect/Tensor/IR/CMakeLists.txt @@ -1,2 +1,8 @@ add_mlir_dialect(TensorOps tensor) add_mlir_doc(TensorOps TensorOps Dialects/ -gen-dialect-doc) + +set(LLVM_TARGET_DEFINITIONS TensorInterfaces.td) +mlir_tablegen(TensorInterfaces.h.inc -gen-op-interface-decls) +mlir_tablegen(TensorInterfaces.cpp.inc -gen-op-interface-defs) +add_public_tablegen_target(MLIRTensorInterfacesIncGen) +add_dependencies(mlir-headers MLIRTensorInterfacesIncGen) diff --git a/mlir/include/mlir/Dialect/Tensor/IR/Tensor.h b/mlir/include/mlir/Dialect/Tensor/IR/Tensor.h index eb550bb469b9f..b3ec796a72337 100644 --- a/mlir/include/mlir/Dialect/Tensor/IR/Tensor.h +++ b/mlir/include/mlir/Dialect/Tensor/IR/Tensor.h @@ -46,6 +46,12 @@ SmallVector getOrCreateRanges(OffsetSizeAndStrideOpInterface op, #include "mlir/Dialect/Tensor/IR/TensorOpsDialect.h.inc" +//===----------------------------------------------------------------------===// +// Tensor Interfaces +//===----------------------------------------------------------------------===// + +#include "mlir/Dialect/Tensor/IR/TensorInterfaces.h.inc" + //===----------------------------------------------------------------------===// // Tensor Dialect Operations //===----------------------------------------------------------------------===// diff --git a/mlir/include/mlir/Dialect/Tensor/IR/TensorInterfaces.td b/mlir/include/mlir/Dialect/Tensor/IR/TensorInterfaces.td new file mode 100644 index 0000000000000..83c173b182664 --- /dev/null +++ b/mlir/include/mlir/Dialect/Tensor/IR/TensorInterfaces.td @@ -0,0 +1,30 @@ +//===- TensorInterfaces.td - Tensor Interfaces Declaration -*- tablegen -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This is the definition file for the structured interface sfor Tensor ops. +// +//===----------------------------------------------------------------------===// + +#ifndef TENSOR_IR_TENSORINTERFACES +#define TENSOR_IR_TENSORINTERFACES + +include "mlir/Interfaces/DestinationStyleOpInterface.td" +include "mlir/IR/OpBase.td" + +def TensorRelayoutOpInterface : OpInterface<"RelayoutOpInterface"> { + let description = [{ + A Tensor relayout-op is either linalg.pack or linalg.unpack. + + While we could extend this interface with methods from Linalg_RelayoutOp, + this is currently not needed and left as a TODO. + }]; + let cppNamespace = "::mlir::tensor"; +} + +#endif // TENSOR_IR_TENSORINTERFACES + diff --git a/mlir/lib/Dialect/Tensor/IR/CMakeLists.txt b/mlir/lib/Dialect/Tensor/IR/CMakeLists.txt index 5425615dac393..d9d09d6361a2f 100644 --- a/mlir/lib/Dialect/Tensor/IR/CMakeLists.txt +++ b/mlir/lib/Dialect/Tensor/IR/CMakeLists.txt @@ -16,6 +16,7 @@ add_mlir_dialect_library(MLIRTensorDialect DEPENDS MLIRTensorOpsIncGen + MLIRTensorInterfacesIncGen LINK_LIBS PUBLIC MLIRAffineDialect diff --git a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp index e741144647043..eeef383ac531a 100644 --- a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp +++ b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp @@ -10,9 +10,7 @@ #include "mlir/Dialect/Arith/IR/Arith.h" #include "mlir/Dialect/Arith/Utils/Utils.h" #include "mlir/Dialect/Complex/IR/Complex.h" -#include "mlir/Dialect/Linalg/IR/Linalg.h" #include "mlir/Dialect/Tensor/IR/Tensor.h" -#include "mlir/Dialect/Tensor/Utils/Utils.h" #include "mlir/Dialect/Utils/IndexingUtils.h" #include "mlir/Dialect/Utils/ReshapeOpsUtils.h" #include "mlir/Dialect/Utils/StaticValueUtils.h" @@ -3914,7 +3912,7 @@ struct FoldTensorCastProducerOp // Reject PackOp/UnpackOp (i.e. RelayoutOps) - there are dedicated patterns // for that instead. if (!foldTensorCastPrecondition(op) || - isa(*op)) + isa(*op)) return failure(); SmallVector newResultTypes(op->getResultTypes()); diff --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel index 92aedac837197..2569847ab6efe 100644 --- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel @@ -11140,6 +11140,7 @@ td_library( "include/mlir/Dialect/Linalg/IR/LinalgEnums.td", "include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td", "include/mlir/Dialect/Linalg/IR/LinalgOps.td", + "include/mlir/Dialect/Linalg/IR/LinalgRelayoutOps.td", ], includes = ["include"], deps = [ @@ -11150,6 +11151,7 @@ td_library( ":LoopLikeInterfaceTdFiles", ":OpBaseTdFiles", ":SideEffectInterfacesTdFiles", + ":TensorOpsTdFiles", ":TilingInterfaceTdFiles", ":ViewLikeInterfaceTdFiles", ], @@ -11206,6 +11208,23 @@ gentbl_cc_library( deps = [":LinalgOpsTdFiles"], ) +gentbl_cc_library( + name = "LinalgRelayoutOpsIncGen", + tbl_outs = [ + ( + ["-gen-op-decls"], + "include/mlir/Dialect/Linalg/IR/LinalgRelayoutOps.h.inc", + ), + ( + ["-gen-op-defs"], + "include/mlir/Dialect/Linalg/IR/LinalgRelayoutOps.cpp.inc", + ), + ], + tblgen = ":mlir-tblgen", + td_file = "include/mlir/Dialect/Linalg/IR/LinalgRelayoutOps.td", + deps = [":LinalgOpsTdFiles"], +) + gentbl_cc_library( name = "LinalgEnumsIncGen", tbl_outs = [ @@ -11557,6 +11576,7 @@ cc_library( ":LinalgInterfacesIncGen", ":LinalgNamedStructuredOpsYamlIncGen", ":LinalgOpsIncGen", + ":LinalgRelayoutOpsIncGen", ":LinalgStructuredOpsIncGen", ":MathDialect", ":MemRefDialect", @@ -11568,6 +11588,7 @@ cc_library( ":SubsetOpInterface", ":Support", ":TensorDialect", + ":TensorUtils", ":TilingInterface", ":ValueBoundsOpInterface", ":ViewLikeInterface",