From 4235bd603a335366e873297ed36b41f79b1753f5 Mon Sep 17 00:00:00 2001 From: Valentin Clement Date: Thu, 18 Aug 2022 20:23:09 +0200 Subject: [PATCH] [flang] Make sure dynamic extents and length are rounded to 0 if negative Function returning CHARACTER with adjustable length or dynamic arrays can have negative length or extents passed to them. This patch makes sure any negative inputs is rounded to 0. Reviewed By: vdonaldson Differential Revision: https://reviews.llvm.org/D132139 --- flang/lib/Lower/ConvertExpr.cpp | 3 +- .../test/Lower/array-elemental-calls-char.f90 | 10 ++-- .../test/Lower/dummy-procedure-character.f90 | 17 ++++-- .../Lower/explicit-interface-results-2.f90 | 40 ++++++++++---- .../test/Lower/explicit-interface-results.f90 | 54 ++++++++++++++----- flang/test/Lower/forall/array-constructor.f90 | 7 ++- .../test/Lower/host-associated-functions.f90 | 12 +++-- 7 files changed, 105 insertions(+), 38 deletions(-) diff --git a/flang/lib/Lower/ConvertExpr.cpp b/flang/lib/Lower/ConvertExpr.cpp index 5929b4bfa28d..ac222078f59f 100644 --- a/flang/lib/Lower/ConvertExpr.cpp +++ b/flang/lib/Lower/ConvertExpr.cpp @@ -2504,8 +2504,9 @@ public: mlir::IndexType idxTy = builder.getIndexType(); auto lowerSpecExpr = [&](const auto &expr) -> mlir::Value { - return builder.createConvert( + mlir::Value convertExpr = builder.createConvert( loc, idxTy, fir::getBase(converter.genExprValue(expr, stmtCtx))); + return fir::factory::genMaxWithZero(builder, loc, convertExpr); }; llvm::SmallVector resultLengths; auto allocatedResult = [&]() -> llvm::Optional { diff --git a/flang/test/Lower/array-elemental-calls-char.f90 b/flang/test/Lower/array-elemental-calls-char.f90 index e695f9bb8b89..dd4a3a8b88e7 100644 --- a/flang/test/Lower/array-elemental-calls-char.f90 +++ b/flang/test/Lower/array-elemental-calls-char.f90 @@ -226,11 +226,13 @@ subroutine foo6(c) ! CHECK: %[[VAL_14:.*]] = fir.emboxchar %[[VAL_13]], %[[VAL_6]]#1 : (!fir.ref>, index) -> !fir.boxchar<1> ! CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_6]]#1 : (index) -> i32 ! CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_15]] : (i32) -> index + ! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_16]], %{{.*}} : index + ! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_16]], %{{.*}} : index ! CHECK: %[[VAL_17:.*]] = fir.call @llvm.stacksave() : () -> !fir.ref - ! CHECK: %[[VAL_18:.*]] = fir.alloca !fir.char<1,?>(%[[VAL_16]] : index) {bindc_name = ".result"} - ! CHECK: %[[VAL_19:.*]] = fir.call @_QMchar_elemPelem_return_char(%[[VAL_18]], %[[VAL_16]], %[[VAL_14]]) : (!fir.ref>, index, !fir.boxchar<1>) -> !fir.boxchar<1> - ! CHECK: %[[VAL_20:.*]] = arith.cmpi slt, %[[VAL_6]]#1, %[[VAL_16]] : index - ! CHECK: %[[VAL_21:.*]] = arith.select %[[VAL_20]], %[[VAL_6]]#1, %[[VAL_16]] : index + ! CHECK: %[[VAL_18:.*]] = fir.alloca !fir.char<1,?>(%[[SELECT]] : index) {bindc_name = ".result"} + ! CHECK: %[[VAL_19:.*]] = fir.call @_QMchar_elemPelem_return_char(%[[VAL_18]], %[[SELECT]], %[[VAL_14]]) : (!fir.ref>, index, !fir.boxchar<1>) -> !fir.boxchar<1> + ! CHECK: %[[VAL_20:.*]] = arith.cmpi slt, %[[VAL_6]]#1, %[[SELECT]] : index + ! CHECK: %[[VAL_21:.*]] = arith.select %[[VAL_20]], %[[VAL_6]]#1, %[[SELECT]] : index ! CHECK: %[[VAL_22:.*]] = fir.convert %[[VAL_21]] : (index) -> i64 ! CHECK: %[[VAL_23:.*]] = fir.convert %[[VAL_13]] : (!fir.ref>) -> !fir.ref ! CHECK: %[[VAL_24:.*]] = fir.convert %[[VAL_18]] : (!fir.ref>) -> !fir.ref diff --git a/flang/test/Lower/dummy-procedure-character.f90 b/flang/test/Lower/dummy-procedure-character.f90 index 8eabf6df418b..681b8fa296a3 100644 --- a/flang/test/Lower/dummy-procedure-character.f90 +++ b/flang/test/Lower/dummy-procedure-character.f90 @@ -168,8 +168,11 @@ subroutine call_explicit_length(bar9) ! CHECK: %[[WAL_1:.*]] = fir.box_addr %[[VAL_4]] : (!fir.boxproc<() -> ()>) -> (() -> ()) ! CHECK: %[[VAL_5:.*]] = arith.constant 7 : i64 ! CHECK: %[[VAL_6:.*]] = fir.convert %[[VAL_5]] : (i64) -> index +! CHECK: %[[C0:.*]] = arith.constant 0 : index +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_6]], %[[C0]] : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_6]], %[[C0]] : index ! CHECK: %[[VAL_8:.*]] = fir.convert %[[WAL_1]] : (() -> ()) -> ((!fir.ref>, index, !fir.ref) -> !fir.boxchar<1>) -! CHECK: fir.call %[[VAL_8]](%[[VAL_1]], %[[VAL_6]], %{{.*}}) : (!fir.ref>, index, !fir.ref) -> !fir.boxchar<1> +! CHECK: fir.call %[[VAL_8]](%[[VAL_1]], %[[SELECT]], %{{.*}}) : (!fir.ref>, index, !fir.ref) -> !fir.boxchar<1> call test(bar9(42)) end subroutine @@ -189,10 +192,13 @@ subroutine call_explicit_length_with_iface(bar10) ! CHECK: %[[WAL_1:.*]] = fir.box_addr %[[VAL_3]] : (!fir.boxproc<() -> ()>) -> (() -> ()) ! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_1]] : !fir.ref ! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_4]] : (i64) -> index +! CHECK: %[[C0:.*]] = arith.constant 0 : index +! CHECK: %[[COMPI:.*]] = arith.cmpi sgt, %[[VAL_5]], %[[C0]] : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_5]], %[[C0]] : index ! CHECK: %[[VAL_6:.*]] = fir.call @llvm.stacksave() : () -> !fir.ref -! CHECK: %[[VAL_7:.*]] = fir.alloca !fir.char<1,?>(%[[VAL_5]] : index) {bindc_name = ".result"} +! CHECK: %[[VAL_7:.*]] = fir.alloca !fir.char<1,?>(%[[SELECT]] : index) {bindc_name = ".result"} ! CHECK: %[[VAL_8:.*]] = fir.convert %[[WAL_1]] : (() -> ()) -> ((!fir.ref>, index, !fir.ref) -> !fir.boxchar<1>) -! CHECK: fir.call %[[VAL_8]](%[[VAL_7]], %[[VAL_5]], %[[VAL_1]]) : (!fir.ref>, index, !fir.ref) -> !fir.boxchar<1> +! CHECK: fir.call %[[VAL_8]](%[[VAL_7]], %[[SELECT]], %[[VAL_1]]) : (!fir.ref>, index, !fir.ref) -> !fir.boxchar<1> call test(bar10(42_8)) end subroutine @@ -247,8 +253,11 @@ contains ! CHECK: %[[WAL_1:.*]] = fir.box_addr %[[VAL_5]] : (!fir.boxproc<() -> ()>) -> (() -> ()) ! CHECK: %[[VAL_6:.*]] = arith.constant 42 : i64 ! CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_6]] : (i64) -> index + ! CHECK: %[[C0:.*]] = arith.constant 0 : index + ! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_7]], %[[C0]] : index + ! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_7]], %[[C0]] : index ! CHECK: %[[VAL_9:.*]] = fir.convert %[[WAL_1]] : (() -> ()) -> ((!fir.ref>, index) -> !fir.boxchar<1>) - ! CHECK: fir.call %[[VAL_9]](%[[VAL_1]], %[[VAL_7]]) : (!fir.ref>, index) -> !fir.boxchar<1> + ! CHECK: fir.call %[[VAL_9]](%[[VAL_1]], %[[SELECT]]) : (!fir.ref>, index) -> !fir.boxchar<1> call test(f()) end subroutine end subroutine diff --git a/flang/test/Lower/explicit-interface-results-2.f90 b/flang/test/Lower/explicit-interface-results-2.f90 index 7e062e638211..d8fd2dd3891e 100644 --- a/flang/test/Lower/explicit-interface-results-2.f90 +++ b/flang/test/Lower/explicit-interface-results-2.f90 @@ -17,7 +17,9 @@ subroutine host1() call takes_array(return_array()) ! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_1]] : !fir.ref ! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_4]] : (i32) -> index -! CHECK: %[[VAL_6:.*]] = fir.alloca !fir.array, %[[VAL_5]] {bindc_name = ".result"} +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_5]], %{{.*}} : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_5]], %{{.*}} : index +! CHECK: %[[VAL_6:.*]] = fir.alloca !fir.array, %[[SELECT]] {bindc_name = ".result"} contains function return_array() real :: return_array(n) @@ -33,7 +35,9 @@ subroutine host2() ! CHECK: %[[VAL_0:.*]] = fir.address_of(@_QMsome_moduleEn_module) : !fir.ref ! CHECK: %[[VAL_1:.*]] = fir.load %[[VAL_0]] : !fir.ref ! CHECK: %[[VAL_2:.*]] = fir.convert %[[VAL_1]] : (i32) -> index -! CHECK: %[[VAL_3:.*]] = fir.alloca !fir.array, %[[VAL_2]] {bindc_name = ".result"} +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_2]], %{{.*}} : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_2]], %{{.*}} : index +! CHECK: %[[VAL_3:.*]] = fir.alloca !fir.array, %[[SELECT]] {bindc_name = ".result"} contains function return_array() real :: return_array(n_module) @@ -48,7 +52,9 @@ subroutine host3() ! CHECK: %[[VAL_0:.*]] = fir.address_of(@_QMsome_moduleEn_module) : !fir.ref ! CHECK: %[[VAL_1:.*]] = fir.load %[[VAL_0]] : !fir.ref ! CHECK: %[[VAL_2:.*]] = fir.convert %[[VAL_1]] : (i32) -> index -! CHECK: %[[VAL_3:.*]] = fir.alloca !fir.array, %[[VAL_2]] {bindc_name = ".result"} +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_2]], %{{.*}} : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_2]], %{{.*}} : index +! CHECK: %[[VAL_3:.*]] = fir.alloca !fir.array, %[[SELECT]] {bindc_name = ".result"} contains function return_array() use :: some_module @@ -72,7 +78,9 @@ contains ! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_2]] : !fir.llvm_ptr> ! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_3]] : !fir.ref ! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_4]] : (i32) -> index -! CHECK: %[[VAL_6:.*]] = fir.alloca !fir.array, %[[VAL_5]] {bindc_name = ".result"} +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_5]], %{{.*}} : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_5]], %{{.*}} : index +! CHECK: %[[VAL_6:.*]] = fir.alloca !fir.array, %[[SELECT]] {bindc_name = ".result"} end subroutine function return_array() real :: return_array(n) @@ -92,7 +100,9 @@ contains ! CHECK: %[[VAL_0:.*]] = fir.address_of(@_QMsome_moduleEn_module) : !fir.ref ! CHECK: %[[VAL_1:.*]] = fir.load %[[VAL_0]] : !fir.ref ! CHECK: %[[VAL_2:.*]] = fir.convert %[[VAL_1]] : (i32) -> index -! CHECK: %[[VAL_3:.*]] = fir.alloca !fir.array, %[[VAL_2]] {bindc_name = ".result"} +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_2]], %{{.*}} : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_2]], %{{.*}} : index +! CHECK: %[[VAL_3:.*]] = fir.alloca !fir.array, %[[SELECT]] {bindc_name = ".result"} end subroutine function return_array() real :: return_array(n_module) @@ -111,7 +121,9 @@ contains ! CHECK: %[[VAL_0:.*]] = fir.address_of(@_QMsome_moduleEn_module) : !fir.ref ! CHECK: %[[VAL_1:.*]] = fir.load %[[VAL_0]] : !fir.ref ! CHECK: %[[VAL_2:.*]] = fir.convert %[[VAL_1]] : (i32) -> index -! CHECK: %[[VAL_3:.*]] = fir.alloca !fir.array, %[[VAL_2]] {bindc_name = ".result"} +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_2]], %{{.*}} : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_2]], %{{.*}} : index +! CHECK: %[[VAL_3:.*]] = fir.alloca !fir.array, %[[SELECT]] {bindc_name = ".result"} end subroutine function return_array() use :: some_module @@ -134,7 +146,9 @@ subroutine host7() ! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_4]] : (!fir.ref) -> !fir.ref ! CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_5]] : !fir.ref ! CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_8]] : (i32) -> index -! CHECK: %[[VAL_10:.*]] = fir.alloca !fir.array, %[[VAL_9]] {bindc_name = ".result"} +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_9]], %{{.*}} : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_9]], %{{.*}} : index +! CHECK: %[[VAL_10:.*]] = fir.alloca !fir.array, %[[SELECT]] {bindc_name = ".result"} contains function return_array() real :: return_array(n_common) @@ -154,7 +168,9 @@ subroutine host8() ! CHECK: %[[VAL_4:.*]] = fir.convert %[[VAL_3]] : (!fir.ref) -> !fir.ref ! CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_4]] : !fir.ref ! CHECK: %[[VAL_6:.*]] = fir.convert %[[VAL_5]] : (i32) -> index -! CHECK: %[[VAL_7:.*]] = fir.alloca !fir.array, %[[VAL_6]] {bindc_name = ".result"} +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_6]], %{{.*}} : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_6]], %{{.*}} : index +! CHECK: %[[VAL_7:.*]] = fir.alloca !fir.array, %[[SELECT]] {bindc_name = ".result"} contains function return_array() integer :: n_common @@ -181,7 +197,9 @@ contains ! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_2]] : !fir.llvm_ptr> ! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_3]] : !fir.ref ! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_4]] : (i32) -> index -! CHECK: %[[VAL_6:.*]] = fir.alloca !fir.array, %[[VAL_5]] {bindc_name = ".result"} +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_5]], %{{.*}} : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_5]], %{{.*}} : index +! CHECK: %[[VAL_6:.*]] = fir.alloca !fir.array, %[[SELECT]] {bindc_name = ".result"} call takes_array(return_array()) end subroutine function return_array() @@ -206,7 +224,9 @@ contains ! CHECK: %[[VAL_4:.*]] = fir.convert %[[VAL_3]] : (!fir.ref) -> !fir.ref ! CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_4]] : !fir.ref ! CHECK: %[[VAL_6:.*]] = fir.convert %[[VAL_5]] : (i32) -> index -! CHECK: %[[VAL_7:.*]] = fir.alloca !fir.array, %[[VAL_6]] {bindc_name = ".result"} +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_6]], %{{.*}} : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_6]], %{{.*}} : index +! CHECK: %[[VAL_7:.*]] = fir.alloca !fir.array, %[[SELECT]] {bindc_name = ".result"} end subroutine function return_array() integer :: n_common diff --git a/flang/test/Lower/explicit-interface-results.f90 b/flang/test/Lower/explicit-interface-results.f90 index add5e761ba59..c414af5b9d67 100644 --- a/flang/test/Lower/explicit-interface-results.f90 +++ b/flang/test/Lower/explicit-interface-results.f90 @@ -186,13 +186,17 @@ subroutine dyn_array(m, n) ! CHECK-DAG: %[[msub:.*]] = arith.subi %[[mcast]], %c1{{.*}} : i64 ! CHECK-DAG: %[[madd:.*]] = arith.addi %[[msub]], %c1{{.*}} : i64 ! CHECK-DAG: %[[mcast2:.*]] = fir.convert %[[madd]] : (i64) -> index + ! CHECK-DAG: %[[mcmpi:.*]] = arith.cmpi sgt, %[[mcast2]], %{{.*}} : index + ! CHECK-DAG: %[[mselect:.*]] = arith.select %[[mcmpi]], %[[mcast2]], %{{.*}} : index ! CHECK-DAG: %[[nload:.*]] = fir.load %[[n]] : !fir.ref ! CHECK-DAG: %[[ncast:.*]] = fir.convert %[[nload]] : (i32) -> i64 ! CHECK-DAG: %[[nsub:.*]] = arith.subi %[[ncast]], %c1{{.*}} : i64 ! CHECK-DAG: %[[nadd:.*]] = arith.addi %[[nsub]], %c1{{.*}} : i64 ! CHECK-DAG: %[[ncast2:.*]] = fir.convert %[[nadd]] : (i64) -> index - ! CHECK: %[[tmp:.*]] = fir.alloca !fir.array, %[[mcast2]], %[[ncast2]] - ! CHECK: %[[shape:.*]] = fir.shape %[[mcast2]], %[[ncast2]] : (index, index) -> !fir.shape<2> + ! CHECK-DAG: %[[ncmpi:.*]] = arith.cmpi sgt, %[[ncast2]], %{{.*}} : index + ! CHECK-DAG: %[[nselect:.*]] = arith.select %[[ncmpi]], %[[ncast2]], %{{.*}} : index + ! CHECK: %[[tmp:.*]] = fir.alloca !fir.array, %[[mselect]], %[[nselect]] + ! CHECK: %[[shape:.*]] = fir.shape %[[mselect]], %[[nselect]] : (index, index) -> !fir.shape<2> ! CHECK: %[[res:.*]] = fir.call @_QMcalleePreturn_dyn_array(%[[m]], %[[n]]) : (!fir.ref, !fir.ref) -> !fir.array ! CHECK: fir.save_result %[[res]] to %[[tmp]](%[[shape]]) : !fir.array, !fir.ref>, !fir.shape<2> print *, return_dyn_array(m, n) @@ -205,10 +209,12 @@ subroutine dyn_char_cst_array(l) ! CHECK: %[[lload:.*]] = fir.load %[[l]] : !fir.ref ! CHECK: %[[lcast:.*]] = fir.convert %[[lload]] : (i32) -> i64 ! CHECK: %[[lcast2:.*]] = fir.convert %[[lcast]] : (i64) -> index - ! CHECK: %[[tmp:.*]] = fir.alloca !fir.array<20x30x!fir.char<1,?>>(%[[lcast2]] : index) + ! CHECK: %[[cmpi:.*]] = arith.cmpi sgt, %[[lcast2]], %{{.*}} : index + ! CHECK: %[[select:.*]] = arith.select %[[cmpi]], %[[lcast2]], %{{.*}} : index + ! CHECK: %[[tmp:.*]] = fir.alloca !fir.array<20x30x!fir.char<1,?>>(%[[select]] : index) ! CHECK: %[[shape:.*]] = fir.shape %{{.*}}, %{{.*}} : (index, index) -> !fir.shape<2> ! CHECK: %[[res:.*]] = fir.call @_QMcalleePreturn_dyn_char_cst_array(%[[l]]) : (!fir.ref) -> !fir.array<20x30x!fir.char<1,?>> - ! CHECK: fir.save_result %[[res]] to %[[tmp]](%[[shape]]) typeparams %[[lcast2]] : !fir.array<20x30x!fir.char<1,?>>, !fir.ref>>, !fir.shape<2>, index + ! CHECK: fir.save_result %[[res]] to %[[tmp]](%[[shape]]) typeparams %[[select]] : !fir.array<20x30x!fir.char<1,?>>, !fir.ref>>, !fir.shape<2>, index print *, return_dyn_char_cst_array(l) end subroutine @@ -221,13 +227,17 @@ subroutine cst_char_dyn_array(m, n) ! CHECK-DAG: %[[msub:.*]] = arith.subi %[[mcast]], %c1{{.*}} : i64 ! CHECK-DAG: %[[madd:.*]] = arith.addi %[[msub]], %c1{{.*}} : i64 ! CHECK-DAG: %[[mcast2:.*]] = fir.convert %[[madd]] : (i64) -> index + ! CHECK-DAG: %[[mcmpi:.*]] = arith.cmpi sgt, %[[mcast2]], %{{.*}} : index + ! CHECK-DAG: %[[mselect:.*]] = arith.select %[[mcmpi]], %[[mcast2]], %{{.*}} : index ! CHECK-DAG: %[[nload:.*]] = fir.load %[[n]] : !fir.ref ! CHECK-DAG: %[[ncast:.*]] = fir.convert %[[nload]] : (i32) -> i64 ! CHECK-DAG: %[[nsub:.*]] = arith.subi %[[ncast]], %c1{{.*}} : i64 ! CHECK-DAG: %[[nadd:.*]] = arith.addi %[[nsub]], %c1{{.*}} : i64 ! CHECK-DAG: %[[ncast2:.*]] = fir.convert %[[nadd]] : (i64) -> index - ! CHECK: %[[tmp:.*]] = fir.alloca !fir.array>, %[[mcast2]], %[[ncast2]] - ! CHECK: %[[shape:.*]] = fir.shape %[[mcast2]], %[[ncast2]] : (index, index) -> !fir.shape<2> + ! CHECK-DAG: %[[ncmpi:.*]] = arith.cmpi sgt, %[[ncast2]], %{{.*}} : index + ! CHECK-DAG: %[[nselect:.*]] = arith.select %[[ncmpi]], %[[ncast2]], %{{.*}} : index + ! CHECK: %[[tmp:.*]] = fir.alloca !fir.array>, %[[mselect]], %[[nselect]] + ! CHECK: %[[shape:.*]] = fir.shape %[[mselect]], %[[nselect]] : (index, index) -> !fir.shape<2> ! CHECK: %[[res:.*]] = fir.call @_QMcalleePreturn_cst_char_dyn_array(%[[m]], %[[n]]) : (!fir.ref, !fir.ref) -> !fir.array> ! CHECK: fir.save_result %[[res]] to %[[tmp]](%[[shape]]) typeparams {{.*}} : !fir.array>, !fir.ref>>, !fir.shape<2>, index print *, return_cst_char_dyn_array(m, n) @@ -241,18 +251,24 @@ subroutine dyn_char_dyn_array(l, m, n) ! CHECK-DAG: %[[msub:.*]] = arith.subi %[[mcast]], %c1{{.*}} : i64 ! CHECK-DAG: %[[madd:.*]] = arith.addi %[[msub]], %c1{{.*}} : i64 ! CHECK-DAG: %[[mcast2:.*]] = fir.convert %[[madd]] : (i64) -> index + ! CHECK-DAG: %[[mcmpi:.*]] = arith.cmpi sgt, %[[mcast2]], %{{.*}} : index + ! CHECK-DAG: %[[mselect:.*]] = arith.select %[[mcmpi]], %[[mcast2]], %{{.*}} : index ! CHECK-DAG: %[[nload:.*]] = fir.load %[[n]] : !fir.ref ! CHECK-DAG: %[[ncast:.*]] = fir.convert %[[nload]] : (i32) -> i64 ! CHECK-DAG: %[[nsub:.*]] = arith.subi %[[ncast]], %c1{{.*}} : i64 ! CHECK-DAG: %[[nadd:.*]] = arith.addi %[[nsub]], %c1{{.*}} : i64 ! CHECK-DAG: %[[ncast2:.*]] = fir.convert %[[nadd]] : (i64) -> index + ! CHECK-DAG: %[[ncmpi:.*]] = arith.cmpi sgt, %[[ncast2]], %{{.*}} : index + ! CHECK-DAG: %[[nselect:.*]] = arith.select %[[ncmpi]], %[[ncast2]], %{{.*}} : index ! CHECK-DAG: %[[lload:.*]] = fir.load %[[l]] : !fir.ref ! CHECK-DAG: %[[lcast:.*]] = fir.convert %[[lload]] : (i32) -> i64 ! CHECK-DAG: %[[lcast2:.*]] = fir.convert %[[lcast]] : (i64) -> index - ! CHECK: %[[tmp:.*]] = fir.alloca !fir.array>(%[[lcast2]] : index), %[[mcast2]], %[[ncast2]] - ! CHECK: %[[shape:.*]] = fir.shape %[[mcast2]], %[[ncast2]] : (index, index) -> !fir.shape<2> + ! CHECK-DAG: %[[lcmpi:.*]] = arith.cmpi sgt, %[[lcast2]], %{{.*}} : index + ! CHECK-DAG: %[[lselect:.*]] = arith.select %[[lcmpi]], %[[lcast2]], %{{.*}} : index + ! CHECK: %[[tmp:.*]] = fir.alloca !fir.array>(%[[lselect]] : index), %[[mselect]], %[[nselect]] + ! CHECK: %[[shape:.*]] = fir.shape %[[mselect]], %[[nselect]] : (index, index) -> !fir.shape<2> ! CHECK: %[[res:.*]] = fir.call @_QMcalleePreturn_dyn_char_dyn_array(%[[l]], %[[m]], %[[n]]) : (!fir.ref, !fir.ref, !fir.ref) -> !fir.array> ! CHECK: fir.save_result %[[res]] to %[[tmp]](%[[shape]]) typeparams {{.*}} : !fir.array>, !fir.ref>>, !fir.shape<2>, index integer :: l, m, n @@ -308,7 +324,9 @@ subroutine test_result_depends_on_equiv_sym() ! CHECK: %[[l:.*]] = fir.convert %[[coor]] : (!fir.ref) -> !fir.ptr ! CHECK: %[[load:.*]] = fir.load %[[l]] : !fir.ptr ! CHECK: %[[lcast:.*]] = fir.convert %[[load]] : (i64) -> index - ! CHECK: fir.alloca !fir.char<1,?>(%[[lcast]] : index) + ! CHECK: %[[cmpi:.*]] = arith.cmpi sgt, %[[lcast]], %{{.*}} : index + ! CHECK: %[[select:.*]] = arith.select %[[cmpi]], %[[lcast]], %{{.*}} : index + ! CHECK: fir.alloca !fir.char<1,?>(%[[select]] : index) print *, result_depends_on_equiv_sym() end subroutine @@ -325,7 +343,9 @@ subroutine test_depends_on_descriptor(x) ! CHECK: %[[dims:.*]]:3 = fir.box_dims %arg0, %c0 : (!fir.box>, index) -> (index, index, index) ! CHECK: %[[extentCast:.*]] = fir.convert %[[dims]]#1 : (index) -> i64 ! CHECK: %[[extent:.*]] = fir.convert %[[extentCast]] : (i64) -> index - ! CHECK: fir.alloca !fir.char<1,?>(%[[extent]] : index) + ! CHECK: %[[cmpi:.*]] = arith.cmpi sgt, %[[extent]], %{{.*}} : index + ! CHECK: %[[select:.*]] = arith.select %[[cmpi]], %[[extent]], %{{.*}} : index + ! CHECK: fir.alloca !fir.char<1,?>(%[[select]] : index) print *, depends_on_descriptor(x) end subroutine @@ -346,7 +366,9 @@ subroutine test_symbol_indirection(n) ! CHECK: %[[n_is_positive:.*]] = arith.cmpi sgt, %[[nload]], %c0{{.*}} : i64 ! CHECK: %[[len:.*]] = arith.select %[[n_is_positive]], %[[nload]], %c0{{.*}} : i64 ! CHECK: %[[len_cast:.*]] = fir.convert %[[len]] : (i64) -> index - ! CHECK: fir.alloca !fir.char<1,?>(%[[len_cast]] : index) + ! CHECK: %[[cmpi:.*]] = arith.cmpi sgt, %[[len_cast]], %{{.*}} : index + ! CHECK: %[[select:.*]] = arith.select %[[cmpi]], %[[len_cast]], %{{.*}} : index + ! CHECK: fir.alloca !fir.char<1,?>(%[[select]] : index) print *, symbol_indirection(c, n) end subroutine @@ -379,7 +401,9 @@ function test_recursion(n) result(res) ! CHECK: %[[nInCallLoad:.*]] = fir.load %[[nInCall]] : !fir.ref ! CHECK: %[[nInCallCast:.*]] = fir.convert %[[nInCallLoad]] : (i64) -> index - ! CHECK: %[[tmp:.*]] = fir.alloca !fir.char<1,?>(%[[nInCallCast]] : index) + ! CHECK: %[[cmpi:.*]] = arith.cmpi sgt, %[[nInCallCast]], %{{.*}} : index + ! CHECK: %[[select:.*]] = arith.select %[[cmpi]], %[[nInCallCast]], %{{.*}} : index + ! CHECK: %[[tmp:.*]] = fir.alloca !fir.char<1,?>(%[[select]] : index) ! CHECK-NOT: fir.alloca !fir.array ! CHECK: fir.call @_QPtest_recursion(%[[tmp]], {{.*}} @@ -403,6 +427,8 @@ subroutine test_not_entirely_explicit_interface(n) print *, return_dyn_char_2(10) ! CHECK: %[[n:.*]] = fir.load %[[n_arg]] : !fir.ref ! CHECK: %[[len:.*]] = fir.convert %[[n]] : (i64) -> index - ! CHECK: %[[result:.*]] = fir.alloca !fir.char<1,?>(%[[len]] : index) {bindc_name = ".result"} - ! CHECK: fir.call @_QPreturn_dyn_char_2(%[[result]], %[[len]], %{{.*}}) : (!fir.ref>, index, !fir.ref) -> !fir.boxchar<1> + ! CHECK: %[[cmpi:.*]] = arith.cmpi sgt, %[[len]], %{{.*}} : index + ! CHECK: %[[select:.*]] = arith.select %[[cmpi]], %[[len]], %{{.*}} : index + ! CHECK: %[[result:.*]] = fir.alloca !fir.char<1,?>(%[[select]] : index) {bindc_name = ".result"} + ! CHECK: fir.call @_QPreturn_dyn_char_2(%[[result]], %[[select]], %{{.*}}) : (!fir.ref>, index, !fir.ref) -> !fir.boxchar<1> end subroutine diff --git a/flang/test/Lower/forall/array-constructor.f90 b/flang/test/Lower/forall/array-constructor.f90 index ed28f498d2a3..761c3e29cdeb 100644 --- a/flang/test/Lower/forall/array-constructor.f90 +++ b/flang/test/Lower/forall/array-constructor.f90 @@ -229,12 +229,15 @@ end subroutine ac2 ! CHECK: %[[VAL_78:.*]] = arith.constant 1 : i64 ! CHECK: %[[VAL_79:.*]] = arith.addi %[[VAL_77]], %[[VAL_78]] : i64 ! CHECK: %[[VAL_80:.*]] = fir.convert %[[VAL_79]] : (i64) -> index +! CHECK: %[[C0:.*]] = arith.constant 0 : index +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_80]], %[[C0]] : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_80]], %[[C0]] : index ! CHECK: %[[VAL_81:.*]] = fir.call @llvm.stacksave() : () -> !fir.ref -! CHECK: %[[VAL_82:.*]] = fir.shape %[[VAL_80]] : (index) -> !fir.shape<1> +! CHECK: %[[VAL_82:.*]] = fir.shape %[[SELECT]] : (index) -> !fir.shape<1> ! CHECK: %[[VAL_83:.*]] = fir.convert %[[VAL_74]] : (!fir.box>) -> !fir.box> ! CHECK: %[[VAL_84:.*]] = fir.call @_QFac2Pfunc(%[[VAL_83]]) : (!fir.box>) -> !fir.array<3xi32> ! CHECK: fir.save_result %[[VAL_84]] to %[[VAL_2]](%[[VAL_82]]) : !fir.array<3xi32>, !fir.ref>, !fir.shape<1> -! CHECK: %[[VAL_85:.*]] = fir.shape %[[VAL_80]] : (index) -> !fir.shape<1> +! CHECK: %[[VAL_85:.*]] = fir.shape %[[SELECT]] : (index) -> !fir.shape<1> ! CHECK: %[[VAL_86:.*]] = fir.array_load %[[VAL_2]](%[[VAL_85]]) : (!fir.ref>, !fir.shape<1>) -> !fir.array<3xi32> ! CHECK: %[[VAL_87:.*]] = arith.constant 1 : index ! CHECK: %[[VAL_88:.*]] = arith.constant 0 : index diff --git a/flang/test/Lower/host-associated-functions.f90 b/flang/test/Lower/host-associated-functions.f90 index 798543ed35da..89cc4755a8d7 100644 --- a/flang/test/Lower/host-associated-functions.f90 +++ b/flang/test/Lower/host-associated-functions.f90 @@ -33,10 +33,13 @@ contains ! CHECK: %[[VAL_14:.*]] = fir.load %[[VAL_6]] : !fir.ref ! CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_14]] : (i32) -> i64 ! CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_15]] : (i64) -> index + ! CHECK: %[[C0:.*]] = arith.constant 0 : index + ! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_16]], %[[C0]] : index + ! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_16]], %[[C0]] : index ! CHECK: %[[VAL_17:.*]] = fir.call @llvm.stacksave() : () -> !fir.ref - ! CHECK: %[[VAL_18:.*]] = fir.alloca !fir.char<1,?>(%[[VAL_16]] : index) {bindc_name = ".result"} + ! CHECK: %[[VAL_18:.*]] = fir.alloca !fir.char<1,?>(%[[SELECT]] : index) {bindc_name = ".result"} ! CHECK: %[[VAL_19:.*]] = fir.convert %[[VAL_13]] : (() -> ()) -> ((!fir.ref>, index) -> !fir.boxchar<1>) - ! CHECK: %[[VAL_20:.*]] = fir.call %[[VAL_19]](%[[VAL_18]], %[[VAL_16]]) : (!fir.ref>, index) -> !fir.boxchar<1> + ! CHECK: %[[VAL_20:.*]] = fir.call %[[VAL_19]](%[[VAL_18]], %[[SELECT]]) : (!fir.ref>, index) -> !fir.boxchar<1> print *, char_func_dummy() end subroutine end subroutine @@ -118,8 +121,11 @@ contains ! CHECK: %[[VAL_13:.*]] = arith.constant 1 : i64 ! CHECK: %[[VAL_14:.*]] = arith.addi %[[VAL_12]], %[[VAL_13]] : i64 ! CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_14]] : (i64) -> index +! CHECK: %[[C0:.*]] = arith.constant 0 : index +! CHECK: %[[CMPI:.*]] = arith.cmpi sgt, %[[VAL_15]], %[[C0]] : index +! CHECK: %[[SELECT:.*]] = arith.select %[[CMPI]], %[[VAL_15]], %[[C0]] : index ! CHECK: %[[VAL_16:.*]] = fir.call @llvm.stacksave() : () -> !fir.ref -! CHECK: %[[VAL_17:.*]] = fir.alloca !fir.array, %[[VAL_15]] {bindc_name = ".result"} +! CHECK: %[[VAL_17:.*]] = fir.alloca !fir.array, %[[SELECT]] {bindc_name = ".result"} print *, array_func() end subroutine end subroutine