[SPIRV] Add missing OpenCL atomic_fetch_min/max builtin mappings#186971
Closed
[SPIRV] Add missing OpenCL atomic_fetch_min/max builtin mappings#186971
Conversation
Member
|
@llvm/pr-subscribers-backend-spir-v Author: Paulius Velesko (pvelesko) ChangesSummary
The backend already had mappings for Test plan
Full diff: https://github.com/llvm/llvm-project/pull/186971.diff 2 Files Affected:
diff --git a/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp b/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp
index f9a9127446013..95adf05b91c8d 100644
--- a/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp
@@ -840,6 +840,17 @@ static bool buildAtomicRMWInst(const SPIRV::IncomingCall *Call, unsigned Opcode,
Semantics, MIRBuilder, GR);
Register ValueReg = Call->Arguments[1];
Register ValueTypeReg = GR->getSPIRVTypeID(Call->ReturnType);
+ // For min/max atomics, select signed or unsigned opcode based on the
+ // integer return type's signedness.
+ if (Call->ReturnType->getOpcode() == SPIRV::OpTypeInt) {
+ bool IsUnsigned = Call->ReturnType->getOperand(2).getImm() == 0;
+ if (IsUnsigned) {
+ if (Opcode == SPIRV::OpAtomicSMax)
+ Opcode = SPIRV::OpAtomicUMax;
+ else if (Opcode == SPIRV::OpAtomicSMin)
+ Opcode = SPIRV::OpAtomicUMin;
+ }
+ }
// support cl_ext_float_atomics
if (Call->ReturnType->getOpcode() == SPIRV::OpTypeFloat) {
if (Opcode == SPIRV::OpAtomicIAdd) {
@@ -1775,6 +1786,10 @@ static bool generateAtomicInst(const SPIRV::IncomingCall *Call,
case SPIRV::OpAtomicXor:
case SPIRV::OpAtomicAnd:
case SPIRV::OpAtomicExchange:
+ case SPIRV::OpAtomicSMax:
+ case SPIRV::OpAtomicSMin:
+ case SPIRV::OpAtomicUMax:
+ case SPIRV::OpAtomicUMin:
return buildAtomicRMWInst(Call, Opcode, MIRBuilder, GR);
case SPIRV::OpMemoryBarrier:
return buildBarrierInst(Call, SPIRV::OpMemoryBarrier, MIRBuilder, GR);
diff --git a/llvm/lib/Target/SPIRV/SPIRVBuiltins.td b/llvm/lib/Target/SPIRV/SPIRVBuiltins.td
index eb1b6c3185e7d..630fb762c6eb2 100644
--- a/llvm/lib/Target/SPIRV/SPIRVBuiltins.td
+++ b/llvm/lib/Target/SPIRV/SPIRVBuiltins.td
@@ -633,6 +633,12 @@ defm : DemangledNativeBuiltin<"atomic_fetch_sub_explicit", OpenCL_std, Atomic, 3
defm : DemangledNativeBuiltin<"atomic_fetch_or_explicit", OpenCL_std, Atomic, 3, 4, OpAtomicOr>;
defm : DemangledNativeBuiltin<"atomic_fetch_xor_explicit", OpenCL_std, Atomic, 3, 4, OpAtomicXor>;
defm : DemangledNativeBuiltin<"atomic_fetch_and_explicit", OpenCL_std, Atomic, 3, 4, OpAtomicAnd>;
+defm : DemangledNativeBuiltin<"atomic_fetch_min", OpenCL_std, Atomic, 2, 4, OpAtomicSMin>;
+defm : DemangledNativeBuiltin<"atomic_fetch_max", OpenCL_std, Atomic, 2, 4, OpAtomicSMax>;
+defm : DemangledNativeBuiltin<"atomic_fetch_min_explicit", OpenCL_std, Atomic, 3, 4, OpAtomicSMin>;
+defm : DemangledNativeBuiltin<"atomic_fetch_max_explicit", OpenCL_std, Atomic, 3, 4, OpAtomicSMax>;
+defm : DemangledNativeBuiltin<"atom_min", OpenCL_std, Atomic, 2, 2, OpAtomicSMin>;
+defm : DemangledNativeBuiltin<"atom_max", OpenCL_std, Atomic, 2, 2, OpAtomicSMax>;
defm : DemangledNativeBuiltin<"atomic_flag_test_and_set", OpenCL_std, Atomic, 1, 1, OpAtomicFlagTestAndSet>;
defm : DemangledNativeBuiltin<"__spirv_AtomicFlagTestAndSet", OpenCL_std, Atomic, 3, 3, OpAtomicFlagTestAndSet>;
defm : DemangledNativeBuiltin<"atomic_flag_test_and_set_explicit", OpenCL_std, Atomic, 2, 3, OpAtomicFlagTestAndSet>;
|
Contributor
Author
|
The Test SPIR-V CI failure is pre-existing on main — confirmed by #186992 (no-op baseline PR on main that shows the same 5 pointer/SpecConstantOp test failures). |
The SPIR-V backend had mappings for atomic_fetch_add/sub/or/xor/and and their _explicit variants, but was missing atomic_fetch_min/max, atomic_fetch_min/max_explicit, and the legacy atom_min/max builtins. This caused OpenCL programs using these atomics to emit unresolved function calls instead of the correct OpAtomicSMin/OpAtomicSMax/ OpAtomicUMin/OpAtomicUMax instructions. Add the missing builtin-to-opcode mappings in SPIRVBuiltins.td and handle the signed/unsigned opcode selection in buildAtomicRMWInst based on the integer return type's signedness (OpTypeInt operand 2).
9c9500e to
2192ffe
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
atomic_fetch_min/max,atomic_fetch_min/max_explicit, and legacyatom_min/maxOpenCL builtin-to-SPIR-V opcode mappings inSPIRVBuiltins.tdOpAtomicSMax/SMin/UMax/UMincases to the atomic RMW dispatch ingenerateAtomicInstbuildAtomicRMWInstbased on theOpTypeIntsignedness operandThe backend already had mappings for
atomic_fetch_add/sub/or/xor/andand their_explicitvariants, but was missingmin/max. This caused OpenCL programs using these atomics to emit unresolved function calls instead of the correctOpAtomicSMin/OpAtomicSMax/OpAtomicUMin/OpAtomicUMaxinstructions.Test plan
atomicMin/Maxtests andcuda-simpleAtomicIntrinsicsnow pass on the in-tree SPIR-V backend (previously failed with "Missing definition foratomic_fetch_max_explicit")atomic_fetch_min/max_explicit→OpAtomicSMin/OpAtomicSMax/OpAtomicUMin/OpAtomicUMaxlowering