Skip to content

[SYCL][Joint Matrix] Pass on address space to Load/Store #9244

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

Merged
merged 10 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions sycl/include/CL/__spirv/spirv_ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,19 @@ extern __DPCPP_SYCL_EXTERNAL __spv::__spirv_JointMatrixINTEL<T, R, C, L, S, U> *
__spirv_VectorInsertDynamic(__spv::__spirv_JointMatrixINTEL<T, R, C, L, S, U> *,
Ts val, size_t i);
#else
template <typename T, std::size_t R, std::size_t C,
template <typename T, typename Tp, std::size_t R, std::size_t C,
__spv::MatrixLayout L = __spv::MatrixLayout::RowMajor,
__spv::Scope::Flag S = __spv::Scope::Flag::Subgroup>
extern __DPCPP_SYCL_EXTERNAL __spv::__spirv_JointMatrixINTEL<T, R, C, L, S> *
extern __DPCPP_SYCL_EXTERNAL __spv::__spirv_JointMatrixINTEL<Tp, R, C, L, S> *
__spirv_JointMatrixLoadINTEL(T *Ptr, std::size_t Stride,
__spv::MatrixLayout Layout = L,
__spv::Scope::Flag Sc = S, int MemOperand = 0);

template <typename T, std::size_t R, std::size_t C,
template <typename T, typename Tp, std::size_t R, std::size_t C,
__spv::MatrixLayout L = __spv::MatrixLayout::RowMajor,
__spv::Scope::Flag S = __spv::Scope::Flag::Subgroup>
extern __DPCPP_SYCL_EXTERNAL void __spirv_JointMatrixStoreINTEL(
T *Ptr, __spv::__spirv_JointMatrixINTEL<T, R, C, L, S> *Object,
T *Ptr, __spv::__spirv_JointMatrixINTEL<Tp, R, C, L, S> *Object,
std::size_t Stride, __spv::MatrixLayout Layout = L,
__spv::Scope::Flag Sc = S, int MemOperand = 0);

