Skip to content

Commit f086f38

Browse files
committed
[Attributor][NFCI] Move attribute collection and manifest to Attributor
Before, we checked and manifested attributes right in the IR. This was bad as we modified the IR before the manifest stage. Now we can add/remove/inspect attributes w/o going to the IR (except for the initial query).
1 parent 77dbd1d commit f086f38

File tree

4 files changed

+308
-254
lines changed

4 files changed

+308
-254
lines changed

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

Lines changed: 59 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,13 @@ struct IRPosition {
821821
"There is no attribute index for a floating or invalid position!");
822822
}
823823

824+
/// Return the value attributes are attached to.
825+
Value *getAttrListAnchor() const {
826+
if (auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
827+
return CB;
828+
return getAssociatedFunction();
829+
}
830+
824831
/// Return the attributes associated with this function or call site scope.
825832
AttributeList getAttrList() const {
826833
if (auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
@@ -878,51 +885,6 @@ struct IRPosition {
878885
return IRP_FLOAT;
879886
}
880887

881-
/// TODO: Figure out if the attribute related helper functions should live
882-
/// here or somewhere else.
883-
884-
/// Return true if any kind in \p AKs existing in the IR at a position that
885-
/// will affect this one. See also getAttrs(...).
886-
/// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
887-
/// e.g., the function position if this is an
888-
/// argument position, should be ignored.
889-
bool hasAttr(ArrayRef<Attribute::AttrKind> AKs,
890-
bool IgnoreSubsumingPositions = false,
891-
Attributor *A = nullptr) const;
892-
893-
/// Return the attributes of any kind in \p AKs existing in the IR at a
894-
/// position that will affect this one. While each position can only have a
895-
/// single attribute of any kind in \p AKs, there are "subsuming" positions
896-
/// that could have an attribute as well. This method returns all attributes
897-
/// found in \p Attrs.
898-
/// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
899-
/// e.g., the function position if this is an
900-
/// argument position, should be ignored.
901-
void getAttrs(ArrayRef<Attribute::AttrKind> AKs,
902-
SmallVectorImpl<Attribute> &Attrs,
903-
bool IgnoreSubsumingPositions = false,
904-
Attributor *A = nullptr) const;
905-
906-
/// Remove the attribute of kind \p AKs existing in the IR at this position.
907-
void removeAttrs(ArrayRef<Attribute::AttrKind> AKs) const {
908-
if (getPositionKind() == IRP_INVALID || getPositionKind() == IRP_FLOAT)
909-
return;
910-
911-
AttributeList AttrList = getAttrList();
912-
913-
bool Changed = false;
914-
unsigned Idx = getAttrIdx();
915-
LLVMContext &Ctx = getAnchorValue().getContext();
916-
for (Attribute::AttrKind AK : AKs) {
917-
if (!AttrList.hasAttributeAtIndex(Idx, AK))
918-
continue;
919-
Changed = true;
920-
AttrList = AttrList.removeAttributeAtIndex(Ctx, Idx, AK);
921-
}
922-
if (Changed)
923-
setAttrList(AttrList);
924-
}
925-
926888
bool isAnyCallSitePosition() const {
927889
switch (getPositionKind()) {
928890
case IRPosition::IRP_CALL_SITE:
@@ -1041,16 +1003,6 @@ struct IRPosition {
10411003
/// Verify internal invariants.
10421004
void verify();
10431005

1044-
/// Return the attributes of kind \p AK existing in the IR as attribute.
1045-
bool getAttrsFromIRAttr(Attribute::AttrKind AK,
1046-
SmallVectorImpl<Attribute> &Attrs) const;
1047-
1048-
/// Return the attributes of kind \p AK existing in the IR as operand bundles
1049-
/// of an llvm.assume.
1050-
bool getAttrsFromAssumes(Attribute::AttrKind AK,
1051-
SmallVectorImpl<Attribute> &Attrs,
1052-
Attributor &A) const;
1053-
10541006
/// Return the underlying pointer as Value *, valid for all positions but
10551007
/// IRP_CALL_SITE_ARGUMENT.
10561008
Value *getAsValuePtr() const {
@@ -1893,6 +1845,52 @@ struct Attributor {
18931845
ToBeDeletedFunctions.insert(&F);
18941846
}
18951847

1848+
/// Return the attributes of kind \p AK existing in the IR as operand bundles
1849+
/// of an llvm.assume.
1850+
bool getAttrsFromAssumes(const IRPosition &IRP, Attribute::AttrKind AK,
1851+
SmallVectorImpl<Attribute> &Attrs);
1852+
1853+
/// Return true if any kind in \p AKs existing in the IR at a position that
1854+
/// will affect this one. See also getAttrs(...).
1855+
/// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
1856+
/// e.g., the function position if this is an
1857+
/// argument position, should be ignored.
1858+
bool hasAttr(const IRPosition &IRP, ArrayRef<Attribute::AttrKind> AKs,
1859+
bool IgnoreSubsumingPositions = false,
1860+
Attribute::AttrKind ImpliedAttributeKind = Attribute::None);
1861+
1862+
/// Return the attributes of any kind in \p AKs existing in the IR at a
1863+
/// position that will affect this one. While each position can only have a
1864+
/// single attribute of any kind in \p AKs, there are "subsuming" positions
1865+
/// that could have an attribute as well. This method returns all attributes
1866+
/// found in \p Attrs.
1867+
/// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
1868+
/// e.g., the function position if this is an
1869+
/// argument position, should be ignored.
1870+
void getAttrs(const IRPosition &IRP, ArrayRef<Attribute::AttrKind> AKs,
1871+
SmallVectorImpl<Attribute> &Attrs,
1872+
bool IgnoreSubsumingPositions = false);
1873+
1874+
ChangeStatus removeAttrs(const IRPosition &IRP,
1875+
const ArrayRef<Attribute::AttrKind> &AttrKinds);
1876+
1877+
ChangeStatus manifestAttrs(const IRPosition &IRP,
1878+
const ArrayRef<Attribute> &DeducedAttrs,
1879+
bool ForceReplace = false);
1880+
1881+
private:
1882+
/// Helper to apply \p CB on all attributes of type \p AttrDescs of \p IRP.
1883+
template <typename DescTy>
1884+
ChangeStatus updateAttrMap(const IRPosition &IRP,
1885+
const ArrayRef<DescTy> &AttrDescs,
1886+
function_ref<bool(const DescTy &, AttributeSet,
1887+
AttributeMask &, AttrBuilder &)>
1888+
CB);
1889+
1890+
/// Mapping from functions/call sites to their attributes.
1891+
DenseMap<Value *, AttributeList> AttrsMap;
1892+
1893+
public:
18961894
/// If \p IRP is assumed to be a constant, return it, if it is unclear yet,
18971895
/// return std::nullopt, otherwise return `nullptr`.
18981896
std::optional<Constant *> getAssumedConstant(const IRPosition &IRP,
@@ -3095,14 +3093,6 @@ template <typename BaseTy> struct SetState : public AbstractState {
30953093
bool IsAtFixedpoint;
30963094
};
30973095

3098-
/// Helper struct necessary as the modular build fails if the virtual method
3099-
/// IRAttribute::manifest is defined in the Attributor.cpp.
3100-
struct IRAttributeManifest {
3101-
static ChangeStatus manifestAttrs(Attributor &A, const IRPosition &IRP,
3102-
const ArrayRef<Attribute> &DeducedAttrs,
3103-
bool ForceReplace = false);
3104-
};
3105-
31063096
/// Helper to tie a abstract state implementation to an abstract attribute.
31073097
template <typename StateTy, typename BaseType, class... Ts>
31083098
struct StateWrapper : public BaseType, public StateTy {
@@ -3129,7 +3119,7 @@ struct IRAttribute : public BaseType {
31293119
bool IgnoreSubsumingPositions = false) {
31303120
if (isa<UndefValue>(IRP.getAssociatedValue()))
31313121
return true;
3132-
return IRP.hasAttr(AttrKinds, IgnoreSubsumingPositions, &A);
3122+
return A.hasAttr(IRP, AttrKinds, IgnoreSubsumingPositions);
31333123
}
31343124

31353125
/// See AbstractAttribute::initialize(...).
@@ -3147,8 +3137,9 @@ struct IRAttribute : public BaseType {
31473137
return ChangeStatus::UNCHANGED;
31483138
SmallVector<Attribute, 4> DeducedAttrs;
31493139
getDeducedAttributes(this->getAnchorValue().getContext(), DeducedAttrs);
3150-
return IRAttributeManifest::manifestAttrs(A, this->getIRPosition(),
3151-
DeducedAttrs);
3140+
if (DeducedAttrs.empty())
3141+
return ChangeStatus::UNCHANGED;
3142+
return A.manifestAttrs(this->getIRPosition(), DeducedAttrs);
31523143
}
31533144

31543145
/// Return the kind that identifies the abstract attribute implementation.
@@ -3627,8 +3618,8 @@ struct AAWillReturn
36273618
return false;
36283619

36293620
SmallVector<Attribute, 1> Attrs;
3630-
IRP.getAttrs({Attribute::Memory}, Attrs,
3631-
/* IgnoreSubsumingPositions */ false);
3621+
A.getAttrs(IRP, {Attribute::Memory}, Attrs,
3622+
/* IgnoreSubsumingPositions */ false);
36323623

36333624
MemoryEffects ME = MemoryEffects::unknown();
36343625
for (const Attribute &Attr : Attrs)

llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ struct AAUniformWorkGroupSizeFunction : public AAUniformWorkGroupSize {
368368

369369
AttrList.push_back(Attribute::get(Ctx, "uniform-work-group-size",
370370
getAssumed() ? "true" : "false"));
371-
return IRAttributeManifest::manifestAttrs(A, getIRPosition(), AttrList,
372-
/* ForceReplace */ true);
371+
return A.manifestAttrs(getIRPosition(), AttrList,
372+
/* ForceReplace */ true);
373373
}
374374

375375
bool isValidState() const override {
@@ -526,8 +526,8 @@ struct AAAMDAttributesFunction : public AAAMDAttributes {
526526
AttrList.push_back(Attribute::get(Ctx, Attr.second));
527527
}
528528

529-
return IRAttributeManifest::manifestAttrs(A, getIRPosition(), AttrList,
530-
/* ForceReplace */ true);
529+
return A.manifestAttrs(getIRPosition(), AttrList,
530+
/* ForceReplace */ true);
531531
}
532532

533533
const std::string getAsStr() const override {
@@ -732,9 +732,9 @@ struct AAAMDSizeRangeAttribute
732732
SmallString<10> Buffer;
733733
raw_svector_ostream OS(Buffer);
734734
OS << getAssumed().getLower() << ',' << getAssumed().getUpper() - 1;
735-
return IRAttributeManifest::manifestAttrs(
736-
A, getIRPosition(), {Attribute::get(Ctx, AttrName, OS.str())},
737-
/* ForceReplace */ true);
735+
return A.manifestAttrs(getIRPosition(),
736+
{Attribute::get(Ctx, AttrName, OS.str())},
737+
/* ForceReplace */ true);
738738
}
739739

740740
const std::string getAsStr() const override {

0 commit comments

Comments
 (0)