Skip to content

Commit 80fd902

Browse files
authored
[mlir][tensor] Introduce TensorRelayoutOpInterface (llvm#125823)
The newly introduced `TensorRelayoutOpInterface` is created specifically for `tensor.pack` + `tensor.unpack`. Although the interface is currently empty, it enables us to refactor the logic in `FoldTensorCastProducerOp` within the Tensor dialect as follows: ```cpp // OLD // Reject tensor::PackOp - there's dedicated pattern for that instead. if (!foldTensorCastPrecondition(op) || isa<tensor::PackOp, tensor::UnPackOp>(*op)) return failure(); ``` is replaced with: ```cpp // NEW // Reject tensor::PackOp - there's dedicated pattern for that instead. if (!foldTensorCastPrecondition(op) || isa<tensor::RelayoutOpInterface>(*op)) return failure(); ``` This will be crucial once `tensor.pack` + `tensor.pack` are replaced with `linalg.pack` + `linalg.unpack` (i.e. moved to Linalg): * llvm#123902, * https://discourse.llvm.org/t/rfc-move-tensor-pack-and-tensor-unpack-into-linalg/. Note that the interface itself will later be moved to the Linalg dialect. This decoupling ensures that the Tensor dialect does not require an understanding of Linalg ops, thus keeping the dependency lightweight. This PR is effectively a preparatory step for moving PackOp and UnpackOp to Linalg. Once that's completed, most CMake changes from this PR will be effectively reverted.
1 parent d2b45ce commit 80fd902

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
add_mlir_dialect(TensorOps tensor)
22
add_mlir_doc(TensorOps TensorOps Dialects/ -gen-dialect-doc)
3+
4+
set(LLVM_TARGET_DEFINITIONS TensorInterfaces.td)
5+
mlir_tablegen(TensorInterfaces.h.inc -gen-op-interface-decls)
6+
mlir_tablegen(TensorInterfaces.cpp.inc -gen-op-interface-defs)
7+
add_public_tablegen_target(MLIRTensorInterfacesIncGen)
8+
add_dependencies(mlir-headers MLIRTensorInterfacesIncGen)

mlir/include/mlir/Dialect/Tensor/IR/Tensor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ SmallVector<Range, 8> getOrCreateRanges(OffsetSizeAndStrideOpInterface op,
4646

4747
#include "mlir/Dialect/Tensor/IR/TensorOpsDialect.h.inc"
4848

49+
//===----------------------------------------------------------------------===//
50+
// Tensor Interfaces
51+
//===----------------------------------------------------------------------===//
52+
53+
#include "mlir/Dialect/Tensor/IR/TensorInterfaces.h.inc"
54+
4955
//===----------------------------------------------------------------------===//
5056
// Tensor Dialect Operations
5157
//===----------------------------------------------------------------------===//
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===- TensorInterfaces.td - Tensor Interfaces Declaration -*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This is the definition file for the structured interface sfor Tensor ops.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef TENSOR_IR_TENSORINTERFACES
14+
#define TENSOR_IR_TENSORINTERFACES
15+
16+
include "mlir/Interfaces/DestinationStyleOpInterface.td"
17+
include "mlir/IR/OpBase.td"
18+
19+
// TODO: To be moved to LinalgInterfaces.td, see:
20+
// * https://github.com/llvm/llvm-project/pull/123902
21+
// * https://discourse.llvm.org/t/rfc-move-tensor-pack-and-tensor-unpack-into-linalg/
22+
def TensorRelayoutOpInterface : OpInterface<"RelayoutOpInterface"> {
23+
let description = [{
24+
A Tensor (soon to be Linalg) relayout-op is either tensor.pack or
25+
tensor.unpack.
26+
27+
While we could extend this interface with methods from Tensor_RelayoutOp,
28+
this is currently not needed and left as a TODO.
29+
}];
30+
let cppNamespace = "::mlir::tensor";
31+
}
32+
33+
#endif // TENSOR_IR_TENSORINTERFACES

mlir/include/mlir/Dialect/Tensor/IR/TensorOps.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define TENSOR_OPS
1111

1212
include "mlir/Dialect/Tensor/IR/TensorBase.td"
13+
include "mlir/Dialect/Tensor/IR/TensorInterfaces.td"
1314
include "mlir/Interfaces/CastInterfaces.td"
1415
include "mlir/Interfaces/ControlFlowInterfaces.td"
1516
include "mlir/Interfaces/DestinationStyleOpInterface.td"
@@ -1833,6 +1834,7 @@ class Tensor_RelayoutOp<string mnemonic, list<Trait> traits = []> :
18331834
DestinationStyleOpInterface,
18341835
ConditionallySpeculatable, NoMemoryEffect,
18351836
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>,
1837+
TensorRelayoutOpInterface,
18361838
TypesMatchWith<"result type matches type of dest",
18371839
"dest", "result",
18381840
"$_self">])> {

mlir/lib/Dialect/Tensor/IR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_mlir_dialect_library(MLIRTensorDialect
1616

1717
DEPENDS
1818
MLIRTensorOpsIncGen
19+
MLIRTensorInterfacesIncGen
1920

2021
LINK_LIBS PUBLIC
2122
MLIRAffineDialect

mlir/lib/Dialect/Tensor/IR/TensorOps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4976,7 +4976,7 @@ struct FoldTensorCastProducerOp
49764976

49774977
// Reject tensor::PackOp - there's dedicated pattern for that instead.
49784978
if (!foldTensorCastPrecondition(op) ||
4979-
isa<tensor::PackOp, tensor::UnPackOp>(*op))
4979+
isa<tensor::RelayoutOpInterface>(*op))
49804980
return failure();
49814981

49824982
SmallVector<Type> newResultTypes(op->getResultTypes());

0 commit comments

Comments
 (0)