Skip to content

Commit 56ef705

Browse files
authored
[SYCL][Clang] Fix annotations applied to integer fields (#8198)
Due to incorrect conflict resolution between 8e13852 (intel/llvm customization) and https://reviews.llvm.org/D138722 clang started emitting annotations applied to integer class/struct fields incorrectly. Incorrect overload of llvm.ptr.annotation intrinsic was picked up and this resulted in invalid IR emission. This patch restores behavior introduced by 8e13852. The existing test added for 8e13852 didn't catch the problem since it is modified upstream test `CodeGen/annotations-field.c` which doesn't test typed pointers.
1 parent 3758404 commit 56ef705

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,19 +2789,21 @@ Address CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D,
27892789
llvm::PointerType *IntrinTy =
27902790
llvm::PointerType::getWithSamePointeeType(CGM.Int8PtrTy, AS);
27912791

2792-
llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
2793-
{IntrinTy, CGM.ConstGlobalsPtrTy});
2794-
27952792
// llvm.ptr.annotation intrinsic accepts a pointer to integer of any width -
27962793
// don't perform bitcasts if value is integer
27972794
if (Addr.getElementType()->isIntegerTy()) {
2795+
llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
2796+
{VTy, CGM.ConstGlobalsPtrTy});
27982797

27992798
for (const auto *I : D->specific_attrs<AnnotateAttr>())
28002799
V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation(), I);
28012800

28022801
return Address(V, Addr.getElementType(), Addr.getAlignment());
28032802
}
28042803

2804+
llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
2805+
{IntrinTy, CGM.ConstGlobalsPtrTy});
2806+
28052807
for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
28062808
V = Builder.CreateBitCast(V, IntrinTy);
28072809
V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation(), I);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %clang_cc1 -fsycl-is-device -triple spir64-unknown-unknown -emit-llvm -no-opaque-pointers -o - %s | FileCheck %s
2+
3+
// This test checks that clang emits @llvm.ptr.annotation intrinsic correctly
4+
// when attribute annotate is applied to a struct field of integer type.
5+
6+
// CHECK: private unnamed_addr addrspace(1) constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
7+
// CHECK: private unnamed_addr addrspace(1) constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
8+
// CHECK: private unnamed_addr addrspace(1) constant [8 x i8] c"w_ann_{{.}}\00", section "llvm.metadata"
9+
// CHECK: private unnamed_addr addrspace(1) constant [8 x i8] c"w_ann_{{.}}\00", section "llvm.metadata"
10+
// CHECK: private unnamed_addr addrspace(1) constant [8 x i8] c"f_ann_{{.}}\00", section "llvm.metadata"
11+
// CHECK: private unnamed_addr addrspace(1) constant [8 x i8] c"f_ann_{{.}}\00", section "llvm.metadata"
12+
13+
struct foo {
14+
int v __attribute__((annotate("v_ann_0"))) __attribute__((annotate("v_ann_1")));
15+
char w __attribute__((annotate("w_ann_0"))) __attribute__((annotate("w_ann_1")));
16+
float f __attribute__((annotate("f_ann_0"))) __attribute__((annotate("f_ann_1")));
17+
};
18+
19+
int __attribute__((sycl_device)) foo() {
20+
struct foo f;
21+
f.v = 1;
22+
// CHECK: getelementptr inbounds %struct.foo, %struct.foo addrspace(4)* %{{.*}}, i32 0, i32 0
23+
// CHECK-NEXT: call i32 addrspace(4)* @llvm.ptr.annotation.p4i32.p1i8({{.*}}str{{.*}}str{{.*}}i32 14, i8 addrspace(1)* null)
24+
// CHECK-NEXT: call i32 addrspace(4)* @llvm.ptr.annotation.p4i32.p1i8({{.*}}str{{.*}}str{{.*}}i32 14, i8 addrspace(1)* null)
25+
f.w = 42;
26+
// CHECK: getelementptr inbounds %struct.foo, %struct.foo addrspace(4)* %{{.*}}, i32 0, i32 1
27+
// CHECK-NEXT: call i8 addrspace(4)* @llvm.ptr.annotation.p4i8.p1i8({{.*}}str{{.*}}str{{.*}}i32 15, i8 addrspace(1)* null)
28+
// CHECK-NEXT: call i8 addrspace(4)* @llvm.ptr.annotation.p4i8.p1i8({{.*}}str{{.*}}str{{.*}}i32 15, i8 addrspace(1)* null)
29+
f.f = 0;
30+
// CHECK: getelementptr inbounds %struct.foo, %struct.foo addrspace(4)* %{{.*}}, i32 0, i32 2
31+
// CHECK-NEXT: bitcast float addrspace(4)* %{{.*}} to i8 addrspace(4)*
32+
// CHECK-NEXT: call i8 addrspace(4)* @llvm.ptr.annotation.p4i8.p1i8({{.*}}str{{.*}}str{{.*}}i32 16, i8 addrspace(1)* null)
33+
// CHECK-NEXT: bitcast i8 addrspace(4)* %{{.*}} to float addrspace(4)*
34+
// CHECK-NEXT: bitcast float addrspace(4)* %{{.*}} to i8 addrspace(4)*
35+
// CHECK-NEXT: call i8 addrspace(4)* @llvm.ptr.annotation.p4i8.p1i8({{.*}}str{{.*}}str{{.*}}i32 16, i8 addrspace(1)* null)
36+
return 0;
37+
}

0 commit comments

Comments
 (0)