[LLVM] Add a check if should cast atomic operations to integer type

Currently for atomic load, store, and rmw instructions, as long as the
operand is floating-point value, they are casted to integer. Nowadays many
targets can actually support part of atomic operations with floating-point
operands. For example, NVPTX supports atomic load and store of floating-point
values. This patch adds a series interface functions `shouldCastAtomicXXXInIR`,
and the default implementations are same as what we currently do. Later for
targets can have their specialization.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D125652
This commit is contained in:
Shilei Tian 2022-05-20 17:23:34 -04:00
parent de06626725
commit ff60a0a364
2 changed files with 36 additions and 4 deletions

View File

@ -248,6 +248,8 @@ public:
/// w.r.t. what they should expand to.
enum class AtomicExpansionKind {
None, // Don't expand the instruction.
CastToInteger, // Cast the atomic instruction to another type, e.g. from
// floating-point to integer type.
LLSC, // Expand the instruction into loadlinked/storeconditional; used
// by ARM/AArch64.
LLOnly, // Expand the (load) instruction into just a load-linked, which has
@ -2045,6 +2047,14 @@ public:
return AtomicExpansionKind::None;
}
/// Returns how the given (atomic) load should be cast by the IR-level
/// AtomicExpand pass.
virtual AtomicExpansionKind shouldCastAtomicLoadInIR(LoadInst *LI) const {
if (LI->getType()->isFloatingPointTy())
return AtomicExpansionKind::CastToInteger;
return AtomicExpansionKind::None;
}
/// Returns how the given (atomic) store should be expanded by the IR-level
/// AtomicExpand pass into. For instance AtomicExpansionKind::Expand will try
/// to use an atomicrmw xchg.
@ -2052,6 +2062,15 @@ public:
return AtomicExpansionKind::None;
}
/// Returns how the given (atomic) store should be cast by the IR-level
/// AtomicExpand pass into. For instance AtomicExpansionKind::CastToInteger
/// will try to cast the operands to integer values.
virtual AtomicExpansionKind shouldCastAtomicStoreInIR(StoreInst *SI) const {
if (SI->getValueOperand()->getType()->isFloatingPointTy())
return AtomicExpansionKind::CastToInteger;
return AtomicExpansionKind::None;
}
/// Returns how the given atomic cmpxchg should be expanded by the IR-level
/// AtomicExpand pass.
virtual AtomicExpansionKind
@ -2066,6 +2085,17 @@ public:
AtomicExpansionKind::CmpXChg : AtomicExpansionKind::None;
}
/// Returns how the given atomic atomicrmw should be cast by the IR-level
/// AtomicExpand pass.
virtual AtomicExpansionKind
shouldCastAtomicRMWIInIR(AtomicRMWInst *RMWI) const {
if (RMWI->getOperation() == AtomicRMWInst::Xchg &&
RMWI->getValOperand()->getType()->isFloatingPointTy())
return AtomicExpansionKind::CastToInteger;
return AtomicExpansionKind::None;
}
/// On some platforms, an AtomicRMW that never actually modifies the value
/// (such as fetch_add of 0) can be turned into a fence followed by an
/// atomic load. This may sound useless, but it makes it possible for the

View File

@ -254,7 +254,8 @@ bool AtomicExpand::runOnFunction(Function &F) {
}
if (LI) {
if (LI->getType()->isFloatingPointTy()) {
if (TLI->shouldCastAtomicLoadInIR(LI) ==
TargetLoweringBase::AtomicExpansionKind::CastToInteger) {
// TODO: add a TLI hook to control this so that each target can
// convert to lowering the original type one at a time.
LI = convertAtomicLoadToIntegerType(LI);
@ -264,7 +265,8 @@ bool AtomicExpand::runOnFunction(Function &F) {
MadeChange |= tryExpandAtomicLoad(LI);
} else if (SI) {
if (SI->getValueOperand()->getType()->isFloatingPointTy()) {
if (TLI->shouldCastAtomicStoreInIR(SI) ==
TargetLoweringBase::AtomicExpansionKind::CastToInteger) {
// TODO: add a TLI hook to control this so that each target can
// convert to lowering the original type one at a time.
SI = convertAtomicStoreToIntegerType(SI);
@ -285,8 +287,8 @@ bool AtomicExpand::runOnFunction(Function &F) {
MadeChange = true;
} else {
AtomicRMWInst::BinOp Op = RMWI->getOperation();
if (Op == AtomicRMWInst::Xchg &&
RMWI->getValOperand()->getType()->isFloatingPointTy()) {
if (TLI->shouldCastAtomicRMWIInIR(RMWI) ==
TargetLoweringBase::AtomicExpansionKind::CastToInteger) {
// TODO: add a TLI hook to control this so that each target can
// convert to lowering the original type one at a time.
RMWI = convertAtomicXchgToIntegerType(RMWI);