Skip to content

Commit b768152

Browse files
authored
Revert "[Attributor] Support nested conditional branches" (#170257)
Reverts #168532 Causing a crash in the flang-rt that needs to be investigated, see #170211.
1 parent 30219f0 commit b768152

File tree

6 files changed

+229
-234
lines changed

6 files changed

+229
-234
lines changed

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,7 @@ static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S,
665665
return;
666666

667667
SmallVector<const BranchInst *, 4> BrInsts;
668-
SmallPtrSet<const Instruction *, 16> Visited;
669668
auto Pred = [&](const Instruction *I) {
670-
if (!Visited.insert(I).second)
671-
return false;
672669
if (const BranchInst *Br = dyn_cast<BranchInst>(I))
673670
if (Br->isConditional())
674671
BrInsts.push_back(Br);
@@ -687,10 +684,28 @@ static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S,
687684
// ParentS_m = ChildS_{m, 1} /\ ChildS_{m, 2} /\ ... /\ ChildS_{m, n_m}
688685
//
689686
// Known State |= ParentS_1 \/ ParentS_2 \/... \/ ParentS_m
687+
//
688+
// FIXME: Currently, recursive branches are not handled. For example, we
689+
// can't deduce that ptr must be dereferenced in below function.
690+
//
691+
// void f(int a, int c, int *ptr) {
692+
// if(a)
693+
// if (b) {
694+
// *ptr = 0;
695+
// } else {
696+
// *ptr = 1;
697+
// }
698+
// else {
699+
// if (b) {
700+
// *ptr = 0;
701+
// } else {
702+
// *ptr = 1;
703+
// }
704+
// }
705+
// }
690706

691707
Explorer->checkForAllContext(&CtxI, Pred);
692-
while (!BrInsts.empty()) {
693-
const BranchInst *Br = BrInsts.pop_back_val();
708+
for (const BranchInst *Br : BrInsts) {
694709
StateType ParentState;
695710

696711
// The known state of the parent state is a conjunction of children's
@@ -699,18 +714,15 @@ static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S,
699714

700715
for (const BasicBlock *BB : Br->successors()) {
701716
StateType ChildState;
717+
702718
size_t BeforeSize = Uses.size();
703-
const Instruction *I = &BB->front();
704-
followUsesInContext(AA, A, *Explorer, I, Uses, ChildState);
719+
followUsesInContext(AA, A, *Explorer, &BB->front(), Uses, ChildState);
705720

706721
// Erase uses which only appear in the child.
707722
for (auto It = Uses.begin() + BeforeSize; It != Uses.end();)
708723
It = Uses.erase(It);
709724

710725
ParentState &= ChildState;
711-
712-
// Check for recursive conditional branches.
713-
Explorer->checkForAllContext(I, Pred);
714726
}
715727

716728
// Use only known state.

llvm/test/Transforms/Attributor/dereferenceable-1.ll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,12 @@ cont2:
555555
; *ptr = 4;
556556
; }
557557
; }
558+
;
559+
; FIXME: %ptr should be dereferenceable(4)
558560
define dso_local void @rec-branch-1(i32 %a, i32 %b, i32 %c, ptr %ptr) {
559561
; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write)
560562
; CHECK-LABEL: define {{[^@]+}}@rec-branch-1
561-
; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]], i32 [[C:%.*]], ptr nofree nonnull writeonly align 4 captures(none) dereferenceable(4) [[PTR:%.*]]) #[[ATTR3]] {
563+
; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]], i32 [[C:%.*]], ptr nofree writeonly captures(none) [[PTR:%.*]]) #[[ATTR3]] {
562564
; CHECK-NEXT: entry:
563565
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[A]], 0
564566
; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_ELSE3:%.*]], label [[IF_THEN:%.*]]
@@ -628,10 +630,11 @@ if.end8: ; preds = %if.then5, %if.else6
628630
; rec-branch-2(1, 1, 1, ptr);
629631
; }
630632
; }
633+
; FIXME: %ptr should be dereferenceable(4)
631634
define dso_local void @rec-branch-2(i32 %a, i32 %b, i32 %c, ptr %ptr) {
632635
; CHECK: Function Attrs: nofree nosync nounwind memory(argmem: write)
633636
; CHECK-LABEL: define {{[^@]+}}@rec-branch-2
634-
; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]], i32 [[C:%.*]], ptr nofree nonnull writeonly align 4 captures(none) dereferenceable(4) [[PTR:%.*]]) #[[ATTR5:[0-9]+]] {
637+
; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]], i32 [[C:%.*]], ptr nofree writeonly captures(none) [[PTR:%.*]]) #[[ATTR5:[0-9]+]] {
635638
; CHECK-NEXT: entry:
636639
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[A]], 0
637640
; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_ELSE3:%.*]], label [[IF_THEN:%.*]]
@@ -651,7 +654,7 @@ define dso_local void @rec-branch-2(i32 %a, i32 %b, i32 %c, ptr %ptr) {
651654
; CHECK-NEXT: store i32 3, ptr [[PTR]], align 4
652655
; CHECK-NEXT: br label [[IF_END8]]
653656
; CHECK: if.else6:
654-
; CHECK-NEXT: tail call void @rec-branch-2(i32 noundef 1, i32 noundef 1, i32 noundef 1, ptr nofree nonnull writeonly align 4 captures(none) dereferenceable(4) [[PTR]]) #[[ATTR8:[0-9]+]]
657+
; CHECK-NEXT: tail call void @rec-branch-2(i32 noundef 1, i32 noundef 1, i32 noundef 1, ptr nofree writeonly captures(none) [[PTR]]) #[[ATTR8:[0-9]+]]
655658
; CHECK-NEXT: br label [[IF_END8]]
656659
; CHECK: if.end8:
657660
; CHECK-NEXT: ret void

0 commit comments

Comments
 (0)