xref: /llvm-project/flang/test/Transforms/stack-arrays.f90 (revision f35f863a88f83332bef9605ef4cfe4f05c066efb)
1*f35f863aSjeanPerier! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | fir-opt --array-value-copy | fir-opt --stack-arrays | FileCheck %s
2cc14bf22STom Eccles
3bf81ba37STom Eccles! In order to verify the whole MLIR pipeline, make the driver generate LLVM IR.
4bf81ba37STom Eccles! This is only to check that -fstack-arrays enables the stack-arrays pass so
5bf81ba37STom Eccles! only check the first example
6*f35f863aSjeanPerier! RUN: %flang_fc1 -emit-llvm -flang-deprecated-no-hlfir -o - -fstack-arrays %s | FileCheck --check-prefix=LLVM-IR %s
7bf81ba37STom Eccles
8cc14bf22STom Eccles! check simple array value copy case
9cc14bf22STom Ecclessubroutine array_value_copy_simple(arr)
10cc14bf22STom Eccles  integer, intent(inout) :: arr(4)
11cc14bf22STom Eccles  arr(3:4) = arr(1:2)
12cc14bf22STom Ecclesend subroutine
13cc14bf22STom Eccles! CHECK-LABEL: func.func @_QParray_value_copy_simple(%arg0: !fir.ref<!fir.array<4xi32>>
14cc14bf22STom Eccles! CHECK-NOT: fir.allocmem
15cc14bf22STom Eccles! CHECK-NOT: fir.freemem
16cc14bf22STom Eccles! CHECK: fir.alloca !fir.array<4xi32>
17cc14bf22STom Eccles! CHECK-NOT: fir.allocmem
18cc14bf22STom Eccles! CHECK-NOT: fir.freemem
19cc14bf22STom Eccles! CHECK: return
20cc14bf22STom Eccles! CHECK-NEXT: }
21cc14bf22STom Eccles
22bf81ba37STom Eccles! LLVM-IR: array_value_copy_simple
23bf81ba37STom Eccles! LLVM-IR-NOT: malloc
24bf81ba37STom Eccles! LLVM-IR-NOT: free
25bf81ba37STom Eccles! LLVM-IR: alloca [4 x i32]
26bf81ba37STom Eccles! LLVM-IR-NOT: malloc
27bf81ba37STom Eccles! LLVM-IR-NOT: free
28bf81ba37STom Eccles! LLVM-IR: ret void
29bf81ba37STom Eccles! LLVM-IR-NEXT: }
30bf81ba37STom Eccles
31cc14bf22STom Eccles! check complex array value copy case
32cc14bf22STom Ecclesmodule stuff
33cc14bf22STom Eccles  type DerivedWithAllocatable
34cc14bf22STom Eccles    integer, dimension(:), allocatable :: dat
35cc14bf22STom Eccles  end type
36cc14bf22STom Eccles
37cc14bf22STom Eccles  contains
38cc14bf22STom Eccles  subroutine array_value_copy_complex(arr)
39cc14bf22STom Eccles    type(DerivedWithAllocatable), intent(inout) :: arr(:)
40cc14bf22STom Eccles    arr(3:4) = arr(1:2)
41cc14bf22STom Eccles  end subroutine
42cc14bf22STom Ecclesend module
43cc14bf22STom Eccles! CHECK: func.func
44cc14bf22STom Eccles! CHECK-SAME: array_value_copy_complex
45cc14bf22STom Eccles! CHECK-NOT: fir.allocmem
46cc14bf22STom Eccles! CHECK-NOT: fir.freemem
47cc14bf22STom Eccles! CHECK: fir.alloca !fir.array<?x!fir.type<_QMstuffTderivedwithallocatable
48cc14bf22STom Eccles! CHECK-NOT: fir.allocmem
49cc14bf22STom Eccles! CHECK-NOT: fir.freemem
50cc14bf22STom Eccles! CHECK: return
51cc14bf22STom Eccles! CHECK-NEXT: }
52cc14bf22STom Eccles
53cc14bf22STom Ecclessubroutine parameter_array_init
54cc14bf22STom Eccles  integer, parameter :: p(100) = 42
55cc14bf22STom Eccles  call use_p(p)
56cc14bf22STom Ecclesend subroutine
57cc14bf22STom Eccles! CHECK: func.func
58cc14bf22STom Eccles! CHECK-SAME: parameter_array_init
59cc14bf22STom Eccles! CHECK-NOT: fir.allocmem
60cc14bf22STom Eccles! CHECK-NOT: fir.freemem
61cc14bf22STom Eccles! CHECK: fir.alloca !fir.array<100xi32>
62cc14bf22STom Eccles! CHECK-NOT: fir.allocmem
63cc14bf22STom Eccles! CHECK-NOT: fir.freemem
64cc14bf22STom Eccles! CHECK: return
65cc14bf22STom Eccles! CHECK-NEXT: }
66cc14bf22STom Eccles
67cc14bf22STom Ecclessubroutine test_vector_subscripted_section_to_box(v, x)
68cc14bf22STom Eccles  interface
69cc14bf22STom Eccles    subroutine takes_box(y)
70cc14bf22STom Eccles      real :: y(:)
71cc14bf22STom Eccles    end subroutine
72cc14bf22STom Eccles  end interface
73cc14bf22STom Eccles
74cc14bf22STom Eccles  integer :: v(:)
75cc14bf22STom Eccles  real :: x(:)
76cc14bf22STom Eccles  call takes_box(x(v))
77cc14bf22STom Ecclesend subroutine
78cc14bf22STom Eccles! CHECK: func.func
79cc14bf22STom Eccles! CHECK-SAME: test_vector_subscripted_section_to_box
80cc14bf22STom Eccles! CHECK-NOT: fir.allocmem
81cc14bf22STom Eccles! CHECK: fir.alloca !fir.array<?xf32>
82cc14bf22STom Eccles! CHECK-NOT: fir.allocmem
83cc14bf22STom Eccles! CHECK: fir.call @_QPtakes_box
84cc14bf22STom Eccles! CHECK-NOT: fir.freemem
85cc14bf22STom Eccles! CHECK: return
86cc14bf22STom Eccles! CHECK-NEXT: }
87cc14bf22STom Eccles
88cc14bf22STom Ecclessubroutine call_parenthesized_arg(x)
89cc14bf22STom Eccles  integer :: x(100)
90cc14bf22STom Eccles  call bar((x))
91cc14bf22STom Ecclesend subroutine
92cc14bf22STom Eccles! CHECK: func.func
93cc14bf22STom Eccles! CHECK-SAME: call_parenthesized_arg
94cc14bf22STom Eccles! CHECK-NOT: fir.allocmem
95cc14bf22STom Eccles! CHECK: fir.alloca !fir.array<100xi32>
96cc14bf22STom Eccles! CHECK-NOT: fir.allocmem
97cc14bf22STom Eccles! CHECK: fir.call @_QPbar
98cc14bf22STom Eccles! CHECK-NOT: fir.freemem
99cc14bf22STom Eccles! CHECK: return
100cc14bf22STom Eccles! CHECK-NEXT: }
101cc14bf22STom Eccles
102cc14bf22STom Ecclessubroutine where_allocatable_assignments(a, b)
103cc14bf22STom Eccles  integer :: a(:)
104cc14bf22STom Eccles  integer, allocatable :: b(:)
105cc14bf22STom Eccles  where(b > 0)
106cc14bf22STom Eccles    b = a
107cc14bf22STom Eccles  elsewhere
108cc14bf22STom Eccles    b(:) = 0
109cc14bf22STom Eccles  end where
110cc14bf22STom Ecclesend subroutine
111cc14bf22STom Eccles! TODO: broken: passing allocation through fir.result
112cc14bf22STom Eccles! CHECK: func.func
113cc14bf22STom Eccles! CHECK-SAME: where_allocatable_assignments
114cc14bf22STom Eccles! CHECK: return
115cc14bf22STom Eccles! CHECK-NEXT: }
116cc14bf22STom Eccles
117cc14bf22STom Ecclessubroutine array_constructor(a, b)
118cc14bf22STom Eccles  real :: a(5), b
119cc14bf22STom Eccles  real, external :: f
120cc14bf22STom Eccles  a = [f(b), f(b+1), f(b+2), f(b+5), f(b+11)]
121cc14bf22STom Ecclesend subroutine
122cc14bf22STom Eccles! TODO: broken: realloc
123cc14bf22STom Eccles! CHECK: func.func
124cc14bf22STom Eccles! CHECK-SAME: array_constructor
125cc14bf22STom Eccles! CHECK: return
126cc14bf22STom Eccles! CHECK-NEXT: }
127cc14bf22STom Eccles
128cc14bf22STom Ecclessubroutine sequence(seq, n)
129cc14bf22STom Eccles  integer :: n, seq(n)
130cc14bf22STom Eccles  seq = [(i,i=1,n)]
131cc14bf22STom Ecclesend subroutine
132cc14bf22STom Eccles! TODO: broken: realloc
133cc14bf22STom Eccles! CHECK: func.func
134cc14bf22STom Eccles! CHECK-SAME: sequence
135cc14bf22STom Eccles! CHECK: return
136cc14bf22STom Eccles! CHECK-NEXT: }
137cc14bf22STom Eccles
138cc14bf22STom Ecclessubroutine CFGLoop(x)
139cc14bf22STom Eccles  integer, parameter :: k = 100, m=1000000, n = k*m
140cc14bf22STom Eccles  integer :: x(n)
141cc14bf22STom Eccles  logical :: has_error
142cc14bf22STom Eccles
143cc14bf22STom Eccles  do i=0,m-1
144cc14bf22STom Eccles    x(k*i+1:k*(i+1)) = x(k*(i+1):k*i+1:-1)
145cc14bf22STom Eccles    if (has_error(x, k)) stop
146cc14bf22STom Eccles  end do
147cc14bf22STom Ecclesend subroutine
148cc14bf22STom Eccles! CHECK: func.func
149cc14bf22STom Eccles! CHECK-SAME: cfgloop
150cc14bf22STom Eccles! CHECK-NEXT: %[[MEM:.*]] = fir.alloca !fir.array<100000000xi32>
151cc14bf22STom Eccles! CHECK-NOT: fir.allocmem
152cc14bf22STom Eccles! CHECK-NOT: fir.freemem
153cc14bf22STom Eccles! CHECK: return
154cc14bf22STom Eccles! CHECK-NEXT: }
155