Skip to content

Commit 8e2ccdc

Browse files
authored
[MLIR][LLVM] Use ViewLikeOpInterface (llvm#111663)
This commit adds the ViewLikeOpInterface to the GEP and AddrSpaceCast operations. This allows us to simplify the inliner interface. At the same time, the change also makes the inliner interface more extensible for downstream users that have custom view-like operations.
1 parent 4b4078a commit 8e2ccdc

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "mlir/Interfaces/FunctionInterfaces.h"
3030
#include "mlir/Interfaces/InferTypeOpInterface.h"
3131
#include "mlir/Interfaces/SideEffectInterfaces.h"
32+
#include "mlir/Interfaces/ViewLikeInterface.h"
3233
#include "mlir/Support/ThreadLocalCache.h"
3334
#include "llvm/ADT/PointerEmbeddedInt.h"
3435
#include "llvm/IR/DerivedTypes.h"

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ include "mlir/Interfaces/ControlFlowInterfaces.td"
2424
include "mlir/Interfaces/InferTypeOpInterface.td"
2525
include "mlir/Interfaces/MemorySlotInterfaces.td"
2626
include "mlir/Interfaces/SideEffectInterfaces.td"
27+
include "mlir/Interfaces/ViewLikeInterface.td"
2728

2829
class LLVM_Builder<string builder> {
2930
string llvmBuilder = builder;
@@ -246,7 +247,9 @@ def LLVM_AllocaOp : LLVM_Op<"alloca",
246247
def LLVM_GEPOp : LLVM_Op<"getelementptr", [Pure,
247248
DeclareOpInterfaceMethods<PromotableOpInterface>,
248249
DeclareOpInterfaceMethods<SafeMemorySlotAccessOpInterface>,
249-
DeclareOpInterfaceMethods<DestructurableAccessorOpInterface>]> {
250+
DeclareOpInterfaceMethods<DestructurableAccessorOpInterface>,
251+
DeclareOpInterfaceMethods<ViewLikeOpInterface>
252+
]> {
250253
let arguments = (ins LLVM_ScalarOrVectorOf<LLVM_AnyPointer>:$base,
251254
Variadic<LLVM_ScalarOrVectorOf<AnySignlessInteger>>:$dynamicIndices,
252255
DenseI32ArrayAttr:$rawConstantIndices,
@@ -495,7 +498,8 @@ def LLVM_BitcastOp : LLVM_CastOp<"bitcast", "BitCast", LLVM_AnyNonAggregate,
495498
def LLVM_AddrSpaceCastOp : LLVM_CastOp<"addrspacecast", "AddrSpaceCast",
496499
LLVM_ScalarOrVectorOf<LLVM_AnyPointer>,
497500
LLVM_ScalarOrVectorOf<LLVM_AnyPointer>,
498-
[DeclareOpInterfaceMethods<PromotableOpInterface>]> {
501+
[DeclareOpInterfaceMethods<PromotableOpInterface>,
502+
DeclareOpInterfaceMethods<ViewLikeOpInterface>]> {
499503
let hasFolder = 1;
500504
}
501505
def LLVM_IntToPtrOp : LLVM_CastOp<"inttoptr", "IntToPtr",

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,6 +3226,8 @@ OpFoldResult LLVM::AddrSpaceCastOp::fold(FoldAdaptor adaptor) {
32263226
return foldChainableCast(*this, adaptor);
32273227
}
32283228

3229+
Value LLVM::AddrSpaceCastOp::getViewSource() { return getArg(); }
3230+
32293231
//===----------------------------------------------------------------------===//
32303232
// Folder for LLVM::GEPOp
32313233
//===----------------------------------------------------------------------===//
@@ -3276,6 +3278,8 @@ OpFoldResult LLVM::GEPOp::fold(FoldAdaptor adaptor) {
32763278
return {};
32773279
}
32783280

3281+
Value LLVM::GEPOp::getViewSource() { return getBase(); }
3282+
32793283
//===----------------------------------------------------------------------===//
32803284
// ShlOp
32813285
//===----------------------------------------------------------------------===//

mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1717
#include "mlir/IR/Matchers.h"
1818
#include "mlir/Interfaces/DataLayoutInterfaces.h"
19+
#include "mlir/Interfaces/ViewLikeInterface.h"
1920
#include "mlir/Transforms/InliningUtils.h"
2021
#include "llvm/ADT/ScopeExit.h"
2122
#include "llvm/Support/Debug.h"
@@ -229,11 +230,10 @@ static FailureOr<SmallVector<Value>>
229230
getUnderlyingObjectSet(Value pointerValue) {
230231
SmallVector<Value> result;
231232
WalkContinuation walkResult = walkSlice(pointerValue, [&](Value val) {
232-
if (auto gepOp = val.getDefiningOp<LLVM::GEPOp>())
233-
return WalkContinuation::advanceTo(gepOp.getBase());
234-
235-
if (auto addrCast = val.getDefiningOp<LLVM::AddrSpaceCastOp>())
236-
return WalkContinuation::advanceTo(addrCast.getOperand());
233+
// Attempt to advance to the source of the underlying view-like operation.
234+
// Examples of view-like operations include GEPOp and AddrSpaceCastOp.
235+
if (auto viewOp = val.getDefiningOp<ViewLikeOpInterface>())
236+
return WalkContinuation::advanceTo(viewOp.getViewSource());
237237

238238
// Attempt to advance to control flow predecessors.
239239
std::optional<SmallVector<Value>> controlFlowPredecessors =

0 commit comments

Comments
 (0)