Skip to content

A @cpp class works under -mm=rc and -mm=own - #627

Merged
ASDAlexander77 merged 1 commit into
mainfrom
cpp-classes-rc-own
Oct 20, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
cpp-classes-rc-own

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Why it was refused

Under rc and own an object's count is the word before it. A @cpp class's reference is a C++ pointer, and the word before a C++ object belongs to the C++ heap (or a neighbour), so a retain or release would write into C++ memory.

Change

No model counts a @cpp reference now:

  • ClassStorageType carries a cpp flag, set right where the class type is made (MLIRGenClasses.cpp), before anything of the type is generated.
  • MLIRTypeHelper::ownsHeapMemory answers no for it. MLIRGen, the ownership passes and the release/retain routines all ask that, so copies into locals, fields, arrays and arguments retain and release nothing, and a holder's destroy skips a @cpp field.
  • The "needs the gc or none memory model" error is removed.

The object stays C++'s to free. An object of a class implementing C++ methods made with new is never freed by tslang under rc/own (documented in docs/cpp-classes.md, new "Memory models" section).

Tests

  • 00cppvtable_class.ts, 00cpp_slot.ts, 00cpp_overloads.ts join the rc/none corpus (and the ownership verifier over it), and run under own (AOT + JIT).
  • cppvtable/memory_model.ts compiles under rc, own and none.
  • test-jit-cppvtable-abi also runs under rc and own, plus --verify-ownership. abi_host.cpp's new heap object sits right after a guard word of C++'s own; the guard is checked while tslang holds copies (field, array, local) and after, then C++ deletes the object.
  • Teeth: with the exemption turned off, the rc run fails "nothing written in the word before a C++ object" and the own runs fail to compile.
  • Windows Release ctest: 4857/4857 passed. The Itanium (Linux) half got the same change but was only run by CI.

🤖 Generated with Claude Code

A @cpp class's reference is a C++ pointer. Under rc and own an object's count is the word
before it, which in front of a C++ object is the C++ heap's (or a neighbour's), so these
models were refused. Now no model counts a @cpp reference: ClassStorageType carries a cpp
flag, set where the class type is made, and MLIRTypeHelper::ownsHeapMemory answers no for
it. MLIRGen, the ownership passes and the release routines all ask that, so copies into
locals, fields, arrays and arguments retain nothing and release nothing, and a holder's
destroy skips the field. The object stays C++'s to free; one made by `new` (a class
implementing C++ methods) is never freed by tslang.

Tests: the @cpp test files join the rc/none corpus and run under own; memory_model.ts
compiles under rc, own and none; test-jit-cppvtable-abi runs under rc and own too, with
--verify-ownership, against a C++ object that sits right after a guard word of C++'s own,
checked while tslang holds copies and then deleted by C++. With the exemption turned off
the rc run fails that check and the own runs fail to compile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 11d747a into main Oct 20, 2026
2 of 3 checks passed
@ASDAlexander77
ASDAlexander77 deleted the cpp-classes-rc-own branch October 20, 2026 19:44
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