Skip to content

Commit

Permalink
[NFC] Use initial-stack-allocations for more data structures (llvm#11…
Browse files Browse the repository at this point in the history
…0544)

This replaces some of the most frequent offenders of using a DenseMap that
cause a malloc, where the typical element-count is small enough to fit in
an initial stack allocation.

Most of these are fairly obvious, one to highlight is the collectOffset
method of GEP instructions: if there's a GEP, of course it's going to have
at least one offset, but every time we've called collectOffset we end up
calling malloc as well for the DenseMap in the MapVector.
  • Loading branch information
jmorse authored and VitaNuo committed Oct 2, 2024
1 parent c8303ec commit d9497ad
Show file tree
Hide file tree
Showing 17 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/IR/Instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ class GetElementPtrInst : public Instruction {
/// the base GEP pointer.
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
bool collectOffset(const DataLayout &DL, unsigned BitWidth,
MapVector<Value *, APInt> &VariableOffsets,
SmallMapVector<Value *, APInt, 4> &VariableOffsets,
APInt &ConstantOffset) const;
// Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Instruction *I) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/IR/Operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ class GEPOperator
/// Collect the offset of this GEP as a map of Values to their associated
/// APInt multipliers, as well as a total Constant Offset.
bool collectOffset(const DataLayout &DL, unsigned BitWidth,
MapVector<Value *, APInt> &VariableOffsets,
SmallMapVector<Value *, APInt, 4> &VariableOffsets,
APInt &ConstantOffset) const;
};

Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2831,7 +2831,8 @@ static void emitRangeList(

// Gather all the ranges that apply to the same section so they can share
// a base address entry.
MapVector<const MCSection *, std::vector<decltype(&*R.begin())>> SectionRanges;
SmallMapVector<const MCSection *, std::vector<decltype(&*R.begin())>, 16>
SectionRanges;

for (const auto &Range : R)
SectionRanges[&Range.Begin->getSection()].push_back(&Range);
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ class VLocTracker {
/// transfer function for this block, as part of the dataflow analysis. The
/// movement of values between locations inside of a block is handled at a
/// much later stage, in the TransferTracker class.
MapVector<DebugVariableID, DbgValue> Vars;
SmallMapVector<DebugVariableID, DbgValue, 8> Vars;
SmallDenseMap<DebugVariableID, const DILocation *, 8> Scopes;
MachineBasicBlock *MBB = nullptr;
const OverlapMap &OverlappingFragments;
Expand Down Expand Up @@ -1128,7 +1128,7 @@ class InstrRefBasedLDV : public LDVImpl {

/// Live in/out structure for the variable values: a per-block map of
/// variables to their values.
using LiveIdxT = DenseMap<const MachineBasicBlock *, DbgValue *>;
using LiveIdxT = SmallDenseMap<const MachineBasicBlock *, DbgValue *, 16>;

using VarAndLoc = std::pair<DebugVariableID, DbgValue>;

Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/CodeGen/ScheduleDAGInstrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,8 @@ void ScheduleDAGInstrs::initSUnits() {
}
}

class ScheduleDAGInstrs::Value2SUsMap : public MapVector<ValueType, SUList> {
class ScheduleDAGInstrs::Value2SUsMap
: public SmallMapVector<ValueType, SUList, 4> {
/// Current total number of SUs in map.
unsigned NumNodes = 0;

Expand Down Expand Up @@ -656,7 +657,7 @@ class ScheduleDAGInstrs::Value2SUsMap : public MapVector<ValueType, SUList> {

/// Clears map from all contents.
void clear() {
MapVector<ValueType, SUList>::clear();
SmallMapVector<ValueType, SUList, 4>::clear();
NumNodes = 0;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class ScheduleDAGRRList : public ScheduleDAGSDNodes {

// Hack to keep track of the inverse of FindCallSeqStart without more crazy
// DAG crawling.
DenseMap<SUnit*, SUnit*> CallSeqEndForStart;
SmallDenseMap<SUnit *, SUnit *, 16> CallSeqEndForStart;

public:
ScheduleDAGRRList(MachineFunction &mf, bool needlatency,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@ bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,

bool GetElementPtrInst::collectOffset(
const DataLayout &DL, unsigned BitWidth,
MapVector<Value *, APInt> &VariableOffsets,
SmallMapVector<Value *, APInt, 4> &VariableOffsets,
APInt &ConstantOffset) const {
// Delegate to the generic GEPOperator implementation.
return cast<GEPOperator>(this)->collectOffset(DL, BitWidth, VariableOffsets,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/Operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ bool GEPOperator::accumulateConstantOffset(

bool GEPOperator::collectOffset(
const DataLayout &DL, unsigned BitWidth,
MapVector<Value *, APInt> &VariableOffsets,
SmallMapVector<Value *, APInt, 4> &VariableOffsets,
APInt &ConstantOffset) const {
assert(BitWidth == DL.getIndexSizeInBits(getPointerAddressSpace()) &&
"The offset bit width does not match DL specification.");
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ static Value *GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca,
// TODO: Extracting a "multiple of X" from a GEP might be a useful generic
// helper.
unsigned BW = DL.getIndexTypeSizeInBits(GEP->getType());
MapVector<Value *, APInt> VarOffsets;
SmallMapVector<Value *, APInt, 4> VarOffsets;
APInt ConstOffset(BW, 0);
if (GEP->getPointerOperand()->stripPointerCasts() != Alloca ||
!GEP->collectOffset(DL, BW, VarOffsets, ConstOffset))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ getStrideAndModOffsetOfGEP(Value *PtrOp, const DataLayout &DL) {
// Return a minimum gep stride, greatest common divisor of consective gep
// index scales(c.f. Bézout's identity).
while (auto *GEP = dyn_cast<GEPOperator>(PtrOp)) {
MapVector<Value *, APInt> VarOffsets;
SmallMapVector<Value *, APInt, 4> VarOffsets;
if (!GEP->collectOffset(DL, BW, VarOffsets, ModOffset))
break;

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/IPO/AttributorAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,7 @@ bool AAPointerInfoFloating::collectConstantsForGEP(Attributor &A,
const OffsetInfo &PtrOI,
const GEPOperator *GEP) {
unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType());
MapVector<Value *, APInt> VariableOffsets;
SmallMapVector<Value *, APInt, 4> VariableOffsets;
APInt ConstantOffset(BitWidth, 0);

assert(!UsrOI.isUnknown() && !PtrOI.isUnknown() &&
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ struct Decomposition {
struct OffsetResult {
Value *BasePtr;
APInt ConstantOffset;
MapVector<Value *, APInt> VariableOffsets;
SmallMapVector<Value *, APInt, 4> VariableOffsets;
bool AllInbounds;

OffsetResult() : BasePtr(nullptr), ConstantOffset(0, uint64_t(0)) {}
Expand All @@ -410,7 +410,7 @@ static OffsetResult collectOffsets(GEPOperator &GEP, const DataLayout &DL) {
// If we have a nested GEP, check if we can combine the constant offset of the
// inner GEP with the outer GEP.
if (auto *InnerGEP = dyn_cast<GetElementPtrInst>(Result.BasePtr)) {
MapVector<Value *, APInt> VariableOffsets2;
SmallMapVector<Value *, APInt, 4> VariableOffsets2;
APInt ConstantOffset2(BitWidth, 0);
bool CanCollectInner = InnerGEP->collectOffset(
DL, BitWidth, VariableOffsets2, ConstantOffset2);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/GVN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ GVNPass::Expression GVNPass::ValueTable::createGEPExpr(GetElementPtrInst *GEP) {
Type *PtrTy = GEP->getType()->getScalarType();
const DataLayout &DL = GEP->getDataLayout();
unsigned BitWidth = DL.getIndexTypeSizeInBits(PtrTy);
MapVector<Value *, APInt> VariableOffsets;
SmallMapVector<Value *, APInt, 4> VariableOffsets;
APInt ConstantOffset(BitWidth, 0);
if (GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset)) {
// Convert into offset representation, to recognize equivalent address
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static std::optional<JumpTableTy> parseJumpTable(GetElementPtrInst *GEP,
const DataLayout &DL = F.getDataLayout();
const unsigned BitWidth =
DL.getIndexSizeInBits(GEP->getPointerAddressSpace());
MapVector<Value *, APInt> VariableOffsets;
SmallMapVector<Value *, APInt, 4> VariableOffsets;
APInt ConstantOffset(BitWidth, 0);
if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
return std::nullopt;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ,
}

using PredBlockVector = SmallVector<BasicBlock *, 16>;
using IncomingValueMap = DenseMap<BasicBlock *, Value *>;
using IncomingValueMap = SmallDenseMap<BasicBlock *, Value *, 16>;

/// Determines the value to use as the phi node input for a block.
///
Expand Down Expand Up @@ -2467,7 +2467,7 @@ Value *getSalvageOpsForGEP(GetElementPtrInst *GEP, const DataLayout &DL,
SmallVectorImpl<Value *> &AdditionalValues) {
unsigned BitWidth = DL.getIndexSizeInBits(GEP->getPointerAddressSpace());
// Rewrite a GEP into a DIExpression.
MapVector<Value *, APInt> VariableOffsets;
SmallMapVector<Value *, APInt, 4> VariableOffsets;
APInt ConstantOffset(BitWidth, 0);
if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
return nullptr;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5122,7 +5122,7 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
// Each 'key' in the map opens a new interval. The values
// of the map are the index of the 'last seen' usage of the
// instruction that is the key.
using IntervalMap = DenseMap<Instruction *, unsigned>;
using IntervalMap = SmallDenseMap<Instruction *, unsigned, 16>;

// Maps instruction to its index.
SmallVector<Instruction *, 64> IdxToInstr;
Expand Down Expand Up @@ -5165,7 +5165,7 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {

// Saves the list of intervals that end with the index in 'key'.
using InstrList = SmallVector<Instruction *, 2>;
DenseMap<unsigned, InstrList> TransposeEnds;
SmallDenseMap<unsigned, InstrList, 16> TransposeEnds;

// Transpose the EndPoints to a list of values that end at each index.
for (auto &Interval : EndPoint)
Expand Down
10 changes: 6 additions & 4 deletions llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5470,7 +5470,7 @@ BoUpSLP::getReorderingData(const TreeEntry &TE, bool TopToBottom) {
}
return I1 < I2;
};
DenseMap<unsigned, unsigned> PhiToId;
SmallDenseMap<unsigned, unsigned, 16> PhiToId;
SmallVector<unsigned> Phis(TE.Scalars.size());
std::iota(Phis.begin(), Phis.end(), 0);
OrdersType ResOrder(TE.Scalars.size());
Expand Down Expand Up @@ -10319,7 +10319,7 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
E->isAltShuffle() ? (unsigned)Instruction::ShuffleVector : E->getOpcode();
if (E->CombinedOp != TreeEntry::NotCombinedOp)
ShuffleOrOp = E->CombinedOp;
SetVector<Value *> UniqueValues(VL.begin(), VL.end());
SmallSetVector<Value *, 16> UniqueValues(VL.begin(), VL.end());
const unsigned Sz = UniqueValues.size();
SmallBitVector UsedScalars(Sz, false);
for (unsigned I = 0; I < Sz; ++I) {
Expand Down Expand Up @@ -18013,7 +18013,7 @@ class HorizontalReduction {
/// List of possibly reduced values.
SmallVector<SmallVector<Value *>> ReducedVals;
/// Maps reduced value to the corresponding reduction operation.
DenseMap<Value *, SmallVector<Instruction *>> ReducedValsToOps;
SmallDenseMap<Value *, SmallVector<Instruction *>, 16> ReducedValsToOps;
WeakTrackingVH ReductionRoot;
/// The type of reduction operation.
RecurKind RdxKind;
Expand Down Expand Up @@ -18382,7 +18382,9 @@ class HorizontalReduction {
// instruction op id and/or alternate op id, plus do extra analysis for
// loads (grouping them by the distabce between pointers) and cmp
// instructions (grouping them by the predicate).
MapVector<size_t, MapVector<size_t, MapVector<Value *, unsigned>>>
SmallMapVector<
size_t, SmallMapVector<size_t, SmallMapVector<Value *, unsigned, 2>, 2>,
8>
PossibleReducedVals;
initReductionOps(Root);
DenseMap<Value *, SmallVector<LoadInst *>> LoadsMap;
Expand Down

0 comments on commit d9497ad

Please sign in to comment.