|
49 | 49 | from psyclone.domain.common.psylayer import PSyLoop |
50 | 50 | from psyclone.psyir import nodes |
51 | 51 | from psyclone.psyir.nodes import ( |
52 | | - Call, Loop, Reference, Routine, |
| 52 | + Call, Loop, Reference, Routine, Assignment, IfBlock, |
53 | 53 | BinaryOperation, IntrinsicCall |
54 | 54 | ) |
| 55 | +from psyclone.psyir.symbols import AutomaticInterface |
55 | 56 | from psyclone.psyir.tools import ( |
56 | 57 | DependencyTools, DTCode, ReductionInferenceTool |
57 | 58 | ) |
@@ -121,18 +122,57 @@ def _attempt_privatisation(loop, symbol_name, dry_run=False): |
121 | 122 | # privatising these |
122 | 123 | return False |
123 | 124 |
|
| 125 | + # If it's not a local symbol, we cannot guarantee its lifetime |
| 126 | + if not isinstance(sym.interface, AutomaticInterface): |
| 127 | + return False |
| 128 | + |
124 | 129 | if sym in loop.explicitly_private_symbols: |
125 | 130 | return True |
126 | 131 |
|
127 | | - # Check that the symbol is not referenced following this loop (before |
128 | | - # the loop is fine because we can use OpenMP/OpenACC first-private or |
129 | | - # Fortran do concurrent local_init()) |
130 | | - refs_in_loop = filter(lambda x: x.symbol is sym, loop.walk(Reference)) |
131 | | - last_access = list(refs_in_loop)[-1] |
132 | 132 | loop.compute_cached_abs_positions() |
| 133 | + |
| 134 | + # Get the last access |
| 135 | + refs_in_loop = filter(lambda x: x.symbol is sym, loop.walk(Reference)) |
| 136 | + refs_in_loop = list(refs_in_loop) |
| 137 | + last_access = refs_in_loop[-1] |
| 138 | + # If it's an assignment the last access is the one in the lhs |
| 139 | + if last_access.ancestor(Assignment): |
| 140 | + lhs = last_access.ancestor(Assignment).lhs |
| 141 | + if isinstance(lhs, Reference) and lhs.symbol is sym: |
| 142 | + last_access = lhs |
| 143 | + # If the value of the symbol is used after the loop, it cannot be |
| 144 | + # private |
133 | 145 | if last_access.escapes_scope(loop): |
134 | 146 | return False |
135 | 147 |
|
| 148 | + # Also prevent cases when the first access in the loop is a read that |
| 149 | + # could come from the previous iteration |
| 150 | + while True: |
| 151 | + first_access = refs_in_loop[0] |
| 152 | + # If it's an assignment the first access is the one in the rhs |
| 153 | + if first_access.ancestor(Assignment): |
| 154 | + rhs = first_access.ancestor(Assignment).rhs |
| 155 | + refs = filter(lambda x: x.symbol is sym, rhs.walk(Reference)) |
| 156 | + refs = list(refs) |
| 157 | + if refs: |
| 158 | + first_access = refs[0] |
| 159 | + if first_access.is_read: |
| 160 | + return False |
| 161 | + # If it is inside a conditional, there may be more entry points for |
| 162 | + # this symbol, so we look for the next 'fist_access' |
| 163 | + inside_conditional = first_access.ancestor(IfBlock, limit=loop) |
| 164 | + if first_access.ancestor(IfBlock, limit=loop): |
| 165 | + following = inside_conditional.following_node() |
| 166 | + if not following: |
| 167 | + break |
| 168 | + # Skip al references in the condition that we already checked |
| 169 | + refs_in_loop = list(filter( |
| 170 | + lambda x: x.abs_position > following.abs_position, |
| 171 | + refs_in_loop |
| 172 | + )) |
| 173 | + if not inside_conditional or not refs_in_loop: |
| 174 | + break |
| 175 | + |
136 | 176 | if not dry_run: |
137 | 177 | loop.explicitly_private_symbols.add(sym) |
138 | 178 |
|
|
0 commit comments