diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst --- a/llvm/docs/LangRef.rst +++ b/llvm/docs/LangRef.rst @@ -886,8 +886,9 @@ [, partition "name"] The linkage must be one of ``private``, ``internal``, ``linkonce``, ``weak``, -``linkonce_odr``, ``weak_odr``, ``external``. Note that some system linkers -might not correctly handle dropping a weak symbol that is aliased. +``linkonce_odr``, ``weak_odr``, ``external``, ``available_externally``. Note +that some system linkers might not correctly handle dropping a weak symbol that +is aliased. Aliases that are not ``unnamed_addr`` are guaranteed to have the same address as the aliasee expression. ``unnamed_addr`` ones are only guaranteed to point @@ -906,8 +907,10 @@ intermediate alias being overridden cannot be represented in an object file. -* No global value in the expression can be a declaration, since that - would require a relocation, which is not possible. +* If the alias has the ``available_externally`` linkage, the aliasee must be an + ``available_externally`` global value; otherwise the aliasee can be an + expression but no global value in the expression can be a declaration, since + that would require a relocation, which is not possible. * If either the alias or the aliasee may be replaced by a symbol outside the module at link time or runtime, any optimization cannot replace the alias with diff --git a/llvm/include/llvm/IR/GlobalAlias.h b/llvm/include/llvm/IR/GlobalAlias.h --- a/llvm/include/llvm/IR/GlobalAlias.h +++ b/llvm/include/llvm/IR/GlobalAlias.h @@ -93,8 +93,8 @@ } static bool isValidLinkage(LinkageTypes L) { - return isExternalLinkage(L) || isLocalLinkage(L) || - isWeakLinkage(L) || isLinkOnceLinkage(L); + return isExternalLinkage(L) || isLocalLinkage(L) || isWeakLinkage(L) || + isLinkOnceLinkage(L) || isAvailableExternallyLinkage(L); } // Methods for support type inquiry through isa, cast, and dyn_cast: diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -817,9 +817,18 @@ void Verifier::visitAliaseeSubExpr(SmallPtrSetImpl &Visited, const GlobalAlias &GA, const Constant &C) { - if (const auto *GV = dyn_cast(&C)) { - Check(!GV->isDeclarationForLinker(), "Alias must point to a definition", + if (GA.hasAvailableExternallyLinkage()) { + Check(isa(C) && + cast(C).hasAvailableExternallyLinkage(), + "available_externally alias must point to available_externally " + "global value", &GA); + } + if (const auto *GV = dyn_cast(&C)) { + if (!GA.hasAvailableExternallyLinkage()) { + Check(!GV->isDeclarationForLinker(), "Alias must point to a definition", + &GA); + } if (const auto *GA2 = dyn_cast(GV)) { Check(Visited.insert(GA2).second, "Aliases cannot form a cycle", &GA); @@ -848,7 +857,7 @@ void Verifier::visitGlobalAlias(const GlobalAlias &GA) { Check(GlobalAlias::isValidLinkage(GA.getLinkage()), "Alias should have private, internal, linkonce, weak, linkonce_odr, " - "weak_odr, or external linkage!", + "weak_odr, external, or available_externally linkage!", &GA); const Constant *Aliasee = GA.getAliasee(); Check(Aliasee, "Aliasee cannot be NULL!", &GA); diff --git a/llvm/test/Verifier/alias.ll b/llvm/test/Verifier/alias.ll --- a/llvm/test/Verifier/alias.ll +++ b/llvm/test/Verifier/alias.ll @@ -1,4 +1,4 @@ -; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s +; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s --implicit-check-not=alias --implicit-check-not=Alias declare void @f() @@ -31,3 +31,15 @@ @test3_c = alias i32, i32* @test3_b ; CHECK: Alias cannot point to an interposable alias ; CHECK-NEXT: i32* @test3_c + +@test4_a = available_externally global i32 42 +@test4_b = available_externally alias i32, i32* @test4_a +@test4_c = available_externally alias void(), void()* @f2 +@test4_d = available_externally alias i32, i32* @test4_b + +@test4_e = available_externally alias i32, i32* @test3_a +@test4_f = available_externally alias i32, inttoptr (i64 sub (i64 ptrtoint (i32* @test4_a to i64), i64 ptrtoint (i32* @test4_a to i64)) to i32*) +; CHECK: available_externally alias must point to available_externally global value +; CHECK-NEXT: i32* @test4_e +; CHECK: available_externally alias must point to available_externally global value +; CHECK-NEXT: i32* @test4_f