Skip to content

Commit 79fec2f

Browse files
authored
[AtomicExpand][RISCV] Call shouldExpandAtomicRMWInIR before widenPartwordAtomicRMW (#80947)
This gives the target a chance to keep an atomicrmw op that is smaller than the minimum cmpxchg size. This is needed to support the Zabha extension for RISC-V which provides i8/i16 atomicrmw operations, but does not provide an i8/i16 cmpxchg or LR/SC instructions. This moves the widening until after the target requests LLSC/CmpXChg/MaskedIntrinsic expansion. Once we widen, we call shouldExpandAtomicRMWInIR again to give the target another chance to make a decision about the widened operation. I considered making the targets return AtomicExpansionKind::Expand or a new expansion kind for And/Or/Xor, but that required the targets to special case And/Or/Xor which they weren't currently doing.
1 parent 8c37e3e commit 79fec2f

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

llvm/lib/CodeGen/AtomicExpandPass.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,6 @@ bool AtomicExpand::runOnFunction(Function &F) {
322322
if (isIdempotentRMW(RMWI) && simplifyIdempotentRMW(RMWI)) {
323323
MadeChange = true;
324324
} else {
325-
AtomicRMWInst::BinOp Op = RMWI->getOperation();
326-
unsigned MinCASSize = TLI->getMinCmpXchgSizeInBits() / 8;
327-
unsigned ValueSize = getAtomicOpSize(RMWI);
328-
if (ValueSize < MinCASSize &&
329-
(Op == AtomicRMWInst::Or || Op == AtomicRMWInst::Xor ||
330-
Op == AtomicRMWInst::And)) {
331-
RMWI = widenPartwordAtomicRMW(RMWI);
332-
MadeChange = true;
333-
}
334-
335325
MadeChange |= tryExpandAtomicRMW(RMWI);
336326
}
337327
} else if (CASI)
@@ -607,6 +597,17 @@ bool AtomicExpand::tryExpandAtomicRMW(AtomicRMWInst *AI) {
607597
return true;
608598
}
609599
case TargetLoweringBase::AtomicExpansionKind::MaskedIntrinsic: {
600+
unsigned MinCASSize = TLI->getMinCmpXchgSizeInBits() / 8;
601+
unsigned ValueSize = getAtomicOpSize(AI);
602+
if (ValueSize < MinCASSize) {
603+
AtomicRMWInst::BinOp Op = AI->getOperation();
604+
// Widen And/Or/Xor and give the target another chance at expanding it.
605+
if (Op == AtomicRMWInst::Or || Op == AtomicRMWInst::Xor ||
606+
Op == AtomicRMWInst::And) {
607+
tryExpandAtomicRMW(widenPartwordAtomicRMW(AI));
608+
return true;
609+
}
610+
}
610611
expandAtomicRMWToMaskedIntrinsic(AI);
611612
return true;
612613
}
@@ -845,6 +846,14 @@ static Value *performMaskedAtomicOp(AtomicRMWInst::BinOp Op,
845846
/// part of the value.
846847
void AtomicExpand::expandPartwordAtomicRMW(
847848
AtomicRMWInst *AI, TargetLoweringBase::AtomicExpansionKind ExpansionKind) {
849+
// Widen And/Or/Xor and give the target another chance at expanding it.
850+
AtomicRMWInst::BinOp Op = AI->getOperation();
851+
if (Op == AtomicRMWInst::Or || Op == AtomicRMWInst::Xor ||
852+
Op == AtomicRMWInst::And) {
853+
tryExpandAtomicRMW(widenPartwordAtomicRMW(AI));
854+
return;
855+
}
856+
848857
AtomicOrdering MemOpOrder = AI->getOrdering();
849858
SyncScope::ID SSID = AI->getSyncScopeID();
850859

@@ -855,18 +864,16 @@ void AtomicExpand::expandPartwordAtomicRMW(
855864
AI->getAlign(), TLI->getMinCmpXchgSizeInBits() / 8);
856865

857866
Value *ValOperand_Shifted = nullptr;
858-
if (AI->getOperation() == AtomicRMWInst::Xchg ||
859-
AI->getOperation() == AtomicRMWInst::Add ||
860-
AI->getOperation() == AtomicRMWInst::Sub ||
861-
AI->getOperation() == AtomicRMWInst::Nand) {
867+
if (Op == AtomicRMWInst::Xchg || Op == AtomicRMWInst::Add ||
868+
Op == AtomicRMWInst::Sub || Op == AtomicRMWInst::Nand) {
862869
ValOperand_Shifted =
863870
Builder.CreateShl(Builder.CreateZExt(AI->getValOperand(), PMV.WordType),
864871
PMV.ShiftAmt, "ValOperand_Shifted");
865872
}
866873

867874
auto PerformPartwordOp = [&](IRBuilderBase &Builder, Value *Loaded) {
868-
return performMaskedAtomicOp(AI->getOperation(), Builder, Loaded,
869-
ValOperand_Shifted, AI->getValOperand(), PMV);
875+
return performMaskedAtomicOp(Op, Builder, Loaded, ValOperand_Shifted,
876+
AI->getValOperand(), PMV);
870877
};
871878

872879
Value *OldResult;

0 commit comments

Comments
 (0)