CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
175 lines
6.8 KiB
C++
175 lines
6.8 KiB
C++
// Copyright Advanced Micro Devices, Inc., or its affiliates.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include <rocRoller/Expression.hpp>
|
|
|
|
namespace rocRoller
|
|
{
|
|
namespace Expression
|
|
{
|
|
struct LowerBitfieldCombineExpressionVisitor
|
|
{
|
|
template <CUnary Expr>
|
|
ExpressionPtr operator()(Expr const& expr) const
|
|
{
|
|
Expr cpy = expr;
|
|
cpy.arg = call(expr.arg);
|
|
return std::make_shared<Expression>(cpy);
|
|
}
|
|
|
|
template <CBinary Expr>
|
|
ExpressionPtr operator()(Expr const& expr) const
|
|
{
|
|
Expr cpy = expr;
|
|
cpy.lhs = call(expr.lhs);
|
|
cpy.rhs = call(expr.rhs);
|
|
return std::make_shared<Expression>(cpy);
|
|
}
|
|
|
|
ExpressionPtr operator()(ScaledMatrixMultiply const& expr) const
|
|
{
|
|
ScaledMatrixMultiply cpy = expr;
|
|
cpy.matA = call(expr.matA);
|
|
cpy.matB = call(expr.matB);
|
|
cpy.matC = call(expr.matC);
|
|
cpy.scaleA = call(expr.scaleA);
|
|
cpy.scaleB = call(expr.scaleB);
|
|
return std::make_shared<Expression>(cpy);
|
|
}
|
|
|
|
template <CTernary Expr>
|
|
ExpressionPtr operator()(Expr const& expr) const
|
|
{
|
|
Expr cpy = expr;
|
|
cpy.lhs = call(expr.lhs);
|
|
cpy.r1hs = call(expr.r1hs);
|
|
cpy.r2hs = call(expr.r2hs);
|
|
return std::make_shared<Expression>(cpy);
|
|
}
|
|
|
|
template <CNary Expr>
|
|
ExpressionPtr operator()(Expr const& expr) const
|
|
{
|
|
auto cpy = expr;
|
|
std::ranges::for_each(cpy.operands, [this](auto& op) { op = call(op); });
|
|
return std::make_shared<Expression>(std::move(cpy));
|
|
}
|
|
|
|
ExpressionPtr operator()(BitfieldCombine const& expr) const
|
|
{
|
|
auto lhs = expr.lhs;
|
|
lhs = call(expr.lhs);
|
|
if(lhs)
|
|
{
|
|
AssertFatal(resultVariableType(lhs).getElementSize() <= 4u,
|
|
"Currently BitfieldCombine only supports: src size <= 1 dword");
|
|
AssertFatal(resultVariableType(lhs).getElementSize() * 8u
|
|
>= expr.srcOffset + expr.width,
|
|
"Bitfield exceeds the number of bits of source, source size "
|
|
"(bytes), offset, width = ",
|
|
ShowValue(resultVariableType(lhs).getElementSize()),
|
|
ShowValue(expr.srcOffset),
|
|
ShowValue(expr.width));
|
|
}
|
|
|
|
auto rhs = expr.rhs;
|
|
rhs = call(expr.rhs);
|
|
if(rhs)
|
|
{
|
|
AssertFatal(resultVariableType(rhs).getElementSize() == 4u,
|
|
"Currently BitfieldCombine only supports: dst size = 1 dword");
|
|
AssertFatal(resultVariableType(rhs).getElementSize() * 8u
|
|
>= expr.dstOffset + expr.width,
|
|
"Bitfield exceeds the number of bits of destination, destination "
|
|
"size (bytes), offset, width = ",
|
|
ShowValue(resultVariableType(rhs).getElementSize()),
|
|
ShowValue(expr.dstOffset),
|
|
ShowValue(expr.width));
|
|
}
|
|
|
|
AssertFatal(expr.width > 0,
|
|
"BitfieldCombine width must be greater than 0, width 0 should have "
|
|
"been optimized away by simplify");
|
|
AssertFatal(expr.width <= 32,
|
|
"BitfieldCombine width must be less than or equal to 32");
|
|
|
|
// Calculate width mask, handling width=32 case to avoid UB
|
|
uint32_t widthMask;
|
|
if(expr.width == 32)
|
|
{
|
|
widthMask = std::numeric_limits<uint32_t>::max();
|
|
}
|
|
else
|
|
{
|
|
widthMask = (1u << expr.width) - 1u;
|
|
}
|
|
|
|
auto const srcIsZero = expr.srcIsZero && expr.srcIsZero.value();
|
|
if(not srcIsZero)
|
|
{
|
|
rocRoller::Raw32 srcMask(widthMask << expr.srcOffset);
|
|
lhs = (literal(srcMask) & lhs); // Extract bits
|
|
}
|
|
|
|
auto const dstIsZero = expr.dstIsZero && expr.dstIsZero.value();
|
|
if(not dstIsZero)
|
|
{
|
|
rocRoller::Raw32 dstMask(~(widthMask << expr.dstOffset));
|
|
rhs = (literal(dstMask) & rhs); // Clear bits
|
|
}
|
|
|
|
if(expr.dstOffset > expr.srcOffset)
|
|
lhs = lhs << literal(expr.dstOffset - expr.srcOffset);
|
|
else if(expr.dstOffset < expr.srcOffset)
|
|
lhs = logicalShiftR(lhs, literal(expr.srcOffset - expr.dstOffset));
|
|
|
|
ExpressionPtr ret = lhs | rhs;
|
|
setComment(ret, expr.comment);
|
|
|
|
// Keep lowered expression type consistent with the original type
|
|
// Otherwise, a Raw32 expr may become a UInt32 when lowered
|
|
auto exprType = resultVariableType(expr);
|
|
auto retType = resultVariableType(ret);
|
|
if(retType != exprType)
|
|
{
|
|
AssertFatal(exprType.getElementSize() == retType.getElementSize(),
|
|
"Expression type size mismatch after lowering BitfieldCombine");
|
|
ret = reinterpret(exprType.dataType, ret);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
template <CValue Value>
|
|
ExpressionPtr operator()(Value const& expr) const
|
|
{
|
|
return std::make_shared<Expression>(expr);
|
|
}
|
|
|
|
ExpressionPtr call(ExpressionPtr expr) const
|
|
{
|
|
if(!expr)
|
|
return expr;
|
|
|
|
return std::visit(*this, *expr);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Replace a BitfieldCombine expression with:
|
|
*
|
|
* srcMask = ((1 << width) - 1) << srcOffset
|
|
* dstMask = ~(((1 << width) - 1) << dstOffset)
|
|
* dst = shift((srcMask & src), abs(srcOffset-dstOffset)) | (dstMask & dst)
|
|
*
|
|
* Note: src=lhs, dst=rhs
|
|
*/
|
|
ExpressionPtr lowerBitfieldCombine(ExpressionPtr expr)
|
|
{
|
|
auto visitor = LowerBitfieldCombineExpressionVisitor();
|
|
return visitor.call(expr);
|
|
}
|
|
|
|
}
|
|
}
|