modular / modular/modular

[Feature Request] [mojo-lang] Non-destructive comptime for unrolling

Open
#7,052 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug_feature_triaged compile-time enhancement mojo Team: Mojo Compiler
Dominant language
Mojo
Stars
29.8k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

Review Mojo's priorities
What is your request?

Currently elaborating a comptime for clones the entire loop body for each iteration (including nested parameter scope graphs), binds the induction variable on that clone, and elaborates by consuming the clone. Because elaboration is destructive, each iteration needs its own disposable IR copy.

I suspect a big share of the cost for large loops is this clone then consume model, not just the work that actually depends on the induction variable. Nested comptime for makes that cost multiplicative.

It would be good to explore a model where the loop body can be treated more like a reusable template, by binding parameters per iteration and elaborating without requiring a full body clone for every trip. But I don't know what shape that could/should take and how it could/should be wired into the existing infrastructure.

What is your motivation for this change?

comptime for is a frequent source of exploding compile times. Making it cheaper would help a lot of generic code.

Any other details?

CC: @dgurchenkov

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the existing comptime for elaboration path described in the issue, especially where the loop body is cloned, induction variables are bound, and the clone is consumed. Determine whether a reusable template model fits the existing infrastructure; done means a concrete design and implementation that avoids full per-iteration body clones while preserving nested-loop behavior and improving compile-time cost.

Written by the indexing model from the issue text.

Assessment

Domain
compilers, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.