[Mlir-commits] [mlir] 87d1986 - [mlir][spirv] Avoid copies of local APInt values in folds (NFC) (#221084)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Sep 7 13:57:24 PDT 2026


Author: Mehdi Amini
Date: 2026-09-07T22:57:19+02:00
New Revision: 87d1986bd2264b434e865044745725efbf52dda5

URL: https://github.com/llvm/llvm-project/commit/87d1986bd2264b434e865044745725efbf52dda5
DIFF: https://github.com/llvm/llvm-project/commit/87d1986bd2264b434e865044745725efbf52dda5.diff

LOG: [mlir][spirv] Avoid copies of local APInt values in folds (NFC) (#221084)

Move locally constructed APInt values into arithmetic operations at
their last use. Expand conditional returns where needed so returning a
local can use implicit move construction.

Found by Coverity.

Assisted-by: Codex

Added: 
    

Modified: 
    mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.cpp b/mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.cpp
index d73a87fcd72eb..5d31aef40088b 100644
--- a/mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.cpp
+++ b/mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.cpp
@@ -534,9 +534,11 @@ OpFoldResult spirv::SModOp::fold(FoldAdaptor adaptor) {
           return c;
         if (b.isNegative()) {
           APInt zero = APInt::getZero(c.getBitWidth());
-          return a.isNegative() ? (zero - c) : (b + c);
+          return a.isNegative() ? (std::move(zero) - c) : (b + std::move(c));
         }
-        return a.isNegative() ? (b - c) : c;
+        if (a.isNegative())
+          return b - std::move(c);
+        return c;
       });
   return div0OrOverflow ? Attribute() : res;
 }
@@ -642,7 +644,7 @@ OpFoldResult spirv::SNegateOp::fold(FoldAdaptor adaptor) {
   return constFoldUnaryOp<IntegerAttr>(
       adaptor.getOperands(), [](const APInt &a) {
         APInt zero = APInt::getZero(a.getBitWidth());
-        return zero - a;
+        return std::move(zero) - a;
       });
 }
 
@@ -745,11 +747,10 @@ OpFoldResult spirv::LogicalNotOp::fold(FoldAdaptor adaptor) {
   // According to the SPIR-V spec:
   //
   // Complement the bits of Operand.
-  return constFoldUnaryOp<IntegerAttr>(adaptor.getOperands(),
-                                       [](const APInt &a) {
-                                         APInt zero = APInt::getZero(1);
-                                         return a == 1 ? zero : (zero + 1);
-                                       });
+  return constFoldUnaryOp<IntegerAttr>(
+      adaptor.getOperands(), [](const APInt &a) {
+        return a == 1 ? APInt::getZero(1) : APInt::getAllOnes(1);
+      });
 }
 
 void spirv::LogicalNotOp::getCanonicalizationPatterns(


        


More information about the Mlir-commits mailing list