[Mlir-commits] [mlir] [mlir][tosa] Add check for invalid tosa.rescale parameters (PR #96480)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Jun 24 05:38:11 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-tosa

Author: Tom Allsop (tom-arm)

<details>
<summary>Changes</summary>

* mlir::tosa::computeMultiplierAndShift returns a boolean, depending on validity of the shift value

---
Full diff: https://github.com/llvm/llvm-project/pull/96480.diff


2 Files Affected:

- (modified) mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h (+1-1) 
- (modified) mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp (+16-3) 


``````````diff
diff --git a/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h b/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h
index 298c97015fe2e..7fba62e9d002b 100644
--- a/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h
+++ b/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h
@@ -28,7 +28,7 @@ namespace tosa {
 
 /// From a scale value, computes multiplier and shift values
 /// for 16 or 32-bit scale widths.
-void computeMultiplierAndShift(double scale, int32_t &multiplier,
+bool computeMultiplierAndShift(double scale, int32_t &multiplier,
                                int32_t &shift, int32_t scaleWidth);
 
 //// Builds ConvOpQuantizationAttr from input and weight.
diff --git a/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp b/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp
index 5c546f59cde41..c318fe3240520 100644
--- a/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp
+++ b/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp
@@ -92,18 +92,31 @@ static void computeMultiplierAndShiftTosaScale32(double scale,
 }
 
 /// Generates a quantized multiplier/shift from double.
-void mlir::tosa::computeMultiplierAndShift(double scale, int32_t &multiplier,
+bool mlir::tosa::computeMultiplierAndShift(double scale, int32_t &multiplier,
                                            int32_t &shift, int32_t scaleWidth) {
 
   switch (scaleWidth) {
   case 16:
     computeMultiplierAndShiftTosaScale16(scale, multiplier, shift);
-    return;
+
+    // In some cases computeMultiplierAndShiftTosaScale16 can return
+    // a value less then 2, which is not valid in the TOSA spec.
+    if (shift < 2)
+      return false;
+    else
+      return true;
   case 32:
     computeMultiplierAndShiftTosaScale32(scale, multiplier, shift);
-    return;
+
+    // In some cases computeMultiplierAndShiftTosaScale32 can return
+    // a value less then 2, which is not valid in the TOSA spec.
+    if (shift < 2)
+      return false;
+    else
+      return true;
   default:
     assert(0 && "Unsupported Tosa quantized_scale regime specified!");
+    return false;
   }
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/96480


More information about the Mlir-commits mailing list