Skip to content

[mlir][memref] Fix type conversion in emulate-wide-int and emulate-narrow-type #112214

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

Merged
merged 1 commit into from
Oct 17, 2024
Merged
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
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Arith/Transforms/EmulateNarrowType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ arith::NarrowTypeEmulationConverter::NarrowTypeEmulationConverter(
addConversion([this](FunctionType ty) -> std::optional<Type> {
SmallVector<Type> inputs;
if (failed(convertTypes(ty.getInputs(), inputs)))
return std::nullopt;
return nullptr;

SmallVector<Type> results;
if (failed(convertTypes(ty.getResults(), results)))
return std::nullopt;
return nullptr;

return FunctionType::get(ty.getContext(), inputs, results);
});
Expand Down
17 changes: 9 additions & 8 deletions mlir/lib/Dialect/MemRef/Transforms/EmulateNarrowType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ struct ConvertMemRefAllocation final : OpConversionPattern<OpTy> {
std::is_same<OpTy, memref::AllocaOp>(),
"expected only memref::AllocOp or memref::AllocaOp");
auto currentType = cast<MemRefType>(op.getMemref().getType());
auto newResultType = dyn_cast<MemRefType>(
this->getTypeConverter()->convertType(op.getType()));
auto newResultType =
this->getTypeConverter()->template convertType<MemRefType>(
op.getType());
if (!newResultType) {
return rewriter.notifyMatchFailure(
op->getLoc(),
Expand Down Expand Up @@ -378,7 +379,7 @@ struct ConvertMemRefReinterpretCast final
matchAndRewrite(memref::ReinterpretCastOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
MemRefType newTy =
dyn_cast<MemRefType>(getTypeConverter()->convertType(op.getType()));
getTypeConverter()->convertType<MemRefType>(op.getType());
if (!newTy) {
return rewriter.notifyMatchFailure(
op->getLoc(),
Expand Down Expand Up @@ -466,8 +467,8 @@ struct ConvertMemRefSubview final : OpConversionPattern<memref::SubViewOp> {
LogicalResult
matchAndRewrite(memref::SubViewOp subViewOp, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
MemRefType newTy = dyn_cast<MemRefType>(
getTypeConverter()->convertType(subViewOp.getType()));
MemRefType newTy =
getTypeConverter()->convertType<MemRefType>(subViewOp.getType());
if (!newTy) {
return rewriter.notifyMatchFailure(
subViewOp->getLoc(),
Expand Down Expand Up @@ -632,14 +633,14 @@ void memref::populateMemRefNarrowTypeEmulationConversions(
SmallVector<int64_t> strides;
int64_t offset;
if (failed(getStridesAndOffset(ty, strides, offset)))
return std::nullopt;
return nullptr;
if (!strides.empty() && strides.back() != 1)
return std::nullopt;
return nullptr;

auto newElemTy = IntegerType::get(ty.getContext(), loadStoreWidth,
intTy.getSignedness());
if (!newElemTy)
return std::nullopt;
return nullptr;

StridedLayoutAttr layoutAttr;
// If the offset is 0, we do not need a strided layout as the stride is
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/MemRef/Transforms/EmulateWideInt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void memref::populateMemRefWideIntEmulationConversions(

Type newElemTy = typeConverter.convertType(intTy);
if (!newElemTy)
return std::nullopt;
return nullptr;

return ty.cloneWith(std::nullopt, newElemTy);
});
Expand Down
21 changes: 11 additions & 10 deletions mlir/test/Dialect/MemRef/emulate-narrow-type.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func.func @memref_subview_dynamic_offset_i4(%idx : index) -> i4 {

// -----


func.func @negative_memref_subview_non_contiguous(%idx : index) -> i4 {
%c0 = arith.constant 0 : index
%arr = memref.alloc() : memref<40x40xi4>
Expand Down Expand Up @@ -543,13 +542,15 @@ func.func @memref_copy_i4(%arg0: memref<32x128xi4, 1>, %arg1: memref<32x128xi4>)

// -----

!colMajor = memref<8x8xi4, strided<[1, 8]>>
func.func @copy_distinct_layouts(%idx : index) -> i4 {
%c0 = arith.constant 0 : index
%arr = memref.alloc() : memref<8x8xi4>
%arr2 = memref.alloc() : !colMajor
// expected-error @+1 {{failed to legalize operation 'memref.copy' that was explicitly marked illegal}}
memref.copy %arr, %arr2 : memref<8x8xi4> to !colMajor
%ld = memref.load %arr2[%c0, %c0] : !colMajor
return %ld : i4
func.func @alloc_non_contiguous() {
// expected-error @+1 {{failed to legalize operation 'memref.alloc' that was explicitly marked illegal}}
%arr = memref.alloc() : memref<8x8xi4, strided<[1, 8]>>
return
}

// -----

// expected-error @+1 {{failed to legalize operation 'func.func' that was explicitly marked illegal}}
func.func @argument_non_contiguous(%arg0 : memref<8x8xi4, strided<[1, 8]>>) {
return
}
35 changes: 34 additions & 1 deletion mlir/test/Dialect/MemRef/emulate-wide-int.mlir
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: mlir-opt --memref-emulate-wide-int="widest-int-supported=32" %s | FileCheck %s
// RUN: mlir-opt --memref-emulate-wide-int="widest-int-supported=32" %s \
// RUN: --split-input-file --verify-diagnostics | FileCheck %s

// Expect no conversions, i32 is supported.
// CHECK-LABEL: func @memref_i32
Expand All @@ -15,6 +16,8 @@ func.func @memref_i32() {
return
}

// -----

// Expect no conversions, f64 is not an integer type.
// CHECK-LABEL: func @memref_f32
// CHECK: [[M:%.+]] = memref.alloc() : memref<4xf32, 1>
Expand All @@ -30,6 +33,8 @@ func.func @memref_f32() {
return
}

// -----

// CHECK-LABEL: func @alloc_load_store_i64
// CHECK: [[C1:%.+]] = arith.constant dense<[1, 0]> : vector<2xi32>
// CHECK-NEXT: [[M:%.+]] = memref.alloc() : memref<4xvector<2xi32>, 1>
Expand All @@ -45,6 +50,7 @@ func.func @alloc_load_store_i64() {
return
}

// -----

// CHECK-LABEL: func @alloc_load_store_i64_nontemporal
// CHECK: [[C1:%.+]] = arith.constant dense<[1, 0]> : vector<2xi32>
Expand All @@ -60,3 +66,30 @@ func.func @alloc_load_store_i64_nontemporal() {
memref.store %c1, %m[%c0] {nontemporal = true} : memref<4xi64, 1>
return
}

// -----

// Make sure we do not crash on unsupported types.
func.func @alloc_i128() {
// expected-error@+1 {{failed to legalize operation 'memref.alloc' that was explicitly marked illegal}}
%m = memref.alloc() : memref<4xi128, 1>
return
}

// -----

func.func @load_i128(%m: memref<4xi128, 1>) {
%c0 = arith.constant 0 : index
// expected-error@+1 {{failed to legalize operation 'memref.load' that was explicitly marked illegal}}
%v = memref.load %m[%c0] : memref<4xi128, 1>
return
}

// -----

func.func @store_i128(%c1: i128, %m: memref<4xi128, 1>) {
%c0 = arith.constant 0 : index
// expected-error@+1 {{failed to legalize operation 'memref.store' that was explicitly marked illegal}}
memref.store %c1, %m[%c0] : memref<4xi128, 1>
return
}
Loading