Skip to content

Commit ab17128

Browse files
fixup! rename CSA to ConditionalScalarAssignment
1 parent 47a9b69 commit ab17128

15 files changed

+231
-173
lines changed

llvm/include/llvm/Analysis/IVDescriptors.h

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88
//
99
// This file "describes" induction, recurrence, and conditional scalar
10-
// assignment (CSA) variables.
10+
// assignment variables.
1111
//
1212
//===----------------------------------------------------------------------===//
1313

@@ -424,54 +424,64 @@ class InductionDescriptor {
424424
SmallVector<Instruction *, 2> RedundantCasts;
425425
};
426426

427-
/// A Conditional Scalar Assignment (CSA) is an assignment from an initial
427+
/// A Conditional Scalar Assignment is an assignment from an initial
428428
/// scalar that may or may not occur.
429-
class CSADescriptor {
429+
class ConditionalScalarAssignmentDescriptor {
430430
/// If the conditional assignment occurs inside a loop, then Phi chooses
431431
/// the value of the assignment from the entry block or the loop body block.
432432
PHINode *Phi = nullptr;
433433

434-
/// The initial value of the CSA. If the condition guarding the assignment is
435-
/// not met, then the assignment retains this value.
434+
/// The initial value of the ConditionalScalarAssignment. If the condition
435+
/// guarding the assignment is not met, then the assignment retains this
436+
/// value.
436437
Value *InitScalar = nullptr;
437438

438439
/// The Instruction that conditionally assigned to inside the loop.
439440
Instruction *Assignment = nullptr;
440441

441-
/// Create a CSA Descriptor that models a valid CSA with its members
442-
/// initialized correctly.
443-
CSADescriptor(PHINode *Phi, Instruction *Assignment, Value *InitScalar)
442+
/// Create a ConditionalScalarAssignmentDescriptor that models a valid
443+
/// conditional scalar assignment with its members initialized correctly.
444+
ConditionalScalarAssignmentDescriptor(PHINode *Phi, Instruction *Assignment,
445+
Value *InitScalar)
444446
: Phi(Phi), InitScalar(InitScalar), Assignment(Assignment) {}
445447

446448
public:
447-
/// Create a CSA Descriptor that models an invalid CSA.
448-
CSADescriptor() = default;
449-
450-
/// If Phi is the root of a CSA, set CSADesc as the CSA rooted by
451-
/// Phi. Otherwise, return a false, leaving CSADesc unmodified.
452-
static bool isCSAPhi(PHINode *Phi, Loop *TheLoop, CSADescriptor &CSADesc);
449+
/// Create a ConditionalScalarAssignmentDescriptor that models an invalid
450+
/// ConditionalScalarAssignment.
451+
ConditionalScalarAssignmentDescriptor() = default;
452+
453+
/// If Phi is the root of a ConditionalScalarAssignment, set
454+
/// ConditionalScalarAssignmentDesc as the ConditionalScalarAssignment rooted
455+
/// by Phi. Otherwise, return a false, leaving ConditionalScalarAssignmentDesc
456+
/// unmodified.
457+
static bool isConditionalScalarAssignmentPhi(
458+
PHINode *Phi, Loop *TheLoop,
459+
ConditionalScalarAssignmentDescriptor &Desc);
453460

454461
operator bool() const { return isValid(); }
455462

456-
/// Returns whether SI is the Assignment in CSA
457-
static bool isCSASelect(CSADescriptor Desc, SelectInst *SI) {
463+
/// Returns whether SI is the Assignment in ConditionalScalarAssignment
464+
static bool isConditionalScalarAssignmentSelect(
465+
ConditionalScalarAssignmentDescriptor Desc, SelectInst *SI) {
458466
return Desc.getAssignment() == SI;
459467
}
460468

461-
/// Return whether this CSADescriptor models a valid CSA.
469+
/// Return whether this ConditionalScalarAssignmentDescriptor models a valid
470+
/// ConditionalScalarAssignment.
462471
bool isValid() const { return Phi && InitScalar && Assignment; }
463472

464-
/// Return the PHI that roots this CSA.
473+
/// Return the PHI that roots this ConditionalScalarAssignment.
465474
PHINode *getPhi() const { return Phi; }
466475

467-
/// Return the initial value of the CSA. This is the value if the conditional
468-
/// assignment does not occur.
476+
/// Return the initial value of the ConditionalScalarAssignment. This is the
477+
/// value if the conditional assignment does not occur.
469478
Value *getInitScalar() const { return InitScalar; }
470479

471480
/// The Instruction that is used after the loop
472481
Instruction *getAssignment() const { return Assignment; }
473482

474-
/// Return the condition that this CSA is conditional upon.
483+
/// Return the condition that this ConditionalScalarAssignment is conditional
484+
/// upon.
475485
Value *getCond() const {
476486
if (auto *SI = dyn_cast_or_null<SelectInst>(Assignment))
477487
return SI->getCondition();

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,7 @@ class TargetTransformInfo {
18541854

18551855
/// \returns true if the loop vectorizer should vectorize conditional
18561856
/// scalar assignments for the target.
1857-
bool enableCSAVectorization() const;
1857+
bool enableConditionalScalarAssignmentVectorization() const;
18581858

18591859
/// \returns How the target needs this vector-predicated operation to be
18601860
/// transformed.
@@ -2309,7 +2309,7 @@ class TargetTransformInfo::Concept {
23092309
SmallVectorImpl<Use *> &OpsToSink) const = 0;
23102310

23112311
virtual bool isVectorShiftByScalarCheap(Type *Ty) const = 0;
2312-
virtual bool enableCSAVectorization() const = 0;
2312+
virtual bool enableConditionalScalarAssignmentVectorization() const = 0;
23132313
virtual VPLegalization
23142314
getVPLegalizationStrategy(const VPIntrinsic &PI) const = 0;
23152315
virtual bool hasArmWideBranch(bool Thumb) const = 0;
@@ -3135,8 +3135,8 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
31353135
return Impl.isVectorShiftByScalarCheap(Ty);
31363136
}
31373137

3138-
bool enableCSAVectorization() const override {
3139-
return Impl.enableCSAVectorization();
3138+
bool enableConditionalScalarAssignmentVectorization() const override {
3139+
return Impl.enableConditionalScalarAssignmentVectorization();
31403140
}
31413141

31423142
VPLegalization

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ class TargetTransformInfoImplBase {
10301030

10311031
bool isVectorShiftByScalarCheap(Type *Ty) const { return false; }
10321032

1033-
bool enableCSAVectorization() const { return false; }
1033+
bool enableConditionalScalarAssignmentVectorization() const { return false; }
10341034

10351035
TargetTransformInfo::VPLegalization
10361036
getVPLegalizationStrategy(const VPIntrinsic &PI) const {

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,11 @@ class LoopVectorizationLegality {
269269
/// induction descriptor.
270270
using InductionList = MapVector<PHINode *, InductionDescriptor>;
271271

272-
/// CSAList contains the CSA descriptors for all the CSAs that were found
273-
/// in the loop, rooted by their phis.
274-
using CSAList = MapVector<PHINode *, CSADescriptor>;
272+
/// ConditionalScalarAssignmentList contains the
273+
/// ConditionalScalarAssignmentDescriptors for all the conditional scalar
274+
/// assignments that were found in the loop, rooted by their phis.
275+
using ConditionalScalarAssignmentList =
276+
MapVector<PHINode *, ConditionalScalarAssignmentDescriptor>;
275277

276278
/// RecurrenceSet contains the phi nodes that are recurrences other than
277279
/// inductions and reductions.
@@ -325,11 +327,17 @@ class LoopVectorizationLegality {
325327
/// Returns True if V is a Phi node of an induction variable in this loop.
326328
bool isInductionPhi(const Value *V) const;
327329

328-
/// Returns the CSAs found in the loop.
329-
const CSAList &getCSAs() const { return CSAs; }
330+
/// Returns the conditional scalar assignments found in the loop.
331+
const ConditionalScalarAssignmentList &
332+
getConditionalScalarAssignments() const {
333+
return ConditionalScalarAssignments;
334+
}
330335

331-
/// Returns true if Phi is the root of a CSA in the loop.
332-
bool isCSAPhi(PHINode *Phi) const { return CSAs.count(Phi) != 0; }
336+
/// Returns true if Phi is the root of a conditional scalar assignments in the
337+
/// loop.
338+
bool isConditionalScalarAssignmentPhi(PHINode *Phi) const {
339+
return ConditionalScalarAssignments.count(Phi) != 0;
340+
}
333341

334342
/// Returns a pointer to the induction descriptor, if \p Phi is an integer or
335343
/// floating point induction.
@@ -560,9 +568,11 @@ class LoopVectorizationLegality {
560568
void addInductionPhi(PHINode *Phi, const InductionDescriptor &ID,
561569
SmallPtrSetImpl<Value *> &AllowedExit);
562570

563-
/// Updates the vetorization state by adding \p Phi to the CSA list.
564-
void addCSAPhi(PHINode *Phi, const CSADescriptor &CSADesc,
565-
SmallPtrSetImpl<Value *> &AllowedExit);
571+
/// Updates the vetorization state by adding \p Phi to the
572+
/// ConditionalScalarAssignment list.
573+
void addConditionalScalarAssignmentPhi(
574+
PHINode *Phi, const ConditionalScalarAssignmentDescriptor &Desc,
575+
SmallPtrSetImpl<Value *> &AllowedExit);
566576

567577
/// The loop that we evaluate.
568578
Loop *TheLoop;
@@ -609,7 +619,7 @@ class LoopVectorizationLegality {
609619
InductionList Inductions;
610620

611621
/// Holds the conditional scalar assignments
612-
CSAList CSAs;
622+
ConditionalScalarAssignmentList ConditionalScalarAssignments;
613623

614624
/// Holds all the casts that participate in the update chain of the induction
615625
/// variables, and that have been proven to be redundant (possibly under a

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88
//
99
// This file "describes" induction, recurrence, and conditional scalar
10-
// assignment (CSA) variables.
10+
// assignment variables.
1111
//
1212
//===----------------------------------------------------------------------===//
1313

@@ -1572,15 +1572,17 @@ bool InductionDescriptor::isInductionPHI(
15721572
return true;
15731573
}
15741574

1575-
/// Return CSADescriptor that describes a CSA that matches one of these
1576-
/// patterns:
1575+
/// Return ConditionalScalarAssignmentDescriptor that describes a
1576+
/// ConditionalScalarAssignment that matches one of these patterns:
15771577
/// phi loop_inv, (select cmp, value, phi)
15781578
/// phi loop_inv, (select cmp, phi, value)
15791579
/// phi (select cmp, value, phi), loop_inv
15801580
/// phi (select cmp, phi, value), loop_inv
1581-
/// If the CSA does not match any of these paterns, return a CSADescriptor
1582-
/// that describes an InvalidCSA.
1583-
bool CSADescriptor::isCSAPhi(PHINode *Phi, Loop *TheLoop, CSADescriptor &CSA) {
1581+
/// If the ConditionalScalarAssignment does not match any of these paterns,
1582+
/// return a ConditionalScalarAssignmentDescriptor that describes an
1583+
/// InvalidConditionalScalarAssignment.
1584+
bool ConditionalScalarAssignmentDescriptor::isConditionalScalarAssignmentPhi(
1585+
PHINode *Phi, Loop *TheLoop, ConditionalScalarAssignmentDescriptor &Desc) {
15841586

15851587
// Must be a scalar.
15861588
Type *Type = Phi->getType();
@@ -1623,6 +1625,6 @@ bool CSADescriptor::isCSAPhi(PHINode *Phi, Loop *TheLoop, CSADescriptor &CSA) {
16231625
!IsOnlyUsedOutsideLoop(Select, Phi))
16241626
return false;
16251627

1626-
CSA = CSADescriptor(Phi, Select, LoopInv);
1628+
Desc = ConditionalScalarAssignmentDescriptor(Phi, Select, LoopInv);
16271629
return true;
16281630
}

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,8 +1373,9 @@ bool TargetTransformInfo::preferEpilogueVectorization() const {
13731373
return TTIImpl->preferEpilogueVectorization();
13741374
}
13751375

1376-
bool TargetTransformInfo::enableCSAVectorization() const {
1377-
return TTIImpl->enableCSAVectorization();
1376+
bool TargetTransformInfo::enableConditionalScalarAssignmentVectorization()
1377+
const {
1378+
return TTIImpl->enableConditionalScalarAssignmentVectorization();
13781379
}
13791380

13801381
TargetTransformInfo::VPLegalization

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2361,7 +2361,7 @@ bool RISCVTTIImpl::isLegalMaskedExpandLoad(Type *DataTy, Align Alignment) {
23612361
return true;
23622362
}
23632363

2364-
bool RISCVTTIImpl::enableCSAVectorization() const {
2364+
bool RISCVTTIImpl::enableConditionalScalarAssignmentVectorization() const {
23652365
return ST->hasVInstructions() &&
23662366
ST->getProcFamily() == RISCVSubtarget::SiFive7;
23672367
}

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
308308

309309
/// \returns true if the loop vectorizer should vectorize conditional
310310
/// scalar assignments for the target.
311-
bool enableCSAVectorization() const;
311+
bool enableConditionalScalarAssignmentVectorization() const;
312312

313313
/// \returns How the target needs this vector-predicated operation to be
314314
/// transformed.

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ static cl::opt<bool> EnableHistogramVectorization(
8383
"enable-histogram-loop-vectorization", cl::init(false), cl::Hidden,
8484
cl::desc("Enables autovectorization of some loops containing histograms"));
8585

86-
static cl::opt<bool>
87-
EnableCSA("enable-csa-vectorization", cl::init(false), cl::Hidden,
88-
cl::desc("Control whether CSA loop vectorization is enabled"));
86+
static cl::opt<bool> EnableConditionalScalarAssignment(
87+
"enable-csa-vectorization", cl::init(false), cl::Hidden,
88+
cl::desc("Control whether loop vectorization is enabled"));
8989

9090
/// Maximum vectorization interleave count.
9191
static const unsigned MaxInterleaveFactor = 16;
@@ -753,13 +753,16 @@ bool LoopVectorizationLegality::setupOuterLoopInductions() {
753753
return llvm::all_of(Header->phis(), IsSupportedPhi);
754754
}
755755

756-
void LoopVectorizationLegality::addCSAPhi(
757-
PHINode *Phi, const CSADescriptor &CSADesc,
756+
void LoopVectorizationLegality::addConditionalScalarAssignmentPhi(
757+
PHINode *Phi, const ConditionalScalarAssignmentDescriptor &Desc,
758758
SmallPtrSetImpl<Value *> &AllowedExit) {
759-
assert(CSADesc.isValid() && "Expected Valid CSADescriptor");
760-
LLVM_DEBUG(dbgs() << "LV: found legal CSA opportunity" << *Phi << "\n");
759+
assert(Desc.isValid() &&
760+
"Expected Valid ConditionalScalarAssignmentDescriptor");
761+
LLVM_DEBUG(
762+
dbgs() << "LV: found legal conditional scalar assignment opportunity"
763+
<< *Phi << "\n");
761764
AllowedExit.insert(Phi);
762-
CSAs.insert({Phi, CSADesc});
765+
ConditionalScalarAssignments.insert({Phi, Desc});
763766
}
764767

765768
/// Checks if a function is scalarizable according to the TLI, in
@@ -887,12 +890,15 @@ bool LoopVectorizationLegality::canVectorizeInstrs() {
887890
continue;
888891
}
889892

890-
// Check if the PHI can be classified as a CSA PHI.
891-
if (EnableCSA || (TTI->enableCSAVectorization() &&
892-
EnableCSA.getNumOccurrences() == 0)) {
893-
CSADescriptor CSADesc;
894-
if (CSADescriptor::isCSAPhi(Phi, TheLoop, CSADesc)) {
895-
addCSAPhi(Phi, CSADesc, AllowedExit);
893+
// Check if the PHI can be classified as a conditional scalar assignment
894+
// PHI.
895+
if (EnableConditionalScalarAssignment ||
896+
(TTI->enableConditionalScalarAssignmentVectorization() &&
897+
EnableConditionalScalarAssignment.getNumOccurrences() == 0)) {
898+
ConditionalScalarAssignmentDescriptor Desc;
899+
if (ConditionalScalarAssignmentDescriptor::
900+
isConditionalScalarAssignmentPhi(Phi, TheLoop, Desc)) {
901+
addConditionalScalarAssignmentPhi(Phi, Desc, AllowedExit);
896902
continue;
897903
}
898904
}
@@ -1868,13 +1874,13 @@ bool LoopVectorizationLegality::canFoldTailByMasking() const {
18681874
ReductionLiveOuts.insert(Reduction.second.getLoopExitInstr());
18691875

18701876
SmallPtrSet<const Value *, 8> CSALiveOuts;
1871-
for (const auto &CSA : getCSAs())
1877+
for (const auto &CSA : getConditionalScalarAssignments())
18721878
CSALiveOuts.insert(CSA.second.getAssignment());
18731879

18741880
// TODO: handle non-reduction outside users when tail is folded by masking.
18751881
for (auto *AE : AllowedExit) {
18761882
// Check that all users of allowed exit values are inside the loop or
1877-
// are the live-out of a reduction or CSA.
1883+
// are the live-out of a reduction or conditional scalar assignment.
18781884
if (ReductionLiveOuts.count(AE) || CSALiveOuts.count(AE))
18791885
continue;
18801886
for (User *U : AE->users()) {

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,20 +257,24 @@ class VPBuilder {
257257
FPBinOp ? FPBinOp->getFastMathFlags() : FastMathFlags()));
258258
}
259259

260-
VPInstruction *createCSAMaskPhi(VPValue *InitMask, DebugLoc DL,
261-
const Twine &Name) {
262-
return createInstruction(VPInstruction::CSAMaskPhi, {InitMask}, DL, Name);
260+
VPInstruction *createConditionalScalarAssignmentMaskPhi(VPValue *InitMask,
261+
DebugLoc DL,
262+
const Twine &Name) {
263+
return createInstruction(VPInstruction::ConditionalScalarAssignmentMaskPhi,
264+
{InitMask}, DL, Name);
263265
}
264266

265267
VPInstruction *createAnyOf(VPValue *Cond, DebugLoc DL, const Twine &Name) {
266268
return createInstruction(VPInstruction::AnyOf, {Cond}, DL, Name);
267269
}
268270

269-
VPInstruction *createCSAMaskSel(VPValue *Cond, VPValue *MaskPhi,
270-
VPValue *AnyOf, DebugLoc DL,
271-
const Twine &Name) {
272-
return createInstruction(VPInstruction::CSAMaskSel, {Cond, MaskPhi, AnyOf},
273-
DL, Name);
271+
VPInstruction *createConditionalScalarAssignmentMaskSel(VPValue *Cond,
272+
VPValue *MaskPhi,
273+
VPValue *AnyOf,
274+
DebugLoc DL,
275+
const Twine &Name) {
276+
return createInstruction(VPInstruction::ConditionalScalarAssignmentMaskSel,
277+
{Cond, MaskPhi, AnyOf}, DL, Name);
274278
}
275279

276280
//===--------------------------------------------------------------------===//

0 commit comments

Comments
 (0)