Skip to content

Commit

Permalink
[Attributor] Avoid more AAs through IR implication
Browse files Browse the repository at this point in the history
  • Loading branch information
jdoerfert committed Jun 24, 2023
1 parent 2caf64d commit 339a1f3
Show file tree
Hide file tree
Showing 29 changed files with 298 additions and 316 deletions.
41 changes: 30 additions & 11 deletions llvm/include/llvm/Transforms/IPO/Attributor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3670,6 +3670,22 @@ struct AANoAlias
StateWrapper<BooleanState, AbstractAttribute>> {
AANoAlias(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}

static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
ArrayRef<Attribute::AttrKind> AttrKinds,
bool IgnoreSubsumingPositions = false) {
if (IRAttribute::isImpliedByIR(A, IRP, AttrKinds))
return true;

Value &Val = IRP.getAnchorValue();
if (isa<AllocaInst>(Val))
return true;
if (isa<ConstantPointerNull>(Val) &&
!NullPointerIsDefined(IRP.getAnchorScope(),
Val.getType()->getPointerAddressSpace()))
return true;
return false;
}

/// Return true if we assume that the underlying value is alias.
bool isAssumedNoAlias() const { return getAssumed(); }

Expand Down Expand Up @@ -5797,24 +5813,27 @@ bool hasAssumedIRAttr(Attributor &A, const AbstractAttribute &QueryingAA,
bool IgnoreSubsumingPositions = false) {
IsKnown = false;
switch (AK) {
#define CASE(ATTRNAME, AANAME) \
#define CASE(ATTRNAME, AANAME, ...) \
case Attribute::ATTRNAME: { \
if (AANAME::isImpliedByIR(A, IRP, {AK}, IgnoreSubsumingPositions)) \
return IsKnown = true; \
const auto *AA = A.getAAFor<AANAME>(QueryingAA, IRP, DepClass); \
if (!AA || !AA->isAssumed()) \
if (!AA || !AA->isAssumed(__VA_ARGS__)) \
return false; \
IsKnown = AA->isKnown(); \
IsKnown = AA->isKnown(__VA_ARGS__); \
return true; \
}
CASE(NoUnwind, AANoUnwind);
CASE(WillReturn, AAWillReturn);
CASE(NoFree, AANoFree);
CASE(NoCapture, AANoCapture);
CASE(NoRecurse, AANoRecurse);
CASE(NoSync, AANoSync);
CASE(NoAlias, AANoAlias);
CASE(MustProgress, AAMustProgress);
CASE(NoUnwind, AANoUnwind, );
CASE(WillReturn, AAWillReturn, );
CASE(NoFree, AANoFree, );
CASE(NoCapture, AANoCapture, );
CASE(NoRecurse, AANoRecurse, );
CASE(NoSync, AANoSync, );
CASE(NoAlias, AANoAlias, );
CASE(MustProgress, AAMustProgress, );
CASE(ReadNone, AAMemoryBehavior, AAMemoryBehavior::NO_ACCESSES);
CASE(ReadOnly, AAMemoryBehavior, AAMemoryBehavior::NO_WRITES);
CASE(WriteOnly, AAMemoryBehavior, AAMemoryBehavior::NO_READS);
#undef CASE
default:
llvm_unreachable("hasAssumedIRAttr not available for this attribute kind");
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/Transforms/IPO/Attributor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,15 @@ bool AA::getPotentialCopiesOfStoredValue(
static bool isAssumedReadOnlyOrReadNone(Attributor &A, const IRPosition &IRP,
const AbstractAttribute &QueryingAA,
bool RequireReadNone, bool &IsKnown) {
if (RequireReadNone) {
if (AA::hasAssumedIRAttr<Attribute::ReadNone>(
A, QueryingAA, IRP, DepClassTy::OPTIONAL, IsKnown,
/* IgnoreSubsumingPositions */ true))
return true;
} else if (AA::hasAssumedIRAttr<Attribute::ReadOnly>(
A, QueryingAA, IRP, DepClassTy::OPTIONAL, IsKnown,
/* IgnoreSubsumingPositions */ true))
return true;

IRPosition::Kind Kind = IRP.getPositionKind();
if (Kind == IRPosition::IRP_FUNCTION || Kind == IRPosition::IRP_CALL_SITE) {
Expand Down
10 changes: 0 additions & 10 deletions llvm/lib/Transforms/IPO/AttributorAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3466,16 +3466,6 @@ struct AAWillReturnImpl : public AAWillReturn {
AAWillReturnImpl(const IRPosition &IRP, Attributor &A)
: AAWillReturn(IRP, A) {}

/// See AbstractAttribute::initialize(...).
void initialize(Attributor &A) override {
AAWillReturn::initialize(A);

if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ true)) {
indicateOptimisticFixpoint();
return;
}
}

/// Check for `mustprogress` and `readonly` as they imply `willreturn`.
bool isImpliedByMustprogressAndReadonly(Attributor &A, bool KnownOnly) {
// Check for `mustprogress` in the scope and the associated function which
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ define i32 @callercaller() {
;.
; TUNIT: attributes #[[ATTR0]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) }
; TUNIT: attributes #[[ATTR1]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) }
; TUNIT: attributes #[[ATTR2]] = { nofree nosync nounwind willreturn }
; TUNIT: attributes #[[ATTR2]] = { nofree nosync nounwind willreturn memory(write) }
;.
; CGSCC: attributes #[[ATTR0]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) }
; CGSCC: attributes #[[ATTR1]] = { mustprogress nofree nosync nounwind willreturn memory(none) }
Expand Down
7 changes: 4 additions & 3 deletions llvm/test/Transforms/Attributor/ArgumentPromotion/musttail.ll
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ define internal i32 @test2b(ptr %p, i32 %p2) {
; TUNIT-NEXT: [[A:%.*]] = load i32, ptr [[A_GEP]], align 4
; TUNIT-NEXT: [[B:%.*]] = load i32, ptr [[B_GEP]], align 4
; TUNIT-NEXT: [[V:%.*]] = add i32 [[A]], [[B]]
; TUNIT-NEXT: [[CA:%.*]] = musttail call noundef i32 @bar(ptr undef, i32 [[V]]) #[[ATTR4]]
; TUNIT-NEXT: [[CA:%.*]] = musttail call noundef i32 @bar(ptr undef, i32 [[V]]) #[[ATTR5:[0-9]+]]
; TUNIT-NEXT: ret i32 [[CA]]
;
; CGSCC: Function Attrs: mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite)
Expand All @@ -154,7 +154,7 @@ define i32 @caller2b(ptr %g) {
; TUNIT: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite)
; TUNIT-LABEL: define {{[^@]+}}@caller2b
; TUNIT-SAME: (ptr nocapture nofree readonly [[G:%.*]]) #[[ATTR3]] {
; TUNIT-NEXT: [[V:%.*]] = call noundef i32 @test2b(ptr nocapture nofree readonly [[G]], i32 undef) #[[ATTR4]]
; TUNIT-NEXT: [[V:%.*]] = call noundef i32 @test2b(ptr nocapture nofree readonly [[G]], i32 undef) #[[ATTR5]]
; TUNIT-NEXT: ret i32 [[V]]
;
; CGSCC: Function Attrs: mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite)
Expand All @@ -171,7 +171,8 @@ define i32 @caller2b(ptr %g) {
; TUNIT: attributes #[[ATTR1]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) }
; TUNIT: attributes #[[ATTR2]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) }
; TUNIT: attributes #[[ATTR3]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) }
; TUNIT: attributes #[[ATTR4]] = { nofree nosync nounwind willreturn }
; TUNIT: attributes #[[ATTR4]] = { nofree nosync nounwind willreturn memory(read) }
; TUNIT: attributes #[[ATTR5]] = { nofree nosync nounwind willreturn }
;.
; CGSCC: attributes #[[ATTR0]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) }
; CGSCC: attributes #[[ATTR1]] = { mustprogress nofree nosync nounwind willreturn memory(argmem: read) }
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/Attributor/ArgumentPromotion/sret.ll
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ define void @f() {
;.
; TUNIT: attributes #[[ATTR0]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) }
; TUNIT: attributes #[[ATTR1]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) }
; TUNIT: attributes #[[ATTR2]] = { nofree nosync nounwind willreturn }
; TUNIT: attributes #[[ATTR2]] = { nofree nosync nounwind willreturn memory(write) }
;.
; CGSCC: attributes #[[ATTR0]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) }
; CGSCC: attributes #[[ATTR1]] = { mustprogress nofree nosync nounwind willreturn memory(none) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ declare i32 @__gxx_personality_v0(...)
;.
; TUNIT: attributes #[[ATTR0]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) }
; TUNIT: attributes #[[ATTR1]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) }
; TUNIT: attributes #[[ATTR2]] = { nofree nosync nounwind willreturn }
; TUNIT: attributes #[[ATTR2]] = { nofree nosync nounwind willreturn memory(write) }
;.
; CGSCC: attributes #[[ATTR0]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) }
; CGSCC: attributes #[[ATTR1]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) }
Expand Down

0 comments on commit 339a1f3

Please sign in to comment.