Skip to content
Open
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
157 changes: 139 additions & 18 deletions tools/clang/lib/Headers/hlsl/dx/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,19 @@ template <ComponentEnum CompTy> struct ComponentTypeTraits {
static const uint ElementsPerScalar = 4;
};

template <typename T> struct TypeTraits {
static const ComponentEnum CompType =
(ComponentEnum)dxil::ComponentType::Invalid;
};

#define __MATRIX_SCALAR_COMPONENT_MAPPING(enum_val, type) \
template <> struct ComponentTypeTraits<enum_val> { \
using Type = type; \
static const bool IsNativeScalar = true; \
static const uint ElementsPerScalar = 1; \
}; \
template <> struct TypeTraits<type> { \
static const ComponentEnum CompType = enum_val; \
};

#if __HLSL_ENABLE_16_BIT
Expand Down Expand Up @@ -498,14 +506,61 @@ Multiply(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
template <typename OutputElTy, typename InputElTy, typename BiasElTy,
SIZE_TYPE M, SIZE_TYPE K, ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<hlsl::is_arithmetic<InputElTy>::value, vector<OutputElTy, M> >::type
typename hlsl::enable_if<hlsl::is_arithmetic<InputElTy>::value &&
__detail::TypeTraits<BiasElTy>::CompType ==
__detail::TypeTraits<OutputElTy>::CompType,
vector<OutputElTy, M> >::type
// clang-format on
MultiplyAdd(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
vector<InputElTy, K> Vec, vector<BiasElTy, M> Bias) {
vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiplyAdd(Result, MatrixA.__handle,
hlsl::is_signed<OutputElTy>::value,
Vec, MatrixDT, Bias, MatrixDT);
__builtin_LinAlg_MatrixVectorMultiplyAdd(
Result, MatrixA.__handle, hlsl::is_signed<OutputElTy>::value, Vec,
__detail::TypeTraits<InputElTy>::CompType, Bias,
__detail::TypeTraits<OutputElTy>::CompType);
return Result;
}

template <typename OutputElTy, typename InputElTy, typename BiasElTy,
SIZE_TYPE M, SIZE_TYPE K, ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<hlsl::is_arithmetic<InputElTy>::value &&
__detail::TypeTraits<BiasElTy>::CompType !=
__detail::TypeTraits<OutputElTy>::CompType,
vector<OutputElTy, M> >::type
// clang-format on
MultiplyAdd(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
vector<InputElTy, K> Vec, vector<BiasElTy, M> Bias) {
vector<OutputElTy, M> BiasVecConv;
__builtin_LinAlg_Convert(BiasVecConv, Bias,
__detail::TypeTraits<BiasElTy>::CompType,
__detail::TypeTraits<OutputElTy>::CompType);
vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiplyAdd(
Result, MatrixA.__handle, hlsl::is_signed<OutputElTy>::value, Vec,
__detail::TypeTraits<InputElTy>::CompType, BiasVecConv,
__detail::TypeTraits<OutputElTy>::CompType);
return Result;
}

template <typename OutputElTy, typename InputElTy, ComponentEnum InputInterp,
typename BiasElTy, SIZE_TYPE M, SIZE_TYPE VecK, SIZE_TYPE K,
ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<
VecK == __detail::ScalarCountFromPackedComponents<InputInterp, K>::Value &&
__detail::TypeTraits<BiasElTy>::CompType ==
__detail::TypeTraits<OutputElTy>::CompType,
vector<OutputElTy, M> >::type
// clang-format on
MultiplyAdd(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
InterpretedVector<InputElTy, VecK, InputInterp> InterpVec,
vector<BiasElTy, M> Bias) {
vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiplyAdd(
Result, MatrixA.__handle, hlsl::is_signed<OutputElTy>::value,
InterpVec.Data, InterpVec.Interpretation, Bias,
__detail::TypeTraits<OutputElTy>::CompType);
return Result;
}

Expand All @@ -514,55 +569,121 @@ template <typename OutputElTy, typename InputElTy, ComponentEnum InputInterp,
ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<
VecK == __detail::ScalarCountFromPackedComponents<InputInterp, K>::Value,
VecK == __detail::ScalarCountFromPackedComponents<InputInterp, K>::Value &&
__detail::TypeTraits<BiasElTy>::CompType !=
__detail::TypeTraits<OutputElTy>::CompType,
vector<OutputElTy, M> >::type
// clang-format on
MultiplyAdd(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
InterpretedVector<InputElTy, VecK, InputInterp> InterpVec,
vector<BiasElTy, M> Bias) {

vector<OutputElTy, M> BiasVecConv;
__builtin_LinAlg_Convert(BiasVecConv, Bias,
__detail::TypeTraits<BiasElTy>::CompType,
__detail::TypeTraits<OutputElTy>::CompType);

vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiplyAdd(
Result, MatrixA.__handle, hlsl::is_signed<OutputElTy>::value,
InterpVec.Data, InterpVec.Interpretation, Bias, MatrixDT);
InterpVec.Data, InterpVec.Interpretation, BiasVecConv,
__detail::TypeTraits<OutputElTy>::CompType);
return Result;
}

