Skip to content

Commit 9114ac6

Browse files
committed
Overload all llvm.annotation intrinsics for globals argument
The global constant arguments could be in a different address space than the first argument, so we have to add another overloaded argument. This patch was originally made for CHERI LLVM (where globals can be in address space 200), but it also appears to be useful for in-tree targets as can be seen from the test diffs. Differential Revision: https://reviews.llvm.org/D138722
1 parent e114979 commit 9114ac6

23 files changed

+117
-90
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4258,8 +4258,9 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
42584258
}
42594259
case Builtin::BI__builtin_annotation: {
42604260
llvm::Value *AnnVal = EmitScalarExpr(E->getArg(0));
4261-
llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::annotation,
4262-
AnnVal->getType());
4261+
llvm::Function *F =
4262+
CGM.getIntrinsic(llvm::Intrinsic::annotation,
4263+
{AnnVal->getType(), CGM.ConstGlobalsPtrTy});
42634264

42644265
// Get the annotation string, go through casts. Sema requires this to be a
42654266
// non-wide string literal, potentially casted, so the cast<> is safe.

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,8 +2460,10 @@ llvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Function *AnnotationFn,
24602460
const AnnotateAttr *Attr) {
24612461
SmallVector<llvm::Value *, 5> Args = {
24622462
AnnotatedVal,
2463-
Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy),
2464-
Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy),
2463+
Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr),
2464+
ConstGlobalsPtrTy),
2465+
Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location),
2466+
ConstGlobalsPtrTy),
24652467
CGM.EmitAnnotationLineNo(Location),
24662468
};
24672469
if (Attr)
@@ -2473,9 +2475,12 @@ void CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) {
24732475
assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
24742476
// FIXME We create a new bitcast for every annotation because that's what
24752477
// llvm-gcc was doing.
2478+
unsigned AS = V->getType()->getPointerAddressSpace();
2479+
llvm::Type *I8PtrTy = Builder.getInt8PtrTy(AS);
24762480
for (const auto *I : D->specific_attrs<AnnotateAttr>())
2477-
EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation),
2478-
Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()),
2481+
EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation,
2482+
{I8PtrTy, CGM.ConstGlobalsPtrTy}),
2483+
Builder.CreateBitCast(V, I8PtrTy, V->getName()),
24792484
I->getAnnotation(), D->getLocation(), I);
24802485
}
24812486

@@ -2488,8 +2493,8 @@ Address CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D,
24882493
unsigned AS = PTy ? PTy->getAddressSpace() : 0;
24892494
llvm::PointerType *IntrinTy =
24902495
llvm::PointerType::getWithSamePointeeType(CGM.Int8PtrTy, AS);
2491-
llvm::Function *F =
2492-
CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation, IntrinTy);
2496+
llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
2497+
{IntrinTy, CGM.ConstGlobalsPtrTy});
24932498

