[LLVM] Fix Maintainers.md formatting (NFC)
[llvm-project.git] / flang / test / Analysis / AliasAnalysis / alias-analysis-3.fir
blobeab438576c2bcf409bfee3b9cb5546acd05aebc9
1 // Use --mlir-disable-threading so that the AA queries are serialized
2 // as well as its diagnostic output.
3 // RUN: fir-opt %s -pass-pipeline='builtin.module(func.func(test-fir-alias-analysis))' -split-input-file --mlir-disable-threading 2>&1 | FileCheck %s
5 // module m
6 //   type t
7 //      real, pointer :: pointer_component
8 //   end type t
9 //   type(t) :: a
10 // contains
11 //   subroutine test(pointer_dummy, x)
12 //     real, pointer :: pointer_dummy
13 //     real, target :: x
14 //     pointer_dummy => x
15 //     call test2(a%pointer_component)
16 //   end subroutine test
17 // end module m
19 // A composite with a pointer component may alias with a dummy pointer
20 // CHECK-LABEL: Testing : "_QMmPtest
21 // CHECK: a#0 <-> func.region0#0: MayAlias
23 // a's box cannot alias with raw reference to f32 (x)
24 // CHECK: a#0 <-> func.region0#1: NoAlias
26 // pointer_dummy's box cannot alias with raw reference to f32 (x)
27 // CHECK: func.region0#0 <-> func.region0#1: NoAlias
29 fir.global @_QMmEa : !fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}> {
30   %0 = fir.undefined !fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>
31   fir.has_value %0 : !fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>
33 func.func @_QMmPtest(%arg0: !fir.ref<!fir.box<!fir.ptr<f32>>> {fir.bindc_name = "pointer_dummy"}, %arg1: !fir.ref<f32> {fir.bindc_name = "x", fir.target}) attributes {test.ptr = "func"} {
34   %0 = fir.address_of(@_QMmEa) {test.ptr = "a"} : !fir.ref<!fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>>
35   %1 = fir.embox %arg1 : (!fir.ref<f32>) -> !fir.box<!fir.ptr<f32>>
36   fir.store %1 to %arg0 : !fir.ref<!fir.box<!fir.ptr<f32>>>
37   %2 = fir.field_index pointer_component, !fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>
38   %3 = fir.coordinate_of %0, %2 : (!fir.ref<!fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>>, !fir.field) -> !fir.ref<!fir.box<!fir.ptr<f32>>>
39   %4 = fir.load %3 : !fir.ref<!fir.box<!fir.ptr<f32>>>
40   %5 = fir.box_addr %4 : (!fir.box<!fir.ptr<f32>>) -> !fir.ptr<f32>
41   %6 = fir.convert %5 : (!fir.ptr<f32>) -> !fir.ref<f32>
42   fir.call @_QPtest2(%6) fastmath<contract> : (!fir.ref<f32>) -> ()
43   return
45 func.func private @_QPtest2(!fir.ref<f32>)
47 // -----
49 // A composite with a pointer component does not alias with a dummy
50 // argument of composite type with a pointer component:
51 // module m
52 //   type t
53 //      real, pointer :: pointer_component
54 //   end type t
55 //   type(t) :: a
56 // contains
57 //   subroutine test(b, x)
58 //     type(t) :: b
59 //     real, target :: x
60 //     a%pointer_component => x
61 //     call test2(b%pointer_component)
62 //   end subroutine test
63 // end module m
65 // CHECK-LABEL: Testing : "_QMmPtest"
66 // CHECK: a#0 <-> func.region0#0: NoAlias
68 fir.global @_QMmEa : !fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}> {
69   %0 = fir.undefined !fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>
70   fir.has_value %0 : !fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>
72 func.func @_QMmPtest(%arg0: !fir.ref<!fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>> {fir.bindc_name = "b"}, %arg1: !fir.ref<f32> {fir.bindc_name = "x", fir.target}) attributes {test.ptr = "func"} {
73   %0 = fir.address_of(@_QMmEa) {test.ptr = "a"} : !fir.ref<!fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>>
74   %1 = fir.field_index pointer_component, !fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>
75   %2 = fir.coordinate_of %0, %1 : (!fir.ref<!fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>>, !fir.field) -> !fir.ref<!fir.box<!fir.ptr<f32>>>
76   %3 = fir.embox %arg1 : (!fir.ref<f32>) -> !fir.box<!fir.ptr<f32>>
77   fir.store %3 to %2 : !fir.ref<!fir.box<!fir.ptr<f32>>>
78   %4 = fir.field_index pointer_component, !fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>
79   %5 = fir.coordinate_of %arg0, %4 : (!fir.ref<!fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>>, !fir.field) -> !fir.ref<!fir.box<!fir.ptr<f32>>>
80   %6 = fir.load %5 : !fir.ref<!fir.box<!fir.ptr<f32>>>
81   %7 = fir.box_addr %6 : (!fir.box<!fir.ptr<f32>>) -> !fir.ptr<f32>
82   %8 = fir.convert %7 : (!fir.ptr<f32>) -> !fir.ref<f32>
83   fir.call @_QPtest2(%8) fastmath<contract> : (!fir.ref<f32>) -> ()
84   return
86 func.func private @_QPtest2(!fir.ref<f32>)
88 // -----
90 // Two dummy arguments of composite type with a pointer component
91 // do not alias each other:
92 // module m
93 //   type t
94 //      real, pointer :: pointer_component
95 //   end type t
96 // contains
97 //   subroutine test(a, b, x)
98 //     type(t) :: a, b
99 //     real, target :: x
100 //     a%pointer_component => x
101 //     call test2(b%pointer_component)
102 //   end subroutine test
103 // end module m
105 // CHECK-LABEL: Testing : "_QMmPtest"
106 // CHECK: func.region0#0 <-> func.region0#1: NoAlias
108 func.func @_QMmPtest(%arg0: !fir.ref<!fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>> {fir.bindc_name = "a"}, %arg1: !fir.ref<!fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>> {fir.bindc_name = "b"}, %arg2: !fir.ref<f32> {fir.bindc_name = "x", fir.target}) attributes {test.ptr = "func"} {
109   %0 = fir.field_index pointer_component, !fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>
110   %1 = fir.coordinate_of %arg0, %0 : (!fir.ref<!fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>>, !fir.field) -> !fir.ref<!fir.box<!fir.ptr<f32>>>
111   %2 = fir.embox %arg2 : (!fir.ref<f32>) -> !fir.box<!fir.ptr<f32>>
112   fir.store %2 to %1 : !fir.ref<!fir.box<!fir.ptr<f32>>>
113   %3 = fir.field_index pointer_component, !fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>
114   %4 = fir.coordinate_of %arg1, %3 : (!fir.ref<!fir.type<_QMmTt{pointer_component:!fir.box<!fir.ptr<f32>>}>>, !fir.field) -> !fir.ref<!fir.box<!fir.ptr<f32>>>
115   %5 = fir.load %4 : !fir.ref<!fir.box<!fir.ptr<f32>>>
116   %6 = fir.box_addr %5 : (!fir.box<!fir.ptr<f32>>) -> !fir.ptr<f32>
117   %7 = fir.convert %6 : (!fir.ptr<f32>) -> !fir.ref<f32>
118   fir.call @_QPtest2(%7) fastmath<contract> : (!fir.ref<f32>) -> ()
119   return
121 func.func private @_QPtest2(!fir.ref<f32>)
123 // -----
125 // Two dummy arguments of composite type consisting of an allocatable
126 // component cannot alias:
127 // module m
128 //   type t
129 //      real, allocatable :: allocatable_component
130 //   end type t
131 // contains
132 //   subroutine test(a, b)
133 //     type(t) :: a, b
134 //     allocate(a%allocatable_component)
135 //     call test2(b%allocatable_component)
136 //   end subroutine test
137 // end module m
139 // CHECK-LABEL: Testing : "_QMmPtest"
140 // CHECK: func.region0#0 <-> func.region0#1: NoAlias
142 func.func @_QMmPtest(%arg0: !fir.ref<!fir.type<_QMmTt{allocatable_component:!fir.box<!fir.heap<f32>>}>> {fir.bindc_name = "a"}, %arg1: !fir.ref<!fir.type<_QMmTt{allocatable_component:!fir.box<!fir.heap<f32>>}>> {fir.bindc_name = "b"}) attributes {test.ptr = "func"} {
143   %0 = fir.field_index allocatable_component, !fir.type<_QMmTt{allocatable_component:!fir.box<!fir.heap<f32>>}>
144   %1 = fir.coordinate_of %arg0, %0 : (!fir.ref<!fir.type<_QMmTt{allocatable_component:!fir.box<!fir.heap<f32>>}>>, !fir.field) -> !fir.ref<!fir.box<!fir.heap<f32>>>
145   %2 = fir.allocmem f32 {uniq_name = "_QMmEallocatable_component.alloc"}
146   %3 = fir.embox %2 : (!fir.heap<f32>) -> !fir.box<!fir.heap<f32>>
147   fir.store %3 to %1 : !fir.ref<!fir.box<!fir.heap<f32>>>
148   %4 = fir.field_index allocatable_component, !fir.type<_QMmTt{allocatable_component:!fir.box<!fir.heap<f32>>}>
149   %5 = fir.coordinate_of %arg1, %4 : (!fir.ref<!fir.type<_QMmTt{allocatable_component:!fir.box<!fir.heap<f32>>}>>, !fir.field) -> !fir.ref<!fir.box<!fir.heap<f32>>>
150   %6 = fir.load %5 : !fir.ref<!fir.box<!fir.heap<f32>>>
151   %7 = fir.box_addr %6 : (!fir.box<!fir.heap<f32>>) -> !fir.heap<f32>
152   %8 = fir.convert %7 : (!fir.heap<f32>) -> !fir.ref<f32>
153   fir.call @_QPtest2(%8) fastmath<contract> : (!fir.ref<f32>) -> ()
154   return
156 func.func private @_QPtest2(!fir.ref<f32>)