Skip to content

Translate OpUMulExtended and OpSMulExtended - #29

Merged
maleadt merged 1 commit into
mainfrom
tb/mul-extended
Nov 10, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/mul-extended

Conversation

@maleadt

@maleadt maleadt commented Nov 10, 2026

Copy link
Copy Markdown
Member

LLVM's SPIR-V back end lowers llvm.umul.with.overflow and llvm.smul.with.overflow to OpUMulExtended and OpSMulExtended, which spirv2clc rejected with UNIMPLEMENTED instruction 151/152. Julia emits these for Base.mul_with_overflow and Base.checked_mul, and therefore for Rational arithmetic. They break on OpenCL.jl's OpenCL C source path, e.g. the new "multiplication with overflow" tests on JuliaGPU/KernelAbstractions.jl#962 (poclc target).

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)))}      // OpSMulExtended

The low half is the same for both signednesses. The signed mul_hi overload needs signed operands, because spirv2clc spells integer types unsigned. Scalar uchar/ushort operands are multiplied as uint: they would otherwise be promoted to int, where a 16-bit product can overflow.

Testing:

  • lit: the new mul_extended.spt covers both opcodes on 8, 16, 32 and 64-bit scalars and on ushort2/uint4 vectors. It fails on main with UNIMPLEMENTED. The suite passes: 151/151.
  • OpenCL.jl, 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.
  • Products and overflow flags against the host: I ran Base.mul_with_overflow in a kernel on PoCL's source path over about 66k pairs per type: every UInt8 pair, plus edge values and random values for the wider types. Results matched the host exactly for UInt8, UInt16, Int32, UInt32, Int64 and UInt64.

Int8/Int16 still don't build. That's not because of this change: Julia computes their overflow flag with an arithmetic shift, and OpShiftRightArithmetic on 8/16-bit types translates to as_uchar(as_char(a) >> as_char(b)). The shift is promoted to int, so as_uchar rejects it. That needs a separate fix.

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.
@maleadt

maleadt commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

CI failure unrelated.

@maleadt
maleadt merged commit 5fd69f0 into main Nov 10, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant