arichardson created this revision.
arichardson added reviewers: arsenm, bader, Tyker, nikic.
Herald added subscribers: jrtc27, hiraditya.
Herald added a project: All.
arichardson requested review of this revision.
Herald added subscribers: llvm-commits, cfe-commits, jdoerfert, wdng.
Herald added projects: clang, LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138722

Files:
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenTypeCache.h
  clang/test/CodeGen/annotations-field.c
  clang/test/CodeGen/annotations-global.c
  clang/test/CodeGen/annotations-loc.c
  clang/test/CodeGen/annotations-var.c
  clang/test/CodeGenCXX/attr-annotate.cpp
  clang/test/CodeGenCXX/attr-annotate2.cpp
  clang/test/CodeGenSYCL/field-annotate-addr-space.cpp
  llvm/include/llvm/IR/Intrinsics.td
  llvm/lib/IR/AutoUpgrade.cpp
  llvm/test/Analysis/CostModel/X86/free-intrinsics.ll
  llvm/test/Analysis/CostModel/free-intrinsics-datalayout.ll
  llvm/test/Analysis/CostModel/free-intrinsics-no_info.ll
  llvm/test/Analysis/ScalarEvolution/annotation-intrinsics.ll
  llvm/test/Assembler/opaque-ptr-intrinsic-remangling.ll
  llvm/test/Bitcode/upgrade-annotation.ll
  llvm/test/Bitcode/upgrade-annotation.ll.bc
  llvm/test/Bitcode/upgrade-ptr-annotation.ll
  llvm/test/Bitcode/upgrade-var-annotation.ll
  llvm/test/Transforms/InstCombine/annotation-intrinsic.ll
  llvm/test/Transforms/InstCombine/assume_inevitable.ll

Index: llvm/test/Transforms/InstCombine/assume_inevitable.ll
===================================================================
--- llvm/test/Transforms/InstCombine/assume_inevitable.ll
+++ llvm/test/Transforms/InstCombine/assume_inevitable.ll
@@ -10,11 +10,11 @@
 ; CHECK-NEXT:    [[M:%.*]] = alloca i64, align 8
 ; CHECK-NEXT:    [[TMP0:%.*]] = load i32, ptr [[A:%.*]], align 32
 ; CHECK-NEXT:    [[LOADRES:%.*]] = load i32, ptr [[B:%.*]], align 4
-; CHECK-NEXT:    [[LOADRES2:%.*]] = call i32 @llvm.annotation.i32(i32 [[LOADRES]], ptr nonnull @.str, ptr nonnull @.str1, i32 2)
+; CHECK-NEXT:    [[LOADRES2:%.*]] = call i32 @llvm.annotation.i32.p0(i32 [[LOADRES]], ptr nonnull @.str, ptr nonnull @.str1, i32 2)
 ; CHECK-NEXT:    store i32 [[LOADRES2]], ptr [[A]], align 32
 ; CHECK-NEXT:    [[DUMMY_EQ:%.*]] = icmp ugt i32 [[LOADRES]], 42
 ; CHECK-NEXT:    tail call void @llvm.assume(i1 [[DUMMY_EQ]])
-; CHECK-NEXT:    [[M_A:%.*]] = call ptr @llvm.ptr.annotation.p0(ptr nonnull [[M]], ptr nonnull @.str, ptr nonnull @.str1, i32 2, ptr null)
+; CHECK-NEXT:    [[M_A:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr nonnull [[M]], ptr nonnull @.str, ptr nonnull @.str1, i32 2, ptr null)
 ; CHECK-NEXT:    [[OBJSZ:%.*]] = call i64 @llvm.objectsize.i64.p0(ptr [[C:%.*]], i1 false, i1 false, i1 false)
 ; CHECK-NEXT:    store i64 [[OBJSZ]], ptr [[M_A]], align 4
 ; CHECK-NEXT:    [[PTRINT:%.*]] = ptrtoint ptr [[A]] to i64
Index: llvm/test/Transforms/InstCombine/annotation-intrinsic.ll
===================================================================
--- llvm/test/Transforms/InstCombine/annotation-intrinsic.ll
+++ llvm/test/Transforms/InstCombine/annotation-intrinsic.ll
@@ -12,7 +12,7 @@
 ; CHECK-LABEL: @annotated(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TMP0:%.*]] = load i32, ptr [[C:%.*]], align 4
-; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.annotation.i32(i32 [[TMP0]], ptr undef, ptr undef, i32 undef)
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.annotation.i32.p0(i32 [[TMP0]], ptr undef, ptr undef, i32 undef)
 ; CHECK-NEXT:    [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP0]]
 ; CHECK-NEXT:    ret i32 [[ADD]]
 ;
Index: llvm/test/Bitcode/upgrade-var-annotation.ll
===================================================================
--- llvm/test/Bitcode/upgrade-var-annotation.ll
+++ llvm/test/Bitcode/upgrade-var-annotation.ll
@@ -1,15 +1,16 @@
 ; Test upgrade of var.annotation intrinsics.
 ;
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
 ; RUN: llvm-dis < %s.bc | FileCheck %s
 
 
 define void @f(i8* %arg0, i8* %arg1, i8* %arg2, i32 %arg3) {
 ;CHECK: @f(i8* [[ARG0:%.*]], i8* [[ARG1:%.*]], i8* [[ARG2:%.*]], i32 [[ARG3:%.*]])
   call void @llvm.var.annotation(i8* %arg0, i8* %arg1, i8* %arg2, i32 %arg3)
-;CHECK:  call void @llvm.var.annotation(i8* [[ARG0]], i8* [[ARG1]], i8* [[ARG2]], i32 [[ARG3]], i8* null)
+;CHECK:  call void @llvm.var.annotation.p0i8.p0i8(i8* [[ARG0]], i8* [[ARG1]], i8* [[ARG2]], i32 [[ARG3]], i8* null)
   ret void
 }
 
 ; Function Attrs: nofree nosync nounwind willreturn
 declare void @llvm.var.annotation(i8*, i8*, i8*, i32)
