Skip to content

IR: Lowering and Optimizations

Codes that start with A2C:IR: come from the compiler's middle: where your Lua becomes code that calls the host's Lua API, and where most optimizations are decided. Besides errors and warnings, every optimization has a code of its own (O), and so has every reason the compiler can give for not applying one (M). A missed optimization says what stood in the way, and its section here says what you can change.

Numbers What
0001 to 0099 What the compiler can't compile (errors)
0100 to 0199 Directives and coroutines (warnings)
1000 to 1999 Numbers
2000 to 2999 Tables
3000 to 3999 Calls and functions
4000 to 4999 Loops and conditions
5000 to 5999 Library functions
6000 to 6999 Stack frames
7000 to 7999 Moving values to and from the host
8000 to 8999 Clean-up of the compiled code
9000 to 9099 Internal errors

Some optimizations apply everywhere, the same way, with nothing to decide. They're marked always on below.

Errors and Warnings

A2C:IR:E:0001 unsupported

The compiler can't compile a construct. This is a bug in autoplay2c: please report it, with the line the message names.

A2C:IR:W:0100 directive-arguments

A directive that takes no arguments has words after its name. They're ignored.

--#cache now                 -- warning A2C:IR:W:0100: `--#cache` takes no arguments; they are ignored

A2C:IR:W:0101 inline-no-effect

--#inline is on a statement that doesn't define a function. It has no effect.

A2C:IR:W:0102 inline-defined-twice

A global function marked --#inline is defined more than once with the directive, so the compiler can't tell which body its calls mean. None of its calls are inlined.

A2C:IR:W:0103 inline-assigned

The name of a function marked --#inline is assigned another value, which breaks the directive's promise: inlined calls keep running the body they copied. For a global name, the compiler warns when the name, or a table it is in, is assigned in two places (the definition is one of them) or in a loop; for a local, when it is assigned at all.

--#inline                            -- warning A2C:IR:W:0103, given here
function Twice(x) return x + x; end

Twice = function(x) return 0; end    -- breaks the promise
tools.lua:1:1: warning A2C:IR:W:0103: `Twice` is assigned again on line 4: its inlined calls keep running the body defined here

Remove the directive, or the assignment.

A2C:IR:W:0104 cached-path-assigned

A name that --#cache caches, such as Log or Config.Path, or a table it is in, is assigned in two places or in a loop. That breaks the directive's promise: cached lookups keep the value they found first, while the assignment gives the name another one.

function Log(text) Debug.Print(text); end

--#cache
function Save() Log("saving"); end

function Log(text) TextFile.WriteFromString("log.txt", text, true); end -- warning A2C:IR:W:0104

One assignment is fine, wherever it is: that's usually where the name is defined. Remove the directive from the code that reads the name, or assign the name only once.

A2C:IR:W:0110 yield-from-compiled

coroutine.yield is called from compiled code. Compiled functions are C functions to the host, and a yield can't cross them: it raises "attempt to yield across metamethod/C-call boundary" (Lua 5.1) or "attempt to yield across a C-call boundary" (Lua 5.3) when it runs. See Coroutines. If the call never runs inside a coroutine, --#noyield silences this warning for its statement or function.

A2C:IR:W:0111 compiled-coroutine

A function written right in a call of coroutine.create or coroutine.wrap is compiled. In Lua 5.3, neither it nor anything it calls can yield; if they never do, --#noyield silences this warning for its statement or function. In Lua 5.1, coroutine.create and coroutine.wrap only take Lua functions and raise "Lua function expected" for a compiled one, so the warning stays. See Coroutines.

Numbers

A2C:IR:O:1001 numeric-local

A local that's only ever given numbers lives in a native variable, and arithmetic on it is native. See Numeric Locals.

A2C:IR:O:1002 tagged-local

Lua 5.3: a local given both integers and floats is native too, with a tag that says which kind it holds. See Integers and Floats.

A2C:IR:M:1003 numeric-local-mixed

A local isn't a native number because it's given a value that may not be a number: a string, a table, a call's result, a table field or a parameter. The message names the value.

local total = 0;
for i = 1, #t do
    total = total + t[i];    -- t[i] could be anything
end

Inside loops, such a local can still be a dynamic local. To make it a numeric local, give it only number expressions: keep the other value in a local of its own.

A2C:IR:M:1004 numeric-local-captured

A local isn't a native number because a function defined inside its function uses it, and it's assigned after that. When those functions only assign numbers to it, it can still be a native cell.

A2C:IR:M:1005 numeric-local-nil-read

