Skip to content

[GlobalOpt] Don't replace aliasee with alias that has weak linkage #91483

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 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 18 additions & 0 deletions llvm/lib/Transforms/IPO/GlobalOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,17 @@ static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U) {
return !U.usedCount(&GA) && !U.compilerUsedCount(&GA);
}

static bool hasMoreThanOneUseOtherThanLLVMUsed(GlobalValue &V,
const LLVMUsed &U) {
unsigned N = 2;
assert((!U.usedCount(&V) || !U.compilerUsedCount(&V)) &&
"We should have removed the duplicated "
"element from llvm.compiler.used");
if (U.usedCount(&V) || U.compilerUsedCount(&V))
++N;
return V.hasNUsesOrMore(N);
}

static bool mayHaveOtherReferences(GlobalValue &GV, const LLVMUsed &U) {
if (!GV.hasLocalLinkage())
return true;
Expand Down Expand Up @@ -2242,6 +2253,13 @@ static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
// define ... @a(...)
Constant *Aliasee = GA.getAliasee();
GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
// Don't perform the transform if the alias may be replaced at link time while
// someone is using the aliasee (e.g., multiple aliases potentially target it
// or someone calls it).
if (GA.isWeakForLinker())
Ret = false;
if (hasMoreThanOneUseOtherThanLLVMUsed(*Target, U))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since there's a stripPointerCasts() earlier, do we need to check for other uses of a cast ConstantExpr, in addition to other uses of the global itself?

Or maybe we can just drop the whole pointer-cast-stripping thing... with opaque pointer types, not sure if there's any legitimate cast that can show up here. (An addressspacecast would be allowed by the verifier, but not sure it's something we can actually generate code for.)

return Ret;
if (mayHaveOtherReferences(*Target, U))
return Ret;

Expand Down
56 changes: 56 additions & 0 deletions llvm/test/Transforms/GlobalOpt/alias-weak.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --include-generated-funcs --version 4
; RUN: opt < %s -passes=globalopt -S | FileCheck %s

@f1_alias = linkonce_odr hidden alias void (), ptr @f1
@f2_alias = linkonce_odr hidden alias void (), ptr @f2

define void @foo() {
call void @f1_alias()
ret void
}

define void @bar() {
call void @f1()
ret void
}

define void @baz() {
call void @f2_alias()
ret void
}

; We cannot use `f1_alias` to replace `f1` because they are both in use
; and `f1_alias` could be replaced at link time.
define internal void @f1() {
ret void
}

