-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: feature/fused-ops
Are you sure you want to change the base?
Conversation
|
||
// 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
No description provided.