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 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
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>>
}
18 changes: 18 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,21 @@ 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_subset
func.func @memref_reinterpret_cast_subset(%arg: memref<2x5xi32>) -> memref<8xi32> {
// CHECK: emitc.cast %{{[^ ]*}} : !emitc.array<2x5xi32> to !emitc.array<8xi32> ref
Copy link

Choose a reason for hiding this comment

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

Does this represent

int ar[2][5];
int newAr[8] = (int[8]) ar;

?
That does not look like valid C/C++ to me.
From what I understand from cppreference is that even a reinterpret_cast (reinterpret_cast<int[8]>(ar)) would be undefined behavior.
Am I missing something here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unless I'm wrong, this code introduces a C++ lvalue reference, like this:

int ar[2][5];
int (&newAr)[8] = (int (&)[8]) ar;

which makes it "compilable" C++ code.

About the undefined behavior: this memref op will let us create any descriptor from any other memref descriptor, so I would expect undefined behavior could happen, but such behavior already existed in the original MLIR. Do you think this lowering is introducing otherwise avoidable undefined behavior?

Copy link
Collaborator

@mgehre-amd mgehre-amd Nov 4, 2024

Choose a reason for hiding this comment

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

I don't think it's undefined behavior while we are in the memref dialect because the dialect defines it as not being undefined.
On the C++ level, this clause of reinterpret_cast applies

An glvalue expression of type T1 can be converted to reference to another type T2. The result is that of *reinterpret_cast<T2*>(p), where p is a pointer of type “pointer to T1” to the object or function designated by expression. No temporary is created, no copy is made, no constructors or conversion functions are called. The resulting reference can only be accessed safely if it is type-accessible.

Type accessibility
If a type T_ref is similar to any of the following types, an object of dynamic type T_obj is type-accessible through a glvalue of type T_ref:

  • char
  • unsigned char
  • std::byte
  • T_obj (this clause applies here)
  • the signed or unsigned type corresponding to T_obj

If a program attempts to read or modify the stored value of an object through a glvalue) through which it is not type-accessible, the behavior is undefined.

Similar types
Informally, two types are similar if, ignoring top-level cv-qualification:

  • they are the same type; or
  • they are both pointers, and the pointed-to types are similar; or
  • they are both pointers to member of the same class, and the types of the pointed-to members are similar; or
  • they are both arrays and the array element types are similar. (this clause applies here)

So to judge whether the access through the cast is fine (the cast itself is never UB), it depends on whether the element type of int ar[2][5]; is int ar[2] or int. If it's the latter, then it would be the same as the element type of int [8], which would makes both types similar and thus type-accessible.

%0 = memref.reinterpret_cast %arg to offset: [0], sizes: [8], strides: [1] : memref<2x5xi32> to memref<8xi32>
return %0 : memref<8xi32>
}

// -----

// CHECK-LABEL: memref_reinterpret_cast_reshape
func.func @memref_reinterpret_cast_reshape(%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>
}