-; CHECK: declare void @llvm.var.annotation(i8*, i8*, i8*, i32, i8*)
+; CHECK: declare void @llvm.var.annotation.p0i8.p0i8(i8*, i8*, i8*, i32, i8*)
Index: llvm/test/Bitcode/upgrade-ptr-annotation.ll
===================================================================
--- llvm/test/Bitcode/upgrade-ptr-annotation.ll
+++ llvm/test/Bitcode/upgrade-ptr-annotation.ll
@@ -1,5 +1,6 @@
 ; Test upgrade of ptr.annotation intrinsics.
 ;
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
 ; RUN: llvm-dis < %s.bc | FileCheck %s
 
 ; Unused return values
@@ -9,17 +10,17 @@
 define void @f1(i8* %arg0, i8* %arg1, i8* %arg2, i32 %arg3) {
 ;CHECK: @f1(i8* [[ARG0:%.*]], i8* [[ARG1:%.*]], i8* [[ARG2:%.*]], i32 [[ARG3:%.*]])
   %t0 = call i8* @llvm.ptr.annotation.p0i8(i8* %arg0, i8* %arg1, i8* %arg2, i32 %arg3)
-;CHECK:  call i8* @llvm.ptr.annotation.p0i8(i8* [[ARG0]], i8* [[ARG1]], i8* [[ARG2]], i32 [[ARG3]], i8* null)
+;CHECK:  call i8* @llvm.ptr.annotation.p0i8.p0i8(i8* [[ARG0]], i8* [[ARG1]], i8* [[ARG2]], i32 [[ARG3]], i8* null)
 
   %arg0_p16 = bitcast i8* %arg0 to i16*
   %t1 = call i16* @llvm.ptr.annotation.p0i16(i16* %arg0_p16, i8* %arg1, i8* %arg2, i32 %arg3)
 ;CHECK:  [[ARG0_P16:%.*]] = bitcast
-;CHECK:  call i16* @llvm.ptr.annotation.p0i16(i16* [[ARG0_P16]], i8* [[ARG1]], i8* [[ARG2]], i32 [[ARG3]], i8* null)
+;CHECK:  call i16* @llvm.ptr.annotation.p0i16.p0i8(i16* [[ARG0_P16]], i8* [[ARG1]], i8* [[ARG2]], i32 [[ARG3]], i8* null)
 
   %arg0_p256 = bitcast i8* %arg0 to i256*
   %t2 = call i256* @llvm.ptr.annotation.p0i256(i256* %arg0_p256, i8* %arg1, i8* %arg2, i32 %arg3)
 ;CHECK:  [[ARG0_P256:%.*]] = bitcast
-;CHECK:  call i256* @llvm.ptr.annotation.p0i256(i256* [[ARG0_P256]], i8* [[ARG1]], i8* [[ARG2]], i32 [[ARG3]], i8* null)
+;CHECK:  call i256* @llvm.ptr.annotation.p0i256.p0i8(i256* [[ARG0_P256]], i8* [[ARG1]], i8* [[ARG2]], i32 [[ARG3]], i8* null)
   ret void
 }
 
@@ -30,16 +31,16 @@
   %cmp = icmp ugt i16* %t0, %t1
   %sel = select i1 %cmp, i16* %t0, i16* %t1
   ret i16* %sel
-; CHECK:  [[T0:%.*]] = call i16* @llvm.ptr.annotation.p0i16(i16* %x, i8* undef, i8* undef, i32 undef, i8* null)
-; CHECK:  [[T1:%.*]] = call i16* @llvm.ptr.annotation.p0i16(i16* %y, i8* undef, i8* undef, i32 undef, i8* null)
+; CHECK:  [[T0:%.*]] = call i16* @llvm.ptr.annotation.p0i16.p0i8(i16* %x, i8* undef, i8* undef, i32 undef, i8* null)
+; CHECK:  [[T1:%.*]] = call i16* @llvm.ptr.annotation.p0i16.p0i8(i16* %y, i8* undef, i8* undef, i32 undef, i8* null)
 ; CHECK:  %cmp = icmp ugt i16* [[T0]], [[T1]]
 ; CHECK:  %sel = select i1 %cmp, i16* [[T0]], i16* [[T1]]
 ; CHECK:  ret i16* %sel
 }
 
 declare i8*   @llvm.ptr.annotation.p0i8(i8*, i8*, i8*, i32)
-; CHECK: declare i8*   @llvm.ptr.annotation.p0i8(i8*, i8*, i8*, i32, i8*)
+; CHECK: declare i8*   @llvm.ptr.annotation.p0i8.p0i8(i8*, i8*, i8*, i32, i8*)
 declare i16*  @llvm.ptr.annotation.p0i16(i16*, i8*, i8*, i32)
-; CHECK: declare i16*   @llvm.ptr.annotation.p0i16(i16*, i8*, i8*, i32, i8*)
+; CHECK: declare i16*   @llvm.ptr.annotation.p0i16.p0i8(i16*, i8*, i8*, i32, i8*)
 declare i256* @llvm.ptr.annotation.p0i256(i256*, i8*, i8*, i32)
