summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
authorkaizhangNV <149626564+kaizhangNV@users.noreply.github.com>2024-03-26 11:05:00 -0700
committerGitHub <noreply@github.com>2024-03-26 11:05:00 -0700
commit57f514d09d3b879e238f37980456634e8286691c (patch)
treef80b263d64373d66029a8d168c2df973de339cf7 /source
parent5c88619c2b8bf357fb727b4ceb781968adc23408 (diff)
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.
Diffstat (limited to 'source')
-rw-r--r--source/slang/slang-ir-sccp.cpp12
-rw-r--r--source/slang/slang-ir.h1
2 files changed, 13 insertions, 0 deletions
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<IRInst*>& outInsts);
// Constant Instructions
typedef int64_t IRIntegerValue;
+typedef uint64_t IRUnsignedIntegerValue;
typedef double IRFloatingPointValue;
struct IRConstant : IRInst