@@ -551,10 +551,6 @@ bool InstrProfiling::run(
551
551
// Emit the runtime hook even if no counters are present.
552
552
bool MadeChange = emitRuntimeHook ();
553
553
554
- // Emit the bias variable in each module when counter relocation is enabled.
555
- if (isRuntimeCounterRelocationEnabled ())
556
- MadeChange |= emitBiasVar ();
557
-
558
554
// Improve compile time by avoiding linear scans when there is no work.
559
555
GlobalVariable *CoverageNamesVar =
560
556
M.getNamedGlobal (getCoverageUnusedNamesVarName ());
@@ -697,6 +693,21 @@ void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) {
697
693
if (!LI) {
698
694
IRBuilder<> Builder (&I);
699
695
GlobalVariable *Bias = M->getGlobalVariable (getInstrProfCounterBiasVarName ());
696
+ if (!Bias) {
697
+ // Compiler must define this variable when runtime counter relocation
698
+ // is being used. Runtime has a weak external reference that is used
699
+ // to check whether that's the case or not.
700
+ Bias = new GlobalVariable (*M, Int64Ty, false , GlobalValue::LinkOnceODRLinkage,
701
+ Constant::getNullValue (Int64Ty),
702
+ getInstrProfCounterBiasVarName ());
703
+ Bias->setVisibility (GlobalVariable::HiddenVisibility);
704
+ // A definition that's weak (linkonce_odr) without being in a COMDAT
705
+ // section wouldn't lead to link errors, but it would lead to a dead
706
+ // data word from every TU but one. Putting it in COMDAT ensures there
707
+ // will be exactly one data slot in the link.
708
+ if (TT.supportsCOMDAT ())
709
+ Bias->setComdat (M->getOrInsertComdat (Bias->getName ()));
710
+ }
700
711
LI = Builder.CreateLoad (Int64Ty, Bias);
701
712
}
702
713
auto *Add = Builder.CreateAdd (Builder.CreatePtrToInt (Addr, Int64Ty), LI);
@@ -1183,26 +1194,3 @@ void InstrProfiling::emitInitialization() {
1183
1194
1184
1195
appendToGlobalCtors (*M, F, 0 );
1185
1196
}
1186
-
1187
- bool InstrProfiling::emitBiasVar () {
1188
- // Module already provided its own variable, nothin to do.
1189
- if (M->getGlobalVariable (getInstrProfCounterBiasVarName ()))
1190
- return false ;
1191
-
1192
- // Compiler must define this variable when runtime counter relocation
1193
- // is being used. Runtime has a weak external reference that is used
1194
- // to check whether that's the case or not.
1195
- Type *Int64Ty = Type::getInt64Ty (M->getContext ());
1196
- auto *Bias = new GlobalVariable (*M, Int64Ty, false , GlobalValue::LinkOnceODRLinkage,
1197
- Constant::getNullValue (Int64Ty),
1198
- getInstrProfCounterBiasVarName ());
1199
- Bias->setVisibility (GlobalVariable::HiddenVisibility);
1200
- // A definition that's weak (linkonce_odr) without being in a COMDAT
1201
- // section wouldn't lead to link errors, but it would lead to a dead
1202
- // data word from every TU but one. Putting it in COMDAT ensures there
1203
- // will be exactly one data slot in the link.
1204
- if (TT.supportsCOMDAT ())
1205
- Bias->setComdat (M->getOrInsertComdat (Bias->getName ()));
1206
-
1207
- return true ;
1208
- }
0 commit comments