diff options
author | Fangrui Song <i@maskray.me> | 2023-08-28 10:02:51 -0700 |
---|---|---|
committer | Fangrui Song <i@maskray.me> | 2023-08-28 10:02:51 -0700 |
commit | 02054d3ae9be1f513a0af30aa34884298316c712 (patch) | |
tree | cee78537c45800ae382763efc7fae8d7a0d5353b /llvm/lib/LTO/LTO.cpp | |
parent | 5fe6f56563a3f727c9387e87d3a797292eb4980f (diff) | |
download | llvm-02054d3ae9be1f513a0af30aa34884298316c712.zip llvm-02054d3ae9be1f513a0af30aa34884298316c712.tar.gz llvm-02054d3ae9be1f513a0af30aa34884298316c712.tar.bz2 |
[LTO] Simplify internalize logic. NFC
D151965 removed incorrect internalization for {linkonce,weak}{,_odr} when
the prevailing copy is in native code. The multiple conditions are based
on negative conditions, which can be simplified to be based on positive cases:
* an object with an external linkage (must be prevailing) can be internalized
* a prevailing object with a {linkonce,weak}{,_odr} or common linkage can be internalized.
Further, the lengthy comment is a bit misleading, as it doesn't say that
objects with an external/linkonce/weak linkage can be internalized.
Clarify it.
Reviewed By: tejohnson
Differential Revision: https://reviews.llvm.org/D158949
Diffstat (limited to 'llvm/lib/LTO/LTO.cpp')
-rw-r--r-- | llvm/lib/LTO/LTO.cpp | 41 |
1 files changed, 14 insertions, 27 deletions
diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 06cf3f2..f851037 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -468,24 +468,13 @@ static void thinLTOInternalizeAndPromoteGUID( if (!EnableLTOInternalization) continue; - // Ignore local and appending linkage values since the linker - // doesn't resolve them (and there is no need to internalize if this is - // already internal). - if (GlobalValue::isLocalLinkage(S->linkage()) || - S->linkage() == GlobalValue::AppendingLinkage) - continue; - - // We can't internalize available_externally globals because this - // can break function pointer equality. - if (S->linkage() == GlobalValue::AvailableExternallyLinkage) - continue; - - bool IsPrevailing = isPrevailing(VI.getGUID(), S.get()); - - if (GlobalValue::isInterposableLinkage(S->linkage()) && !IsPrevailing) + // Non-exported values with external linkage can be internalized. + if (GlobalValue::isExternalLinkage(S->linkage())) { + S->setLinkage(GlobalValue::InternalLinkage); continue; + } - // Non-exported functions and variables with linkonce_odr or weak_odr + // Non-exported function and variable definitions with a weak-for-linker // linkage can be internalized in certain cases. The minimum legality // requirements would be that they are not address taken to ensure that we // don't break pointer equality checks, and that variables are either read- @@ -494,7 +483,7 @@ static void thinLTOInternalizeAndPromoteGUID( // (which is how this is guaranteed for variables, when analyzing whether // they are read or write-only). // - // However, we only get to this code for weak/linkonce ODR values in one of + // However, we only get to this code for weak-for-linkage values in one of // two cases: // 1) The prevailing copy is not in IR (it is in native code). // 2) The prevailing copy in IR is not exported from its module. @@ -506,10 +495,10 @@ static void thinLTOInternalizeAndPromoteGUID( // duplicate linkonce_odr copies as exported via the tool, so we need // to handle that case below by checking the number of copies. // - // Generally, we only want to internalize a linkonce/weak ODR value in case + // Generally, we only want to internalize a weak-for-linker value in case // 2, because in case 1 we cannot see how the value is used to know if it // is read or write-only. We also don't want to bloat the binary with - // multiple internalized copies of non-prevailing linkonce_odr functions. + // multiple internalized copies of non-prevailing linkonce/weak functions. // Note if we don't internalize, we will convert non-prevailing copies to // available_externally anyway, so that we drop them after inlining. The // only reason to internalize such a function is if we indeed have a single @@ -520,18 +509,16 @@ static void thinLTOInternalizeAndPromoteGUID( // already perform this elsewhere in the ThinLTO backend handling for // read or write-only variables (processGlobalForThinLTO). // - // Therefore, only internalize linkonce/weak ODR if there is a single copy, - // that is prevailing in this IR module. We can do so aggressively, without + // Therefore, only internalize linkonce/weak if there is a single copy, that + // is prevailing in this IR module. We can do so aggressively, without // requiring the address to be insignificant, or that a variable be read or // write-only. - if ((S->linkage() == GlobalValue::WeakODRLinkage || - S->linkage() == GlobalValue::LinkOnceODRLinkage) && - // We can have only one copy in ThinLTO that isn't prevailing, if the - // prevailing copy is in a native object. - (!IsPrevailing || ExternallyVisibleCopies > 1)) + if (!GlobalValue::isWeakForLinker(S->linkage()) || + GlobalValue::isExternalWeakLinkage(S->linkage())) continue; - S->setLinkage(GlobalValue::InternalLinkage); + if (isPrevailing(VI.getGUID(), S.get()) && ExternallyVisibleCopies == 1) + S->setLinkage(GlobalValue::InternalLinkage); } } |