Skip to content

Commit 3337483

Browse files
!fixup squashed review (squashed to simplify rebase hell)
1 parent bbaac6f commit 3337483

File tree

16 files changed

+3245
-5544
lines changed

16 files changed

+3245
-5544
lines changed

llvm/include/llvm/Analysis/CSADescriptors.h

Lines changed: 0 additions & 78 deletions
This file was deleted.

llvm/include/llvm/Analysis/IVDescriptors.h

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file "describes" induction and recurrence variables.
9+
// This file "describes" induction, recurrence, and conditional scalar
10+
// assignment (CSA) variables.
1011
//
1112
//===----------------------------------------------------------------------===//
1213

@@ -391,6 +392,61 @@ class InductionDescriptor {
391392
SmallVector<Instruction *, 2> RedundantCasts;
392393
};
393394

395+
/// A Conditional Scalar Assignment (CSA) is an assignment from an initial
396+
/// scalar that may or may not occur.
397+
class CSADescriptor {
398+
/// If the conditional assignment occurs inside a loop, then Phi chooses
399+
/// the value of the assignment from the entry block or the loop body block.
400+
PHINode *Phi = nullptr;
401+
402+
/// The initial value of the CSA. If the condition guarding the assignment is
403+
/// not met, then the assignment retains this value.
404+
Value *InitScalar = nullptr;
405+
406+
/// The Instruction that conditionally assigned to inside the loop.
407+
Instruction *Assignment = nullptr;
408+
409+
/// Create a CSA Descriptor that models a valid CSA with its members
410+
/// initialized correctly.
411+
CSADescriptor(PHINode *Phi, Instruction *Assignment, Value *InitScalar)
412+
: Phi(Phi), InitScalar(InitScalar), Assignment(Assignment) {}
413+
414+
public:
415+
/// Create a CSA Descriptor that models an invalid CSA.
416+
CSADescriptor() = default;
417+
418+
/// If Phi is the root of a CSA, set CSADesc as the CSA rooted by
419+
/// Phi. Otherwise, return a false, leaving CSADesc unmodified.
420+
static bool isCSAPhi(PHINode *Phi, Loop *TheLoop, CSADescriptor &CSADesc);
421+
422+
operator bool() const { return isValid(); }
423+
424+
/// Returns whether SI is the Assignment in CSA
425+
static bool isCSASelect(CSADescriptor Desc, SelectInst *SI) {
426+
return Desc.getAssignment() == SI;
427+
}
428+
429+
/// Return whether this CSADescriptor models a valid CSA.
430+
bool isValid() const { return Phi && InitScalar && Assignment; }
431+
432+
/// Return the PHI that roots this CSA.
433+
PHINode *getPhi() const { return Phi; }
434+
435+
/// Return the initial value of the CSA. This is the value if the conditional
436+
/// assignment does not occur.
437+
Value *getInitScalar() const { return InitScalar; }
438+
439+
/// The Instruction that is used after the loop
440+
Instruction *getAssignment() const { return Assignment; }
441+
442+
/// Return the condition that this CSA is conditional upon.
443+
Value *getCond() const {
444+
if (auto *SI = dyn_cast_or_null<SelectInst>(Assignment))
445+
return SI->getCondition();
446+
return nullptr;
447+
}
448+
};
449+
394450
} // end namespace llvm
395451

396452
#endif // LLVM_ANALYSIS_IVDESCRIPTORS_H

llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#define LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZATIONLEGALITY_H
2828

2929
#include "llvm/ADT/MapVector.h"
30-
#include "llvm/Analysis/CSADescriptors.h"
3130
#include "llvm/Analysis/LoopAccessAnalysis.h"
3231
#include "llvm/Support/TypeSize.h"
3332
#include "llvm/Transforms/Utils/LoopUtils.h"

