[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / flang / test / Lower / HLFIR / substrings.f90
blobd97f75eb51d652573163d3db9bfbe5e47dfe8034
1 ! Test lowering of substrings to HLFIR
2 ! Note: cse is run to make the expected output more readable by sharing
3 ! the boilerplate between the different susbtring cases.
4 ! RUN: bbc -emit-fir -hlfir -o - %s | fir-opt -cse -o - | FileCheck %s
6 ! CHECK-LABEL: func.func @_QPcst_len(
7 subroutine cst_len(array, scalar)
8 character(10) :: array(100), scalar
9 ! CHECK: %[[VAL_5:.*]] = arith.constant 100 : index
10 ! CHECK: %[[VAL_7:.*]]:2 = hlfir.declare %{{.*}}(%[[VAL_6:.*]]) typeparams %[[VAL_3:[^ ]*]] {{.*}}array"
11 ! CHECK: %[[VAL_9:.*]]:2 = hlfir.declare %{{.*}} typeparams %[[VAL_3]] {{.*}}scalar"
12 print *, array(:)(2:5)
13 ! CHECK: %[[VAL_15:.*]] = arith.constant 1 : index
14 ! CHECK: %[[VAL_16:.*]] = arith.constant 2 : index
15 ! CHECK: %[[VAL_17:.*]] = arith.constant 5 : index
16 ! CHECK: %[[VAL_18:.*]] = arith.constant 4 : index
17 ! CHECK: %[[VAL_19:.*]] = hlfir.designate %[[VAL_7]]#0 (%[[VAL_15]]:%[[VAL_5]]:%[[VAL_15]]) substr %[[VAL_16]], %[[VAL_17]] shape %[[VAL_6]] typeparams %[[VAL_18]] : (!fir.ref<!fir.array<100x!fir.char<1,10>>>, index, index, index, index, index, !fir.shape<1>, index) -> !fir.box<!fir.array<100x!fir.char<1,4>>>
19 print *, array(42)(2:5)
20 ! CHECK: %[[VAL_25:.*]] = arith.constant 42 : index
21 ! CHECK: %[[VAL_26:.*]] = hlfir.designate %[[VAL_7]]#0 (%[[VAL_25]]) substr %[[VAL_16]], %[[VAL_17]] typeparams %[[VAL_18]] : (!fir.ref<!fir.array<100x!fir.char<1,10>>>, index, index, index, index) -> !fir.ref<!fir.char<1,4>>
22 print *, array(:)(2:)
23 ! CHECK: %[[VAL_33:.*]] = arith.constant 9 : index
24 ! CHECK: %[[VAL_34:.*]] = hlfir.designate %[[VAL_7]]#0 (%[[VAL_15]]:%[[VAL_5]]:%[[VAL_15]]) substr %[[VAL_16]], %[[VAL_3]] shape %[[VAL_6]] typeparams %[[VAL_33]] : (!fir.ref<!fir.array<100x!fir.char<1,10>>>, index, index, index, index, index, !fir.shape<1>, index) -> !fir.box<!fir.array<100x!fir.char<1,9>>>
26 print *, scalar(2:5)
27 ! CHECK: %[[VAL_40:.*]] = hlfir.designate %[[VAL_9]]#0 substr %[[VAL_16]], %[[VAL_17]] typeparams %[[VAL_18]] : (!fir.boxchar<1>, index, index, index) -> !fir.ref<!fir.char<1,4>>
28 end subroutine
30 ! CHECK-LABEL: func.func @_QPdyn_len(
31 subroutine dyn_len(array, scalar, l, n, m, k)
32 integer(8) :: n,m,k
33 ! CHECK: %[[VAL_7:.*]]:2 = hlfir.declare {{.*}}k"
34 ! CHECK: %[[VAL_8:.*]]:2 = hlfir.declare {{.*}}m"
35 ! CHECK: %[[VAL_9:.*]]:2 = hlfir.declare {{.*}}n"
36 character(l) :: array(:), scalar
37 ! CHECK: %[[VAL_14:.*]]:2 = hlfir.declare %{{.*}} typeparams %[[VAL_13:[^ ]*]] {{.*}}array"
38 ! CHECK: %[[VAL_19:.*]]:2 = hlfir.declare %{{.*}} typeparams %[[VAL_18:[^ ]*]] {{.*}}scalar"
40 print *, array(:)(n:m)
41 ! CHECK: %[[VAL_25:.*]] = arith.constant 1 : index
42 ! CHECK: %[[VAL_26:.*]] = arith.constant 0 : index
43 ! CHECK: %[[VAL_27:.*]]:3 = fir.box_dims %[[VAL_14]]#1, %[[VAL_26]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>, index) -> (index, index, index)
44 ! CHECK: %[[VAL_28:.*]] = arith.subi %[[VAL_27]]#1, %[[VAL_25]] : index
45 ! CHECK: %[[VAL_29:.*]] = arith.addi %[[VAL_28]], %[[VAL_25]] : index
46 ! CHECK: %[[VAL_30:.*]] = arith.divsi %[[VAL_29]], %[[VAL_25]] : index
47 ! CHECK: %[[VAL_31:.*]] = arith.cmpi sgt, %[[VAL_30]], %[[VAL_26]] : index
48 ! CHECK: %[[VAL_32:.*]] = arith.select %[[VAL_31]], %[[VAL_30]], %[[VAL_26]] : index
49 ! CHECK: %[[VAL_33:.*]] = fir.shape %[[VAL_32]] : (index) -> !fir.shape<1>
50 ! CHECK: %[[VAL_34:.*]] = fir.load %[[VAL_9]]#0 : !fir.ref<i64>
51 ! CHECK: %[[VAL_35:.*]] = fir.load %[[VAL_8]]#0 : !fir.ref<i64>
52 ! CHECK: %[[VAL_36:.*]] = fir.convert %[[VAL_34]] : (i64) -> index
53 ! CHECK: %[[VAL_37:.*]] = fir.convert %[[VAL_35]] : (i64) -> index
54 ! CHECK: %[[VAL_38:.*]] = arith.subi %[[VAL_37]], %[[VAL_36]] : index
55 ! CHECK: %[[VAL_39:.*]] = arith.addi %[[VAL_38]], %[[VAL_25]] : index
56 ! CHECK: %[[VAL_40:.*]] = arith.cmpi sgt, %[[VAL_39]], %[[VAL_26]] : index
57 ! CHECK: %[[VAL_41:.*]] = arith.select %[[VAL_40]], %[[VAL_39]], %[[VAL_26]] : index
58 ! CHECK: %[[VAL_42:.*]] = hlfir.designate %[[VAL_14]]#0 (%[[VAL_25]]:%[[VAL_27]]#1:%[[VAL_25]]) substr %[[VAL_36]], %[[VAL_37]] shape %[[VAL_33]] typeparams %[[VAL_41]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>, index, index, index, index, index, !fir.shape<1>, index) -> !fir.box<!fir.array<?x!fir.char<1,?>>>
60 print *, array(k)(n:m)
61 ! CHECK: %[[VAL_48:.*]] = fir.load %[[VAL_7]]#0 : !fir.ref<i64>
62 ! CHECK: %[[VAL_49:.*]] = fir.load %[[VAL_9]]#0 : !fir.ref<i64>
63 ! CHECK: %[[VAL_50:.*]] = fir.load %[[VAL_8]]#0 : !fir.ref<i64>
64 ! CHECK: %[[VAL_51:.*]] = fir.convert %[[VAL_49]] : (i64) -> index
65 ! CHECK: %[[VAL_52:.*]] = fir.convert %[[VAL_50]] : (i64) -> index
66 ! CHECK: %[[VAL_53:.*]] = arith.subi %[[VAL_52]], %[[VAL_51]] : index
67 ! CHECK: %[[VAL_54:.*]] = arith.addi %[[VAL_53]], %[[VAL_25]] : index
68 ! CHECK: %[[VAL_55:.*]] = arith.cmpi sgt, %[[VAL_54]], %[[VAL_26]] : index
69 ! CHECK: %[[VAL_56:.*]] = arith.select %[[VAL_55]], %[[VAL_54]], %[[VAL_26]] : index
70 ! CHECK: %[[VAL_57:.*]] = hlfir.designate %[[VAL_14]]#0 (%[[VAL_48]]) substr %[[VAL_51]], %[[VAL_52]] typeparams %[[VAL_56]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>, i64, index, index, index) -> !fir.boxchar<1>
72 print *, array(:)(n:)
73 ! CHECK: %[[VAL_65:.*]] = fir.load %[[VAL_9]]#0 : !fir.ref<i64>
74 ! CHECK: %[[VAL_66:.*]] = fir.convert %[[VAL_13]] : (i32) -> i64
75 ! CHECK: %[[VAL_67:.*]] = fir.convert %[[VAL_65]] : (i64) -> index
76 ! CHECK: %[[VAL_68:.*]] = fir.convert %[[VAL_66]] : (i64) -> index
77 ! CHECK: %[[VAL_69:.*]] = arith.subi %[[VAL_68]], %[[VAL_67]] : index
78 ! CHECK: %[[VAL_70:.*]] = arith.addi %[[VAL_69]], %[[VAL_25]] : index
79 ! CHECK: %[[VAL_71:.*]] = arith.cmpi sgt, %[[VAL_70]], %[[VAL_26]] : index
80 ! CHECK: %[[VAL_72:.*]] = arith.select %[[VAL_71]], %[[VAL_70]], %[[VAL_26]] : index
81 ! CHECK: %[[VAL_73:.*]] = hlfir.designate %[[VAL_14]]#0 (%[[VAL_25]]:%[[VAL_27]]#1:%[[VAL_25]]) substr %[[VAL_67]], %[[VAL_68]] shape %[[VAL_33]] typeparams %[[VAL_72]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>, index, index, index, index, index, !fir.shape<1>, index) -> !fir.box<!fir.array<?x!fir.char<1,?>>>
83 print *, scalar(n:m)
84 ! CHECK: %[[VAL_79:.*]] = fir.load %[[VAL_9]]#0 : !fir.ref<i64>
85 ! CHECK: %[[VAL_80:.*]] = fir.load %[[VAL_8]]#0 : !fir.ref<i64>
86 ! CHECK: %[[VAL_81:.*]] = fir.convert %[[VAL_79]] : (i64) -> index
87 ! CHECK: %[[VAL_82:.*]] = fir.convert %[[VAL_80]] : (i64) -> index
88 ! CHECK: %[[VAL_83:.*]] = arith.subi %[[VAL_82]], %[[VAL_81]] : index
89 ! CHECK: %[[VAL_84:.*]] = arith.addi %[[VAL_83]], %[[VAL_25]] : index
90 ! CHECK: %[[VAL_85:.*]] = arith.cmpi sgt, %[[VAL_84]], %[[VAL_26]] : index
91 ! CHECK: %[[VAL_86:.*]] = arith.select %[[VAL_85]], %[[VAL_84]], %[[VAL_26]] : index
92 ! CHECK: %[[VAL_87:.*]] = hlfir.designate %[[VAL_19]]#0 substr %[[VAL_81]], %[[VAL_82]] typeparams %[[VAL_86]] : (!fir.boxchar<1>, index, index, index) -> !fir.boxchar<1>
93 end subroutine
95 subroutine test_static_substring(i, j)
96 integer(8) :: i, j
97 print *, "hello"(i:j)
98 end subroutine
99 ! CHECK-LABEL: func.func @_QPtest_static_substring(
100 ! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %{{.*}}i"
101 ! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %{{.*}}j"
102 ! CHECK: %[[VAL_10:.*]] = arith.constant 5 : index
103 ! CHECK: %[[VAL_11:.*]]:2 = hlfir.declare %{{.*}} typeparams %[[VAL_10]] {fortran_attrs = #fir.var_attrs<parameter>, uniq_name = ".stringlit"} : (!fir.ref<!fir.char<1,5>>, index) -> (!fir.ref<!fir.char<1,5>>, !fir.ref<!fir.char<1,5>>)
104 ! CHECK: %[[VAL_12:.*]] = fir.load %[[VAL_2]]#0 : !fir.ref<i64>
105 ! CHECK: %[[VAL_13:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<i64>
106 ! CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_12]] : (i64) -> index
107 ! CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_13]] : (i64) -> index
108 ! CHECK: %[[VAL_16:.*]] = arith.constant 1 : index
109 ! CHECK: %[[VAL_17:.*]] = arith.subi %[[VAL_15]], %[[VAL_14]] : index
110 ! CHECK: %[[VAL_18:.*]] = arith.addi %[[VAL_17]], %[[VAL_16]] : index
111 ! CHECK: %[[VAL_19:.*]] = arith.constant 0 : index
112 ! CHECK: %[[VAL_20:.*]] = arith.cmpi sgt, %[[VAL_18]], %[[VAL_19]] : index
113 ! CHECK: %[[VAL_21:.*]] = arith.select %[[VAL_20]], %[[VAL_18]], %[[VAL_19]] : index
114 ! CHECK: %[[VAL_22:.*]] = hlfir.designate %[[VAL_11]]#0 substr %[[VAL_14]], %[[VAL_15]] typeparams %[[VAL_21]] : (!fir.ref<!fir.char<1,5>>, index, index, index) -> !fir.boxchar<1>