Skip to content

Commit 754b93e

Browse files
authored
[Attributor] New attribute to identify what byte ranges are alive for an allocation (#66148)
Changes the size of allocations automatically. For now, implements the case when a single range from start of the allocation is alive and the allocation can be reduced.
1 parent 0f8d3e6 commit 754b93e

File tree

13 files changed

+830
-23
lines changed

13 files changed

+830
-23
lines changed

llvm/include/llvm/Transforms/IPO/Attributor.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
#include "llvm/ADT/STLExtras.h"
104104
#include "llvm/ADT/SetOperations.h"
105105
#include "llvm/ADT/SetVector.h"
106+
#include "llvm/ADT/SmallSet.h"
106107
#include "llvm/ADT/iterator.h"
107108
#include "llvm/Analysis/AssumeBundleQueries.h"
108109
#include "llvm/Analysis/CFG.h"
@@ -132,6 +133,7 @@
132133
#include "llvm/Support/ErrorHandling.h"
133134
#include "llvm/Support/ModRef.h"
134135
#include "llvm/Support/TimeProfiler.h"
136+
#include "llvm/Support/TypeSize.h"
135137
#include "llvm/TargetParser/Triple.h"
136138
#include "llvm/Transforms/Utils/CallGraphUpdater.h"
137139

@@ -6105,6 +6107,12 @@ struct AAPointerInfo : public AbstractAttribute {
61056107
/// See AbstractAttribute::getIdAddr()
61066108
const char *getIdAddr() const override { return &ID; }
61076109

6110+
using OffsetBinsTy = DenseMap<AA::RangeTy, SmallSet<unsigned, 4>>;
6111+
using const_bin_iterator = OffsetBinsTy::const_iterator;
6112+
virtual const_bin_iterator begin() const = 0;
6113+
virtual const_bin_iterator end() const = 0;
6114+
virtual int64_t numOffsetBins() const = 0;
6115+
61086116
/// Call \p CB on all accesses that might interfere with \p Range and return
61096117
/// true if all such accesses were known and the callback returned true for
61106118
/// all of them, false otherwise. An access interferes with an offset-size
@@ -6258,6 +6266,41 @@ struct AAAddressSpace : public StateWrapper<BooleanState, AbstractAttribute> {
62586266
static const char ID;
62596267
};
62606268

6269+
struct AAAllocationInfo : public StateWrapper<BooleanState, AbstractAttribute> {
6270+
AAAllocationInfo(const IRPosition &IRP, Attributor &A)
6271+
: StateWrapper<BooleanState, AbstractAttribute>(IRP) {}
6272+
6273+
/// See AbstractAttribute::isValidIRPositionForInit
6274+
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
6275+
if (!IRP.getAssociatedType()->isPtrOrPtrVectorTy())
6276+
return false;
6277+
return AbstractAttribute::isValidIRPositionForInit(A, IRP);
6278+
}
6279+
6280+
/// Create an abstract attribute view for the position \p IRP.
6281+
static AAAllocationInfo &createForPosition(const IRPosition &IRP,
6282+
Attributor &A);
6283+
6284+
virtual std::optional<TypeSize> getAllocatedSize() const = 0;
6285+
6286+
/// See AbstractAttribute::getName()
6287+
const std::string getName() const override { return "AAAllocationInfo"; }
6288+
6289+
/// See AbstractAttribute::getIdAddr()
6290+
const char *getIdAddr() const override { return &ID; }
6291+
6292+
/// This function should return true if the type of the \p AA is
6293+
/// AAAllocationInfo
6294+
static bool classof(const AbstractAttribute *AA) {
6295+
return (AA->getIdAddr() == &ID);
6296+
}
6297+
6298+
constexpr static const std::optional<TypeSize> HasNoAllocationSize =
6299+
std::optional<TypeSize>(TypeSize(-1, true));
6300+
6301+
static const char ID;
6302+
};
6303+
62616304
/// An abstract interface for llvm::GlobalValue information interference.
62626305
struct AAGlobalValueInfo
62636306
: public StateWrapper<BooleanState, AbstractAttribute> {

llvm/lib/Transforms/IPO/Attributor.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3611,14 +3611,13 @@ void Attributor::identifyDefaultAbstractAttributes(Function &F) {
36113611
};
36123612

36133613
auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
3614-
bool Success;
3614+
[[maybe_unused]] bool Success;
36153615
bool UsedAssumedInformation = false;
36163616
Success = checkForAllInstructionsImpl(
36173617
nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr,
36183618
{(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
36193619
(unsigned)Instruction::Call},
36203620
UsedAssumedInformation);
3621-
(void)Success;
36223621
assert(Success && "Expected the check call to be successful!");
36233622

36243623
auto LoadStorePred = [&](Instruction &I) -> bool {
@@ -3644,7 +3643,17 @@ void Attributor::identifyDefaultAbstractAttributes(Function &F) {
36443643
nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr,
36453644
{(unsigned)Instruction::Load, (unsigned)Instruction::Store},
36463645
UsedAssumedInformation);
3647-
(void)Success;
3646+
assert(Success && "Expected the check call to be successful!");
3647+
3648+
// AllocaInstPredicate
3649+
auto AAAllocationInfoPred = [&](Instruction &I) -> bool {
3650+
getOrCreateAAFor<AAAllocationInfo>(IRPosition::value(I));
3651+
return true;
3652+
};
3653+
3654+
Success = checkForAllInstructionsImpl(
3655+
nullptr, OpcodeInstMap, AAAllocationInfoPred, nullptr, nullptr,
3656+
{(unsigned)Instruction::Alloca}, UsedAssumedInformation);
36483657
assert(Success && "Expected the check call to be successful!");
36493658
}
36503659

0 commit comments

Comments
 (0)