llvm-project
3e2f0bce - [ObjCDirectPreconditionThunk] precondition check thunk generation (#170618)

Commit
28 days ago
[ObjCDirectPreconditionThunk] precondition check thunk generation (#170618) ## TL;DR This is a stack of PRs implementing features to expose direct methods ABI. You can see the RFC, design, and discussion [here](https://discourse.llvm.org/t/rfc-optimizing-code-size-of-objc-direct-by-exposing-function-symbols-and-moving-nil-checks-to-thunks/88866). https://github.com/llvm/llvm-project/pull/170616 Flag `-fobjc-direct-precondition-thunk` set up https://github.com/llvm/llvm-project/pull/170617 Code refactoring to ease later reviews https://github.com/llvm/llvm-project/pull/170618 **Thunk generation** https://github.com/llvm/llvm-project/pull/170619 Optimizations, some class objects can be known to be realized ## Implementation details ### Dispatching - `GetDirectMethodCallee` handles the dispatching logic. Previously we only need to call `GenerateDirectMethod` to get the declaration of a direct method. - `GenerateDirectMethod` first attempts to acquire the declaration of the implementation, and return it if the flag is not set. - Generate and return thunk if we can't dispatch to true implementation (i.e. we can't reason receiver is def not null or class object is not realized) ### Precondition check thunk generation - `GenerateObjCDirectThunk` generates the thunk, it is called on demand by `GetDirectMethodCallee` - Thunk inherits all attributes from the true implementation, see `StartObjCDirectThunk` for more detail. - `StartObjCDirectThunk` and `FinishObjCDirectThunk` follows the design pattern of `StartThunk` and `FinishThunk` in CGVTable. ### Precondition check inline generation - If the function need to have precondition check inlined (`shouldHaveNilCheckInline`), caller will emit the nil check during `EmitMessageSend` - Class realization is generated inline - No extra nil check is generated - we reuse `NullReturnState` to emit the nil check for us, it already emits nil check at caller side to handle `ns_consumed`, we just need to tell `NullReturnState` to do the work by setting the flag `RequiresNullCheck |= ReceiverCanBeNull;` ### Visibility and linkage - Visibility is still by default `Hidden`. But `StartObjCMethod` will now respect source level visibility attributes so methods with `__attribute((visibility("default"))` can be used in other linking units - Linkage is by default `External` ## Tests - `expose-direct-method.m` follow the example of `direct-method.m` - `direct-method-ret-mismatch.m` make sure we can handle the corner case - `expose-direct-method-consumed.m ` and `expose-direct-method-linkedlist.m` executable test on Mac only to validate ARC correctness - `expose-direct-method-varargs.m` - `expose-direct-method-visibility-linkage.m`
Author
Parents
Loading