Skip to content

Commit 8e13852

Browse files
vmaksimobader
authored andcommitted
[SYCL] Don't emit extra bitcast for @llvm.ptr.annotation (#925)
llvm.ptr.annotation intrinsic has overloads for integers of any width. So, for integer type of intrinsic argument do not create a bitcast, and align type of the intrinsic's argument with i8 pointer in other cases. Signed-off-by: Viktoria Maksimova <[email protected]>
1 parent fac918c commit 8e13852

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,15 +2196,23 @@ Address CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D,
21962196
assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
21972197
llvm::Value *V = Addr.getPointer();
21982198
llvm::Type *VTy = V->getType();
2199+
2200+
// llvm.ptr.annotation intrinsic accepts a pointer to integer of any width -
2201+
// don't perform bitcasts if value is integer
2202+
if (VTy->getPointerElementType()->isIntegerTy()) {
2203+
llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation, VTy);
2204+
2205+
for (const auto *I : D->specific_attrs<AnnotateAttr>())
2206+
V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation());
2207+
2208+
return Address(V, Addr.getAlignment());
2209+
}
2210+
21992211
llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
22002212
CGM.Int8PtrTy);
22012213

22022214
for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2203-
// FIXME Always emit the cast inst so we can differentiate between
2204-
// annotation on the first field of a struct and annotation on the struct
2205-
// itself.
2206-
if (VTy != CGM.Int8PtrTy)
2207-
V = Builder.CreateBitCast(V, CGM.Int8PtrTy);
2215+
V = Builder.CreateBitCast(V, CGM.Int8PtrTy);
22082216
V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation());
22092217
V = Builder.CreateBitCast(V, VTy);
22102218
}

clang/test/CodeGen/annotations-field.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33

44
// CHECK: private unnamed_addr constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
55
// CHECK: private unnamed_addr constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
6+
// CHECK: private unnamed_addr constant [8 x i8] c"w_ann_{{.}}\00", section "llvm.metadata"
7+
// CHECK: private unnamed_addr constant [8 x i8] c"w_ann_{{.}}\00", section "llvm.metadata"
8+
// CHECK: private unnamed_addr constant [8 x i8] c"f_ann_{{.}}\00", section "llvm.metadata"
9+
// CHECK: private unnamed_addr constant [8 x i8] c"f_ann_{{.}}\00", section "llvm.metadata"
610

711
struct foo {
812
int v __attribute__((annotate("v_ann_0"))) __attribute__((annotate("v_ann_1")));
13+
char w __attribute__((annotate("w_ann_0"))) __attribute__((annotate("w_ann_1")));
14+
float f __attribute__((annotate("f_ann_0"))) __attribute__((annotate("f_ann_1")));
915
};
1016

1117
static struct foo gf;
@@ -14,13 +20,25 @@ int main(int argc, char **argv) {
1420
struct foo f;
1521
f.v = argc;
1622
// CHECK: getelementptr inbounds %struct.foo, %struct.foo* %f, i32 0, i32 0
17-
// CHECK-NEXT: bitcast i32* {{.*}} to i8*
18-
// CHECK-NEXT: call i8* @llvm.ptr.annotation.p0i8({{.*}}str{{.*}}str{{.*}}i32 8)
19-
// CHECK-NEXT: bitcast i8* {{.*}} to i32*
20-
// CHECK-NEXT: bitcast i32* {{.*}} to i8*
21-
// CHECK-NEXT: call i8* @llvm.ptr.annotation.p0i8({{.*}}str{{.*}}str{{.*}}i32 8)
22-
// CHECK-NEXT: bitcast i8* {{.*}} to i32*
23+
// CHECK-NEXT: call i32* @llvm.ptr.annotation.p0i32({{.*}}str{{.*}}str{{.*}}i32 12)
24+
// CHECK-NEXT: call i32* @llvm.ptr.annotation.p0i32({{.*}}str{{.*}}str{{.*}}i32 12)
25+
f.w = 42;
26+
// CHECK: getelementptr inbounds %struct.foo, %struct.foo* %f, i32 0, i32 1
27+
// CHECK-NEXT: call i8* @llvm.ptr.annotation.p0i8({{.*}}str{{.*}}str{{.*}}i32 13)
28+
// CHECK-NEXT: call i8* @llvm.ptr.annotation.p0i8({{.*}}str{{.*}}str{{.*}}i32 13)
29+
f.f = 0;
30+
// CHECK: getelementptr inbounds %struct.foo, %struct.foo* %f, i32 0, i32 2
31+
// CHECK-NEXT: bitcast float* {{.*}} to i8*
32+
// CHECK-NEXT: call i8* @llvm.ptr.annotation.p0i8({{.*}}str{{.*}}str{{.*}}i32 14)
33+
// CHECK-NEXT: bitcast i8* {{.*}} to float*
34+
// CHECK-NEXT: bitcast float* {{.*}} to i8*
35+
// CHECK-NEXT: call i8* @llvm.ptr.annotation.p0i8({{.*}}str{{.*}}str{{.*}}i32 14)
36+
// CHECK-NEXT: bitcast i8* {{.*}} to float*
2337
gf.v = argc;
24-
// CHECK: call i8* @llvm.ptr.annotation.p0i8(i8* bitcast (%struct.foo* @gf to i8*), {{.*}}str{{.*}}str{{.*}}i32 8)
38+
// CHECK: call i32* @llvm.ptr.annotation.p0i32(i32* getelementptr inbounds (%struct.foo, %struct.foo* @gf, i32 0, i32 0), {{.*}}str{{.*}}str{{.*}}i32 12)
39+
gf.w = 42;
40+
// CHECK: call i8* @llvm.ptr.annotation.p0i8(i8* getelementptr inbounds (%struct.foo, %struct.foo* @gf, i32 0, i32 1), {{.*}}str{{.*}}str{{.*}}i32 13)
41+
gf.f = 0;
42+
// CHECK: call i8* @llvm.ptr.annotation.p0i8(i8* bitcast (float* getelementptr inbounds (%struct.foo, %struct.foo* @gf, i32 0, i32 2) to i8*), {{.*}}str{{.*}}str{{.*}}i32 14)
2543
return 0;
2644
}

0 commit comments

Comments
 (0)