24942499
for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
24952500
// FIXME Always emit the cast inst so we can differentiate between

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ CodeGenModule::CodeGenModule(ASTContext &C,
141141
const llvm::DataLayout &DL = M.getDataLayout();
142142
AllocaInt8PtrTy = Int8Ty->getPointerTo(DL.getAllocaAddrSpace());
143143
GlobalsInt8PtrTy = Int8Ty->getPointerTo(DL.getDefaultGlobalsAddressSpace());
144+
ConstGlobalsPtrTy = Int8Ty->getPointerTo(
145+
C.getTargetAddressSpace(GetGlobalConstantAddressSpace()));
144146
ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace();
145147

146148
// Build C++20 Module initializers.
@@ -2812,9 +2814,10 @@ llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
28122814

28132815
// Not found yet, create a new global.
28142816
llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
2815-
auto *gv =
2816-
new llvm::GlobalVariable(getModule(), s->getType(), true,
2817-
llvm::GlobalValue::PrivateLinkage, s, ".str");
2817+
auto *gv = new llvm::GlobalVariable(
2818+
getModule(), s->getType(), true, llvm::GlobalValue::PrivateLinkage, s,
2819+
".str", nullptr, llvm::GlobalValue::NotThreadLocal,
2820+
ConstGlobalsPtrTy->getAddressSpace());
28182821
gv->setSection(AnnotationSection);
28192822
gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
28202823
AStr = gv;
@@ -2840,7 +2843,7 @@ llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) {
28402843
llvm::Constant *CodeGenModule::EmitAnnotationArgs(const AnnotateAttr *Attr) {
28412844
ArrayRef<Expr *> Exprs = {Attr->args_begin(), Attr->args_size()};
28422845
if (Exprs.empty())
2843-
return llvm::ConstantPointerNull::get(GlobalsInt8PtrTy);
2846+
return llvm::ConstantPointerNull::get(ConstGlobalsPtrTy);
28442847

28452848
llvm::FoldingSetNodeID ID;
28462849
for (Expr *E : Exprs) {
@@ -2890,8 +2893,8 @@ llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
28902893
// Create the ConstantStruct for the global annotation.
28912894
llvm::Constant *Fields[] = {
28922895
llvm::ConstantExpr::getBitCast(GVInGlobalsAS, GlobalsInt8PtrTy),
2893-
llvm::ConstantExpr::getBitCast(AnnoGV, GlobalsInt8PtrTy),
2894-
llvm::ConstantExpr::getBitCast(UnitGV, GlobalsInt8PtrTy),
2896+
llvm::ConstantExpr::getBitCast(AnnoGV, ConstGlobalsPtrTy),
2897+
llvm::ConstantExpr::getBitCast(UnitGV, ConstGlobalsPtrTy),
28952898
LineNoCst,
28962899
Args,
28972900
};

clang/lib/CodeGen/CodeGenTypeCache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ struct CodeGenTypeCache {
7575
llvm::PointerType *GlobalsInt8PtrTy;
7676
};
7777

78+
/// void* in the address space for constant globals
79+
llvm::PointerType *ConstGlobalsPtrTy;
80+
7881
/// The size and alignment of the builtin C type 'int'. This comes
7982
/// up enough in various ABI lowering tasks to be worth pre-computing.
8083
union {
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
// RUN: %clang_cc1 -emit-llvm -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple x86_64 -emit-llvm -o - %s | FileCheck %s '-D$MANGLE_AS=p0' '-D$CONST_AS=' --check-prefix=X86
2+
// RUN: %clang_cc1 -triple amdgcn -emit-llvm -o - %s | FileCheck %s '-D$MANGLE_AS=p4' '-D$CONST_AS= addrspace(4)' --check-prefix=AMDGPU
23
// END.
34

4-
// CHECK: private unnamed_addr constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
5-
// CHECK: private unnamed_addr constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
5+
// CHECK: private unnamed_addr[[$CONST_AS]] constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
6+
// CHECK: private unnamed_addr[[$CONST_AS]] constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
67

78
struct foo {
89
int v __attribute__((annotate("v_ann_0"))) __attribute__((annotate("v_ann_1")));
@@ -13,10 +14,13 @@ static struct foo gf;
1314
int main(int argc, char **argv) {
1415
struct foo f;
1516
f.v = argc;
16-
// CHECK: getelementptr inbounds %struct.foo, ptr %f, i32 0, i32 0
17-
// CHECK-NEXT: call ptr @llvm.ptr.annotation.p0({{.*}}str{{.*}}str{{.*}}i32 8, ptr null)
18-
// CHECK-NEXT: call ptr @llvm.ptr.annotation.p0({{.*}}str{{.*}}str{{.*}}i32 8, ptr null)
17+
// CHECK: getelementptr inbounds %struct.foo, ptr %{{.*}}, i32 0, i32 0
18+
// CHECK-NEXT: call ptr @llvm.ptr.annotation.p0.[[$MANGLE_AS]]({{.*}}str{{.*}}str{{.*}}i32 9, ptr[[$CONST_AS]] null)
19+
// CHECK-NEXT: call ptr @llvm.ptr.annotation.p0.[[$MANGLE_AS]]({{.*}}str{{.*}}str{{.*}}i32 9, ptr[[$CONST_AS]] null)
1920
gf.v = argc;
20-
// CHECK: call ptr @llvm.ptr.annotation.p0(ptr @gf, {{.*}}str{{.*}}str{{.*}}i32 8, ptr null)
21+
// X86: call ptr @llvm.ptr.annotation.p0.p0(ptr @gf, ptr @.str{{.*}}, ptr @.str{{.*}}, i32 9, ptr null)
22+
// X86-NEXT: call ptr @llvm.ptr.annotation.p0.p0(ptr %{{.*}}, ptr @.str{{.*}}, ptr @.str{{.*}}, i32 9, ptr null)
23+
// AMDGPU: call ptr @llvm.ptr.annotation.p0.p4(ptr addrspacecast (ptr addrspace(1) @gf to ptr), ptr addrspace(4) @.str{{.*}}, ptr addrspace(4) @.str{{.*}}, i32 9, ptr addrspace(4) null)
24+
// AMDGPU-NEXT: call ptr @llvm.ptr.annotation.p0.p4(ptr %{{.*}}, ptr addrspace(4) @.str{{.*}}, ptr addrspace(4) @.str{{.*}}, i32 9, ptr addrspace(4) null)
2125
return 0;
2226
}

clang/test/CodeGen/annotations-global.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// RUN: %clang_cc1 %s -emit-llvm -o -
12
// RUN: %clang_cc1 %s -emit-llvm -o %t1
23
// RUN: FileCheck --check-prefix=FOO %s < %t1
34
// RUN: FileCheck --check-prefix=A %s < %t1
@@ -48,6 +49,6 @@ __attribute((address_space(1))) __attribute__((annotate("addrspace1_ann"))) char
4849
// ADDRSPACE: @llvm.global.annotations = appending global {{.*}} addrspacecast (ptr addrspace(1) @addrspace1_var to ptr), {{.*}}
4950

5051
// AS1-GLOBALS: target datalayout = "{{.+}}-A5-G1"
51-
// AS1-GLOBALS: @llvm.global.annotations = appending addrspace(1) global [11 x { ptr addrspace(1), ptr addrspace(1), ptr addrspace(1), i32, ptr addrspace(1) }]
52+
// AS1-GLOBALS: @llvm.global.annotations = appending addrspace(1) global [11 x { ptr addrspace(1), ptr addrspace(4), ptr addrspace(4), i32, ptr addrspace(4) }]
5253
// AS1-GLOBALS-SAME: { ptr addrspace(1) @a.bar,
5354
// AS1-GLOBALS-SAME: { ptr addrspace(1) @addrspace1_var,

clang/test/CodeGen/annotations-loc.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// RUN: %clang_cc1 -emit-llvm -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple x86_64 -emit-llvm -o - %s | FileCheck %s '-D$CONST_AS='
2+
// RUN: %clang_cc1 -triple amdgcn -emit-llvm -o - %s | FileCheck %s '-D$CONST_AS= addrspace(4)'
23
// END.
34
# 1 "t.c"
45
# 1 "<built-in>"
56
# 1 "<command-line>"
67
# 1 "t.c"
78
int __attribute((annotate("foo"))) foo(void) { return 0; }
89

9-
// CHECK: private unnamed_addr constant [4 x i8] c"t.c\00"
10-
// CHECK: @llvm.global.annotations = {{.*}}, i32 1, ptr null }
10+
// CHECK: private unnamed_addr[[$CONST_AS]] constant [4 x i8] c"t.c\00"
11+
// CHECK: @llvm.global.annotations = {{.*}}, i32 1, ptr[[$CONST_AS]] null }

clang/test/CodeGen/annotations-var.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ int foo(int v __attribute__((annotate("param_ann_0"))) __attribute__((annotate("
2020
// PARAM: define {{.*}}@foo
2121
// PARAM: [[V:%.*]] = alloca i32
2222
// PARAM: bitcast i32* [[V]] to i8*
23-
// PARAM-NEXT: call void @llvm.var.annotation(
23+
// PARAM-NEXT: call void @llvm.var.annotation.p0i8.p0i8(
2424
// PARAM-NEXT: bitcast i32* [[V]] to i8*
25-
// PARAM-NEXT: call void @llvm.var.annotation(
25+
// PARAM-NEXT: call void @llvm.var.annotation.p0i8.p0i8(
2626
// PARAM-NEXT: bitcast i32* [[V]] to i8*
27-
// PARAM-NEXT: call void @llvm.var.annotation(
27+
// PARAM-NEXT: call void @llvm.var.annotation.p0i8.p0i8(
2828
// PARAM-NEXT: bitcast i32* [[V]] to i8*
29-
// PARAM-NEXT: call void @llvm.var.annotation(
29+
// PARAM-NEXT: call void @llvm.var.annotation.p0i8.p0i8(
3030
}
3131

3232
void local(void) {
3333
int localvar __attribute__((annotate("localvar_ann_0"))) __attribute__((annotate("localvar_ann_1"))) = 3;
3434
// LOCAL-LABEL: define{{.*}} void @local()
3535
// LOCAL: [[LOCALVAR:%.*]] = alloca i32,
3636
// LOCAL-NEXT: [[T0:%.*]] = bitcast i32* [[LOCALVAR]] to i8*
37-
// LOCAL-NEXT: call void @llvm.var.annotation(i8* [[T0]], i8* getelementptr inbounds ([15 x i8], [15 x i8]* @{{.*}}), i8* getelementptr inbounds ({{.*}}), i32 33, i8* null)
37+
// LOCAL-NEXT: call void @llvm.var.annotation.p0i8.p0i8(i8* [[T0]], i8* getelementptr inbounds ([15 x i8], [15 x i8]* @{{.*}}), i8* getelementptr inbounds ({{.*}}), i32 33, i8* null)
3838
// LOCAL-NEXT: [[T0:%.*]] = bitcast i32* [[LOCALVAR]] to i8*
39-
// LOCAL-NEXT: call void @llvm.var.annotation(i8* [[T0]], i8* getelementptr inbounds ([15 x i8], [15 x i8]* @{{.*}}), i8* getelementptr inbounds ({{.*}}), i32 33, i8* null)
39+
// LOCAL-NEXT: call void @llvm.var.annotation.p0i8.p0i8(i8* [[T0]], i8* getelementptr inbounds ([15 x i8], [15 x i8]* @{{.*}}), i8* getelementptr inbounds ({{.*}}), i32 33, i8* null)
4040
}
4141

4242
void local_after_return(void) {
@@ -53,5 +53,5 @@ void undef(void) {
5353
// UNDEF-LABEL: define{{.*}} void @undef()
5454
// UNDEF: [[UNDEFVAR:%.*]] = alloca i32,
5555
// UNDEF-NEXT: [[T0:%.*]] = bitcast i32* [[UNDEFVAR]] to i8*
56-
// UNDEF-NEXT: call void @llvm.var.annotation(i8* [[T0]], i8* getelementptr inbounds ([15 x i8], [15 x i8]* @{{.*}}), i8* getelementptr inbounds ({{.*}}), i32 52, i8* null)
56+
// UNDEF-NEXT: call void @llvm.var.annotation.p0i8.p0i8(i8* [[T0]], i8* getelementptr inbounds ([15 x i8], [15 x i8]* @{{.*}}), i8* getelementptr inbounds ({{.*}}), i32 52, i8* null)
5757
}

clang/test/CodeGenCXX/attr-annotate.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ static B<int long, -1>::foo<unsigned, 9> gf;
5151
// CHECK-NEXT: store ptr [[ARGV:%.*]], ptr [[ARGV_ADDR]], align 8
5252
// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARGC_ADDR]], align 4
5353
// CHECK-NEXT: [[V:%.*]] = getelementptr inbounds %"struct.B<int, 7>::foo", ptr [[F]], i32 0, i32 0
54-
// CHECK-NEXT: [[TMP2:%.*]] = call ptr @llvm.ptr.annotation.p0(ptr [[V]], ptr @.str, ptr @.str.1, i32 {{.*}}, ptr @.args)
55-
// CHECK-NEXT: [[TMP5:%.*]] = call ptr @llvm.ptr.annotation.p0(ptr [[TMP2]], ptr @.str.3, ptr @.str.1, i32 {{.*}}, ptr @.args.4)
54+
// CHECK-NEXT: [[TMP2:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[V]], ptr @.str, ptr @.str.1, i32 {{.*}}, ptr @.args)
55+
// CHECK-NEXT: [[TMP5:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[TMP2]], ptr @.str.3, ptr @.str.1, i32 {{.*}}, ptr @.args.4)
5656
// CHECK-NEXT: store i32 [[TMP0]], ptr [[TMP5]], align 4
5757
// CHECK-NEXT: [[TMP7:%.*]] = load i32, ptr [[ARGC_ADDR]], align 4
58-
// CHECK-NEXT: [[TMP8:%.*]] = call ptr @llvm.ptr.annotation.p0(ptr @_ZL2gf, ptr @.str, ptr @.str.1, i32 {{.*}}, ptr @.args.5)
59-
// CHECK-NEXT: [[TMP11:%.*]] = call ptr @llvm.ptr.annotation.p0(ptr [[TMP8]], ptr @.str.3, ptr @.str.1, i32 {{.*}}, ptr @.args.4)
58+
// CHECK-NEXT: [[TMP8:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr @_ZL2gf, ptr @.str, ptr @.str.1, i32 {{.*}}, ptr @.args.5)
59+
// CHECK-NEXT: [[TMP11:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[TMP8]], ptr @.str.3, ptr @.str.1, i32 {{.*}}, ptr @.args.4)
6060
// CHECK-NEXT: store i32 [[TMP7]], ptr [[TMP11]], align 4
6161
// CHECK-NEXT: ret i32 0
6262
//

clang/test/CodeGenCXX/attr-annotate2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// CHECK-NEXT: [[N:%.*]] = alloca i32, align 4
1111
// CHECK-NEXT: [[J:%.*]] = alloca i32, align 4
1212
// CHECK-NEXT: store i32 10, ptr [[N]], align 4
13-
// CHECK-NEXT: call void @llvm.var.annotation(ptr [[J]], ptr @[[STR]], ptr @[[FILENAME]], i32 {{.*}}, ptr @[[ARGS]])
13+
// CHECK-NEXT: call void @llvm.var.annotation.p0.p0(ptr [[J]], ptr @[[STR]], ptr @[[FILENAME]], i32 {{.*}}, ptr @[[ARGS]])
1414
// CHECK-NEXT: store i32 0, ptr [[J]], align 4
1515
// CHECK-NEXT: ret void
1616
//

clang/test/CodeGenSYCL/field-annotate-addr-space.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %clang_cc1 -no-opaque-pointers -triple spir64 -fsycl-is-device -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s
22

3-
// CHECK: [[ANNOT:.+]] = private unnamed_addr constant {{.*}}c"my_annotation\00"
3+
// CHECK: [[ANNOT:.+]] = private unnamed_addr addrspace(1) constant {{.*}}c"my_annotation\00"
44

55
struct HasField {
66
// This caused an assertion on creating a bitcast here,
@@ -13,7 +13,7 @@ void foo(int *b) {
1313
struct HasField f;
1414
// CHECK: %[[A:.+]] = getelementptr inbounds %struct.HasField, %struct.HasField addrspace(4)* %{{.+}}
1515
// 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]]
16+
// CHECK: %[[CALL:.+]] = call i8 addrspace(4)* @llvm.ptr.annotation.p4i8.p1i8(i8 addrspace(4)* %[[BITCAST]], i8 addrspace(1)* getelementptr inbounds ([14 x i8], [14 x i8] addrspace(1)* [[ANNOT]]
1717
// CHECK: bitcast i8 addrspace(4)* %[[CALL]] to i32 addrspace(4)* addrspace(4)*
1818
f.a = b;
1919
}

llvm/include/llvm/IR/Intrinsics.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,17 +1049,17 @@ def int_eh_sjlj_setup_dispatch : Intrinsic<[], []>;
10491049
//===---------------- Generic Variable Attribute Intrinsics----------------===//
10501050
//
10511051
def int_var_annotation : DefaultAttrsIntrinsic<
1052-
[], [llvm_ptr_ty, llvm_ptr_ty, llvm_ptr_ty, llvm_i32_ty, llvm_ptr_ty],
1052+
[], [llvm_anyptr_ty, llvm_anyptr_ty, LLVMMatchType<1>, llvm_i32_ty, LLVMMatchType<1>],
10531053
[IntrInaccessibleMemOnly], "llvm.var.annotation">;
10541054

10551055
def int_ptr_annotation : DefaultAttrsIntrinsic<
10561056
[LLVMAnyPointerType<llvm_anyint_ty>],
1057-
[LLVMMatchType<0>, llvm_ptr_ty, llvm_ptr_ty, llvm_i32_ty, llvm_ptr_ty],
1057+
[LLVMMatchType<0>, llvm_anyptr_ty, LLVMMatchType<1>, llvm_i32_ty, LLVMMatchType<1>],
10581058
[IntrInaccessibleMemOnly], "llvm.ptr.annotation">;
10591059

10601060
def int_annotation : DefaultAttrsIntrinsic<
10611061
[llvm_anyint_ty],
1062-
[LLVMMatchType<0>, llvm_ptr_ty, llvm_ptr_ty, llvm_i32_ty],
1062+
[LLVMMatchType<0>, llvm_anyptr_ty, LLVMMatchType<1>, llvm_i32_ty],
10631063
[IntrInaccessibleMemOnly], "llvm.annotation">;
10641064

10651065
// Annotates the current program point with metadata strings which are emitted

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,9 +1093,9 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
10931093
}
10941094
} else if (Name.startswith("ptr.annotation.") && F->arg_size() == 4) {
10951095
rename(F);
1096-
NewFn = Intrinsic::getDeclaration(F->getParent(),
1097-
Intrinsic::ptr_annotation,
1098-
F->arg_begin()->getType());
1096+
NewFn = Intrinsic::getDeclaration(
1097+
F->getParent(), Intrinsic::ptr_annotation,
1098+
{F->arg_begin()->getType(), F->getArg(1)->getType()});
10991099
return true;
11001100
}
11011101
break;
@@ -1110,8 +1110,9 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
11101110
case 'v': {
11111111
if (Name == "var.annotation" && F->arg_size() == 4) {
11121112
rename(F);
1113-
NewFn = Intrinsic::getDeclaration(F->getParent(),
1114-
Intrinsic::var_annotation);
1113+
NewFn = Intrinsic::getDeclaration(
1114+
F->getParent(), Intrinsic::var_annotation,
1115+
{{F->arg_begin()->getType(), F->getArg(1)->getType()}});
11151116
return true;
11161117
}
11171118
break;
@@ -4146,13 +4147,17 @@ void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
41464147

41474148
case Intrinsic::var_annotation:
41484149
// Upgrade from versions that lacked the annotation attribute argument.
4149-
assert(CI->arg_size() == 4 &&
4150-
"Before LLVM 12.0 this intrinsic took four arguments");
4150+
if (CI->arg_size() != 4) {
4151+
DefaultCase();
4152+
return;
4153+
}
41514154
// Create a new call with an added null annotation attribute argument.
41524155
NewCall = Builder.CreateCall(
41534156
NewFn,
41544157
{CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2),
41554158
CI->getArgOperand(3), Constant::getNullValue(Builder.getInt8PtrTy())});
4159+
NewCall->takeName(CI);
4160+
CI->replaceAllUsesWith(NewCall);
41564161
CI->eraseFromParent();
41574162
return;
41584163

0 commit comments

Comments
 (0)