Repository navigation
Translate OpUMulExtended and OpSMulExtended - #29
Merged
Merged
Conversation
The LLVM SPIR-V back end lowers llvm.{u,s}mul.with.overflow to these, so
checked multiplication (Julia's checked_mul, and with it Rational
arithmetic) failed to translate with "UNIMPLEMENTED instruction 152".
Like OpIAddCarry, the result is a struct initialized from its two halves:
the wrapping product and mul_hi, with the operands reinterpreted as signed
for OpSMulExtended. Narrow scalars are multiplied as uint, since their
promotion to int can overflow for 16-bit operands.
Member
Author
|
CI failure unrelated. |
This was referenced Nov 10, 2026
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.
LLVM's SPIR-V back end lowers
llvm.umul.with.overflowandllvm.smul.with.overflowtoOpUMulExtendedandOpSMulExtended, which spirv2clc rejected withUNIMPLEMENTED instruction 151/152. Julia emits these forBase.mul_with_overflowandBase.checked_mul, and therefore forRationalarithmetic. They break on OpenCL.jl's OpenCL C source path, e.g. the new "multiplication with overflow" tests on JuliaGPU/KernelAbstractions.jl#962 (poclctarget).Like
OpIAddCarry, the result is a{low, high}struct built with an initializer:{(T)(a * b), mul_hi(a, b)} // OpUMulExtended {(T)(a * b), as_T(mul_hi(as_sT(a), as_sT(b)))} // OpSMulExtendedThe low half is the same for both signednesses. The signed
mul_hioverload needs signed operands, because spirv2clc spells integer types unsigned. Scalaruchar/ushortoperands are multiplied asuint: they would otherwise be promoted toint, where a 16-bit product can overflow.Testing:
mul_extended.sptcovers both opcodes on 8, 16, 32 and 64-bit scalars and onushort2/uint4vectors. It fails on main with UNIMPLEMENTED. The suite passes: 151/151.poclc/kernelabstractions, with KernelAbstractions.jl#962 and this build of libspirv2clc (PoCL 7.2 on an M1): before, 2814 pass and 5 errors, all in "multiplication with overflow". After, 2827 pass and no errors.Base.mul_with_overflowin a kernel on PoCL's source path over about 66k pairs per type: everyUInt8pair, plus edge values and random values for the wider types. Results matched the host exactly forUInt8,UInt16,Int32,UInt32,Int64andUInt64.Int8/Int16still don't build. That's not because of this change: Julia computes their overflow flag with an arithmetic shift, andOpShiftRightArithmeticon 8/16-bit types translates toas_uchar(as_char(a) >> as_char(b)). The shift is promoted toint, soas_ucharrejects it. That needs a separate fix.