Expand Down
8 changes: 6 additions & 2 deletions sycl/include/sycl/ext/oneapi/matrix/matrix-intel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include "matrix-unified-utils.hpp"
#include "utils.hpp"
#include <CL/__spirv/spirv_ops.hpp>
#include <sycl/detail/defines_elementary.hpp>
#include <sycl/feature_test.hpp>
Expand Down Expand Up @@ -481,6 +482,8 @@ joint_matrix_store(Group sg,
Group, Tp, Use, NumRows, NumCols, Layout> &src,
multi_ptr<T, Space, IsDecorated> dst, size_t stride) {
#if defined(__SYCL_DEVICE_ONLY__)
static_assert(Space != access::address_space::private_space,
"Joint Matrix doesn't support store to private memory!");
#if defined(__NVPTX__)
std::ignore = sg;
std::ignore = src;
Expand All @@ -492,8 +495,9 @@ joint_matrix_store(Group sg,
PI_ERROR_INVALID_DEVICE);
#else
// intel's impl
T *Ptr = dst.get();
__spirv_JointMatrixStoreINTEL<T, Tp, NumRows, NumCols,
using DecorT = typename sycl::detail::DecoratedType<T, Space>::type;
DecorT *Ptr = sycl::detail::getDecorated<DecorT>(dst);
__spirv_JointMatrixStoreINTEL<DecorT, Tp, NumRows, NumCols,
sycl::ext::oneapi::experimental::matrix::
spv_matrix_use_traits<Use>::value,
sycl::ext::oneapi::experimental::matrix::
Expand Down
27 changes: 17 additions & 10 deletions sycl/include/sycl/ext/oneapi/matrix/matrix-jit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once

#include "utils.hpp"
#include <CL/__spirv/spirv_ops.hpp>
#include <sycl/detail/defines_elementary.hpp>
#include <sycl/ext/oneapi/bfloat16.hpp>
Expand Down Expand Up @@ -77,34 +78,37 @@ inline __SYCL_ALWAYS_INLINE void joint_matrix_load(
Group sg, joint_matrix<T, NumRows, NumCols, Layout, Group> &res,
multi_ptr<T, Space, IsDecorated> src, size_t stride, matrix_layout MemL) {
#ifdef __SYCL_DEVICE_ONLY__
T *Ptr = src.get();
static_assert(Space != access::address_space::private_space,
"Joint Matrix doesn't support load from private memory!");
using DecorT = typename sycl::detail::DecoratedType<T, Space>::type;
DecorT *Ptr = sycl::detail::getDecorated<DecorT>(src);
switch (MemL) {
default:
assert(false && "Invalid Memory Layout!");
case matrix_layout::row_major:
res.spvm =
__spirv_JointMatrixLoadINTEL<T, NumRows, NumCols,
__spirv_JointMatrixLoadINTEL<DecorT, T, NumRows, NumCols,
spv_matrix_layout_traits<Layout>::value>(
Ptr, stride, __spv::MatrixLayout::RowMajor,
spv_scope_traits<Group>::value);
break;
case matrix_layout::col_major:
res.spvm =
__spirv_JointMatrixLoadINTEL<T, NumRows, NumCols,
__spirv_JointMatrixLoadINTEL<DecorT, T, NumRows, NumCols,
spv_matrix_layout_traits<Layout>::value>(
Ptr, stride, __spv::MatrixLayout::ColumnMajor,
spv_scope_traits<Group>::value);
break;
case matrix_layout::packed_a:
res.spvm =
__spirv_JointMatrixLoadINTEL<T, NumRows, NumCols,
__spirv_JointMatrixLoadINTEL<DecorT, T, NumRows, NumCols,
spv_matrix_layout_traits<Layout>::value>(
Ptr, stride, __spv::MatrixLayout::PackedA,
spv_scope_traits<Group>::value);
break;
case matrix_layout::packed_b:
res.spvm =
__spirv_JointMatrixLoadINTEL<T, NumRows, NumCols,
__spirv_JointMatrixLoadINTEL<DecorT, T, NumRows, NumCols,
spv_matrix_layout_traits<Layout>::value>(
Ptr, stride, __spv::MatrixLayout::PackedB,
spv_scope_traits<Group>::value);
Expand All @@ -128,30 +132,33 @@ inline __SYCL_ALWAYS_INLINE void joint_matrix_store(
Group sg, joint_matrix<T, NumRows, NumCols, MatL, Group> &src,
multi_ptr<T, Space, IsDecorated> res, size_t stride, matrix_layout MemL) {
#ifdef __SYCL_DEVICE_ONLY__
T *Ptr = res.get();
static_assert(Space != access::address_space::private_space,
"Joint Matrix doesn't support store to private memory!");
using DecorT = typename sycl::detail::DecoratedType<T, Space>::type;
DecorT *Ptr = sycl::detail::getDecorated<DecorT>(res);
switch (MemL) {
default:
assert(false && "Invalid Memory Layout!");
case matrix_layout::row_major:
__spirv_JointMatrixStoreINTEL<T, NumRows, NumCols,
__spirv_JointMatrixStoreINTEL<DecorT, T, NumRows, NumCols,
spv_matrix_layout_traits<MatL>::value>(
Ptr, src.spvm, stride, __spv::MatrixLayout::RowMajor,
spv_scope_traits<Group>::value);
break;
case matrix_layout::col_major:
__spirv_JointMatrixStoreINTEL<T, NumRows, NumCols,
__spirv_JointMatrixStoreINTEL<DecorT, T, NumRows, NumCols,
spv_matrix_layout_traits<MatL>::value>(
Ptr, src.spvm, stride, __spv::MatrixLayout::ColumnMajor,
spv_scope_traits<Group>::value);
break;
case matrix_layout::packed_a:
__spirv_JointMatrixStoreINTEL<T, NumRows, NumCols,
__spirv_JointMatrixStoreINTEL<DecorT, T, NumRows, NumCols,
spv_matrix_layout_traits<MatL>::value>(
Ptr, src.spvm, stride, __spv::MatrixLayout::PackedA,
spv_scope_traits<Group>::value);
break;
case matrix_layout::packed_b:
__spirv_JointMatrixStoreINTEL<T, NumRows, NumCols,
__spirv_JointMatrixStoreINTEL<DecorT, T, NumRows, NumCols,
spv_matrix_layout_traits<MatL>::value>(
Ptr, src.spvm, stride, __spv::MatrixLayout::PackedB,
spv_scope_traits<Group>::value);
Expand Down
36 changes: 26 additions & 10 deletions sycl/include/sycl/ext/oneapi/matrix/matrix-unified.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once
#include "matrix-intel.hpp"
#include "utils.hpp"
#include <sycl/ext/oneapi/matrix/matrix-tensorcores.hpp>
namespace sycl {
__SYCL_INLINE_VER_NAMESPACE(_V1) {
Expand Down Expand Up @@ -199,32 +200,38 @@ inline __SYCL_ALWAYS_INLINE void joint_matrix_load(
multi_ptr<T, Space, IsDecorated> src, size_t stride,
sycl::ext::oneapi::experimental::matrix::layout Layout) {
#if defined(__SYCL_DEVICE_ONLY__)
static_assert(Space != access::address_space::private_space,
"Joint Matrix doesn't support load from private memory!");
#if defined(__NVPTX__)
std::ignore = sg;
sycl::ext::oneapi::detail::load_accumulator_cuda(res.cuda_impl, src, stride,
Layout);
#else
T *Ptr = src.get();
using DecorT = typename sycl::detail::DecoratedType<T, Space>::type;
DecorT *Ptr = sycl::detail::getDecorated<DecorT>(src);
switch (Layout) {
default:
assert(false && "Invalid Memory Layout!");
case layout::row_major:
res.spvm = __spirv_JointMatrixLoadINTEL<
T, S, NumRows, NumCols, spv_matrix_use_traits<use::accumulator>::value,
DecorT, S, NumRows, NumCols,
spv_matrix_use_traits<use::accumulator>::value,
spv_matrix_layout_traits<layout::dynamic>::value>(
Ptr, stride, __spv::MatrixLayout::RowMajor,
spv_scope_traits<Group>::value);
break;
case layout::col_major:
res.spvm = __spirv_JointMatrixLoadINTEL<
T, S, NumRows, NumCols, spv_matrix_use_traits<use::accumulator>::value,
DecorT, S, NumRows, NumCols,
spv_matrix_use_traits<use::accumulator>::value,
spv_matrix_layout_traits<layout::dynamic>::value>(
Ptr, stride, __spv::MatrixLayout::ColumnMajor,
spv_scope_traits<Group>::value);
break;
case sycl::ext::intel::experimental::matrix::layout::packed:
res.spvm = __spirv_JointMatrixLoadINTEL<
T, S, NumRows, NumCols, spv_matrix_use_traits<use::accumulator>::value,
DecorT, S, NumRows, NumCols,
spv_matrix_use_traits<use::accumulator>::value,
spv_matrix_layout_traits<layout::dynamic>::value>(
Ptr, stride, __spv::MatrixLayout::Packed,
spv_scope_traits<Group>::value);
Expand Down Expand Up @@ -255,15 +262,18 @@ joint_matrix_load(Group sg,
joint_matrix<Group, S, Use, NumRows, NumCols, Layout> &res,
multi_ptr<T, Space, IsDecorated> src, size_t stride) {
#if defined(__SYCL_DEVICE_ONLY__)
static_assert(Space != access::address_space::private_space,
"Joint Matrix doesn't support load from private memory!");
#if defined(__NVPTX__)
std::ignore = sg;
sycl::ext::oneapi::detail::load_multiplicand_cuda<S, T, NumRows, NumCols, Use,
Layout, Space>(
res.cuda_impl, src, stride);
#else
T *Ptr = src.get();
using DecorT = typename sycl::detail::DecoratedType<T, Space>::type;
DecorT *Ptr = sycl::detail::getDecorated<DecorT>(src);
res.spvm =
__spirv_JointMatrixLoadINTEL<T, S, NumRows, NumCols,
__spirv_JointMatrixLoadINTEL<DecorT, S, NumRows, NumCols,
spv_matrix_use_traits<Use>::value,
spv_matrix_layout_traits<Layout>::value>(
Ptr, stride, spv_matrix_layout_traits<Layout>::value,
Expand All @@ -288,33 +298,39 @@ inline __SYCL_ALWAYS_INLINE void joint_matrix_store(
multi_ptr<T, Space, IsDecorated> dst, size_t stride,
sycl::ext::oneapi::experimental::matrix::layout Layout) {
#if defined(__SYCL_DEVICE_ONLY__)
static_assert(Space != access::address_space::private_space,
"Joint Matrix doesn't support store to private memory!");
#if defined(__NVPTX__)
std::ignore = sg;
sycl::ext::oneapi::detail::joint_matrix_store_cuda<T, NumRows, NumCols,
Space>(src.cuda_impl, dst,
stride, Layout);
#else
T *Ptr = dst.get();
using DecorT = typename sycl::detail::DecoratedType<T, Space>::type;
DecorT *Ptr = sycl::detail::getDecorated<DecorT>(dst);
switch (Layout) {
default:
assert(false && "Invalid Memory Layout!");
case layout::row_major:
__spirv_JointMatrixStoreINTEL<
T, T, NumRows, NumCols, spv_matrix_use_traits<use::accumulator>::value,
DecorT, T, NumRows, NumCols,
spv_matrix_use_traits<use::accumulator>::value,
spv_matrix_layout_traits<layout::dynamic>::value>(
Ptr, src.spvm, stride, __spv::MatrixLayout::RowMajor,
spv_scope_traits<Group>::value);
break;
case layout::col_major:
__spirv_JointMatrixStoreINTEL<
T, T, NumRows, NumCols, spv_matrix_use_traits<use::accumulator>::value,
DecorT, T, NumRows, NumCols,
spv_matrix_use_traits<use::accumulator>::value,
spv_matrix_layout_traits<layout::dynamic>::value>(
Ptr, src.spvm, stride, __spv::MatrixLayout::ColumnMajor,
spv_scope_traits<Group>::value);
break;
case sycl::ext::intel::experimental::matrix::layout::packed:
__spirv_JointMatrixStoreINTEL<
T, T, NumRows, NumCols, spv_matrix_use_traits<use::accumulator>::value,
DecorT, T, NumRows, NumCols,
spv_matrix_use_traits<use::accumulator>::value,
spv_matrix_layout_traits<layout::dynamic>::value>(
Ptr, src.spvm, stride, __spv::MatrixLayout::Packed,
spv_scope_traits<Group>::value);
Expand Down
33 changes: 33 additions & 0 deletions sycl/include/sycl/ext/oneapi/matrix/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===------- utils.hpp - SYCL matrix extension ----*- C++ -*------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// ===--------------------------------------------------------------------=== //

#pragma once

#include <sycl/access/access.hpp>
#include <sycl/multi_ptr.hpp>

namespace sycl {
__SYCL_INLINE_VER_NAMESPACE(_V1) {
namespace detail {

// Helper to return decorated pointer for different values
// of access::decorated parameter.
// If access::decorated::legacy is removed in the future
// this helper usage can be replaced with ptr.get_decorated().
template <typename DecorT, typename T, access::address_space Space,
access::decorated IsDecorated>
DecorT *getDecorated(multi_ptr<T, Space, IsDecorated> ptr) {
if constexpr (IsDecorated == access::decorated::legacy)
return ptr.get();
else
return ptr.get_decorated();
}

} // namespace detail
} // __SYCL_INLINE_VER_NAMESPACE(_V1)
} // namespace sycl
61 changes: 61 additions & 0 deletions sycl/test/check_device_code/matrix/matrix_load_store_as.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// RUN: %clangxx -fsycl-device-only -S -emit-llvm -o - %s | FileCheck %s

// check that correct address spaces are used to load from and store to
#define SYCL_EXT_ONEAPI_MATRIX_VERSION 4
#include <sycl/sycl.hpp>

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;

int main(void) {
queue q;
unsigned short *A = malloc_shared<unsigned short>(8 * 16, q);
unsigned short *B = malloc_shared<unsigned short>(16 * 16, q);
float *C = malloc_shared<float>(8 * 16, q);

auto pA = multi_ptr<unsigned short, access::address_space::global_space>(A);
auto pB = multi_ptr<unsigned short, access::address_space::global_space>(B);
auto pC = multi_ptr<float, access::address_space::global_space>(C);

q.submit([&](handler &h) {
local_accessor<unsigned short, 2> tileA{{8, 16}, h};

h.parallel_for(
nd_range<2>({1, 16}, {1, 16}),
[=](nd_item<2> it) [[intel::reqd_sub_group_size(16)]] {
joint_matrix<sub_group, unsigned short, use::a, 8, 16,
layout::row_major>
tA;
joint_matrix<sub_group, unsigned short, use::b, 16, 16,
ext::intel::experimental::matrix::layout::packed>
tB;
joint_matrix<sub_group, float, use::accumulator, 8, 16> tC;

sub_group sg = it.get_sub_group();
vec<unsigned short, 8> slmvec = sg.load<8>(pA);
sg.store<8>(
tileA.template get_multi_ptr<sycl::access::decorated::yes>(),
slmvec);
it.barrier(access::fence_space::local_space);

// A should load from local address space
// CHECK: %{{.*}} = tail call spir_func noundef %spirv.JointMatrixINTEL._short_8_16_0_3_0 addrspace(4)* @_Z[[#]]__spirv_JointMatrixLoadINTEL{{.*}}(i16 addrspace(3)* noundef %{{.*}}, i64 noundef 16, i32 noundef 0, i32 noundef 3, i32 noundef 0) #{{.*}}
joint_matrix_load(
sg, tA,
tileA.template get_multi_ptr<sycl::access::decorated::yes>(), 16);
// B should load from global address space
// CHECK: %{{.*}} = tail call spir_func noundef %spirv.JointMatrixINTEL._short_16_16_2_3_1 addrspace(4)* @_Z[[#]]__spirv_JointMatrixLoadINTEL{{.*}}(i16 addrspace(1)* noundef %{{.*}}, i64 noundef 32, i32 noundef 2, i32 noundef 3, i32 noundef 0) #{{.*}}
joint_matrix_load(sg, tB, pB, 32);
tC = joint_matrix_mad(sg, tA, tB, tC);
// C should store to global address space
// CHECK: tail call spir_func void @_Z[[#]]__spirv_JointMatrixStoreINTEL{{.*}}(float addrspace(1)* noundef %{{.*}}, %spirv.JointMatrixINTEL._float_8_16_3_3_2 addrspace(4)* noundef %{{.*}}, i64 noundef 16, i32 noundef 0, i32 noundef 3, i32 noundef 0) #{{.*}}
joint_matrix_store(sg, tC, pC, 16, layout::row_major);
});
});

free(A, q);
free(B, q);
free(C, q);

return 0;
}
Loading