-
Notifications
You must be signed in to change notification settings - Fork 15.2k
AMDGPU: Refactor atomicrmw fadd expansion logic (NFC) #89469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
arsenm
merged 3 commits into
llvm:main
from
arsenm:amdgpu-refactor-atomicrmw-fadd-expand
Apr 22, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15977,6 +15977,8 @@ bool SITargetLowering::isKnownNeverNaNForTargetNode(SDValue Op, | |
| SNaN, Depth); | ||
| } | ||
|
|
||
| #if 0 | ||
| // FIXME: This should be checked before unsafe fp atomics are enabled | ||
| // Global FP atomic instructions have a hardcoded FP mode and do not support | ||
| // FP32 denormals, and only support v2f16 denormals. | ||
| static bool fpModeMatchesGlobalFPAtomicMode(const AtomicRMWInst *RMW) { | ||
|
|
@@ -15986,6 +15988,7 @@ static bool fpModeMatchesGlobalFPAtomicMode(const AtomicRMWInst *RMW) { | |
| return DenormMode == DenormalMode::getPreserveSign(); | ||
| return DenormMode == DenormalMode::getIEEE(); | ||
| } | ||
| #endif | ||
|
|
||
| // The amdgpu-unsafe-fp-atomics attribute enables generation of unsafe | ||
| // floating point atomic instructions. May generate more efficient code, | ||
|
|
@@ -16046,76 +16049,76 @@ SITargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const { | |
| case AtomicRMWInst::FAdd: { | ||
| Type *Ty = RMW->getType(); | ||
|
|
||
| if (Ty->isHalfTy()) | ||
| // TODO: Handle REGION_ADDRESS | ||
| if (AS == AMDGPUAS::LOCAL_ADDRESS) { | ||
| // DS F32 FP atomics do respect the denormal mode, but the rounding mode | ||
| // is fixed to round-to-nearest-even. | ||
| // | ||
| // F64 / PK_F16 / PK_BF16 never flush and are also fixed to | ||
| // round-to-nearest-even. | ||
| // | ||
| // We ignore the rounding mode problem, even in strictfp. The C++ standard | ||
| // suggests it is OK if the floating-point mode may not match the calling | ||
| // thread. | ||
| if (Ty->isFloatTy()) { | ||
| return Subtarget->hasLDSFPAtomicAddF32() ? AtomicExpansionKind::None | ||
| : AtomicExpansionKind::CmpXChg; | ||
| } | ||
|
|
||
| if (Ty->isDoubleTy()) { | ||
| // Ignores denormal mode, but we don't consider flushing mandatory. | ||
| return Subtarget->hasLDSFPAtomicAddF64() ? AtomicExpansionKind::None | ||
| : AtomicExpansionKind::CmpXChg; | ||
| } | ||
|
|
||
| // TODO: Handle v2f16/v2bf16 cases for gfx940 | ||
| return AtomicExpansionKind::CmpXChg; | ||
| } | ||
|
|
||
| if (!Ty->isFloatTy() && (!Subtarget->hasGFX90AInsts() || !Ty->isDoubleTy())) | ||
| if (!AMDGPU::isFlatGlobalAddrSpace(AS) && | ||
| AS != AMDGPUAS::BUFFER_FAT_POINTER) | ||
| return AtomicExpansionKind::CmpXChg; | ||
|
|
||
| if ((AMDGPU::isFlatGlobalAddrSpace(AS) || | ||
| AS == AMDGPUAS::BUFFER_FAT_POINTER) && | ||
| Subtarget->hasAtomicFaddNoRtnInsts()) { | ||
| if (Subtarget->hasGFX940Insts()) | ||
| return AtomicExpansionKind::None; | ||
| // TODO: gfx940 supports v2f16 and v2bf16 | ||
| if (Subtarget->hasGFX940Insts() && (Ty->isFloatTy() || Ty->isDoubleTy())) | ||
| return AtomicExpansionKind::None; | ||
|
|
||
| if (unsafeFPAtomicsDisabled(RMW->getFunction())) | ||
| return AtomicExpansionKind::CmpXChg; | ||
| if (unsafeFPAtomicsDisabled(RMW->getFunction())) | ||
| return AtomicExpansionKind::CmpXChg; | ||
|
|
||
| // Always expand system scope fp atomics. | ||
| if (HasSystemScope) | ||
| return AtomicExpansionKind::CmpXChg; | ||
| // Always expand system scope fp atomics. | ||
| if (HasSystemScope) | ||
| return AtomicExpansionKind::CmpXChg; | ||
|
|
||
| if ((AMDGPU::isExtendedGlobalAddrSpace(AS) || | ||
| AS == AMDGPUAS::BUFFER_FAT_POINTER) && | ||
| Ty->isFloatTy()) { | ||
| // global/buffer atomic fadd f32 no-rtn: gfx908, gfx90a, gfx940, gfx11+. | ||
| if (RMW->use_empty() && Subtarget->hasAtomicFaddNoRtnInsts()) | ||
| return ReportUnsafeHWInst(AtomicExpansionKind::None); | ||
| // global/buffer atomic fadd f32 rtn: gfx90a, gfx940, gfx11+. | ||
| if (!RMW->use_empty() && Subtarget->hasAtomicFaddRtnInsts()) | ||
| return ReportUnsafeHWInst(AtomicExpansionKind::None); | ||
| } | ||
| // global and flat atomic fadd f64: gfx90a, gfx940. | ||
| if (Subtarget->hasGFX90AInsts() && Ty->isDoubleTy()) | ||
| return ReportUnsafeHWInst(AtomicExpansionKind::None); | ||
|
|
||
| // flat atomic fadd f32: gfx940, gfx11+. | ||
| if (AS == AMDGPUAS::FLAT_ADDRESS && Ty->isFloatTy() && | ||
| Subtarget->hasFlatAtomicFaddF32Inst()) | ||
| if (AS != AMDGPUAS::FLAT_ADDRESS && Ty->isFloatTy()) { | ||
| // global/buffer atomic fadd f32 no-rtn: gfx908, gfx90a, gfx940, gfx11+. | ||
| if (RMW->use_empty() && Subtarget->hasAtomicFaddNoRtnInsts()) | ||
| return ReportUnsafeHWInst(AtomicExpansionKind::None); | ||
| // global/buffer atomic fadd f32 rtn: gfx90a, gfx940, gfx11+. | ||
| if (!RMW->use_empty() && Subtarget->hasAtomicFaddRtnInsts()) | ||
| return ReportUnsafeHWInst(AtomicExpansionKind::None); | ||
| } | ||
|
|
||
| // global and flat atomic fadd f64: gfx90a, gfx940. | ||
| if (Ty->isDoubleTy() && Subtarget->hasGFX90AInsts()) | ||
| // flat atomic fadd f32: gfx940, gfx11+. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment seems to be wrong, gfx940 shall be already handled by this point (where the check for hasGFX940Insts is). |
||
| if (AS == AMDGPUAS::FLAT_ADDRESS && Ty->isFloatTy()) { | ||
| if (Subtarget->hasFlatAtomicFaddF32Inst()) | ||
| return ReportUnsafeHWInst(AtomicExpansionKind::None); | ||
|
|
||
| // If it is in flat address space, and the type is float, we will try to | ||
| // expand it, if the target supports global and lds atomic fadd. The | ||
| // reason we need that is, in the expansion, we emit the check of address | ||
| // space. If it is in global address space, we emit the global atomic | ||
| // fadd; if it is in shared address space, we emit the LDS atomic fadd. | ||
| if (AS == AMDGPUAS::FLAT_ADDRESS && Ty->isFloatTy() && | ||
| Subtarget->hasLDSFPAtomicAdd()) { | ||
| if (Subtarget->hasLDSFPAtomicAddF32()) { | ||
| if (RMW->use_empty() && Subtarget->hasAtomicFaddNoRtnInsts()) | ||
| return AtomicExpansionKind::Expand; | ||
| if (!RMW->use_empty() && Subtarget->hasAtomicFaddRtnInsts()) | ||
| return AtomicExpansionKind::Expand; | ||
| } | ||
|
|
||
| return AtomicExpansionKind::CmpXChg; | ||
| } | ||
|
|
||
| // DS FP atomics do respect the denormal mode, but the rounding mode is | ||
| // fixed to round-to-nearest-even. | ||
| // The only exception is DS_ADD_F64 which never flushes regardless of mode. | ||
| if (AS == AMDGPUAS::LOCAL_ADDRESS && Subtarget->hasLDSFPAtomicAdd()) { | ||
| if (!Ty->isDoubleTy()) | ||
| return AtomicExpansionKind::None; | ||
|
|
||
| if (fpModeMatchesGlobalFPAtomicMode(RMW)) | ||
| return AtomicExpansionKind::None; | ||
|
|
||
| return RMW->getFunction() | ||
| ->getFnAttribute("amdgpu-unsafe-fp-atomics") | ||
| .getValueAsString() == "true" | ||
| ? ReportUnsafeHWInst(AtomicExpansionKind::None) | ||
| : AtomicExpansionKind::CmpXChg; | ||
| } | ||
|
|
||
| return AtomicExpansionKind::CmpXChg; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I lost the track of this condition after many changes. Can this hasGFX940Insts() be spelled out in terms of a specific feature/target property or its combination? What about gfx11/12, we seem to expand even f32?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should that be something like:
Maybe also check addresspace global vs flat.
I believe that was the original intent of the gfx940 check, but I am not sure the meaning of unsafe is still the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have created #100217