A local declared without a value (local i;) may be read before it gets a number on some path, so its nil may be seen. Give it a number when you declare it. See Locals Declared Without a Value.

A2C:IR:M:1006 numeric-local-goto

A local declared without a value, in a function with goto. The compiler doesn't follow goto to prove the local gets a number before it's read. Give it a number when you declare it.

A2C:IR:O:1010 dynamic-local

A local used in arithmetic or comparisons inside a loop keeps its value natively whenever it's a number, and as a Lua value otherwise. See Dynamic Locals.

A2C:IR:M:1011 dynamic-local-captured

A local isn't dynamic because a function defined inside its function uses it. Copy it into a local of the loop's own, and use that in the loop.

A2C:IR:O:1020 number-parameter

A parameter that every call gives a number is a native number. See Number Parameters.

A2C:IR:M:1021 number-parameter-escapes

A parameter isn't known to be a number because its function is assigned again, or used other than by calling it by its name (passed, stored, returned), so code the compiler can't see may call it. Use a local function that's never assigned again, and only call it.

A2C:IR:M:1022 number-parameter-argument

A parameter isn't known to be a number, because a call passes it something that may not be a number, the results of another call, or nothing at all. The message names the call. If the parameter is meant to be a number, make every call pass a number expression.

A2C:IR:O:1023 native-parameter

A parameter used in arithmetic or comparisons is checked once when the function starts, and used natively whenever it's a number. See Dynamic Locals.

A2C:IR:M:1024 native-parameter-recursive

A parameter isn't read natively because its function calls itself by its name. Native variables live on the C stack, which recursion pays for at every level, so recursive functions keep their depth instead.

A2C:IR:O:1030 numeric-upvalue

A local of an enclosing function that's a native number, and never assigned again, is read once when the nested function is called, and used as a native number after that.

A2C:IR:O:1031 numeric-cell

A local that nested functions change, but only with numbers, lives in a native cell they share. See Counters in Closures.

A2C:IR:M:1032 numeric-cell-recursive

No native cell, because a function that sees the local calls itself by its name, and every level of recursion would pay for the cell.

A2C:IR:M:1033 numeric-cell-nil

No native cell, because the local is declared without a value, so a nested function may read nil. Give it a number when you declare it: local clicks = 0;.

A2C:IR:M:1034 numeric-cell-loop

No native cell, because the local is a loop's variable, which is a new local in every round.

A2C:IR:M:1035 numeric-cell-results

Reserved: autoplay2c no longer gives this code. When a script has both native cells and functions that could return native numbers, the cells win, and the functions say so with native-result-cells.

A2C:IR:O:1040 boolean-local

A local only ever given true, false, comparisons, or not, and and or of those, is a native flag. See Booleans.

A2C:IR:M:1041 boolean-local-mixed

A local isn't a native flag, because it's given a value that may not be a boolean, such as nil or a call's result. The message names the value. Give it only true, false, comparisons, or not, and and or of those.

A2C:IR:M:1042 boolean-local-used

A local isn't a native flag because it's indexed, called, used by a nested function, or declared without a value.

A2C:IR:O:1050 number-expression

An expression whose operands are all native numbers, such as x * 2 + y with numeric locals, is computed natively, without the host.

A2C:IR:O:1051 speculative-expression

An expression over values that are usually numbers checks them once, then computes natively. When one of them isn't a number, it runs the ordinary code instead, so the result is always the same. See Dynamic Locals.

A2C:IR:M:1052 speculative-order

An expression is computed the ordinary way because one of its values has to be read after an operation that may run code (a metamethod, a call), and reading it early could see a different value. Compute the parts in locals of their own first.

A2C:IR:O:1053 known-operands

Lua 5.3: both operands of an arithmetic operator or a comparison are known to be numbers, so the host computes it directly, without the checks before it.

A2C:IR:O:1054 float-leaf

Lua 5.3: a value whose kind (integer or float) can't change the result, such as an operand of /, is read with one call.

A2C:IR:O:1055 integer-operator

Lua 5.3: //, % and the bitwise operators are computed natively, with the interpreter's rounding and errors. See Lua 5.3 Integers.

A2C:IR:O:1056 speculative-bitwise

Lua 5.3: an expression with bitwise operators over values that are usually integers, such as (h ~ byte) & 0xFF with byte read from a string, checks the values once and computes natively when they're all integers. Otherwise it runs the ordinary code. See Lua 5.3 Integers.

A2C:IR:O:1057 typed-value

A value that isn't arithmetic but is always a number, such as x or 0 with x a number or nil, (a == b) and 1 or 0, #s of a string, or the call of a local function that returns numbers, is computed the ordinary way and then read as a number, without checking its type. See Values Known to Be Numbers.

