Optimize away trivial inlined abstract constructor blocks#12839
Merged
Simn merged 5 commits intohaxe-numericfrom Mar 18, 2026
Merged
Optimize away trivial inlined abstract constructor blocks#12839Simn merged 5 commits intohaxe-numericfrom
Simn merged 5 commits intohaxe-numericfrom
Conversation
When inlining abstract constructors of the form `inline function new(x) this = x;`,
the compiler generates a block pattern `{ var v; v = expr; cast v }` that wasn't
being simplified. This caused performance regressions in generated JVM and HL
bytecode for numeric type operations (Int32→Int64 conversions produced unnecessary
intermediate variables and store/load instructions).
Add a pattern match in reduce_expr to collapse these trivial blocks into a single
`cast expr`, eliminating the temporary variable entirely. This restores the
generated bytecode to match the pre-refactoring output exactly.
Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Add tests/hlcode/src/cases/NumericTypes.hx with three tests verifying that Int64/Int32 mixed operations produce clean bytecode without unnecessary intermediate variables: - eqI64I32: Int64 == Int32 comparison produces direct toint + jnoteq - addI64Int: Int64 + Int addition produces direct toint + add - i32ToI64: Int32 to Int64 assignment produces a single toint Also enhance the HL code test normalizer (Macro.hx) to handle integer constant pool references, making tests more robust across compilation contexts. Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Unify numeric types API across all targets
Optimize away trivial inlined abstract constructor blocks
Mar 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The numeric types refactoring (Int64→Int64Native layering) introduced a codegen regression:
new Int64(Int64Native.ofInt(x))inlines the abstract constructor as{ var this; this = cast x; cast this }instead of collapsing tocast x. This produces unnecessary intermediate variables in JVM/HL bytecode.Before fix (JVM for
i64 == i32):After fix:
Compiler changes
src/filters/safe/sanitize.ml: Pattern inreduce_exprcollapses{ var v; v = expr; cast v }→cast exprwhen the initializer is trivial (None/null) andexprdoesn't referencev. Runs duringreduce_loopbefore the analyzer, so works with@:analyzer(ignore).src/core/texpr.ml: AddTexpr.references_localhelper used by the safety check above.Tests
tests/hlcode/src/cases/NumericTypes.hx: Three HL bytecode tests (eqI64I32, addI64Int, i32ToI64) asserting clean codegen without intermediate variables.tests/hlcode/src/Macro.hx: Normalize integer constant pool references (int R,@N) for stability across compilation contexts.💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.