Skip to content

Commit 42ae7eb

Browse files
author
Erich Keane
committed
Ensure field-annotations on pointers properly match the AS of the field.
Discovered in SYCL, the field annotations were always cast to an i8*, which is an invalid bitcast for a pointer type with an address space. This patch makes sure that we create an intrinsic that takes a pointer to the correct address-space and properly do our casts. Differential Revision: https://reviews.llvm.org/D109003
1 parent 9b6c813 commit 42ae7eb

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,15 +2405,19 @@ Address CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D,
24052405
assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
24062406
llvm::Value *V = Addr.getPointer();
24072407
llvm::Type *VTy = V->getType();
2408-
llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
2409-
CGM.Int8PtrTy);
2408+
auto *PTy = dyn_cast<llvm::PointerType>(VTy);
2409+
unsigned AS = PTy ? PTy->getAddressSpace() : 0;
2410+
llvm::PointerType *IntrinTy =
2411+
llvm::PointerType::getWithSamePointeeType(CGM.Int8PtrTy, AS);
2412+
llvm::Function *F =
2413+
CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation, IntrinTy);
24102414

24112415
for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
24122416
// FIXME Always emit the cast inst so we can differentiate between
24132417
// annotation on the first field of a struct and annotation on the struct
24142418
// itself.
2415-
if (VTy != CGM.Int8PtrTy)
2416-
V = Builder.CreateBitCast(V, CGM.Int8PtrTy);
2419+
if (VTy != IntrinTy)
2420+
V = Builder.CreateBitCast(V, IntrinTy);
24172421
V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation(), I);
24182422
V = Builder.CreateBitCast(V, VTy);
24192423
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -triple spir64 -fsycl-is-device -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s
2+
3+
// CHECK: [[ANNOT:.+]] = private unnamed_addr constant {{.*}}c"my_annotation\00"
4+
5+
struct HasField {
6+
// This caused an assertion on creating a bitcast here,
7+
// since the address space didn't match.
8+
[[clang::annotate("my_annotation")]]
9+
int *a;
10+
};
11+
12+
void foo(int *b) {
13+
struct HasField f;
14+
// CHECK: %[[A:.+]] = getelementptr inbounds %struct.HasField, %struct.HasField addrspace(4)* %{{.+}}
15+
// CHECK: %[[BITCAST:.+]] = bitcast i32 addrspace(4)* addrspace(4)* %[[A]] to i8 addrspace(4)*
16+
// CHECK: %[[CALL:.+]] = call i8 addrspace(4)* @llvm.ptr.annotation.p4i8(i8 addrspace(4)* %[[BITCAST]], i8* getelementptr inbounds ([14 x i8], [14 x i8]* [[ANNOT]]
17+
// CHECK: bitcast i8 addrspace(4)* %[[CALL]] to i32 addrspace(4)* addrspace(4)*
18+
f.a = b;
19+
}

0 commit comments

Comments
 (0)