Skip to content

AMDGPU: Custom expand flat cmpxchg which may access private #109410

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 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions llvm/include/llvm/CodeGen/TargetLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,11 @@ class TargetLoweringBase {
"Generic atomicrmw expansion unimplemented on this target");
}

/// Perform a cmpxchg expansion using a target-specific method.
virtual void emitExpandAtomicCmpXchg(AtomicCmpXchgInst *CI) const {
llvm_unreachable("Generic cmpxchg expansion unimplemented on this target");
}

/// Perform a bit test atomicrmw using a target-specific intrinsic. This
/// represents the combined bit test intrinsic which will be lowered at a late
/// stage by the backend.
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/Transforms/Utils/LowerAtomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class IRBuilderBase;
/// Convert the given Cmpxchg into primitive load and compare.
bool lowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI);

/// Emit IR to implement the given cmpxchg operation on values in registers,
/// returning the new value.
std::pair<Value *, Value *> buildCmpXchgValue(IRBuilderBase &Builder,
Value *Ptr, Value *Cmp,
Value *Val, Align Alignment);

/// Convert the given RMWI into primitive load and stores,
/// assuming that doing so is legal. Return true if the lowering
/// succeeds.
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/CodeGen/AtomicExpandPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,10 @@ bool AtomicExpandImpl::tryExpandAtomicCmpXchg(AtomicCmpXchgInst *CI) {
return true;
case TargetLoweringBase::AtomicExpansionKind::NotAtomic:
return lowerAtomicCmpXchgInst(CI);
case TargetLoweringBase::AtomicExpansionKind::Expand: {
TLI->emitExpandAtomicCmpXchg(CI);
return true;
}
}
}

Expand Down
159 changes: 101 additions & 58 deletions llvm/lib/Target/AMDGPU/SIISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16322,15 +16322,14 @@ static bool flatInstrMayAccessPrivate(const Instruction *I) {
++I) {
auto *Low = mdconst::extract<ConstantInt>(
NoaliasAddrSpaceMD->getOperand(2 * I + 0));
auto *High = mdconst::extract<ConstantInt>(
NoaliasAddrSpaceMD->getOperand(2 * I + 1));

if (Low->getValue().uge(AMDGPUAS::PRIVATE_ADDRESS) &&
High->getValue().ult(AMDGPUAS::PRIVATE_ADDRESS))
return true;
if (Low->getValue().uge(AMDGPUAS::PRIVATE_ADDRESS)) {
auto *High = mdconst::extract<ConstantInt>(
NoaliasAddrSpaceMD->getOperand(2 * I + 1));
return High->getValue().ule(AMDGPUAS::PRIVATE_ADDRESS);
}
}

return false;
return true;
}

TargetLowering::AtomicExpansionKind
Expand Down Expand Up @@ -16577,9 +16576,21 @@ SITargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {

TargetLowering::AtomicExpansionKind
SITargetLowering::shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *CmpX) const {
return CmpX->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS
? AtomicExpansionKind::NotAtomic
: AtomicExpansionKind::None;
unsigned AddrSpace = CmpX->getPointerAddressSpace();
if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS)
return AtomicExpansionKind::NotAtomic;

if (AddrSpace != AMDGPUAS::FLAT_ADDRESS || !flatInstrMayAccessPrivate(CmpX))
return AtomicExpansionKind::None;

const DataLayout &DL = CmpX->getDataLayout();

Type *ValTy = CmpX->getNewValOperand()->getType();

// If a 64-bit flat atomic may alias private, we need to avoid using the
// atomic in the private case.
return DL.getTypeSizeInBits(ValTy) == 64 ? AtomicExpansionKind::Expand
: AtomicExpansionKind::None;
}

const TargetRegisterClass *
Expand Down Expand Up @@ -16745,40 +16756,8 @@ bool SITargetLowering::checkForPhysRegDependency(
return false;
}

