[mlir] Factor type reconciliation out of Standard-to-LLVM conversion
Conversion to the LLVM dialect is being refactored to be more progressive and
is now performed as a series of independent passes converting different
dialects. These passes may produce `unrealized_conversion_cast` operations that
represent pending conversions between built-in and LLVM dialect types.
Historically, a more monolithic Standard-to-LLVM conversion pass did not need
these casts as all operations were converted in one shot. Previous refactorings
have led to the requirement of running the Standard-to-LLVM conversion pass to
clean up `unrealized_conversion_cast`s even though the IR had no standard
operations in it. The pass must have been also run the last among all to-LLVM
passes, in contradiction with the partial conversion logic. Additionally, the
way it was set up could produce invalid operations by removing casts between
LLVM and built-in types even when the consumer did not accept the uncasted
type, or could lead to cryptic conversion errors (recursive application of the
rewrite pattern on `unrealized_conversion_cast` as a means to indicate failure
to eliminate casts).
In fact, the need to eliminate A->B->A `unrealized_conversion_cast`s is not
specific to to-LLVM conversions and can be factored out into a separate type
reconciliation pass, which is achieved in this commit. While the cast operation
itself has a folder pattern, it is insufficient in most conversion passes as
the folder only applies to the second cast. Without complex legality setup in
the conversion target, the conversion infra will either consider the cast
operations valid and not fold them (a separate canonicalization would be
necessary to trigger the folding), or consider the first cast invalid upon
generation and stop with error. The pattern provided by the reconciliation pass
applies to the first cast operation instead. Furthermore, having a separate
pass makes it clear when `unrealized_conversion_cast`s could not have been
eliminated since it is the only reason why this pass can fail.
Reviewed By: nicolasvasilache
Differential Revision: https://reviews.llvm.org/D109507
2021-09-09 22:06:10 +08:00
|
|
|
// RUN: mlir-opt %s -convert-scf-to-std -convert-memref-to-llvm -convert-std-to-llvm='use-bare-ptr-memref-call-conv=1' -reconcile-unrealized-casts | mlir-cpu-runner -shared-libs=%linalg_test_lib_dir/libmlir_c_runner_utils%shlibext -entry-point-result=void | FileCheck %s
|
[mlir] Introduce bare ptr calling convention for MemRefs in LLVM dialect
Summary:
This patch introduces an alternative calling convention for
MemRef function arguments in LLVM dialect. It converts MemRef
function arguments to LLVM bare pointers to the MemRef element
type instead of creating a MemRef descriptor. Bare pointers are
then promoted to a MemRef descriptors at the beginning of the
function. This calling convention is only enabled with a flag.
Reviewers: ftynse, bondhugula, nicolasvasilache, rriddle, mehdi_amini
Reviewed By: ftynse, rriddle, mehdi_amini
Subscribers: Joonsoo, flaub, merge_guards_bot, jholewinski, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72802
2020-02-01 07:18:58 +08:00
|
|
|
|
|
|
|
// Verify bare pointer memref calling convention. `simple_add1_add2_test`
|
|
|
|
// gets two 2xf32 memrefs, adds 1.0f to the first one and 2.0f to the second
|
|
|
|
// one. 'main' calls 'simple_add1_add2_test' with {1, 1} and {2, 2} so {2, 2}
|
|
|
|
// and {4, 4} are the expected outputs.
|
|
|
|
|
|
|
|
func @simple_add1_add2_test(%arg0: memref<2xf32>, %arg1: memref<2xf32>) {
|
|
|
|
%c2 = constant 2 : index
|
|
|
|
%c0 = constant 0 : index
|
|
|
|
%c1 = constant 1 : index
|
|
|
|
%cst = constant 1.000000e+00 : f32
|
|
|
|
%cst_0 = constant 2.000000e+00 : f32
|
2020-05-13 18:12:30 +08:00
|
|
|
scf.for %arg2 = %c0 to %c2 step %c1 {
|
2021-02-10 20:53:11 +08:00
|
|
|
%0 = memref.load %arg0[%arg2] : memref<2xf32>
|
[mlir] Introduce bare ptr calling convention for MemRefs in LLVM dialect
Summary:
This patch introduces an alternative calling convention for
MemRef function arguments in LLVM dialect. It converts MemRef
function arguments to LLVM bare pointers to the MemRef element
type instead of creating a MemRef descriptor. Bare pointers are
then promoted to a MemRef descriptors at the beginning of the
function. This calling convention is only enabled with a flag.
Reviewers: ftynse, bondhugula, nicolasvasilache, rriddle, mehdi_amini
Reviewed By: ftynse, rriddle, mehdi_amini
Subscribers: Joonsoo, flaub, merge_guards_bot, jholewinski, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72802
2020-02-01 07:18:58 +08:00
|
|
|
%1 = addf %0, %cst : f32
|
2021-02-10 20:53:11 +08:00
|
|
|
memref.store %1, %arg0[%arg2] : memref<2xf32>
|
[mlir] Introduce bare ptr calling convention for MemRefs in LLVM dialect
Summary:
This patch introduces an alternative calling convention for
MemRef function arguments in LLVM dialect. It converts MemRef
function arguments to LLVM bare pointers to the MemRef element
type instead of creating a MemRef descriptor. Bare pointers are
then promoted to a MemRef descriptors at the beginning of the
function. This calling convention is only enabled with a flag.
Reviewers: ftynse, bondhugula, nicolasvasilache, rriddle, mehdi_amini
Reviewed By: ftynse, rriddle, mehdi_amini
Subscribers: Joonsoo, flaub, merge_guards_bot, jholewinski, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72802
2020-02-01 07:18:58 +08:00
|
|
|
// CHECK: 2, 2
|
|
|
|
|
2021-02-10 20:53:11 +08:00
|
|
|
%2 = memref.load %arg1[%arg2] : memref<2xf32>
|
[mlir] Introduce bare ptr calling convention for MemRefs in LLVM dialect
Summary:
This patch introduces an alternative calling convention for
MemRef function arguments in LLVM dialect. It converts MemRef
function arguments to LLVM bare pointers to the MemRef element
type instead of creating a MemRef descriptor. Bare pointers are
then promoted to a MemRef descriptors at the beginning of the
function. This calling convention is only enabled with a flag.
Reviewers: ftynse, bondhugula, nicolasvasilache, rriddle, mehdi_amini
Reviewed By: ftynse, rriddle, mehdi_amini
Subscribers: Joonsoo, flaub, merge_guards_bot, jholewinski, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72802
2020-02-01 07:18:58 +08:00
|
|
|
%3 = addf %1, %cst_0 : f32
|
2021-02-10 20:53:11 +08:00
|
|
|
memref.store %3, %arg1[%arg2] : memref<2xf32>
|
[mlir] Introduce bare ptr calling convention for MemRefs in LLVM dialect
Summary:
This patch introduces an alternative calling convention for
MemRef function arguments in LLVM dialect. It converts MemRef
function arguments to LLVM bare pointers to the MemRef element
type instead of creating a MemRef descriptor. Bare pointers are
then promoted to a MemRef descriptors at the beginning of the
function. This calling convention is only enabled with a flag.
Reviewers: ftynse, bondhugula, nicolasvasilache, rriddle, mehdi_amini
Reviewed By: ftynse, rriddle, mehdi_amini
Subscribers: Joonsoo, flaub, merge_guards_bot, jholewinski, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72802
2020-02-01 07:18:58 +08:00
|
|
|
// CHECK-NEXT: 4, 4
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// External declarations.
|
[mlir] replace LLVMIntegerType with built-in integer type
The LLVM dialect type system has been closed until now, i.e. did not support
types from other dialects inside containers. While this has had obvious
benefits of deriving from a common base class, it has led to some simple types
being almost identical with the built-in types, namely integer and floating
point types. This in turn has led to a lot of larger-scale complexity: simple
types must still be converted, numerous operations that correspond to LLVM IR
intrinsics are replicated to produce versions operating on either LLVM dialect
or built-in types leading to quasi-duplicate dialects, lowering to the LLVM
dialect is essentially required to be one-shot because of type conversion, etc.
In this light, it is reasonable to trade off some local complexity in the
internal implementation of LLVM dialect types for removing larger-scale system
complexity. Previous commits to the LLVM dialect type system have adapted the
API to support types from other dialects.
Replace LLVMIntegerType with the built-in IntegerType plus additional checks
that such types are signless (these are isolated in a utility function that
replaced `isa<LLVMType>` and in the parser). Temporarily keep the possibility
to parse `!llvm.i32` as a synonym for `i32`, but add a deprecation notice.
Reviewed By: mehdi_amini, silvas, antiagainst
Differential Revision: https://reviews.llvm.org/D94178
2021-01-06 23:19:04 +08:00
|
|
|
llvm.func @malloc(i64) -> !llvm.ptr<i8>
|
2020-08-04 17:37:50 +08:00
|
|
|
llvm.func @free(!llvm.ptr<i8>)
|
2020-11-14 05:04:53 +08:00
|
|
|
func private @printF32(%arg0: f32)
|
|
|
|
func private @printComma()
|
|
|
|
func private @printNewline()
|
[mlir] Introduce bare ptr calling convention for MemRefs in LLVM dialect
Summary:
This patch introduces an alternative calling convention for
MemRef function arguments in LLVM dialect. It converts MemRef
function arguments to LLVM bare pointers to the MemRef element
type instead of creating a MemRef descriptor. Bare pointers are
then promoted to a MemRef descriptors at the beginning of the
function. This calling convention is only enabled with a flag.
Reviewers: ftynse, bondhugula, nicolasvasilache, rriddle, mehdi_amini
Reviewed By: ftynse, rriddle, mehdi_amini
Subscribers: Joonsoo, flaub, merge_guards_bot, jholewinski, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72802
2020-02-01 07:18:58 +08:00
|
|
|
|
2020-09-30 04:19:23 +08:00
|
|
|
func @main()
|
|
|
|
{
|
|
|
|
%c2 = constant 2 : index
|
|
|
|
%c0 = constant 0 : index
|
|
|
|
%c1 = constant 1 : index
|
|
|
|
%cst = constant 1.000000e+00 : f32
|
|
|
|
%cst_0 = constant 2.000000e+00 : f32
|
2021-02-10 20:53:11 +08:00
|
|
|
%a = memref.alloc() : memref<2xf32>
|
|
|
|
%b = memref.alloc() : memref<2xf32>
|
2020-09-30 04:19:23 +08:00
|
|
|
scf.for %i = %c0 to %c2 step %c1 {
|
2021-02-10 20:53:11 +08:00
|
|
|
memref.store %cst, %a[%i] : memref<2xf32>
|
|
|
|
memref.store %cst, %b[%i] : memref<2xf32>
|
2020-09-30 04:19:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
call @simple_add1_add2_test(%a, %b) : (memref<2xf32>, memref<2xf32>) -> ()
|
[mlir] Introduce bare ptr calling convention for MemRefs in LLVM dialect
Summary:
This patch introduces an alternative calling convention for
MemRef function arguments in LLVM dialect. It converts MemRef
function arguments to LLVM bare pointers to the MemRef element
type instead of creating a MemRef descriptor. Bare pointers are
then promoted to a MemRef descriptors at the beginning of the
function. This calling convention is only enabled with a flag.
Reviewers: ftynse, bondhugula, nicolasvasilache, rriddle, mehdi_amini
Reviewed By: ftynse, rriddle, mehdi_amini
Subscribers: Joonsoo, flaub, merge_guards_bot, jholewinski, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72802
2020-02-01 07:18:58 +08:00
|
|
|
|
2021-02-10 20:53:11 +08:00
|
|
|
%l0 = memref.load %a[%c0] : memref<2xf32>
|
2020-09-30 04:19:23 +08:00
|
|
|
call @printF32(%l0) : (f32) -> ()
|
|
|
|
call @printComma() : () -> ()
|
2021-02-10 20:53:11 +08:00
|
|
|
%l1 = memref.load %a[%c1] : memref<2xf32>
|
2020-09-30 04:19:23 +08:00
|
|
|
call @printF32(%l1) : (f32) -> ()
|
|
|
|
call @printNewline() : () -> ()
|
[mlir] Introduce bare ptr calling convention for MemRefs in LLVM dialect
Summary:
This patch introduces an alternative calling convention for
MemRef function arguments in LLVM dialect. It converts MemRef
function arguments to LLVM bare pointers to the MemRef element
type instead of creating a MemRef descriptor. Bare pointers are
then promoted to a MemRef descriptors at the beginning of the
function. This calling convention is only enabled with a flag.
Reviewers: ftynse, bondhugula, nicolasvasilache, rriddle, mehdi_amini
Reviewed By: ftynse, rriddle, mehdi_amini
Subscribers: Joonsoo, flaub, merge_guards_bot, jholewinski, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72802
2020-02-01 07:18:58 +08:00
|
|
|
|
2021-02-10 20:53:11 +08:00
|
|
|
%l2 = memref.load %b[%c0] : memref<2xf32>
|
2020-09-30 04:19:23 +08:00
|
|
|
call @printF32(%l2) : (f32) -> ()
|
|
|
|
call @printComma() : () -> ()
|
2021-02-10 20:53:11 +08:00
|
|
|
%l3 = memref.load %b[%c1] : memref<2xf32>
|
2020-09-30 04:19:23 +08:00
|
|
|
call @printF32(%l3) : (f32) -> ()
|
|
|
|
call @printNewline() : () -> ()
|
|
|
|
|
2021-02-10 20:53:11 +08:00
|
|
|
memref.dealloc %a : memref<2xf32>
|
|
|
|
memref.dealloc %b : memref<2xf32>
|
2020-09-30 04:19:23 +08:00
|
|
|
return
|
[mlir] Introduce bare ptr calling convention for MemRefs in LLVM dialect
Summary:
This patch introduces an alternative calling convention for
MemRef function arguments in LLVM dialect. It converts MemRef
function arguments to LLVM bare pointers to the MemRef element
type instead of creating a MemRef descriptor. Bare pointers are
then promoted to a MemRef descriptors at the beginning of the
function. This calling convention is only enabled with a flag.
Reviewers: ftynse, bondhugula, nicolasvasilache, rriddle, mehdi_amini
Reviewed By: ftynse, rriddle, mehdi_amini
Subscribers: Joonsoo, flaub, merge_guards_bot, jholewinski, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72802
2020-02-01 07:18:58 +08:00
|
|
|
}
|