This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Allow comparisons between different ms ptr size address space types.
ClosedPublic

Authored by akhuang on Sep 28 2021, 5:29 PM.

Details

Summary

We're currently using address spaces to implement ptr32/ptr64 attributes;
this patch fixes a bug where clang doesn't allow types with different pointer
size attributes to be compared.

Fixes https://bugs.llvm.org/show_bug.cgi?id=51889

Diff Detail

Event Timeline

akhuang requested review of this revision.Sep 28 2021, 5:29 PM
akhuang created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptSep 28 2021, 5:29 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman added inline comments.
clang/lib/Sema/SemaExprCXX.cpp
6682

I'm pretty sure this is correct based on my inspection of what code MSVC is generating. But it would be helpful to have some codegen tests in Clang for this functionality as well.

clang/test/Sema/MicrosoftExtensions.cpp
10–11

(Side question, not directly about this patch but sorta related.) Should there be a diagnostic about conversion potentially causing a possible loss of data (on the 64-bit target)?

akhuang added inline comments.Sep 29 2021, 3:12 PM
clang/lib/Sema/SemaExprCXX.cpp
6682

ha, I apparently didn't check that the behavior actually matches.. apparently in MSVC a ptr32 isn't equivalent to a ptr64

aaron.ballman added inline comments.Sep 30 2021, 4:58 AM
clang/lib/Sema/SemaExprCXX.cpp
6682

Oh! I had tested this:

int test1(int * __ptr32 __uptr p32u, int * __ptr32 __sptr p32s,
                         int * __ptr64 p64) {
  return (p32u == p64);
}

int test2(int * __ptr32 __uptr p32u, int * __ptr32 __sptr p32s,
                         int * __ptr64 p64) {
  return (p64 == p32u);
}

to see whether the order of the operands mattered as to which conversion "won" and I thought I saw that your patch generates the same code that MSVC does. However, I could have messed my testing up somehow, so double-checking is a good idea.

akhuang added inline comments.Sep 30 2021, 2:54 PM
clang/lib/Sema/SemaExprCXX.cpp
6682

Whoops, you're right, I was not specifying sptr/uptr and I guess clang and msvc have different defaults for sign/zero extension.

Will add tests. Also, thanks for reviewing!

clang/test/Sema/MicrosoftExtensions.cpp
10–11

Hmm, it seems reasonable, but I also don't know how motivated I am to add a diagnostic --

aaron.ballman added inline comments.Oct 1 2021, 8:49 AM
clang/test/Sema/MicrosoftExtensions.cpp
10–11

I don't insist -- we are missing that warning in general here (we don't warn on assignment yet). But it might be a nice follow-up for anyone who's interested in working on it.

aaron.ballman accepted this revision.Oct 5 2021, 5:16 AM

LGTM, thank you!

This revision is now accepted and ready to land.Oct 5 2021, 5:16 AM