Repository navigation
@cppvtable: classes implementing C++ methods (C++ calls them through their vtable) - #554
Merged
Merged
Conversation
… their vtable A @cppvtable class whose methods have bodies implements C++ methods, for example nvrhi::IMessageCallback: - it extends the declared C++ interface and overrides its slots by name; - its objects are allocated by tslang and carry a vtable with the C++ layout: the slots and nothing else; - a slot left to a method of the interface (one with no body) is null. It may have a constructor and field initializers, and still has no RTTI. A method with a body is marked ts.cpp_method, and LowerToLLVM extends its narrow results as C++ callers expect. Not yet supported (both errors): returning a struct, and exporting the class. Tests: - abi_host.cpp's Callback, implemented by Counter in abi_msvc.ts and abi_itanium.ts and called by C++ (MSVC locally, g++ in WSL); - the Itanium vtable and the bool result's zeroext, checked in IR for x86-64 Linux and AArch64 Android; - errors for a struct return from a body and for an exported implementation. cppvtable/method_body.ts now tests the struct-return error, since a body is no longer an error. Full suite: 4012/4012. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
@cppvtableclasses so far only described C++ objects that C++ creates, so tslang could call them. With this PR, a@cppvtableclass whose methods have bodies implements a C++ interface: tslang creates the object and C++ calls it. The first use isnvrhi::IMessageCallback, NVRHI's message sink:How it works
ClassInfo::isCppVTableImplementation: set for a@cppvtableclass with a method that has a body. Such a class gets:.new;.sizestatic;It still gets no
.instanceOfor RTTI, so the vtable is exactly the C++ slots.Vtable slots: a method with a body overrides the base's slot of the same name, and a constructor never takes a slot. A slot left to a method of the declared interface (no body, such as a destructor) is
null, as a pure virtual's would be.Object layout:
newstores the vtable's address in the object's first word, where C++ expects its vtable pointer. The interface's fields and then the class's own fields follow.Narrow results: methods with bodies are marked
ts.cpp_method.LowerToLLVMgives their narrow results (boolean,u8, ...)zeroext/signext, because C++ callers rely on the callee widening them. Other definitions are unchanged.Errors:
Statics, accessors,
implements, generics and rc/own stay errors as before.cppvtable/method_body.ts, whose error a body no longer triggers, now tests the struct-return error.Lifetime and limits
These are documented in
docs/cppvtable-classes.md, "Implementing C++ methods":stringargument is C++'s buffer, valid during the call only.dynamic_castortypeidthe object.Tests
test-jit-cppvtable-abi:cppvtable/abi_host.cppgains a C++Callbackinterface and functions that call it.Counterinabi_msvc.tsandabi_itanium.tsimplements it, with a constructor and fields. C++ callsmessage()twice (checking the text),count(), andisEven()for aboolresult; tslang then calls it through the interface type too.abi_itanium.tslinked with a g++-builtabi_host.cpppasses in WSL Ubuntu.test-compile-cppvtable-itanium-implementation-<triple>(x86-64 Linux, AArch64 Android): checks the vtable is{ null, null, message, count, isEven }and thatisEveniszeroext i1.cppvtable-method_body(struct return) andcppvtable-export_implementation.nvrhi_directimplementsnvrhi::IMessageCallbackthis way.-debug, D3D12 and Vulkan both receive a provoked validation error and pass.message()isn't exercised there, since there's no validation layer.🤖 Generated with Claude Code