Skip to content

Commit 442db1b

Browse files
committed
IRGen: extract generating a cond_fail into a utility function emitConditionalTrap
NFC
1 parent 31cd6f9 commit 442db1b

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
@@ -477,6 +477,9 @@ class IRGenFunction {
477477
/// Emit a non-mergeable trap call, optionally followed by a terminator.
478478
void emitTrap(StringRef failureMessage, bool EmitUnreachable);
479479

480+
void emitConditionalTrap(llvm::Value *condition, StringRef failureMessage,
481+
const SILDebugScope *debugScope = nullptr);
482+
480483
/// Given at least a src address to a list of elements, runs body over each
481484
/// element passing its address. An optional destination address can be
482485
/// 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
@@ -8268,35 +8268,7 @@ void IRGenSILFunction::visitDestroyAddrInst(swift::DestroyAddrInst *i) {
82688268
void IRGenSILFunction::visitCondFailInst(swift::CondFailInst *i) {
82698269
Explosion e = getLoweredExplosion(i->getOperand());
82708270
llvm::Value *cond = e.claimNext();
8271-
8272-
// The condition should be false, or we die.
8273-
auto expectedCond = Builder.CreateExpect(cond,
8274-
llvm::ConstantInt::get(IGM.Int1Ty, 0));
8275-
8276-
// Emit individual fail blocks so that we can map the failure back to a source
8277-
// line.
8278-
auto origInsertionPoint = Builder.GetInsertBlock();
8279-
8280-
llvm::BasicBlock *failBB = llvm::BasicBlock::Create(IGM.getLLVMContext());
8281-
llvm::BasicBlock *contBB = llvm::BasicBlock::Create(IGM.getLLVMContext());
8282-
auto br = Builder.CreateCondBr(expectedCond, failBB, contBB);
8283-
8284-
if (IGM.getOptions().AnnotateCondFailMessage && !i->getMessage().empty())
8285-
br->addAnnotationMetadata(i->getMessage());
8286-
8287-
Builder.SetInsertPoint(&CurFn->back());
8288-
Builder.emitBlock(failBB);
8289-
if (IGM.DebugInfo)
8290-
// If we are emitting DWARF, this does nothing. Otherwise the ``llvm.trap``
8291-
// instruction emitted from ``Builtin.condfail`` should have an inlined
8292-
// debug location. This is because zero is not an artificial line location
8293-
// in CodeView.
8294-
IGM.DebugInfo->setInlinedTrapLocation(Builder, i->getDebugScope());
8295-
emitTrap(i->getMessage(), /*EmitUnreachable=*/true);
8296-
8297-
Builder.SetInsertPoint(origInsertionPoint);
8298-
Builder.emitBlock(contBB);
8299-
FailBBs.push_back(failBB);
8271+
emitConditionalTrap(cond, i->getMessage(), i->getDebugScope());
83008272
}
83018273

83028274
void IRGenSILFunction::visitIncrementProfilerCounterInst(

0 commit comments

Comments
 (0)