Skip to content

Commit 027c036

Browse files
committed
[Polly] Reject regions entered by an indirectbr/callbr.
SplitBlockPredecessors is unable to insert an additional BasicBlock between an indirectbr/callbr terminator and the successor blocks. This is needed by Polly to normalize the control flow before emitting its optimzed code. This patches rejects regions entered by an indirectbr/callbr to not fail later at code generation. This fixes llvm.org/PR51964 Recommit with "REQUIRES: asserts" in test that uses statistics.
1 parent 9451d9d commit 027c036

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

polly/include/polly/ScopDetectionDiagnostic.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ enum class RejectReasonKind {
7373
InvalidTerminator,
7474
IrreducibleRegion,
7575
UnreachableInExit,
76+
IndirectPredecessor,
7677
LastCFG,
7778

7879
// Non-Affinity
@@ -275,6 +276,32 @@ class ReportUnreachableInExit : public ReportCFG {
275276
//@}
276277
};
277278

279+
//===----------------------------------------------------------------------===//
280+
/// Captures regions with an IndirectBr predecessor.
281+
class ReportIndirectPredecessor : public ReportCFG {
282+
Instruction *Inst;
283+
DebugLoc DbgLoc;
284+
285+
public:
286+
ReportIndirectPredecessor(Instruction *Inst, DebugLoc DbgLoc)
287+
: ReportCFG(RejectReasonKind::IndirectPredecessor), Inst(Inst),
288+
DbgLoc(DbgLoc) {}
289+
290+
/// @name LLVM-RTTI interface
291+
//@{
292+
static bool classof(const RejectReason *RR);
293+
//@}
294+
295+
/// @name RejectReason interface
296+
//@{
297+
std::string getRemarkName() const override;
298+
const Value *getRemarkBB() const override;
299+
std::string getMessage() const override;
300+
std::string getEndUserMessage() const override;
301+
const DebugLoc &getDebugLoc() const override;
302+
//@}
303+
};
304+
278305
//===----------------------------------------------------------------------===//
279306
/// Base class for non-affine reject reasons.
280307
///

polly/lib/Analysis/ScopDetection.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,13 @@ bool ScopDetection::isValidRegion(DetectionContext &Context) {
17571757
return false;
17581758
}
17591759

1760+
for (BasicBlock *Pred : predecessors(CurRegion.getEntry())) {
1761+
Instruction *PredTerm = Pred->getTerminator();
1762+
if (isa<IndirectBrInst>(PredTerm) || isa<CallBrInst>(PredTerm))
1763+
return invalid<ReportIndirectPredecessor>(
1764+
Context, /*Assert=*/true, PredTerm, PredTerm->getDebugLoc());
1765+
}
1766+
17601767
// SCoP cannot contain the entry block of the function, because we need
17611768
// to insert alloca instruction there when translate scalar to array.
17621769
if (!PollyAllowFullFunction &&

polly/lib/Analysis/ScopDetectionDiagnostic.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ using namespace llvm;
5050
static Statistic RejectStatistics[] = {
5151
SCOP_STAT(CFG, ""),
5252
SCOP_STAT(InvalidTerminator, "Unsupported terminator instruction"),
53-
SCOP_STAT(UnreachableInExit, "Unreachable in exit block"),
5453
SCOP_STAT(IrreducibleRegion, "Irreducible loops"),
54+
SCOP_STAT(UnreachableInExit, "Unreachable in exit block"),
55+
SCOP_STAT(IndirectPredecessor, "Branch from indirect terminator"),
5556
SCOP_STAT(LastCFG, ""),
5657
SCOP_STAT(AffFunc, ""),
5758
SCOP_STAT(UndefCond, "Undefined branch condition"),
@@ -239,6 +240,37 @@ bool ReportUnreachableInExit::classof(const RejectReason *RR) {
239240
return RR->getKind() == RejectReasonKind::UnreachableInExit;
240241
}
241242

243+
//===----------------------------------------------------------------------===//
244+
// IndirectPredecessor.
245+
246+
std::string ReportIndirectPredecessor::getRemarkName() const {
247+
return "IndirectPredecessor";
248+
}
249+
250+
const Value *ReportIndirectPredecessor::getRemarkBB() const {
251+
if (Inst)
252+
return Inst->getParent();
253+
return nullptr;
254+
}
255+
256+
std::string ReportIndirectPredecessor::getMessage() const {
257+
if (Inst)
258+
return "Branch from indirect terminator: " + *Inst;
259+
return getEndUserMessage();
260+
}
261+
262+
const DebugLoc &ReportIndirectPredecessor::getDebugLoc() const {
263+
return DbgLoc;
264+
}
265+
266+
std::string ReportIndirectPredecessor::getEndUserMessage() const {
267+
return "Branch from indirect terminator.";
268+
}
269+
270+
bool ReportIndirectPredecessor::classof(const RejectReason *RR) {
271+
return RR->getKind() == RejectReasonKind::IndirectPredecessor;
272+
}
273+
242274
//===----------------------------------------------------------------------===//
243275
// ReportIrreducibleRegion.
244276

polly/test/ScopDetect/callbr.ll

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; RUN: opt %loadPolly -polly-detect -polly-detect-track-failures -disable-output -pass-remarks-missed=polly-detect < %s 2>&1 | FileCheck %s --check-prefix=REMARK
2+
; RUN: opt %loadPolly -polly-detect -polly-detect-track-failures -disable-output -stats < %s 2>&1 | FileCheck %s --check-prefix=STAT
3+
; REQUIRES: asserts
4+
5+
; REMARK: Branch from indirect terminator.
6+
7+
; STAT: 1 polly-detect - Number of rejected regions: Branch from indirect terminator
8+
9+
10+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
11+
target triple = "x86_64-unknown-linux-gnu"
12+
13+
define void @func(i32 %n, double* noalias nonnull %A) {
14+
entry:
15+
callbr void asm sideeffect "", "X,~{dirflag},~{fpsr},~{flags}"(i8* blockaddress(@func, %for)) #1
16+
to label %fallthrough [label %for]
17+
18+
fallthrough:
19+
br label %for
20+
21+
for:
22+
%j = phi i32 [0, %entry], [0, %fallthrough], [%j.inc, %inc]
23+
%j.cmp = icmp slt i32 %j, %n
24+
br i1 %j.cmp, label %body, label %exit
25+
26+
body:
27+
store double 42.0, double* %A
28+
br label %inc
29+
30+
inc:
31+
%j.inc = add nuw nsw i32 %j, 1
32+
br label %for
33+
34+
exit:
35+
br label %return
36+
37+
return:
38+
ret void
39+
}

0 commit comments

Comments
 (0)