8000 [mlir][memref] Revert #140730 by banach-space · Pull Request #141406 · llvm/llvm-project · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[mlir][memref] Revert #140730 #141406

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 pr 8000 ivacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2025
Merged

Conversation

banach-space
Copy link
Contributor
@banach-space banach-space commented May 25, 2025

Reverts #140730 - that turned out not to be an NFC as we originally
thought. See the attached test for an example. Many thanks to @Garra1980
for reporting!

Note, without this change, the newly added test would be incorrectly converted
to:

  func.func @view_memref_as_rank0(%arg0: index, %arg1: memref<2xi8>) {
    %0 = llvm.mlir.poison : !llvm.struct<(ptr, ptr, i64)>
    return
  }

Reverts llvm#140730 - that turned out not to be an NFC as we originally
thought. See the attached test for an example. Many thanks to @Garra1980
for reporting!
@llvmbot llvmbot added the mlir label May 25, 2025
@llvmbot
Copy link
Member
llvmbot commented May 25, 2025

@llvm/pr-subscribers-mlir

Author: Andrzej Warzyński (banach-space)

Changes

Reverts #140730 - that turned out not to be an NFC as we originally
thought. See the attached test for an example. Many thanks to @Garra1980
for reporting!


Full diff: https://github.com/llvm/llvm-project/pull/141406.diff

2 Files Affected:

  • (modified) mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp (+4-4)
  • (modified) mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir (+24-2)
diff --git a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
index 339f8a61136fc..ade4e4d3de8ec 100644
--- a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
+++ b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
@@ -1721,10 +1721,6 @@ struct ViewOpLowering : public ConvertOpToLLVMPattern<memref::ViewOp> {
     MemRefDescriptor sourceMemRef(adaptor.getSource());
     auto targetMemRef = MemRefDescriptor::poison(rewriter, loc, targetDescTy);
 
-    // Early exit for 0-D corner case.
-    if (viewMemRefType.getRank() == 0)
-      return rewriter.replaceOp(viewOp, {targetMemRef}), success();
-
     // Field 1: Copy the allocated pointer, used for malloc/free.
     Value allocatedPtr = sourceMemRef.allocatedPtr(rewriter, loc);
     auto srcMemRefType = cast<MemRefType>(viewOp.getSource().getType());
@@ -1747,6 +1743,10 @@ struct ViewOpLowering : public ConvertOpToLLVMPattern<memref::ViewOp> {
         rewriter, loc,
         createIndexAttrConstant(rewriter, loc, indexType, offset));
 
+    // Early exit for 0-D corner case.
+    if (viewMemRefType.getRank() == 0)
+      return rewriter.replaceOp(viewOp, {targetMemRef}), success();
+
     // Fields 4 and 5: Update sizes and strides.
     Value stride = nullptr, nextSize = nullptr;
     for (int i = viewMemRefType.getRank() - 1; i >= 0; --i) {
diff --git a/mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir b/mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir
index 5538ddf8e4c3c..ffa8a76ba03e1 100644
--- a/mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir
+++ b/mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir
@@ -1,10 +1,10 @@
-// RUN: mlir-opt -finalize-memref-to-llvm %s -split-input-file | FileCheck %s
+// RUN: mlir-opt -finalize-memref-to-llvm %s -split-input-file | FileCheck --check-prefixes=ALL,CHECK %s
 // RUN: mlir-opt -finalize-memref-to-llvm='index-bitwidth=32' %s -split-input-file | FileCheck --check-prefix=CHECK32 %s
 
 // Same below, but using the `ConvertToLLVMPatternInterface` entry point
 // and the generic `convert-to-llvm` pass. This produces slightly different IR
 // because the conversion target is set up differently.
-// RUN: mlir-opt --convert-to-llvm="filter-dialects=memref" --split-input-file %s | FileCheck --check-prefix=CHECK-INTERFACE %s
+// RUN: mlir-opt --convert-to-llvm="filter-dialects=memref" --split-input-file %s | FileCheck --check-prefixes=ALL,CHECK-INTERFACE %s
 
 // CHECK-LABEL: func @view(
 // CHECK: %[[ARG0F:.*]]: index, %[[ARG1F:.*]]: index, %[[ARG2F:.*]]: index
@@ -132,6 +132,28 @@ func.func @view_empty_memref(%offset: index, %mem: memref<0xi8>) {
 
 // -----
 
+// ALL-LABEL:   func.func @view_memref_as_rank0(
+// ALL-SAME:      %[[ARG0:.*]]: index,
+// ALL-SAME:      %[[ARG1:.*]]: memref<2xi8>) {
+func.func @view_memref_as_rank0(%offset: index, %mem: memref<2xi8>) {
+
+  // ALL: %[[VAL_0:.*]] = builtin.unrealized_conversion_cast %[[ARG0]] : index to i64
+  // ALL: %[[VAL_1:.*]] = builtin.unrealized_conversion_cast %[[ARG1]] : memref<2xi8> to !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  // ALL: %[[VAL_2:.*]] = llvm.mlir.poison : !llvm.struct<(ptr, ptr, i64)>
+  // ALL: %[[VAL_3:.*]] = llvm.extractvalue %[[VAL_1]][0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  // ALL: %[[VAL_4:.*]] = llvm.insertvalue %[[VAL_3]], %[[VAL_2]][0] : !llvm.struct<(ptr, ptr, i64)>
+  // ALL: %[[VAL_5:.*]] = llvm.extractvalue %[[VAL_1]][1] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  // ALL: %[[VAL_6:.*]] = llvm.getelementptr %[[VAL_5]]{{\[}}%[[VAL_0]]] : (!llvm.ptr, i64) -> !llvm.ptr, i8
+  // ALL: %[[VAL_7:.*]] = llvm.insertvalue %[[VAL_6]], %[[VAL_4]][1] : !llvm.struct<(ptr, ptr, i64)>
+  // ALL: %[[VAL_8:.*]] = llvm.mlir.constant(0 : index) : i64
+  // ALL: %[[VAL_9:.*]] = llvm.insertvalue %[[VAL_8]], %[[VAL_7]][2] : !llvm.struct<(ptr, ptr, i64)>
+  %memref_view_bf16 = memref.view %mem[%offset][] : memref<2xi8> to memref<bf16>
+
+  return
+}
+
+// -----
+
 // Subviews needs to be expanded outside of the memref-to-llvm pass.
 // CHECK-LABEL: func @subview(
 // CHECK:         %[[MEMREF:.*]]: memref<{{.*}}>,

Add a TODO to re-use check lines, remove LIT variables (for consistency with existing examples)
@banach-space banach-space merged commit dca74f7 into llvm:main May 25, 2025
11 checks passed
@banach-space banach-space deleted the andrzej/revert/140730 branch May 25, 2025 16:50
@Garra1980
Copy link

Thanks a lot!

sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
Reverts llvm#140730 - that turned out not to be an NFC as we originally
thought. See the attached test for an example. Many thanks to @Garra1980
for reporting!

Note, without this change, the newly added test would be incorrectly
converted to:
```mlir
  func.func @view_memref_as_rank0(%arg0: index, %arg1: memref<2xi8>) {
    %0 = llvm.mlir.poison : !llvm.struct<(ptr, ptr, i64)>
    return
  }
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0