Skip to content

looplets

SequencePass

Bases: LoopletPass

Lowers one Sequence looplet at the time.

Source code in src/finch/compile/looplets.py
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
class SequencePass(LoopletPass):
    """
    Lowers one Sequence looplet at the time.
    """

    @property
    def priority(self):
        return 4

    @classmethod
    def get_sequence_variations(
        cls, ctx, seqs: list[Sequence], heads, tails, ext: SymbolicExtent
    ) -> list[tuple]:
        ext_start, ext_end = ext.get_start(), ext.get_end()

        match seqs:
            case []:
                return [(heads, tails, ext_start, ext_end)]
            case [Sequence() as seq, *tail]:
                split = seq.split(ctx, ext)
                left = cls.get_sequence_variations(
                    ctx,
                    tail,
                    heads + [seq],
                    tails,
                    SymbolicExtent(
                        ext_start,
                        ntn.Call(ntn.L(ffuncs.min), (ext_end, split)),
                    ),
                )
                right = cls.get_sequence_variations(
                    ctx,
                    tail,
                    heads,
                    tails + [seq],
                    SymbolicExtent(
                        ntn.Call(ntn.L(ffuncs.max), (ext_start, split)),
                        ext_end,
                    ),
                )
                return left + right
            case other:
                raise Exception(f"Invalid sequence: {other}")

    def __call__(self, ctx: LoopletContext, idx, ext: SymbolicExtent, body):
        found_seqs: list[Sequence] = []

        for node in PostOrderDFS(body):
            match node:
                case ntn.Looplet(Sequence() as seq, _):
                    found_seqs.append(seq)

        found_seqs = list({id(seq): seq for seq in found_seqs}.values())

        def sequence_node(
            node: ntn.NotationNode, heads: set[Sequence], tails: set[Sequence]
        ):
            match node:
                case ntn.Access(
                    ntn.Looplet(Sequence() as tns, type_), mode, (j, *idxs)
                ) if j == idx:
                    if tns in heads:
                        new_tns = tns.head(ctx, idx)  # type: ignore[call-arg]
                    elif tns in tails:
                        new_tns = tns.tail(ctx, idx)
                    else:
                        raise Exception(f"Seq: {tns} not present.")
                    return ntn.Access(ntn.Looplet(new_tns, type_), mode, (j, *idxs))

        variations = self.get_sequence_variations(ctx, found_seqs, [], [], ext)
        blocks = []

        for v in variations:
            heads, tails, subext_start, subext_end = v
            sub_body = PostWalk(partial(sequence_node, heads=heads, tails=tails))(body)
            subext = SymbolicExtent(subext_start, subext_end)
            sub_ctx = ctx.scope()
            sub_ctx(subext, self.looplet_simplify(sub_body))
            blocks += sub_ctx.emit()

        ctx.exec(asm.Block(tuple(blocks)))