[Attributor] Change allocation size and load/store offsets using AAPointerInfo for Alloca instructions by vidsinghal · Pull Request #72029 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-llvm-transforms

Author: Vidhush Singhal (vidsinghal)

Changes

Work in progress

a.) Need to fix some TODOs (if offsets need to be changed for Load and Store instructions)
b.) For some tests assertions fail

opt: llvm-project/llvm/include/llvm/Transforms/IPO/Attributor.h:1870: bool llvm::Attributor::changeAfterManifest(const IRPosition, Value &, bool): Assertion `(!CurNV || CurNV == &NV || isa<UndefValue>(NV)) && "Value replacement was registered twice with different values!"' failed.

Patch is 295.62 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/72029.diff

46 Files Affected:

diff --git a/llvm/include/llvm/Transforms/IPO/Attributor.h b/llvm/include/llvm/Transforms/IPO/Attributor.h index bd1bd8261123e51..8488568113d553c 100644 --- a/llvm/include/llvm/Transforms/IPO/Attributor.h +++ b/llvm/include/llvm/Transforms/IPO/Attributor.h @@ -103,6 +103,7 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SetOperations.h" #include "llvm/ADT/SetVector.h" +#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/iterator.h" #include "llvm/Analysis/AssumeBundleQueries.h" #include "llvm/Analysis/CFG.h" @@ -132,6 +133,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ModRef.h" #include "llvm/Support/TimeProfiler.h" +#include "llvm/Support/TypeSize.h" #include "llvm/TargetParser/Triple.h" #include "llvm/Transforms/Utils/CallGraphUpdater.h" @@ -6117,6 +6119,14 @@ struct AAPointerInfo : public AbstractAttribute { /// See AbstractAttribute::getIdAddr() const char *getIdAddr() const override { return &ID; } + using OffsetBinsTy = DenseMap<AA::RangeTy, SmallSet<unsigned, 4>>; + using const_bin_iterator = OffsetBinsTy::const_iterator; + virtual const_bin_iterator begin() const = 0; + virtual const_bin_iterator end() const = 0; + virtual int64_t numOffsetBins() const = 0; + virtual void dumpState(raw_ostream &O) const = 0; + virtual const Access &getBinAccess(unsigned Index) const = 0; + /// Call \p CB on all accesses that might interfere with \p Range and return /// true if all such accesses were known and the callback returned true for /// all of them, false otherwise. An access interferes with an offset-size @@ -6270,6 +6280,44 @@ struct AAAddressSpace : public StateWrapper<BooleanState, AbstractAttribute> { static const char ID; }; +struct AAAllocationInfo : public StateWrapper<BooleanState, AbstractAttribute> { + AAAllocationInfo(const IRPosition &IRP, Attributor &A) + : StateWrapper<BooleanState, AbstractAttribute>(IRP) {} + + /// See AbstractAttribute::isValidIRPositionForInit + static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) { + if (!IRP.getAssociatedType()->isPtrOrPtrVectorTy()) + return false; + return AbstractAttribute::isValidIRPositionForInit(A, IRP); + } + + /// Create an abstract attribute view for the position \p IRP. + static AAAllocationInfo &createForPosition(const IRPosition &IRP, + Attributor &A); + + virtual std::optional getAllocatedSize() const = 0; + + using NewOffsetsTy = DenseMap<AA::RangeTy, AA::RangeTy>; + virtual const NewOffsetsTy &getNewOffsets() const = 0; + + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAAllocationInfo"; } + + /// See AbstractAttribute::getIdAddr() + const char *getIdAddr() const override { return &ID; } + + /// This function should return true if the type of the \p AA is + /// AAAllocationInfo + static bool classof(const AbstractAttribute *AA) { + return (AA->getIdAddr() == &ID); + } + + constexpr static const std::optional HasNoAllocationSize = + std::optional(TypeSize(-1, true)); + + static const char ID; +}; + /// An abstract interface for llvm::GlobalValue information interference. struct AAGlobalValueInfo : public StateWrapper<BooleanState, AbstractAttribute> { diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp index 49ced893d5c7340..f1a88bc564ced71 100644 --- a/llvm/lib/Transforms/IPO/Attributor.cpp +++ b/llvm/lib/Transforms/IPO/Attributor.cpp @@ -3611,14 +3611,13 @@ void Attributor::identifyDefaultAbstractAttributes(Function &F) { }; auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); - bool Success; + [[maybe_unused]] bool Success; bool UsedAssumedInformation = false; Success = checkForAllInstructionsImpl( nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr, {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, (unsigned)Instruction::Call}, UsedAssumedInformation); - (void)Success; assert(Success && "Expected the check call to be successful!"); auto LoadStorePred = [&](Instruction &I) -> bool { @@ -3644,7 +3643,17 @@ void Attributor::identifyDefaultAbstractAttributes(Function &F) { nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr, {(unsigned)Instruction::Load, (unsigned)Instruction::Store}, UsedAssumedInformation); - (void)Success; + assert(Success && "Expected the check call to be successful!"); + + // AllocaInstPredicate + auto AAAllocationInfoPred = [&](Instruction &I) -> bool { + getOrCreateAAFor(IRPosition::value(I)); + return true; + }; + + Success = checkForAllInstructionsImpl( + nullptr, OpcodeInstMap, AAAllocationInfoPred, nullptr, nullptr, + {(unsigned)Instruction::Alloca}, UsedAssumedInformation); assert(Success && "Expected the check call to be successful!"); } diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp index bbb0cfa0eb05fe6..dea00c0039798b2 100644 --- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp +++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp @@ -65,6 +65,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/GraphWriter.h" #include "llvm/Support/MathExtras.h" +#include "llvm/Support/TypeSize.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/CallPromotionUtils.h" @@ -192,6 +193,7 @@ PIPE_OPERATOR(AAPointerInfo) PIPE_OPERATOR(AAAssumptionInfo) PIPE_OPERATOR(AAUnderlyingObjects) PIPE_OPERATOR(AAAddressSpace) +PIPE_OPERATOR(AAAllocationInfo) PIPE_OPERATOR(AAIndirectCallInfo) PIPE_OPERATOR(AAGlobalValueInfo) PIPE_OPERATOR(AADenormalFPMath) @@ -881,11 +883,9 @@ struct AA::PointerInfo::State : public AbstractState { AAPointerInfo::AccessKind Kind, Type *Ty, Instruction *RemoteI = nullptr); - using OffsetBinsTy = DenseMap<RangeTy, SmallSet<unsigned, 4>>;

@@ -905,7 +905,7 @@ struct AA::PointerInfo::State : public AbstractState { // are all combined into a single Access object. This may result in loss of // information in RangeTy in the Access object. SmallVectorAAPointerInfo::Access AccessList;

@@ -1109,6 +1109,16 @@ struct AAPointerInfoImpl return AAPointerInfo::manifest(A); }

@@ -1455,7 +1465,7 @@ struct AAPointerInfoImpl void trackPointerInfoStatistics(const IRPosition &IRP) const {}

/// Dump the state into \p O.

@@ -6521,7 +6531,7 @@ struct AAValueSimplifyCallSiteReturned : AAValueSimplifyImpl {

/// See AbstractAttribute::updateImpl(...). ChangeStatus updateImpl(Attributor &A) override {

}

void trackStatistics() const override { @@ -12688,6 +12698,330 @@ struct AAAddressSpaceCallSiteArgument final : AAAddressSpaceImpl { }; } // namespace

+/// ----------- Allocation Info ---------- +namespace { +struct AAAllocationInfoImpl : public AAAllocationInfo {

diff --git a/llvm/test/Transforms/Attributor/ArgumentPromotion/2008-02-01-ReturnAttrs.ll b/llvm/test/Transforms/Attributor/ArgumentPromotion/2008-02-01-ReturnAttrs.ll index 8254ab6230440f5..e6062da1d1457b9 100644 --- a/llvm/test/Transforms/Attributor/ArgumentPromotion/2008-02-01-ReturnAttrs.ll +++ b/llvm/test/Transforms/Attributor/ArgumentPromotion/2008-02-01-ReturnAttrs.ll @@ -9,8 +9,8 @@ define internal i32 @deref(ptr %x) nounwind { ; CG... [truncated]