Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class Bufferization_Op<string mnemonic, list<Trait> traits = []>

def Bufferization_AllocTensorOp : Bufferization_Op<"alloc_tensor",
[AttrSizedOperandSegments, BufferizableOpInterface,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>]> {
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface, [
"reifyResultShapes"]>]> {
let summary = "allocate buffer for a tensor";

let description = [{
Expand Down Expand Up @@ -219,7 +220,8 @@ def Bufferization_MaterializeInDestinationOp
: Bufferization_Op<"materialize_in_destination",
[AllElementTypesMatch<["source", "dest"]>,
BufferizableOpInterface, DestinationStyleOpInterface,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface, [
"reifyResultShapes"]>,
DeclareOpInterfaceMethods<SubsetOpInterface,
["operatesOnEquivalentSubset", "operatesOnDisjointSubset"]>,
DeclareOpInterfaceMethods<SubsetInsertionOpInterface,
Expand Down
3 changes: 2 additions & 1 deletion mlir/include/mlir/Dialect/Linalg/IR/LinalgOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def Linalg_IndexOp : Linalg_Op<"index", [Pure]>,
def Linalg_SoftmaxOp : Linalg_Op<"softmax",
[DestinationStyleOpInterface,
PredOpTrait<"input and output have same element type", TCopVTEtIsSameAs<0, 1>>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface,
["reifyResultShapes"]>,
DeclareOpInterfaceMethods<AggregatedOpInterface, ["decomposeOperation"]>,
DeclareOpInterfaceMethods<MemoryEffectsOpInterface>,
DeclareOpInterfaceMethods<TilingInterface,
Expand Down
3 changes: 2 additions & 1 deletion mlir/include/mlir/Dialect/Linalg/IR/LinalgRelayoutOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class Linalg_RelayoutOp<string mnemonic, list<Trait> traits = []> :
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
DestinationStyleOpInterface, LinalgRelayoutOpInterface,
ConditionallySpeculatable, NoMemoryEffect,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface, [
"reifyResultShapes"]>,
TypesMatchWith<"result type matches type of dest",
"dest", "result",
"$_self">])> {
Expand Down
3 changes: 2 additions & 1 deletion mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,8 @@ class MemRef_ReassociativeReshapeOp<string mnemonic, list<Trait> traits = []> :
def MemRef_ExpandShapeOp : MemRef_ReassociativeReshapeOp<"expand_shape", [
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
DeclareOpInterfaceMethods<MemorySpaceCastConsumerOpInterface>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>]> {
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface,
["reifyResultShapes"]>]> {
let summary = "operation to produce a memref with a higher rank.";
let description = [{
The `memref.expand_shape` op produces a new view with a higher rank whose
Expand Down
10 changes: 10 additions & 0 deletions mlir/include/mlir/Dialect/MemRef/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ def ResolveRankedShapeTypeResultDimsPass
implement the `ReifyRankedShapedTypeOpInterface` in terms of
shapes of its operands.
}];
let options = [
Option<"errorOnPatternIterationLimit", "error-on-pattern-iteration-limit", "bool",
/*default=*/"true",
"Throw an error when pattern rewriter hits iteration limit">,
];
let dependentDialects = [
"memref::MemRefDialect", "tensor::TensorDialect"
];
Expand All @@ -177,6 +182,11 @@ def ResolveShapedTypeResultDimsPass : Pass<"resolve-shaped-type-result-dims"> {
`ReifyRankedShapedTypeOpInterface` in terms of shapes of its
operands.
}];
let options = [
Option<"errorOnPatternIterationLimit", "error-on-pattern-iteration-limit", "bool",
/*default=*/"true",
"Throw an error when pattern rewriter hits iteration limit">,
];
let dependentDialects = [
"affine::AffineDialect", "memref::MemRefDialect", "tensor::TensorDialect"
];
Expand Down
22 changes: 15 additions & 7 deletions mlir/include/mlir/Dialect/Tensor/IR/TensorOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ def Tensor_CastOp : Tensor_Op<"cast", [
def Tensor_ConcatOp : Tensor_Op<"concat",
[Pure,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>]> {
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface, [
"reifyResultShapes"]>,
]> {
let summary = "tensor concatenation operation";
let description = [{
The "concat" operation constructs a tensor out of a variadic list of input
Expand Down Expand Up @@ -261,7 +263,8 @@ def Tensor_DimOp : Tensor_Op<"dim", [

def Tensor_EmptyOp : Tensor_Op<"empty",
[Pure,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>]> {
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface, [
"reifyResultShapes"]>]> {
let summary = "empty tensor operation";

let description = [{
Expand Down Expand Up @@ -358,7 +361,8 @@ def Tensor_ExtractOp : Tensor_Op<"extract", [

def Tensor_ExtractSliceOp : Tensor_OpWithOffsetSizesAndStrides<"extract_slice", [
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface, [
"reifyResultShapes"]>,
AttrSizedOperandSegments,
Pure,
OffsetSizeAndStrideOpInterface
Expand Down Expand Up @@ -740,7 +744,8 @@ def Tensor_GatherOp : Tensor_Op<"gather", [
def Tensor_GenerateOp : Tensor_Op<"generate", [
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
RecursiveMemoryEffects,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface, [
"reifyResultShapes"]>,
SingleBlockImplicitTerminator<"mlir::tensor::YieldOp">]> {
let summary = "Creates a dynamically sized tensor from elements";
let description = [{
Expand Down Expand Up @@ -835,7 +840,8 @@ def Tensor_InsertOp : Tensor_Op<"insert", [

def Tensor_InsertSliceOp : Tensor_OpWithOffsetSizesAndStrides<"insert_slice", [
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface, [
"reifyResultShapes"]>,
AttrSizedOperandSegments,
DestinationStyleOpInterface,
Pure,
Expand Down Expand Up @@ -1256,7 +1262,8 @@ def Tensor_CollapseShapeOp : Tensor_ReassociativeReshapeOp<"collapse_shape"> {

def Tensor_PadOp : Tensor_Op<"pad", [
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface, [
"reifyResultShapes"]>,
AttrSizedOperandSegments,
Pure,
SingleBlockImplicitTerminator<"mlir::tensor::YieldOp">]> {
Expand Down Expand Up @@ -1764,7 +1771,8 @@ def Tensor_ScatterOp : Tensor_Op<"scatter", [

def Tensor_SplatOp : Tensor_Op<"splat", [
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface,
["reifyResultShapes"]>,
Pure,
TypesMatchWith<"operand type matches element type of result",
"aggregate", "input",
Expand Down
3 changes: 2 additions & 1 deletion mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -2218,7 +2218,8 @@ def Tosa_TileOp : Tosa_InferShapedTypeOp<"tile"> {
// Operator: transpose
//===----------------------------------------------------------------------===//
def Tosa_TransposeOp : Tosa_InferShapedTypeOp<"transpose",
[DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>,
[DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface ,
["reifyResultShapes"]>,
AllElementTypesMatch<["input1", "output"]>]> {
let summary = "Transpose operator.";

Expand Down
4 changes: 4 additions & 0 deletions mlir/include/mlir/Interfaces/InferTypeOpInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ using ReifiedRankedShapedTypeDims = SmallVector<SmallVector<OpFoldResult>>;
LogicalResult
reifyResultShapes(OpBuilder &b, Operation *op,
ReifiedRankedShapedTypeDims &reifiedReturnShapes);
FailureOr<SmallVector<OpFoldResult>>
reifyShapeOfResult(OpBuilder &b, Operation *op, int resultIndex);
FailureOr<OpFoldResult> reifyDimOfResult(OpBuilder &b, Operation *op,
int resultIndex, int dim);

/// Adaptor class to abstract the differences between whether value is from
/// a ShapedType or ShapedTypeComponents or DenseIntElementsAttribute.
Expand Down
64 changes: 60 additions & 4 deletions mlir/include/mlir/Interfaces/InferTypeOpInterface.td
Original file line number Diff line number Diff line change
Expand Up @@ -361,20 +361,76 @@ def ReifyRankedShapedTypeOpInterface :
let methods = [
InterfaceMethod<
/*desc=*/[{
Reify the shape of the result of an operation (typically in terms of the
shape of its operands).
Reify the shapes of all the result of an operation (typically in terms
of the shape of its operands).

`reifiedReturnShapes` is populated with one vector per op result. Each
of those vectors contains an OpFoldResult for each dimension of the
shaped type. The given builder may be used to insert ops that compute
result shapes.

If the shape of a particular result cannot be computed it must be empty.
If the shape of a particular result cannot be computed it in terms of
its operands it must be left empty. If any dimension of the result cannot
be computed it must be set to OpFoldResult().
}],
/*retTy=*/"::llvm::LogicalResult",
/*methodName=*/"reifyResultShapes",
/*args=*/(ins "::mlir::OpBuilder &":$builder,
"::mlir::ReifiedRankedShapedTypeDims &":$reifiedReturnShapes)
"::mlir::ReifiedRankedShapedTypeDims &":$reifiedReturnShapes),
/*methodBody=*/"",
/*defaultImplementation=*/[{ return ::mlir::failure(); }]
>,
InterfaceMethod<
/*desc=*/[{
Reify the shape of a single result of an operation (typically in terms
of the shape of its operands).

Returns the shape of a single result of the operation as a
`SmallVector<OpFoldResult>`, one per dimension of the shaped type. The
given builder may be used to insert ops that compute result shapes.

If any dimension of the result cannot be computed it must be set to
OpFoldResult().
}],
/*retTy=*/"::llvm::FailureOr<::llvm::SmallVector<::mlir::OpFoldResult>>",
/*methodName=*/"reifyShapeOfResult",
/*args=*/(ins "::mlir::OpBuilder &":$builder,
"int":$resultIndex),
/*methodBody=*/"",
/*defaultImplementation=*/[{
ReifiedRankedShapedTypeDims reifiedShapes;
if (failed(cast<ReifyRankedShapedTypeOpInterface>($_op.getOperation()).reifyResultShapes(builder, reifiedShapes)))
return failure();
if (resultIndex < 0 || resultIndex >= static_cast<int>(reifiedShapes.size()))
return $_op.emitOpError("invalid result index");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would turn this into an assert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I just assert here, then in release builds it will hit a segfault. I'd rather bail gracefully.

return reifiedShapes[resultIndex];
}]
>,
InterfaceMethod<
/*desc=*/[{
Reify the shape of a dimension of a given result of an operation
(typically in terms of the shape of its operands).

Returns the shape of a specific dimension of a result of the operation as
an OpFoldResult. The given builder may be used to insert ops that compute
the shapes.

If the dimension of the result cannot be computed the method must return
`failure()`.
}],
/*retTy=*/"::llvm::FailureOr<::mlir::OpFoldResult>",
/*methodName=*/"reifyDimOfResult",
/*args=*/(ins "::mlir::OpBuilder &":$builder,
"int":$resultIndex, "int":$dim),
/*methodBody=*/"",
/*defaultImplementation=*/[{
auto shapes = cast<ReifyRankedShapedTypeOpInterface>($_op.getOperation()).reifyShapeOfResult(builder, resultIndex);
if (failed(shapes))
return failure();
if (dim < 0 || dim >= static_cast<int>((*shapes).size()))
return $_op.emitOpError("invalid dimension");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would turn this into an assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. With assert in release builds this would segfault linstead of erroring out?

return (*shapes)[dim];
}]
>
];
}
Expand Down
33 changes: 21 additions & 12 deletions mlir/lib/Dialect/MemRef/Transforms/ResolveShapedTypeResultDims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,16 @@ struct DimOfReifyRankedShapedTypeOpInterface : public OpRewritePattern<OpTy> {
if (!dimIndex)
return failure();

ReifiedRankedShapedTypeDims reifiedResultShapes;
if (failed(reifyResultShapes(rewriter, dimValue.getOwner(),
reifiedResultShapes)))
FailureOr<OpFoldResult> replacement = reifyDimOfResult(
rewriter, dimValue.getOwner(), dimValue.getResultNumber(), *dimIndex);
if (failed(replacement))
return failure();
unsigned resultNumber = dimValue.getResultNumber();
// Do not apply pattern if the IR is invalid (dim out of bounds).
if ((size_t)(*dimIndex) >= reifiedResultShapes[resultNumber].size())
return rewriter.notifyMatchFailure(dimOp, "dimension is out of bounds");
Value replacement = getValueOrCreateConstantIndexOp(
rewriter, dimOp.getLoc(), reifiedResultShapes[resultNumber][*dimIndex]);
rewriter.replaceOp(dimOp, replacement);
// Check if the OpFoldResult is empty (unreifiable dimension).
if (!replacement.value())
return failure();
Value replacementVal = getValueOrCreateConstantIndexOp(
rewriter, dimOp.getLoc(), replacement.value());
rewriter.replaceOp(dimOp, replacementVal);
return success();
}
};
Expand Down Expand Up @@ -166,12 +165,14 @@ namespace {
struct ResolveRankedShapeTypeResultDimsPass final
: public memref::impl::ResolveRankedShapeTypeResultDimsPassBase<
ResolveRankedShapeTypeResultDimsPass> {
using Base::Base;
void runOnOperation() override;
};

struct ResolveShapedTypeResultDimsPass final
: public memref::impl::ResolveShapedTypeResultDimsPassBase<
ResolveShapedTypeResultDimsPass> {
using Base::Base;
void runOnOperation() override;
};

Expand All @@ -195,14 +196,22 @@ void memref::populateResolveShapedTypeResultDimsPatterns(
void ResolveRankedShapeTypeResultDimsPass::runOnOperation() {
RewritePatternSet patterns(&getContext());
memref::populateResolveRankedShapedTypeResultDimsPatterns(patterns);
if (failed(applyPatternsGreedily(getOperation(), std::move(patterns))))
auto result = applyPatternsGreedily(getOperation(), std::move(patterns));
if (errorOnPatternIterationLimit && failed(result)) {
getOperation()->emitOpError(
"dim operation resolution hit pattern iteration limit");
return signalPassFailure();
}
}

void ResolveShapedTypeResultDimsPass::runOnOperation() {
RewritePatternSet patterns(&getContext());
memref::populateResolveRankedShapedTypeResultDimsPatterns(patterns);
memref::populateResolveShapedTypeResultDimsPatterns(patterns);
if (failed(applyPatternsGreedily(getOperation(), std::move(patterns))))
auto result = applyPatternsGreedily(getOperation(), std::move(patterns));
if (errorOnPatternIterationLimit && failed(result)) {
getOperation()->emitOpError(
"dim operation resolution hit pattern iteration limit");
return signalPassFailure();
}
}
3 changes: 3 additions & 0 deletions mlir/lib/Dialect/Tensor/IR/TensorInferTypeOpInterfaceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ namespace {
struct ReifyExpandShapeOp
: public ReifyRankedShapedTypeOpInterface::ExternalModel<ReifyExpandShapeOp,
ExpandShapeOp> {
using Base =
ReifyRankedShapedTypeOpInterface::ExternalModel<ReifyExpandShapeOp,
ExpandShapeOp>;
LogicalResult
reifyResultShapes(Operation *op, OpBuilder &b,
ReifiedRankedShapedTypeDims &reifyResultShapes) const {
Expand Down
16 changes: 16 additions & 0 deletions mlir/lib/Interfaces/InferTypeOpInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ mlir::reifyResultShapes(OpBuilder &b, Operation *op,
return status;
}

FailureOr<SmallVector<OpFoldResult>>
mlir::reifyShapeOfResult(OpBuilder &b, Operation *op, int resultIndex) {
auto reifiableOp = dyn_cast<ReifyRankedShapedTypeOpInterface>(op);
if (!reifiableOp)
return failure();
return reifiableOp.reifyShapeOfResult(b, resultIndex);
}

FailureOr<OpFoldResult> mlir::reifyDimOfResult(OpBuilder &b, Operation *op,
int resultIndex, int dim) {
auto reifiableOp = dyn_cast<ReifyRankedShapedTypeOpInterface>(op);
if (!reifiableOp)
return failure();
return reifiableOp.reifyDimOfResult(b, resultIndex, dim);
}

bool ShapeAdaptor::hasRank() const {
if (val.isNull())
return false;
Expand Down
Loading