Skip to content

Remove std::function churn and wasted arena interning in MLIRGen#203

Merged
ASDAlexander77 merged 1 commit into
mainfrom
refactor/allocation-hotspots
Jul 11, 2026
Merged

Remove std::function churn and wasted arena interning in MLIRGen#203
ASDAlexander77 merged 1 commit into
mainfrom
refactor/allocation-hotspots

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Summary

Implements item A4 (allocation hot spots) from docs/MLIRGen-refactoring-review.md:

  • Utility-type dispatch tables. getEmbeddedTypeWithParamBuiltins / ...NoBuiltins / ...ManyParamsBuiltins used llvm::StringSwitch<std::function<...>>, which constructs every case's closure eagerly on each call (~17 std::functions per getType of a builtin utility type). Rewritten as StringSwitch<enum> + a plain switch — allocation-free, and the shared "evaluate first type argument" prologue is now hoisted once. Behavior notes: cases that previously returned a possibly-null type without checking (Readonly/Partial/Required/ThisType) are unchanged in effect, since returning a null mlir::Type equals returning mlir::Type(); the NoBuiltins variant deliberately keeps no null check, matching the original.
  • std::bind elimination (18 sites → 0). The four verbatim 5-placeholder cast binds became one explicit lambda; the two anyOrUndefined/optionalValueOrUndefined ternaries became member-function-pointer selection (identical signatures) invoked via this->*; the constructor's four MLIRTypeHelper callbacks and the getGlobalsFullNamespaceName bind became lambdas.
  • Arena interning. getFullNamespaceName(name) permanently interned its result into the never-freed BumpPtrAllocator on every call. New concatFullNamespaceName() builds the name without interning and is used where the result is transient — getNameOfFunction (which immediately called .str()), the two enum FlatSymbolRefAttr sites (MLIR interns into the context anyway), and the dynamic-import symbol name. Registration paths keep interning.

Test plan

  • tslang rebuilt cleanly (vs2026 debug)
  • New smoke test covering all three rewritten dispatch tables (Uppercase, Capitalize, Pick, Record, Readonly, ReturnType, Exclude), spread-into-fixed-params (member-pointer valueFactory path), Array.push (cast-lambda path), enums and namespaced functions (concatFullNamespaceName paths) — JIT output 5 1 / ABC Hello 1 x 3 true c / 6 / 42, all as expected
  • Prior smoke tests for Refactor theModule handling in MLIRGenImpl #201/Add GenContext field initializers and SourceFileScope RAII guard #202 paths still produce 42 15 42 3 1 and 5 21 7 x
  • MLIRGenTests.exe: 12/12 pass

🤖 Generated with Claude Code

- Rewrite the three StringSwitch<std::function> utility-type tables
  (getEmbeddedTypeWithParam*/ManyParams) as enum dispatch; the old form
  constructed every case closure eagerly on each call
- Replace all std::bind sites with lambdas or member-function pointers
- Add concatFullNamespaceName() (no interning) and use it where the
  full name is transient (getNameOfFunction, enum type refs, dynamic
  import names); getFullNamespaceName keeps interning for stored names

Review doc item A4.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ASDAlexander77 ASDAlexander77 merged commit d36c838 into main Jul 11, 2026
2 checks passed
@ASDAlexander77 ASDAlexander77 deleted the refactor/allocation-hotspots branch July 11, 2026 21:26
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