[SimplifyCFG] Always allow hoisting if all instructions match. (#97158)
[llvm-project.git] / flang / test / Lower / Intrinsics / random.f90
blob4fb1a9a5da27a76e1c110a01cde5a5500eff90b7
1 ! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s
3 ! CHECK-LABEL: func @_QPrandom_test_1
4 subroutine random_test_1
5 ! CHECK-DAG: [[ss:%[0-9]+]] = fir.alloca {{.*}}random_test_1Ess
6 ! CHECK-DAG: [[vv:%[0-9]+]] = fir.alloca {{.*}}random_test_1Evv
7 integer ss, vv(40)
8 ! CHECK-DAG: [[rr:%[0-9]+]] = fir.alloca {{.*}}random_test_1Err
9 ! CHECK-DAG: [[aa:%[0-9]+]] = fir.alloca {{.*}}random_test_1Eaa
10 real rr, aa(5)
11 ! CHECK: fir.call @_FortranARandomInit(%true{{.*}}, %false{{.*}}) {{.*}}: (i1, i1) -> none
12 call random_init(.true., .false.)
13 ! CHECK: [[box:%[0-9]+]] = fir.embox [[ss]]
14 ! CHECK: [[argbox:%[0-9]+]] = fir.convert [[box]]
15 ! CHECK: fir.call @_FortranARandomSeedSize([[argbox]]
16 call random_seed(size=ss)
17 print*, 'size: ', ss
18 ! CHECK: fir.call @_FortranARandomSeedDefaultPut() {{.*}}: () -> none
19 call random_seed()
20 ! CHECK: [[box:%[0-9]+]] = fir.embox [[rr]]
21 ! CHECK: [[argbox:%[0-9]+]] = fir.convert [[box]]
22 ! CHECK: fir.call @_FortranARandomNumber([[argbox]]
23 call random_number(rr)
24 print*, rr
25 ! CHECK: [[box:%[0-9]+]] = fir.embox [[vv]]
26 ! CHECK: [[argbox:%[0-9]+]] = fir.convert [[box]]
27 ! CHECK: fir.call @_FortranARandomSeedGet([[argbox]]
28 call random_seed(get=vv)
29 ! print*, 'get: ', vv(1:ss)
30 ! CHECK: [[box:%[0-9]+]] = fir.embox [[vv]]
31 ! CHECK: [[argbox:%[0-9]+]] = fir.convert [[box]]
32 ! CHECK: fir.call @_FortranARandomSeedPut([[argbox]]
33 call random_seed(put=vv)
34 print*, 'put: ', vv(1:ss)
35 ! CHECK: [[box:%[0-9]+]] = fir.embox [[aa]]
36 ! CHECK: [[argbox:%[0-9]+]] = fir.convert [[box]]
37 ! CHECK: fir.call @_FortranARandomNumber([[argbox]]
38 call random_number(aa)
39 print*, aa
40 end
42 ! CHECK-LABEL: func @_QPrandom_test_2
43 subroutine random_test_2
44 integer :: size, get(5) = -9
45 call foo(size)
46 call bar(size, get)
47 contains
48 ! CHECK-LABEL: func private @_QFrandom_test_2Pfoo
49 subroutine foo(size, put, get)
50 ! CHECK: [[s1:%[0-9]+]] = fir.is_present %arg0
51 ! CHECK: [[s2:%[0-9]+]] = fir.embox %arg0
52 ! CHECK: [[s3:%[0-9]+]] = fir.absent !fir.box<i32>
53 ! CHECK: [[s4:%[0-9]+]] = arith.select [[s1]], [[s2]], [[s3]] : !fir.box<i32>
54 integer, optional :: size
55 ! CHECK: [[p1:%[0-9]+]] = fir.is_present %arg1
56 ! CHECK: [[p2:%[0-9]+]] = fir.embox %arg1
57 ! CHECK: [[p3:%[0-9]+]] = fir.absent !fir.box<!fir.array<5xi32>>
58 ! CHECK: [[p4:%[0-9]+]] = arith.select [[p1]], [[p2]], [[p3]] : !fir.box<!fir.array<5xi32>>
59 integer, optional :: put(5)
60 ! CHECK: [[g1:%[0-9]+]] = fir.is_present %arg2
61 ! CHECK: [[g2:%[0-9]+]] = fir.embox %arg2
62 ! CHECK: [[g3:%[0-9]+]] = fir.absent !fir.box<!fir.array<5xi32>>
63 ! CHECK: [[g4:%[0-9]+]] = arith.select [[g1]], [[g2]], [[g3]] : !fir.box<!fir.array<5xi32>>
64 integer, optional :: get(5)
65 ! CHECK: [[s5:%[0-9]+]] = fir.convert [[s4]] : (!fir.box<i32>) -> !fir.box<none>
66 ! CHECK: [[p5:%[0-9]+]] = fir.convert [[p4]] : (!fir.box<!fir.array<5xi32>>) -> !fir.box<none>
67 ! CHECK: [[g5:%[0-9]+]] = fir.convert [[g4]] : (!fir.box<!fir.array<5xi32>>) -> !fir.box<none>
68 ! CHECK: fir.call @_FortranARandomSeed([[s5]], [[p5]], [[g5]]
69 call random_seed(size, put, get)
70 print*, size
71 end subroutine
73 ! CHECK-LABEL: func private @_QFrandom_test_2Pbar
74 subroutine bar(size, get, put)
75 integer, optional :: size
76 ! CHECK: [[p1:%[0-9]+]] = fir.is_present %arg2
77 ! CHECK: [[p2:%[0-9]+]] = fir.embox %arg2
78 ! CHECK: [[p3:%[0-9]+]] = fir.absent !fir.box<!fir.array<5xi32>>
79 ! CHECK: [[p4:%[0-9]+]] = arith.select [[p1]], [[p2]], [[p3]] : !fir.box<!fir.array<5xi32>>
80 integer, optional :: put(5)
81 ! CHECK: [[g1:%[0-9]+]] = fir.is_present %arg1
82 ! CHECK: [[g2:%[0-9]+]] = fir.embox %arg1
83 ! CHECK: [[g3:%[0-9]+]] = fir.absent !fir.box<!fir.array<5xi32>>
84 ! CHECK: [[g4:%[0-9]+]] = arith.select [[g1]], [[g2]], [[g3]] : !fir.box<!fir.array<5xi32>>
85 integer, optional :: get(5)
86 ! CHECK: [[s1:%[0-9]+]] = fir.absent !fir.box<none>
87 ! CHECK: [[p5:%[0-9]+]] = fir.convert [[p4]] : (!fir.box<!fir.array<5xi32>>) -> !fir.box<none>
88 ! CHECK: [[g5:%[0-9]+]] = fir.convert [[g4]] : (!fir.box<!fir.array<5xi32>>) -> !fir.box<none>
89 ! CHECK: fir.call @_FortranARandomSeed([[s1]], [[p5]], [[g5]]
90 call random_seed(put=put, get=get)
91 print*, get(1:size+1) ! "extra" value should be -9
92 end subroutine
93 end
95 call random_test_1
96 call random_test_2
97 end