[PATCH] D139841: [mlir][test] Add expand-strided-metadata to lower-to-llvm

Quentin Colombet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 12 06:46:38 PST 2022


qcolombet created this revision.
qcolombet added reviewers: nicolasvasilache, ftynse, springerm.
qcolombet added a project: MLIR.
Herald added subscribers: Moerafaat, zero9178, bzcheeseman, awarzynski, sdasgup3, wenzhicui, wrengr, cota, teijeong, rdzhabarov, tatianashp, msifontes, jurahul, Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, mgester, arpith-jacob, antiagainst, shauheen, rriddle, mehdi_amini.
Herald added a project: All.
qcolombet requested review of this revision.
Herald added a subscriber: stephenneuendorffer.
Herald added a reviewer: dcaballe.

This patch adds the `ExpandStridedMetadataPass` to the LowerToLLVM named pipeline.
This is required now that "complex" memref operations, like subviews, need to be expanded before being lowered.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139841

Files:
  mlir/test/Dialect/LLVM/lower-to-llvm-e2e.mlir
  mlir/test/lib/Dialect/LLVM/CMakeLists.txt
  mlir/test/lib/Dialect/LLVM/TestLowerToLLVM.cpp
  utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel


Index: utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel
===================================================================
--- utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel
@@ -599,6 +599,7 @@
         "//mlir:LLVMDialect",
         "//mlir:MathToLLVM",
         "//mlir:MemRefToLLVM",
+        "//mlir:MemRefTransforms",
         "//mlir:Pass",
         "//mlir:ReconcileUnrealizedCasts",
         "//mlir:SCFToControlFlow",
Index: mlir/test/lib/Dialect/LLVM/TestLowerToLLVM.cpp
===================================================================
--- mlir/test/lib/Dialect/LLVM/TestLowerToLLVM.cpp
+++ mlir/test/lib/Dialect/LLVM/TestLowerToLLVM.cpp
@@ -24,6 +24,7 @@
 #include "mlir/Dialect/Func/IR/FuncOps.h"
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
 #include "mlir/Dialect/Linalg/Passes.h"
+#include "mlir/Dialect/MemRef/Transforms/Passes.h"
 #include "mlir/IR/DialectRegistry.h"
 #include "mlir/Pass/Pass.h"
 #include "mlir/Pass/PassManager.h"
@@ -68,6 +69,10 @@
           options.reassociateFPReductions)));
   // Convert Math to LLVM (always needed).
   pm.addNestedPass<func::FuncOp>(createConvertMathToLLVMPass());
+  // Expand complicated MemRef operations before lowering them.
+  pm.addPass(memref::createExpandStridedMetadataPass());
+  // The expansion may create affine expressions. Get rid of them.
+  pm.addPass(createLowerAffinePass());
   // Convert MemRef to LLVM (always needed).
   pm.addPass(createMemRefToLLVMConversionPass());
   // Convert Func to LLVM (always needed).
Index: mlir/test/lib/Dialect/LLVM/CMakeLists.txt
===================================================================
--- mlir/test/lib/Dialect/LLVM/CMakeLists.txt
+++ mlir/test/lib/Dialect/LLVM/CMakeLists.txt
@@ -15,6 +15,7 @@
   MLIRLinalgTransforms
   MLIRMathToLLVM
   MLIRMemRefToLLVM
+  MLIRMemRefTransforms
   MLIRPass
   MLIRReconcileUnrealizedCasts
   MLIRSCFToControlFlow
