-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[llvm][GlobalOpt] Optimize statically resolvable IFuncs #80606
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
Changes from 1 commit
5318a4d
633116c
a62ad24
940090f
c05c28b
929c23e
6e11201
96d584e
53a7b10
0bf69ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,6 +89,7 @@ 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"); | ||
|
|
||
| static cl::opt<bool> | ||
| EnableColdCCStressTest("enable-coldcc-stress-test", | ||
|
|
@@ -2404,6 +2405,42 @@ static bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) { | |
| return Changed; | ||
| } | ||
|
|
||
| static Function *hasSideeffectFreeStaticResolution(GlobalIFunc &IF) { | ||
| Function *Resolver = IF.getResolverFunction(); | ||
| if (!Resolver) | ||
| return nullptr; | ||
|
|
||
| Function *Callee = nullptr; | ||
| for (BasicBlock &BB : *Resolver) { | ||
|
||
| if (any_of(BB, [](Instruction &I) { return I.mayHaveSideEffects(); })) | ||
| return nullptr; | ||
|
|
||
| if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) | ||
| if (auto *F = dyn_cast<Function>(Ret->getReturnValue())) { | ||
nikic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!Callee) | ||
| Callee = F; | ||
| else if (F != Callee) | ||
| return nullptr; | ||
| } | ||
| } | ||
|
|
||
| return Callee; | ||
| } | ||
|
|
||
| /// 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 | ||
| optimizeGlobalsInModule(Module &M, const DataLayout &DL, | ||
| function_ref<TargetLibraryInfo &(Function &)> GetTLI, | ||
|
|
@@ -2464,6 +2501,9 @@ optimizeGlobalsInModule(Module &M, const DataLayout &DL, | |
| if (CXAAtExitFn) | ||
| LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn); | ||
|
|
||
| // Optimize IFuncs whose callee's are statically known. | ||
| LocalChange |= OptimizeStaticIFuncs(M); | ||
|
|
||
| Changed |= LocalChange; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| ; RUN: opt --passes=globalopt -o - -S < %s | FileCheck %s | ||
jroelofs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" | ||
| target triple = "aarch64-unknown-linux-gnu" | ||
|
|
||
| $callee_with_trivial_resolver.resolver = comdat any | ||
| @callee_with_trivial_resolver.ifunc = weak_odr dso_local ifunc void (), ptr @callee_with_trivial_resolver.resolver | ||
| define weak_odr ptr @callee_with_trivial_resolver.resolver() comdat { | ||
jroelofs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ret ptr @callee_with_trivial_resolver._Msimd | ||
| } | ||
| define void @callee_with_trivial_resolver._Msimd() { | ||
| ret void | ||
| } | ||
| define void @callee_with_trivial_resolver.default() { | ||
| ret void | ||
| } | ||
|
|
||
| @unknown_condition = external global i1 | ||
| $callee_with_complex_static_resolver.resolver = comdat any | ||
| @callee_with_complex_static_resolver.ifunc = weak_odr dso_local ifunc void (), ptr @callee_with_complex_static_resolver.resolver | ||
| define weak_odr ptr @callee_with_complex_static_resolver.resolver() comdat { | ||
| entry: | ||
| %v = load i1, ptr @unknown_condition | ||
| br i1 %v, label %fast, label %slow | ||
| fast: | ||
| ret ptr @callee_with_complex_static_resolver._Msimd | ||
| slow: | ||
| ret ptr @callee_with_complex_static_resolver._Msimd | ||
| } | ||
| define void @callee_with_complex_static_resolver._Msimd() { | ||
| ret void | ||
| } | ||
| define void @callee_with_complex_static_resolver.default() { | ||
| ret void | ||
| } | ||
|
|
||
| $callee_with_complex_dynamic_resolver.resolver = comdat any | ||
| @callee_with_complex_dynamic_resolver.ifunc = weak_odr dso_local ifunc void (), ptr @callee_with_complex_dynamic_resolver.resolver | ||
| define weak_odr ptr @callee_with_complex_dynamic_resolver.resolver() comdat { | ||
| entry: | ||
| %v = load i1, ptr @unknown_condition | ||
| br i1 %v, label %fast, label %slow | ||
| fast: | ||
| ret ptr @callee_with_complex_dynamic_resolver._Msimd | ||
| slow: | ||
| ret ptr @callee_with_complex_dynamic_resolver.default | ||
| } | ||
| define void @callee_with_complex_dynamic_resolver._Msimd() { | ||
| ret void | ||
| } | ||
| define void @callee_with_complex_dynamic_resolver.default() { | ||
| ret void | ||
| } | ||
|
|
||
| $callee_with_sideeffects_resolver.resolver = comdat any | ||
| @callee_with_sideeffects_resolver.ifunc = weak_odr dso_local ifunc void (), ptr @callee_with_sideeffects_resolver.resolver | ||
| define weak_odr ptr @callee_with_sideeffects_resolver.resolver() comdat { | ||
| store i1 0, ptr @unknown_condition | ||
| ret ptr @callee_with_sideeffects_resolver.default | ||
| } | ||
| define void @callee_with_sideeffects_resolver._Msimd() { | ||
| ret void | ||
| } | ||
| define void @callee_with_sideeffects_resolver.default() { | ||
| ret void | ||
| } | ||
|
|
||
| define void @caller() { | ||
| call void @callee_with_trivial_resolver.ifunc() | ||
| call void @callee_with_complex_static_resolver.ifunc() | ||
| call void @callee_with_complex_dynamic_resolver.ifunc() | ||
| call void @callee_with_sideeffects_resolver.ifunc() | ||
| ret void | ||
| } | ||
|
|
||
| ; CHECK-LABEL: define void @caller() | ||
| ; CHECK-NEXT: call void @callee_with_trivial_resolver._Msimd() | ||
| ; CHECK-NEXT: call void @callee_with_complex_static_resolver._Msimd() | ||
| ; CHECK-NEXT: call void @callee_with_complex_dynamic_resolver.ifunc() | ||
| ; CHECK-NEXT: call void @callee_with_sideeffects_resolver.ifunc() | ||
| ; CHECK-NEXT: ret void | ||
Uh oh!
There was an error while loading. Please reload this page.