Skip to content

Commit e353db9

Browse files
author
Kane Wang
committed
[RISCV][GlobalISel] Lower G_ATOMICRMW_SUB via G_ATOMICRMW_ADD
RISCV does not provide a native atomic subtract instruction, so this patch lowers G_ATOMICRMW_SUB by negating the RHS value and performing an atomic add. The legalization rules in RISCVLegalizerInfo are updated accordingly, with libcall fallbacks when StdExtA is not available, and intrinsic legalization is extended to support riscv_masked_atomicrmw_sub.
1 parent 19464d9 commit e353db9

13 files changed

+1771
-695
lines changed

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4773,6 +4773,15 @@ LegalizerHelper::lower(MachineInstr &MI, unsigned TypeIdx, LLT LowerHintTy) {
47734773
return lowerVectorReduction(MI);
47744774
case G_VAARG:
47754775
return lowerVAArg(MI);
4776+
case G_ATOMICRMW_SUB: {
4777+
auto [Ret, RetLLT, Mem, MemLLT, Val, ValLLT] = MI.getFirst3RegLLTs();
4778+
MachineMemOperand *MMO = *MI.memoperands_begin();
4779+
4780+
auto VNeg = MIRBuilder.buildNeg(ValLLT, Val);
4781+
MIRBuilder.buildAtomicRMW(G_ATOMICRMW_ADD, Ret, Mem, VNeg, *MMO);
4782+
MI.eraseFromParent();
4783+
return Legalized;
4784+
}
47764785
}
47774786
}
47784787

llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,11 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST)
700700
.libcallFor(!ST.hasStdExtA(), {{s8, p0}, {s16, p0}, {s32, p0}, {s64, p0}})
701701
.clampScalar(0, sXLen, sXLen);
702702

703+
getActionDefinitionsBuilder(G_ATOMICRMW_SUB)
704+
.libcallFor(!ST.hasStdExtA(), {{s8, p0}, {s16, p0}, {s32, p0}, {s64, p0}})
705+
.clampScalar(0, sXLen, sXLen)
706+
.lower();
707+
703708
getLegacyLegalizerInfo().computeTables();
704709
verify(*ST.getInstrInfo());
705710
}
@@ -738,6 +743,7 @@ bool RISCVLegalizerInfo::legalizeIntrinsic(LegalizerHelper &Helper,
738743
return true;
739744
}
740745
case Intrinsic::riscv_masked_atomicrmw_add:
746+
case Intrinsic::riscv_masked_atomicrmw_sub:
741747
return true;
742748
}
743749
}

0 commit comments

Comments
 (0)