Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect alignment issues and throw a Julia error. #134

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,26 @@ using Base.Cartesian: @ntuple

struct Vec{N, T} end

@noinline function throw_alignmenterror(ptr)
@cuprintln "ERROR: AlignmentError: Pointer $ptr is not properly aligned for vectorized load/store"
error()
end

@inline function checkalignment(ptr)
checkalignment(Bool, ptr) || throw_alignmenterror(ptr)
nothing
end

@inline function checkalignment(::Type{Bool}, ptr)
Int(ptr) % 16 == 0
end

@inline @generated function vloada(::Type{Vec{N, T}}, ptr::Core.LLVMPtr{T, AS}) where {N, T, AS}
alignment = sizeof(T) * N

return quote
vec_ptr = Base.bitcast(Core.LLVMPtr{NTuple{N, VecElement{T}}, AS}, ptr)
@boundscheck checkalignment(vec_ptr)
return unsafe_load(vec_ptr, 1, Val($alignment))
end
end
Expand All @@ -40,6 +55,7 @@ end
append!(ex.args, (quote
y = @ntuple $N j -> VecElement{T}(x[j+$offset].value)
vec_ptr = Base.bitcast(Core.LLVMPtr{NTuple{N, VecElement{T}}, AS}, ptr)
@boundscheck checkalignment(vec_ptr)
unsafe_store!(vec_ptr, y, $offset ÷ N + 1, Val($alignment))
end).args)
end
Expand Down