[clang] c7104e5 - [Sema] Allow comparisons between different ms ptr size address space types.

Amy Huang via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 5 10:56:39 PDT 2021


Author: Amy Huang
Date: 2021-10-05T10:56:29-07:00
New Revision: c7104e506619b551ee7ab888a040114f260e8cb5

URL: https://github.com/llvm/llvm-project/commit/c7104e506619b551ee7ab888a040114f260e8cb5
DIFF: https://github.com/llvm/llvm-project/commit/c7104e506619b551ee7ab888a040114f260e8cb5.diff

LOG: [Sema] Allow comparisons between different ms ptr size address space 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

Added: 
    clang/test/Sema/MicrosoftExtensions.cpp

Modified: 
    clang/lib/Sema/SemaExprCXX.cpp
    clang/test/CodeGen/ms-mixed-ptr-sizes.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 07d27aaf7bdc..1b4c49d7880b 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -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 {

diff  --git a/clang/test/CodeGen/ms-mixed-ptr-sizes.c b/clang/test/CodeGen/ms-mixed-ptr-sizes.c
index 08e4a5f81cff..ececa42a4c4d 100644
--- a/clang/test/CodeGen/ms-mixed-ptr-sizes.c
+++ b/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;
@@ -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);
+}

diff  --git a/clang/test/Sema/MicrosoftExtensions.cpp b/clang/test/Sema/MicrosoftExtensions.cpp
new file mode 100644
index 000000000000..cb9ad2048633
--- /dev/null
+++ b/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);
+}


        


More information about the cfe-commits mailing list