template <typename OutputElTy, typename InputElTy, ComponentEnum BiasInterp,
SIZE_TYPE M, SIZE_TYPE K, ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<hlsl::is_arithmetic<InputElTy>::value &&
__detail::TypeTraits<OutputElTy>::CompType == BiasInterp,
vector<OutputElTy, M> >::type
// clang-format on
MultiplyAdd(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
vector<InputElTy, K> Vec, VectorRef<BiasInterp, M> BiasRef) {
using BiasOutputVecTy = vector<OutputElTy, M>;
BiasOutputVecTy BiasVec =
BiasRef.Buf.template Load<BiasOutputVecTy>(BiasRef.Offset);

BiasOutputVecTy Result;
__builtin_LinAlg_MatrixVectorMultiplyAdd(Result, MatrixA.__handle,
hlsl::is_signed<OutputElTy>::value,
Vec, MatrixDT, BiasVec, BiasInterp);
return Result;
}

template <typename OutputElTy, typename InputElTy, ComponentEnum BiasElTy,
template <typename OutputElTy, typename InputElTy, ComponentEnum BiasInterp,
SIZE_TYPE M, SIZE_TYPE K, ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<hlsl::is_arithmetic<InputElTy>::value,
typename hlsl::enable_if<hlsl::is_arithmetic<InputElTy>::value &&
__detail::TypeTraits<OutputElTy>::CompType != BiasInterp,
vector<OutputElTy, M> >::type
// clang-format on
MultiplyAdd(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
vector<InputElTy, K> Vec, VectorRef<BiasElTy, M> BiasRef) {
vector<InputElTy, K> Vec, VectorRef<BiasInterp, M> BiasRef) {
using BiasVecTy =
vector<typename __detail::ComponentTypeTraits<BiasElTy>::Type, M>;
vector<typename __detail::ComponentTypeTraits<BiasInterp>::Type,
__detail::ScalarCountFromPackedComponents<BiasInterp, M>::Value>;
BiasVecTy BiasVec = BiasRef.Buf.template Load<BiasVecTy>(BiasRef.Offset);

vector<OutputElTy, M> BiasVecConv;
ComponentEnum OutputCompType = __detail::TypeTraits<OutputElTy>::CompType;
__builtin_LinAlg_Convert(BiasVecConv, BiasVec, BiasInterp, OutputCompType);

vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiplyAdd(Result, MatrixA.__handle,
hlsl::is_signed<OutputElTy>::value,
Vec, MatrixDT, BiasVec, BiasElTy);
__builtin_LinAlg_MatrixVectorMultiplyAdd(
Result, MatrixA.__handle, hlsl::is_signed<OutputElTy>::value, Vec,
__detail::TypeTraits<InputElTy>::CompType, BiasVecConv, OutputCompType);
return Result;
}

template <typename OutputElTy, typename InputElTy, ComponentEnum InputInterp,
ComponentEnum BiasElTy, SIZE_TYPE M, SIZE_TYPE VecK, SIZE_TYPE K,
ComponentEnum BiasInterp, SIZE_TYPE M, SIZE_TYPE VecK, SIZE_TYPE K,
ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<
VecK == __detail::ScalarCountFromPackedComponents<InputInterp, K>::Value,
VecK == __detail::ScalarCountFromPackedComponents<InputInterp, K>::Value &&
__detail::TypeTraits<OutputElTy>::CompType == BiasInterp,
vector<OutputElTy, M> >::type
// clang-format on
MultiplyAdd(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
InterpretedVector<InputElTy, VecK, InputInterp> InterpVec,
VectorRef<BiasElTy, M> BiasRef) {
VectorRef<BiasInterp, M> BiasRef) {
using BiasOutputVecTy = vector<OutputElTy, M>;
BiasOutputVecTy BiasVec =
BiasRef.Buf.template Load<BiasOutputVecTy>(BiasRef.Offset);

vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiplyAdd(
Result, MatrixA.__handle, hlsl::is_signed<OutputElTy>::value,
InterpVec.Data, InterpVec.Interpretation, BiasVec, BiasInterp);
return Result;
}

template <typename OutputElTy, typename InputElTy, ComponentEnum InputInterp,
ComponentEnum BiasInterp, SIZE_TYPE M, SIZE_TYPE VecK, SIZE_TYPE K,
ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<
VecK == __detail::ScalarCountFromPackedComponents<InputInterp, K>::Value &&
__detail::TypeTraits<OutputElTy>::CompType != BiasInterp,
vector<OutputElTy, M> >::type
// clang-format on
MultiplyAdd(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
InterpretedVector<InputElTy, VecK, InputInterp> InterpVec,
VectorRef<BiasInterp, M> BiasRef) {
using BiasVecTy =
vector<typename __detail::ComponentTypeTraits<BiasElTy>::Type, M>;
vector<typename __detail::ComponentTypeTraits<BiasInterp>::Type,
__detail::ScalarCountFromPackedComponents<BiasInterp, M>::Value>;
BiasVecTy BiasVec = BiasRef.Buf.template Load<BiasVecTy>(BiasRef.Offset);

ComponentEnum OutputCompType = __detail::TypeTraits<OutputElTy>::CompType;
vector<OutputElTy, M> BiasVecConv;
__builtin_LinAlg_Convert(BiasVecConv, BiasVec, BiasInterp, OutputCompType);

vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiplyAdd(
Result, MatrixA.__handle, hlsl::is_signed<OutputElTy>::value,
InterpVec.Data, InterpVec.Interpretation, BiasVec, BiasElTy);
InterpVec.Data, InterpVec.Interpretation, BiasVecConv, OutputCompType);
return Result;
}

Expand Down
Loading
Loading