Skip to content
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

[llvm][GlobalOpt] Optimize statically resolvable IFuncs #80606

Merged
merged 10 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
59 changes: 59 additions & 0 deletions llvm/lib/Transforms/IPO/GlobalOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated");
STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed");
STATISTIC(NumInternalFunc, "Number of internal functions");
STATISTIC(NumColdCC, "Number of functions marked coldcc");
STATISTIC(NumIFuncsResolved, "Number of statically resolved IFuncs");
STATISTIC(NumIFuncsDeleted, "Number of IFuncs removed");

static cl::opt<bool>
EnableColdCCStressTest("enable-coldcc-stress-test",
Expand Down Expand Up @@ -2404,6 +2406,57 @@ static bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) {
return Changed;
}

static Function *hasSideeffectFreeStaticResolution(GlobalIFunc &IF) {
Function *Resolver = IF.getResolverFunction();
if (!Resolver)
return nullptr;
jroelofs marked this conversation as resolved.
Show resolved Hide resolved

if (Resolver->isInterposable())
nikic marked this conversation as resolved.
Show resolved Hide resolved
return nullptr;

// Only handle functions that have been optimized into a single basic block.
auto It = Resolver->begin();
if (++It != Resolver->end())
return nullptr;

BasicBlock &BB = Resolver->getEntryBlock();

if (any_of(BB, [](Instruction &I) { return I.mayHaveSideEffects(); }))
return nullptr;

auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
if (!Ret)
return nullptr;

return dyn_cast<Function>(Ret->getReturnValue());
}

/// Find IFuncs that have resolvers that always point at the same statically
/// known callee, and replace their callers with a direct call.
static bool OptimizeStaticIFuncs(Module &M) {
bool Changed = false;
for (GlobalIFunc &IF : M.ifuncs())
if (Function *Callee = hasSideeffectFreeStaticResolution(IF))
if (!IF.use_empty()) {
IF.replaceAllUsesWith(Callee);
NumIFuncsResolved++;
jroelofs marked this conversation as resolved.
Show resolved Hide resolved
Changed = true;
}
return Changed;
}

static bool
DeleteDeadIFuncs(Module &M,
SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {
bool Changed = false;
for (GlobalIFunc &IF : make_early_inc_range(M.ifuncs()))
if (deleteIfDead(IF, NotDiscardableComdats)) {
NumIFuncsDeleted++;
Changed = true;
}
return Changed;
}

static bool
optimizeGlobalsInModule(Module &M, const DataLayout &DL,
function_ref<TargetLibraryInfo &(Function &)> GetTLI,
Expand Down Expand Up @@ -2464,6 +2517,12 @@ optimizeGlobalsInModule(Module &M, const DataLayout &DL,
if (CXAAtExitFn)
LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn);

// Optimize IFuncs whose callee's are statically known.
LocalChange |= OptimizeStaticIFuncs(M);

// Remove any IFuncs that are now dead.
LocalChange |= DeleteDeadIFuncs(M, NotDiscardableComdats);

Changed |= LocalChange;
}

Expand Down
95 changes: 95 additions & 0 deletions llvm/test/Transforms/GlobalOpt/resolve-static-ifunc.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function caller --check-globals all --version 4
; RUN: opt --passes=globalopt -o - -S < %s | FileCheck %s --implicit-check-not=trivial\.ifunc --implicit-check-not=dead_ifunc

target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64-unknown-linux-gnu"

@trivial.ifunc = internal ifunc void (), ptr @trivial.resolver
;.
; CHECK: @unknown_condition = external local_unnamed_addr global i1
; CHECK: @external_ifunc.ifunc = dso_local ifunc void (), ptr @external_ifunc.resolver
; CHECK: @complex.ifunc = internal ifunc void (), ptr @complex.resolver
; CHECK: @sideeffects.ifunc = internal ifunc void (), ptr @sideeffects.resolver
; CHECK: @interposable.ifunc = internal ifunc void (), ptr @interposable.resolver
;.
define ptr @trivial.resolver() {
ret ptr @trivial._Msimd
}
define void @trivial._Msimd() {
ret void
}
define void @trivial.default() {
ret void
}


@dead_ifunc.ifunc = internal ifunc void (), ptr @trivial.resolver

@external_ifunc.ifunc = dso_local ifunc void (), ptr @external_ifunc.resolver
define ptr @external_ifunc.resolver() {
ret ptr @external_ifunc._Msimd
}
define void @external_ifunc._Msimd() {
ret void
}
define void @external_ifunc.default() {
ret void
}

@unknown_condition = external global i1
@complex.ifunc = internal ifunc void (), ptr @complex.resolver
define ptr @complex.resolver() {
entry:
%v = load i1, ptr @unknown_condition
br i1 %v, label %fast, label %slow
fast:
ret ptr @complex._Msimd
slow:
ret ptr @complex._Msimd
}
define void @complex._Msimd() {
ret void
}
define void @complex.default() {
ret void
}

@sideeffects.ifunc = internal ifunc void (), ptr @sideeffects.resolver
define ptr @sideeffects.resolver() {
store i1 0, ptr @unknown_condition
ret ptr @sideeffects.default
}
define void @sideeffects._Msimd() {
ret void
}
define void @sideeffects.default() {
ret void
}

@interposable.ifunc = internal ifunc void (), ptr @interposable.resolver
define weak ptr @interposable.resolver() {
ret ptr @interposable.resolver
}
define void @interposable._Msimd() {
ret void
}
define void @interposable.default() {
ret void
}

define void @caller() {
; CHECK-LABEL: define void @caller() local_unnamed_addr {
; CHECK-NEXT: call void @trivial._Msimd()
; CHECK-NEXT: call void @external_ifunc._Msimd()
; CHECK-NEXT: call void @complex.ifunc()
; CHECK-NEXT: call void @sideeffects.ifunc()
; CHECK-NEXT: call void @interposable.ifunc()
; CHECK-NEXT: ret void
;
call void @trivial.ifunc()
call void @external_ifunc.ifunc()
call void @complex.ifunc()
call void @sideeffects.ifunc()
call void @interposable.ifunc()
ret void
}
Loading