Skip to content

Commit 2458cd2

Browse files
committed
[mlir] Add a ViewLikeOpInterface
This can help provide a common interface for view-like ops so that for example Linalg's dependency analysis can avoid relying on concrete ops. Differential Revision: https://reviews.llvm.org/D78645
1 parent 0efb958 commit 2458cd2

File tree

14 files changed

+121
-14
lines changed

14 files changed

+121
-14
lines changed

mlir/include/mlir/Dialect/Linalg/IR/LinalgOps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "mlir/IR/TypeUtilities.h"
2424
#include "mlir/IR/Types.h"
2525
#include "mlir/Interfaces/SideEffects.h"
26+
#include "mlir/Interfaces/ViewLikeInterface.h"
2627
#include "mlir/Support/LLVM.h"
2728

2829
namespace mlir {

mlir/include/mlir/Dialect/Linalg/IR/LinalgOps.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
include "mlir/Dialect/Affine/IR/AffineOpsBase.td"
1717
include "mlir/Dialect/Linalg/IR/LinalgBase.td"
1818
include "mlir/Interfaces/SideEffects.td"
19+
include "mlir/Interfaces/ViewLikeInterface.td"
1920

2021
// Base class for Linalg dialect ops that do not correspond to library calls.
2122
class Linalg_Op<string mnemonic, list<OpTrait> traits = []> :
@@ -179,7 +180,8 @@ def Linalg_TensorReshapeOp : Linalg_ReshapeLikeOp<"tensor_reshape">,
179180
}];
180181
}
181182

