[flang-commits] [flang] ff3e75f - [flang][lowering] Add support for lowering of the `{l|u}bound` intrinsics

Andrzej Warzynski via flang-commits flang-commits at lists.llvm.org
Fri Mar 18 03:17:03 PDT 2022


Author: Andrzej Warzynski
Date: 2022-03-18T10:16:54Z
New Revision: ff3e75f7390e387f8134bacd9c862475a08d3348

URL: https://github.com/llvm/llvm-project/commit/ff3e75f7390e387f8134bacd9c862475a08d3348
DIFF: https://github.com/llvm/llvm-project/commit/ff3e75f7390e387f8134bacd9c862475a08d3348.diff

LOG: [flang][lowering] Add support for lowering of the `{l|u}bound` intrinsics

This patch adds support for lowering of the `{l|u}bound` intrinsics from
Fortran to the FIR dialect of MLIR. Note that `ubound` is already
supported, but the test was missing (added here).

This is part of the upstreaming effort from the `fir-dev` branch in [1].

[1] https://github.com/flang-compiler/f18-llvm-project

Differential Revision: https://reviews.llvm.org/D121926

Co-authored-by: Jean Perier <jperier at nvidia.com>
Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>
Co-authored-by: Peter Steinfeld <psteinfeld at nvidia.com>

Added: 
    flang/test/Lower/Intrinsics/lbound.f90
    flang/test/Lower/Intrinsics/ubound.f90

Modified: 
    flang/lib/Lower/IntrinsicCall.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp
index 16a250e659200..8fba5c180175a 100644
--- a/flang/lib/Lower/IntrinsicCall.cpp
+++ b/flang/lib/Lower/IntrinsicCall.cpp
@@ -737,6 +737,10 @@ static constexpr IntrinsicHandler handlers[]{
        {"kind", asValue}}}},
     {"ishft", &I::genIshft},
     {"ishftc", &I::genIshftc},