; We can use `f2_alias` to replace `f2` because `b2` is not in use.
define internal void @f2() {
ret void
}
;.
; CHECK: @f1_alias = linkonce_odr hidden alias void (), ptr @f1
;.
; CHECK-LABEL: define void @foo() local_unnamed_addr {
; CHECK-NEXT: call void @f1_alias()
; CHECK-NEXT: ret void
;
;
; CHECK-LABEL: define void @bar() local_unnamed_addr {
; CHECK-NEXT: call void @f1()
; CHECK-NEXT: ret void
;
;
; CHECK-LABEL: define void @baz() local_unnamed_addr {
; CHECK-NEXT: call void @f2_alias()
; CHECK-NEXT: ret void
;
;
; CHECK-LABEL: define internal void @f1() {
; CHECK-NEXT: ret void
;
;
; CHECK-LABEL: define linkonce_odr hidden void @f2_alias() local_unnamed_addr {
; CHECK-NEXT: ret void
;
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ attributes #6 = { noreturn nounwind }
; CHECK-LABEL: define dso_local noundef range(i32 0, 2) i32 @_Z10call_catchi
; CHECK-SAME: (i32 noundef [[NUM:%.*]]) local_unnamed_addr #[[ATTR1:[0-9]+]] !type [[META4]] !type [[META5]] !type [[META6]] {
; CHECK-NEXT: entry:
; CHECK-NEXT: store ptr @_Z9nothrow_ei.cfi_jt, ptr @catch_ptr, align 8, !tbaa [[TBAA7:![0-9]+]]
; CHECK-NEXT: store ptr @.cfi.jumptable, ptr @catch_ptr, align 8, !tbaa [[TBAA7:![0-9]+]]
; CHECK-NEXT: [[TOBOOL_NOT_I:%.*]] = icmp ne i32 [[NUM]], 0
; CHECK-NEXT: [[DOT_I:%.*]] = zext i1 [[TOBOOL_NOT_I]] to i32
; CHECK-NEXT: ret i32 [[DOT_I]]
Expand Down Expand Up @@ -152,7 +152,7 @@ attributes #6 = { noreturn nounwind }
;
;
; CHECK: Function Attrs: naked nocf_check noinline nounwind
; CHECK-LABEL: define internal void @_Z9nothrow_ei.cfi_jt
; CHECK-LABEL: define private void @.cfi.jumptable
; CHECK-SAME: () #[[ATTR5:[0-9]+]] align 8 {
; CHECK-NEXT: entry:
; CHECK-NEXT: tail call void asm sideeffect "jmp ${0:c}@plt\0Aint3\0Aint3\0Aint3\0A", "s"(ptr nonnull @_Z9nothrow_ei) #[[ATTR7:[0-9]+]]
Expand Down
8 changes: 4 additions & 4 deletions llvm/test/Transforms/LowerTypeTests/cfi-unwind-direct-call.ll
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ attributes #8 = { noreturn nounwind }
; CHECK-LABEL: define dso_local void @_Z10call_catchi(
; CHECK-SAME: i32 noundef [[NUM:%.*]]) local_unnamed_addr #[[ATTR0]] personality ptr @__gxx_personality_v0 !type [[META4]] !type [[META5]] !type [[META6]] {
; CHECK-NEXT: entry:
; CHECK-NEXT: store ptr @_Z7throw_ei.cfi_jt, ptr @catch_ptr, align 8, !tbaa [[TBAA11:![0-9]+]]
; CHECK-NEXT: invoke void @_Z7throw_ei.cfi_jt(i32 noundef [[NUM]]) #[[ATTR8:[0-9]+]]
; CHECK-NEXT: store ptr @.cfi.jumptable, ptr @catch_ptr, align 8, !tbaa [[TBAA11:![0-9]+]]
; CHECK-NEXT: invoke void @.cfi.jumptable(i32 noundef [[NUM]]) #[[ATTR8:[0-9]+]]
; CHECK-NEXT: to label [[TRY_CONT:%.*]] unwind label [[LPAD:%.*]], !callees [[META13:![0-9]+]]
; CHECK: lpad:
; CHECK-NEXT: [[TMP0:%.*]] = landingpad { ptr, i32 }
Expand Down Expand Up @@ -220,7 +220,7 @@ attributes #8 = { noreturn nounwind }
;
;
; CHECK: Function Attrs: naked nocf_check noinline
; CHECK-LABEL: define internal void @_Z7throw_ei.cfi_jt(
; CHECK-LABEL: define private void @.cfi.jumptable(
; CHECK-SAME: ) #[[ATTR5:[0-9]+]] align 8 {
; CHECK-NEXT: entry:
; CHECK-NEXT: tail call void asm sideeffect "jmp ${0:c}@plt\0Aint3\0Aint3\0Aint3\0A", "s"(ptr nonnull @_Z7throw_ei) #[[ATTR6]]
Expand All @@ -236,6 +236,6 @@ attributes #8 = { noreturn nounwind }
; CHECK: [[META10]] = !{!"Simple C++ TBAA"}
; CHECK: [[TBAA11]] = !{[[META12:![0-9]+]], [[META12]], i64 0}
; CHECK: [[META12]] = !{!"any pointer", [[META9]], i64 0}
; CHECK: [[META13]] = !{ptr @_Z7throw_ei.cfi_jt}
; CHECK: [[META13]] = !{ptr @.cfi.jumptable}
; CHECK: [[META14]] = !{}
;.