A2C:IR:M:1058 typed-value-refused

A local is given a value that is always a number, but it can't be read as one where it stands. The value reads a local that a function defined inside changes, or it may run code (a call, a metamethod) in a script that has counters in closures. The interpreter reads such a local only after the rest of the expression, so the compiler leaves the expression to the ordinary code. Copy the local into a local of its own first.

A2C:IR:O:1059 narrowed-local

A local that may be nil is checked, as in if x == nil then x = 0 end or if not n then return end, and from the end of that if it can only be a number: it is a native number from there on. See Checked for nil.

A2C:IR:O:1060 native-entry

A function that takes numbers, only computes with numbers and returns one number gets a fully native version, which its calls use. See Number Parameters.

A2C:IR:M:1061 native-entry-impure

No fully native version, because the function uses Lua values somewhere: a table, a global, a call of another function that isn't native. The message names the first. It can still return its number natively (native-result).

A2C:IR:M:1062 native-entry-kind

No fully native version, because a parameter or the result isn't always the same kind of number: in Lua 5.3, sometimes an integer and sometimes a float.

A2C:IR:M:1063 native-entry-vararg

No fully native version, because the function takes ....

A2C:IR:O:1064 native-result

A function that always returns one number gets a second version that returns it natively, which calls by its name use. See Number Parameters.

A2C:IR:M:1065 native-result-closures

No native result, because the function defines functions.

A2C:IR:M:1066 native-result-kind

No native result, because some return gives something other than one number, or numbers of both kinds.

A2C:IR:M:1067 native-result-cells

No native result, because the script has captured locals that live in native cells (numeric-cell). A call of a function returning a native number could change such a cell in the middle of an expression that reads it, so the compiler keeps the cells and gives up the native results. The detail says how many locals live in cells.

A2C:IR:O:1070 declared-dynamics

local a, b = x, y stores each value as it's computed, natively where it can.

Tables

A2C:IR:O:2001 native-array

A table only its function sees, indexed only by numbers, holds its elements in native memory. See Native Arrays.

A2C:IR:O:2002 shared-native-array

The same, shared with the nested functions that use it.

A2C:IR:M:2003 native-array-escapes

A table isn't a native array because it's used other than by indexing it: passed to a function, returned, stored in another table, compared, measured with #, given a metatable or iterated. Keep the table inside its function, and only read and write its elements.

A2C:IR:M:2004 native-array-key

A table isn't a native array, because it's indexed by a name (t.x), or stored into with a key that isn't a number. The message names the first such use. Store into it with number keys; reads may use any key.

A2C:IR:M:2005 native-array-declaration

A table isn't a native array, because it isn't made with a constructor of its own holding only numbers, booleans and nil ({} or { 1, 2, 3 }), or the local is assigned again. Declare it as local t = { 1, 2, 3 }; on its own, without names or calls in the constructor.

A2C:IR:M:2006 native-array-goto

A table isn't a native array because its function, or a nested function that uses it, has goto.

A2C:IR:O:2007 filled-native-array

A native array declared with numbers, booleans and nil, such as local k = { 0x428a2f98, 0x71374491 }, starts with them in native memory. See Native Arrays.

A2C:IR:O:2008 computed-array-key

A native array is read with a key that isn't a plain number, such as crc[(c ~ b) & 0xFF] or a string. The key is computed once, natively where it can be; a key that isn't a number finds nil, as it would in the table. See Native Arrays.

A2C:IR:O:2010 native-record

Tables made with fixed field names, which only your compiled code reads and writes by those names, are records in native memory. See Native Records.

A2C:IR:M:2011 native-record-escapes

Tables made in a function can become native records, a fixed set of fields without a Lua table behind them, but only while no code could see them as tables. These stay tables, because one of them is used as a table: passed to a call, compared, used as a condition, given to pairs, stored into a table or a global, used in a method call or by a nested function. The message names the first such use. Records are all or nothing, so that one use keeps every table it could be mixed up with as a table.

A2C:IR:M:2012 native-record-keys

Tables aren't records because they're indexed with a key that isn't a fixed name, or have more than 64 names.

A2C:IR:M:2013 native-record-function

Tables aren't records because the function that makes or returns them is assigned again or used other than by calling it by its name.

A2C:IR:O:2020 known-table

A local holding a table made with a table constructor, such as {} or { x = 1 }, and never assigned again, is indexed without checking that it's a table. See Known Tables.

A2C:IR:O:2021 flagged-table

A local that's indexed often remembers whether it holds a table. While it does, t.x and t[i] skip the check that t is a table.

A2C:IR:M:2022 flagged-table-captured

A local that's indexed often doesn't remember whether it holds a table, because a nested function uses it, or it's a native number or array. Each time it's indexed, it's checked; that costs little.

A2C:IR:O:2023 table-result

A local given the result of a function that always returns a new table, such as local p = NewPoint(1, 2) with NewPoint a local function that only returns constructors, is known to be a table and indexed without the check. See Known Tables.

A2C:IR:O:2030 number-key

A number used as a key, as in t[1] or t[i], goes to the host as a number, without being pushed onto the Lua stack first. See Number Keys.

A2C:IR:O:2031 native-length

#t of a local that's known to hold a table is computed without checking that it's a table. See Lengths.

A2C:IR:O:2032 append

t[#t + 1] = v takes one length, one native addition and one store.

A2C:IR:O:2033 field-chain

Lua 5.3: in a.b.c, each table is indexed by the type the host said it has. See Chains in Lua 5.3.

A2C:IR:O:2040 held-field

A field read again, or read after it was written, reuses the number already in hand. See Fields Read Again.

A2C:IR:O:2050 pushed-field-keys

Setup Factory 9.7 x64: fixed field names are passed with their length, which that host reads faster.

A2C:IR:O:2060 constant-key

Always on. t.name and t["name"] look the field up by its name directly. See Fixed Keys.

Calls and Functions

A2C:IR:O:3001 inlined

A call of a function marked --#inline is replaced by the function's body.

A2C:IR:W:3002 not-inlined-vararg

A function marked --#inline isn't inlined, because it takes ....

--#inline                    -- warning A2C:IR:W:3002: `f` is not inlined: it takes `...`
local function f(...) return ...; end

A2C:IR:W:3003 not-inlined-recursive

A function marked --#inline isn't inlined, because it calls itself.

A2C:IR:W:3004 not-inlined-upvalues

A function marked --#inline isn't inlined, because it uses local variables of an enclosing function.

A2C:IR:W:3005 not-inlined-closures

A function marked --#inline isn't inlined, because it defines functions.

A2C:IR:W:3006 not-inlined-size

A function marked --#inline isn't inlined, because it's too large.

A2C:IR:W:3007 not-inlined-name

A function marked --#inline isn't inlined, because it isn't defined under a global name, a path of global tables, or as a local.

A2C:IR:O:3008 inlined-expression

A call of an --#inline function whose body is one return of an expression is computed as part of the arithmetic around it, with its parameters kept native. See Functions of One Expression.

A2C:IR:O:3010 unboxed-capture

A local that nested functions use, and that's never assigned again, is handed to them as it is. A local that's assigned again needs a box the functions share (see boxed-capture), which costs more. See Cheap Upvalues.

A2C:IR:M:3011 boxed-capture

A local that nested functions use is kept in a box they share, because it's assigned after it's declared. The message names where. A box costs a table and an extra lookup per use. When you can, give it its final value when you declare it.

A2C:IR:O:3020 direct-call

A call of a local function that's never assigned again goes straight to its compiled code, without looking the function up first. See Known Functions.

A2C:IR:M:3021 direct-call-assigned

A call of a local function goes the ordinary way, because the local is assigned again somewhere, so the compiler can't be sure which function it holds. The message names where. If you can, give it its function once, where you declare it.

A2C:IR:O:3022 self-tail-call

return f(...) of the running function is a loop. See Self Tail Calls Are Loops.

A2C:IR:O:3023 call-result-in-place

A call's result is used or stored right where the call left it, instead of being copied first.

A2C:IR:O:3030 fixed-environment

Lua 5.3: globals are read through _ENV, which is looked up once per call. See Globals.

A2C:IR:M:3031 environment-assigned

Lua 5.3: the script declares or assigns _ENV, so every global goes through it each time.

A2C:IR:O:3040 folded-stack-check

Before it runs, a function checks that the host's stack has room for its values. A function that calls a known function checks for both at once, so the callee doesn't check again. The message says how many calls this saves a check for.

A2C:IR:M:3041 stack-check-recursive

A call of a known function doesn't share its caller's stack check (see folded-stack-check), because the call is part of a recursion: the callee checks its own room each time it runs. The message says how many calls.

A2C:IR:O:3050 kept-library-table

In string.format(...) and other calls of a function stored in a table, the table stays where it was read until the statement ends, instead of being moved out of the way first. Each such call saves a move.

A2C:IR:O:3060 one-callee-check

Always on. One check of the function a call calls decides whether it's compiled, whether it's the host's error, and how a tail call goes.

A2C:IR:O:3061 fixed-results

Always on. Results move with one call each, and stay where the caller takes them.

A2C:IR:O:3062 host-tail-call

Always on. return f(...) of a function of the host, such as return setmetatable(t, mt), calls it right there.

A2C:IR:O:3063 entry-body

Always on. A compiled function's entry knows its body, so the host calls it without moving anything.

A2C:IR:O:3064 room-passed

Always on. Room on the host's stack that a caller checked is passed down, and a function that fits skips its own check.

Loops and Conditions

A2C:IR:O:4001 native-for

A numeric for counts natively. See Numeric For Loops.

A2C:IR:M:4002 native-for-values

A numeric for runs the ordinary way, because its start, limit or step may not be a number, or in Lua 5.3 may be either an integer or a float, which decides the kind of loop only when it runs. The message names the value. Use numbers the compiler can see for all three, such as literals, #t or numeric locals.

A2C:IR:O:4010 host-iterator

A for loop over pairs or next (and in Lua 5.1 ipairs) walks the table itself instead of calling the iterator each round, once it has checked that the iterator is the host's own. See pairs and ipairs.

A2C:IR:O:4011 ipairs-loop

Lua 5.3: a loop over ipairs reads each element directly, once ipairs is the host's.

A2C:IR:O:4020 peeled-loop

The first round of a loop runs in a copy, so the rest of the loop knows the kind of each number. See Integers and Floats.

A2C:IR:M:4021 peeled-loop-limit

A loop's first round doesn't get a copy, because the loop is too large. Moving work out of the loop into a function of its own makes it smaller.

A2C:IR:O:4030 branch-on-comparison

Always on. A comparison in an if, while or repeat branches on its result directly. See Conditions.

A2C:IR:O:4031 truth-test

Always on. A condition tests a value where it is, or by the type it's known to have.

Library Functions

A2C:IR:O:5001 math-intrinsic

math.sqrt, math.floor, math.ceil, math.abs, math.min and math.max are computed natively when the function is the host's own. See Library Functions.

A2C:IR:M:5002 math-intrinsic-results

A math function is called the ordinary way, because its last argument is a call or ..., which may give any number of values.

A2C:IR:O:5010 string-intrinsic

string.byte, string.sub and string.len are computed natively when the function is the host's own and the subject is a string.

A2C:IR:M:5011 string-intrinsic-subject

string.byte, string.sub or string.len is called the ordinary way, because the string isn't held by a plain local, or a position isn't a number of the right kind (an integer, in Lua 5.3). Put the string in a local first, as in local s = self.src;.

A2C:IR:O:5012 library-call

tonumber(x), or in Lua 5.3 string.unpack with a format of one integer such as ">I4", is computed natively when the function is the host's own, and called otherwise. See Library Functions.

A2C:IR:O:5020 type-call

type(x) gives the name of the type without calling the function, and type(x) == "table" is a plain comparison, without making a string.

A2C:IR:O:5030 select-vararg

select("#", ...) and select(i, ...) read the function's own arguments.

Stack Frames

A2C:IR:O:6001 placed-locals

A local is stored where its value was just computed, the way Lua's own compiler places it, instead of being copied there. See Local Variables.

A2C:IR:M:6002 placed-locals-goto

Each local gets a place of its own, because the function has goto or a label.

A2C:IR:M:6003 deferred-cuts-goto

The host's stack is tidied after every statement, because the function has goto.

Moving Values

A2C:IR:O:7001 site-record

Always on. What a call needs to report an error (its line, the name of what it calls) is one constant, read only when an error happens.

A2C:IR:O:7002 operands-in-place

Always on. A comparison reads locals where they are, without copying them.

A2C:IR:O:7003 store-from-top

Always on. A store takes its value from where it was just computed.

A2C:IR:O:7004 deferred-cuts

Always on. The host's stack is tidied once, at the end of a block, not after each statement.

A2C:IR:O:7005 concat-run

Always on. A run of .. is joined at once, as the interpreter does. See Concatenation.

Clean-up

A2C:IR:O:8001 constant-propagation

Always on. Checks whose outcome the compiler already knows are left out, and so are values that are never read.

A2C:IR:O:8002 jump-threading

Always on. A jump to a check whose outcome is known goes straight to where the check leads.

A2C:IR:O:8003 merged-blocks

Always on. Pieces of code that always run one after the other are joined, so a comparison and the jump it decides sit together. The code is shorter and faster.

Internal Errors

A2C:IR:E:9000 ir-internal

An internal error of the compiler. This is a bug in autoplay2c: please report it, with the script that causes it.