[Mlir-commits] [mlir] [mlir][EmitC] Expand the MemRefToEmitC pass - Lowering `reinterpret_cast` (PR #152610)
    Paul Kirth 
    llvmlistbot at llvm.org
       
    Fri Aug  8 11:13:10 PDT 2025
    
    
  
================
@@ -0,0 +1,16 @@
+// RUN: mlir-opt -convert-memref-to-emitc %s -split-input-file | FileCheck %s
+
+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
+}
+
+//CHECK: module {
+//CHECK-NEXT:   func.func @casting(%arg0: memref<999xi32>) {
+//CHECK-NEXT:     %0 = builtin.unrealized_conversion_cast %arg0 : memref<999xi32> to !emitc.array<999xi32>
+//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>>
----------------
ilovepi wrote:
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? 
https://github.com/llvm/llvm-project/pull/152610
    
    
More information about the Mlir-commits
mailing list