Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 159c53a

Browse files
committed
[ScopBuilder] Fix bug 38358 by preserving correct order of ScopStmts.
ScopBuilder::buildEqivClassBlockStmts creates ScopStmts for instruction groups in basic block and inserts these ScopStmts into Scop::StmtMap, however, as described in llvm.org/PR38358, comment #5, StmtScops are inserted into vector ScopStmt[BB] in wrong order. As a result, ScopBuilder::buildSchedule creates wrong order sequence node. Looking closer to code, it's clear there is no equivalent classes with interleaving isOrderedInstruction(memory access) instructions after joinOrderedInstructions. Afterwards, ScopStmts need to be created and inserted in the original order of memory access instructions, however, at the moment ScopStmts are inserted in the order of leader instructions which are probably not memory access instructions. The fix is simple with a standalone loop scanning isOrderedInstruction(memory access) instructions in basic block and inserting elements into LeaderToInstList one by one. The patch also removes double reversing operations which are now unnecessary. New test preserve-equiv-class-order-in-basic_block.ll is also added. Differential Revision: https://reviews.llvm.org/D68941 git-svn-id: https://llvm.org/svn/llvm-project/polly/trunk@375192 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 3c0043e commit 159c53a

File tree

2 files changed

+112
-6
lines changed

2 files changed

+112
-6
lines changed

lib/Analysis/ScopBuilder.cpp

