Skip to content

Let Base handle zero-source map! on GPU arrays - #824

Open
maleadt wants to merge 1 commit into
mainfrom
fix-gpu-map-inplace-c0a8cfc1
Open

maleadt wants to merge 1 commit into
mainfrom
fix-gpu-map-inplace-c0a8cfc1

Conversation

@maleadt

@maleadt maleadt commented Oct 19, 2026

Copy link
Copy Markdown
Member

Julia 1.12 added map!(f, A), which maps an array in place by forwarding to map!(f, A, A). Our map!(f, dest::AnyGPUArray, xs::AbstractArray...) is more specific, so it also caught the zero-source call and broadcast f():

julia> map!(sqrt, JLArray(Float32[1, 4, 9]))
ERROR: MethodError: no method matching sqrt()

On a real GPU this surfaces as an InvalidIRError. Before 1.12 the same interception hid Base's ArgumentError: map! requires at least one "source" argument, which is what #482 originally reported.

This requires at least one source argument in our method, so the zero-source call falls through to Base: on 1.12+ Base forwards it back to us with dest as the source, and on older versions users get Base's error. Adding an explicit map!(f, dest::AnyGPUArray) = map!(f, dest, dest) instead would also enable in-place map! for GPU arrays on 1.10/1.11, where it doesn't exist for Array.

Tested with JLArrays on 1.10, 1.11, 1.12 and 1.13 (no new map! ambiguities); the added test fails on 1.12 without the change.

Fixes #482

Julia 1.12 added `map!(f, A)`, which maps an array in place by
forwarding to `map!(f, A, A)`. Our `map!(f, dest::AnyGPUArray,
xs::AbstractArray...)` is more specific and also matched the
zero-source call, building an empty broadcast that invoked `f()`.

Requiring at least one source lets Base handle that call: on 1.12+ it
forwards back to us with `dest` as the source, and on older versions
Base throws its "map! requires at least one source argument" error
instead of a confusing compilation failure.

This branch has not been deployed

No deployments
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.

Better error for map! misuse

1 participant