llvm/lib/Analysis/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ add_llvm_component_library(LLVMAnalysis
4646
CostModel.cpp
4747
CodeMetrics.cpp
4848
ConstantFolding.cpp
49-
CSADescriptors.cpp
5049
CtxProfAnalysis.cpp
5150
CycleAnalysis.cpp
5251
DDG.cpp

llvm/lib/Analysis/CSADescriptors.cpp

Lines changed: 0 additions & 73 deletions
This file was deleted.

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file "describes" induction and recurrence variables.
9+
// This file "describes" induction, recurrence, and conditional scalar
10+
// assignment (CSA) variables.
1011
//
1112
//===----------------------------------------------------------------------===//
1213

@@ -1467,3 +1468,58 @@ bool InductionDescriptor::isInductionPHI(
14671468
D = InductionDescriptor(StartValue, IK_PtrInduction, Step);
14681469
return true;
14691470
}
1471+
1472+
/// Return CSADescriptor that describes a CSA that matches one of these
1473+
/// patterns:
1474+
/// phi loop_inv, (select cmp, value, phi)
1475+
/// phi loop_inv, (select cmp, phi, value)
1476+
/// phi (select cmp, value, phi), loop_inv
1477+
/// phi (select cmp, phi, value), loop_inv
1478+
/// If the CSA does not match any of these paterns, return a CSADescriptor
1479+
/// that describes an InvalidCSA.
1480+
bool CSADescriptor::isCSAPhi(PHINode *Phi, Loop *TheLoop, CSADescriptor &CSA) {
1481+
1482+
// Must be a scalar
1483+
Type *Type = Phi->getType();
1484+
if (!Type->isIntegerTy() && !Type->isFloatingPointTy() &&
1485+
!Type->isPointerTy())
1486+
return false;
1487+
1488+
// Match phi loop_inv, (select cmp, value, phi)
1489+
// or phi loop_inv, (select cmp, phi, value)
1490+
// or phi (select cmp, value, phi), loop_inv
1491+
// or phi (select cmp, phi, value), loop_inv
1492+
if (Phi->getNumIncomingValues() != 2)
1493+
return false;
1494+
auto SelectInstIt = find_if(Phi->incoming_values(), [&Phi](const Use &U) {
1495+
return match(U.get(), m_Select(m_Value(), m_Specific(Phi), m_Value())) ||
1496+
match(U.get(), m_Select(m_Value(), m_Value(), m_Specific(Phi)));
1497+
});
1498+
if (SelectInstIt == Phi->incoming_values().end())
1499+
return false;
1500+
auto LoopInvIt = find_if(Phi->incoming_values(), [&](Use &U) {
1501+
return U.get() != *SelectInstIt && TheLoop->isLoopInvariant(U.get());
1502+
});
1503+
if (LoopInvIt == Phi->incoming_values().end())
1504+
return false;
1505+
1506+
// Phi or Sel must be used only outside the loop,
1507+
// excluding if Phi use Sel or Sel use Phi
1508+
auto IsOnlyUsedOutsideLoop = [&](Value *V, Value *Ignore) {
1509+
return all_of(V->users(), [Ignore, TheLoop](User *U) {
1510+
if (U == Ignore)
1511+
return true;
1512+
if (auto *I = dyn_cast<Instruction>(U))
1513+
return !TheLoop->contains(I);
1514+
return true;
1515+
});
1516+
};
1517+
Instruction *Select = cast<SelectInst>(SelectInstIt->get());
1518+
Value *LoopInv = LoopInvIt->get();
1519+
if (!IsOnlyUsedOutsideLoop(Phi, Select) ||
1520+
!IsOnlyUsedOutsideLoop(Select, Phi))
1521+
return false;
1522+
1523+
CSA = CSADescriptor(Phi, Select, LoopInv);
1524+
return true;
1525+
}

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,8 @@ bool LoopVectorizationLegality::canVectorizeInstrs() {
891891
// Check if the PHI can be classified as a CSA PHI.
892892
if (EnableCSA || (TTI->enableCSAVectorization() &&
893893
EnableCSA.getNumOccurrences() == 0)) {
894-
if (auto CSADesc = CSADescriptor::isCSAPhi(Phi, TheLoop)) {
894+
CSADescriptor CSADesc;
895+
if (CSADescriptor::isCSAPhi(Phi, TheLoop, CSADesc)) {
895896
addCSAPhi(Phi, CSADesc, AllowedExit);
896897
continue;
897898
}
@@ -1875,7 +1876,7 @@ bool LoopVectorizationLegality::canFoldTailByMasking() const {
18751876
// TODO: handle non-reduction outside users when tail is folded by masking.
18761877
for (auto *AE : AllowedExit) {
18771878
// Check that all users of allowed exit values are inside the loop or
1878-
// are the live-out of a reduction or a CSA
1879+
// are the live-out of a reduction or a CSA.
18791880
if (ReductionLiveOuts.count(AE) || CSALiveOuts.count(AE))
18801881
continue;
18811882
for (User *U : AE->users()) {

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ class VPBuilder {
174174
new VPInstruction(Opcode, Operands, WrapFlags, DL, Name));
175175
}
176176

177-
VPValue *createNot(VPValue *Operand, DebugLoc DL = {},
178-
const Twine &Name = "") {
177+
VPInstruction *createNot(VPValue *Operand, DebugLoc DL = {},
178+
const Twine &Name = "") {
179179
return createInstruction(VPInstruction::Not, {Operand}, DL, Name);
180180
}
181181

@@ -231,6 +231,39 @@ class VPBuilder {
231231
Ptr, Offset, VPRecipeWithIRFlags::GEPFlagsTy(true), DL, Name));
232232
}
233233

234+
VPInstruction *createCSAMaskPhi(VPValue *InitMask, DebugLoc DL,
235+
const Twine &Name) {
236+
return createInstruction(VPInstruction::CSAMaskPhi, {InitMask}, DL, Name);
237+
}
238+
239+
VPInstruction *createAnyActive(VPValue *Cond, DebugLoc DL,
240+
const Twine &Name) {
241+
return createInstruction(VPInstruction::AnyActive, {Cond}, DL, Name);
242+
}
243+
244+
VPInstruction *createCSAMaskSel(VPValue *Cond, VPValue *MaskPhi,
245+
VPValue *AnyActive, DebugLoc DL,
246+
const Twine &Name) {
247+
return createInstruction(VPInstruction::CSAMaskSel,
248+
{Cond, MaskPhi, AnyActive}, DL, Name);
249+
}
250+
251+
VPInstruction *createAnyActiveEVL(VPValue *Cond, VPValue *EVL, DebugLoc DL,
252+
const Twine &Name) {
253+
return createInstruction(VPInstruction::AnyActiveEVL, {Cond, EVL}, DL,
254+
Name);
255+
}
256+
257+
VPInstruction *createCSAVLPhi(DebugLoc DL, const Twine &Name) {
258+
return createInstruction(VPInstruction::CSAVLPhi, {}, DL, Name);
259+
}
260+
261+
VPInstruction *createCSAVLSel(VPValue *AnyActiveEVL, VPValue *VLPhi,
262+
VPValue *EVL, DebugLoc DL, const Twine &Name) {
263+
return createInstruction(VPInstruction::CSAVLSel,
264+
{AnyActiveEVL, VLPhi, EVL}, DL, Name);
265+
}
266+
234267
VPDerivedIVRecipe *createDerivedIV(InductionDescriptor::InductionKind Kind,
235268
FPMathOperator *FPBinOp, VPValue *Start,
236269
VPCanonicalIVPHIRecipe *CanonicalIV,

0 commit comments

Comments
 (0)