Skip to content

Handwritten semantic-rewrite layer for record surgery and identity foreigns #236

Description

@Unisay

Problem

The source-derived ForeignLift (#178) is the right tool for scalar primops and uncurried wrappers: it reads the real FFI source, so the registry cannot drift from the package. But a class of higher-level rewrites cannot be expressed as a lift of FFI source: record surgery (Record.Builder.unsafeInsert/Delete/Modify, Record.Unsafe.unsafeGet/Set/Has, unsafeUnionFn) and identity foreigns (unsafeCoerce a → a, Record.Builder.copyRecord r → r). A small handwritten registry folds these: when the record is statically known, the surgery collapses into a record literal or update, and the identity coercions vanish. On the Lua target, where a table allocation is expensive, folding record surgery on a known record removes a real allocation.

Approach

A narrow handwritten semantic layer on top of ForeignLift, keyed by qualified name and spine shape, kept small. When the record operand is a manifest literal or update, rewrite the surgery into the resulting literal/update; rewrite unsafeCoerce/copyRecord to their argument unconditionally. Anything not statically known is left as a call. This stays strictly complementary to the source-derived lift: no scalar or uncurried rewrites move here.

Prerequisites / Relations

Independent. Complements ForeignLift (#178) rather than replacing it, and relates to the allowlist-extension direction. Ordering fusion (compare … is LT → <) is deliberately out of scope: it is already covered by #180 (Ordering-constructor collapse through the if-tree) plus #220 and #222.

Verification / Measurement

unsafeInsert "k" v <record literal> folds to a record literal carrying the new field, and unsafeCoerce x to x, in focused optimizer tests; a surgery on an unknown record is left as a call. Allocation-counter drop on a record-builder benchmark through #172; eval goldens unchanged.

Metadata

Metadata

Assignees

Labels

OptimisationA Compiler Optimisationarea: irIR / optimizer / DCE / inlinerenhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions