From 3494ee95902cef62f767489802e469c58a13ea04 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 1 Nov 2024 16:23:39 +0100 Subject: [PATCH 1/2] Reapply [APInt] Enable APInt ctor assertion by default (#114539) This enables the assertion introduced in https://github.com/llvm/llvm-project/pull/106524, which checks that the value passed to the constructor is indeed a valid N-bit signed or unsigned integer. Places that previously violated the assertion were updated in advance, e.g. in https://github.com/llvm/llvm-project/pull/80309. It is possible to opt-out of the check and restore the previous behavior by setting implicitTrunc=true. ----- The buildbot failures from the previous attempt should be fixed by a18dd29077c84fc076a4ed431d9e815a3d0b6f24 and e2074c60bb3982cd8afb6408670332ea27da6383. --- 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 63a138527b32e..953b2a27b7152 100644 --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -109,7 +109,7 @@ class [[nodiscard]] APInt { /// \param implicitTrunc allow implicit truncation of non-zero/sign bits of /// val beyond the range of numBits APInt(unsigned numBits, uint64_t val, bool isSigned = false, - bool implicitTrunc = true) + bool implicitTrunc = false) : BitWidth(numBits) { if (!implicitTrunc) { if (isSigned) { From 6fc95f2c42867846d038d4933f6ff3e63e8d47bd Mon Sep 17 00:00:00 2001 From: Jonas Rickert Date: Mon, 3 Feb 2025 07:56:40 -0700 Subject: [PATCH 2/2] Fix asserts in APInt contructor --- mlir/lib/Dialect/PDL/IR/Builtins.cpp | 3 ++- mlir/lib/Dialect/Tosa/Transforms/TosaFolders.cpp | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/mlir/lib/Dialect/PDL/IR/Builtins.cpp b/mlir/lib/Dialect/PDL/IR/Builtins.cpp index 770a390a3fe5f..9e4efbf7e71c0 100644 --- a/mlir/lib/Dialect/PDL/IR/Builtins.cpp +++ b/mlir/lib/Dialect/PDL/IR/Builtins.cpp @@ -63,7 +63,8 @@ LogicalResult static unaryOp(PatternRewriter &rewriter, PDLResultList &results, ? std::pow(2, operandIntAttr.getValue().getZExtValue()) : std::pow(2, operandIntAttr.getValue().getSExtValue()); - APInt resultInt(bitWidth, resultVal, integerType.isSigned()); + APInt resultInt(bitWidth, resultVal, integerType.isSigned(), + /*implicitTrunc*/ true); bool isOverflow = integerType.isSigned() ? resultInt.slt(operandIntAttr.getValue()) diff --git a/mlir/lib/Dialect/Tosa/Transforms/TosaFolders.cpp b/mlir/lib/Dialect/Tosa/Transforms/TosaFolders.cpp index 63594d83ccc11..ea6f295ff2feb 100644 --- a/mlir/lib/Dialect/Tosa/Transforms/TosaFolders.cpp +++ b/mlir/lib/Dialect/Tosa/Transforms/TosaFolders.cpp @@ -1540,9 +1540,11 @@ struct TosaFoldConstantMatMul // Convert int64_t to the correct output type. std::vector apintValues; - llvm::transform(values, std::back_inserter(apintValues), - [&](const int64_t &val) { - APInt apIntVal(baseType.getIntOrFloatBitWidth(), val); + llvm::transform( + values, std::back_inserter(apintValues), [&](const int64_t &val) { + APInt apIntVal(baseType.getIntOrFloatBitWidth(), val, + /*isSigned=*/true); // tosa-mlir uses signless + // instead of signed return apIntVal; }); return DenseElementsAttr::get(outputType, apintValues);