Skip to content

Commit

Permalink
Fix broken BasicBlock in Emu64OpsPass
Browse files Browse the repository at this point in the history
InstExpander::convertUIToFP32 did not set its insertion point
correctly and were thus adding instructions to a non-existing
BasicBlock.

Fix by ensuring that the insertion point is set to the right
instruction in the new block and allow the callers to continue from
where convertUIToFP32 left off.
  • Loading branch information
troels authored and igcbot committed Sep 6, 2021
1 parent 538e0f9 commit e785722
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions IGC/Compiler/CISACodeGen/Emu64OpsPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1425,18 +1425,9 @@ bool InstExpander::visitFPToSI(FPToSIInst& F2S) {
return true;
}

// Note: This method uses splitBasciBlock() which moves Pos (and all
// instructions following it till the end of basic block) to a new basic block.
// If IRB's insert point is set to Pos or an instruction after Pos in the same
// basic block than the insert basic block value in IRB will be invalid after
// this method completes. A correct insert point should be set in IRB after this
// method is called.
//
Value* InstExpander::convertUIToFP32(Type* DstTy, Value* Lo, Value* Hi, Instruction* Pos) {
IGC_ASSERT(nullptr != Pos);
IGC_ASSERT(nullptr != IRB);
BuilderType::InsertPointGuard Guard(*IRB);
IRB->SetInsertPoint(Pos);

IGCLLVM::Intrinsic IID;
IID = Intrinsic::ctlz;
Expand All @@ -1449,6 +1440,10 @@ Value* InstExpander::convertUIToFP32(Type* DstTy, Value* Lo, Value* Hi, Instruct
BasicBlock* OldBB = Pos->getParent();
IGC_ASSERT(nullptr != OldBB);
BasicBlock* JointBB = OldBB->splitBasicBlock(Pos);

// Set insert point to Pos in the new block JointBB
IRB->SetInsertPoint(Pos);

PHINode* Res = PHINode::Create(IRB->getInt32Ty(), 2, ".u2f.outer.merge", Pos);

{
Expand Down Expand Up @@ -1497,7 +1492,7 @@ Value* InstExpander::convertUIToFP32(Type* DstTy, Value* Lo, Value* Hi, Instruct
BasicBlock* RoundingJBB = InnerJBB->splitBasicBlock(InnerJmp);
PHINode* RoundingRes = PHINode::Create(IRB->getInt32Ty(), 2, ".u2f.rounding.merge.hi", InnerJmp);

BasicBlock* RoundingBB = BasicBlock::Create(*Emu->getContext(), ".u2f.roudning.branch");
BasicBlock* RoundingBB = BasicBlock::Create(*Emu->getContext(), ".u2f.rounding.branch");
RoundingBB->insertInto(Emu->getFunction(), RoundingJBB);
BranchInst::Create(RoundingJBB, RoundingBB);

Expand Down Expand Up @@ -1555,7 +1550,6 @@ bool InstExpander::visitUIToFP(UIToFPInst& U2F) {
}
else {
NewVal = convertUIToFP32(IRB->getFloatTy(), Lo, Hi, &U2F);
IRB->SetInsertPoint(&U2F);
// It's OK to apply the same approach in `convertUIToFP32` to convert 64-bit
// integer into half. But, it would introduce a little more instructions to
// properly round the remaining 48 bits into the high 16 bits. Instead, that
Expand Down Expand Up @@ -1617,7 +1611,6 @@ bool InstExpander::visitSIToFP(SIToFPInst& S2F) {
Hi = IRB->CreateExtractValue(V, 1);

NewVal = convertUIToFP32(IRB->getFloatTy(), Lo, Hi, &S2F);
IRB->SetInsertPoint(&S2F);
NewVal = IRB->CreateBitCast(NewVal, IRB->getInt32Ty());
Sign = IRB->CreateAnd(Sign, IRB->getInt32(0x80000000));
NewVal = IRB->CreateOr(NewVal, Sign);
Expand Down

0 comments on commit e785722

Please sign in to comment.