-; CHECK: declare i256*   @llvm.ptr.annotation.p0i256(i256*, i8*, i8*, i32, i8*)
+; CHECK: declare i256*   @llvm.ptr.annotation.p0i256.p0i8(i256*, i8*, i8*, i32, i8*)
Index: llvm/test/Bitcode/upgrade-annotation.ll
===================================================================
--- /dev/null
+++ llvm/test/Bitcode/upgrade-annotation.ll
@@ -0,0 +1,15 @@
+; Test upgrade of llvm.annotation intrinsics.
+;
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+; RUN: llvm-dis < %s.bc | FileCheck %s
+
+
+; CHECK: define i32 @f(i32 [[ARG0:%.*]], i8* [[ARG1:%.*]], i8* [[ARG2:%.*]], i32 [[ARG3:%.*]])
+define i32 @f(i32 %arg0, i8* %arg1, i8* %arg2, i32 %arg3) {
+  %result = call i32 @llvm.annotation.i32(i32 %arg0, i8* %arg1, i8* %arg2, i32 %arg3)
+  ; CHECK: [[RESULT:%.*]] = call i32 @llvm.annotation.i32.p0i8(i32 [[ARG0]], i8* [[ARG1]], i8* [[ARG2]], i32 [[ARG3]])
+  ret i32 %result
+}
+
+declare i32 @llvm.annotation.i32(i32, i8*, i8*, i32)
+; CHECK: declare i32 @llvm.annotation.i32.p0i8(i32, i8*, i8*, i32)
Index: llvm/test/Assembler/opaque-ptr-intrinsic-remangling.ll
===================================================================
--- llvm/test/Assembler/opaque-ptr-intrinsic-remangling.ll
+++ llvm/test/Assembler/opaque-ptr-intrinsic-remangling.ll
@@ -38,7 +38,7 @@
 
 define i8* @test_ptr_annotation(i8* %p) {
 ; CHECK-LABEL: @test_ptr_annotation(
-; CHECK-NEXT:    [[P2:%.*]] = call ptr @llvm.ptr.annotation.p0(ptr [[P:%.*]], ptr undef, ptr undef, i32 undef, ptr undef)
+; CHECK-NEXT:    [[P2:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[P:%.*]], ptr undef, ptr undef, i32 undef, ptr undef)
 ; CHECK-NEXT:    ret ptr [[P2]]
 ;
   %p2 = call i8* @llvm.ptr.annotation.p0i8(i8* %p, i8* undef, i8* undef, i32 undef, i8* undef)
Index: llvm/test/Analysis/ScalarEvolution/annotation-intrinsics.ll
===================================================================
--- llvm/test/Analysis/ScalarEvolution/annotation-intrinsics.ll
+++ llvm/test/Analysis/ScalarEvolution/annotation-intrinsics.ll
@@ -7,7 +7,7 @@
 define i64 @annotation(i64 %x) {
 ; CHECK-LABEL: 'annotation'
 ; CHECK-NEXT:  Classifying expressions for: @annotation
-; CHECK-NEXT:    %1 = tail call i64 @llvm.annotation.i64(i64 %x, i8* null, i8* null, i32 0)
+; CHECK-NEXT:    %1 = tail call i64 @llvm.annotation.i64.p0i8(i64 %x, i8* null, i8* null, i32 0)
 ; CHECK-NEXT:    --> %x U: full-set S: full-set
 ; CHECK-NEXT:  Determining loop execution counts for: @annotation
 ;
@@ -18,7 +18,7 @@
 define i8* @ptr_annotation(i8* %x) {
 ; CHECK-LABEL: 'ptr_annotation'
 ; CHECK-NEXT:  Classifying expressions for: @ptr_annotation
-; CHECK-NEXT:    %1 = call i8* @llvm.ptr.annotation.p0i8(i8* %x, i8* null, i8* null, i32 0, i8* null)
+; CHECK-NEXT:    %1 = call i8* @llvm.ptr.annotation.p0i8.p0i8(i8* %x, i8* null, i8* null, i32 0, i8* null)
 ; CHECK-NEXT:    --> %x U: full-set S: full-set
 ; CHECK-NEXT:  Determining loop execution counts for: @ptr_annotation
 ;
Index: llvm/test/Analysis/CostModel/free-intrinsics-no_info.ll
===================================================================
--- llvm/test/Analysis/CostModel/free-intrinsics-no_info.ll
+++ llvm/test/Analysis/CostModel/free-intrinsics-no_info.ll
@@ -4,7 +4,7 @@
 
 define i32 @trivially_free() {
 ; CHECK-SIZE-LABEL: 'trivially_free'
-; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a0 = call i32 @llvm.annotation.i32(i32 undef, i8* undef, i8* undef, i32 undef)
+; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a0 = call i32 @llvm.annotation.i32.p0i8(i32 undef, i8* undef, i8* undef, i32 undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.assume(i1 undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.experimental.noalias.scope.decl(metadata !0)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.sideeffect()
@@ -16,12 +16,12 @@
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.lifetime.start.p0i8(i64 1, i8* undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.lifetime.end.p0i8(i64 1, i8* undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a5 = call i64 @llvm.objectsize.i64.p0i8(i8* undef, i1 true, i1 true, i1 true)
-; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a6 = call i8* @llvm.ptr.annotation.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
-; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.var.annotation(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
+; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a6 = call i8* @llvm.ptr.annotation.p0i8.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
+; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.var.annotation.p0i8.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
 ;
 ; CHECK-THROUGHPUT-LABEL: 'trivially_free'
-; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a0 = call i32 @llvm.annotation.i32(i32 undef, i8* undef, i8* undef, i32 undef)
+; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a0 = call i32 @llvm.annotation.i32.p0i8(i32 undef, i8* undef, i8* undef, i32 undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.assume(i1 undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.experimental.noalias.scope.decl(metadata !0)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.sideeffect()
@@ -33,8 +33,8 @@
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.lifetime.start.p0i8(i64 1, i8* undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.lifetime.end.p0i8(i64 1, i8* undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a5 = call i64 @llvm.objectsize.i64.p0i8(i8* undef, i1 true, i1 true, i1 true)
-; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a6 = call i8* @llvm.ptr.annotation.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
-; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.var.annotation(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
+; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a6 = call i8* @llvm.ptr.annotation.p0i8.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
+; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.var.annotation.p0i8.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
 ;
   %a0 = call i32 @llvm.annotation.i32(i32 undef, i8* undef, i8* undef, i32 undef)
Index: llvm/test/Analysis/CostModel/free-intrinsics-datalayout.ll
===================================================================
--- llvm/test/Analysis/CostModel/free-intrinsics-datalayout.ll
+++ llvm/test/Analysis/CostModel/free-intrinsics-datalayout.ll
@@ -6,7 +6,7 @@
 
 define i32 @trivially_free() {
 ; CHECK-SIZE-LABEL: 'trivially_free'
-; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a0 = call i32 @llvm.annotation.i32(i32 undef, i8* undef, i8* undef, i32 undef)
+; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a0 = call i32 @llvm.annotation.i32.p0i8(i32 undef, i8* undef, i8* undef, i32 undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.assume(i1 undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.experimental.noalias.scope.decl(metadata !0)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.sideeffect()
@@ -18,12 +18,12 @@
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.lifetime.start.p0i8(i64 1, i8* undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.lifetime.end.p0i8(i64 1, i8* undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a5 = call i64 @llvm.objectsize.i64.p0i8(i8* undef, i1 true, i1 true, i1 true)
-; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a6 = call i8* @llvm.ptr.annotation.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
-; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.var.annotation(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
+; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a6 = call i8* @llvm.ptr.annotation.p0i8.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
+; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.var.annotation.p0i8.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
 ;
 ; CHECK-THROUGHPUT-LABEL: 'trivially_free'
-; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a0 = call i32 @llvm.annotation.i32(i32 undef, i8* undef, i8* undef, i32 undef)
+; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a0 = call i32 @llvm.annotation.i32.p0i8(i32 undef, i8* undef, i8* undef, i32 undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.assume(i1 undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.experimental.noalias.scope.decl(metadata !0)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.sideeffect()
@@ -35,8 +35,8 @@
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.lifetime.start.p0i8(i64 1, i8* undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.lifetime.end.p0i8(i64 1, i8* undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a5 = call i64 @llvm.objectsize.i64.p0i8(i8* undef, i1 true, i1 true, i1 true)
-; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a6 = call i8* @llvm.ptr.annotation.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
-; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.var.annotation(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
+; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a6 = call i8* @llvm.ptr.annotation.p0i8.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
+; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.var.annotation.p0i8.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
 ;
   %a0 = call i32 @llvm.annotation.i32(i32 undef, i8* undef, i8* undef, i32 undef)
Index: llvm/test/Analysis/CostModel/X86/free-intrinsics.ll
===================================================================
--- llvm/test/Analysis/CostModel/X86/free-intrinsics.ll
+++ llvm/test/Analysis/CostModel/X86/free-intrinsics.ll
@@ -4,7 +4,7 @@
 
 define i32 @trivially_free() {
 ; CHECK-SIZE-LABEL: 'trivially_free'
-; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a0 = call i32 @llvm.annotation.i32(i32 undef, i8* undef, i8* undef, i32 undef)
+; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a0 = call i32 @llvm.annotation.i32.p0i8(i32 undef, i8* undef, i8* undef, i32 undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.assume(i1 undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.experimental.noalias.scope.decl(metadata !0)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.sideeffect()
@@ -16,12 +16,12 @@
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.lifetime.start.p0i8(i64 1, i8* undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.lifetime.end.p0i8(i64 1, i8* undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a5 = call i64 @llvm.objectsize.i64.p0i8(i8* undef, i1 true, i1 true, i1 true)
-; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a6 = call i8* @llvm.ptr.annotation.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
-; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.var.annotation(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
+; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a6 = call i8* @llvm.ptr.annotation.p0i8.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
+; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.var.annotation.p0i8.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
 ; CHECK-SIZE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
 ;
 ; CHECK-THROUGHPUT-LABEL: 'trivially_free'
-; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a0 = call i32 @llvm.annotation.i32(i32 undef, i8* undef, i8* undef, i32 undef)
+; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a0 = call i32 @llvm.annotation.i32.p0i8(i32 undef, i8* undef, i8* undef, i32 undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.assume(i1 undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.experimental.noalias.scope.decl(metadata !0)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.sideeffect()
@@ -33,8 +33,8 @@
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.lifetime.start.p0i8(i64 1, i8* undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.lifetime.end.p0i8(i64 1, i8* undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a5 = call i64 @llvm.objectsize.i64.p0i8(i8* undef, i1 true, i1 true, i1 true)
-; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a6 = call i8* @llvm.ptr.annotation.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
-; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.var.annotation(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
+; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %a6 = call i8* @llvm.ptr.annotation.p0i8.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
+; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: call void @llvm.var.annotation.p0i8.p0i8(i8* undef, i8* undef, i8* undef, i32 undef, i8* undef)
 ; CHECK-THROUGHPUT-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: ret i32 undef
 ;
   %a0 = call i32 @llvm.annotation.i32(i32 undef, i8* undef, i8* undef, i32 undef)
Index: llvm/lib/IR/AutoUpgrade.cpp
===================================================================
--- llvm/lib/IR/AutoUpgrade.cpp
+++ llvm/lib/IR/AutoUpgrade.cpp
@@ -1078,9 +1078,9 @@
       }
     } else if (Name.startswith("ptr.annotation.") && F->arg_size() == 4) {
       rename(F);
-      NewFn = Intrinsic::getDeclaration(F->getParent(),
-                                        Intrinsic::ptr_annotation,
-                                        F->arg_begin()->getType());
+      NewFn = Intrinsic::getDeclaration(
+          F->getParent(), Intrinsic::ptr_annotation,
+          {F->arg_begin()->getType(), F->getArg(1)->getType()});
       return true;
     }
     break;
@@ -1095,8 +1095,9 @@
   case 'v': {
     if (Name == "var.annotation" && F->arg_size() == 4) {
       rename(F);
-      NewFn = Intrinsic::getDeclaration(F->getParent(),
-                                        Intrinsic::var_annotation);
+      NewFn = Intrinsic::getDeclaration(
+          F->getParent(), Intrinsic::var_annotation,
+          {{F->arg_begin()->getType(), F->getArg(1)->getType()}});
       return true;
     }
     break;
@@ -4121,13 +4122,17 @@
 
   case Intrinsic::var_annotation:
     // Upgrade from versions that lacked the annotation attribute argument.
-    assert(CI->arg_size() == 4 &&
-           "Before LLVM 12.0 this intrinsic took four arguments");
+    if (CI->arg_size() != 4) {
+      DefaultCase();
+      return;
+    }
     // Create a new call with an added null annotation attribute argument.
     NewCall = Builder.CreateCall(
         NewFn,
         {CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2),
          CI->getArgOperand(3), Constant::getNullValue(Builder.getInt8PtrTy())});
+    NewCall->takeName(CI);
+    CI->replaceAllUsesWith(NewCall);
     CI->eraseFromParent();
     return;
 
Index: llvm/include/llvm/IR/Intrinsics.td
===================================================================
--- llvm/include/llvm/IR/Intrinsics.td
+++ llvm/include/llvm/IR/Intrinsics.td
@@ -1046,17 +1046,17 @@
 //===---------------- Generic Variable Attribute Intrinsics----------------===//
 //
 def int_var_annotation : DefaultAttrsIntrinsic<
-    [], [llvm_ptr_ty, llvm_ptr_ty, llvm_ptr_ty, llvm_i32_ty, llvm_ptr_ty],
+    [], [llvm_anyptr_ty, llvm_anyptr_ty, LLVMMatchType<1>, llvm_i32_ty, LLVMMatchType<1>],
     [IntrInaccessibleMemOnly], "llvm.var.annotation">;
 
 def int_ptr_annotation : DefaultAttrsIntrinsic<
     [LLVMAnyPointerType<llvm_anyint_ty>],
-    [LLVMMatchType<0>, llvm_ptr_ty, llvm_ptr_ty, llvm_i32_ty, llvm_ptr_ty],
+    [LLVMMatchType<0>, llvm_anyptr_ty, LLVMMatchType<1>, llvm_i32_ty, LLVMMatchType<1>],
     [IntrInaccessibleMemOnly], "llvm.ptr.annotation">;
 
 def int_annotation : DefaultAttrsIntrinsic<
     [llvm_anyint_ty],
-    [LLVMMatchType<0>, llvm_ptr_ty, llvm_ptr_ty, llvm_i32_ty],
+    [LLVMMatchType<0>, llvm_anyptr_ty, LLVMMatchType<1>, llvm_i32_ty],
     [IntrInaccessibleMemOnly], "llvm.annotation">;
 
 // Annotates the current program point with metadata strings which are emitted
Index: clang/test/CodeGenSYCL/field-annotate-addr-space.cpp
===================================================================
--- clang/test/CodeGenSYCL/field-annotate-addr-space.cpp
+++ clang/test/CodeGenSYCL/field-annotate-addr-space.cpp
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -no-opaque-pointers -triple spir64 -fsycl-is-device -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s
 
-// CHECK: [[ANNOT:.+]] = private unnamed_addr constant {{.*}}c"my_annotation\00"
+// CHECK: [[ANNOT:.+]] = private unnamed_addr addrspace(1) constant {{.*}}c"my_annotation\00"
 
 struct HasField {
   // This caused an assertion on creating a bitcast here,
@@ -13,7 +13,7 @@
   struct HasField f;
   // CHECK: %[[A:.+]] = getelementptr inbounds %struct.HasField, %struct.HasField addrspace(4)* %{{.+}}
   // CHECK: %[[BITCAST:.+]] = bitcast i32 addrspace(4)* addrspace(4)* %[[A]] to i8 addrspace(4)*
-  // CHECK: %[[CALL:.+]] = call i8 addrspace(4)* @llvm.ptr.annotation.p4i8(i8 addrspace(4)* %[[BITCAST]], i8* getelementptr inbounds ([14 x i8], [14 x i8]* [[ANNOT]]
+  // 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]]
   // CHECK: bitcast i8 addrspace(4)* %[[CALL]] to i32 addrspace(4)* addrspace(4)*
   f.a = b;
 }
Index: clang/test/CodeGenCXX/attr-annotate2.cpp
===================================================================
--- clang/test/CodeGenCXX/attr-annotate2.cpp
+++ clang/test/CodeGenCXX/attr-annotate2.cpp
@@ -10,7 +10,7 @@
 // CHECK-NEXT:    [[N:%.*]] = alloca i32, align 4
 // CHECK-NEXT:    [[J:%.*]] = alloca i32, align 4
 // CHECK-NEXT:    store i32 10, ptr [[N]], align 4
-// CHECK-NEXT:    call void @llvm.var.annotation(ptr [[J]], ptr @[[STR]], ptr @[[FILENAME]], i32 {{.*}}, ptr @[[ARGS]])
+// CHECK-NEXT:    call void @llvm.var.annotation.p0.p0(ptr [[J]], ptr @[[STR]], ptr @[[FILENAME]], i32 {{.*}}, ptr @[[ARGS]])
 // CHECK-NEXT:    store i32 0, ptr [[J]], align 4
 // CHECK-NEXT:    ret void
 //
Index: clang/test/CodeGenCXX/attr-annotate.cpp
===================================================================
--- clang/test/CodeGenCXX/attr-annotate.cpp
+++ clang/test/CodeGenCXX/attr-annotate.cpp
@@ -51,12 +51,12 @@
 // CHECK-NEXT:    store ptr [[ARGV:%.*]], ptr [[ARGV_ADDR]], align 8
 // CHECK-NEXT:    [[TMP0:%.*]] = load i32, ptr [[ARGC_ADDR]], align 4
 // CHECK-NEXT:    [[V:%.*]] = getelementptr inbounds %"struct.B<int, 7>::foo", ptr [[F]], i32 0, i32 0
-// CHECK-NEXT:    [[TMP2:%.*]] = call ptr @llvm.ptr.annotation.p0(ptr [[V]], ptr @.str, ptr @.str.1, i32 {{.*}}, ptr @.args)
-// CHECK-NEXT:    [[TMP5:%.*]] = call ptr @llvm.ptr.annotation.p0(ptr [[TMP2]], ptr @.str.3, ptr @.str.1, i32 {{.*}}, ptr @.args.4)
+// CHECK-NEXT:    [[TMP2:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[V]], ptr @.str, ptr @.str.1, i32 {{.*}}, ptr @.args)
+// CHECK-NEXT:    [[TMP5:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[TMP2]], ptr @.str.3, ptr @.str.1, i32 {{.*}}, ptr @.args.4)
 // CHECK-NEXT:    store i32 [[TMP0]], ptr [[TMP5]], align 4
 // CHECK-NEXT:    [[TMP7:%.*]] = load i32, ptr [[ARGC_ADDR]], align 4
-// CHECK-NEXT:    [[TMP8:%.*]] = call ptr @llvm.ptr.annotation.p0(ptr @_ZL2gf, ptr @.str, ptr @.str.1, i32 {{.*}}, ptr @.args.5)
-// CHECK-NEXT:    [[TMP11:%.*]] = call ptr @llvm.ptr.annotation.p0(ptr [[TMP8]], ptr @.str.3, ptr @.str.1, i32 {{.*}}, ptr @.args.4)
+// CHECK-NEXT:    [[TMP8:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr @_ZL2gf, ptr @.str, ptr @.str.1, i32 {{.*}}, ptr @.args.5)
+// CHECK-NEXT:    [[TMP11:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[TMP8]], ptr @.str.3, ptr @.str.1, i32 {{.*}}, ptr @.args.4)
 // CHECK-NEXT:    store i32 [[TMP7]], ptr [[TMP11]], align 4
 // CHECK-NEXT:    ret i32 0
 //
Index: clang/test/CodeGen/annotations-var.c
===================================================================
--- clang/test/CodeGen/annotations-var.c
+++ clang/test/CodeGen/annotations-var.c
@@ -20,13 +20,13 @@
 // PARAM: define {{.*}}@foo
 // PARAM:      [[V:%.*]] = alloca i32
 // PARAM:      bitcast i32* [[V]] to i8*
-// PARAM-NEXT: call void @llvm.var.annotation(
+// PARAM-NEXT: call void @llvm.var.annotation.p0i8.p0i8(
 // PARAM-NEXT: bitcast i32* [[V]] to i8*
-// PARAM-NEXT: call void @llvm.var.annotation(
+// PARAM-NEXT: call void @llvm.var.annotation.p0i8.p0i8(
 // PARAM-NEXT: bitcast i32* [[V]] to i8*
-// PARAM-NEXT: call void @llvm.var.annotation(
+// PARAM-NEXT: call void @llvm.var.annotation.p0i8.p0i8(
 // PARAM-NEXT: bitcast i32* [[V]] to i8*
-// PARAM-NEXT: call void @llvm.var.annotation(
+// PARAM-NEXT: call void @llvm.var.annotation.p0i8.p0i8(
 }
 
 void local(void) {
@@ -34,9 +34,9 @@
 // LOCAL-LABEL: define{{.*}} void @local()
 // LOCAL:      [[LOCALVAR:%.*]] = alloca i32,
 // LOCAL-NEXT: [[T0:%.*]] = bitcast i32* [[LOCALVAR]] to i8*
-// 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)
+// 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)
 // LOCAL-NEXT: [[T0:%.*]] = bitcast i32* [[LOCALVAR]] to i8*
-// 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)
+// 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)
 }
 
 void local_after_return(void) {
@@ -53,5 +53,5 @@
 // UNDEF-LABEL: define{{.*}} void @undef()
 // UNDEF:      [[UNDEFVAR:%.*]] = alloca i32,
 // UNDEF-NEXT: [[T0:%.*]] = bitcast i32* [[UNDEFVAR]] to i8*
-// 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)
+// 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)
 }
Index: clang/test/CodeGen/annotations-loc.c
===================================================================
--- clang/test/CodeGen/annotations-loc.c
+++ clang/test/CodeGen/annotations-loc.c
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64 -emit-llvm -o - %s | FileCheck %s '-D$CONST_AS='
+// RUN: %clang_cc1 -triple amdgcn -emit-llvm -o - %s | FileCheck %s '-D$CONST_AS= addrspace(4)'
 // END.
 # 1 "t.c"
 # 1 "<built-in>"
@@ -6,5 +7,5 @@
 # 1 "t.c"
 int __attribute((annotate("foo"))) foo(void) { return 0; }
 
-// CHECK: private unnamed_addr constant [4 x i8] c"t.c\00"
-// CHECK: @llvm.global.annotations = {{.*}}, i32 1, ptr null }
+// CHECK: private unnamed_addr[[$CONST_AS]] constant [4 x i8] c"t.c\00"
+// CHECK: @llvm.global.annotations = {{.*}}, i32 1, ptr[[$CONST_AS]] null }
Index: clang/test/CodeGen/annotations-global.c
===================================================================
--- clang/test/CodeGen/annotations-global.c
+++ clang/test/CodeGen/annotations-global.c
@@ -1,3 +1,4 @@
+// RUN: %clang_cc1 %s -emit-llvm -o -
 // RUN: %clang_cc1 %s -emit-llvm -o %t1
 // RUN: FileCheck --check-prefix=FOO %s < %t1
 // RUN: FileCheck --check-prefix=A %s < %t1
@@ -48,6 +49,6 @@
 // ADDRSPACE: @llvm.global.annotations = appending global {{.*}} addrspacecast (ptr addrspace(1) @addrspace1_var to ptr), {{.*}}
 
 // AS1-GLOBALS: target datalayout = "{{.+}}-A5-G1"
-// AS1-GLOBALS: @llvm.global.annotations = appending addrspace(1) global [11 x { ptr addrspace(1), ptr addrspace(1), ptr addrspace(1), i32, ptr addrspace(1) }]
+// AS1-GLOBALS: @llvm.global.annotations = appending addrspace(1) global [11 x { ptr addrspace(1), ptr addrspace(4), ptr addrspace(4), i32, ptr addrspace(4) }]
 // AS1-GLOBALS-SAME: { ptr addrspace(1) @a.bar,
 // AS1-GLOBALS-SAME: { ptr addrspace(1) @addrspace1_var,
Index: clang/test/CodeGen/annotations-field.c
===================================================================
--- clang/test/CodeGen/annotations-field.c
+++ clang/test/CodeGen/annotations-field.c
@@ -1,8 +1,9 @@
-// RUN: %clang_cc1 -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64 -emit-llvm -o - %s | FileCheck %s '-D$MANGLE_AS=p0' '-D$CONST_AS=' --check-prefix=X86
+// RUN: %clang_cc1 -triple amdgcn -emit-llvm -o - %s | FileCheck %s '-D$MANGLE_AS=p4' '-D$CONST_AS= addrspace(4)' --check-prefix=AMDGPU
 // END.
 
-// CHECK: private unnamed_addr constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
-// CHECK: private unnamed_addr constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
+// CHECK: private unnamed_addr[[$CONST_AS]] constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
+// CHECK: private unnamed_addr[[$CONST_AS]] constant [8 x i8] c"v_ann_{{.}}\00", section "llvm.metadata"
 
 struct foo {
     int v __attribute__((annotate("v_ann_0"))) __attribute__((annotate("v_ann_1")));
@@ -13,10 +14,13 @@
 int main(int argc, char **argv) {
     struct foo f;
     f.v = argc;
-// CHECK: getelementptr inbounds %struct.foo, ptr %f, i32 0, i32 0
-// CHECK-NEXT: call ptr @llvm.ptr.annotation.p0({{.*}}str{{.*}}str{{.*}}i32 8, ptr null)
-// CHECK-NEXT: call ptr @llvm.ptr.annotation.p0({{.*}}str{{.*}}str{{.*}}i32 8, ptr null)
+// CHECK: getelementptr inbounds %struct.foo, ptr %{{.*}}, i32 0, i32 0
+// CHECK-NEXT: call ptr @llvm.ptr.annotation.p0.[[$MANGLE_AS]]({{.*}}str{{.*}}str{{.*}}i32 9, ptr[[$CONST_AS]] null)
+// CHECK-NEXT: call ptr @llvm.ptr.annotation.p0.[[$MANGLE_AS]]({{.*}}str{{.*}}str{{.*}}i32 9, ptr[[$CONST_AS]] null)
     gf.v = argc;
-// CHECK: call ptr @llvm.ptr.annotation.p0(ptr @gf, {{.*}}str{{.*}}str{{.*}}i32 8, ptr null)
+// X86: call ptr @llvm.ptr.annotation.p0.p0(ptr @gf, ptr @.str{{.*}}, ptr @.str{{.*}}, i32 9, ptr null)
+// X86-NEXT: call ptr @llvm.ptr.annotation.p0.p0(ptr %{{.*}}, ptr @.str{{.*}}, ptr @.str{{.*}}, i32 9, ptr null)
+// 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)
+// AMDGPU-NEXT: call ptr @llvm.ptr.annotation.p0.p4(ptr %{{.*}}, ptr addrspace(4) @.str{{.*}}, ptr addrspace(4) @.str{{.*}}, i32 9, ptr addrspace(4) null)
     return 0;
 }
Index: clang/lib/CodeGen/CodeGenTypeCache.h
===================================================================
--- clang/lib/CodeGen/CodeGenTypeCache.h
+++ clang/lib/CodeGen/CodeGenTypeCache.h
@@ -75,6 +75,9 @@
     llvm::PointerType *GlobalsInt8PtrTy;
   };
 
+  /// void* in the address space for constant globals
+  llvm::PointerType *ConstGlobalsPtrTy;
+
   /// The size and alignment of the builtin C type 'int'.  This comes
   /// up enough in various ABI lowering tasks to be worth pre-computing.
   union {
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -140,6 +140,8 @@
   const llvm::DataLayout &DL = M.getDataLayout();
   AllocaInt8PtrTy = Int8Ty->getPointerTo(DL.getAllocaAddrSpace());
   GlobalsInt8PtrTy = Int8Ty->getPointerTo(DL.getDefaultGlobalsAddressSpace());
+  ConstGlobalsPtrTy = Int8Ty->getPointerTo(
+      C.getTargetAddressSpace(GetGlobalConstantAddressSpace()));
   ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace();
 
   // Build C++20 Module initializers.
@@ -2803,9 +2805,10 @@
 
   // Not found yet, create a new global.
   llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
-  auto *gv =
-      new llvm::GlobalVariable(getModule(), s->getType(), true,
-                               llvm::GlobalValue::PrivateLinkage, s, ".str");
+  auto *gv = new llvm::GlobalVariable(
+      getModule(), s->getType(), true, llvm::GlobalValue::PrivateLinkage, s,
+      ".str", nullptr, llvm::GlobalValue::NotThreadLocal,
+      ConstGlobalsPtrTy->getAddressSpace());
   gv->setSection(AnnotationSection);
   gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
   AStr = gv;
@@ -2831,7 +2834,7 @@
 llvm::Constant *CodeGenModule::EmitAnnotationArgs(const AnnotateAttr *Attr) {
   ArrayRef<Expr *> Exprs = {Attr->args_begin(), Attr->args_size()};
   if (Exprs.empty())
-    return llvm::ConstantPointerNull::get(GlobalsInt8PtrTy);
+    return llvm::ConstantPointerNull::get(ConstGlobalsPtrTy);
 
   llvm::FoldingSetNodeID ID;
   for (Expr *E : Exprs) {
@@ -2881,8 +2884,8 @@
   // Create the ConstantStruct for the global annotation.
   llvm::Constant *Fields[] = {
       llvm::ConstantExpr::getBitCast(GVInGlobalsAS, GlobalsInt8PtrTy),
-      llvm::ConstantExpr::getBitCast(AnnoGV, GlobalsInt8PtrTy),
-      llvm::ConstantExpr::getBitCast(UnitGV, GlobalsInt8PtrTy),
+      llvm::ConstantExpr::getBitCast(AnnoGV, ConstGlobalsPtrTy),
+      llvm::ConstantExpr::getBitCast(UnitGV, ConstGlobalsPtrTy),
       LineNoCst,
       Args,
   };
Index: clang/lib/CodeGen/CodeGenFunction.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenFunction.cpp
+++ clang/lib/CodeGen/CodeGenFunction.cpp
@@ -2458,8 +2458,10 @@
                                                  const AnnotateAttr *Attr) {
   SmallVector<llvm::Value *, 5> Args = {
       AnnotatedVal,
-      Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy),
-      Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy),
+      Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr),
+                            ConstGlobalsPtrTy),
+      Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location),
+                            ConstGlobalsPtrTy),
       CGM.EmitAnnotationLineNo(Location),
   };
   if (Attr)
@@ -2471,9 +2473,12 @@
   assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
   // FIXME We create a new bitcast for every annotation because that's what
   // llvm-gcc was doing.
+  unsigned AS = V->getType()->getPointerAddressSpace();
+  llvm::Type *I8PtrTy = Builder.getInt8PtrTy(AS);
   for (const auto *I : D->specific_attrs<AnnotateAttr>())
-    EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation),
-                       Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()),
+    EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation,
+                                        {I8PtrTy, CGM.ConstGlobalsPtrTy}),
+                       Builder.CreateBitCast(V, I8PtrTy, V->getName()),
                        I->getAnnotation(), D->getLocation(), I);
 }
 
@@ -2486,8 +2491,8 @@
   unsigned AS = PTy ? PTy->getAddressSpace() : 0;
   llvm::PointerType *IntrinTy =
       llvm::PointerType::getWithSamePointeeType(CGM.Int8PtrTy, AS);
-  llvm::Function *F =
-      CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation, IntrinTy);
+  llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
+                                       {IntrinTy, CGM.ConstGlobalsPtrTy});
 
   for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
     // FIXME Always emit the cast inst so we can differentiate between
Index: clang/lib/CodeGen/CGBuiltin.cpp
===================================================================
--- clang/lib/CodeGen/CGBuiltin.cpp
+++ clang/lib/CodeGen/CGBuiltin.cpp
@@ -4258,8 +4258,9 @@
   }
   case Builtin::BI__builtin_annotation: {
     llvm::Value *AnnVal = EmitScalarExpr(E->getArg(0));
-    llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::annotation,
-                                      AnnVal->getType());
+    llvm::Function *F =
+        CGM.getIntrinsic(llvm::Intrinsic::annotation,
+                         {AnnVal->getType(), CGM.ConstGlobalsPtrTy});
 
     // Get the annotation string, go through casts. Sema requires this to be a
     // non-wide string literal, potentially casted, so the cast<> is safe.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to