Skip to content

Commit 8b9b0fd

Browse files
authored
[CodeGen][TLI] Allow targets to custom expand atomic load/stores (#154708)
Loads didn't have the `Expand` option in `AtomicExpandPass`. Stores had `Expand` but it didn't defer to TLI and instead did an action directly. Add a `CustomExpand` option and make it always map to the TLI hook for all cases. The `Expand` option now refers to a generic expansion for all targets.
1 parent 96b44a1 commit 8b9b0fd

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ class LLVM_ABI TargetLoweringBase {
268268
CmpArithIntrinsic, // Use a target-specific intrinsic for special compare
269269
// operations; used by X86.
270270
Expand, // Generic expansion in terms of other atomic operations.
271+
CustomExpand, // Custom target-specific expansion using TLI hooks.
271272

272273
// Rewrite to a non-atomic form for use in a known non-preemptible
273274
// environment.
@@ -2275,6 +2276,18 @@ class LLVM_ABI TargetLoweringBase {
22752276
"Generic atomicrmw expansion unimplemented on this target");
22762277
}
22772278

2279+
/// Perform a atomic store using a target-specific way.
2280+
virtual void emitExpandAtomicStore(StoreInst *SI) const {
2281+
llvm_unreachable(
2282+
"Generic atomic store expansion unimplemented on this target");
2283+
}
2284+
2285+
/// Perform a atomic load using a target-specific way.
2286+
virtual void emitExpandAtomicLoad(LoadInst *LI) const {
2287+
llvm_unreachable(
2288+
"Generic atomic load expansion unimplemented on this target");
2289+
}
2290+
22782291
/// Perform a cmpxchg expansion using a target-specific method.
22792292
virtual void emitExpandAtomicCmpXchg(AtomicCmpXchgInst *CI) const {
22802293
llvm_unreachable("Generic cmpxchg expansion unimplemented on this target");
@@ -2379,8 +2392,8 @@ class LLVM_ABI TargetLoweringBase {
23792392
}
23802393

23812394
/// Returns how the given (atomic) store should be expanded by the IR-level
2382-
/// AtomicExpand pass into. For instance AtomicExpansionKind::Expand will try
2383-
/// to use an atomicrmw xchg.
2395+
/// AtomicExpand pass into. For instance AtomicExpansionKind::CustomExpand
2396+
/// will try to use an atomicrmw xchg.
23842397
virtual AtomicExpansionKind shouldExpandAtomicStoreInIR(StoreInst *SI) const {
23852398
return AtomicExpansionKind::None;
23862399
}

llvm/lib/CodeGen/AtomicExpandPass.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class AtomicExpandImpl {
8484
bool expandAtomicLoadToCmpXchg(LoadInst *LI);
8585
StoreInst *convertAtomicStoreToIntegerType(StoreInst *SI);
8686
bool tryExpandAtomicStore(StoreInst *SI);
87-
void expandAtomicStore(StoreInst *SI);
87+
void expandAtomicStoreToXChg(StoreInst *SI);
8888
bool tryExpandAtomicRMW(AtomicRMWInst *AI);
8989
AtomicRMWInst *convertAtomicXchgToIntegerType(AtomicRMWInst *RMWI);
9090
Value *
@@ -537,6 +537,9 @@ bool AtomicExpandImpl::tryExpandAtomicLoad(LoadInst *LI) {
537537
case TargetLoweringBase::AtomicExpansionKind::NotAtomic:
538538
LI->setAtomic(AtomicOrdering::NotAtomic);
539539
return true;
540+
case TargetLoweringBase::AtomicExpansionKind::CustomExpand:
541+
TLI->emitExpandAtomicLoad(LI);
542+
return true;
540543
default:
541544
llvm_unreachable("Unhandled case in tryExpandAtomicLoad");
542545
}
@@ -546,8 +549,11 @@ bool AtomicExpandImpl::tryExpandAtomicStore(StoreInst *SI) {
546549
switch (TLI->shouldExpandAtomicStoreInIR(SI)) {
547550
case TargetLoweringBase::AtomicExpansionKind::None:
548551
return false;
552+
case TargetLoweringBase::AtomicExpansionKind::CustomExpand:
553+
TLI->emitExpandAtomicStore(SI);
554+
return true;
549555
case TargetLoweringBase::AtomicExpansionKind::Expand:
550-
expandAtomicStore(SI);
556+
expandAtomicStoreToXChg(SI);
551557
return true;
552558
case TargetLoweringBase::AtomicExpansionKind::NotAtomic:
553559
SI->setAtomic(AtomicOrdering::NotAtomic);
@@ -620,7 +626,7 @@ StoreInst *AtomicExpandImpl::convertAtomicStoreToIntegerType(StoreInst *SI) {
620626
return NewSI;
621627
}
622628

623-
void AtomicExpandImpl::expandAtomicStore(StoreInst *SI) {
629+
void AtomicExpandImpl::expandAtomicStoreToXChg(StoreInst *SI) {
624630
// This function is only called on atomic stores that are too large to be
625631
// atomic if implemented as a native store. So we replace them by an
626632
// atomic swap, that can be implemented for example as a ldrex/strex on ARM
@@ -741,7 +747,7 @@ bool AtomicExpandImpl::tryExpandAtomicRMW(AtomicRMWInst *AI) {
741747
}
742748
case TargetLoweringBase::AtomicExpansionKind::NotAtomic:
743749
return lowerAtomicRMWInst(AI);
744-
case TargetLoweringBase::AtomicExpansionKind::Expand:
750+
case TargetLoweringBase::AtomicExpansionKind::CustomExpand:
745751
TLI->emitExpandAtomicRMW(AI);
746752
return true;
747753
default:
@@ -1695,7 +1701,7 @@ bool AtomicExpandImpl::tryExpandAtomicCmpXchg(AtomicCmpXchgInst *CI) {
16951701
return true;
16961702
case TargetLoweringBase::AtomicExpansionKind::NotAtomic:
16971703
return lowerAtomicCmpXchgInst(CI);
1698-
case TargetLoweringBase::AtomicExpansionKind::Expand: {
1704+
case TargetLoweringBase::AtomicExpansionKind::CustomExpand: {
16991705
TLI->emitExpandAtomicCmpXchg(CI);
17001706
return true;
17011707
}

llvm/lib/Target/AMDGPU/SIISelLowering.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17823,7 +17823,7 @@ SITargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const {
1782317823
if (AS == AMDGPUAS::FLAT_ADDRESS &&
1782417824
DL.getTypeSizeInBits(RMW->getType()) == 64 &&
1782517825
flatInstrMayAccessPrivate(RMW))
17826-
return AtomicExpansionKind::Expand;
17826+
return AtomicExpansionKind::CustomExpand;
1782717827

1782817828
auto ReportUnsafeHWInst = [=](TargetLowering::AtomicExpansionKind Kind) {
1782917829
OptimizationRemarkEmitter ORE(RMW->getFunction());
@@ -17898,7 +17898,7 @@ SITargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const {
1789817898
// does. InstCombine transforms these with 0 to or, so undo that.
1789917899
if (Constant *ConstVal = dyn_cast<Constant>(RMW->getValOperand());
1790017900
ConstVal && ConstVal->isNullValue())
17901-
return AtomicExpansionKind::Expand;
17901+
return AtomicExpansionKind::CustomExpand;
1790217902
}
1790317903

1790417904
// If the allocation could be in remote, fine-grained memory, the rmw
@@ -18027,9 +18027,9 @@ SITargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const {
1802718027
// fadd.
1802818028
if (Subtarget->hasLDSFPAtomicAddF32()) {
1802918029
if (RMW->use_empty() && Subtarget->hasAtomicFaddNoRtnInsts())
18030-
return AtomicExpansionKind::Expand;
18030+
return AtomicExpansionKind::CustomExpand;
1803118031
if (!RMW->use_empty() && Subtarget->hasAtomicFaddRtnInsts())
18032-
return AtomicExpansionKind::Expand;
18032+
return AtomicExpansionKind::CustomExpand;
1803318033
}
1803418034
}
1803518035
}
@@ -18109,7 +18109,7 @@ SITargetLowering::shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *CmpX) const {
1810918109

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

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7949,7 +7949,7 @@ LoongArchTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
79497949
if (Size < 32 && (AI->getOperation() == AtomicRMWInst::And ||
79507950
AI->getOperation() == AtomicRMWInst::Or ||
79517951
AI->getOperation() == AtomicRMWInst::Xor))
7952-
return AtomicExpansionKind::Expand;
7952+
return AtomicExpansionKind::CustomExpand;
79537953
if (AI->getOperation() == AtomicRMWInst::Nand || Size < 32)
79547954
return AtomicExpansionKind::CmpXChg;
79557955
}

0 commit comments

Comments
 (0)