Skip to content

Commit e6a1187

Browse files
committed
Limit the recursion depth of SelectionDAG::isSplatValue()
This method previously always recursively checked both the left-hand side and right-hand side of binary operations for splatted (broadcast) vector values to determine if the parent DAG node is a splat. Like several other SelectionDAG methods, limit the recursion depth to MaxRecursionDepth (6). This prevents stack overflow. See also https://issuetracker.google.com/173785481 Patch by Nicolas Capens. Thanks! Differential Revision: https://reviews.llvm.org/D92421
1 parent be9b4bb commit e6a1187

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

llvm/include/llvm/CodeGen/SelectionDAG.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,8 @@ class SelectionDAG {
18291829
/// for \p DemandedElts.
18301830
///
18311831
/// NOTE: The function will return true for a demanded splat of UNDEF values.
1832-
bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts);
1832+
bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts,
1833+
unsigned Depth = 0);
18331834

18341835
/// Test whether \p V has a splatted value.
18351836
bool isSplatValue(SDValue V, bool AllowUndefs = false);

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,13 +2355,16 @@ bool SelectionDAG::MaskedValueIsAllOnes(SDValue V, const APInt &Mask,
23552355
/// sense to specify which elements are demanded or undefined, therefore
23562356
/// they are simply ignored.
23572357
bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2358-
APInt &UndefElts) {
2358+
APInt &UndefElts, unsigned Depth) {
23592359
EVT VT = V.getValueType();
23602360
assert(VT.isVector() && "Vector type expected");
23612361

23622362
if (!VT.isScalableVector() && !DemandedElts)
23632363
return false; // No demanded elts, better to assume we don't know anything.
23642364

2365+
if (Depth >= MaxRecursionDepth)
2366+
return false; // Limit search depth.
2367+
23652368
// Deal with some common cases here that work for both fixed and scalable
23662369
// vector types.
23672370
switch (V.getOpcode()) {
@@ -2376,8 +2379,8 @@ bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
23762379
APInt UndefLHS, UndefRHS;
23772380
SDValue LHS = V.getOperand(0);
23782381
SDValue RHS = V.getOperand(1);
2379-
if (isSplatValue(LHS, DemandedElts, UndefLHS) &&
2380-
isSplatValue(RHS, DemandedElts, UndefRHS)) {
2382+
if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2383+
isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
23812384
UndefElts = UndefLHS | UndefRHS;
23822385
return true;
23832386
}
@@ -2386,7 +2389,7 @@ bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
23862389
case ISD::TRUNCATE:
23872390
case ISD::SIGN_EXTEND:
23882391
case ISD::ZERO_EXTEND:
2389-
return isSplatValue(V.getOperand(0), DemandedElts, UndefElts);
2392+
return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
23902393
}
23912394

23922395
// We don't support other cases than those above for scalable vectors at
@@ -2441,7 +2444,7 @@ bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
24412444
unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
24422445
APInt UndefSrcElts;
24432446
APInt DemandedSrcElts = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
2444-
if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts)) {
2447+
if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
24452448
UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
24462449
return true;
24472450
}

0 commit comments

Comments
 (0)