Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lower memref.reinterpret_cast to EmitC #392

Open
wants to merge 2 commits into
base: feature/fused-ops
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 29 additions & 2 deletions mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,33 @@ struct ConvertExpandShape final
}
};

struct ConvertReinterpretCast final
: public OpConversionPattern<memref::ReinterpretCastOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(memref::ReinterpretCastOp op, OpAdaptor operands,
ConversionPatternRewriter &rewriter) const override {
auto arrayValue =
dyn_cast<TypedValue<emitc::ArrayType>>(operands.getSource());
if (!arrayValue) {
return rewriter.notifyMatchFailure(op.getLoc(), "expected array type");
}

auto resultTy = getTypeConverter()->convertType(op.getType());
if (!resultTy) {
return rewriter.notifyMatchFailure(op.getLoc(),
"cannot convert result type");
}

auto newCastOp = rewriter.create<emitc::CastOp>(op->getLoc(), resultTy,
operands.getSource());
newCastOp.setReference(true);
rewriter.replaceOp(op, newCastOp);
return success();
}
};

} // namespace

void mlir::populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter) {
Expand All @@ -251,6 +278,6 @@ void mlir::populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter) {
void mlir::populateMemRefToEmitCConversionPatterns(RewritePatternSet &patterns,
TypeConverter &converter) {
patterns.add<ConvertAlloca, ConvertGlobal, ConvertGetGlobal, ConvertLoad,
ConvertStore, ConvertCollapseShape, ConvertExpandShape>(
converter, patterns.getContext());
ConvertStore, ConvertCollapseShape, ConvertExpandShape,
ConvertReinterpretCast>(converter, patterns.getContext());
}
45 changes: 45 additions & 0 deletions mlir/test/Conversion/MemRefToEmitC/memref-to-emitc-failed.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,48 @@ func.func @memref_collapse_dyn_shape(%arg: memref<?x5xi32>) -> memref<?xi32> {
%0 = memref.collapse_shape %arg [[0, 1]] : memref<?x5xi32> into memref<?xi32>
return %0 : memref<?xi32>
}

// -----

// CHECK-LABEL: memref_reinterpret_cast_dyn_shape
func.func @memref_reinterpret_cast_dyn_shape(%arg: memref<2x5xi32>, %size: index) -> memref<?xi32> {
// expected-error@+1 {{failed to legalize operation 'memref.reinterpret_cast'}}
%0 = memref.reinterpret_cast %arg to offset: [0], sizes: [%size], strides: [1] : memref<2x5xi32> to memref<?xi32>
return %0 : memref<?xi32>
}

// -----

// CHECK-LABEL: memref_reinterpret_cast_dyn_offset
func.func @memref_reinterpret_cast_dyn_offset(%arg: memref<2x5xi32>, %offset: index) -> memref<10xi32, strided<[1], offset: ?>> {
// expected-error@+1 {{failed to legalize operation 'memref.reinterpret_cast'}}
%0 = memref.reinterpret_cast %arg to offset: [%offset], sizes: [10], strides: [1] : memref<2x5xi32> to memref<10xi32, strided<[1], offset: ?>>
return %0 : memref<10xi32, strided<[1], offset:? >>
}

// -----

// CHECK-LABEL: memref_reinterpret_cast_static_offset
func.func @memref_reinterpret_cast_static_offset(%arg: memref<2x5xi32>) -> memref<10xi32, strided<[1], offset: 10>> {
// expected-error@+1 {{failed to legalize operation 'memref.reinterpret_cast'}}
%0 = memref.reinterpret_cast %arg to offset: [10], sizes: [10], strides: [1] : memref<2x5xi32> to memref<10xi32, strided<[1], offset: 10>>
return %0 : memref<10xi32, strided<[1], offset: 10>>
}

// -----

// CHECK-LABEL: memref_reinterpret_cast_static_strides
func.func @memref_reinterpret_cast_offset(%arg: memref<2x5xi32>) -> memref<10xi32, strided<[2], offset: 0>> {
// expected-error@+1 {{failed to legalize operation 'memref.reinterpret_cast'}}
%0 = memref.reinterpret_cast %arg to offset: [0], sizes: [10], strides: [2] : memref<2x5xi32> to memref<10xi32, strided<[2], offset: 0>>
return %0 : memref<10xi32, strided<[2], offset: 0>>
}

// -----

// CHECK-LABEL: memref_reinterpret_cast_dyn_strides
func.func @memref_reinterpret_cast_offset(%arg: memref<2x5xi32>, %stride: index) -> memref<10xi32, strided<[?], offset: 0>> {
// expected-error@+1 {{failed to legalize operation 'memref.reinterpret_cast'}}
%0 = memref.reinterpret_cast %arg to offset: [0], sizes: [10], strides: [%stride] : memref<2x5xi32> to memref<10xi32, strided<[?], offset: 0>>
return %0 : memref<10xi32, strided<[?], offset: 0>>
}
9 changes: 9 additions & 0 deletions mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,12 @@ func.func @memref_collapse_shape(%arg: memref<2x5xi32>) -> memref<10xi32> {
%0 = memref.collapse_shape %arg [[0, 1]] : memref<2x5xi32> into memref<10xi32>
return %0 : memref<10xi32>
}

// -----

// CHECK-LABEL: memref_reinterpret_cast
func.func @memref_reinterpret_cast(%arg: memref<2x5xi32>) -> memref<10xi32> {
// CHECK: emitc.cast %{{[^ ]*}} : !emitc.array<2x5xi32> to !emitc.array<10xi32> ref
%0 = memref.reinterpret_cast %arg to offset: [0], sizes: [10], strides: [1] : memref<2x5xi32> to memref<10xi32>
cferry-AMD marked this conversation as resolved.
Show resolved Hide resolved
return %0 : memref<10xi32>
}