diff --git a/mlir/test/Dialect/LLVM/lower-to-llvm-e2e.mlir b/mlir/test/Dialect/LLVM/lower-to-llvm-e2e.mlir new file mode 100644 --- /dev/null +++ b/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> { + // CHECK-LABEL: @subview + // CHECK-SAME: %[[BASE:[^:]*]]: !llvm.ptr, + // CHECK-SAME: %[[BASE_ALIGNED:[^:]*]]: !llvm.ptr, + // 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, ptr, 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, ptr, 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, ptr, i64, array<2 x i64>, array<2 x i64>)> + // CHECK-DAG: %[[DESC1:.*]] = llvm.insertvalue %[[BASE_ALIGNED]], %[[DESC0]][1] : !llvm.struct<(ptr, ptr, i64, array<2 x i64>, array<2 x i64>)> + + // Offset. + // CHECK: %[[DESC2:.*]] = llvm.insertvalue %[[OFF2]], %[[DESC1]][2] : !llvm.struct<(ptr, ptr, i64, array<2 x i64>, array<2 x i64>)> + // Size 0. + // CHECK: %[[DESC3:.*]] = llvm.insertvalue %[[ARG0]], %[[DESC2]][3, 0] : !llvm.struct<(ptr, ptr, i64, array<2 x i64>, array<2 x i64>)> + // Stride 0 == 4 * %arg0. + // CHECK: %[[DESC4:.*]] = llvm.insertvalue %[[DESCSTRIDE0]], %[[DESC3]][4, 0] : !llvm.struct<(ptr, ptr, i64, array<2 x i64>, array<2 x i64>)> + // Size 1. + // CHECK: %[[DESC5:.*]] = llvm.insertvalue %[[ARG1]], %[[DESC4]][3, 1] : !llvm.struct<(ptr, ptr, i64, array<2 x i64>, array<2 x i64>)> + // Stride 1 == 1 * %arg1. + // CHECK: %[[DESC6:.*]] = llvm.insertvalue %[[ARG1]], %[[DESC5]][4, 1] : !llvm.struct<(ptr, ptr, 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> + return %1 : memref> +} diff --git a/mlir/test/lib/Dialect/LLVM/CMakeLists.txt b/mlir/test/lib/Dialect/LLVM/CMakeLists.txt --- a/mlir/test/lib/Dialect/LLVM/CMakeLists.txt +++ b/mlir/test/lib/Dialect/LLVM/CMakeLists.txt @@ -15,6 +15,7 @@ MLIRLinalgTransforms MLIRMathToLLVM MLIRMemRefToLLVM + MLIRMemRefTransforms MLIRPass MLIRReconcileUnrealizedCasts MLIRSCFToControlFlow diff --git a/mlir/test/lib/Dialect/LLVM/TestLowerToLLVM.cpp b/mlir/test/lib/Dialect/LLVM/TestLowerToLLVM.cpp --- a/mlir/test/lib/Dialect/LLVM/TestLowerToLLVM.cpp +++ b/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(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). diff --git a/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel --- a/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel +++ b/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",