Skip to content

Commit 265a0a3

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Move BasicBlock::splitAfter to CFG
Summary: BasicBlock shouldn't be in the business of modifying the CFG. Reviewed By: DinoV Differential Revision: D88176338 fbshipit-source-id: ac098a197414d7b58cc347c52c01b4f1e6d0eaf3
1 parent 7f02367 commit 265a0a3

5 files changed

Lines changed: 25 additions & 26 deletions

File tree

cinderx/Jit/hir/hir.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -658,22 +658,6 @@ bool BasicBlock::IsTrampoline() {
658658
return false;
659659
}
660660

661-
BasicBlock* BasicBlock::splitAfter(Instr& instr) {
662-
JIT_CHECK(cfg != nullptr, "cannot split unlinked block");
663-
auto tail = cfg->AllocateBlock();
664-
for (auto it = std::next(instrs_.iterator_to(instr)); it != instrs_.end();) {
665-
auto& instr_2 = *it;
666-
++it;
667-
instr_2.unlink();
668-
tail->Append(&instr_2);
669-
}
670-
671-
for (auto edge : tail->out_edges()) {
672-
edge->to()->fixupPhis(this, tail);
673-
}
674-
return tail;
675-
}
676-
677661
void BasicBlock::fixupPhis(BasicBlock* old_pred, BasicBlock* new_pred) {
678662
// This won't work correctly if this block has two incoming edges from the
679663
// same block, but we already can't handle that correctly with our current Phi
@@ -765,6 +749,22 @@ void CFG::RemoveBlock(BasicBlock* block) {
765749
block->cfg = nullptr;
766750
}
767751

752+
BasicBlock* CFG::splitAfter(Instr& target) {
753+
auto block = target.block();
754+
auto tail = AllocateBlock();
755+
for (auto it = std::next(block->iterator_to(target)); it != block->end();) {
756+
auto& instr = *it;
757+
++it;
758+
instr.unlink();
759+
tail->Append(&instr);
760+
}
761+
762+
for (auto edge : tail->out_edges()) {
763+
edge->to()->fixupPhis(block, tail);
764+
}
765+
return tail;
766+
}
767+
768768
void CFG::splitCriticalEdges() {
769769
std::vector<Edge*> critical_edges;
770770

cinderx/Jit/hir/hir.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4000,11 +4000,6 @@ class BasicBlock {
40004000
explicit BasicBlock(int id_) : id(id_), cfg(nullptr) {}
40014001
~BasicBlock();
40024002

4003-
// Split this block after instr. Once split, this block will contain all
4004-
// instructions up to and including instr. A newly allocated block is returned
4005-
// that contains all instructions following instr.
4006-
BasicBlock* splitAfter(Instr& instr);
4007-
40084003
// Replace any references to old_pred in this block's Phis with new_pred.
40094004
void fixupPhis(BasicBlock* old_pred, BasicBlock* new_pred);
40104005
// Adds a new predecessor to the phi that follows from the old predecessor
@@ -4203,6 +4198,11 @@ class CFG {
42034198
// Remove block from the CFG
42044199
void RemoveBlock(BasicBlock* block);
42054200

4201+
// Split a block after instr. Once split, the block will contain all
4202+
// instructions up to and including instr. A newly allocated block is returned
4203+
// that contains all instructions following instr.
4204+
BasicBlock* splitAfter(Instr& target);
4205+
42064206
// Split any critical edges by inserting trampoline blocks.
42074207
void splitCriticalEdges();
42084208

cinderx/Jit/hir/inliner.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ void inlineFunctionCall(Function& caller, AbstractCall* call_instr) {
246246
caller.fullname);
247247

248248
BorrowedRef<PyCodeObject> callee_code{callee->func_code};
249-
BasicBlock* head = call_instr->instr->block();
250-
BasicBlock* tail = head->splitAfter(*call_instr->instr);
249+
BasicBlock* tail = caller.cfg.splitAfter(*call_instr->instr);
251250
auto begin_inlined_function = BeginInlinedFunction::create(
252251
callee, std::move(caller_frame_state), callee_name);
253252
auto callee_branch = Branch::create(result.entry);

cinderx/Jit/hir/simplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ struct Env {
167167
JIT_CHECK(
168168
cursor != block->begin(),
169169
"block should not be empty after calling do_branch()");
170-
BasicBlock* tail = block->splitAfter(*std::prev(cursor));
170+
BasicBlock* tail = func.cfg.splitAfter(*std::prev(cursor));
171171

172172
block = bb1;
173173
cursor = bb1->end();
@@ -212,7 +212,7 @@ struct Env {
212212
BasicBlock* slow_path = func.cfg.AllocateBlock();
213213

214214
auto branch = do_branch(slow_path);
215-
BasicBlock* fast_path = block->splitAfter(*branch);
215+
BasicBlock* fast_path = func.cfg.splitAfter(*branch);
216216
branch->set_true_bb(fast_path);
217217

218218
block = slow_path;

cinderx/RuntimeTests/hir_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ TEST(BasicBlockTest, SplitAfterSplitsBlockAfterInstruction) {
5757
head->append<LoadConst>(v0, TNoneType);
5858
Instr* load_const = head->GetTerminator();
5959
head->append<Return>(v0);
60-
BasicBlock* tail = head->splitAfter(*load_const);
60+
BasicBlock* tail = cfg.splitAfter(*load_const);
6161
ASSERT_NE(nullptr, head->GetTerminator());
6262
EXPECT_TRUE(head->GetTerminator()->IsLoadConst());
6363
ASSERT_NE(nullptr, tail->GetTerminator());

0 commit comments

Comments
 (0)