Skip to content

Commit

Permalink
Revert "[FuncSpec] Add Phi nodes to the InstCostVisitor."
Browse files Browse the repository at this point in the history
Reverting due to the crash reported in D154852.

Also reverting the subsequent commit as collateral damage:

"[FuncSpec] Split the specialization bonus into CodeSize and Latency."
  • Loading branch information
labrinea committed Jul 26, 2023
1 parent a8aabba commit c52ab9e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 293 deletions.
59 changes: 7 additions & 52 deletions llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,65 +108,24 @@ struct Spec {
SpecSig Sig;

// Profitability of the specialization.
unsigned Score;
Cost Score;

// List of call sites, matching this specialization.
SmallVector<CallBase *> CallSites;

Spec(Function *F, const SpecSig &S, unsigned Score)
Spec(Function *F, const SpecSig &S, Cost Score)
: F(F), Sig(S), Score(Score) {}
Spec(Function *F, const SpecSig &&S, unsigned Score)
Spec(Function *F, const SpecSig &&S, Cost Score)
: F(F), Sig(S), Score(Score) {}
};

struct Bonus {
unsigned CodeSize = 0;
unsigned Latency = 0;

Bonus() = default;

Bonus(Cost CodeSize, Cost Latency) {
int64_t Sz = *CodeSize.getValue();
int64_t Ltc = *Latency.getValue();

assert(Sz >= 0 && Ltc >= 0 && "CodeSize and Latency cannot be negative");
// It is safe to down cast since we know the arguments
// cannot be negative and Cost is of type int64_t.
this->CodeSize = static_cast<unsigned>(Sz);
this->Latency = static_cast<unsigned>(Ltc);
}

Bonus &operator+=(const Bonus RHS) {
CodeSize += RHS.CodeSize;
Latency += RHS.Latency;
return *this;
}

Bonus operator+(const Bonus RHS) const {
return Bonus(CodeSize + RHS.CodeSize, Latency + RHS.Latency);
}

bool operator==(const Bonus RHS) const {
return CodeSize == RHS.CodeSize && Latency == RHS.Latency;
}
};

class InstCostVisitor : public InstVisitor<InstCostVisitor, Constant *> {
const DataLayout &DL;
BlockFrequencyInfo &BFI;
TargetTransformInfo &TTI;
SCCPSolver &Solver;

ConstMap KnownConstants;
// Basic blocks known to be unreachable after constant propagation.
DenseSet<BasicBlock *> DeadBlocks;
// PHI nodes we have visited before.
DenseSet<Instruction *> VisitedPHIs;
// PHI nodes we have visited once without successfully constant folding them.
// Once the InstCostVisitor has processed all the specialization arguments,
// it should be possible to determine whether those PHIs can be folded
// (some of their incoming values may have become constant or dead).
SmallVector<Instruction *> PendingPHIs;

ConstMap::iterator LastVisited;

Expand All @@ -175,10 +134,7 @@ class InstCostVisitor : public InstVisitor<InstCostVisitor, Constant *> {
TargetTransformInfo &TTI, SCCPSolver &Solver)
: DL(DL), BFI(BFI), TTI(TTI), Solver(Solver) {}

Bonus getUserBonus(Instruction *User, Value *Use = nullptr,
Constant *C = nullptr);

Bonus getBonusFromPendingPHIs();
Cost getUserBonus(Instruction *User, Value *Use, Constant *C);

private:
friend class InstVisitor<InstCostVisitor, Constant *>;
Expand All @@ -187,7 +143,6 @@ class InstCostVisitor : public InstVisitor<InstCostVisitor, Constant *> {
Cost estimateBranchInst(BranchInst &I);

Constant *visitInstruction(Instruction &I) { return nullptr; }
Constant *visitPHINode(PHINode &I);
Constant *visitFreezeInst(FreezeInst &I);
Constant *visitCallBase(CallBase &I);
Constant *visitLoadInst(LoadInst &I);
Expand Down Expand Up @@ -240,8 +195,8 @@ class FunctionSpecializer {
}

/// Compute a bonus for replacing argument \p A with constant \p C.
Bonus getSpecializationBonus(Argument *A, Constant *C,
InstCostVisitor &Visitor);
Cost getSpecializationBonus(Argument *A, Constant *C,
InstCostVisitor &Visitor);

private:
Constant *getPromotableAlloca(AllocaInst *Alloca, CallInst *Call);
Expand All @@ -268,7 +223,7 @@ class FunctionSpecializer {
/// @param AllSpecs A vector to add potential specializations to.
/// @param SM A map for a function's specialisation range
/// @return True, if any potential specializations were found
bool findSpecializations(Function *F, unsigned SpecCost,
bool findSpecializations(Function *F, Cost SpecCost,
SmallVectorImpl<Spec> &AllSpecs, SpecMap &SM);

bool isCandidateFunction(Function *F);
Expand Down

0 comments on commit c52ab9e

Please sign in to comment.