Index: mlir/test/Dialect/LLVM/lower-to-llvm-e2e.mlir
===================================================================
--- /dev/null
+++ mlir/test/Dialect/LLVM/lower-to-llvm-e2e.mlir
@@ -0,0 +1,45 @@
+// RUN: mlir-opt %s -o - -test-lower-to-llvm -cse -split-input-file | FileCheck %s
+// Note: We run CSE here to make the pattern matching more direct.
+
+// Check that we properly lower to llvm memref operations that require to be
+// expanded first, like `memref.subview`.
+func.func @subview(%0 : memref<64x4xf32, strided<[4, 1], offset: 0>>, %arg0 : index, %arg1 : index, %arg2 : index)
+-> memref<?x?xf32, strided<[?, ?], offset: ?>> {
+  // CHECK-LABEL: @subview
+  // CHECK-SAME: %[[BASE:[^:]*]]: !llvm.ptr<f32>,
+  // CHECK-SAME: %[[BASE_ALIGNED:[^:]*]]: !llvm.ptr<f32>,
+  // CHECK-SAME: %[[BASE_OFFSET:[^:]*]]: i64,
+  // CHECK-SAME: %[[BASE_STRIDE0:[^:]*]]: i64,
+  // CHECK-SAME: %[[BASE_STRIDE1:[^:]*]]: i64,
+  // CHECK-SAME: %[[BASE_SIZE0:[^:]*]]: i64,
+  // CHECK-SAME: %[[BASE_SIZE1:[^:]*]]: i64,
+  // CHECK-SAME: %[[ARG0:[^:]*]]: i64,
+  // CHECK-SAME: %[[ARG1:[^:]*]]: i64,
+  // CHECK-SAME: %[[ARG2:[^:]*]]: i64)
+  // CHECK-SAME: -> !llvm.struct<(ptr<f32>, ptr<f32>, i64, array<2 x i64>, array<2 x i64>
+
+  // CHECK-DAG: %[[STRIDE0:.*]] = llvm.mlir.constant(4 : index) : i64
+  // CHECK-DAG: %[[DESCSTRIDE0:.*]] = llvm.mul %[[ARG0]], %[[STRIDE0]] : i64
+  // CHECK-DAG: %[[OFF2:.*]] = llvm.add %[[DESCSTRIDE0]], %[[ARG1]] : i64
+  // CHECK-DAG: %[[DESC:.*]] = llvm.mlir.undef : !llvm.struct<(ptr<f32>, ptr<f32>, i64, array<2 x i64>, array<2 x i64>)>
+
+  // Base address and algined address.
+  // CHECK-DAG: %[[DESC0:.*]] = llvm.insertvalue %[[BASE]], %[[DESC]][0] : !llvm.struct<(ptr<f32>, ptr<f32>, i64, array<2 x i64>, array<2 x i64>)>
+  // CHECK-DAG: %[[DESC1:.*]] = llvm.insertvalue %[[BASE_ALIGNED]], %[[DESC0]][1] : !llvm.struct<(ptr<f32>, ptr<f32>, i64, array<2 x i64>, array<2 x i64>)>
+
+  // Offset.
+  // CHECK: %[[DESC2:.*]] = llvm.insertvalue %[[OFF2]], %[[DESC1]][2] : !llvm.struct<(ptr<f32>, ptr<f32>, i64, array<2 x i64>, array<2 x i64>)>
+  // Size 0.
+  // CHECK: %[[DESC3:.*]] = llvm.insertvalue %[[ARG0]], %[[DESC2]][3, 0] : !llvm.struct<(ptr<f32>, ptr<f32>, i64, array<2 x i64>, array<2 x i64>)>
+  // Stride 0 == 4 * %arg0.
+  // CHECK: %[[DESC4:.*]] = llvm.insertvalue %[[DESCSTRIDE0]], %[[DESC3]][4, 0] : !llvm.struct<(ptr<f32>, ptr<f32>, i64, array<2 x i64>, array<2 x i64>)>
+  // Size 1.
+  // CHECK: %[[DESC5:.*]] = llvm.insertvalue %[[ARG1]], %[[DESC4]][3, 1] : !llvm.struct<(ptr<f32>, ptr<f32>, i64, array<2 x i64>, array<2 x i64>)>
+  // Stride 1 == 1 * %arg1.
+  // CHECK: %[[DESC6:.*]] = llvm.insertvalue %[[ARG1]], %[[DESC5]][4, 1] : !llvm.struct<(ptr<f32>, ptr<f32>, i64, array<2 x i64>, array<2 x i64>)>
+
+  %1 = memref.subview %0[%arg0, %arg1][%arg0, %arg1][%arg0, %arg1] :
+    memref<64x4xf32, strided<[4, 1], offset: 0>>
+  to memref<?x?xf32, strided<[?, ?], offset: ?>>
+  return %1 : memref<?x?xf32, strided<[?, ?], offset: ?>>
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139841.482105.patch
Type: text/x-patch
Size: 4959 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221212/effc340b/attachment.bin>


More information about the llvm-commits mailing list