@@ -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+
551584Address IRGenFunction::emitTaskAlloc (llvm::Value *size, Alignment alignment) {
552585 auto *call = Builder.CreateCall (IGM.getTaskAllocFunctionPointer (), {size});
553586 call->setDoesNotThrow ();
0 commit comments