Files
clang-p2996/mlir/test/mlir-cpu-runner/memref-reshape.mlir
Matthias Springer eb6c4197d5 [mlir][CF] Split cf-to-llvm from func-to-llvm (#120580)
Do not run `cf-to-llvm` as part of `func-to-llvm`. This commit fixes
https://github.com/llvm/llvm-project/issues/70982.

This commit changes the way how `func.func` ops are lowered to LLVM.
Previously, the signature of the entire region (i.e., entry block and
all other blocks in the `func.func` op) was converted as part of the
`func.func` lowering pattern.

Now, only the entry block is converted. The remaining block signatures
are converted together with `cf.br` and `cf.cond_br` as part of
`cf-to-llvm`. All unstructured control flow is not converted as part of
a single pass (`cf-to-llvm`). `func-to-llvm` no longer deals with
unstructured control flow.

Also add more test cases for control flow dialect ops.

Note: This PR is in preparation of #120431, which adds an additional
GPU-specific lowering for `cf.assert`. This was a problem because
`cf.assert` used to be converted as part of `func-to-llvm`.

Note for LLVM integration: If you see failures, add
`-convert-cf-to-llvm` to your pass pipeline.
2024-12-20 13:46:45 +01:00

108 lines
4.3 KiB
MLIR

// RUN: mlir-opt %s -pass-pipeline="builtin.module(func.func(convert-scf-to-cf,memref-expand,convert-arith-to-llvm),finalize-memref-to-llvm,convert-func-to-llvm,convert-cf-to-llvm,reconcile-unrealized-casts)" \
// RUN: | mlir-cpu-runner -e main -entry-point-result=void \
// RUN: -shared-libs=%mlir_runner_utils,%mlir_c_runner_utils \
// RUN: | FileCheck %s
func.func private @printMemrefF32(memref<*xf32>) attributes { llvm.emit_c_interface }
func.func @main() -> () {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
// Initialize input.
%input = memref.alloc() : memref<2x3xf32>
%dim_x = memref.dim %input, %c0 : memref<2x3xf32>
%dim_y = memref.dim %input, %c1 : memref<2x3xf32>
scf.parallel (%i, %j) = (%c0, %c0) to (%dim_x, %dim_y) step (%c1, %c1) {
%prod = arith.muli %i, %dim_y : index
%val = arith.addi %prod, %j : index
%val_i64 = arith.index_cast %val : index to i64
%val_f32 = arith.sitofp %val_i64 : i64 to f32
memref.store %val_f32, %input[%i, %j] : memref<2x3xf32>
}
%unranked_input = memref.cast %input : memref<2x3xf32> to memref<*xf32>
call @printMemrefF32(%unranked_input) : (memref<*xf32>) -> ()
// CHECK: rank = 2 offset = 0 sizes = [2, 3] strides = [3, 1]
// CHECK-NEXT: [0, 1, 2]
// CHECK-NEXT: [3, 4, 5]
// Initialize shape.
%shape = memref.alloc() : memref<2xindex>
%c2 = arith.constant 2 : index
%c3 = arith.constant 3 : index
memref.store %c3, %shape[%c0] : memref<2xindex>
memref.store %c2, %shape[%c1] : memref<2xindex>
// Test cases.
call @reshape_ranked_memref_to_ranked(%input, %shape)
: (memref<2x3xf32>, memref<2xindex>) -> ()
call @reshape_unranked_memref_to_ranked(%input, %shape)
: (memref<2x3xf32>, memref<2xindex>) -> ()
call @reshape_ranked_memref_to_unranked(%input, %shape)
: (memref<2x3xf32>, memref<2xindex>) -> ()
call @reshape_unranked_memref_to_unranked(%input, %shape)
: (memref<2x3xf32>, memref<2xindex>) -> ()
memref.dealloc %input : memref<2x3xf32>
memref.dealloc %shape : memref<2xindex>
return
}
func.func @reshape_ranked_memref_to_ranked(%input : memref<2x3xf32>,
%shape : memref<2xindex>) {
%output = memref.reshape %input(%shape)
: (memref<2x3xf32>, memref<2xindex>) -> memref<?x?xf32>
%unranked_output = memref.cast %output : memref<?x?xf32> to memref<*xf32>
call @printMemrefF32(%unranked_output) : (memref<*xf32>) -> ()
// CHECK: rank = 2 offset = 0 sizes = [3, 2] strides = [2, 1] data =
// CHECK: [0, 1],
// CHECK: [2, 3],
// CHECK: [4, 5]
return
}
func.func @reshape_unranked_memref_to_ranked(%input : memref<2x3xf32>,
%shape : memref<2xindex>) {
%unranked_input = memref.cast %input : memref<2x3xf32> to memref<*xf32>
%output = memref.reshape %input(%shape)
: (memref<2x3xf32>, memref<2xindex>) -> memref<?x?xf32>
%unranked_output = memref.cast %output : memref<?x?xf32> to memref<*xf32>
call @printMemrefF32(%unranked_output) : (memref<*xf32>) -> ()
// CHECK: rank = 2 offset = 0 sizes = [3, 2] strides = [2, 1] data =
// CHECK: [0, 1],
// CHECK: [2, 3],
// CHECK: [4, 5]
return
}
func.func @reshape_ranked_memref_to_unranked(%input : memref<2x3xf32>,
%shape : memref<2xindex>) {
%dyn_size_shape = memref.cast %shape : memref<2xindex> to memref<?xindex>
%output = memref.reshape %input(%dyn_size_shape)
: (memref<2x3xf32>, memref<?xindex>) -> memref<*xf32>
call @printMemrefF32(%output) : (memref<*xf32>) -> ()
// CHECK: rank = 2 offset = 0 sizes = [3, 2] strides = [2, 1] data =
// CHECK: [0, 1],
// CHECK: [2, 3],
// CHECK: [4, 5]
return
}
func.func @reshape_unranked_memref_to_unranked(%input : memref<2x3xf32>,
%shape : memref<2xindex>) {
%unranked_input = memref.cast %input : memref<2x3xf32> to memref<*xf32>
%dyn_size_shape = memref.cast %shape : memref<2xindex> to memref<?xindex>
%output = memref.reshape %input(%dyn_size_shape)
: (memref<2x3xf32>, memref<?xindex>) -> memref<*xf32>
call @printMemrefF32(%output) : (memref<*xf32>) -> ()
// CHECK: rank = 2 offset = 0 sizes = [3, 2] strides = [2, 1] data =
// CHECK: [0, 1],
// CHECK: [2, 3],
// CHECK: [4, 5]
return
}