@@ -16054,26 +16054,21 @@ bool SITargetLowering::isKnownNeverNaNForTargetNode(SDValue Op,
1605416054 SNaN, Depth);
1605516055}
1605616056
16057- #if 0
16058- // FIXME: This should be checked before unsafe fp atomics are enabled
16059- // Global FP atomic instructions have a hardcoded FP mode and do not support
16060- // FP32 denormals, and only support v2f16 denormals.
16061- static bool fpModeMatchesGlobalFPAtomicMode(const AtomicRMWInst *RMW) {
16057+ // On older subtargets, global FP atomic instructions have a hardcoded FP mode
16058+ // and do not support FP32 denormals, and only support v2f16/f64 denormals.
16059+ static bool atomicIgnoresDenormalModeOrFPModeIsFTZ(const AtomicRMWInst *RMW) {
16060+ if (RMW->hasMetadata("amdgpu.ignore.denormal.mode"))
16061+ return true;
16062+
1606216063 const fltSemantics &Flt = RMW->getType()->getScalarType()->getFltSemantics();
16063- auto DenormMode = RMW->getParent()->getParent()->getDenormalMode(Flt);
16064- if (&Flt == &APFloat::IEEEsingle())
16065- return DenormMode == DenormalMode::getPreserveSign();
16066- return DenormMode == DenormalMode::getIEEE();
16067- }
16068- #endif
16064+ auto DenormMode = RMW->getFunction()->getDenormalMode(Flt);
16065+ if (DenormMode == DenormalMode::getPreserveSign())
16066+ return true;
1606916067
16070- // The amdgpu-unsafe-fp-atomics attribute enables generation of unsafe
16071- // floating point atomic instructions. May generate more efficient code,
16072- // but may not respect rounding and denormal modes, and may give incorrect
16073- // results for certain memory destinations.
16074- bool unsafeFPAtomicsDisabled(Function *F) {
16075- return F->getFnAttribute("amdgpu-unsafe-fp-atomics").getValueAsString() !=
16076- "true";
16068+ // TODO: Remove this.
16069+ return RMW->getFunction()
16070+ ->getFnAttribute("amdgpu-unsafe-fp-atomics")
16071+ .getValueAsBool();
1607716072}
1607816073
1607916074static OptimizationRemark emitAtomicRMWLegalRemark(const AtomicRMWInst *RMW) {
@@ -16202,75 +16197,76 @@ SITargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const {
1620216197 return AtomicExpansionKind::CmpXChg;
1620316198 }
1620416199
16205- if (!AMDGPU::isFlatGlobalAddrSpace(AS) &&
16206- AS != AMDGPUAS::BUFFER_FAT_POINTER)
16207- return AtomicExpansionKind::CmpXChg;
16208-
16209- if (Subtarget->hasGFX940Insts() && (Ty->isFloatTy() || Ty->isDoubleTy()))
16210- return AtomicExpansionKind::None;
16211-
16212- if (AS == AMDGPUAS::FLAT_ADDRESS) {
16213- // gfx940, gfx12
16214- // FIXME: Needs to account for no fine-grained memory
16215- if (Subtarget->hasAtomicFlatPkAdd16Insts() && isHalf2OrBFloat2(Ty))
16216- return AtomicExpansionKind::None;
16217- } else if (AMDGPU::isExtendedGlobalAddrSpace(AS)) {
16218- // gfx90a, gfx940, gfx12
16219- // FIXME: Needs to account for no fine-grained memory
16220- if (Subtarget->hasAtomicBufferGlobalPkAddF16Insts() && isHalf2(Ty))
16221- return AtomicExpansionKind::None;
16222-
16223- // gfx940, gfx12
16224- // FIXME: Needs to account for no fine-grained memory
16225- if (Subtarget->hasAtomicGlobalPkAddBF16Inst() && isBFloat2(Ty))
16226- return AtomicExpansionKind::None;
16227- } else if (AS == AMDGPUAS::BUFFER_FAT_POINTER) {
16228- // gfx90a, gfx940, gfx12
16229- // FIXME: Needs to account for no fine-grained memory
16230- if (Subtarget->hasAtomicBufferGlobalPkAddF16Insts() && isHalf2(Ty))
16231- return AtomicExpansionKind::None;
16232-
16233- // While gfx90a/gfx940 supports v2bf16 for global/flat, it does not for
16234- // buffer. gfx12 does have the buffer version.
16235- if (Subtarget->hasAtomicBufferPkAddBF16Inst() && isBFloat2(Ty))
16236- return AtomicExpansionKind::None;
16237- }
16238-
16239- if (unsafeFPAtomicsDisabled(RMW->getFunction()))
16200+ // LDS atomics respect the denormal mode from the mode register.
16201+ //
16202+ // Traditionally f32 global/buffer memory atomics would unconditionally
16203+ // flush denormals, but newer targets do not flush. f64/f16/bf16 cases never
16204+ // flush.
16205+ //
16206+ // On targets with flat atomic fadd, denormals would flush depending on
16207+ // whether the target address resides in LDS or global memory. We consider
16208+ // this flat-maybe-flush as will-flush.
16209+ if (Ty->isFloatTy() &&
16210+ !Subtarget->hasMemoryAtomicFaddF32DenormalSupport() &&
16211+ !atomicIgnoresDenormalModeOrFPModeIsFTZ(RMW))
1624016212 return AtomicExpansionKind::CmpXChg;
1624116213
16242- // Always expand system scope fp atomics.
16243- if (HasSystemScope)
16244- return AtomicExpansionKind::CmpXChg;
16214+ // FIXME: These ReportUnsafeHWInsts are imprecise. Some of these cases are
16215+ // safe. The message phrasing also should be better.
16216+ if (globalMemoryFPAtomicIsLegal(*Subtarget, RMW, HasSystemScope)) {
16217+ if (AS == AMDGPUAS::FLAT_ADDRESS) {
16218+ // gfx940, gfx12
16219+ if (Subtarget->hasAtomicFlatPkAdd16Insts() && isHalf2OrBFloat2(Ty))
16220+ return ReportUnsafeHWInst(AtomicExpansionKind::None);
16221+ } else if (AMDGPU::isExtendedGlobalAddrSpace(AS)) {
16222+ // gfx90a, gfx940, gfx12
16223+ if (Subtarget->hasAtomicBufferGlobalPkAddF16Insts() && isHalf2(Ty))
16224+ return ReportUnsafeHWInst(AtomicExpansionKind::None);
1624516225
16246- // global and flat atomic fadd f64: gfx90a, gfx940.
16247- if (Subtarget->hasFlatBufferGlobalAtomicFaddF64Inst() && Ty->isDoubleTy())
16248- return ReportUnsafeHWInst(AtomicExpansionKind::None);
16226+ // gfx940, gfx12
16227+ if (Subtarget->hasAtomicGlobalPkAddBF16Inst() && isBFloat2(Ty))
16228+ return ReportUnsafeHWInst(AtomicExpansionKind::None);
16229+ } else if (AS == AMDGPUAS::BUFFER_FAT_POINTER) {
16230+ // gfx90a, gfx940, gfx12
16231+ if (Subtarget->hasAtomicBufferGlobalPkAddF16Insts() && isHalf2(Ty))
16232+ return ReportUnsafeHWInst(AtomicExpansionKind::None);
1624916233
16250- if (AS != AMDGPUAS::FLAT_ADDRESS && Ty->isFloatTy()) {
16251- // global/buffer atomic fadd f32 no-rtn: gfx908, gfx90a, gfx940, gfx11+.
16252- if (RMW->use_empty() && Subtarget->hasAtomicFaddNoRtnInsts())
16253- return ReportUnsafeHWInst(AtomicExpansionKind::None);
16254- // global/buffer atomic fadd f32 rtn: gfx90a, gfx940, gfx11+.
16255- if (!RMW->use_empty() && Subtarget->hasAtomicFaddRtnInsts())
16256- return ReportUnsafeHWInst(AtomicExpansionKind::None);
16257- }
16234+ // While gfx90a/gfx940 supports v2bf16 for global/flat, it does not for
16235+ // buffer. gfx12 does have the buffer version.
16236+ if (Subtarget->hasAtomicBufferPkAddBF16Inst() && isBFloat2(Ty))
16237+ return ReportUnsafeHWInst(AtomicExpansionKind::None);
16238+ }
1625816239
16259- // flat atomic fadd f32: gfx940, gfx11+.
16260- if (AS == AMDGPUAS::FLAT_ADDRESS && Ty->isFloatTy()) {
16261- if (Subtarget->hasFlatAtomicFaddF32Inst())
16240+ // global and flat atomic fadd f64: gfx90a, gfx940.
16241+ if (Subtarget->hasFlatBufferGlobalAtomicFaddF64Inst() && Ty->isDoubleTy())
1626216242 return ReportUnsafeHWInst(AtomicExpansionKind::None);
1626316243
16264- // If it is in flat address space, and the type is float, we will try to
16265- // expand it, if the target supports global and lds atomic fadd. The
16266- // reason we need that is, in the expansion, we emit the check of address
16267- // space. If it is in global address space, we emit the global atomic
16268- // fadd; if it is in shared address space, we emit the LDS atomic fadd.
16269- if (Subtarget->hasLDSFPAtomicAddF32()) {
16244+ if (AS != AMDGPUAS::FLAT_ADDRESS && Ty->isFloatTy()) {
16245+ // global/buffer atomic fadd f32 no-rtn: gfx908, gfx90a, gfx940, gfx11+.
1627016246 if (RMW->use_empty() && Subtarget->hasAtomicFaddNoRtnInsts())
16271- return AtomicExpansionKind::Expand;
16247+ return ReportUnsafeHWInst(AtomicExpansionKind::None);
16248+ // global/buffer atomic fadd f32 rtn: gfx90a, gfx940, gfx11+.
1627216249 if (!RMW->use_empty() && Subtarget->hasAtomicFaddRtnInsts())
16273- return AtomicExpansionKind::Expand;
16250+ return ReportUnsafeHWInst(AtomicExpansionKind::None);
16251+ }
16252+
16253+ // flat atomic fadd f32: gfx940, gfx11+.
16254+ if (AS == AMDGPUAS::FLAT_ADDRESS && Ty->isFloatTy()) {
16255+ if (Subtarget->hasFlatAtomicFaddF32Inst())
16256+ return ReportUnsafeHWInst(AtomicExpansionKind::None);
16257+
16258+ // If it is in flat address space, and the type is float, we will try to
16259+ // expand it, if the target supports global and lds atomic fadd. The
16260+ // reason we need that is, in the expansion, we emit the check of
16261+ // address space. If it is in global address space, we emit the global
16262+ // atomic fadd; if it is in shared address space, we emit the LDS atomic
16263+ // fadd.
16264+ if (Subtarget->hasLDSFPAtomicAddF32()) {
16265+ if (RMW->use_empty() && Subtarget->hasAtomicFaddNoRtnInsts())
16266+ return AtomicExpansionKind::Expand;
16267+ if (!RMW->use_empty() && Subtarget->hasAtomicFaddRtnInsts())
16268+ return AtomicExpansionKind::Expand;
16269+ }
1627416270 }
1627516271 }
1627616272
0 commit comments