|
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 | 55 | from psyclone.psyir.tools import ( |
@@ -124,15 +124,50 @@ def _attempt_privatisation(loop, symbol_name, dry_run=False): |
124 | 124 | if sym in loop.explicitly_private_symbols: |
125 | 125 | return True |
126 | 126 |
|
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 | 127 | loop.compute_cached_abs_positions() |
| 128 | + |
| 129 | + # Get the last access |
| 130 | + refs_in_loop = filter(lambda x: x.symbol is sym, loop.walk(Reference)) |
| 131 | + refs_in_loop = list(refs_in_loop) |
| 132 | + last_access = refs_in_loop[-1] |
| 133 | + # If it's an assignment the last access is the one in the lhs |
| 134 | + if last_access.ancestor(Assignment): |
| 135 | + lhs = last_access.ancestor(Assignment).lhs |
| 136 | + if isinstance(lhs, Reference) and lhs.symbol is sym: |
| 137 | + last_access = lhs |
| 138 | + # If the value of the symbol is used after the loop, it cannot be |
| 139 | + # private |
133 | 140 | if last_access.escapes_scope(loop): |
134 | 141 | return False |
135 | 142 |
|
| 143 | + # Also prevent cases when the first access in the loop is a read that |
| 144 | + # could come from the previous iteration |
| 145 | + while True: |
| 146 | + first_access = refs_in_loop[0] |
| 147 | + # If it's an assignment the first access is the one in the rhs |
| 148 | + if first_access.ancestor(Assignment): |
| 149 | + rhs = first_access.ancestor(Assignment).rhs |
| 150 | + refs = filter(lambda x: x.symbol is sym, rhs.walk(Reference)) |
| 151 | + refs = list(refs) |
| 152 | + if refs: |
| 153 | + first_access = refs[0] |
| 154 | + if first_access.is_read: |
| 155 | + return False |
| 156 | + # If it is inside a conditional, there may be more entry points for |
| 157 | + # this symbol, so we look for the next 'first_access' |
| 158 | + inside_conditional = first_access.ancestor(IfBlock, limit=loop) |
| 159 | + if inside_conditional: |
| 160 | + following = inside_conditional.following_node() |
| 161 | + if not following: |
| 162 | + break |
| 163 | + # Skip al references in the condition that we already checked |
| 164 | + refs_in_loop = list(filter( |
| 165 | + lambda x: x.abs_position > following.abs_position, |
| 166 | + refs_in_loop |
| 167 | + )) |
| 168 | + if not inside_conditional or not refs_in_loop: |
| 169 | + break |
| 170 | + |
136 | 171 | if not dry_run: |
137 | 172 | loop.explicitly_private_symbols.add(sym) |
138 | 173 |
|
@@ -281,9 +316,10 @@ def validate(self, node, options=None, **kwargs): |
281 | 316 | dry_run=True): |
282 | 317 | errors.append( |
283 | 318 | f"The write-write dependency in '{var_name}'" |
284 | | - f" cannot be solved by array privatisation " |
285 | | - f"because it is not a plain local array or " |
286 | | - f"it is used after the loop.") |
| 319 | + f" cannot be solved by automatic array " |
| 320 | + f"privatisation. Use 'loop.explictly_private" |
| 321 | + f"_sybmols.add(sybmol)' if *YOU* can guarantee" |
| 322 | + f" that it is private.") |
287 | 323 | continue |
288 | 324 | # See if the scalar in question allows parallelisation of |
289 | 325 | # the loop using reduction clauses. |
|
0 commit comments