This is an archive of the discontinued LLVM Phabricator instance.

[ObjC] Allow declaring __weak pointer fields in C structs in ObjC-ARC
ClosedPublic

Authored by ahatanak on Mar 5 2018, 8:22 AM.

Diff Detail

Event Timeline

ahatanak created this revision.Mar 5 2018, 8:22 AM
rjmccall added inline comments.Mar 5 2018, 5:17 PM
include/clang/AST/Decl.h
3631

I feel like this flag should be set by Sema for C++ types that have to be passed indirectly as well; it can then become the single point of truth for that information.

lib/CodeGen/CGNonTrivialStruct.cpp
764

I guess this is the most reasonable thing to do, given that we don't have an entrypoint for it. Please ask the ObjC runtime team to consider giving us one, though. We could pretty easily peephole assignments into __weak variables where the source is loaded from a __weak l-value / x-value, and Swift would probably be able to take advantage of it, too.

You might want to go ahead and add emitARCCopyAssignWeak / emitARCMoveAssignWeak methods on CGF that do these operations and which can be optimized to use those entrypoints if/when they're added.

lib/CodeGen/TargetInfo.cpp
1326

Can we combine this into a single function that covers both the C++ case and this new one? Otherwise it seems likely that individual targets over time will only add one or the other.

ahatanak updated this revision to Diff 137500.Mar 7 2018, 3:23 PM
ahatanak marked 2 inline comments as done.

Address review comments.

ahatanak added inline comments.Mar 7 2018, 3:24 PM
include/clang/AST/Decl.h
3631

I moved CXXRecordDecl::CanPassInRegisters to RecordDecl along with its setter and getter functions.

lib/CodeGen/CGNonTrivialStruct.cpp
764

Do you mean we should ask for the following objc runtime functions and use them in visitARCWeak?

// dst and src are either null or registered as __weak objects.
void objc_copyAssignWeak(id *dst, id *src)
void objc_moveAssignWeak(id *dst, id *src)
`
lib/CodeGen/TargetInfo.cpp
1326

I added another definition of classifyReturnType and taught getRecordArgABI to detect non-trivial C structs. Currently, x86, arm, and arm64 are the only targets that use the new overload of classifyReturnType. I can modify the other targets to use it too if that is desirable.

rjmccall accepted this revision.Mar 7 2018, 8:41 PM

LGTM, thanks.

lib/CodeGen/CGNonTrivialStruct.cpp
764

I meant that we should implement emitARCCopyAssignWeak and emitARCMoveAssignWeak by calling those functions if they're available, yes.

(Obviously that would be a follow-up patch, even assuming the ObjC runtime agrees to add them.)

Those functions would be specified as leaving the source in a zeroed state, which is as good as uninitialized, so they could be used for true-destructive moves as well.

This revision is now accepted and ready to land.Mar 7 2018, 8:41 PM
This revision was automatically updated to reflect the committed changes.
This revision was automatically updated to reflect the committed changes.

This was reverted in r327294 because it caused module-enabled builders to fail after I moved CXXRecordDecl::CanPassInRegisters to RecordDecl. I'm recommitting this patch with fixes to ASTDeclReader and ASTWriter.