Mica 7.7.9 shows the optimizer at work and the heap as its owners hold it. An export at O1 or O2 lists every routine after each step of the optimizer that changed it, and at O2 it states how each routine’s registers were chosen. The Mica extension for VS Code plays the steps as a film strip and draws the graph the register allocator coloured. A program built at the debug tier answers at a stop who owns each cell of its heap, the editor draws the heap as the forest its owners make, and gdb and the editor read the variables of a task that waits.

The generated code is shorter again on both processors. On AArch64 a local is read or written in one instruction anywhere in its frame, and a call moves the stack pointer nowhere. On AArch64 and x86-64 a routine whose only calls are its checks’ failures keeps no frame, a loop reads an array’s length once, and a list walk tests its cursor once a round. A loop of calls that drop a function’s result no longer runs out of stack, and the export of a large program grows with the program instead of its square.

The optimizer, step by step

{
    ListWalk - two walks over a list, one that reads its cursor before it moves it and one that moves it first.

    'Walk' reads its cursor twice ahead of the write that moves it, so at O1 and O2 the loop's own test answers both nil
    checks and the round is the loads, the compare and the branch. 'MoveFirst' moves the cursor before it reads it, so the
    read behind the write keeps its nil check, whose failure is the routine's only call.
}
library ListWalk;

