[clang] c6da9ec - clang: Fix assert on void pointer arithmetic with address_space

Matt Arsenault via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 31 20:09:21 PDT 2019


Author: Matt Arsenault
Date: 2019-10-31T20:07:23-07:00
New Revision: c6da9ec0e90ea8798ecae583bb8d26bdf6b9b79f

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

LOG: clang: Fix assert on void pointer arithmetic with address_space

This attempted to always use the default address space void pointer
type instead of preserving the source address space.

Added: 
    

Modified: 
    clang/lib/CodeGen/CGExprScalar.cpp
    clang/test/CodeGen/address-space.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 55a413a2a717..c1391d46f60c 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -3258,7 +3258,7 @@ static Value *emitPointerArithmetic(CodeGenFunction &CGF,
   // GNU void* casts amount to no-ops since our void* type is i8*, but this is
   // future proof.
   if (elementType->isVoidType() || elementType->isFunctionType()) {
-    Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy);
+    Value *result = CGF.EmitCastToVoidPtr(pointer);
     result = CGF.Builder.CreateGEP(result, index, "add.ptr");
     return CGF.Builder.CreateBitCast(result, pointer->getType());
   }

diff  --git a/clang/test/CodeGen/address-space.c b/clang/test/CodeGen/address-space.c
index d6a40f33029f..a76d2e743e6e 100644
--- a/clang/test/CodeGen/address-space.c
+++ b/clang/test/CodeGen/address-space.c
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm < %s | FileCheck -check-prefixes=CHECK,X86 %s
-// RUN: %clang_cc1 -triple amdgcn -emit-llvm < %s | FileCheck -check-prefixes=CHECK,AMDGCN %s
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm < %s | FileCheck -enable-var-scope -check-prefixes=CHECK,X86 %s
+// RUN: %clang_cc1 -triple amdgcn -emit-llvm < %s | FileCheck -enable-var-scope -check-prefixes=CHECK,AMDGCN %s
 
 // CHECK: @foo = common addrspace(1) global
 int foo __attribute__((address_space(1)));
@@ -10,11 +10,11 @@ int ban[10] __attribute__((address_space(1)));
 // CHECK: @a = common global
 int a __attribute__((address_space(0)));
 
-// CHECK-LABEL: define i32 @test1() 
+// CHECK-LABEL: define i32 @test1()
 // CHECK: load i32, i32 addrspace(1)* @foo
 int test1() { return foo; }
 
-// CHECK-LABEL: define i32 @test2(i32 %i) 
+// CHECK-LABEL: define i32 @test2(i32 %i)
 // CHECK: load i32, i32 addrspace(1)*
 // CHECK-NEXT: ret i32
 int test2(int i) { return ban[i]; }
@@ -45,3 +45,17 @@ void test4(MyStruct __attribute__((address_space(2))) *pPtr) {
   MyStruct s = pPtr[0];
   pPtr[0] = s;
 }
+
+// Make sure the right address space is used when doing arithmetic on a void
+// pointer. Make sure no invalid bitcast is introduced.
+
+// CHECK-LABEL: @void_ptr_arithmetic_test(
+// X86: [[ALLOCA:%.*]] = alloca i8 addrspace(1)*
+// X86-NEXT: store i8 addrspace(1)* %arg, i8 addrspace(1)** [[ALLOCA]]
+// X86-NEXT: load i8 addrspace(1)*, i8 addrspace(1)** [[ALLOCA]]
+// X86-NEXT: getelementptr i8, i8 addrspace(1)*
+// X86-NEXT: ret i8 addrspace(1)*
+void __attribute__((address_space(1)))*
+void_ptr_arithmetic_test(void __attribute__((address_space(1))) *arg) {
+    return arg + 4;
+}


        


More information about the cfe-commits mailing list