-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[nfc][InstrProfiling]For comdat setting helper function, move comment closer to the code #83757
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
minglotus-6
commented
Mar 4, 2024
•
edited
edited
- [nfc][InstrProfiling]Compute a boolean state as a constant and use it everywhere #83756 is a parent commit of this.
…use it everywhere
… closer to the code
@llvm/pr-subscribers-pgo @llvm/pr-subscribers-llvm-transforms Author: Mingming Liu (minglotus-6) ChangesFull diff: https://github.com/llvm/llvm-project/pull/83757.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
index d5d55dec6382fb..84c6aef4c998a7 100644
--- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -206,8 +206,8 @@ class InstrLowerer final {
const bool IsCS;
std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
-
const bool DataReferencedByCode;
+
struct PerFunctionProfileData {
uint32_t NumValueSites[IPVK_Last + 1] = {};
GlobalVariable *RegionCounters = nullptr;
@@ -1193,18 +1193,42 @@ static bool needsRuntimeRegistrationOfSectionRange(const Triple &TT) {
}
void InstrLowerer::maybeSetComdat(GlobalVariable *GV, Function *Fn,
- StringRef VarName) {
+ StringRef CounterGroupName) {
+ // Place lowered global variables in a comdat group if the associated function
+ // is a COMDAT. This will make sure that only one copy of global variable
+ // (e.g. function counters) of the COMDAT function will be emitted after
+ // linking.
bool NeedComdat = needsComdatForCounter(*Fn, M);
+
bool UseComdat = (NeedComdat || TT.isOSBinFormatELF());
if (!UseComdat)
return;
- StringRef GroupName =
- TT.isOSBinFormatCOFF() && DataReferencedByCode ? GV->getName() : VarName;
+ // Keep in mind that this pass may run before the inliner, so we need to
+ // create a new comdat group (for counters, profiling data, etc). If we use
+ // the comdat of the parent function, that will result in relocations against
+ // discarded sections.
+ //
+ // If the data variable is referenced by code, non-counter variables (notably
+ // profiling data) and counters have to be in different comdats for COFF
+ // because the Visual C++ linker will report duplicate symbol errors if there
+ // are multiple external symbols with the same name marked
+ // IMAGE_COMDAT_SELECT_ASSOCIATIVE.
+ StringRef GroupName = TT.isOSBinFormatCOFF() && DataReferencedByCode
+ ? GV->getName()
+ : CounterGroupName;
Comdat *C = M.getOrInsertComdat(GroupName);
- if (!NeedComdat)
+
+ if (!NeedComdat) {
+ // Object file format must be ELF since `UseComdat && !NeedComdat` is true.
+ //
+ // For ELF, when not using COMDAT, put counters, data and values into a
+ // nodeduplicate COMDAT which is lowered to a zero-flag section group. This
+ // allows -z start-stop-gc to discard the entire group when the function is
+ // discarded.
C->setSelectionKind(Comdat::NoDeduplicate);
+ }
GV->setComdat(C);
// COFF doesn't allow the comdat group leader to have private linkage, so
// upgrade private linkage to internal linkage to produce a symbol table
@@ -1238,23 +1262,7 @@ GlobalVariable *InstrLowerer::setupProfileSection(InstrProfInstBase *Inc,
Linkage = GlobalValue::PrivateLinkage;
Visibility = GlobalValue::DefaultVisibility;
}
- // Move the name variable to the right section. Place them in a COMDAT group
- // if the associated function is a COMDAT. This will make sure that only one
- // copy of counters of the COMDAT function will be emitted after linking. Keep
- // in mind that this pass may run before the inliner, so we need to create a
- // new comdat group for the counters and profiling data. If we use the comdat
- // of the parent function, that will result in relocations against discarded
- // sections.
- //
- // If the data variable is referenced by code, counters and data have to be
- // in different comdats for COFF because the Visual C++ linker will report
- // duplicate symbol errors if there are multiple external symbols with the
- // same name marked IMAGE_COMDAT_SELECT_ASSOCIATIVE.
- //
- // For ELF, when not using COMDAT, put counters, data and values into a
- // nodeduplicate COMDAT which is lowered to a zero-flag section group. This
- // allows -z start-stop-gc to discard the entire group when the function is
- // discarded.
+ // Move the name variable to the right section.
bool Renamed;
GlobalVariable *Ptr;
StringRef VarPrefix;
@@ -1524,6 +1532,7 @@ void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) {
Data->setSection(
getInstrProfSectionName(DataSectionKind, TT.getObjectFormat()));
Data->setAlignment(Align(INSTR_PROF_DATA_ALIGNMENT));
+
maybeSetComdat(Data, Fn, CntsVarName);
PD.DataVar = Data;
|
teresajohnson
approved these changes
Mar 4, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.