const
    { The sum before a walk's first round. }
    Nothing = 0;

type
    { One cell of the list a walk follows through its 'left' links. }
    Node = record
        left : pointer Node;
        amount : int64;
    end;

{ The sum of the amounts along the list, read before the cursor moves. }
exp function Walk(n : pointer Node) : int64;
var
    sum : int64;
begin
    sum := Nothing;

    while n # nil do
    begin
        sum := sum + n.amount;
        n := n.left;
    end;

    Walk := sum;
end;

{ The sum of the amounts behind each cell, read after the cursor moves. }
exp function MoveFirst(n : pointer Node) : int64;
var
    sum : int64;
begin
    sum := Nothing;

    while n # nil do
    begin
        n := n.left;
        sum := sum + n.amount;
    end;

    MoveFirst := sum;
end;

begin
end.
mica --compile --optimize O2 --platform linux,arm64,utf-8 --source ListWalk.mica --build build --export

--export at O1 and O2 writes a new stage, 095.passes, between 090.ssa and 100.out-of-ssa, with its JSON twin. It names every step the optimizer took over the unit in order, each with what it counted under the labels of 130.optimization. In build/export/ListWalk.095.passes two of the twenty-five steps read:

Step 6 of 25, nil-checks-plant: nil-check planting
  Nil checks planted: 8
Step 7 of 25, nil-checks-dedup: redundant nil-check erasure
  Redundant nil checks erased: 7

Then the stage lists each routine as the form was built and again after every step that changed it. A step that changed nothing in a routine is counted and leaves no frame. Walk has six frames, each a heading with the step’s counts beneath it and then the routine whole. Its headings, the rest left out:

Routine 'ListWalk.Walk': 5 of 25 steps changed it

Frame 0 of 5: the form as built, before any pass
Frame 1 of 5: after step 6 of 25, nil-checks-plant: nil-check planting
Frame 2 of 5: after step 7 of 25, nil-checks-dedup: redundant nil-check erasure
Frame 3 of 5: after step 21 of 25, leave-form: leaving static-single-assignment form
Frame 4 of 5: after step 23 of 25, rotate-loops: loop rotation
Frame 5 of 5: after step 24 of 25, jump-elimination: redundant jump elimination

Frame 1 holds the nil checks the planting gave each read through n, and frame 2 the routine once the loop’s own test answered them. A routine whose listing passes 2,000 lines, and the frames past 100,000 lines in one unit, are named with their size instead of listed. A debug export writes no strip, and a build without --export does no work for it: its assembly is the same to the byte.

In the editor, Show Optimizer Film Strip shows those frames for one routine as a row: choose a frame, and the lines its step added, removed, changed or moved are marked by a sign, words and colour. Step forward and back, read a frame beside the one before it or against the form as built, and click a line to open its source line. The strip opens from the export overview’s film strip link, the editor’s title bar and context menu on any export stage, or the Command Palette. In the picture, the strip of a routine that counts the steps of a Collatz sequence, with the frame of the first step that changed it chosen: a load the step removed is struck through, and the line that read it stands changed, with the line it replaced beneath.

The optimizer’s film strip of a routine: a row of frames, each named by its step and pass with what it counted, and beneath it the chosen frame’s listing with a removed line struck through and a changed line above the line it replaced

Who gets a register, and why

At O2 the placement export, 110.placement and its JSON twin, states how each routine’s registers were chosen. Under function ListWalk.Walk it names the allocator and its graph:

    allocation: graph colouring of 12 values, at most 512, lines of a class stated up to 1000

It lists every value with its weight, then the lines between values alive at the same time, the copies, and the order in which the colourer took the values off the graph and handed the registers out:

      line t2.3 -- t2.4: both alive [19, 20)
      line t2.3 -- v2.1: both alive [18, 20)
      line t2.4 -- v2.1: both alive [19, 21)
      line t2.6 -- v2.1: both alive [11, 13)
      lines: 4 of 4 stated
      web t2.9 of [t2.9 v2.2]: homed ahead in x13
      remove t2.3: degree 2, 12 colours, cost 20, trivially colourable
      remove t2.4: degree 1, 12 colours, cost 20, trivially colourable
      remove t2.6: degree 1, 12 colours, cost 20, trivially colourable
      remove v2.1: degree 0, 12 colours, cost 22, trivially colourable
      select v2.1: register x16, neighbours hold []
      select t2.6: register x17, neighbours hold [x16]

v2.1 is sum. It has three neighbours and twelve registers to choose from; once its neighbours have left the graph it leaves with none, and it is the first to get a register back, x16, the register the loop below adds into. A copy merged into one value carries the counts of Briggs’s test that let it merge or kept it apart, and a value left in the stack frame names its slot and what a register would have saved. A web of copies placed ahead of the colouring is named once with its values and its register. A routine with more than 512 register candidates, which O2 leaves to O1’s allocator, says so in its entry, with its count and the 512 the graph colourer takes.

Show Interference Graph draws that graph across the panel: a dot for each value, a line between two values alive at the same time, each dot in its register’s colour with the register’s name inside, and the values left in the stack frame dashed. Walk the colourer’s steps takes the dots off one at a time, each with the sentence that says why it could leave, then hands the registers out in the reverse order, each with the registers its neighbours already hold. A routine without a graph says why: built at the debug tier, placed by the linear scan, or inlined into the routines whose graphs it links. In the picture, the walk through the main body of a Collatz program on AArch64 stands at the step where n comes back to the graph: the one neighbour that already has a register, count, holds x16, so n takes x17, and the dots still off the graph are dotted.

The interference graph of a routine mid-walk: dots on a circle, each naming its value and the register it got, lines between values alive at the same time, the step’s dot outlined heavy with the sentence above that says why it takes its register

A list walk tests its cursor once a round

On AArch64 at O2, Walk from above is ten instructions. The loop’s own test, n # nil, answers the nil check of both reads through the cursor that come before the cursor moves, so the round is the two loads, the addition, the compare and the branch:

  mov         x13, x0
  movz        x16, #0
  b           .Ll2.2
.Ll2.2.rotated:
  ldr         x10, [x13, #8]
  add         x16, x16, x10
  ldr         x13, [x13]
.Ll2.2:
  cmp         x13, #0
  b.ne        .Ll2.2.rotated
.Ll2.3:
  mov         x0, x16
.LListWalk.Walk_leave:
  ret

7.7.8 wrote twenty-one instructions: a frame, a copy of sum and a second test of the cursor in every round, and a cold path for that test’s failure, which was the routine’s one call:

  stp         x29, x30, [sp, #-16]!
  mov         x29, sp
  sub         sp, sp, #16
  mov         x13, x0
  movz        x16, #0
  b           .Ll2.2
.Ll2.2.rotated:
  mov         x9, x16
  cmp         x13, #0
  b.eq        .Lguard.cold.1
  ldr         x10, [x13, #8]
  add         x16, x9, x10
  ldr         x13, [x13]
.Ll2.2:
  cmp         x13, #0
  b.ne        .Ll2.2.rotated
.Ll2.3:
  mov         x0, x16
.LListWalk.Walk_leave:
  mov         sp, x29
  ldp         x29, x30, [sp], #16
  ret
.Lguard.cold.1:
  movz        x1, #56
  movk        x1, #1, lsl #32
  bl          .Lrt_runtime_failure_map_reason_nil_dereference

The same build for linux,amd64,utf-8 writes Walk in seventeen instructions on x86-64, where 7.7.8 wrote twenty-one. The test also answers a read behind a round that moves the cursor and ends at next, both cursors of while (a # nil) and (b # nil), and a read behind while n = nil. A read the test cannot answer keeps its check: a read behind the write that moves the cursor, as in MoveFirst, the first round of a repeat loop, a cursor a nested routine moves, or a pointer held in a record field. A nil read there still stops the program with the nil-dereference failure.

Checks that keep no frame

MoveFirst reads through the cursor after it moved, so that read keeps its nil check, and the check’s failure is the routine’s only call. At O1 and O2 such a routine runs without a frame: its good path is the work and the return, as GCC and clang write it, and the cold path that reports the failure sets up its own frame record. On AArch64:

  mov         x13, x0
  movz        x16, #0
  b           .Ll3.2
.Ll3.2.rotated:
  ldr         x13, [x13]
  cmp         x13, #0
  b.eq        .Lguard.cold.1
  ldr         x10, [x13, #8]
  add         x16, x16, x10
.Ll3.2:
  cmp         x13, #0
  b.ne        .Ll3.2.rotated
.Ll3.3:
  mov         x0, x16
.LListWalk.MoveFirst_leave:
  ret
.Lguard.cold.1:
  stp         x29, x30, [sp, #-16]!
  mov         x29, sp
  movz        x1, #112
  movk        x1, #1, lsl #32
  bl          .Lrt_runtime_failure_map_reason_nil_dereference

That is seventeen instructions, where 7.7.8 wrote twenty-five with a frame and two checks a round. x86-64 does the same now: a checked Bumped := datum + 1 is mov rax, rdi; add rax, 1; jo and a ret, with the overflow report off the good path. A trap still names its file, line and column, and a debugger’s or a profiler’s backtrace from the failure still shows the routine and its caller. Several checks of one statement that report the same place share one failure path on both processors, so chain.next.target.amount carries one for its three nil checks. Over the 11,765 AArch64 builds of the compiler’s test programs, 8,646 routines are shorter and 35 longer, 52,828 instructions fewer. Each of the 35 has three or more failure paths of its own, and its good path is still five instructions shorter.

Every local of an AArch64 frame in one instruction

On AArch64 a local variable anywhere in a frame of up to 32 KiB is read or written in one instruction, as GCC and LLVM write it: a local past the frame pointer’s short reach is addressed from the stack pointer, where an address was built first. A routine’s frame holds the area its calls pass stack arguments and hidden results in, so a call moves the stack pointer nowhere, and the listing names that area’s size at the start of each routine that has one, # outgoing argument area 32 bytes, as GCC’s ARM listings state their frames. At O1 and O2 the callee-saved registers a routine keeps are saved and restored in pairs again, one stp and one ldp for two neighbours.

Across the compiler’s test programs an AArch64 debug build writes 21.6 % fewer instructions and an optimized build 4.3 % to 5.1 % fewer, by tier, and no routine is longer. At O2 the standard library’s BigintArithmetic kernel runs 7.20 % fewer instructions and its StringBuild kernel 1.11 % fewer. Variables keep their places in the frame, so a debugger shows every local where it showed it, and backtraces are unchanged.

A loop reads an array’s length once

At O2 on both processors a loop over a string or an array reads its length and its data pointer once, ahead of the loop, where the loop does not change them, and several reads of one word in a round are one read. A loop that appends to the array, assigns the string, hands its address to a routine, shares it with a nested routine, a task or a generator, or handles a failure that changes it still reads both as they are in each round. Appending to an array in a loop runs 27.6 % fewer instructions on AArch64 and 10.0 % fewer on x86-64, and sorting an array 4.2 % and 1.5 % fewer, measured on the standard library’s ArrayAppend and ArraySort kernels at O2.

Code closer to GCC -O2

On x86-64 at O1 and O2 two int32 values are compared in their 32-bit registers, as GCC does, and an 8-bit or 16-bit value is widened straight from the register it arrives in or was computed in, so a narrow parameter that is compared, summed or divided needs no copy of its own. A value is stored from the register that holds it, a store that a later store to the same place overwrites goes where nothing between the two can read it, and no value is kept alive in an argument register for a runtime check that never fails in a correct program, so the code ahead of a range or a zero check is as short as the code without one. Over the compiler’s test programs built for x86-64, the comparisons and the stores make 3,071 routines shorter by 9,800 instructions, and the narrow values and the checks, measured on top of them, 2,085 routines shorter by 6,481. The stores a later store overwrites, with the zero test and the folded return copy beside them, take 96,779 instructions more away, most of them in the main routines of three large generated programs. No routine is longer.

On x86-64 a multiplication by a literal reads its factor in the register it lives in, imul r10, rdi, 3, a 32-bit value is tested for zero or for its sign in its own register, test r11d, r11d, and a choice whose first arm keeps no instruction takes one conditional jump where it took a jump over a jump. A value stays in place through a compare or a test, so a routine that tests a pointer for nil and reads behind it needs no frame, and a local whose address only its own routine takes to read and write it back keeps no store nothing reads. A repeat ... until count = 0 loop jumps on its subtraction’s own flags, and mov r10, 0; add r10, 10 is mov r10, 10. A store into a frame slot that a later store overwrites before anything can read it goes also where a pointer to the slot exists, so the zero a routine’s head writes into a local it then assigns is gone, and a routine that answers one of two values with a conditional move writes the choice straight into the return register, cmovne rax, r8.

On AArch64 an access a constant distance from a register names the distance itself, ldr x9, [x29, #-16], where an add or sub formed the address first. A local whose address is taken is treated like any other memory, so a value stored there and read back before anything else can reach it is the register it was stored from, and where only its own routine takes the address the stores nothing reads are gone, with the frame if nothing else needs it. A loop’s counter and the value its increment computes share one register in more loops at O2, add x14, x14, #1 where add x14, x13, #1; mov x13, x14 stood, and value := value * 1024 in a loop is lsl x13, x13, #10 where it was a movz and a mul.

On both processors a copy at the head of a block that a branch jumps to is read through to its source, so a function that kept a callee-saved register only for such a copy no longer saves it. A test of a sum’s, a difference’s or a negation’s sign against zero reads the arithmetic’s own flags: x - y < 0 branches on the subtraction’s sign, wrapped or not, and computes exactly what it computed before.

The heap at a stop, as the forest its owners make

A program built at the debug tier answers at a stop who owns each cell of its heap. Two debugger entries of the runtime answer a routine’s roots, its variables, parameters and result whose types own something, and what one root holds, cell by cell through the owning slots, each cell with its type, its bytes and the line that made it. Draw the Heap Forest, in the Mica Runtime view’s title bar, draws the answer: each root a variable named with its routine, each cell a box, each arrow named by the slot that owns the cell beneath. For a program that built a binary tree, the drawing is that tree, and two variables that name one cell are two arrows to one box. What no root reaches stands apart and says so, a region’s cell under a dashed box naming who holds the region and cells made by new as a count with their line. In the picture, such a tree at a stop: root, a variable of the main body, over its fifteen nodes, each with its type, its 40 bytes and the line that made it.

The panel Draw the Heap Forest for a program that built a binary tree: the root, a variable of main, with a heavy outline, and under it the fifteen nodes of the tree, each a box with its type, its bytes and the line that made it, every arrow named left or right

Draw What the Program Holds draws a stopped or running program’s regions large in a panel: the regions grouped by who holds them, every block a strip of its cells coloured by type or by the line that made them, a fixed arena whole with its fill and the most it held at once. The runtime’s regions reading states the heap’s high-water mark, "heap":{"used":N,"peak":N}, and a program built at the debug tier counts its heap from its start, so the panel always states the most the heap held. A release-tier program counts when it is started with +mica heap -mica, as before.

A hello-world program built at the debug tier carries 8,641 bytes of code for the two readings on AArch64 and 9,113 on x86-64, and small tables of its own. The heap’s count costs an allocation and its release together 75 instructions on AArch64 and 53 on x86-64. No cell grows, and a released program carries nothing of the readings.

A waiting task’s variables

At a stop, gdb reads the variables of a task that waits. mica-task-locals 1 prints the frames of task 1, ready, parked or suspended, a stackful generator between two emits among them, with each frame’s arguments and locals beneath it and the variables of the routines around it, as info locals prints them. mica-task-locals 1 3 prints frame 3 alone, and $mica_task_frames(1) answers the same variables as JSON, with each variable’s type and address. The values are read, never written. A task running on another thread is refused with that thread named, and a build without variable locations prints the frames and says the variables need a debug build.

In the editor each Mica frame of a waiting task shows its arguments and locals in the Variables view, beside the variables of the routines around it under Enclosing routines, read only, and the heap forest draws a waiting task’s variables as roots over the cells it holds. At a stop in a multicore program the task tree lists every task, a task on another carrier as the thread it runs on, and says so where it could not read every carrier.

A large export in seconds

--export of a large program at an optimizing tier grows with the program, where its time and memory grew with the square of the program: the export of a hundred-thousand-line program at O2 ran for minutes and held tens of gigabytes, and it takes 24.1 seconds and 1.7 GiB on an AMD Ryzen Threadripper 3990X and 13.8 seconds and 2.1 GiB on an Arm Neoverse-V2. Each JSON twin is written into its file while it is encoded, so the same program built for x86-64 and exported at the debug tier needs about a third of the memory it needed: 3,417 and 3,513 MiB at its peak in two runs on an AMD Ryzen 9 3900X, where it needed 11,421 and 10,673 MiB. Every file the export writes is the same, byte for byte. The platform page states what building and exporting units of three sizes takes on both processors.

A loop of calls that drop their results keeps its stack

A loop that called a function for its effect, its result not used, with more arguments than the processor’s argument registers hold, ended in a stack overflow after some hundred thousand rounds: each such call left the stack area its remaining arguments travelled in standing. A formatted WriteLn with many values was such a call, and so was a dropped call to a function that returns a large record, a dropped call that can fail, and any of them inside a task or a generator, whose smaller stacks overflowed after a few thousand rounds. Every call now gives its area back as it returns, at every optimization level and on both processors: a formatted WriteLn with ten values runs a million times to its end, where 7.7.8 stopped after 174,607 rounds on x86-64 and 261,872 on AArch64.

Fixed

  • A running program answers every memory reading of the runtime protocol while its regions come and go, where a reading taken while a region holding a value of more than 64 kilobytes was being given back ended the program by SIGSEGV.
  • A program stopped through the runtime protocol stands still with all its carriers: a carrier the pool started during the stop waits until the client lets the program go.
  • mica-task-backtrace run after up leaves the program exactly as it found it, where it wrote into the stopped program’s stack.
  • Tasks that waited together run in the order they began to wait, where three sleepers woken together ran last first, on the single-tasking flavor and on one carrier alike.
  • Built at O2 with profiling, a breakpoint on an inlined routine’s line stops in the routine’s frame, and a failure inside a generic routine from a library names the library’s file, line and column.
  • A checked program that stops on a check inside a heapless routine always ends with its one failure report and status 1, where it could wait forever after the report.
  • On x86-64 a checked floating-point sum whose result is a NaN reports floating_arithmetic_invalid at O1 and O2, as the debug tier and AArch64 do.
  • A routine that adds to the same variable hundreds of times in a row compiles for x86-64 at O1 and O2, where from 435 such statements on it stopped with internal error 23103.
  • A file in an --export folder is whole or absent when the compiler fails while writing it.

Upgrading

Things a 7.7.8 build meets differently in 7.7.9:

  • A var written directly behind a statement is refused with expected semicolon, found variable. Behind a loop’s do or a branch’s then or else such a source compiled into a loop or a branch with an empty body, and the compound behind the var ran once, after it.
  • In a build with profiling, a breakpoint on the line of an inlined call stops at the first instruction of the inlined body, before the call has run, where it landed on the next line.
  • Tasks woken together by a timer, a cancellation or a stream’s end run first, second, third, where they ran last first. On several carriers the order is still not a promise.
  • On x86-64 at O1 and O2 a checked sum that makes a NaN stops with floating_arithmetic_invalid, where it said floating_arithmetic_overflow.
  • The film strip, the interference graph, the heap forest and a waiting task’s variables in the editor need the Mica extension 5.10.0 and a program or an export made by Mica 7.7.9.

The packages and the commands that install them are on the install page.