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

Make vectorized store convert and perform multiple stores if required #111

Merged
merged 4 commits into from
Jun 29, 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
22 changes: 17 additions & 5 deletions src/layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Layout
using CUDA
using LLVMLoopInfo: @loopinfo
using GemmKernels.Tiling
using Base.Cartesian: @ntuple

# ---------------------
# Customise computation
Expand All @@ -17,7 +18,8 @@ using GemmKernels.Tiling

struct Vec{N, T} end

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

return quote
Expand All @@ -26,13 +28,23 @@ struct Vec{N, T} end
end
end

@inline @generated function vstorea!(::Type{Vec{N, T}}, ptr::Core.LLVMPtr{T, AS}, x, i::Integer = 1) where {N, T, AS}
@inline @generated function vstorea!(::Type{Vec{N, T}}, ptr::Core.LLVMPtr{T, AS},
x::NTuple{M,<:Any}, i::Integer = 1) where {N, T, AS, M}
alignment = sizeof(T) * N

return quote
vec_ptr = Base.bitcast(Core.LLVMPtr{NTuple{N, VecElement{T}}, AS}, ptr)
return unsafe_store!(vec_ptr, x, (i-1) ÷ N + 1, Val($alignment))
ex = quote end

# we may be storing more values than we can using a single vectorized operation
# (e.g., when types mismatch, storing 8 Float16s in a Float32 shared memory layout)
for offset = 0:N:M-1
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)
unsafe_store!(vec_ptr, y, (i+$offset-1) ÷ N + 1, Val($alignment))
end).args)
end

return ex
end

# -----------
Expand Down