Skip to content

Commit 622c33f

Browse files
authored
Merge pull request numba#1025 from dlee992/skip_if_meet_raise_bb
skip `raise` basic blocks in `verifyFanoutBackward`
2 parents 42b9834 + 01d6aed commit 622c33f

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

ffi/custom_passes.cpp

+35-4
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ struct RefPrunePass : public FunctionPass {
400400
SmallBBSet tail_nodes;
401401
tail_nodes.insert(decref->getParent());
402402
if (!verifyFanoutBackward(incref, incref->getParent(),
403-
&tail_nodes))
403+
&tail_nodes, false))
404+
404405
continue;
405406

406407
// scan the CFG between the incref and decref BBs, if
@@ -495,6 +496,29 @@ struct RefPrunePass : public FunctionPass {
495496
* │ MORE CFG │
496497
* └────────────┘
497498
*
499+
* a complex pattern about fanout-raise
500+
* https://github.com/numba/llvmlite/issues/1023
501+
* ┌────────────┐
502+
* │ incref │
503+
* │ incref │
504+
* └────────────┘
505+
* / \
506+
* / \
507+
* ┌────────────┐ \
508+
* │ decref | \
509+
* └────────────┘ \
510+
* / \ \
511+
* / \ \
512+
* ┌────────────┐ ┌────────────┐ \
513+
* │ decref | │ incref | \
514+
* └────────────┘ └────────────┘ \
515+
* / \ \
516+
* / \ \
517+
* ┌────────────┐ ┌────────────┐
518+
* │ decref | │ raise |
519+
* │ decref | └────────────┘
520+
* └────────────┘
521+
*
498522
* Parameters:
499523
* - F a Function
500524
* - prune_raise_exit, if false case 1 is considered, if true case 2 is
@@ -648,10 +672,12 @@ struct RefPrunePass : public FunctionPass {
648672
for (BasicBlock *bb : *decref_blocks) {
649673
raising_blocks.insert(bb);
650674
}
651-
if (verifyFanoutBackward(incref, head_node, p_raising_blocks))
675+
if (verifyFanoutBackward(incref, head_node, p_raising_blocks,
676+
prune_raise_exit))
652677
return true;
653678

654-
} else if (verifyFanoutBackward(incref, head_node, decref_blocks)) {
679+
} else if (verifyFanoutBackward(incref, head_node, decref_blocks,
680+
prune_raise_exit)) {
655681
return true;
656682
}
657683
}
@@ -844,7 +870,8 @@ struct RefPrunePass : public FunctionPass {
844870
*
845871
*/
846872
bool verifyFanoutBackward(CallInst *incref, BasicBlock *head_node,
847-
const SmallBBSet *tail_nodes) {
873+
const SmallBBSet *tail_nodes,
874+
bool prune_raise_exit) {
848875
// push the tail nodes into a work list
849876
SmallVector<BasicBlock *, 10> todo;
850877
for (BasicBlock *bb : *tail_nodes) {
@@ -864,6 +891,10 @@ struct RefPrunePass : public FunctionPass {
864891
while (workstack.size() > 0) {
865892
// Get a basic block
866893
BasicBlock *cur_node = workstack.pop_back_val();
894+
// If cur_node is a raising block, then skip it
895+
if (prune_raise_exit && isRaising(cur_node)) {
896+
continue;
897+
}
867898
// if the block has been seen before then skip
868899
if (visited.count(cur_node)) {
869900
// Already visited

llvmlite/tests/test_refprune.py

+31
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,37 @@ def test_fanout_raise_5(self):
521521
mod, stats = self.check(self.fanout_raise_5)
522522
self.assertEqual(stats.fanout_raise, 2)
523523

524+
# test case 6 is from https://github.com/numba/llvmlite/issues/1023
525+
fanout_raise_6 = r"""
526+
define i32 @main(i8* %ptr, i1 %cond1, i1 %cond2, i1 %cond3, i8** %excinfo) {
527+
bb_A:
528+
call void @NRT_incref(i8* %ptr)
529+
call void @NRT_incref(i8* %ptr)
530+
br i1 %cond1, label %bb_B, label %bb_C
531+
bb_B:
532+
call void @NRT_decref(i8* %ptr)
533+
br i1 %cond2, label %bb_D, label %bb_E
534+
bb_C:
535+
store i8* null, i8** %excinfo, !numba_exception_output !0
536+
ret i32 1
537+
bb_D:
538+
call void @NRT_decref(i8* %ptr)
539+
ret i32 0
540+
bb_E:
541+
call void @NRT_incref(i8* %ptr)
542+
br i1 %cond3, label %bb_F, label %bb_C
543+
bb_F:
544+
call void @NRT_decref(i8* %ptr)
545+
call void @NRT_decref(i8* %ptr)
546+
ret i32 0
547+
}
548+
!0 = !{i1 1}
549+
"""
550+
551+
def test_fanout_raise_6(self):
552+
mod, stats = self.check(self.fanout_raise_6)
553+
self.assertEqual(stats.fanout_raise, 7)
554+
524555

525556
if __name__ == '__main__':
526557
unittest.main()

0 commit comments

Comments
 (0)