Move loop work without changing behavior - Internet Explorer
Address
https://freetolearn.world/compiler-optimizer-reading-07.html
free to learn.world
← Compiler Optimizer

Static school reading · Terminal implementation

Move loop work without changing behavior

Read here, then implement your own optimizer in the learner project. This page does not execute code, save answers or award results. Use the terminal tools to preserve first attempts, source versions, help and revisions.

The named U08/U09 reduction defect has passed an independent focused public-interface recheck. The owner package’s broader pending-review statements describe its original scope; the complete documented terminal route has since been exercised in a source-aware owner run. Browser and human learning are not claimed.

Get the learner project on GitHub
Setup, nine lessons and contracts

Move loop work without changing behavior

A computation repeated inside a loop may be moved once to its preheader. Implement loops(program, contract) so the move remains valid for zero-trip loops and aliased memory.

Prerequisites: dominance, conditional constants and alias/effect reasoning.

Find a supported natural loop

A backedge from latch L to header H has H dominating L. Starting from L, walk predecessors backward until H to collect the loop's blocks. The required profile has one latch and one outside predecessor serving as preheader. Exits are dedicated to the loop. Unsupported shapes should remain unchanged.

A value is invariant when its operands are available outside the loop or are values you have already moved in a valid order. Moving an instruction changes availability, so revisit dependent instructions.

For total scalar arithmetic, executing it earlier cannot add a trap or event. It can change work on a zero-trip input. That is a profitability issue, addressed separately in U07.

Commit item 06a.

A load needs more than an invariant pointer

A pointer can stay fixed while a store changes its contents. A load can also be skipped by the original program and trap if executed speculatively.

Our required load-motion profile is narrow: the frozen contract establishes a fixed, in-bounds address, and no loop store may alias it. The contract is checked on every input. A single observed nonempty array is not a proof for future arrays.

The editor records the move and checks the loop, availability, bounds and write premises. Your analysis chooses which operation to propose. It does not gain permission to invent a stronger contract.

Commit item 06b.

Project B: optimize loop kernels

node cli.mjs check 06 work --help-used none
node cli.mjs example loop loop.json
node cli.mjs optimize work loop.json loops --help-used none

Inspect three outcomes: a scalar move, a safe load move under explicit preconditions, and refusal of a load that may see an aliasing write. Include a zero-trip invocation with an empty object when no nonempty contract exists.

A valid implementation need not optimize every loop. It must recognize the stated supported opportunity and preserve behavior on the refusal controls. General symbolic range analysis, unrolling and vectorization are outside this core.

Return to setup and delivery limits

Course overviewInternet
Mission.txt - Notepad

Learning without paywalls.

Find what people pay to learn.
Build a great course. Give it away.

One course at a time.

Usage & privacy

Start