Skip to content

[ExpandVectorPredication] Be more precise reporting changes #102313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions llvm/include/llvm/CodeGen/ExpandVectorPredication.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ namespace llvm {
class TargetTransformInfo;
class VPIntrinsic;

/// Expand a vector predication intrinsic. Returns true if the intrinsic was
/// removed/replaced.
bool expandVectorPredicationIntrinsic(VPIntrinsic &VPI,
const TargetTransformInfo &TTI);
/// Represents the details the expansion of a VP intrinsic.
enum class VPExpansionDetails {
/// No change happened during expansion.
IntrinsicUnchanged,
/// At least one operand was updated.
IntrinsicUpdated,
/// The whole intrinsic was replaced.
IntrinsicReplaced,
};

/// Expand a vector predication intrinsic. Returns the kind of expansion
/// that was applied to the intrinsic.
VPExpansionDetails
expandVectorPredicationIntrinsic(VPIntrinsic &VPI,
const TargetTransformInfo &TTI);

} // end namespace llvm

Expand Down
51 changes: 32 additions & 19 deletions llvm/lib/CodeGen/ExpandVectorPredication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,15 @@ struct CachingVPExpander {
Value *convertEVLToMask(IRBuilder<> &Builder, Value *EVLParam,
ElementCount ElemCount);

Value *foldEVLIntoMask(VPIntrinsic &VPI);
/// If needed, folds the EVL in the mask operand and discards the EVL
/// parameter. Returns a pair of the value of the intrinsic after the change
/// (if any) and whether the mask was actually folded.
std::pair<Value *, bool> foldEVLIntoMask(VPIntrinsic &VPI);

/// "Remove" the %evl parameter of \p PI by setting it to the static vector
/// length of the operation.
void discardEVLParameter(VPIntrinsic &PI);
/// length of the operation. Returns true if the %evl (if any) was effectively
/// changed.
bool discardEVLParameter(VPIntrinsic &PI);

/// Lower this VP binary operator to a unpredicated binary operator.
Value *expandPredicationInBinaryOperator(IRBuilder<> &Builder,
Expand Down Expand Up @@ -206,7 +210,9 @@ struct CachingVPExpander {
CachingVPExpander(const TargetTransformInfo &TTI)
: TTI(TTI), UsingTTIOverrides(anyExpandVPOverridesSet()) {}

bool expandVectorPredication(VPIntrinsic &VPI);
/// Expand llvm.vp.* intrinsics as requested by \p TTI.
/// Returns the details of the expansion.
VPExpansionDetails expandVectorPredication(VPIntrinsic &VPI);
};

//// CachingVPExpander {
Expand Down Expand Up @@ -645,15 +651,15 @@ Value *CachingVPExpander::expandPredicationInComparison(IRBuilder<> &Builder,
return NewCmp;
}

void CachingVPExpander::discardEVLParameter(VPIntrinsic &VPI) {
bool CachingVPExpander::discardEVLParameter(VPIntrinsic &VPI) {
LLVM_DEBUG(dbgs() << "Discard EVL parameter in " << VPI << "\n");

if (VPI.canIgnoreVectorLengthParam())
return;
return false;

Value *EVLParam = VPI.getVectorLengthParam();
if (!EVLParam)
return;
return false;

ElementCount StaticElemCount = VPI.getStaticVectorLength();
Value *MaxEVL = nullptr;
Expand All @@ -672,16 +678,17 @@ void CachingVPExpander::discardEVLParameter(VPIntrinsic &VPI) {
MaxEVL = ConstantInt::get(Int32Ty, StaticElemCount.getFixedValue(), false);
}
VPI.setVectorLengthParam(MaxEVL);
return true;
}

Value *CachingVPExpander::foldEVLIntoMask(VPIntrinsic &VPI) {
std::pair<Value *, bool> CachingVPExpander::foldEVLIntoMask(VPIntrinsic &VPI) {
LLVM_DEBUG(dbgs() << "Folding vlen for " << VPI << '\n');

IRBuilder<> Builder(&VPI);

// Ineffective %evl parameter and so nothing to do here.
if (VPI.canIgnoreVectorLengthParam())
return &VPI;
return {&VPI, false};

// Only VP intrinsics can have an %evl parameter.
Value *OldMaskParam = VPI.getMaskParam();
Expand All @@ -704,7 +711,7 @@ Value *CachingVPExpander::foldEVLIntoMask(VPIntrinsic &VPI) {
"transformation did not render the evl param ineffective!");

// Reassess the modified instruction.
return &VPI;
return {&VPI, true};
}

Value *CachingVPExpander::expandPredication(VPIntrinsic &VPI) {
Expand Down Expand Up @@ -807,21 +814,27 @@ CachingVPExpander::getVPLegalizationStrategy(const VPIntrinsic &VPI) const {
return VPStrat;
}

/// Expand llvm.vp.* intrinsics as requested by \p TTI.
bool CachingVPExpander::expandVectorPredication(VPIntrinsic &VPI) {
VPExpansionDetails
CachingVPExpander::expandVectorPredication(VPIntrinsic &VPI) {
auto Strategy = getVPLegalizationStrategy(VPI);
sanitizeStrategy(VPI, Strategy);

VPExpansionDetails Changed = VPExpansionDetails::IntrinsicUnchanged;

// Transform the EVL parameter.
switch (Strategy.EVLParamStrategy) {
case VPLegalization::Legal:
break;
case VPLegalization::Discard:
discardEVLParameter(VPI);
if (discardEVLParameter(VPI))
Changed = VPExpansionDetails::IntrinsicUpdated;
break;
case VPLegalization::Convert:
if (foldEVLIntoMask(VPI))
if (auto [NewVPI, Folded] = foldEVLIntoMask(VPI); Folded) {
(void)NewVPI;
Changed = VPExpansionDetails::IntrinsicUpdated;
++NumFoldedVL;
}
break;
}

Expand All @@ -834,17 +847,17 @@ bool CachingVPExpander::expandVectorPredication(VPIntrinsic &VPI) {
case VPLegalization::Convert:
if (Value *V = expandPredication(VPI); V != &VPI) {
++NumLoweredVPOps;
// Return true if and only if the intrinsic was actually removed.
return true;
Changed = VPExpansionDetails::IntrinsicReplaced;
}
break;
}

return false;
return Changed;
}
} // namespace

bool llvm::expandVectorPredicationIntrinsic(VPIntrinsic &VPI,
const TargetTransformInfo &TTI) {
VPExpansionDetails
llvm::expandVectorPredicationIntrinsic(VPIntrinsic &VPI,
const TargetTransformInfo &TTI) {
return CachingVPExpander(TTI).expandVectorPredication(VPI);
}
10 changes: 7 additions & 3 deletions llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,14 @@ bool PreISelIntrinsicLowering::lowerIntrinsics(Module &M) const {
Function *Parent = CI->getParent()->getParent();
const TargetTransformInfo &TTI = LookupTTI(*Parent);
auto *VPI = cast<VPIntrinsic>(CI);
return expandVectorPredicationIntrinsic(*VPI, TTI);
VPExpansionDetails ED = expandVectorPredicationIntrinsic(*VPI, TTI);
// Expansion of VP intrinsics may change the IR but not actually
// replace the intrinsic, so update Changed for the pass
// and compute Removed for forEachCall.
Changed |= ED != VPExpansionDetails::IntrinsicUnchanged;
bool Removed = ED == VPExpansionDetails::IntrinsicReplaced;
return Removed;
});
// Not all intrinsics are removed, but the code is changed in any case.
Changed = true;
break;
case Intrinsic::objc_autorelease:
Changed |= lowerObjCCall(F, "objc_autorelease");
Expand Down
Loading