This is an archive of the discontinued LLVM Phabricator instance.

Implement [[msvc::no_unique_address]]
AbandonedPublic

Authored by akhuang on Aug 11 2023, 3:27 PM.

Details

Summary

This implements the [[msvc::no_unique_address]] attribute.

There is not ABI compatibility in this patch because the attribute is relatively new and there's still some uncertainty in the MSVC version.

Bug: https://github.com/llvm/llvm-project/issues/49358

Diff Detail

Event Timeline

akhuang created this revision.Aug 11 2023, 3:27 PM
Herald added a project: Restricted Project. · View Herald Transcript
akhuang requested review of this revision.Aug 11 2023, 3:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2023, 3:27 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
dblaikie added inline comments.
clang/lib/AST/Decl.cpp
4510–4512

maybe llvm::any_of? Not especially shorter, but maybe makes it a smidge easier to read?

4524–4525

Having to check both of these in several places seems problematic - can we wrap that up somewhere? (or, maybe ideally, is there a way for msvc::no_unique_address to map to the actual NoUniqueAddressAttr as a different spelling of the same thing?)

Thank you for working on this!

I had some comments, but I ran out of time before I could complete my review.

clang/include/clang/Basic/Attr.td
1799

Hmmm, it would sure be nice if we could combine this attribute with NoUniqueAddress above and just have an accessor for whether it's the microsoft version or not... but I think the TargetSpecificAttr bit prevents us from doing that. CC @erichkeane in case he's got ideas.

(No changes needed currently, mostly just a cleanup question.)

1800

I was a bit shocked to learn that this really is the correct return value for __has_cpp_attribute: https://godbolt.org/z/MW9q1hPEh

clang/include/clang/Basic/AttrDocs.td
1410

"similar" makes me wonder "so how is it different?" which the docs don't answer. I think this could be stated as:

For Windows targets, ``[[no_unique_address]]`` is ignored; use ``[[msvc::no_unique_address]]`` instead.

WDYT?

clang/lib/AST/Decl.cpp
4509

Hmmm, this seems to be more like MSVC doesn't believe an empty structure or union can have zero size: https://godbolt.org/z/fj4eYvnM7

4524–4525

This was why I was hoping we could merge the two in Attr.td, but I'm not certain that will be easy.

akhuang added inline comments.Aug 17 2023, 1:01 PM
clang/lib/AST/Decl.cpp
4524–4525

What does merging the two in Attr.td mean? Could we just put the two spellings in one attribute, or would that make it impossible for clang-cl to ignore the [[no_unique_address]] spelling

akhuang updated this revision to Diff 551242.Aug 17 2023, 1:23 PM
akhuang marked an inline comment as done.

Add function to check for no_unique_address attributes
Some changes to layout code

still on vaca, but doing a drive-by.

clang/include/clang/Basic/Attr.td
1799

I think I would combine this and the one above as a single attribute. Then, only 'add' it in SemaDeclAttr.cpp when the spelling is the appropriate one for the current target. IMO, msvc::no_unique_address should ONLY be valid in MSVC mode, else we encourage its propagation.

We don't need it to be a 'TargetSpecificAttr', just do the 'ignored' warning in the handler in SemaDeclAttr.td.

The accessor that we need is to just query which spelling.

1800

Thats... interesting....horrifying... else?

clang/lib/AST/Decl.cpp
4477

This ends up going away if we combine them.

aaron.ballman added inline comments.Aug 18 2023, 6:15 AM
clang/include/clang/Basic/Attr.td
1799

That's the approach I was thinking of, but then we lose some of the declarative expressiveness from Attr.td, which is unfortunate. But this is a pretty unique situation and so perhaps it's reasonable and we can try to think of a tablegen approach once we get a second attribute in the same situation.

clang/lib/AST/Decl.cpp
4524–4525

We can have multiple syntactic spellings for the same semantic attribute (e.g., __attribute__((foo)) and __attribute__((bar)) can both map to a single FooBarAttr AST node), and we have "accessors" on the AST node that let you tell which spelling was used: https://github.com/llvm/llvm-project/blob/90ecadde62f30275c35fdf7928e3477a41691d21/clang/include/clang/Basic/Attr.td#L4095

The suggestion Erich and I are thinking of is:

  1. Add the additional spelling to NoUniqueAddress.
  2. Add accessors to differentiate the spellings.
  3. Remove the TargetSpecificAttr from NoUniqueAddress, manually implement those checks in an attribute handler in SemaDeclAttr.cpp.

Then, anywhere you care about the attribute in general, you can look for isa<NoUniqueAddress>, and anywhere you care about which spelling, you can use cast<NoUniqueAddress>(A)->isMSVC() (or whatever you name the accessors).

akhuang marked an inline comment as done.Aug 18 2023, 11:12 AM
akhuang added inline comments.
clang/include/clang/Basic/AttrDocs.td
1410

yep, that sounds good

akhuang updated this revision to Diff 551576.Aug 18 2023, 11:13 AM

combine attributes into the same attribute and add accessors

akhuang marked an inline comment as done.Aug 18 2023, 11:39 AM
akhuang added inline comments.
clang/lib/AST/Decl.cpp
4524–4525

Thanks! This patch should implement this, and so far, I don't think there are any places outside of SemaDeclAttr where we have to differentiate the two.

mstorsjo added inline comments.Aug 18 2023, 12:24 PM
clang/include/clang/Basic/AttrDocs.td
1409

On MSVC targets, [[no_unique_address]] is ignored - it's not ignored for mingw targets.

dblaikie added inline comments.Aug 18 2023, 12:41 PM
clang/lib/AST/Decl.cpp
4510
4524–4525

(awesome - glad to see this was so tidy to do/that the generic attribute handling stuff mostly covers it)

clang/lib/AST/RecordLayoutBuilder.cpp
3108–3110
akhuang marked 2 inline comments as done.Aug 18 2023, 3:35 PM

I'm still trying to figure out the MSVC layouts. I used the EmptySubobjects class for a lot of the logic, which is not always the same as what MSVC does. (some examples of differences in https://godbolt.org/z/6cP554ddb)

akhuang updated this revision to Diff 551662.Aug 18 2023, 3:36 PM

address small fixes

akhuang updated this revision to Diff 556090.Sep 6 2023, 3:51 PM

more code updates and added some test cases

akhuang retitled this revision from [WIP] Implement [[msvc::no_unique_address]] to Implement [[msvc::no_unique_address]].Sep 6 2023, 3:51 PM
akhuang edited the summary of this revision. (Show Details)Sep 6 2023, 3:56 PM
akhuang updated this revision to Diff 556091.Sep 6 2023, 4:18 PM

add note to the docs about no ABI compatibility

akhuang abandoned this revision.Sep 7 2023, 2:16 PM