Skip to content

[NFC][AMDGPU] Refactor handling of amdgpu-synchronize-as MD on fences #148630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions llvm/lib/Target/AMDGPU/SIMemoryLegalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,15 +705,15 @@ void diagnoseUnknownMMRAASName(const MachineInstr &MI, StringRef AS) {
}

/// Reads \p MI's MMRAs to parse the "amdgpu-synchronize-as" MMRA.
/// If this tag isn't present, or if it has no meaningful values, returns \p
/// Default. Otherwise returns all the address spaces concerned by the MMRA.
static SIAtomicAddrSpace getFenceAddrSpaceMMRA(const MachineInstr &MI,
SIAtomicAddrSpace Default) {
/// If this tag isn't present, or if it has no meaningful values, returns
/// \p none, otherwise returns the address spaces specified by the MD.
static std::optional<SIAtomicAddrSpace>
getSynchronizeAddrSpaceMD(const MachineInstr &MI) {
static constexpr StringLiteral FenceASPrefix = "amdgpu-synchronize-as";

auto MMRA = MMRAMetadata(MI.getMMRAMetadata());
if (!MMRA)
return Default;
return std::nullopt;

SIAtomicAddrSpace Result = SIAtomicAddrSpace::NONE;
for (const auto &[Prefix, Suffix] : MMRA) {
Expand All @@ -726,7 +726,10 @@ static SIAtomicAddrSpace getFenceAddrSpaceMMRA(const MachineInstr &MI,
diagnoseUnknownMMRAASName(MI, Suffix);
}

return (Result != SIAtomicAddrSpace::NONE) ? Result : Default;
if (Result == SIAtomicAddrSpace::NONE)
return std::nullopt;

return Result;
}

} // end anonymous namespace
Expand Down Expand Up @@ -903,12 +906,19 @@ SIMemOpAccess::getAtomicFenceInfo(const MachineBasicBlock::iterator &MI) const {
std::tie(Scope, OrderingAddrSpace, IsCrossAddressSpaceOrdering) =
*ScopeOrNone;

if ((OrderingAddrSpace == SIAtomicAddrSpace::NONE) ||
((OrderingAddrSpace & SIAtomicAddrSpace::ATOMIC) != OrderingAddrSpace)) {
if (OrderingAddrSpace != SIAtomicAddrSpace::ATOMIC) {
// We currently expect refineOrderingAS to be the only place that
// can refine the AS ordered by the fence.
// If that changes, we need to review the semantics of that function
// in case it needs to preserve certain address spaces.
reportUnsupported(MI, "Unsupported atomic address space");
return std::nullopt;
}

auto SynchronizeAS = getSynchronizeAddrSpaceMD(*MI);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no auto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type name is quite long and distracting. I think we generally allow auto in such cases, at least we idiomatically use auto with functions like getIConstantVRegValWithLookThrough everywhere in global isel

if (SynchronizeAS)
OrderingAddrSpace = *SynchronizeAS;

return SIMemOpInfo(Ordering, Scope, OrderingAddrSpace, SIAtomicAddrSpace::ATOMIC,
IsCrossAddressSpaceOrdering, AtomicOrdering::NotAtomic);
}
Expand Down Expand Up @@ -2687,11 +2697,7 @@ bool SIMemoryLegalizer::expandAtomicFence(const SIMemOpInfo &MOI,
AtomicPseudoMIs.push_back(MI);
bool Changed = false;

// Refine fenced address space based on MMRAs.
//
// TODO: Should we support this MMRA on other atomic operations?
auto OrderingAddrSpace =
getFenceAddrSpaceMMRA(*MI, MOI.getOrderingAddrSpace());
const SIAtomicAddrSpace OrderingAddrSpace = MOI.getOrderingAddrSpace();

if (MOI.isAtomic()) {
const AtomicOrdering Order = MOI.getOrdering();
Expand Down
Loading