182-
def Linalg_SliceOp : Linalg_Op<"slice", [NoSideEffect]>,
183+
def Linalg_SliceOp : Linalg_Op<"slice", [
184+
DeclareOpInterfaceMethods<ViewLikeOpInterface>, NoSideEffect]>,
183185
Arguments<(ins AnyStridedMemRef:$view,
184186
Variadic<AnyTypeOf<[Range, Index]>>:$indexings)>,
185187
Results<(outs AnyStridedMemRef)> {

mlir/include/mlir/Dialect/StandardOps/IR/Ops.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "mlir/Interfaces/CallInterfaces.h"
2222
#include "mlir/Interfaces/ControlFlowInterfaces.h"
2323
#include "mlir/Interfaces/SideEffects.h"
24+
#include "mlir/Interfaces/ViewLikeInterface.h"
2425

2526
// Pull in all enum type definitions and utility function declarations.
2627
#include "mlir/Dialect/StandardOps/IR/OpsEnums.h.inc"

mlir/include/mlir/Dialect/StandardOps/IR/Ops.td

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ include "mlir/IR/OpAsmInterface.td"
1717
include "mlir/Interfaces/CallInterfaces.td"
1818
include "mlir/Interfaces/ControlFlowInterfaces.td"
1919
include "mlir/Interfaces/SideEffects.td"
20+
include "mlir/Interfaces/ViewLikeInterface.td"
2021

2122
def StandardOps_Dialect : Dialect {
2223
let name = "std";
@@ -2315,7 +2316,11 @@ def SubIOp : IntArithmeticOp<"subi"> {
23152316
// SubViewOp
23162317
//===----------------------------------------------------------------------===//
23172318

2318-
def SubViewOp : Std_Op<"subview", [AttrSizedOperandSegments, NoSideEffect]> {
2319+
def SubViewOp : Std_Op<"subview", [
2320+
AttrSizedOperandSegments,
2321+
DeclareOpInterfaceMethods<ViewLikeOpInterface>,
2322+
NoSideEffect,
2323+
]> {
23192324
let summary = "memref subview operation";
23202325
let description = [{
23212326
The "subview" operation converts a memref type to another memref type
@@ -2785,7 +2790,8 @@ def UnsignedShiftRightOp : IntArithmeticOp<"shift_right_unsigned"> {
27852790
// ViewOp
27862791
//===----------------------------------------------------------------------===//
27872792

2788-
def ViewOp : Std_Op<"view", [NoSideEffect]> {
2793+
def ViewOp : Std_Op<"view", [
2794+
DeclareOpInterfaceMethods<ViewLikeOpInterface>, NoSideEffect]> {
27892795
let summary = "memref view operation";
27902796
let description = [{
27912797
The "view" operation converts a 1-D memref with i8 element type,

mlir/include/mlir/Interfaces/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ set(LLVM_TARGET_DEFINITIONS SideEffects.td)
2727
mlir_tablegen(SideEffectInterfaces.h.inc -gen-op-interface-decls)
2828
mlir_tablegen(SideEffectInterfaces.cpp.inc -gen-op-interface-defs)
2929
add_public_tablegen_target(MLIRSideEffectOpInterfacesIncGen)
30+
31+
set(LLVM_TARGET_DEFINITIONS ViewLikeInterface.td)
32+
mlir_tablegen(ViewLikeInterface.h.inc -gen-op-interface-decls)
33+
mlir_tablegen(ViewLikeInterface.cpp.inc -gen-op-interface-defs)
34+
add_public_tablegen_target(MLIRViewLikeInterfaceIncGen)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===- ViewLikeInterface.h - View-like operations interface ---------------===//
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 file implements the operation interface for view-like operations.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_INTERFACES_VIEWLIKEINTERFACE_H_
14+
#define MLIR_INTERFACES_VIEWLIKEINTERFACE_H_
15+
16+
#include "mlir/IR/OpDefinition.h"
17+
18+
namespace mlir {
19+
20+
#include "mlir/Interfaces/ViewLikeInterface.h.inc"
21+
22+
} // namespace mlir
23+
24+
#endif // MLIR_INTERFACES_VIEWLIKEINTERFACE_H_
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===- ViewLikeInterface.td - ViewLike interface -----------*- 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+
// Defines the interface for view-like operations.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_INTERFACES_VIEWLIKEINTERFACE
14+
#define MLIR_INTERFACES_VIEWLIKEINTERFACE
15+
16+
include "mlir/IR/OpBase.td"
17+
18+
def ViewLikeOpInterface : OpInterface<"ViewLikeOpInterface"> {
19+
let description = [{
20+
A view-like operation "views" a buffer in a potentially different way. It
21+
takes in a (view of) buffer (and potentially some other operands) and returns
22+
another view of buffer.
23+
}];
24+
25+
let methods = [
26+
InterfaceMethod<
27+
"Returns the source buffer from which the view is created.",
28+
"Value", "getViewSource">
29+
];
30+
}
31+
32+
#endif // MLIR_INTERFACES_VIEWLIKEINTERFACE

mlir/lib/Dialect/Linalg/Analysis/DependenceAnalysis.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,17 @@ Value Aliases::find(Value v) {
3737
while (true) {
3838
if (v.isa<BlockArgument>())
3939
return v;
40-
if (auto alloc = dyn_cast_or_null<AllocOp>(v.getDefiningOp())) {
40+
Operation *defOp = v.getDefiningOp();
41+
if (auto alloc = dyn_cast_or_null<AllocOp>(defOp)) {
4142
if (isStrided(alloc.getType()))
4243
return alloc.getResult();
4344
}
44-
if (auto slice = dyn_cast_or_null<SliceOp>(v.getDefiningOp())) {
45-
auto it = aliases.insert(std::make_pair(v, find(slice.view())));
45+
if (auto viewLikeOp = dyn_cast_or_null<ViewLikeOpInterface>(defOp)) {
46+
auto it =
47+
aliases.insert(std::make_pair(v, find(viewLikeOp.getViewSource())));
4648
return it.first->second;
4749
}
48-
if (auto view = dyn_cast_or_null<ViewOp>(v.getDefiningOp())) {
49-
auto it = aliases.insert(std::make_pair(v, view.source()));
50-
return it.first->second;
51-
}
52-
if (auto view = dyn_cast_or_null<SubViewOp>(v.getDefiningOp())) {
53-
v = view.source();
54-
continue;
55-
}
50+
5651
llvm::errs() << "View alias analysis reduces to: " << v << "\n";
5752
llvm_unreachable("unsupported view alias case");
5853
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ target_link_libraries(MLIRLinalgOps
1717
PUBLIC
1818
MLIRIR
1919
MLIRSideEffects
20+
MLIRViewLikeInterface
2021
MLIRStandardOps
2122
)

mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,8 @@ static LogicalResult verify(SliceOp op) {
680680
return success();
681681
}
682682

683+
Value SliceOp::getViewSource() { return view(); }
684+
683685
//===----------------------------------------------------------------------===//
684686
// TransposeOp
685687
//===----------------------------------------------------------------------===//

mlir/lib/Dialect/StandardOps/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ target_link_libraries(MLIRStandardOps
1616
MLIREDSC
1717
MLIRIR
1818
MLIRSideEffects
19+
MLIRViewLikeInterface
1920
LLVMSupport
2021
)

mlir/lib/Dialect/StandardOps/IR/Ops.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,8 @@ SubViewOp::getStaticStrides(SmallVectorImpl<int64_t> &staticStrides) {
23242324
return success();
23252325
}
23262326

2327+
Value SubViewOp::getViewSource() { return source(); }
2328+
23272329
namespace {
23282330

23292331
/// Pattern to rewrite a subview op with constant size arguments.
@@ -2669,6 +2671,8 @@ static LogicalResult verify(ViewOp op) {
26692671
return success();
26702672
}
26712673

2674+
Value ViewOp::getViewSource() { return source(); }
2675+
26722676
namespace {
26732677

26742678
struct ViewOpShapeFolder : public OpRewritePattern<ViewOp> {

mlir/lib/Interfaces/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LLVM_OPTIONAL_SOURCES
55
InferTypeOpInterface.cpp
66
LoopLikeInterface.cpp
77
SideEffects.cpp
8+
ViewLikeInterface.cpp
89
)
910

1011
add_mlir_library(MLIRCallInterfaces
@@ -90,3 +91,17 @@ target_link_libraries(MLIRSideEffects
9091
PUBLIC
9192
MLIRIR
9293
)
94+
95+
add_mlir_library(MLIRViewLikeInterface
96+
ViewLikeInterface.cpp
97+
98+
ADDITIONAL_HEADER_DIRS
99+
${MLIR_MAIN_INCLUDE_DIR}/mlir/Interfaces
100+
101+
DEPENDS
102+
MLIRViewLikeInterfaceIncGen
103+
)
104+
target_link_libraries(MLIRLoopLikeInterface
105+
PUBLIC
106+
MLIRIR
107+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===- ViewLikeInterface.cpp - View-like operations in MLIR ---------------===//
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+
#include "mlir/Interfaces/ViewLikeInterface.h"
10+
11+
using namespace mlir;
12+
13+
//===----------------------------------------------------------------------===//
14+
// ViewLike Interfaces
15+
//===----------------------------------------------------------------------===//
16+
17+
/// Include the definitions of the loop-like interfaces.
18+
#include "mlir/Interfaces/ViewLikeInterface.cpp.inc"

0 commit comments

Comments
 (0)