158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367 | @dataclass
class Desymrefier:
"""
Rewrites the program by removing all reads/writes from/to symbols and symbol
definitions.
"""
def desymrefy(self, op: Operation):
"""
Desymrefy an operation. This method guarantees that the operation does
not have any symbols.
"""
self.prepare_op(op)
self.promote_op(op)
def promote_op(self, op: Operation):
"""
Promotes an operation. This method guarantees that the operation does
not have any symbols.
"""
# TODO: Add promoters in the next patch.
pass
def prepare_op(self, op: Operation):
"""
Prepares an operation for promotion. This method guarantees that any
symbol in any region of this operation is read at most once and written
at most once.
"""
# For operation with no regions we don't have to do any work.
if len(op.regions) == 0:
return
# Otherwise, we have to prepare regions.
for region in op.regions:
self.prepare_region(region)
def prepare_region(self, region: Region):
"""
Prepares a region for promotion. This method guarantees that any symbol
in the region is read at most once and written at most once.
"""
num_blocks = len(region.blocks)
if num_blocks == 1:
# If there is only one block, preparing region is easier, so we
# handle it separately.
self.prepare_block(region.block)
else:
# TODO: Support regions with multiple blocks. This is not too
# difficult but requires many more things:
# 1. SSA-construction algorithm to prune symbol declarations. This
# also requires analyses passes (dominators).
# 2. Insertion of entry/exit blocks to ensure dominance.
raise FrontendProgramException(
f"Running desymrefier on region with {num_blocks} > 1 blocks is "
"not supported."
)
def prepare_block(self, block: Block):
"""Prepares a block for promotion."""
# First, desymrefy nested regions.
for op in block.ops:
self.desymrefy(op)
self.prune_definitions(block)
self.prune_uses_without_definitions(block)
# Ensure that each symbol is read/written at most once.
symbols = get_symbols(block)
for symbol in symbols:
num_reads = sum(
isinstance(op, symref.FetchOp)
for op in block.ops
if get_symbol(op) == symbol
)
num_writes = sum(
isinstance(op, symref.UpdateOp)
for op in block.ops
if get_symbol(op) == symbol
)
if num_reads > 1 or num_writes > 1:
raise FrontendProgramException(
f"Block {block} not ready for promotion: found {num_reads}"
f" reads and {num_writes} writes."
)
def prune_definitions(self, block: Block):
"""Removes all symbol definitions and their uses from the block."""
# Find all symbol definitions in this block. If no definitions
# found, terminate.
while (
len(
definitions := [
op for op in block.ops if isinstance(op, symref.DeclareOp)
]
)
> 0
):
# Otherwise, some definitions are still alive.
for definition in definitions:
symbol = get_symbol(definition)
# Find all reads and writes for this symbol.
reads = [
op
for op in block.ops
if isinstance(op, symref.FetchOp) and get_symbol(op) == symbol
]
writes = [
op
for op in block.ops
if isinstance(op, symref.UpdateOp) and get_symbol(op) == symbol
]
# Symbol is never read, so remove its definition and any writes.
if len(reads) == 0:
for write in writes:
Rewriter.erase_op(write)
Rewriter.erase_op(definition)
continue
# For symbols which are written once, the write dominates all
# the uses and therefore can be trivially replaced.
if len(writes) == 1:
write = writes[0]
for read in reads:
Rewriter.replace_op(read, [], [write.operands[0]])
Rewriter.erase_op(write)
Rewriter.erase_op(definition)
continue
# If there are multiple reads and writes, replace every
# read with the closest preceding write.
for read in reads:
write = lower_positional_bound(writes, read)
if write is not None:
Rewriter.replace_op(read, [], [write.operands[0]])
def _prune_unused_reads(self, block: Block):
def is_unused_read(op: Operation) -> bool:
return isinstance(op, symref.FetchOp) and not op.results[0].uses
unused_reads = [op for op in block.ops if is_unused_read(op)]
for read in unused_reads:
Rewriter.erase_op(read)
def prune_uses_without_definitions(self, block: Block):
"""Removes all possible symbol uses in a single block."""
prepared_symbols: set[str] = set()
while True:
self._prune_unused_reads(block)
# Find all symbols that are still in use in this block.
symbol_worklist: set[str] = {
symbol
for symbol in get_symbols(block)
if symbol not in prepared_symbols
}
if len(symbol_worklist) == 0:
return
for symbol in symbol_worklist:
reads = [
op
for op in block.ops
if isinstance(op, symref.FetchOp) and get_symbol(op) == symbol
]
writes = [
op
for op in block.ops
if isinstance(op, symref.UpdateOp) and get_symbol(op) == symbol
]
assert len(reads) > 0 or len(writes) > 0
# There are no reads, so we can only keep the last write to the
# symbol.
if len(reads) == 0:
for write in writes[:-1]:
Rewriter.erase_op(write)
prepared_symbols.add(symbol)
continue
# There are no writes, so we can replace all reads with this
# symbol.
if len(writes) == 0:
for read in reads[1:]:
Rewriter.replace_op(read, [], [reads[0].results[0]])
prepared_symbols.add(symbol)
continue
# sets of reads and writes are disjoint.
last_read_idx = block.get_operation_index(reads[-1])
first_write_idx = block.get_operation_index(writes[0])
if last_read_idx < first_write_idx:
for read in reads[1:]:
Rewriter.replace_op(read, [], [reads[0].results[0]])
for write in writes[:-1]:
Rewriter.erase_op(write)
prepared_symbols.add(symbol)
continue
# Otherwise, replace reads with the closest preceding write.
for read in reads:
write = lower_positional_bound(writes, read)
if write is not None:
Rewriter.replace_op(read, [], [write.operands[0]])
|