From 57f514d09d3b879e238f37980456634e8286691c Mon Sep 17 00:00:00 2001 From: kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> Date: Tue, 26 Mar 2024 11:05:00 -0700 Subject: Fix the sign-extending issue in right shift (#3820) Fix issue (#3637). In constant folding of a right shift operation,slang always uses signed interger as the operand no matter the input source code is signed or unsigned, this could causes sign-extending issue if the input source is unsigned integer with highest bit set to 1. Fix the issue by checking the original type of the input and use the unsigned type if the input is unsigned. --- source/slang/slang-ir-sccp.cpp | 12 ++++++++++++ source/slang/slang-ir.h | 1 + 2 files changed, 13 insertions(+) (limited to 'source') diff --git a/source/slang/slang-ir-sccp.cpp b/source/slang/slang-ir-sccp.cpp index fc3766e63..80cee8ecb 100644 --- a/source/slang/slang-ir-sccp.cpp +++ b/source/slang/slang-ir-sccp.cpp @@ -676,11 +676,23 @@ struct SCCPContext } LatticeVal evalLsh(IRType* type, LatticeVal v0, LatticeVal v1) { + IntInfo info = getIntTypeInfo(type); + if (info.isSigned == false) + { + return evalBinaryIntImpl( + type, v0, v1, [](IRUnsignedIntegerValue c0, IRUnsignedIntegerValue c1) { return c0 << c1; }); + } return evalBinaryIntImpl( type, v0, v1, [](IRIntegerValue c0, IRIntegerValue c1) { return c0 << c1; }); } LatticeVal evalRsh(IRType* type, LatticeVal v0, LatticeVal v1) { + IntInfo info = getIntTypeInfo(type); + if (info.isSigned == false) + { + return evalBinaryIntImpl( + type, v0, v1, [](IRUnsignedIntegerValue c0, IRUnsignedIntegerValue c1) { return c0 >> c1; }); + } return evalBinaryIntImpl( type, v0, v1, [](IRIntegerValue c0, IRIntegerValue c1) { return c0 >> c1; }); } diff --git a/source/slang/slang-ir.h b/source/slang/slang-ir.h index eb77127c8..36e145c01 100644 --- a/source/slang/slang-ir.h +++ b/source/slang/slang-ir.h @@ -1059,6 +1059,7 @@ void findAllInstsBreadthFirst(IRInst* inst, List& outInsts); // Constant Instructions typedef int64_t IRIntegerValue; +typedef uint64_t IRUnsignedIntegerValue; typedef double IRFloatingPointValue; struct IRConstant : IRInst -- cgit v1.2.3