[clang] d1d93da - [Clang] Fix __ptr32 arguments passed to builtins

Kai Nacke via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 6 11:53:53 PST 2023


Author: Ariel Burton
Date: 2023-02-06T19:53:13Z
New Revision: d1d93da701b1609bdd992e57be60bb58c6d080cc

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

LOG: [Clang] Fix __ptr32 arguments passed to builtins

Currently when clang deals with a call to a builtin function that
is supplied with an argument that has an explicit address space
it rewrites the signature of the callee to make the types of
the formal parameters match those of the actual arguments.
This functionality was added to support OpenCL, and was
introduced with commit b919c7d.

However, this does not work properly for "size" related address
spaces such as those used for __ptr32. This affects platforms
like Microsoft and z/OS.

This change preserves the OpenCL functionality, but will use
the formal parameter types when an address space is size-related.

Reviewed By: akhuang

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

Added: 
    

Modified: 
    clang/lib/Sema/SemaExpr.cpp
    clang/test/CodeGen/address-space-ptr32.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 2842add2cc4af..0c4fa43d88061 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6655,10 +6655,10 @@ static FunctionDecl *rewriteBuiltinFunctionDecl(Sema *Sema, ASTContext &Context,
       return nullptr;
     Expr *Arg = ArgRes.get();
     QualType ArgType = Arg->getType();
-    if (!ParamType->isPointerType() ||
-        ParamType.hasAddressSpace() ||
+    if (!ParamType->isPointerType() || ParamType.hasAddressSpace() ||
         !ArgType->isPointerType() ||
-        !ArgType->getPointeeType().hasAddressSpace()) {
+        !ArgType->getPointeeType().hasAddressSpace() ||
+        isPtrSizeAddressSpace(ArgType->getPointeeType().getAddressSpace())) {
       OverloadParams.push_back(ParamType);
       continue;
     }

diff  --git a/clang/test/CodeGen/address-space-ptr32.c b/clang/test/CodeGen/address-space-ptr32.c
index 2ed1d866b8e08..ead9550e4a64f 100644
--- a/clang/test/CodeGen/address-space-ptr32.c
+++ b/clang/test/CodeGen/address-space-ptr32.c
@@ -38,3 +38,31 @@ int fugu(void) {
   int_star __ptr32 p;
   return sizeof(p);
 }
+
+typedef __SIZE_TYPE__ size_t;
+size_t strlen(const char *);
+
+size_t test_calling_strlen_with_32_bit_pointer ( char *__ptr32 s ) {
+  // CHECK-LABEL: define dso_local i64 @test_calling_strlen_with_32_bit_pointer(ptr addrspace(270) noundef %s)
+  // CHECK-NEXT: entry:
+  // CHECK-NEXT:   %s.addr = alloca ptr addrspace(270), align 4
+  // CHECK-NEXT:   store ptr addrspace(270) %s, ptr %s.addr, align 4
+  // CHECK-NEXT:   %0 = load ptr addrspace(270), ptr %s.addr, align 4
+  // CHECK-NEXT:   %1 = addrspacecast ptr addrspace(270) %0 to ptr
+  // CHECK-NEXT:   %call = call i64 @strlen(ptr  noundef %1)
+  // CHECK-NEXT:   ret i64 %call
+   return strlen ( s );
+}
+
+// CHECK-LABEL: declare dso_local i64 @strlen(ptr noundef)
+
+size_t test_calling_strlen_with_64_bit_pointer ( char *s ) {
+  // CHECK-LABEL: define dso_local i64 @test_calling_strlen_with_64_bit_pointer(ptr noundef %s)
+  // CHECK-NEXT: entry:
+  // CHECK-NEXT:   %s.addr = alloca ptr, align 8
+  // CHECK-NEXT:   store ptr %s, ptr %s.addr, align 8
+  // CHECK-NEXT:   %0 = load ptr, ptr %s.addr, align 8
+  // CHECK-NEXT:   %call = call i64 @strlen(ptr noundef %0)
+  // CHECK-NEXT:   ret i64 %call
+  return strlen ( s );
+}


        


More information about the cfe-commits mailing list