Skip to content

Commit 51baeaf

Browse files
committed
BranchPredict: a smattering of guesses
1 parent 44e21b0 commit 51baeaf

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

lib/IRGen/GenIntegerLiteral.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,13 @@ void irgen::emitIntegerLiteralCheckedTrunc(IRGenFunction &IGF, Explosion &in,
262262
IGF.IGM.getSize(Size(resultWidth + 1)));
263263
hasOverflow = IGF.Builder.CreateOr(isNegative, tooBig);
264264
}
265-
IGF.Builder.CreateCondBr(hasOverflow, invalidBB, validBB);
265+
266+
// Predict no overflow.
267+
auto *weight =
268+
IGF.getSILModule().getOptions().EnableStaticBranchPrediction ?
269+
IGF.IGM.createProfileWeightsExpecting(false) : nullptr;
270+
271+
IGF.Builder.CreateCondBr(hasOverflow, invalidBB, validBB, weight);
266272
}
267273

268274
// In the invalid block, we just need to construct the result. This block

lib/IRGen/GenThunk.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,13 @@ void IRGenThunk::emit() {
357357
llvm::Value *nil = llvm::ConstantPointerNull::get(
358358
cast<llvm::PointerType>(errorValue->getType()));
359359
auto *hasError = IGF.Builder.CreateICmpNE(errorValue, nil);
360-
IGF.Builder.CreateCondBr(hasError, errorBB, successBB);
360+
361+
// Predict no error is thrown.
362+
auto *weight =
363+
IGF.IGM.getSILModule().getOptions().EnableStaticBranchPrediction ?
364+
IGF.IGM.createProfileWeightsExpecting(false) : nullptr;
365+
366+
IGF.Builder.CreateCondBr(hasError, errorBB, successBB, weight);
361367

362368
IGF.Builder.emitBlock(errorBB);
363369
if (isAsync) {

lib/IRGen/IRGenFunction.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,13 @@ void IRGenFunction::emitAwaitAsyncContinuation(
718718
auto firstAtAwait = Builder.CreateExtractValue(results, 1);
719719
auto contBB = createBasicBlock("await.async.resume");
720720
auto abortBB = createBasicBlock("await.async.abort");
721-
Builder.CreateCondBr(firstAtAwait, abortBB, contBB);
721+
722+
// Predict no abort.
723+
auto *weight =
724+
getSILModule().getOptions().EnableStaticBranchPrediction ?
725+
IGM.createProfileWeightsExpecting(false) : nullptr;
726+
727+
Builder.CreateCondBr(firstAtAwait, abortBB, contBB, weight);
722728
Builder.emitBlock(abortBB);
723729
{
724730
// We were the first to the sync point. "Abort" (return from the
@@ -777,7 +783,13 @@ void IRGenFunction::emitAwaitAsyncContinuation(
777783
auto nullError = llvm::Constant::getNullValue(errorRes->getType());
778784
auto hasError = Builder.CreateICmpNE(errorRes, nullError);
779785
optionalErrorResult->addIncoming(errorRes, Builder.GetInsertBlock());
780-
Builder.CreateCondBr(hasError, optionalErrorBB, normalContBB);
786+
787+
// Predict no error.
788+
auto *weight =
789+
getSILModule().getOptions().EnableStaticBranchPrediction ?
790+
IGM.createProfileWeightsExpecting(false) : nullptr;
791+
792+
Builder.CreateCondBr(hasError, optionalErrorBB, normalContBB, weight);
781793
Builder.emitBlock(normalContBB);
782794
}
783795

lib/IRGen/IRGenModule.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,6 +2069,13 @@ llvm::MDNode *IRGenModule::createProfileWeights(uint64_t TrueCount,
20692069
return MDHelper.createBranchWeights(ScaledTrueCount, ScaledFalseCount);
20702070
}
20712071

2072+
llvm::MDNode *IRGenModule::createProfileWeightsExpecting(bool condition) const {
2073+
if (condition)
2074+
return createProfileWeights(999, 0);
2075+
else
2076+
return createProfileWeights(0, 999);
2077+
}
2078+
20722079
void IRGenModule::unimplemented(SourceLoc loc, StringRef message) {
20732080
Context.Diags.diagnose(loc, diag::irgen_unimplemented, message);
20742081
}

lib/IRGen/IRGenModule.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,10 @@ private: \
19811981
llvm::MDNode *createProfileWeights(uint64_t TrueCount,
19821982
uint64_t FalseCount) const;
19831983

1984+
/// Create llvm metadata with synthetic branch weights where the expectation
1985+
/// is that the branch condition will be \c condition
1986+
llvm::MDNode *createProfileWeightsExpecting(bool condition) const;
1987+
19841988
private:
19851989
void emitGlobalDecl(Decl *D);
19861990
};

lib/IRGen/IRGenSIL.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4636,7 +4636,13 @@ void IRGenSILFunction::visitYieldInst(swift::YieldInst *i) {
46364636
// Branch to the appropriate destination.
46374637
auto unwindBB = getLoweredBB(i->getUnwindBB()).bb;
46384638
auto resumeBB = getLoweredBB(i->getResumeBB()).bb;
4639-
Builder.CreateCondBr(isUnwind, unwindBB, resumeBB);
4639+
4640+
// Predict no unwind happens.
4641+
auto *weight =
4642+
IGM.getSILModule().getOptions().EnableStaticBranchPrediction ?
4643+
IGM.createProfileWeightsExpecting(false) : nullptr;
4644+
4645+
Builder.CreateCondBr(isUnwind, unwindBB, resumeBB, weight);
46404646
}
46414647

46424648
void IRGenSILFunction::visitBeginApplyInst(BeginApplyInst *i) {

0 commit comments

Comments
 (0)