@@ -16110,6 +16110,34 @@ static bool isBFloat2(Type *Ty) {
16110
16110
return VT && VT->getNumElements() == 2 && VT->getElementType()->isBFloatTy();
16111
16111
}
16112
16112
16113
+ /// \returns true if it's valid to emit a native instruction for \p RMW, based
16114
+ /// on the properties of the target memory.
16115
+ static bool globalMemoryFPAtomicIsLegal(const GCNSubtarget &Subtarget,
16116
+ const AtomicRMWInst *RMW,
16117
+ bool HasSystemScope) {
16118
+ // The remote/fine-grained access logic is different from the integer
16119
+ // atomics. Without AgentScopeFineGrainedRemoteMemoryAtomics support,
16120
+ // fine-grained access does not work, even for a device local allocation.
16121
+ //
16122
+ // With AgentScopeFineGrainedRemoteMemoryAtomics, system scoped device local
16123
+ // allocations work.
16124
+ if (HasSystemScope) {
16125
+ if (Subtarget.supportsAgentScopeFineGrainedRemoteMemoryAtomics() &&
16126
+ RMW->hasMetadata("amdgpu.no.remote.memory"))
16127
+ return true;
16128
+ } else if (Subtarget.supportsAgentScopeFineGrainedRemoteMemoryAtomics())
16129
+ return true;
16130
+
16131
+ if (RMW->hasMetadata("amdgpu.no.fine.grained.memory"))
16132
+ return true;
16133
+
16134
+ // TODO: Auto-upgrade this attribute to the metadata in function body and stop
16135
+ // checking it.
16136
+ return RMW->getFunction()
16137
+ ->getFnAttribute("amdgpu-unsafe-fp-atomics")
16138
+ .getValueAsBool();
16139
+ }
16140
+
16113
16141
TargetLowering::AtomicExpansionKind
16114
16142
SITargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const {
16115
16143
unsigned AS = RMW->getPointerAddressSpace();
@@ -16260,37 +16288,32 @@ SITargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const {
16260
16288
Type *Ty = RMW->getType();
16261
16289
16262
16290
// LDS float and double fmin/fmax were always supported.
16263
- if (AS == AMDGPUAS::LOCAL_ADDRESS && (Ty->isFloatTy() || Ty->isDoubleTy()))
16264
- return AtomicExpansionKind::None;
16265
-
16266
- if (unsafeFPAtomicsDisabled(RMW->getFunction()))
16267
- return AtomicExpansionKind::CmpXChg;
16268
-
16269
- // Always expand system scope fp atomics.
16270
- if (HasSystemScope)
16271
- return AtomicExpansionKind::CmpXChg;
16291
+ if (AS == AMDGPUAS::LOCAL_ADDRESS) {
16292
+ return Ty->isFloatTy() || Ty->isDoubleTy() ? AtomicExpansionKind::None
16293
+ : AtomicExpansionKind::CmpXChg;
16294
+ }
16272
16295
16273
- // For flat and global cases:
16274
- // float, double in gfx7. Manual claims denormal support.
16275
- // Removed in gfx8 .
16276
- // float, double restored in gfx10 .
16277
- // double removed again in gfx11, so only f32 for gfx11/gfx12 .
16278
- //
16279
- // For gfx9, gfx90a and gfx940 support f64 for global (same as fadd), but no
16280
- // f32.
16281
- //
16282
- // FIXME: Check scope and fine grained memory
16283
- if (AS == AMDGPUAS::FLAT_ADDRESS) {
16284
- if (Subtarget->hasAtomicFMinFMaxF32FlatInsts() && Ty->isFloatTy())
16285
- return ReportUnsafeHWInst(AtomicExpansionKind::None);
16286
- if (Subtarget->hasAtomicFMinFMaxF64FlatInsts() && Ty->isDoubleTy())
16287
- return ReportUnsafeHWInst(AtomicExpansionKind::None);
16288
- } else if (AMDGPU::isExtendedGlobalAddrSpace(AS) ||
16289
- AS == AMDGPUAS::BUFFER_FAT_POINTER) {
16290
- if (Subtarget->hasAtomicFMinFMaxF32GlobalInsts() && Ty->isFloatTy())
16291
- return ReportUnsafeHWInst(AtomicExpansionKind::None);
16292
- if (Subtarget->hasAtomicFMinFMaxF64GlobalInsts() && Ty->isDoubleTy())
16293
- return ReportUnsafeHWInst(AtomicExpansionKind::None);
16296
+ if (globalMemoryFPAtomicIsLegal(*Subtarget, RMW, HasSystemScope)) {
16297
+ // For flat and global cases:
16298
+ // float, double in gfx7. Manual claims denormal support .
16299
+ // Removed in gfx8 .
16300
+ // float, double restored in gfx10 .
16301
+ // double removed again in gfx11, so only f32 for gfx11/gfx12.
16302
+ //
16303
+ // For gfx9, gfx90a and gfx940 support f64 for global (same as fadd), but
16304
+ // no f32.
16305
+ if (AS == AMDGPUAS::FLAT_ADDRESS) {
16306
+ if (Subtarget->hasAtomicFMinFMaxF32FlatInsts() && Ty->isFloatTy())
16307
+ return ReportUnsafeHWInst(AtomicExpansionKind::None);
16308
+ if (Subtarget->hasAtomicFMinFMaxF64FlatInsts() && Ty->isDoubleTy())
16309
+ return ReportUnsafeHWInst(AtomicExpansionKind::None);
16310
+ } else if (AMDGPU::isExtendedGlobalAddrSpace(AS) ||
16311
+ AS == AMDGPUAS::BUFFER_FAT_POINTER) {
16312
+ if (Subtarget->hasAtomicFMinFMaxF32GlobalInsts() && Ty->isFloatTy())
16313
+ return ReportUnsafeHWInst(AtomicExpansionKind::None);
16314
+ if (Subtarget->hasAtomicFMinFMaxF64GlobalInsts() && Ty->isDoubleTy())
16315
+ return ReportUnsafeHWInst(AtomicExpansionKind::None);
16316
+ }
16294
16317
}
16295
16318
16296
16319
return AtomicExpansionKind::CmpXChg;
0 commit comments