https://github.com/RKSimon updated https://github.com/llvm/llvm-project/pull/175603
>From d227f56493efdf2171ee145208ee4bcac1e8e7a8 Mon Sep 17 00:00:00 2001 From: Simon Pilgrim <[email protected]> Date: Mon, 12 Jan 2026 18:18:42 +0000 Subject: [PATCH 1/3] [ADT] Add APInt::truncSSatU truncation from signed to unsigned variant This matches the behaviour of ISD::TRUNCATE_SSAT_U and X86ISD::PACKUS truncations (saturate signed input to unsigned result - truncate(smin(smax(x, 0), C))) Add unit test coverage and update existing PACKUS constant folding to use the APInt helper --- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 7 +------ clang/lib/AST/ExprConstant.cpp | 7 +------ llvm/include/llvm/ADT/APInt.h | 9 ++++++++- llvm/lib/Support/APInt.cpp | 13 ++++++++++++- llvm/lib/Target/X86/X86ISelLowering.cpp | 7 +------ llvm/unittests/ADT/APIntTest.cpp | 10 ++++++++++ 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index c6d4a9d63b383..37ab6a1fe99e6 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -4816,12 +4816,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, case clang::X86::BI__builtin_ia32_packuswb256: case clang::X86::BI__builtin_ia32_packuswb512: return interp__builtin_x86_pack(S, OpPC, Call, [](const APSInt &Src) { - unsigned DstBits = Src.getBitWidth() / 2; - if (Src.isNegative()) - return APInt::getZero(DstBits); - if (Src.isIntN(DstBits)) - return APInt(Src).trunc(DstBits); - return APInt::getAllOnes(DstBits); + return APInt(Src).truncSSatU(Src.getBitWidth() / 2); }); case clang::X86::BI__builtin_ia32_selectss_128: diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index ec7ca893fb50e..c91261988434e 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -12665,12 +12665,7 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { case X86::BI__builtin_ia32_packuswb256: case X86::BI__builtin_ia32_packuswb512: return evalPackBuiltin(E, Info, Result, [](const APSInt &Src) { - unsigned DstBits = Src.getBitWidth() / 2; - if (Src.isNegative()) - return APInt::getZero(DstBits); - if (Src.isIntN(DstBits)) - return APInt((Src).trunc(DstBits)); - return APInt::getAllOnes(DstBits); + return APSInt(Src).truncSSatU(Src.getBitWidth() / 2); }); case clang::X86::BI__builtin_ia32_selectss_128: return EvalSelectScalar(4); diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h index 7e73cc1957c05..9a786ee178ee6 100644 --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -1278,13 +1278,20 @@ class [[nodiscard]] APInt { /// the new bitwidth, then return truncated APInt. Else, return max value. LLVM_ABI APInt truncUSat(unsigned width) const; - /// Truncate to new width with signed saturation. + /// Truncate to new width with signed saturation to signed result. /// /// If this APInt, treated as signed integer, can be losslessly truncated to /// the new bitwidth, then return truncated APInt. Else, return either /// signed min value if the APInt was negative, or signed max value. LLVM_ABI APInt truncSSat(unsigned width) const; + /// Truncate to new width with signed saturation to unsigned result. + /// + /// If this APInt, treated as unsigned integer, can be losslessly truncated to + /// the new bitwidth, then return truncated APInt. Else, return either + /// zero if the APInt was negative, or unsigned max value. + LLVM_ABI APInt truncSSatU(unsigned width) const; + /// Sign extend to a new width. /// /// This operation sign extends the APInt to a new width. If the high order diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp index 673cd867f0e45..c35abf725dbe0 100644 --- a/llvm/lib/Support/APInt.cpp +++ b/llvm/lib/Support/APInt.cpp @@ -969,7 +969,7 @@ APInt APInt::truncUSat(unsigned width) const { return APInt::getMaxValue(width); } -// Truncate to new width with signed saturation. +// Truncate to new width with signed saturation to signed result. APInt APInt::truncSSat(unsigned width) const { assert(width <= BitWidth && "Invalid APInt Truncate request"); @@ -981,6 +981,17 @@ APInt APInt::truncSSat(unsigned width) const { : APInt::getSignedMaxValue(width); } +// Truncate to new width with signed saturation to unsigned result. +APInt APInt::truncSSatU(unsigned width) const { + assert(width <= BitWidth && "Invalid APInt Truncate request"); + + // Can we just losslessly truncate it? + if (isIntN(width)) + return trunc(width); + // If not, then just return the new limits. + return isNegative() ? APInt::getZero(width) : APInt::getMaxValue(width); +} + // Sign extend to a new width. APInt APInt::sext(unsigned Width) const { assert(Width >= BitWidth && "Invalid APInt SignExtend request"); diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp index a354704c5958b..33f303c19d72f 100644 --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -50708,12 +50708,7 @@ static SDValue combineVectorPack(SDNode *N, SelectionDAG &DAG, // Source values less than zero are saturated to zero. // Source values greater than dst maxuint are saturated to maxuint. // NOTE: This is different from APInt::truncUSat. - if (Val.isIntN(DstBitsPerElt)) - Val = Val.trunc(DstBitsPerElt); - else if (Val.isNegative()) - Val = APInt::getZero(DstBitsPerElt); - else - Val = APInt::getAllOnes(DstBitsPerElt); + Val = Val.truncSSatU(DstBitsPerElt); } Bits[Lane * NumDstEltsPerLane + Elt] = Val; } diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp index 4cb537da72e87..271d17cb29905 100644 --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -1269,11 +1269,21 @@ TEST(APIntTest, SaturatingMath) { EXPECT_EQ(APInt(6, 31), AP_42.truncSSat(6)); EXPECT_EQ(APInt(5, 15), AP_42.truncSSat(5)); + EXPECT_EQ(APInt(8, 42, false), AP_42.truncSSatU(8)); + EXPECT_EQ(APInt(7, 42, false), AP_42.truncSSatU(7)); + EXPECT_EQ(APInt(6, 42, false), AP_42.truncSSatU(6)); + EXPECT_EQ(APInt(5, 31, false), AP_42.truncSSatU(5)); + EXPECT_EQ(APInt(8, -56, true), AP_200.truncSSat(8)); EXPECT_EQ(APInt(7, -56, true), AP_200.truncSSat(7)); EXPECT_EQ(APInt(6, -32, true), AP_200.truncSSat(6)); EXPECT_EQ(APInt(5, -16, true), AP_200.truncSSat(5)); + EXPECT_EQ(APInt(8, 200, false), AP_200.truncSSatU(8)); + EXPECT_EQ(APInt(7, 0, false), AP_200.truncSSatU(7)); + EXPECT_EQ(APInt(6, 0, false), AP_200.truncSSatU(6)); + EXPECT_EQ(APInt(5, 0, false), AP_200.truncSSatU(5)); + EXPECT_EQ(APInt(8, 200), AP_100.uadd_sat(AP_100)); EXPECT_EQ(APInt(8, 255), AP_100.uadd_sat(AP_200)); EXPECT_EQ(APInt(8, 255), APInt(8, 255).uadd_sat(APInt(8, 255))); >From 201cc22b62716ef1067c6b4e89b588043ef3f86c Mon Sep 17 00:00:00 2001 From: Simon Pilgrim <[email protected]> Date: Tue, 13 Jan 2026 11:33:07 +0000 Subject: [PATCH 2/3] Fix doxygen description --- llvm/include/llvm/ADT/APInt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h index 9a786ee178ee6..50404a1a1321e 100644 --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -1287,7 +1287,7 @@ class [[nodiscard]] APInt { /// Truncate to new width with signed saturation to unsigned result. /// - /// If this APInt, treated as unsigned integer, can be losslessly truncated to + /// If this APInt, treated as signed integer, can be losslessly truncated to /// the new bitwidth, then return truncated APInt. Else, return either /// zero if the APInt was negative, or unsigned max value. LLVM_ABI APInt truncSSatU(unsigned width) const; >From 29040f359f97ba82154f319d2ba1ab832a083dba Mon Sep 17 00:00:00 2001 From: Simon Pilgrim <[email protected]> Date: Tue, 13 Jan 2026 15:12:11 +0000 Subject: [PATCH 3/3] Add note that no changes are made if the truncated width is the same size --- llvm/include/llvm/ADT/APInt.h | 1 + 1 file changed, 1 insertion(+) diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h index 50404a1a1321e..9193b5f8994e0 100644 --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -1290,6 +1290,7 @@ class [[nodiscard]] APInt { /// If this APInt, treated as signed integer, can be losslessly truncated to /// the new bitwidth, then return truncated APInt. Else, return either /// zero if the APInt was negative, or unsigned max value. + /// If \p width matches the current bit width then no changes are made. LLVM_ABI APInt truncSSatU(unsigned width) const; /// Sign extend to a new width. _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