+18-6
Original file line numberDiff line numberDiff line change
@@ -2119,14 +2119,27 @@ void ScopBuilder::buildEqivClassBlockStmts(BasicBlock *BB) {
21192119
joinOrderedPHIs(UnionFind, ModeledInsts);
21202120

21212121
// The list of instructions for statement (statement represented by the leader
2122-
// instruction). The order of statements instructions is reversed such that
2123-
// the epilogue is first. This makes it easier to ensure that the epilogue is
2124-
// the last statement.
2122+
// instruction).
21252123
MapVector<Instruction *, std::vector<Instruction *>> LeaderToInstList;
21262124

2125+
// The order of statements must be preserved w.r.t. their ordered
2126+
// instructions. Without this explicit scan, we would also use non-ordered
2127+
// instructions (whose order is arbitrary) to determine statement order.
2128+
for (Instruction &Inst : *BB) {
2129+
if (!isOrderedInstruction(&Inst))
2130+
continue;
2131+
2132+
auto LeaderIt = UnionFind.findLeader(&Inst);
2133+
if (LeaderIt == UnionFind.member_end())
2134+
continue;
2135+
2136+
// Insert element for the leader instruction.
2137+
(void)LeaderToInstList[*LeaderIt];
2138+
}
2139+
21272140
// Collect the instructions of all leaders. UnionFind's member iterator
21282141
// unfortunately are not in any specific order.
2129-
for (Instruction &Inst : reverse(*BB)) {
2142+
for (Instruction &Inst : *BB) {
21302143
auto LeaderIt = UnionFind.findLeader(&Inst);
21312144
if (LeaderIt == UnionFind.member_end())
21322145
continue;
@@ -2140,13 +2153,12 @@ void ScopBuilder::buildEqivClassBlockStmts(BasicBlock *BB) {
21402153
// Finally build the statements.
21412154
int Count = 0;
21422155
long BBIdx = scop->getNextStmtIdx();
2143-
for (auto &Instructions : reverse(LeaderToInstList)) {
2156+
for (auto &Instructions : LeaderToInstList) {
21442157
std::vector<Instruction *> &InstList = Instructions.second;
21452158

21462159
// If there is no main instruction, make the first statement the main.
21472160
bool IsMain = (MainInst ? MainLeader == Instructions.first : Count == 0);
21482161

2149-
std::reverse(InstList.begin(), InstList.end());
21502162
std::string Name = makeStmtName(BB, BBIdx, Count, IsMain);
21512163
scop->addScopStmt(BB, Name, L, std::move(InstList));
21522164
Count += 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
; RUN: opt %loadPolly -polly-stmt-granularity=scalar-indep -polly-print-instructions -polly-scops -analyze < %s | FileCheck %s -match-full-lines
2+
3+
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
4+
5+
@b = dso_local local_unnamed_addr global i32 1, align 4
6+
@e = dso_local local_unnamed_addr global i32 3, align 4
7+
@a = common dso_local local_unnamed_addr global [56 x i32] zeroinitializer, align 16
8+
@f = common dso_local local_unnamed_addr global i16 0, align 2
9+
@d = common dso_local local_unnamed_addr global i8 0, align 1
10+
11+
; Function Attrs: nounwind uwtable
12+
define dso_local i32 @func() {
13+
entry:
14+
br label %entry.split
15+
16+
entry.split: ; preds = %entry
17+
br label %for.body
18+
19+
for.body: ; preds = %for.body, %entry.split
20+
%indvars.iv = phi i64 [ 0, %entry.split ], [ %indvars.iv.next, %for.body ]
21+
%arrayidx = getelementptr inbounds [56 x i32], [56 x i32]* @a, i64 0, i64 %indvars.iv
22+
%0 = trunc i64 %indvars.iv to i32
23+
store i32 %0, i32* %arrayidx, align 4, !tbaa !0
24+
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
25+
%exitcond = icmp eq i64 %indvars.iv.next, 56
26+
br i1 %exitcond, label %for.end, label %for.body
27+
28+
for.end: ; preds = %for.body
29+
%1 = load i32, i32* @e, align 4, !tbaa !0
30+
store i32 2, i32* @e, align 4, !tbaa !0
31+
%2 = trunc i32 %1 to i16
32+
%conv = and i16 %2, 1
33+
%tobool = icmp eq i16 %conv, 0
34+
br label %for.body3
35+
36+
for.body3: ; preds = %for.end, %for.inc11
37+
%storemerge20 = phi i32 [ 2, %for.end ], [ %dec12, %for.inc11 ]
38+
%3 = load i8, i8* @d, align 1
39+
%cmp6 = icmp eq i8 %3, 8
40+
%or.cond = or i1 %tobool, %cmp6
41+
br i1 %or.cond, label %for.inc11, label %for.inc11.loopexit
42+
43+
for.inc11.loopexit: ; preds = %for.body3
44+
store i32 0, i32* @b, align 4, !tbaa !0
45+
store i8 8, i8* @d, align 1, !tbaa !4
46+
br label %for.inc11
47+
48+
for.inc11: ; preds = %for.inc11.loopexit, %for.body3
49+
%dec12 = add nsw i32 %storemerge20, -1
50+
%cmp2 = icmp sgt i32 %storemerge20, -18
51+
br i1 %cmp2, label %for.body3, label %for.end13
52+
53+
for.end13: ; preds = %for.inc11
54+
store i16 %conv, i16* @f, align 2, !tbaa !5
55+
store i32 -19, i32* @e, align 4, !tbaa !0
56+
%4 = load i32, i32* @b, align 4, !tbaa !0
57+
ret i32 %4
58+
}
59+
60+
!0 = !{!1, !1, i64 0}
61+
!1 = !{!"int", !2, i64 0}
62+
!2 = !{!"omnipotent char", !3, i64 0}
63+
!3 = !{!"Simple C/C++ TBAA"}
64+
!4 = !{!2, !2, i64 0}
65+
!5 = !{!6, !6, i64 0}
66+
!6 = !{!"short", !2, i64 0}
67+
68+
; CHECK: Stmt_for_end_a
69+
; CHECK-NEXT: Domain :=
70+
; CHECK-NEXT: { Stmt_for_end_a[] };
71+
; CHECK-NEXT: Schedule :=
72+
; CHECK-NEXT: { Stmt_for_end_a[] -> [1, 0] };
73+
; CHECK-NEXT: ReadAccess := [Reduction Type: NONE] [Scalar: 0]
74+
; CHECK-NEXT: { Stmt_for_end_a[] -> MemRef_e[0] };
75+
; CHECK-NEXT: MustWriteAccess := [Reduction Type: NONE] [Scalar: 1]
76+
; CHECK-NEXT: { Stmt_for_end_a[] -> MemRef_tobool[] };
77+
; CHECK-NEXT: MustWriteAccess := [Reduction Type: NONE] [Scalar: 1]
78+
; CHECK-NEXT: { Stmt_for_end_a[] -> MemRef_conv[] };
79+
; CHECK-NEXT: Instructions {
80+
; CHECK-NEXT: %1 = load i32, i32* @e, align 4, !tbaa !0
81+
; CHECK-NEXT: %2 = trunc i32 %1 to i16
82+
; CHECK-NEXT: %conv = and i16 %2, 1
83+
; CHECK-NEXT: %tobool = icmp eq i16 %conv, 0
84+
; CHECK-NEXT: }
85+
; CHECK-NEXT: Stmt_for_end
86+
; CHECK-NEXT: Domain :=
87+
; CHECK-NEXT: { Stmt_for_end[] };
88+
; CHECK-NEXT: Schedule :=
89+
; CHECK-NEXT: { Stmt_for_end[] -> [2, 0] };
90+
; CHECK-NEXT: MustWriteAccess := [Reduction Type: NONE] [Scalar: 0]
91+
; CHECK-NEXT: { Stmt_for_end[] -> MemRef_e[0] };
92+
; CHECK-NEXT: Instructions {
93+
; CHECK-NEXT: store i32 2, i32* @e, align 4, !tbaa !0
94+
; CHECK-NEXT: }

0 commit comments

Comments
 (0)