Skip to content

Conversation

@Jaddyen
Copy link
Contributor

@Jaddyen Jaddyen commented Aug 7, 2025

This patch lowers memref.reinterpret_cast.
From:

func.func @casting(%arg0: memref<999xi32>) {
  %reinterpret_cast_5 = memref.reinterpret_cast %arg0 to offset: [0], sizes: [1, 1, 999], strides: [999, 999, 1] : memref<999xi32> to memref<1x1x999xi32>
  return
}

To:

void casting(int32_t v1[999]) {
  std::size_t v2 = 0;
  int32_t* v3 = &v1[v2];
  int32_t(*v4)[1][1][999]  = reinterpret_cast<int32_t(*)[1][1][999]>(v3);
  return;
}

@Jaddyen Jaddyen requested review from ilovepi and jpienaar August 7, 2025 23:15
@ilovepi
Copy link
Contributor

ilovepi commented Aug 8, 2025

In your example, int32_t(*)[1][1][999] v4 is invalid syntax, right? Shouldn't it be: int32_t(*v4)[1][1][999]? Is that a typo in the description? or is that what's generated/translated?

Comment on lines +300 to +301
emitc::ConstantOp zeroIndex = rewriter.create<emitc::ConstantOp>(
loc, rewriter.getIndexType(), rewriter.getIndexAttr(0));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I've seen a zeroIndex crated in several places now. Its not much code, but that may be a good candidate for a helper function (e.g. in the anonymous namespace and marked static).

Comment on lines +307 to +310
llvm::SmallVector<mlir::Value> indices;
for (int i = 0; i < rank; ++i) {
indices.push_back(zeroIndex);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
llvm::SmallVector<mlir::Value> indices;
for (int i = 0; i < rank; ++i) {
indices.push_back(zeroIndex);
}
llvm::SmallVector<mlir::Value> indices(rank, zeroIndex);

Comment on lines +303 to +310
auto createPointerFromEmitcArray =
[loc, &rewriter, &zeroIndex](
mlir::TypedValue<emitc::ArrayType> arrayValue) -> emitc::ApplyOp {
int64_t rank = arrayValue.getType().getRank();
llvm::SmallVector<mlir::Value> indices;
for (int i = 0; i < rank; ++i) {
indices.push_back(zeroIndex);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This closure seems familar from your other patches. I'm guessing that means it should probably be a helper function instead, since I don't see anything in the capture list that couldn't be a parameter.

//CHECK-NEXT: %1 = "emitc.constant"() <{value = 0 : index}> : () -> index
//CHECK-NEXT: %2 = emitc.subscript %0[%1] : (!emitc.array<999xi32>, index) -> !emitc.lvalue<i32>
//CHECK-NEXT: %3 = emitc.apply "&"(%2) : (!emitc.lvalue<i32>) -> !emitc.ptr<i32>
//CHECK-NEXT: %4 = emitc.call_opaque "reinterpret_cast"(%3) {args = [0 : index], template_args = [!emitc.ptr<!emitc.array<1x1x999xi32>>]} : (!emitc.ptr<i32>) -> !emitc.ptr<!emitc.array<1x1x999xi32>>
Copy link
Contributor

Choose a reason for hiding this comment

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

an actual reinterpret_cast<T>() is appropriate for C++, but for C, you'd need a C-style cast. given that the C-style cast will work in both languages and what you're doing can't be covered by static or dyn casts, maybe we should use a normal cast operation? Would that work here, do you think?

Copy link
Member

Choose a reason for hiding this comment

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

+1, good to check.

indices.push_back(zeroIndex);
}

emitc::SubscriptOp subPtr = rewriter.create<emitc::SubscriptOp>(
Copy link
Member

Choose a reason for hiding this comment

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

If you switch to the new builder form (emit::SubscriptOp::create(...)) you should no longer need ValueRange here.

auto castCall = rewriter.create<emitc::CastOp>(
loc, emitc::PointerType::get(targetInEmitC), srcPtr.getResult());

rewriter.replaceOp(castOp, castCall);
Copy link
Member

Choose a reason for hiding this comment

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

You can combine this with the preceding by doing rewriter.replaceWithNewOp


LogicalResult CppEmitter::emitVariableDeclaration(Location loc, Type type,
StringRef name) {
if (auto pType = dyn_cast<emitc::PointerType>(type)) {
Copy link
Member

Choose a reason for hiding this comment

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

Could this be a standalone PR? (this feels like it is a general refinement).

//CHECK-NEXT: %1 = "emitc.constant"() <{value = 0 : index}> : () -> index
//CHECK-NEXT: %2 = emitc.subscript %0[%1] : (!emitc.array<999xi32>, index) -> !emitc.lvalue<i32>
//CHECK-NEXT: %3 = emitc.apply "&"(%2) : (!emitc.lvalue<i32>) -> !emitc.ptr<i32>
//CHECK-NEXT: %4 = emitc.call_opaque "reinterpret_cast"(%3) {args = [0 : index], template_args = [!emitc.ptr<!emitc.array<1x1x999xi32>>]} : (!emitc.ptr<i32>) -> !emitc.ptr<!emitc.array<1x1x999xi32>>
Copy link
Member

Choose a reason for hiding this comment

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

+1, good to check.

return rewriter.notifyMatchFailure(castOp.getLoc(),
"cannot convert memref type");
}
Location loc = castOp.getLoc();
Copy link
Member

Choose a reason for hiding this comment

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

Empty lines are for me similar to paragraphs in text, it creates logical separations/groupings which aid reading. Here I can't quite figure those out.

@@ -0,0 +1,16 @@
// RUN: mlir-opt -convert-memref-to-emitc %s -split-input-file | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// RUN: mlir-opt -convert-memref-to-emitc %s -split-input-file | FileCheck %s
// RUN: mlir-opt -convert-memref-to-emitc %s | FileCheck %s

The -split-input-file option must be dropped since there is no need to use it. Test includes only one function.

auto srcArrayValue =
cast<TypedValue<emitc::ArrayType>>(adaptor.getSource());

emitc::ConstantOp zeroIndex = rewriter.create<emitc::ConstantOp>(
Copy link
Contributor

Choose a reason for hiding this comment

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

Using emitc::Literal can reduce emitted code by avoiding extra creation of constants, as the literal's value is emitted directly in place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants