Inline small closures called from sibling closures (#4808)

This commit is contained in:
Chris Tate
2026-12-03 11:39:38 +00:00
committed by GitHub
parent d33962ddd9
commit be0056a702
3 changed files with 72 additions and 0 deletions
+8
View File
@@ -829,6 +829,14 @@ std::optional<ProgramPartitions> partitionProgramText(StringRef Text,
(S.Kind == SymbolKind::Function
? std::binary_search(S.Calls.begin(), S.Calls.end(), Ref)
: S.Constant));
} else if (S.Kind == SymbolKind::Function && Symbols[Ref].Kind == SymbolKind::Function &&
Symbols[Ref].Header.contains("alwaysinline") &&
std::binary_search(S.Calls.begin(), S.Calls.end(), Ref)) {
// A later caller's direct call makes an always-inlined definition a
// candidate of the partition it already follows: a factory takes the
// closure body's address, and the sibling closure that calls it is
// the one that can inline it.
MayFollow[Ref] = 1;
} else {
Referrer[Ref] = Shared;
}
@@ -0,0 +1,51 @@
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, test } from "vitest";
import { compile, deserializeModule } from "../../index.js";
import { emitLlvmModule } from "./emitter.js";
async function llvmFor(source: string): Promise<string> {
const dir = await mkdtemp(join(tmpdir(), "scriptc-closure-inlining-"));
try {
const entry = join(dir, "main.ts"),
output = join(dir, "main.ir.json");
await writeFile(entry, source);
const result = await compile(entry, { outDir: dir, outPath: output, outputKind: "ir" });
if (!result.ok)
throw new Error(result.diagnostics.map((item) => `${item.code}: ${item.message}`).join("\n"));
return emitLlvmModule(deserializeModule(await readFile(output, "utf8")));
} finally {
await rm(dir, { recursive: true, force: true });
}
}
test("a small lifted closure body is marked alwaysinline", async () => {
const ir = await llvmFor(`
function make() {
let n = 0
function inc() {
n = n + 1
}
inc()
return inc
}
make()()
`);
expect(ir).toMatch(/define internal \S+ @sc_\S+\(ptr %sc_env[^\n]*alwaysinline #0 \{/);
});
test("a lifted closure body above the inline size keeps the cost model's decision", async () => {
const body = Array.from({ length: 140 }, () => " n = n + 1").join("\n");
const ir = await llvmFor(`
function make() {
let n = 0
function big() {
${body}
}
return big
}
make()()
`);
expect(ir).not.toMatch(/define internal \S+ @sc_\S+\(ptr %sc_env[^\n]*alwaysinline #0 \{/);
});
@@ -893,6 +893,19 @@ export function emitFunction(emitter: LlEmitter, fn: IrFunction): string {
// the function had before the version existed.
else if (versionedFunctions(emitter).has(fn.name)) attrs = `noinline ${attrs}`;
if (fn.name.startsWith(RESULT_ENTRY_PREFIX)) attrs = `inlinehint ${attrs}`;
// A small closure body is inlined at every direct sibling call: the
// address the factory stores keeps the partitioner from following it
// otherwise, and the call is the only dispatch a known callee pays.
if (
!unoptimized &&
emitter.debug === null &&
!attrs.includes("noinline") &&
fn.captures !== undefined &&
!fn.async &&
!fn.generator &&
(emitter.nodeIndex.nodeCount(fn) ?? Infinity) <= 120
)
attrs = `alwaysinline ${attrs}`;
if (unoptimized && attrs === FN_ATTRS) attrs = `noinline optnone ${attrs}`;
const debug = emitter.debugScope === null ? "" : ` !dbg ${emitter.debugScope}`;
const symbol = borrowed ? mangleBorrowedFunction(fn.name) : mangleFunction(fn.name);