Skip to content

Call a hybrid function cast from a plain function directly - #322

Merged
ASDAlexander77 merged 1 commit into
mainfrom
hybrid-func-null-this-direct-call
Sep 14, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
hybrid-func-null-this-direct-call

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Problem

Casting a plain function value (e.g. an address from GetProcAddress) to a function type produces a hybrid function whose this is a literal null. Every call through it was still lowered to a runtime this test with two call paths and a phi, at every optimization level. The dead path even called the plain function with an extra ptr null argument.

br i1 false, label %2, label %4
2: %3 = call double @add1(ptr null, double 4.1e+01)   ; dead, wrong arity
4: %5 = call double @add1(double 4.1e+01)
6: %7 = phi double [ %5, %4 ], [ %3, %2 ]

Fix

SimplifyIndirectCallWithKnownCallee now handles a callee defined by ts.Cast from FunctionType to HybridFunctionType: it calls the cast's input directly and erases the cast if it is unused.

  • The canonicalizer runs regardless of --opt, so --opt_level=0 / --di builds benefit.
  • It runs before LowerToAffine turns calls inside try into ts.Invoke, so one pattern covers both call and invoke; no LLVM lowering changes.
  • Function-typed variables keep the runtime check, since they may hold a bound method.

Result at --opt_level=0: %2 = call double @add1(double 4.1e+01).

Design notes and rejected alternatives: docs/hybrid-func-null-this-call-optimization.md.

Tests

  • New 00funcs_hybrid_null_this.ts: direct call, call inside try, and a bound method through a function-typed variable. Registered as compile, jit and rc/none corpus; all 6 pass.
  • Full Windows debug suite: 2698/2715. The 17 failures are pre-existing: 14 were already failing before this branch, and the 3 gc-shared-auto / gc-defaultlib-collector failures (--emit=dll exits 0x80000003) reproduce with this change reverted.

🤖 Generated with Claude Code

@ASDAlexander77
ASDAlexander77 merged commit 59fd8e1 into main Sep 14, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the hybrid-func-null-this-direct-call branch September 14, 2026 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant