Skip to content

Commit e3f934a

Browse files
authored
Merge pull request #27724 from jckarter/hint-condfail
IRGen: Hint condfail branches.
2 parents 64d0b18 + 4d5bf27 commit e3f934a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/IRGen/IRGenSIL.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5466,11 +5466,19 @@ void IRGenSILFunction::visitCondFailInst(swift::CondFailInst *i) {
54665466
Explosion e = getLoweredExplosion(i->getOperand());
54675467
llvm::Value *cond = e.claimNext();
54685468

5469+
// The condition should be false, or we die.
5470+
auto expectedCond = Builder.CreateExpect(cond,
5471+
llvm::ConstantInt::get(IGM.Int1Ty, 0));
5472+
54695473
// Emit individual fail blocks so that we can map the failure back to a source
54705474
// line.
5475+
auto origInsertionPoint = Builder.GetInsertBlock();
5476+
54715477
llvm::BasicBlock *failBB = llvm::BasicBlock::Create(IGM.getLLVMContext());
54725478
llvm::BasicBlock *contBB = llvm::BasicBlock::Create(IGM.getLLVMContext());
5473-
Builder.CreateCondBr(cond, failBB, contBB);
5479+
Builder.CreateCondBr(expectedCond, failBB, contBB);
5480+
5481+
Builder.SetInsertPoint(&CurFn->back());
54745482
Builder.emitBlock(failBB);
54755483
if (IGM.DebugInfo)
54765484
// If we are emitting DWARF, this does nothing. Otherwise the ``llvm.trap``
@@ -5479,6 +5487,8 @@ void IRGenSILFunction::visitCondFailInst(swift::CondFailInst *i) {
54795487
// in CodeView.
54805488
IGM.DebugInfo->setInlinedTrapLocation(Builder, i->getDebugScope());
54815489
emitTrap(i->getMessage(), /*EmitUnreachable=*/true);
5490+
5491+
Builder.SetInsertPoint(origInsertionPoint);
54825492
Builder.emitBlock(contBB);
54835493
FailBBs.push_back(failBB);
54845494
}

test/IRGen/builtins.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,12 @@ func testStaticReport(_ b: Bool, ptr: Builtin.RawPointer) -> () {
314314

315315
// CHECK-LABEL: define hidden {{.*}}void @"$s8builtins12testCondFail{{[_0-9a-zA-Z]*}}F"(i1, i1)
316316
func testCondFail(_ b: Bool, c: Bool) {
317-
// CHECK: br i1 %0, label %[[FAIL:.*]], label %[[CONT:.*]]
317+
// CHECK: [[EXPECT:%.*]] = call i1 @llvm.expect.i1(i1 %0, i1 false)
318+
// CHECK: br i1 [[EXPECT]], label %[[FAIL:.*]], label %[[CONT:.*]]
318319
Builtin.condfail_message(b, StaticString("message").unsafeRawPointer)
319320
// CHECK: [[CONT]]
320-
// CHECK: br i1 %1, label %[[FAIL2:.*]], label %[[CONT:.*]]
321+
// CHECK: [[EXPECT:%.*]] = call i1 @llvm.expect.i1(i1 %1, i1 false)
322+
// CHECK: br i1 [[EXPECT]], label %[[FAIL2:.*]], label %[[CONT:.*]]
321323
Builtin.condfail_message(c, StaticString("message").unsafeRawPointer)
322324
// CHECK: [[CONT]]
323325
// CHECK: ret void

0 commit comments

Comments
 (0)