+    {"lbound",
+     &I::genLbound,
+     {{{"array", asInquired}, {"dim", asValue}, {"kind", asValue}}},
+     /*isElemental=*/false},
     {"len",
      &I::genLen,
      {{{"string", asInquired}, {"kind", asValue}}},

diff  --git a/flang/test/Lower/Intrinsics/lbound.f90 b/flang/test/Lower/Intrinsics/lbound.f90
new file mode 100644
index 0000000000000..bc2bfa9d9723b
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/lbound.f90
@@ -0,0 +1,54 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s
+
+
+! CHECK-LABEL: func @_QPlbound_test(
+! CHECK:  %[[VAL_0:.*]] = fir.load %arg1 : !fir.ref<i64>
+! CHECK:  %[[VAL_1:.*]] = fir.rebox %arg0 : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<!fir.array<?x?xf32>>
+subroutine lbound_test(a, dim, res)
+  real, dimension(:, :) :: a
+  integer(8):: dim, res
+! CHECK:         %[[VAL_2:.*]] = fir.address_of(
+! CHECK:         %[[VAL_3:.*]] = fir.convert %[[VAL_1]] : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
+! CHECK:         %[[VAL_4:.*]] = fir.convert %[[VAL_0]] : (i64) -> i32
+! CHECK:         %[[VAL_5:.*]] = fir.convert %[[VAL_2]]
+! CHECK:         %[[VAL_6:.*]] = fir.call @_FortranALboundDim(%[[VAL_3]], %[[VAL_4]], %[[VAL_5]], %{{.*}}) : (!fir.box<none>, i32, !fir.ref<i8>, i32) -> i64
+! CHECK:         fir.store %[[VAL_6]] to %arg2 : !fir.ref<i64>
+  res = lbound(a, dim, 8)
+end subroutine
+
+! CHECK-LABEL: func @_QPlbound_test_2(
+! CHECK:  %[[VAL_c1_i64:.*]] = arith.constant 1 : i64
+! CHECK:  %[[VAL_0:.*]] = fir.convert %[[VAL_c1_i64]] : (i64) -> index
+! CHECK:  %[[VAL_c2_i64:.*]] = arith.constant 2 : i64
+! CHECK:  %[[VAL_1:.*]] = fir.convert %[[VAL_c2_i64]] : (i64) -> index
+! CHECK:  %[[VAL_2:.*]] = fir.load %arg1 : !fir.ref<i64>
+subroutine lbound_test_2(a, dim, res)
+  real, dimension(:, 2:) :: a
+  integer(8):: dim, res
+! CHECK:  %[[VAL_3:.*]] = fir.shift %[[VAL_0]], %[[VAL_1]] : (index, index) -> !fir.shift<2>
+! CHECK:  %[[VAL_4:.*]] = fir.rebox %arg0(%[[VAL_3]]) : (!fir.box<!fir.array<?x?xf32>>, !fir.shift<2>) -> !fir.box<!fir.array<?x?xf32>>
+! CHECK:  %[[VAL_5:.*]] = fir.address_of(
+! CHECK:  %[[VAL_6:.*]] = fir.convert %[[VAL_4]] : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
+! CHECK:  %[[VAL_7:.*]] = fir.convert %[[VAL_2]] : (i64) -> i32
+! CHECK:  %[[VAL_8:.*]] = fir.convert %[[VAL_5]]
+! CHECK:  %[[VAL_9:.*]] = fir.call @_FortranALboundDim(%[[VAL_6]], %[[VAL_7]], %[[VAL_8]], %{{.*}}) : (!fir.box<none>, i32, !fir.ref<i8>, i32) -> i64
+! CHECK:         fir.store %[[VAL_9]] to %arg2 : !fir.ref<i64>
+  res = lbound(a, dim, 8)
+end subroutine
+
+! CHECK:  %[[VAL_0:.*]] = fir.undefined index
+subroutine lbound_test_3(a, dim, res)
+  real, dimension(2:10, 3:*) :: a
+  integer(8):: dim, res
+! CHECK:  %[[VAL_1:.*]] = fir.load %arg1 : !fir.ref<i64>
+! CHECK:  %[[VAL_2:.*]] = fir.shape_shift %{{.*}}, %{{.*}}, %{{.*}}, %[[VAL_0]] : (index, index, index, index) -> !fir.shapeshift<2>
+! CHECK:         %[[VAL_3:.*]] = fir.embox %arg0(%[[VAL_2]]) : (!fir.ref<!fir.array<9x?xf32>>, !fir.shapeshift<2>) -> !fir.box<!fir.array<9x?xf32>>
+! CHECK:         %[[VAL_4:.*]] = fir.address_of(
+! CHECK:         %[[VAL_5:.*]] = fir.convert %[[VAL_3]] : (!fir.box<!fir.array<9x?xf32>>) -> !fir.box<none>
+! CHECK:         %[[VAL_6:.*]] = fir.convert %[[VAL_1]] : (i64) -> i32
+! CHECK:         %[[VAL_7:.*]] = fir.convert %[[VAL_4]]
+! CHECK:         %[[VAL_8:.*]] = fir.call @_FortranALboundDim(%[[VAL_5]], %[[VAL_6]], %[[VAL_7]], %{{.*}}) : (!fir.box<none>, i32, !fir.ref<i8>, i32) -> i64
+! CHECK:         fir.store %[[VAL_8]] to %arg2 : !fir.ref<i64>
+  res = lbound(a, dim, 8)
+end subroutine

diff  --git a/flang/test/Lower/Intrinsics/ubound.f90 b/flang/test/Lower/Intrinsics/ubound.f90
new file mode 100644
index 0000000000000..4d464b89cdff6
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/ubound.f90
@@ -0,0 +1,71 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s
+
+! CHECK-LABEL: func @_QPubound_test(
+subroutine ubound_test(a, dim, res)
+  real, dimension(:, :) :: a
+  integer(8):: dim, res
+! CHECK:         %[[VAL_0:.*]] = fir.load
+! CHECK:         %[[VAL_1:.*]] = fir.address_of(
+! CHECK:         %[[VAL_2:.*]] = fir.convert
+! CHECK:         %[[VAL_3:.*]] = fir.convert %[[VAL_0]] : (i64) -> i32
+! CHECK:         %[[VAL_4:.*]] = fir.convert %[[VAL_1]]
+! CHECK:         %[[VAL_5:.*]] = fir.call @_FortranASizeDim(%[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}) : (!fir.box<none>, i32, !fir.ref<i8>, i32) -> i64
+! CHECK:         %[[VAL_6:.*]] = fir.address_of(
+! CHECK:         %[[VAL_7:.*]] = fir.convert %{{.*}} : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
+! CHECK:         %[[VAL_8:.*]] = fir.convert %[[VAL_0]] : (i64) -> i32
+! CHECK:         %[[VAL_9:.*]] = fir.convert %[[VAL_6]]
+! CHECK:         %[[VAL_10:.*]] = fir.call @_FortranALboundDim(%[[VAL_7]], %[[VAL_8]], %[[VAL_9]], %{{.*}}) : (!fir.box<none>, i32, !fir.ref<i8>, i32) -> i64
+! CHECK:         %[[VAL_11:.*]] = arith.subi %[[VAL_10]], %c1_i64 : i64
+! CHECK:         %[[VAL_12:.*]] = arith.addi %[[VAL_11]], %[[VAL_5]] : i64
+
+! CHECK:         fir.store %[[VAL_12]] to %{{.*}} : !fir.ref<i64>
+  res = ubound(a, dim, 8)
+end subroutine
+
+! CHECK-LABEL: func @_QPubound_test_2(
+subroutine ubound_test_2(a, dim, res)
+  real, dimension(2:, 3:) :: a
+  integer(8):: dim, res
+! CHECK:         %[[VAL_0:.*]] = fir.load %{{.*}} : !fir.ref<i64>
+! CHECK:         %[[VAL_1:.*]] = fir.address_of(
+! CHECK:         %[[VAL_2:.*]] = fir.convert %{{.*}} : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
+! CHECK:         %[[VAL_3:.*]] = fir.convert %[[VAL_0]] : (i64) -> i32
+! CHECK:         %[[VAL_4:.*]] = fir.convert %[[VAL_1]]
+! CHECK:         %[[VAL_5:.*]] = fir.call @_FortranASizeDim(%[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}) : (!fir.box<none>, i32, !fir.ref<i8>, i32) -> i64
+! CHECK:         %[[VAL_6:.*]] = fir.address_of(
+! CHECK:         %[[VAL_7:.*]] = fir.convert %{{.*}} : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
+! CHECK:         %[[VAL_8:.*]] = fir.convert %[[VAL_0]] : (i64) -> i32
+! CHECK:         %[[VAL_9:.*]] = fir.convert %[[VAL_6]]
+! CHECK:         %[[VAL_10:.*]] = fir.call @_FortranALboundDim(%[[VAL_7]], %[[VAL_8]], %[[VAL_9]], %{{.*}}) : (!fir.box<none>, i32, !fir.ref<i8>, i32) -> i64
+! CHECK:         %[[VAL_11:.*]] = arith.subi %[[VAL_10]], %{{.*}} : i64
+! CHECK:         %[[VAL_12:.*]] = arith.addi %[[VAL_11]], %[[VAL_5]] : i64
+! CHECK:         fir.store %[[VAL_12]] to %{{.*}} : !fir.ref<i64>
+  res = ubound(a, dim, 8)
+end subroutine
+
+! CHECK-LABEL: func @_QPubound_test_3(
+subroutine ubound_test_3(a, dim, res)
+  real, dimension(10, 20, *) :: a
+  integer(8):: dim, res
+! CHECK:         %[[VAL_0:.*]] = fir.undefined index
+! CHECK:         %[[VAL_1:.*]] = fir.shape %{{.*}}, %{{.*}}, %[[VAL_0]] : (index, index, index) -> !fir.shape<3>
+! CHECK:         %[[VAL_2:.*]] = fir.embox %{{.*}}(%[[VAL_1]]) : (!fir.ref<!fir.array<10x20x?xf32>>, !fir.shape<3>) -> !fir.box<!fir.array<10x20x?xf32>>
+! CHECK:         %[[VAL_3:.*]] = fir.load %{{.*}} : !fir.ref<i64>
+! CHECK:         %[[VAL_4:.*]] = fir.address_of(
+! CHECK:         %[[VAL_5:.*]] = fir.convert %[[VAL_2]] : (!fir.box<!fir.array<10x20x?xf32>>) -> !fir.box<none>
+
+! CHECK:         %[[VAL_6:.*]] = fir.convert %[[VAL_3]] : (i64) -> i32
+! CHECK:         %[[VAL_7:.*]] = fir.convert %[[VAL_4]]
+! CHECK:         %[[VAL_8:.*]] = fir.call @_FortranASizeDim(%[[VAL_5]], %[[VAL_6]], %[[VAL_7]], %{{.*}}) : (!fir.box<none>, i32, !fir.ref<i8>, i32) -> i64
+! CHECK:         %[[VAL_9:.*]] = fir.rebox %[[VAL_2]] : (!fir.box<!fir.array<10x20x?xf32>>) -> !fir.box<!fir.array<10x20x?xf32>>
+! CHECK:         %[[VAL_10:.*]] = fir.address_of(
+! CHECK:         %[[VAL_11:.*]] = fir.convert %[[VAL_9]] : (!fir.box<!fir.array<10x20x?xf32>>) -> !fir.box<none>
+! CHECK:         %[[VAL_12:.*]] = fir.convert %[[VAL_3]]
+! CHECK:         %[[VAL_13:.*]] = fir.convert %[[VAL_10]]
+! CHECK:         %[[VAL_14:.*]] = fir.call @_FortranALboundDim(%[[VAL_11]], %[[VAL_12]], %[[VAL_13]], %{{.*}}) : (!fir.box<none>, i32, !fir.ref<i8>, i32) -> i64
+! CHECK:         %[[VAL_15:.*]] = arith.subi %[[VAL_14]], %{{.*}} : i64
+! CHECK:         %[[VAL_16:.*]] = arith.addi %[[VAL_15]], %[[VAL_8]] : i64
+! CHECK:         fir.store %[[VAL_16]] to %{{.*}} : !fir.ref<i64>
+  res = ubound(a, dim, 8)
+end subroutine


        


More information about the flang-commits mailing list