Skip to content

Commit a711b9e

Browse files
committed
IRGen: extract generating a cond_fail into a utility function emitConditionalTrap
NFC
1 parent 7c3d7fa commit a711b9e

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

lib/IRGen/IRGenFunction.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,39 @@ void IRGenFunction::emitTrap(StringRef failureMessage, bool EmitUnreachable) {
548548
Builder.CreateUnreachable();
549549
}
550550

551+
void IRGenFunction::emitConditionalTrap(llvm::Value *condition, StringRef failureMessage,
552+
const SILDebugScope *debugScope) {
553+
// The condition should be false, or we die.
554+
auto expectedCond = Builder.CreateExpect(condition,
555+
llvm::ConstantInt::get(IGM.Int1Ty, 0));
556+
557+
// Emit individual fail blocks so that we can map the failure back to a source
558+
// line.
559+
auto origInsertionPoint = Builder.GetInsertBlock();
560+
561+
llvm::BasicBlock *failBB = llvm::BasicBlock::Create(IGM.getLLVMContext());
562+
llvm::BasicBlock *contBB = llvm::BasicBlock::Create(IGM.getLLVMContext());
563+
auto br = Builder.CreateCondBr(expectedCond, failBB, contBB);
564+
565+
if (IGM.getOptions().AnnotateCondFailMessage && !failureMessage.empty())
566+
br->addAnnotationMetadata(failureMessage);
567+
568+
Builder.SetInsertPoint(&CurFn->back());
569+
Builder.emitBlock(failBB);
570+
if (IGM.DebugInfo && debugScope) {
571+
// If we are emitting DWARF, this does nothing. Otherwise the ``llvm.trap``
572+
// instruction emitted from ``Builtin.condfail`` should have an inlined
573+
// debug location. This is because zero is not an artificial line location
574+
// in CodeView.
575+
IGM.DebugInfo->setInlinedTrapLocation(Builder, debugScope);
576+
}
577+
emitTrap(failureMessage, /*EmitUnreachable=*/true);
578+
579+
Builder.SetInsertPoint(origInsertionPoint);
580+
Builder.emitBlock(contBB);
581+
FailBBs.push_back(failBB);
582+
}
583+
551584
Address IRGenFunction::emitTaskAlloc(llvm::Value *size, Alignment alignment) {
552585
auto *call = Builder.CreateCall(IGM.getTaskAllocFunctionPointer(), {size});
553586
call->setDoesNotThrow();

lib/IRGen/IRGenFunction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ class IRGenFunction {
478478
/// Emit a non-mergeable trap call, optionally followed by a terminator.
479479
void emitTrap(StringRef failureMessage, bool EmitUnreachable);
480480

481+
void emitConditionalTrap(llvm::Value *condition, StringRef failureMessage,
482+
const SILDebugScope *debugScope = nullptr);
483+
481484
/// Given at least a src address to a list of elements, runs body over each
482485
/// element passing its address. An optional destination address can be
483486
/// provided which this will run over as well to perform things like

lib/IRGen/IRGenSIL.cpp

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8277,35 +8277,7 @@ void IRGenSILFunction::visitDestroyAddrInst(swift::DestroyAddrInst *i) {
82778277
void IRGenSILFunction::visitCondFailInst(swift::CondFailInst *i) {
82788278
Explosion e = getLoweredExplosion(i->getOperand());
82798279
llvm::Value *cond = e.claimNext();
8280-
8281-
// The condition should be false, or we die.
8282-
auto expectedCond = Builder.CreateExpect(cond,
8283-
llvm::ConstantInt::get(IGM.Int1Ty, 0));
8284-
8285-
// Emit individual fail blocks so that we can map the failure back to a source
8286-
// line.
8287-
auto origInsertionPoint = Builder.GetInsertBlock();
8288-
8289-
llvm::BasicBlock *failBB = llvm::BasicBlock::Create(IGM.getLLVMContext());
8290-
llvm::BasicBlock *contBB = llvm::BasicBlock::Create(IGM.getLLVMContext());
8291-
auto br = Builder.CreateCondBr(expectedCond, failBB, contBB);
8292-
8293-
if (IGM.getOptions().AnnotateCondFailMessage && !i->getMessage().empty())
8294-
br->addAnnotationMetadata(i->getMessage());
8295-
8296-
Builder.SetInsertPoint(&CurFn->back());
8297-
Builder.emitBlock(failBB);
8298-
if (IGM.DebugInfo)
8299-
// If we are emitting DWARF, this does nothing. Otherwise the ``llvm.trap``
8300-
// instruction emitted from ``Builtin.condfail`` should have an inlined
8301-
// debug location. This is because zero is not an artificial line location
8302-
// in CodeView.
8303-
IGM.DebugInfo->setInlinedTrapLocation(Builder, i->getDebugScope());
8304-
emitTrap(i->getMessage(), /*EmitUnreachable=*/true);
8305-
8306-
Builder.SetInsertPoint(origInsertionPoint);
8307-
Builder.emitBlock(contBB);
8308-
FailBBs.push_back(failBB);
8280+
emitConditionalTrap(cond, i->getMessage(), i->getDebugScope());
83098281
}
83108282

83118283
void IRGenSILFunction::visitIncrementProfilerCounterInst(

0 commit comments

Comments
 (0)