Skip to content

Commit

Permalink
[Sema] Allow comparisons between different ms ptr size address space …
Browse files Browse the repository at this point in the history
…types.

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

Differential Revision: https://reviews.llvm.org/D110670
  • Loading branch information
amykhuang committed Oct 5, 2021
1 parent 2e5daac commit c7104e5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
11 changes: 9 additions & 2 deletions clang/lib/Sema/SemaExprCXX.cpp
Expand Up @@ -6674,8 +6674,15 @@ QualType Sema::FindCompositePointerType(SourceLocation Loc,
} else if (Steps.size() == 1) {
bool MaybeQ1 = Q1.isAddressSpaceSupersetOf(Q2);
bool MaybeQ2 = Q2.isAddressSpaceSupersetOf(Q1);
if (MaybeQ1 == MaybeQ2)
return QualType(); // No unique best address space.
if (MaybeQ1 == MaybeQ2) {
// Exception for ptr size address spaces. Should be able to choose
// either address space during comparison.
if (isPtrSizeAddressSpace(Q1.getAddressSpace()) ||
isPtrSizeAddressSpace(Q2.getAddressSpace()))
MaybeQ1 = true;
else
return QualType(); // No unique best address space.
}
Quals.setAddressSpace(MaybeQ1 ? Q1.getAddressSpace()
: Q2.getAddressSpace());
} else {
Expand Down
40 changes: 38 additions & 2 deletions clang/test/CodeGen/ms-mixed-ptr-sizes.c
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -triple x86_64-windows-msvc -fms-extensions -emit-llvm -O2 < %s | FileCheck %s --check-prefix=X64
// RUN: %clang_cc1 -triple i386-pc-win32 -fms-extensions -emit-llvm -O2 < %s | FileCheck %s --check-prefix=X86
// RUN: %clang_cc1 -triple x86_64-windows-msvc -fms-extensions -emit-llvm -O2 < %s | FileCheck %s --check-prefixes=X64,ALL
// RUN: %clang_cc1 -triple i386-pc-win32 -fms-extensions -emit-llvm -O2 < %s | FileCheck %s --check-prefixes=X86,ALL

struct Foo {
int * __ptr32 p32;
Expand Down Expand Up @@ -47,3 +47,39 @@ void test_other(struct Foo *f, __attribute__((address_space(10))) int *i) {
f->p32 = (int * __ptr32)i;
use_foo(f);
}

int test_compare1(int *__ptr32 __uptr i, int *__ptr64 j) {
// ALL-LABEL: define dso_local i32 @test_compare1
// X64: %{{.+}} = addrspacecast i32* %j to i32 addrspace(271)*
// X64: %cmp = icmp eq i32 addrspace(271)* %{{.+}}, %i
// X86: %{{.+}} = addrspacecast i32 addrspace(272)* %j to i32 addrspace(271)*
// X86: %cmp = icmp eq i32 addrspace(271)* %{{.+}}, %i
return (i == j);
}

int test_compare2(int *__ptr32 __sptr i, int *__ptr64 j) {
// ALL-LABEL: define dso_local i32 @test_compare2
// X64: %{{.+}} = addrspacecast i32* %j to i32 addrspace(270)*
// X64: %cmp = icmp eq i32 addrspace(270)* %{{.+}}, %i
// X86: %{{.+}} = addrspacecast i32 addrspace(272)* %j to i32*
// X86: %cmp = icmp eq i32* %{{.+}}, %i
return (i == j);
}

int test_compare3(int *__ptr32 __uptr i, int *__ptr64 j) {
// ALL-LABEL: define dso_local i32 @test_compare3
// X64: %{{.+}} = addrspacecast i32 addrspace(271)* %i to i32*
// X64: %cmp = icmp eq i32* %{{.+}}, %j
// X86: %{{.+}} = addrspacecast i32 addrspace(271)* %i to i32 addrspace(272)*
// X86: %cmp = icmp eq i32 addrspace(272)* %{{.+}}, %j
return (j == i);
}

int test_compare4(int *__ptr32 __sptr i, int *__ptr64 j) {
// ALL-LABEL: define dso_local i32 @test_compare4
// X64: %{{.+}} = addrspacecast i32 addrspace(270)* %i to i32*
// X64: %cmp = icmp eq i32* %{{.+}}, %j
// X86: %{{.+}} = addrspacecast i32* %i to i32 addrspace(272)*
// X86: %cmp = icmp eq i32 addrspace(272)* %{{.+}}, %j
return (j == i);
}
11 changes: 11 additions & 0 deletions clang/test/Sema/MicrosoftExtensions.cpp
@@ -0,0 +1,11 @@
// RUN: %clang_cc1 -triple i686-windows %s -fsyntax-only -Wmicrosoft -verify -fms-extensions
// RUN: %clang_cc1 -triple x86_64-windows %s -fsyntax-only -Wmicrosoft -verify -fms-extensions
// expected-no-diagnostics

// Check that __ptr32/__ptr64 can be compared.
int test_ptr_comparison(int *__ptr32 __uptr p32u, int *__ptr32 __sptr p32s,
int *__ptr64 p64) {
return (p32u == p32s) +
(p32u == p64) +
(p32s == p64);
}

0 comments on commit c7104e5

Please sign in to comment.