Skip to content

[SYCL][Clang] Fix annotations applied to integer fields #8198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2789,19 +2789,21 @@ Address CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D,
llvm::PointerType *IntrinTy =
llvm::PointerType::getWithSamePointeeType(CGM.Int8PtrTy, AS);

llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
{IntrinTy, CGM.ConstGlobalsPtrTy});

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

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

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

llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
{IntrinTy, CGM.ConstGlobalsPtrTy});

for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
V = Builder.CreateBitCast(V, IntrinTy);
V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation(), I);
Expand Down
37 changes: 37 additions & 0 deletions clang/test/CodeGenSYCL/annotations-field-no-opaque.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: %clang_cc1 -fsycl-is-device -triple spir64-unknown-unknown -emit-llvm -no-opaque-pointers -o - %s | FileCheck %s

// This test checks that clang emits @llvm.ptr.annotation intrinsic correctly
// when attribute annotate is applied to a struct field of integer type.

// CHECK: private unnamed_addr addrspace(1) constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
// CHECK: private unnamed_addr addrspace(1) constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
// CHECK: private unnamed_addr addrspace(1) constant [8 x i8] c"w_ann_{{.}}\00", section "llvm.metadata"
// CHECK: private unnamed_addr addrspace(1) constant [8 x i8] c"w_ann_{{.}}\00", section "llvm.metadata"
// CHECK: private unnamed_addr addrspace(1) constant [8 x i8] c"f_ann_{{.}}\00", section "llvm.metadata"
// CHECK: private unnamed_addr addrspace(1) constant [8 x i8] c"f_ann_{{.}}\00", section "llvm.metadata"

struct foo {
int v __attribute__((annotate("v_ann_0"))) __attribute__((annotate("v_ann_1")));
char w __attribute__((annotate("w_ann_0"))) __attribute__((annotate("w_ann_1")));
float f __attribute__((annotate("f_ann_0"))) __attribute__((annotate("f_ann_1")));
};

int __attribute__((sycl_device)) foo() {
struct foo f;
f.v = 1;
// CHECK: getelementptr inbounds %struct.foo, %struct.foo addrspace(4)* %{{.*}}, i32 0, i32 0
// CHECK-NEXT: call i32 addrspace(4)* @llvm.ptr.annotation.p4i32.p1i8({{.*}}str{{.*}}str{{.*}}i32 14, i8 addrspace(1)* null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this str a variable holding annotation?. Also what does 14 here refer to?

Just curious and for my own learning

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, str is a variable with annotation string. 14 seems to be a line of code on which annotation happened.

// CHECK-NEXT: call i32 addrspace(4)* @llvm.ptr.annotation.p4i32.p1i8({{.*}}str{{.*}}str{{.*}}i32 14, i8 addrspace(1)* null)
f.w = 42;
// CHECK: getelementptr inbounds %struct.foo, %struct.foo addrspace(4)* %{{.*}}, i32 0, i32 1
// CHECK-NEXT: call i8 addrspace(4)* @llvm.ptr.annotation.p4i8.p1i8({{.*}}str{{.*}}str{{.*}}i32 15, i8 addrspace(1)* null)
// CHECK-NEXT: call i8 addrspace(4)* @llvm.ptr.annotation.p4i8.p1i8({{.*}}str{{.*}}str{{.*}}i32 15, i8 addrspace(1)* null)
f.f = 0;
// CHECK: getelementptr inbounds %struct.foo, %struct.foo addrspace(4)* %{{.*}}, i32 0, i32 2
// CHECK-NEXT: bitcast float addrspace(4)* %{{.*}} to i8 addrspace(4)*
// CHECK-NEXT: call i8 addrspace(4)* @llvm.ptr.annotation.p4i8.p1i8({{.*}}str{{.*}}str{{.*}}i32 16, i8 addrspace(1)* null)
// CHECK-NEXT: bitcast i8 addrspace(4)* %{{.*}} to float addrspace(4)*
// CHECK-NEXT: bitcast float addrspace(4)* %{{.*}} to i8 addrspace(4)*
// CHECK-NEXT: call i8 addrspace(4)* @llvm.ptr.annotation.p4i8.p1i8({{.*}}str{{.*}}str{{.*}}i32 16, i8 addrspace(1)* null)
return 0;
}