A useful scalar optimizer - Internet Explorer
Address
https://freetolearn.world/compiler-optimizer-reading-04.html
free to learn.world
← Compiler Optimizer

Static school reading · Terminal implementation

A useful scalar optimizer

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

A useful scalar optimizer

The calibration kernel computes the same multiplication twice and carries unnecessary constant work. Implement scalar(program, contract) to reduce its executed work while emitting a checked edit chain.

Prerequisites: U01–U02. Use the supplied Editor; return editor.finish(). You choose opportunities and ordering. The editor checks the rule premises and records exact before/after identities.

Fold only under the declared arithmetic

An i8 multiplication of constants 90 and 3 produces 14: the mathematical product 270 wraps modulo 256. An i32 multiplication of the same constants produces 270. Use the supplied word primitives rather than JavaScript Number multiplication for arbitrary i32 operands.

A constant fold requires known constant operands and the exact operation/type. The result becomes a typed constant with the original result ID. Folding can make its original constants unused, but that is a separate edit.

For example, after establishing the correct result for a particular definition, an edit has this shape:

editor.edit({kind: 'fold', id: chosenID, value: canonicalValue});

This is an edit request, not permission to invent a value. Wrong values are rejected.

Reuse and remove with distinct premises

Reuse requires the same total, pure expression and an available earlier result. Addition and multiplication permit operand-order normalization; signed comparisons do not. A definition in one arm of a diamond is not available in the other arm merely because the expressions match.

Dead-result removal requires no remaining use, including return, branch, phi and effect operands, and a total pure instruction. An unused load can still trap. Store and emit are observable even without result IDs.

Commit items 03a and 03b before looking at their feedback. Implement folding, safe reuse and dead-result cleanup. Revisit opportunities after an edit: folding can expose dead constants, and reuse can make a definition unnecessary. Avoid unbounded loops when no edit succeeds.

Project A: run your optimizer

node cli.mjs check 03 work --help-used none
node cli.mjs example scalar calibration.json
node cli.mjs optimize work calibration.json scalar --help-used none

The report compares every listed input, checks the whole edit chain and reports unit-cost IR work. The output program is adopted only when the required evidence and improvement hold. A no-op implementation is safe but does not complete this project.

Inspect a rejected edit, repair its premise and rerun. Then try your pass on a kernel with a trap-bearing load. Saving one multiplication never justifies changing whether a trap occurs.

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