diff --git a/AGENTS.md b/AGENTS.md index bd8b647f8..38913a209 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1304,3 +1304,13 @@ this section verbatim). Each entry is one bullet starting with `- `. Never repair this by modifying installed package caches or disabling GPU validation, and keep backend-only preflight and Dagger integration in separate processes to isolate process-level state during diagnosis. + +118. **Span offsets must use the backend's address domain.** An array's + `pointer` need not convert to an integer: Metal before 1.10 returns an + opaque `MtlPtr`, while its `data_address` method supplies the numeric + address already used by aliasing. Use that same hook for span descriptor, + gather and scatter base addresses. A remote `MethodError` can retain the + opaque handle in its arguments and then fail again during exception + serialization, hiding the original conversion error. An opaque-pointer + CPU fixture can protect the address-hook contract; it does not replace + native backend validation. diff --git a/src/memory-spaces.jl b/src/memory-spaces.jl index 0b43e212d..ffca9678b 100644 --- a/src/memory-spaces.jl +++ b/src/memory-spaces.jl @@ -762,7 +762,7 @@ end """ data_address(x::DenseArray) -> UInt64 -The address of `x`'s first element, for aliasing analysis. Defaults to +The address of `x`'s first element, for aliasing and copy-span offsets. Defaults to `pointer(x)`; GPU extensions override it where `pointer` has side effects (CUDA.jl's and AMDGPU.jl's take stream ownership for the *active* stream, and CUDA.jl's throws when that device cannot reach `x` without peer access), or diff --git a/src/utils/span_copy.jl b/src/utils/span_copy.jl index 7f925456f..02081689b 100644 --- a/src/utils/span_copy.jl +++ b/src/utils/span_copy.jl @@ -70,8 +70,10 @@ function _build_span_descriptors(dst::AbstractArray{T}, src::AbstractArray{T}, elsize = sizeof(T) dst_vec = reshape(dst, :) src_vec = reshape(src, :) - dst_base = UInt64(pointer(dst_vec)) - src_base = UInt64(pointer(src_vec)) + # Match the address domain used to construct the spans. Some backends + # expose opaque handles through `pointer` instead of numeric addresses. + dst_base = data_address(dst_vec) + src_base = data_address(src_vec) dst_offs = Vector{UInt32}(undef, n) src_offs = Vector{UInt32}(undef, n) prefix = Vector{UInt32}(undef, n + 1) @@ -189,7 +191,7 @@ function _multi_span_gather_packed!(dst::AbstractArray{T}, src::AbstractArray{T} dst_ptrs = Vector{UInt64}(undef, n) src_ptrs = Vector{UInt64}(undef, n) lens = Vector{UInt64}(undef, n) - dst_base = UInt64(pointer(reshape(dst, :))) + dst_base = data_address(reshape(dst, :)) offset = UInt64(0) for (i, span) in enumerate(src_spans) len = UInt64(span_len(span)) @@ -208,7 +210,7 @@ function _multi_span_scatter_packed!(dst::AbstractArray{T}, src::AbstractArray{T dst_ptrs = Vector{UInt64}(undef, n) src_ptrs = Vector{UInt64}(undef, n) lens = Vector{UInt64}(undef, n) - src_base = UInt64(pointer(reshape(src, :))) + src_base = data_address(reshape(src, :)) offset = UInt64(0) for (i, span) in enumerate(dst_spans) len = UInt64(span_len(span)) diff --git a/test/memory-spaces.jl b/test/memory-spaces.jl index 5eacd0d39..6fcad9f22 100644 --- a/test/memory-spaces.jl +++ b/test/memory-spaces.jl @@ -83,3 +83,5 @@ @test fetch(Dagger.@spawn kernel_lock_proc_is_task_proc(1.0)) end end + +include("span-copy-address.jl") diff --git a/test/span-copy-address.jl b/test/span-copy-address.jl new file mode 100644 index 000000000..013479b21 --- /dev/null +++ b/test/span-copy-address.jl @@ -0,0 +1,86 @@ +module SpanCopyAddressTests + +using Dagger, Test +import Dagger: GPUArraysCore, KernelAbstractions + +# A backend may expose an opaque buffer handle through `pointer`, while its +# address hook supplies the numeric domain used by aliasing and memory spans. +# Metal <1.10 is one such backend. No Metal device is needed for this contract. +struct OpaquePointer{T} + address::Ptr{T} +end +struct AddressVector{T} <: GPUArraysCore.AbstractGPUVector{T} + data::Vector{T} +end +Base.size(x::AddressVector) = size(x.data) +Base.IndexStyle(::Type{<:AddressVector}) = IndexLinear() +Base.getindex(x::AddressVector, i::Int) = x.data[i] +Base.setindex!(x::AddressVector, value, i::Int) = (x.data[i] = value) +Base.pointer(x::AddressVector) = OpaquePointer(pointer(x.data)) +Base.reshape(x::AddressVector, ::Colon) = x +Base.collect(x::AddressVector) = copy(x.data) +KernelAbstractions.get_backend(::AddressVector) = KernelAbstractions.CPU() +Dagger.data_address(x::AddressVector) = UInt64(pointer(x.data)) + +@testset "Span copy backend addresses" begin + x = AddressVector(Float32.(1:16)) + @test_throws MethodError UInt(pointer(x)) + @test @inferred(Dagger.data_address(x)) == UInt64(pointer(x.data)) + + for wrap in (identity, AddressVector) + @testset "$wrap" begin + src = wrap(Float32.(1:16)) + dst = wrap(zeros(Float32, 16)) + src_base, dst_base = Dagger.data_address(src), Dagger.data_address(dst) + src_ptrs = UInt64[src_base + 2*sizeof(Float32), src_base + 8*sizeof(Float32)] + dst_ptrs = UInt64[dst_base + sizeof(Float32), dst_base + 11*sizeof(Float32)] + lens = UInt64[3*sizeof(Float32), 2*sizeof(Float32)] + src_spans = Dagger.LocalMemorySpan.(src_ptrs, lens) + dst_spans = Dagger.LocalMemorySpan.(dst_ptrs, lens) + expected = zeros(Float32, 16) + expected[2:4] = Float32[3, 4, 5] + expected[12:13] = Float32[9, 10] + + @testset "Descriptor offsets" begin + dv, sv, doffs, soffs, prefix = @inferred Dagger._build_span_descriptors( + dst, src, dst_ptrs, src_ptrs, lens) + @test dv === dst + @test sv === src + @test doffs == UInt32[1, 11] + @test soffs == UInt32[2, 8] + @test prefix == UInt32[0, 3, 5] + end + @testset "Span copy" begin + Dagger.multi_span_copy!(dst, src, dst_ptrs, src_ptrs, lens) + @test collect(dst) == expected + end + @testset "Gather" begin + packed = wrap(zeros(Float32, 5)) + Dagger._multi_span_gather_packed!(packed, src, src_spans) + @test collect(packed) == Float32[3, 4, 5, 9, 10] + end + @testset "Scatter" begin + packed = wrap(Float32[3, 4, 5, 9, 10]) + scattered = wrap(zeros(Float32, 16)) + base = Dagger.data_address(scattered) + spans = Dagger.LocalMemorySpan.(UInt64[base + sizeof(Float32), + base + 11*sizeof(Float32)], lens) + Dagger._multi_span_scatter_packed!(scattered, packed, spans) + @test collect(scattered) == expected + end + @testset "Public byte packing" begin + bytes = zeros(UInt8, 5*sizeof(Float32)) + Dagger.multi_span_gather!(bytes, src, src_spans) + @test collect(reinterpret(Float32, bytes)) == Float32[3, 4, 5, 9, 10] + scattered = wrap(zeros(Float32, 16)) + base = Dagger.data_address(scattered) + spans = Dagger.LocalMemorySpan.(UInt64[base + sizeof(Float32), + base + 11*sizeof(Float32)], lens) + Dagger.multi_span_scatter!(scattered, bytes, spans) + @test collect(scattered) == expected + end + end + end +end + +end # module