Fix address space for function pointers with qualifier
authorElizabeth Andrews <elizabeth.andrews@intel.com>
Fri, 4 Feb 2022 23:45:35 +0000 (15:45 -0800)
committerElizabeth Andrews <elizabeth.andrews@intel.com>
Mon, 7 Feb 2022 20:53:24 +0000 (12:53 -0800)
This patch fixes a bug introduced in commit 4eaf5846d0e7. Commit
4eaf5846d0e7 sets address space of function type as program
address space unconditionally. This breaks types which have
address space qualifiers. E.g. __ptr32.

This patch fixes the bug by using address space qualifiers if
present.

Differential Revision: https://reviews.llvm.org/D119045

clang/lib/AST/ASTContext.cpp
clang/test/CodeGen/address-space-ptr32.c [new file with mode: 0644]

index 5fa2d46..f2ac574 100644 (file)
@@ -11959,8 +11959,13 @@ uint64_t ASTContext::getTargetNullPointerValue(QualType QT) const {
 }
 
 unsigned ASTContext::getTargetAddressSpace(QualType T) const {
-  return T->isFunctionType() ? getTargetInfo().getProgramAddressSpace()
-                             : getTargetAddressSpace(T.getQualifiers());
+  // Return the address space for the type. If the type is a
+  // function type without an address space qualifier, the
+  // program address space is used. Otherwise, the target picks
+  // the best address space based on the type information
+  return T->isFunctionType() && !T.hasAddressSpace()
+             ? getTargetInfo().getProgramAddressSpace()
+             : getTargetAddressSpace(T.getQualifiers());
 }
 
 unsigned ASTContext::getTargetAddressSpace(Qualifiers Q) const {
diff --git a/clang/test/CodeGen/address-space-ptr32.c b/clang/test/CodeGen/address-space-ptr32.c
new file mode 100644 (file)
index 0000000..7684fdc
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -fms-extensions -emit-llvm < %s | FileCheck %s
+
+int foo(void) {
+  int (*__ptr32 a)(int);
+  return sizeof(a);
+}
+
+// CHECK: define dso_local i32 @foo
+// CHECK: %a = alloca i32 (i32) addrspace(270)*, align 4
+// CHECK: ret i32 4