void SITargetLowering::emitExpandAtomicRMW(AtomicRMWInst *AI) const {
AtomicRMWInst::BinOp Op = AI->getOperation();

if (Op == AtomicRMWInst::Sub || Op == AtomicRMWInst::Or ||
Op == AtomicRMWInst::Xor) {
if (auto *ConstVal = dyn_cast<Constant>(AI->getValOperand());
ConstVal && ConstVal->isNullValue()) {
// atomicrmw or %ptr, 0 -> atomicrmw add %ptr, 0
AI->setOperation(AtomicRMWInst::Add);

// TODO: Turn the below private handling into a no-op for idempotent
// cases.
}
}

// The non-flat expansions should only perform the de-canonicalization of
// identity values.
if (AI->getPointerAddressSpace() != AMDGPUAS::FLAT_ADDRESS)
return;

// FullFlatEmulation is true if we need to issue the private, shared, and
// global cases.
//
// If this is false, we are only dealing with the flat-targeting-private case,
// where we only insert a check for private and still use the flat instruction
// for global and shared.

// TODO: Avoid the private check for the fadd case depending on
// noalias.addrspace.

bool FullFlatEmulation = Op == AtomicRMWInst::FAdd &&
Subtarget->hasAtomicFaddInsts() &&
AI->getType()->isFloatTy();

void SITargetLowering::emitExpandAtomicAddrSpacePredicate(
Instruction *AI) const {
// Given: atomicrmw fadd ptr %addr, float %val ordering
//
// With this expansion we produce the following code:
Expand Down Expand Up @@ -16825,6 +16804,34 @@ void SITargetLowering::emitExpandAtomicRMW(AtomicRMWInst *AI) const {
IRBuilder<> Builder(AI);
LLVMContext &Ctx = Builder.getContext();

auto *RMW = dyn_cast<AtomicRMWInst>(AI);
const unsigned PtrOpIdx = RMW ? AtomicRMWInst::getPointerOperandIndex()
: AtomicCmpXchgInst::getPointerOperandIndex();
Value *Addr = AI->getOperand(PtrOpIdx);

/// TODO: Only need to check private, then emit flat-known-not private (no
/// need for shared block, or cast to global).
AtomicCmpXchgInst *CX = dyn_cast<AtomicCmpXchgInst>(AI);

Align Alignment;
if (RMW)
Alignment = RMW->getAlign();
else if (CX)
Alignment = CX->getAlign();
else
llvm_unreachable("unhandled atomic operation");

// FullFlatEmulation is true if we need to issue the private, shared, and
// global cases.
//
// If this is false, we are only dealing with the flat-targeting-private case,
// where we only insert a check for private and still use the flat instruction
// for global and shared.

bool FullFlatEmulation = RMW && RMW->getOperation() == AtomicRMWInst::FAdd &&
Subtarget->hasAtomicFaddInsts() &&
RMW->getType()->isFloatTy();

// If the return value isn't used, do not introduce a false use in the phi.
bool ReturnValueIsUsed = !AI->use_empty();

Expand All @@ -16846,11 +16853,6 @@ void SITargetLowering::emitExpandAtomicRMW(AtomicRMWInst *AI) const {
BasicBlock *GlobalBB = BasicBlock::Create(Ctx, "atomicrmw.global", F, ExitBB);
BasicBlock *PhiBB = BasicBlock::Create(Ctx, "atomicrmw.phi", F, ExitBB);

Value *Val = AI->getValOperand();
Type *ValTy = Val->getType();
Value *Addr = AI->getPointerOperand();
Align Alignment = AI->getAlign();

std::prev(BB->end())->eraseFromParent();
Builder.SetInsertPoint(BB);

Expand All @@ -16865,8 +16867,7 @@ void SITargetLowering::emitExpandAtomicRMW(AtomicRMWInst *AI) const {

Instruction *Clone = AI->clone();
Clone->insertInto(SharedBB, SharedBB->end());
Clone->getOperandUse(AtomicRMWInst::getPointerOperandIndex())
.set(CastToLocal);
Clone->getOperandUse(PtrOpIdx).set(CastToLocal);
LoadedShared = Clone;

Builder.CreateBr(PhiBB);
Expand All @@ -16878,14 +16879,29 @@ void SITargetLowering::emitExpandAtomicRMW(AtomicRMWInst *AI) const {
Builder.CreateCondBr(IsPrivate, PrivateBB, GlobalBB);

Builder.SetInsertPoint(PrivateBB);

Value *CastToPrivate = Builder.CreateAddrSpaceCast(
Addr, PointerType::get(Ctx, AMDGPUAS::PRIVATE_ADDRESS));
Value *LoadedPrivate = Builder.CreateAlignedLoad(ValTy, CastToPrivate,
Alignment, "loaded.private");

Value *NewVal = buildAtomicRMWValue(Op, Builder, LoadedPrivate, Val);
Value *LoadedPrivate;
if (RMW) {
LoadedPrivate = Builder.CreateAlignedLoad(
RMW->getType(), CastToPrivate, RMW->getAlign(), "loaded.private");

Value *NewVal = buildAtomicRMWValue(RMW->getOperation(), Builder,
LoadedPrivate, RMW->getValOperand());

Builder.CreateAlignedStore(NewVal, CastToPrivate, RMW->getAlign());
} else {
auto [ResultLoad, Equal] =
buildCmpXchgValue(Builder, CastToPrivate, CX->getCompareOperand(),
CX->getNewValOperand(), CX->getAlign());

Value *Insert = Builder.CreateInsertValue(PoisonValue::get(CX->getType()),
ResultLoad, 0);
LoadedPrivate = Builder.CreateInsertValue(Insert, Equal, 1);
}

Builder.CreateAlignedStore(NewVal, CastToPrivate, Alignment);
Builder.CreateBr(PhiBB);

Builder.SetInsertPoint(GlobalBB);
Expand All @@ -16895,8 +16911,7 @@ void SITargetLowering::emitExpandAtomicRMW(AtomicRMWInst *AI) const {
if (FullFlatEmulation) {
Value *CastToGlobal = Builder.CreateAddrSpaceCast(
Addr, PointerType::get(Ctx, AMDGPUAS::GLOBAL_ADDRESS));
AI->getOperandUse(AtomicRMWInst::getPointerOperandIndex())
.set(CastToGlobal);
AI->getOperandUse(PtrOpIdx).set(CastToGlobal);
}

AI->removeFromParent();
Expand All @@ -16920,7 +16935,7 @@ void SITargetLowering::emitExpandAtomicRMW(AtomicRMWInst *AI) const {
Builder.SetInsertPoint(PhiBB);

if (ReturnValueIsUsed) {
PHINode *Loaded = Builder.CreatePHI(ValTy, 3);
PHINode *Loaded = Builder.CreatePHI(AI->getType(), 3);
AI->replaceAllUsesWith(Loaded);
if (FullFlatEmulation)
Loaded->addIncoming(LoadedShared, SharedBB);
Expand All @@ -16932,6 +16947,34 @@ void SITargetLowering::emitExpandAtomicRMW(AtomicRMWInst *AI) const {
Builder.CreateBr(ExitBB);
}

void SITargetLowering::emitExpandAtomicRMW(AtomicRMWInst *AI) const {
AtomicRMWInst::BinOp Op = AI->getOperation();

if (Op == AtomicRMWInst::Sub || Op == AtomicRMWInst::Or ||
Op == AtomicRMWInst::Xor) {
if (const auto *ConstVal = dyn_cast<Constant>(AI->getValOperand());
ConstVal && ConstVal->isNullValue()) {
// atomicrmw or %ptr, 0 -> atomicrmw add %ptr, 0
AI->setOperation(AtomicRMWInst::Add);

// We may still need the private-alias-flat handling below.

// TODO: Skip this for cases where we cannot access remote memory.
}
}

// The non-flat expansions should only perform the de-canonicalization of
// identity values.
if (AI->getPointerAddressSpace() != AMDGPUAS::FLAT_ADDRESS)
return;

emitExpandAtomicAddrSpacePredicate(AI);
}

void SITargetLowering::emitExpandAtomicCmpXchg(AtomicCmpXchgInst *CI) const {
emitExpandAtomicAddrSpacePredicate(CI);
}

LoadInst *
SITargetLowering::lowerIdempotentRMWIntoFencedLoad(AtomicRMWInst *AI) const {
IRBuilder<> Builder(AI);
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Target/AMDGPU/SIISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,10 @@ class SITargetLowering final : public AMDGPUTargetLowering {
AtomicExpansionKind shouldExpandAtomicStoreInIR(StoreInst *SI) const override;
AtomicExpansionKind
shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *AI) const override;

void emitExpandAtomicAddrSpacePredicate(Instruction *AI) const;
void emitExpandAtomicRMW(AtomicRMWInst *AI) const override;
void emitExpandAtomicCmpXchg(AtomicCmpXchgInst *CI) const override;

LoadInst *
lowerIdempotentRMWIntoFencedLoad(AtomicRMWInst *AI) const override;
Expand Down
22 changes: 16 additions & 6 deletions llvm/lib/Transforms/Utils/LowerAtomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,30 @@ bool llvm::lowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI) {
Value *Cmp = CXI->getCompareOperand();
Value *Val = CXI->getNewValOperand();

LoadInst *Orig =
Builder.CreateAlignedLoad(Val->getType(), Ptr, CXI->getAlign());
Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
Value *Res = Builder.CreateSelect(Equal, Val, Orig);
Builder.CreateAlignedStore(Res, Ptr, CXI->getAlign());
auto [Orig, Equal] =
buildCmpXchgValue(Builder, Ptr, Cmp, Val, CXI->getAlign());

Res = Builder.CreateInsertValue(PoisonValue::get(CXI->getType()), Orig, 0);
Value *Res =
Builder.CreateInsertValue(PoisonValue::get(CXI->getType()), Orig, 0);
Res = Builder.CreateInsertValue(Res, Equal, 1);

CXI->replaceAllUsesWith(Res);
CXI->eraseFromParent();
return true;
}

std::pair<Value *, Value *> llvm::buildCmpXchgValue(IRBuilderBase &Builder,
Value *Ptr, Value *Cmp,
Value *Val,
Align Alignment) {
LoadInst *Orig = Builder.CreateAlignedLoad(Val->getType(), Ptr, Alignment);
Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
Value *Res = Builder.CreateSelect(Equal, Val, Orig);
Builder.CreateAlignedStore(Res, Ptr, Alignment);

return {Orig, Equal};
}

Value *llvm::buildAtomicRMWValue(AtomicRMWInst::BinOp Op,
IRBuilderBase &Builder, Value *Loaded,
Value *Val) {
Expand Down
Loading
Loading