Skip to content

[CostModel] Remove optional from InstructionCost::getValue() #135596

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 1 commit into from
Apr 23, 2025
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
4 changes: 2 additions & 2 deletions llvm/include/llvm/CodeGen/BasicTTIImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {

// Scale the cost of the load by the fraction of legal instructions that
// will be used.
Cost = divideCeil(UsedInsts.count() * *Cost.getValue(), NumLegalInsts);
Cost = divideCeil(UsedInsts.count() * Cost.getValue(), NumLegalInsts);
}

// Then plus the cost of interleave operation.
Expand Down Expand Up @@ -2878,7 +2878,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
SubTp && SubTp->getElementType() == FTp->getElementType())
return divideCeil(FTp->getNumElements(), SubTp->getNumElements());
}
return *LT.first.getValue();
return LT.first.getValue();
}

InstructionCost getAddressComputationCost(Type *Ty, ScalarEvolution *,
Expand Down
8 changes: 3 additions & 5 deletions llvm/include/llvm/Support/InstructionCost.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include "llvm/Support/MathExtras.h"
#include <limits>
#include <optional>

namespace llvm {

Expand Down Expand Up @@ -84,10 +83,9 @@ class InstructionCost {
/// This function is intended to be used as sparingly as possible, since the
/// class provides the full range of operator support required for arithmetic
/// and comparisons.
std::optional<CostType> getValue() const {
if (isValid())
return Value;
return std::nullopt;
CostType getValue() const {
assert(isValid());
return Value;
}

/// For all of the arithmetic operators provided here any invalid state is
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Transforms/Utils/UnrollLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class UnrollCostEstimator {
/// Whether it is legal to unroll this loop.
bool canUnroll() const;

uint64_t getRolledLoopSize() const { return *LoopSize.getValue(); }
uint64_t getRolledLoopSize() const { return LoopSize.getValue(); }

/// Returns loop size estimation for unrolled loop, given the unrolling
/// configuration specified by UP.
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/CostModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ PreservedAnalyses CostModelPrinterPass::run(Function &F,
} else {
InstructionCost Cost =
getCost(Inst, OutputCostKindToTargetCostKind(CostKind), TTI, TLI);
if (auto CostVal = Cost.getValue())
OS << "Found an estimated cost of " << *CostVal;
if (Cost.isValid())
OS << "Found an estimated cost of " << Cost.getValue();
else
OS << "Invalid cost";
OS << " for instruction: " << Inst << "\n";
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/CodeGen/SelectOptimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class SelectOptimizeImpl {
getI()->getOpcode(), I->getType(), TargetTransformInfo::TCK_Latency,
{TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None},
{TTI::OK_UniformConstantValue, TTI::OP_PowerOf2});
auto TotalCost = Scaled64::get(*Cost.getValue());
auto TotalCost = Scaled64::get(Cost.getValue());
if (auto *OpI = dyn_cast<Instruction>(I->getOperand(1 - CondIdx))) {
auto It = InstCostMap.find(OpI);
if (It != InstCostMap.end())
Expand Down Expand Up @@ -1380,8 +1380,8 @@ std::optional<uint64_t>
SelectOptimizeImpl::computeInstCost(const Instruction *I) {
InstructionCost ICost =
TTI->getInstructionCost(I, TargetTransformInfo::TCK_Latency);
if (auto OC = ICost.getValue())
return std::optional<uint64_t>(*OC);
if (ICost.isValid())
return std::optional<uint64_t>(ICost.getValue());
return std::nullopt;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28530,7 +28530,7 @@ bool AArch64TargetLowering::shouldLocalize(
Imm, CI->getType(), TargetTransformInfo::TCK_CodeSize);
assert(Cost.isValid() && "Expected a valid imm cost");

unsigned RematCost = *Cost.getValue();
unsigned RematCost = Cost.getValue();
RematCost += AdditionalCost;
Register Reg = MI.getOperand(0).getReg();
unsigned MaxUses = maxUses(RematCost);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4618,7 +4618,7 @@ static bool isLoopSizeWithinBudget(Loop *L, const AArch64TTIImpl &TTI,
}

if (FinalSize)
*FinalSize = *LoopCost.getValue();
*FinalSize = LoopCost.getValue();
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ static CostType calculateFunctionCosts(GetTTIFn GetTTI, Module &M,
TTI.getInstructionCost(&I, TargetTransformInfo::TCK_CodeSize);
assert(Cost != InstructionCost::getMax());
// Assume expensive if we can't tell the cost of an instruction.
CostType CostVal =
Cost.getValue().value_or(TargetTransformInfo::TCC_Expensive);
CostType CostVal = Cost.isValid() ? Cost.getValue()
: TargetTransformInfo::TCC_Expensive;
assert((FnCost + CostVal) >= FnCost && "Overflow!");
FnCost += CostVal;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1277,9 +1277,9 @@ static unsigned adjustInliningThresholdUsingCallee(const CallBase *CB,
// The penalty cost is computed relative to the cost of instructions and does
// not model any storage costs.
adjustThreshold += std::max(0, SGPRsInUse - NrOfSGPRUntilSpill) *
*ArgStackCost.getValue() * InlineConstants::getInstrCost();
ArgStackCost.getValue() * InlineConstants::getInstrCost();
adjustThreshold += std::max(0, VGPRsInUse - NrOfVGPRUntilSpill) *
*ArgStackCost.getValue() * InlineConstants::getInstrCost();
ArgStackCost.getValue() * InlineConstants::getInstrCost();
return adjustThreshold;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ InstructionCost PPCTTIImpl::getVPMemoryOpCost(unsigned Opcode, Type *Src,
float AlignmentProb = ((float)Alignment.value()) / DesiredAlignment.value();
float MisalignmentProb = 1.0 - AlignmentProb;
return (MisalignmentProb * P9PipelineFlushEstimate) +
(AlignmentProb * *Cost.getValue());
(AlignmentProb * Cost.getValue());
}

// Usually we should not get to this point, but the following is an attempt to
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2909,7 +2909,7 @@ InstructionCost RISCVTargetLowering::getVRGatherVVCost(MVT VT) const {
bool Log2CostModel =
Subtarget.getVRGatherCostModel() == llvm::RISCVSubtarget::NLog2N;
if (Log2CostModel && LMULCost.isValid()) {
unsigned Log = Log2_64(*LMULCost.getValue());
unsigned Log = Log2_64(LMULCost.getValue());
if (Log > 0)
return LMULCost * Log;
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ costShuffleViaVRegSplitting(const RISCVTTIImpl &TTI, MVT LegalVT,
auto *SingleOpTy = FixedVectorType::get(Tp->getElementType(),
LegalVT.getVectorNumElements());

unsigned E = *NumOfDests.getValue();
unsigned E = NumOfDests.getValue();
unsigned NormalizedVF =
LegalVT.getVectorNumElements() * std::max(NumOfSrcs, E);
unsigned NumOfSrcRegs = NormalizedVF / LegalVT.getVectorNumElements();
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ void SystemZTTIImpl::getUnrollingPreferences(
// The z13 processor will run out of store tags if too many stores
// are fed into it too quickly. Therefore make sure there are not
// too many stores in the resulting unrolled loop.
unsigned const NumStoresVal = *NumStores.getValue();
unsigned const NumStoresVal = NumStores.getValue();
unsigned const Max = (NumStoresVal ? (12 / NumStoresVal) : UINT_MAX);

if (HasCall) {
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Target/X86/X86TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,7 @@ InstructionCost X86TTIImpl::getShuffleCost(
getTypeLegalizationCost(
FixedVectorType::get(BaseTp->getElementType(), Mask.size()))
.first;
unsigned E = *NumOfDests.getValue();
unsigned E = NumOfDests.getValue();
unsigned NormalizedVF =
LegalVT.getVectorNumElements() * std::max(NumOfSrcs, E);
unsigned NumOfSrcRegs = NormalizedVF / LegalVT.getVectorNumElements();
Expand Down Expand Up @@ -4931,7 +4931,7 @@ InstructionCost X86TTIImpl::getScalarizationOverhead(
(LegalVectorBitWidth % LaneBitWidth) == 0) &&
"Illegal vector");

const int NumLegalVectors = *LT.first.getValue();
const int NumLegalVectors = LT.first.getValue();
assert(NumLegalVectors >= 0 && "Negative cost!");

// For insertions, a ISD::BUILD_VECTOR style vector initialization can be much
Expand Down Expand Up @@ -6164,7 +6164,7 @@ InstructionCost X86TTIImpl::getGSVectorCost(unsigned Opcode,
std::pair<InstructionCost, MVT> IdxsLT = getTypeLegalizationCost(IndexVTy);
std::pair<InstructionCost, MVT> SrcLT = getTypeLegalizationCost(SrcVTy);
InstructionCost::CostType SplitFactor =
*std::max(IdxsLT.first, SrcLT.first).getValue();
std::max(IdxsLT.first, SrcLT.first).getValue();
if (SplitFactor > 1) {
// Handle splitting of vector of pointers
auto *SplitSrcTy =
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ FunctionSpecializer::~FunctionSpecializer() {
/// non-negative, which is true for both TCK_CodeSize and TCK_Latency, and
/// always Valid.
static unsigned getCostValue(const Cost &C) {
int64_t Value = *C.getValue();
int64_t Value = C.getValue();

assert(Value >= 0 && "CodeSize and Latency cannot be negative");
// It is safe to down cast since we know the arguments cannot be negative and
Expand Down Expand Up @@ -713,7 +713,7 @@ bool FunctionSpecializer::run() {
if (!SpecializeLiteralConstant && !Inserted && !Metrics.isRecursive)
continue;

int64_t Sz = *Metrics.NumInsts.getValue();
int64_t Sz = Metrics.NumInsts.getValue();
assert(Sz > 0 && "CodeSize should be positive");
// It is safe to down cast from int64_t, NumInsts is always positive.
unsigned FuncSize = static_cast<unsigned>(Sz);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/IPO/PartialInlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ bool PartialInlinerImpl::tryPartialInline(FunctionCloner &Cloner) {
RelativeToEntryFreq = BranchProbability(0, 1);

BlockFrequency WeightedRcost =
BlockFrequency(*NonWeightedRcost.getValue()) * RelativeToEntryFreq;
BlockFrequency(NonWeightedRcost.getValue()) * RelativeToEntryFreq;

// The call sequence(s) to the outlined function(s) are larger than the sum of
// the original outlined region size(s), it does not increase the chances of
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ void ConstantHoistingPass::collectConstantCandidates(
ConstIntCandVec.push_back(ConstantCandidate(ConstInt));
Itr->second = ConstIntCandVec.size() - 1;
}
ConstIntCandVec[Itr->second].addUser(Inst, Idx, *Cost.getValue());
ConstIntCandVec[Itr->second].addUser(Inst, Idx, Cost.getValue());
LLVM_DEBUG(if (isa<ConstantInt>(Inst->getOperand(Idx))) dbgs()
<< "Collect constant " << *ConstInt << " from " << *Inst
<< " with cost " << Cost << '\n';
Expand Down Expand Up @@ -446,7 +446,7 @@ void ConstantHoistingPass::collectConstantCandidates(
ConstExpr));
Itr->second = ExprCandVec.size() - 1;
}
ExprCandVec[Itr->second].addUser(Inst, Idx, *Cost.getValue());
ExprCandVec[Itr->second].addUser(Inst, Idx, Cost.getValue());
}

/// Check the operand for instruction Inst at index Idx.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/LoopDataPrefetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ bool LoopDataPrefetch::runOnLoop(Loop *L) {
if (!Metrics.NumInsts.isValid())
return MadeChange;

unsigned LoopSize = *Metrics.NumInsts.getValue();
unsigned LoopSize = Metrics.NumInsts.getValue();
if (!LoopSize)
LoopSize = 1;

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,7 @@ void Cost::RateFormula(const Formula &F,
C.NumBaseAdds += (F.UnfoldedOffset.isNonZero());

// Accumulate non-free scaling amounts.
C.ScaleCost += *getScalingFactorCost(*TTI, LU, F, *L).getValue();
C.ScaleCost += getScalingFactorCost(*TTI, LU, F, *L).getValue();

// Tally up the non-zero immediates.
for (const LSRFixup &Fixup : LU.Fixups) {
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,8 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
LLVM_DEBUG(dbgs() << "Analysis finished:\n"
<< "UnrolledCost: " << UnrolledCost << ", "
<< "RolledDynamicCost: " << RolledDynamicCost << "\n");
return {{unsigned(*UnrolledCost.getValue()),
unsigned(*RolledDynamicCost.getValue())}};
return {{unsigned(UnrolledCost.getValue()),
unsigned(RolledDynamicCost.getValue())}};
}

UnrollCostEstimator::UnrollCostEstimator(
Expand Down Expand Up @@ -729,7 +729,7 @@ bool UnrollCostEstimator::canUnroll() const {
uint64_t UnrollCostEstimator::getUnrolledLoopSize(
const TargetTransformInfo::UnrollingPreferences &UP,
unsigned CountOverwrite) const {
unsigned LS = *LoopSize.getValue();
unsigned LS = LoopSize.getValue();
assert(LS >= UP.BEInsns && "LoopSize should not be less than BEInsns!");
if (CountOverwrite)
return static_cast<uint64_t>(LS - UP.BEInsns) * CountOverwrite + UP.BEInsns;
Expand Down
12 changes: 6 additions & 6 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,7 @@ class GeneratedRTChecks {
InstructionCost NewMemCheckCost = MemCheckCost / BestTripCount;

// Let's ensure the cost is always at least 1.
NewMemCheckCost = std::max(*NewMemCheckCost.getValue(),
NewMemCheckCost = std::max(NewMemCheckCost.getValue(),
(InstructionCost::CostType)1);

if (BestTripCount > 1)
Expand Down Expand Up @@ -5314,7 +5314,7 @@ LoopVectorizationCostModel::selectInterleaveCount(VPlan &Plan, ElementCount VF,
// to estimate the cost of the loop and interleave until the cost of the
// loop overhead is about 5% of the cost of the loop.
unsigned SmallIC = std::min(IC, (unsigned)llvm::bit_floor<uint64_t>(
SmallLoopCost / *LoopCost.getValue()));
SmallLoopCost / LoopCost.getValue()));

// Interleave until store/load ports (estimated by max interleave count) are
// saturated.
Expand Down Expand Up @@ -7659,7 +7659,7 @@ InstructionCost LoopVectorizationPlanner::cost(VPlan &Plan,
LLVM_DEBUG(dbgs() << "Cost for VF " << VF << ": " << Cost
<< " (Estimated cost per lane: ");
if (Cost.isValid()) {
double CostPerLane = double(*Cost.getValue()) / EstimatedWidth;
double CostPerLane = double(Cost.getValue()) / EstimatedWidth;
LLVM_DEBUG(dbgs() << format("%.1f", CostPerLane));
} else /* No point dividing an invalid cost - it will still be invalid */
LLVM_DEBUG(dbgs() << "Invalid");
Expand Down Expand Up @@ -10478,7 +10478,7 @@ static bool isOutsideLoopWorkProfitable(GeneratedRTChecks &Checks,

// The scalar cost should only be 0 when vectorizing with a user specified
// VF/IC. In those cases, runtime checks should always be generated.
uint64_t ScalarC = *VF.ScalarCost.getValue();
uint64_t ScalarC = VF.ScalarCost.getValue();
if (ScalarC == 0)
return true;

Expand Down Expand Up @@ -10513,8 +10513,8 @@ static bool isOutsideLoopWorkProfitable(GeneratedRTChecks &Checks,
// the computations are performed on doubles, not integers and the result
// is rounded up, hence we get an upper estimate of the TC.
unsigned IntVF = getEstimatedRuntimeVF(VF.Width, VScale);
uint64_t RtC = *TotalCost.getValue();
uint64_t Div = ScalarC * IntVF - *VF.Cost.getValue();
uint64_t RtC = TotalCost.getValue();
uint64_t Div = ScalarC * IntVF - VF.Cost.getValue();
uint64_t MinTC1 = Div == 0 ? 0 : divideCeil(RtC * IntVF, Div);

// Second, compute a minimum iteration count so that the cost of the
Expand Down
5 changes: 2 additions & 3 deletions llvm/unittests/Support/InstructionCostTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TEST_F(CostTest, DefaultCtor) {
InstructionCost DefaultCost;

ASSERT_TRUE(DefaultCost.isValid());
EXPECT_EQ(*(DefaultCost.getValue()), 0);
EXPECT_EQ(DefaultCost.getValue(), 0);
}

TEST_F(CostTest, Operators) {
Expand Down Expand Up @@ -70,8 +70,7 @@ TEST_F(CostTest, Operators) {
EXPECT_FALSE(TmpCost.isValid());

// Test value extraction
EXPECT_EQ(*(VThree.getValue()), 3);
EXPECT_EQ(IThreeA.getValue(), std::nullopt);
EXPECT_EQ(VThree.getValue(), 3);

EXPECT_EQ(std::min(VThree, VNegTwo), -2);
EXPECT_EQ(std::max(VThree, VSix), 6);
Expand Down
Loading