@@ -342,6 +342,33 @@ static const char *ValueProfKindDescr[] = {
342342#include " llvm/ProfileData/InstrProfData.inc"
343343};
344344
345+ // Create a COMDAT variable INSTR_PROF_RAW_VERSION_VAR to make the runtime
346+ // aware this is an ir_level profile so it can set the version flag.
347+ static GlobalVariable *createIRLevelProfileFlagVar (Module &M, bool IsCS) {
348+ const StringRef VarName (INSTR_PROF_QUOTE (INSTR_PROF_RAW_VERSION_VAR));
349+ Type *IntTy64 = Type::getInt64Ty (M.getContext ());
350+ uint64_t ProfileVersion = (INSTR_PROF_RAW_VERSION | VARIANT_MASK_IR_PROF);
351+ if (IsCS)
352+ ProfileVersion |= VARIANT_MASK_CSIR_PROF;
353+ if (PGOInstrumentEntry)
354+ ProfileVersion |= VARIANT_MASK_INSTR_ENTRY;
355+ if (DebugInfoCorrelate)
356+ ProfileVersion |= VARIANT_MASK_DBG_CORRELATE;
357+ if (PGOFunctionEntryCoverage)
358+ ProfileVersion |=
359+ VARIANT_MASK_BYTE_COVERAGE | VARIANT_MASK_FUNCTION_ENTRY_ONLY;
360+ auto IRLevelVersionVariable = new GlobalVariable (
361+ M, IntTy64, true , GlobalValue::WeakAnyLinkage,
362+ Constant::getIntegerValue (IntTy64, APInt (64 , ProfileVersion)), VarName);
363+ IRLevelVersionVariable->setVisibility (GlobalValue::DefaultVisibility);
364+ Triple TT (M.getTargetTriple ());
365+ if (TT.supportsCOMDAT ()) {
366+ IRLevelVersionVariable->setLinkage (GlobalValue::ExternalLinkage);
367+ IRLevelVersionVariable->setComdat (M.getOrInsertComdat (VarName));
368+ }
369+ return IRLevelVersionVariable;
370+ }
371+
345372namespace {
346373
347374// / The select instruction visitor plays three roles specified
@@ -474,9 +501,7 @@ class PGOInstrumentationGenCreateVarLegacyPass : public ModulePass {
474501 createProfileFileNameVar (M, InstrProfileOutput);
475502 // The variable in a comdat may be discarded by LTO. Ensure the
476503 // declaration will be retained.
477- appendToCompilerUsed (M, createIRLevelProfileFlagVar (
478- M, /* IsCS=*/ true , PGOInstrumentEntry,
479- DebugInfoCorrelate, PGOFunctionEntryCoverage));
504+ appendToCompilerUsed (M, createIRLevelProfileFlagVar (M, /* IsCS=*/ true ));
480505 return false ;
481506 }
482507 std::string InstrProfileOutput;
@@ -1646,8 +1671,7 @@ static bool InstrumentAllFunctions(
16461671 // For the context-sensitve instrumentation, we should have a separated pass
16471672 // (before LTO/ThinLTO linking) to create these variables.
16481673 if (!IsCS)
1649- createIRLevelProfileFlagVar (M, /* IsCS=*/ false , PGOInstrumentEntry,
1650- DebugInfoCorrelate, PGOFunctionEntryCoverage);
1674+ createIRLevelProfileFlagVar (M, /* IsCS=*/ false );
16511675 std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers;
16521676 collectComdatMembers (M, ComdatMembers);
16531677
@@ -1669,9 +1693,7 @@ PGOInstrumentationGenCreateVar::run(Module &M, ModuleAnalysisManager &AM) {
16691693 createProfileFileNameVar (M, CSInstrName);
16701694 // The variable in a comdat may be discarded by LTO. Ensure the declaration
16711695 // will be retained.
1672- appendToCompilerUsed (M, createIRLevelProfileFlagVar (
1673- M, /* IsCS=*/ true , PGOInstrumentEntry,
1674- DebugInfoCorrelate, PGOFunctionEntryCoverage));
1696+ appendToCompilerUsed (M, createIRLevelProfileFlagVar (M, /* IsCS=*/ true ));
16751697 return PreservedAnalyses::all ();
16761698}
16771699
0 commit comments