[clang] [libcxx] [flang] [llvm] [mlir] [lldb] [libc] [mlir] Skip invalid test on big endian platform (s390x) (PR #80246)

2024-02-01 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe updated 
https://github.com/llvm/llvm-project/pull/80246

>From e4bfaa1c07a96b9c78bb64368f5b65c49feb4bd7 Mon Sep 17 00:00:00 2001
From: Kai Sasaki 
Date: Thu, 1 Feb 2024 15:33:00 +0900
Subject: [PATCH 1/2] [mlir] Skip invalid test on big endian platform (s390x)

---
 mlir/test/Target/LLVMIR/llvmir.mlir | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir 
b/mlir/test/Target/LLVMIR/llvmir.mlir
index 448aa3a5d85d7..816f6dd03aad7 100644
--- a/mlir/test/Target/LLVMIR/llvmir.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir.mlir
@@ -1,5 +1,8 @@
 // RUN: mlir-translate -mlir-to-llvmir -split-input-file %s | FileCheck %s
 
+// Decoding the attribute does not work on big-endian platforms currently
+// XFAIL: target=s390x-{{.*}}
+
 // CHECK: @global_aligned32 = private global i64 42, align 32
 "llvm.mlir.global"() ({}) {sym_name = "global_aligned32", global_type = i64, 
value = 42 : i64, linkage = #llvm.linkage, alignment = 32} : () -> ()
 

>From 5a468695bf30aa31b9f85f5816b71d3fdde0595a Mon Sep 17 00:00:00 2001
From: Kai Sasaki 
Date: Thu, 1 Feb 2024 20:08:15 +0900
Subject: [PATCH 2/2] [mlir] Split the LE specific test cases

---
 .../Target/LLVMIR/llvmir-le-specific.mlir | 27 +++
 mlir/test/Target/LLVMIR/llvmir.mlir   | 26 --
 2 files changed, 27 insertions(+), 26 deletions(-)
 create mode 100644 mlir/test/Target/LLVMIR/llvmir-le-specific.mlir

diff --git a/mlir/test/Target/LLVMIR/llvmir-le-specific.mlir 
b/mlir/test/Target/LLVMIR/llvmir-le-specific.mlir
new file mode 100644
index 0..f8d082082117c
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/llvmir-le-specific.mlir
@@ -0,0 +1,27 @@
+// RUN: mlir-translate -mlir-to-llvmir -split-input-file %s | FileCheck %s
+
+// Decoding the attribute does not work on big-endian platforms currently
+// XFAIL: target=s390x-{{.*}}
+
+// CHECK{LITERAL}: @dense_resource_tensor_constant = internal constant [5 x 
float] [float 0x3FCA03408000, float 0xBFD04663, float 
0xBFD75DDF8000, float 0xBFDE074F4000, float 0x3FDDD3A1C000]
+llvm.mlir.global internal constant 
@dense_resource_tensor_constant(dense_resource : 
tensor<5xf32>) : !llvm.array<5 x f32>
+
+// CHECK{LITERAL}: @dense_resource_vector_constant = internal constant <5 x 
float> 
+llvm.mlir.global internal constant 
@dense_resource_vector_constant(dense_resource : 
vector<5xf32>) : vector<5xf32>
+
+
+// CHECK{LITERAL}: @dense_resource_multidim_tensor_constant = internal 
constant [1 x [2 x [2 x float]]] [[2 x [2 x float]] [[2 x float] [float 
0x3FD6B46A8000, float 0x3FD6781AC000], [2 x float] [float 
0xBFB45A2AA000, float 0x3FD77A5CA000]]]
+llvm.mlir.global internal constant 
@dense_resource_multidim_tensor_constant(dense_resource
 : tensor<1x2x2xf32>) : !llvm.array<1 x !llvm.array<2 x !llvm.array<2 x f32>>>
+
+// CHECK{LITERAL}: @dense_resource_multidim_vector_constant = internal 
constant [1 x [2 x <2 x float>]] [[2 x <2 x float>] [<2 x float> , <2 x float> ]]
+llvm.mlir.global internal constant 
@dense_resource_multidim_vector_constant(dense_resource
 : vector<1x2x2xf32>) : !llvm.array<1 x !llvm.array<2 x vector<2 x f32>>>
+
+// Resources are kept at end of file. New tests should be added above this.
+{-#
+  dialect_resources: {
+builtin: {
+  dense_resource_test_5xf32: 
"0x0800041A503E183382BEFCEEBABE7A3AF0BE0E9DEE3E",
+  dense_resource_test_2x2xf32: "0x080054A3B53ED6C0B33E55D1A2BDE5D2BB3E"
+}
+  }
+#-}
\ No newline at end of file
diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir 
b/mlir/test/Target/LLVMIR/llvmir.mlir
index 816f6dd03aad7..961c948444684 100644
--- a/mlir/test/Target/LLVMIR/llvmir.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir.mlir
@@ -1,8 +1,5 @@
 // RUN: mlir-translate -mlir-to-llvmir -split-input-file %s | FileCheck %s
 
-// Decoding the attribute does not work on big-endian platforms currently
-// XFAIL: target=s390x-{{.*}}
-
 // CHECK: @global_aligned32 = private global i64 42, align 32
 "llvm.mlir.global"() ({}) {sym_name = "global_aligned32", global_type = i64, 
value = 42 : i64, linkage = #llvm.linkage, alignment = 32} : () -> ()
 
@@ -104,19 +101,6 @@ llvm.mlir.global internal 
@dense_float_vector_3d(dense<[[[1.0, 2.0], [3.0, 4.0]]
 // CHECK{LITERAL}: @splat_float_vector_3d = internal global [2 x [2 x <2 x 
float>]] [[2 x <2 x float>] [<2 x float> , <2 x float> ], [2 x <2 x 
float>] [<2 x float> , <2 x float> 
]]
 llvm.mlir.global internal @splat_float_vector_3d(dense<42.0> : 
vector<2x2x2xf32>) : !llvm.array<2 x !llvm.array<2 x vector<2xf32>>>
 
-// CHECK{LITERAL}: @dense_resource_tensor_constant = internal constant [5 x 
float] [float 0x3FCA03408000, float 0xBFD04663, float 
0xBFD75DDF8000, float 0xBFDE074F4000, float 0x3FDDD3A1C000]
-llvm.mlir.global internal constant 
@dense_resource_tensor_constant(dense_resource : 
tensor<5xf32>) : !llvm.array<5 x f32>
-
-// CHECK{LITERAL}: 

[llvm] [clang] [flang] [mlir] [mlir][complex] Prevent underflow in complex.abs (PR #79786)

2024-01-30 Thread Kai Sasaki via cfe-commits

Lewuathe wrote:

The build which previously failed is now finished successfully.
https://lab.llvm.org/buildbot/#/builders/264/builds/6256

https://github.com/llvm/llvm-project/pull/79786
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [clang] [mlir] [flang] [mlir][complex] Prevent underflow in complex.abs (PR #79786)

2024-01-30 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe closed 
https://github.com/llvm/llvm-project/pull/79786
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [mlir] [flang] [llvm] [mlir][complex] Prevent underflow in complex.abs (PR #79786)

2024-01-29 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe commented:

@joker-eph @matthias-springer I've fixed a bug in the previous PR that caused 
the integration test failure in the previous change. Could you review this 
change when you get a chance?

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

https://github.com/llvm/llvm-project/pull/79786
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[mlir] [flang] [clang] [llvm] [mlir][complex] Prevent underflow in complex.abs (PR #79786)

2024-01-28 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe updated 
https://github.com/llvm/llvm-project/pull/79786

>From 8a7243c4c2be5db5e0a95535f36386557e68e18c Mon Sep 17 00:00:00 2001
From: Kai Sasaki 
Date: Fri, 15 Dec 2023 15:53:54 +0900
Subject: [PATCH] [mlir][complex] Prevent underflow in complex.abs

---
 .../ComplexToStandard/ComplexToStandard.cpp   |  58 ++--
 .../convert-to-standard.mlir  | 125 +++---
 .../ComplexToStandard/full-conversion.mlir|  27 +++-
 .../Dialect/Complex/CPU/correctness.mlir  |  48 +++
 4 files changed, 218 insertions(+), 40 deletions(-)

diff --git a/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp 
b/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
index 4c9dad9e2c17312..6e0eddab0e3aae0 100644
--- a/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
+++ b/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
@@ -26,29 +26,59 @@ namespace mlir {
 using namespace mlir;
 
 namespace {
+// The algorithm is listed in https://dl.acm.org/doi/pdf/10.1145/363717.363780.
 struct AbsOpConversion : public OpConversionPattern {
   using OpConversionPattern::OpConversionPattern;
 
   LogicalResult
   matchAndRewrite(complex::AbsOp op, OpAdaptor adaptor,
   ConversionPatternRewriter ) const override {
-auto loc = op.getLoc();
-auto type = op.getType();
+mlir::ImplicitLocOpBuilder b(op.getLoc(), rewriter);
 
 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
 
-Value real =
-rewriter.create(loc, type, adaptor.getComplex());
-Value imag =
-rewriter.create(loc, type, adaptor.getComplex());
-Value realSqr =
-rewriter.create(loc, real, real, fmf.getValue());
-Value imagSqr =
-rewriter.create(loc, imag, imag, fmf.getValue());
-Value sqNorm =
-rewriter.create(loc, realSqr, imagSqr, fmf.getValue());
-
-rewriter.replaceOpWithNewOp(op, sqNorm);
+Type elementType = op.getType();
+Value arg = adaptor.getComplex();
+
+Value zero =
+b.create(elementType, b.getZeroAttr(elementType));
+Value one = b.create(elementType,
+b.getFloatAttr(elementType, 1.0));
+
+Value real = b.create(elementType, arg);
+Value imag = b.create(elementType, arg);
+
+Value realIsZero =
+b.create(arith::CmpFPredicate::OEQ, real, zero);
+Value imagIsZero =
+b.create(arith::CmpFPredicate::OEQ, imag, zero);
+
+// Real > Imag
+Value imagDivReal = b.create(imag, real, fmf.getValue());
+Value imagSq =
+b.create(imagDivReal, imagDivReal, fmf.getValue());
+Value imagSqPlusOne = b.create(imagSq, one, fmf.getValue());
+Value imagSqrt = b.create(imagSqPlusOne, fmf.getValue());
+Value realAbs = b.create(real, fmf.getValue());
+Value absImag = b.create(imagSqrt, realAbs, fmf.getValue());
+
+// Real <= Imag
+Value realDivImag = b.create(real, imag, fmf.getValue());
+Value realSq =
+b.create(realDivImag, realDivImag, fmf.getValue());
+Value realSqPlusOne = b.create(realSq, one, fmf.getValue());
+Value realSqrt = b.create(realSqPlusOne, fmf.getValue());
+Value imagAbs = b.create(imag, fmf.getValue());
+Value absReal = b.create(realSqrt, imagAbs, fmf.getValue());
+
+rewriter.replaceOpWithNewOp(
+op, realIsZero, imag,
+b.create(
+imagIsZero, real,
+b.create(
+b.create(arith::CmpFPredicate::OGT, real, imag),
+absImag, absReal)));
+
 return success();
   }
 };
diff --git a/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir 
b/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
index 8fa29ea43854a4b..de519f6a706ab03 100644
--- a/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
+++ b/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
@@ -7,13 +7,30 @@ func.func @complex_abs(%arg: complex) -> f32 {
   %abs = complex.abs %arg: complex
   return %abs : f32
 }
+
+// CHECK: %[[ZERO:.*]] = arith.constant 0.00e+00 : f32
+// CHECK: %[[ONE:.*]] = arith.constant 1.00e+00 : f32
 // CHECK: %[[REAL:.*]] = complex.re %[[ARG]] : complex
 // CHECK: %[[IMAG:.*]] = complex.im %[[ARG]] : complex
-// CHECK-DAG: %[[REAL_SQ:.*]] = arith.mulf %[[REAL]], %[[REAL]] : f32
-// CHECK-DAG: %[[IMAG_SQ:.*]] = arith.mulf %[[IMAG]], %[[IMAG]] : f32
-// CHECK: %[[SQ_NORM:.*]] = arith.addf %[[REAL_SQ]], %[[IMAG_SQ]] : f32
-// CHECK: %[[NORM:.*]] = math.sqrt %[[SQ_NORM]] : f32
-// CHECK: return %[[NORM]] : f32
+// CHECK: %[[IS_REAL_ZERO:.*]] = arith.cmpf oeq, %[[REAL]], %[[ZERO]] : f32
+// CHECK: %[[IS_IMAG_ZERO:.*]] = arith.cmpf oeq, %[[IMAG]], %[[ZERO]] : f32
+// CHECK: %[[IMAG_DIV_REAL:.*]] = arith.divf %[[IMAG]], %[[REAL]] : f32
+// CHECK: %[[IMAG_SQ:.*]] = arith.mulf %[[IMAG_DIV_REAL]], %[[IMAG_DIV_REAL]] 
: f32
+// CHECK: %[[IMAG_SQ_PLUS_ONE:.*]] = arith.addf %[[IMAG_SQ]], %[[ONE]] : f32
+// CHECK: %[[IMAG_SQRT:.*]] = 

[flang] [mlir] [lldb] [libcxxabi] [llvm] [clang-tools-extra] [libcxx] [libc] [clang] [lld] [compiler-rt] [mlir][complex] Prevent underflow in complex.abs (PR #76316)

2024-01-28 Thread Kai Sasaki via cfe-commits

Lewuathe wrote:

@joker-eph Oh, sorry for bothering you. I'll check what's going on.

https://github.com/llvm/llvm-project/pull/76316
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[flang] [compiler-rt] [llvm] [clang-tools-extra] [clang] [lld] [libcxxabi] [lldb] [mlir] [libcxx] [libc] [mlir][complex] Prevent underflow in complex.abs (PR #76316)

2024-01-26 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe closed 
https://github.com/llvm/llvm-project/pull/76316
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libcxx] [mlir] [clang] [libcxxabi] [lld] [clang-tools-extra] [libc] [llvm] [flang] [lldb] [compiler-rt] [mlir][complex] Prevent underflow in complex.abs (PR #76316)

2024-01-26 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe updated 
https://github.com/llvm/llvm-project/pull/76316

>From a5810363e546da073543cb2d62cceb956c46b2e6 Mon Sep 17 00:00:00 2001
From: Kai Sasaki 
Date: Fri, 15 Dec 2023 15:53:54 +0900
Subject: [PATCH 1/2] [mlir][complex] Prevent underflow in complex.abs

---
 .../ComplexToStandard/ComplexToStandard.cpp   |  56 ++---
 .../convert-to-standard.mlir  | 115 ++
 .../ComplexToStandard/full-conversion.mlir|  25 +++-
 .../Dialect/Complex/CPU/correctness.mlir  |  38 ++
 4 files changed, 194 insertions(+), 40 deletions(-)

diff --git a/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp 
b/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
index bf753c7062f3664..7c1db57b55f996b 100644
--- a/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
+++ b/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
@@ -26,29 +26,57 @@ namespace mlir {
 using namespace mlir;
 
 namespace {
+// The algorithm is listed in https://dl.acm.org/doi/pdf/10.1145/363717.363780.
 struct AbsOpConversion : public OpConversionPattern {
   using OpConversionPattern::OpConversionPattern;
 
   LogicalResult
   matchAndRewrite(complex::AbsOp op, OpAdaptor adaptor,
   ConversionPatternRewriter ) const override {
-auto loc = op.getLoc();
-auto type = op.getType();
+mlir::ImplicitLocOpBuilder b(op.getLoc(), rewriter);
 
 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
 
-Value real =
-rewriter.create(loc, type, adaptor.getComplex());
-Value imag =
-rewriter.create(loc, type, adaptor.getComplex());
-Value realSqr =
-rewriter.create(loc, real, real, fmf.getValue());
-Value imagSqr =
-rewriter.create(loc, imag, imag, fmf.getValue());
-Value sqNorm =
-rewriter.create(loc, realSqr, imagSqr, fmf.getValue());
-
-rewriter.replaceOpWithNewOp(op, sqNorm);
+Type elementType = op.getType();
+Value arg = adaptor.getComplex();
+
+Value zero =
+b.create(elementType, b.getZeroAttr(elementType));
+Value one = b.create(elementType,
+b.getFloatAttr(elementType, 1.0));
+
+Value real = b.create(elementType, arg);
+Value imag = b.create(elementType, arg);
+
+Value realIsZero =
+b.create(arith::CmpFPredicate::OEQ, real, zero);
+Value imagIsZero =
+b.create(arith::CmpFPredicate::OEQ, imag, zero);
+
+// Real > Imag
+Value imagDivReal = b.create(imag, real, fmf.getValue());
+Value imagSq =
+b.create(imagDivReal, imagDivReal, fmf.getValue());
+Value imagSqPlusOne = b.create(imagSq, one, fmf.getValue());
+Value imagSqrt = b.create(imagSqPlusOne, fmf.getValue());
+Value absImag = b.create(imagSqrt, real, fmf.getValue());
+
+// Real <= Imag
+Value realDivImag = b.create(real, imag, fmf.getValue());
+Value realSq =
+b.create(realDivImag, realDivImag, fmf.getValue());
+Value realSqPlusOne = b.create(realSq, one, fmf.getValue());
+Value realSqrt = b.create(realSqPlusOne, fmf.getValue());
+Value absReal = b.create(realSqrt, imag, fmf.getValue());
+
+rewriter.replaceOpWithNewOp(
+op, realIsZero, imag,
+b.create(
+imagIsZero, real,
+b.create(
+b.create(arith::CmpFPredicate::OGT, real, imag),
+absImag, absReal)));
+
 return success();
   }
 };
diff --git a/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir 
b/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
index 3af28150fd5c3f3..1028c9aae92c056 100644
--- a/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
+++ b/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
@@ -7,13 +7,28 @@ func.func @complex_abs(%arg: complex) -> f32 {
   %abs = complex.abs %arg: complex
   return %abs : f32
 }
+
+// CHECK: %[[ZERO:.*]] = arith.constant 0.00e+00 : f32
+// CHECK: %[[ONE:.*]] = arith.constant 1.00e+00 : f32
 // CHECK: %[[REAL:.*]] = complex.re %[[ARG]] : complex
 // CHECK: %[[IMAG:.*]] = complex.im %[[ARG]] : complex
-// CHECK-DAG: %[[REAL_SQ:.*]] = arith.mulf %[[REAL]], %[[REAL]] : f32
-// CHECK-DAG: %[[IMAG_SQ:.*]] = arith.mulf %[[IMAG]], %[[IMAG]] : f32
-// CHECK: %[[SQ_NORM:.*]] = arith.addf %[[REAL_SQ]], %[[IMAG_SQ]] : f32
-// CHECK: %[[NORM:.*]] = math.sqrt %[[SQ_NORM]] : f32
-// CHECK: return %[[NORM]] : f32
+// CHECK: %[[IS_REAL_ZERO:.*]] = arith.cmpf oeq, %[[REAL]], %[[ZERO]] : f32
+// CHECK: %[[IS_IMAG_ZERO:.*]] = arith.cmpf oeq, %[[IMAG]], %[[ZERO]] : f32
+// CHECK: %[[IMAG_DIV_REAL:.*]] = arith.divf %[[IMAG]], %[[REAL]] : f32
+// CHECK: %[[IMAG_SQ:.*]] = arith.mulf %[[IMAG_DIV_REAL]], %[[IMAG_DIV_REAL]] 
: f32
+// CHECK: %[[IMAG_SQ_PLUS_ONE:.*]] = arith.addf %[[IMAG_SQ]], %[[ONE]] : f32
+// CHECK: %[[IMAG_SQRT:.*]] = math.sqrt %[[IMAG_SQ_PLUS_ONE]] : f32
+// CHECK: %[[ABS_IMAG:.*]] = arith.mulf %[[IMAG_SQRT]], %[[REAL]] : 

[clang] [libcxxabi] [flang] [clang-tools-extra] [compiler-rt] [mlir] [lldb] [llvm] [mlir][complex] Prevent underflow in complex.abs (PR #76316)

2024-01-25 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe updated 
https://github.com/llvm/llvm-project/pull/76316

>From a5810363e546da073543cb2d62cceb956c46b2e6 Mon Sep 17 00:00:00 2001
From: Kai Sasaki 
Date: Fri, 15 Dec 2023 15:53:54 +0900
Subject: [PATCH 1/2] [mlir][complex] Prevent underflow in complex.abs

---
 .../ComplexToStandard/ComplexToStandard.cpp   |  56 ++---
 .../convert-to-standard.mlir  | 115 ++
 .../ComplexToStandard/full-conversion.mlir|  25 +++-
 .../Dialect/Complex/CPU/correctness.mlir  |  38 ++
 4 files changed, 194 insertions(+), 40 deletions(-)

diff --git a/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp 
b/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
index bf753c7062f366..7c1db57b55f996 100644
--- a/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
+++ b/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
@@ -26,29 +26,57 @@ namespace mlir {
 using namespace mlir;
 
 namespace {
+// The algorithm is listed in https://dl.acm.org/doi/pdf/10.1145/363717.363780.
 struct AbsOpConversion : public OpConversionPattern {
   using OpConversionPattern::OpConversionPattern;
 
   LogicalResult
   matchAndRewrite(complex::AbsOp op, OpAdaptor adaptor,
   ConversionPatternRewriter ) const override {
-auto loc = op.getLoc();
-auto type = op.getType();
+mlir::ImplicitLocOpBuilder b(op.getLoc(), rewriter);
 
 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
 
-Value real =
-rewriter.create(loc, type, adaptor.getComplex());
-Value imag =
-rewriter.create(loc, type, adaptor.getComplex());
-Value realSqr =
-rewriter.create(loc, real, real, fmf.getValue());
-Value imagSqr =
-rewriter.create(loc, imag, imag, fmf.getValue());
-Value sqNorm =
-rewriter.create(loc, realSqr, imagSqr, fmf.getValue());
-
-rewriter.replaceOpWithNewOp(op, sqNorm);
+Type elementType = op.getType();
+Value arg = adaptor.getComplex();
+
+Value zero =
+b.create(elementType, b.getZeroAttr(elementType));
+Value one = b.create(elementType,
+b.getFloatAttr(elementType, 1.0));
+
+Value real = b.create(elementType, arg);
+Value imag = b.create(elementType, arg);
+
+Value realIsZero =
+b.create(arith::CmpFPredicate::OEQ, real, zero);
+Value imagIsZero =
+b.create(arith::CmpFPredicate::OEQ, imag, zero);
+
+// Real > Imag
+Value imagDivReal = b.create(imag, real, fmf.getValue());
+Value imagSq =
+b.create(imagDivReal, imagDivReal, fmf.getValue());
+Value imagSqPlusOne = b.create(imagSq, one, fmf.getValue());
+Value imagSqrt = b.create(imagSqPlusOne, fmf.getValue());
+Value absImag = b.create(imagSqrt, real, fmf.getValue());
+
+// Real <= Imag
+Value realDivImag = b.create(real, imag, fmf.getValue());
+Value realSq =
+b.create(realDivImag, realDivImag, fmf.getValue());
+Value realSqPlusOne = b.create(realSq, one, fmf.getValue());
+Value realSqrt = b.create(realSqPlusOne, fmf.getValue());
+Value absReal = b.create(realSqrt, imag, fmf.getValue());
+
+rewriter.replaceOpWithNewOp(
+op, realIsZero, imag,
+b.create(
+imagIsZero, real,
+b.create(
+b.create(arith::CmpFPredicate::OGT, real, imag),
+absImag, absReal)));
+
 return success();
   }
 };
diff --git a/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir 
b/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
index 3af28150fd5c3f..1028c9aae92c05 100644
--- a/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
+++ b/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
@@ -7,13 +7,28 @@ func.func @complex_abs(%arg: complex) -> f32 {
   %abs = complex.abs %arg: complex
   return %abs : f32
 }
+
+// CHECK: %[[ZERO:.*]] = arith.constant 0.00e+00 : f32
+// CHECK: %[[ONE:.*]] = arith.constant 1.00e+00 : f32
 // CHECK: %[[REAL:.*]] = complex.re %[[ARG]] : complex
 // CHECK: %[[IMAG:.*]] = complex.im %[[ARG]] : complex
-// CHECK-DAG: %[[REAL_SQ:.*]] = arith.mulf %[[REAL]], %[[REAL]] : f32
-// CHECK-DAG: %[[IMAG_SQ:.*]] = arith.mulf %[[IMAG]], %[[IMAG]] : f32
-// CHECK: %[[SQ_NORM:.*]] = arith.addf %[[REAL_SQ]], %[[IMAG_SQ]] : f32
-// CHECK: %[[NORM:.*]] = math.sqrt %[[SQ_NORM]] : f32
-// CHECK: return %[[NORM]] : f32
+// CHECK: %[[IS_REAL_ZERO:.*]] = arith.cmpf oeq, %[[REAL]], %[[ZERO]] : f32
+// CHECK: %[[IS_IMAG_ZERO:.*]] = arith.cmpf oeq, %[[IMAG]], %[[ZERO]] : f32
+// CHECK: %[[IMAG_DIV_REAL:.*]] = arith.divf %[[IMAG]], %[[REAL]] : f32
+// CHECK: %[[IMAG_SQ:.*]] = arith.mulf %[[IMAG_DIV_REAL]], %[[IMAG_DIV_REAL]] 
: f32
+// CHECK: %[[IMAG_SQ_PLUS_ONE:.*]] = arith.addf %[[IMAG_SQ]], %[[ONE]] : f32
+// CHECK: %[[IMAG_SQRT:.*]] = math.sqrt %[[IMAG_SQ_PLUS_ONE]] : f32
+// CHECK: %[[ABS_IMAG:.*]] = arith.mulf %[[IMAG_SQRT]], %[[REAL]] : f32

[clang] [clang-tools-extra] [llvm] [flang] [mlir] [lldb] [mlir][complex] Prevent underflow in complex.abs (PR #76316)

2024-01-25 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe updated 
https://github.com/llvm/llvm-project/pull/76316

>From a5810363e546da073543cb2d62cceb956c46b2e6 Mon Sep 17 00:00:00 2001
From: Kai Sasaki 
Date: Fri, 15 Dec 2023 15:53:54 +0900
Subject: [PATCH 1/2] [mlir][complex] Prevent underflow in complex.abs

---
 .../ComplexToStandard/ComplexToStandard.cpp   |  56 ++---
 .../convert-to-standard.mlir  | 115 ++
 .../ComplexToStandard/full-conversion.mlir|  25 +++-
 .../Dialect/Complex/CPU/correctness.mlir  |  38 ++
 4 files changed, 194 insertions(+), 40 deletions(-)

diff --git a/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp 
b/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
index bf753c7062f366..7c1db57b55f996 100644
--- a/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
+++ b/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
@@ -26,29 +26,57 @@ namespace mlir {
 using namespace mlir;
 
 namespace {
+// The algorithm is listed in https://dl.acm.org/doi/pdf/10.1145/363717.363780.
 struct AbsOpConversion : public OpConversionPattern {
   using OpConversionPattern::OpConversionPattern;
 
   LogicalResult
   matchAndRewrite(complex::AbsOp op, OpAdaptor adaptor,
   ConversionPatternRewriter ) const override {
-auto loc = op.getLoc();
-auto type = op.getType();
+mlir::ImplicitLocOpBuilder b(op.getLoc(), rewriter);
 
 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
 
-Value real =
-rewriter.create(loc, type, adaptor.getComplex());
-Value imag =
-rewriter.create(loc, type, adaptor.getComplex());
-Value realSqr =
-rewriter.create(loc, real, real, fmf.getValue());
-Value imagSqr =
-rewriter.create(loc, imag, imag, fmf.getValue());
-Value sqNorm =
-rewriter.create(loc, realSqr, imagSqr, fmf.getValue());
-
-rewriter.replaceOpWithNewOp(op, sqNorm);
+Type elementType = op.getType();
+Value arg = adaptor.getComplex();
+
+Value zero =
+b.create(elementType, b.getZeroAttr(elementType));
+Value one = b.create(elementType,
+b.getFloatAttr(elementType, 1.0));
+
+Value real = b.create(elementType, arg);
+Value imag = b.create(elementType, arg);
+
+Value realIsZero =
+b.create(arith::CmpFPredicate::OEQ, real, zero);
+Value imagIsZero =
+b.create(arith::CmpFPredicate::OEQ, imag, zero);
+
+// Real > Imag
+Value imagDivReal = b.create(imag, real, fmf.getValue());
+Value imagSq =
+b.create(imagDivReal, imagDivReal, fmf.getValue());
+Value imagSqPlusOne = b.create(imagSq, one, fmf.getValue());
+Value imagSqrt = b.create(imagSqPlusOne, fmf.getValue());
+Value absImag = b.create(imagSqrt, real, fmf.getValue());
+
+// Real <= Imag
+Value realDivImag = b.create(real, imag, fmf.getValue());
+Value realSq =
+b.create(realDivImag, realDivImag, fmf.getValue());
+Value realSqPlusOne = b.create(realSq, one, fmf.getValue());
+Value realSqrt = b.create(realSqPlusOne, fmf.getValue());
+Value absReal = b.create(realSqrt, imag, fmf.getValue());
+
+rewriter.replaceOpWithNewOp(
+op, realIsZero, imag,
+b.create(
+imagIsZero, real,
+b.create(
+b.create(arith::CmpFPredicate::OGT, real, imag),
+absImag, absReal)));
+
 return success();
   }
 };
diff --git a/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir 
b/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
index 3af28150fd5c3f..1028c9aae92c05 100644
--- a/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
+++ b/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
@@ -7,13 +7,28 @@ func.func @complex_abs(%arg: complex) -> f32 {
   %abs = complex.abs %arg: complex
   return %abs : f32
 }
+
+// CHECK: %[[ZERO:.*]] = arith.constant 0.00e+00 : f32
+// CHECK: %[[ONE:.*]] = arith.constant 1.00e+00 : f32
 // CHECK: %[[REAL:.*]] = complex.re %[[ARG]] : complex
 // CHECK: %[[IMAG:.*]] = complex.im %[[ARG]] : complex
-// CHECK-DAG: %[[REAL_SQ:.*]] = arith.mulf %[[REAL]], %[[REAL]] : f32
-// CHECK-DAG: %[[IMAG_SQ:.*]] = arith.mulf %[[IMAG]], %[[IMAG]] : f32
-// CHECK: %[[SQ_NORM:.*]] = arith.addf %[[REAL_SQ]], %[[IMAG_SQ]] : f32
-// CHECK: %[[NORM:.*]] = math.sqrt %[[SQ_NORM]] : f32
-// CHECK: return %[[NORM]] : f32
+// CHECK: %[[IS_REAL_ZERO:.*]] = arith.cmpf oeq, %[[REAL]], %[[ZERO]] : f32
+// CHECK: %[[IS_IMAG_ZERO:.*]] = arith.cmpf oeq, %[[IMAG]], %[[ZERO]] : f32
+// CHECK: %[[IMAG_DIV_REAL:.*]] = arith.divf %[[IMAG]], %[[REAL]] : f32
+// CHECK: %[[IMAG_SQ:.*]] = arith.mulf %[[IMAG_DIV_REAL]], %[[IMAG_DIV_REAL]] 
: f32
+// CHECK: %[[IMAG_SQ_PLUS_ONE:.*]] = arith.addf %[[IMAG_SQ]], %[[ONE]] : f32
+// CHECK: %[[IMAG_SQRT:.*]] = math.sqrt %[[IMAG_SQ_PLUS_ONE]] : f32
+// CHECK: %[[ABS_IMAG:.*]] = arith.mulf %[[IMAG_SQRT]], %[[REAL]] : f32

[clang] [clang-tools-extra] [mlir] [llvm] [mlir][complex] Prevent underflow in complex.abs (PR #76316)

2024-01-25 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe updated 
https://github.com/llvm/llvm-project/pull/76316

>From a5810363e546da073543cb2d62cceb956c46b2e6 Mon Sep 17 00:00:00 2001
From: Kai Sasaki 
Date: Fri, 15 Dec 2023 15:53:54 +0900
Subject: [PATCH 1/2] [mlir][complex] Prevent underflow in complex.abs

---
 .../ComplexToStandard/ComplexToStandard.cpp   |  56 ++---
 .../convert-to-standard.mlir  | 115 ++
 .../ComplexToStandard/full-conversion.mlir|  25 +++-
 .../Dialect/Complex/CPU/correctness.mlir  |  38 ++
 4 files changed, 194 insertions(+), 40 deletions(-)

diff --git a/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp 
b/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
index bf753c7062f3664..7c1db57b55f996b 100644
--- a/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
+++ b/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
@@ -26,29 +26,57 @@ namespace mlir {
 using namespace mlir;
 
 namespace {
+// The algorithm is listed in https://dl.acm.org/doi/pdf/10.1145/363717.363780.
 struct AbsOpConversion : public OpConversionPattern {
   using OpConversionPattern::OpConversionPattern;
 
   LogicalResult
   matchAndRewrite(complex::AbsOp op, OpAdaptor adaptor,
   ConversionPatternRewriter ) const override {
-auto loc = op.getLoc();
-auto type = op.getType();
+mlir::ImplicitLocOpBuilder b(op.getLoc(), rewriter);
 
 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
 
-Value real =
-rewriter.create(loc, type, adaptor.getComplex());
-Value imag =
-rewriter.create(loc, type, adaptor.getComplex());
-Value realSqr =
-rewriter.create(loc, real, real, fmf.getValue());
-Value imagSqr =
-rewriter.create(loc, imag, imag, fmf.getValue());
-Value sqNorm =
-rewriter.create(loc, realSqr, imagSqr, fmf.getValue());
-
-rewriter.replaceOpWithNewOp(op, sqNorm);
+Type elementType = op.getType();
+Value arg = adaptor.getComplex();
+
+Value zero =
+b.create(elementType, b.getZeroAttr(elementType));
+Value one = b.create(elementType,
+b.getFloatAttr(elementType, 1.0));
+
+Value real = b.create(elementType, arg);
+Value imag = b.create(elementType, arg);
+
+Value realIsZero =
+b.create(arith::CmpFPredicate::OEQ, real, zero);
+Value imagIsZero =
+b.create(arith::CmpFPredicate::OEQ, imag, zero);
+
+// Real > Imag
+Value imagDivReal = b.create(imag, real, fmf.getValue());
+Value imagSq =
+b.create(imagDivReal, imagDivReal, fmf.getValue());
+Value imagSqPlusOne = b.create(imagSq, one, fmf.getValue());
+Value imagSqrt = b.create(imagSqPlusOne, fmf.getValue());
+Value absImag = b.create(imagSqrt, real, fmf.getValue());
+
+// Real <= Imag
+Value realDivImag = b.create(real, imag, fmf.getValue());
+Value realSq =
+b.create(realDivImag, realDivImag, fmf.getValue());
+Value realSqPlusOne = b.create(realSq, one, fmf.getValue());
+Value realSqrt = b.create(realSqPlusOne, fmf.getValue());
+Value absReal = b.create(realSqrt, imag, fmf.getValue());
+
+rewriter.replaceOpWithNewOp(
+op, realIsZero, imag,
+b.create(
+imagIsZero, real,
+b.create(
+b.create(arith::CmpFPredicate::OGT, real, imag),
+absImag, absReal)));
+
 return success();
   }
 };
diff --git a/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir 
b/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
index 3af28150fd5c3f3..1028c9aae92c056 100644
--- a/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
+++ b/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
@@ -7,13 +7,28 @@ func.func @complex_abs(%arg: complex) -> f32 {
   %abs = complex.abs %arg: complex
   return %abs : f32
 }
+
+// CHECK: %[[ZERO:.*]] = arith.constant 0.00e+00 : f32
+// CHECK: %[[ONE:.*]] = arith.constant 1.00e+00 : f32
 // CHECK: %[[REAL:.*]] = complex.re %[[ARG]] : complex
 // CHECK: %[[IMAG:.*]] = complex.im %[[ARG]] : complex
-// CHECK-DAG: %[[REAL_SQ:.*]] = arith.mulf %[[REAL]], %[[REAL]] : f32
-// CHECK-DAG: %[[IMAG_SQ:.*]] = arith.mulf %[[IMAG]], %[[IMAG]] : f32
-// CHECK: %[[SQ_NORM:.*]] = arith.addf %[[REAL_SQ]], %[[IMAG_SQ]] : f32
-// CHECK: %[[NORM:.*]] = math.sqrt %[[SQ_NORM]] : f32
-// CHECK: return %[[NORM]] : f32
+// CHECK: %[[IS_REAL_ZERO:.*]] = arith.cmpf oeq, %[[REAL]], %[[ZERO]] : f32
+// CHECK: %[[IS_IMAG_ZERO:.*]] = arith.cmpf oeq, %[[IMAG]], %[[ZERO]] : f32
+// CHECK: %[[IMAG_DIV_REAL:.*]] = arith.divf %[[IMAG]], %[[REAL]] : f32
+// CHECK: %[[IMAG_SQ:.*]] = arith.mulf %[[IMAG_DIV_REAL]], %[[IMAG_DIV_REAL]] 
: f32
+// CHECK: %[[IMAG_SQ_PLUS_ONE:.*]] = arith.addf %[[IMAG_SQ]], %[[ONE]] : f32
+// CHECK: %[[IMAG_SQRT:.*]] = math.sqrt %[[IMAG_SQ_PLUS_ONE]] : f32
+// CHECK: %[[ABS_IMAG:.*]] = arith.mulf %[[IMAG_SQRT]], %[[REAL]] : 

[compiler-rt] [llvm] [flang] [mlir] [libcxx] [clang-tools-extra] [clang] [mlir] Fix a zero stride canonicalizer crash (PR #74200)

2023-12-05 Thread Kai Sasaki via cfe-commits

Lewuathe wrote:

@joker-eph Thank you. That gets clearer to me!

https://github.com/llvm/llvm-project/pull/74200
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[compiler-rt] [llvm] [clang] [libcxx] [mlir] [flang] [clang-tools-extra] [mlir] Fix a zero stride canonicalizer crash (PR #74200)

2023-12-04 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe approved this pull request.

Thanks for the update. It LGTM.

https://github.com/llvm/llvm-project/pull/74200
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [mlir] [clang] [llvm] [mlir] Fix a zero stride canonicalizer crash (PR #74200)

2023-12-03 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe commented:

Is it possible to add the test case (or minimal similar example) with 
`--inline` option so that we can confirm the original issue is resolved.

https://github.com/llvm/llvm-project/issues/73383

https://github.com/llvm/llvm-project/pull/74200
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [llvm] [clang] [mlir] [mlir] Fix a zero stride canonicalizer crash (PR #74200)

2023-12-03 Thread Kai Sasaki via cfe-commits


@@ -139,12 +139,36 @@ SmallVector
 getValuesSortedByKey(ArrayRef keys, ArrayRef values,
  llvm::function_ref compare);
 
+/// Helper function to check whether the passed in `sizes` or `values` are
+/// valid. This can be used to re-check whether dimensions are still valid
+/// after constant folding the dynamic dimensions.
+bool hasValidSizesOffsets(SmallVector sizesOrOffsets);
+
+/// Helper function to check whether the passed in `strides` are valid. This
+/// can be used to re-check whether dimensions are still valid after constant
+/// folding the dynamic dimensions.
+bool hasValidStrides(SmallVector strides);
+
 /// Returns "success" when any of the elements in `ofrs` is a constant value. 
In
 /// that case the value is replaced by an attribute. Returns "failure" when no
-/// folding happened. If `onlyNonNegative` is set, only non-negative constant
-/// values are folded.
+/// folding happened. If `onlyNonNegative` and `onlyNonZero` are set, only
+/// non-negative and non-zero constant values are folded respectively.
 LogicalResult foldDynamicIndexList(SmallVectorImpl ,
-   bool onlyNonNegative = false);
+   bool onlyNonNegative = false,
+   bool onlyNonZero = false);
+
+/// Returns "success" when any of the elements in `OffsetsOrSizes` is a

Lewuathe wrote:

```suggestion
/// Returns "success" when any of the elements in `offsetsOrSizes` is a
```

https://github.com/llvm/llvm-project/pull/74200
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[mlir] [clang-tools-extra] [llvm] [clang] [mlir] Fix a zero stride canonicalizer crash (PR #74200)

2023-12-03 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe edited 
https://github.com/llvm/llvm-project/pull/74200
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [mlir] [clang-tools-extra] [clang] [mlir] Fix a zero stride canonicalizer crash (PR #74200)

2023-12-03 Thread Kai Sasaki via cfe-commits


@@ -139,12 +139,36 @@ SmallVector
 getValuesSortedByKey(ArrayRef keys, ArrayRef values,
  llvm::function_ref compare);
 
+/// Helper function to check whether the passed in `sizes` or `values` are

Lewuathe wrote:

```suggestion
/// Helper function to check whether the passed in `sizes` or `offsets` are
```

https://github.com/llvm/llvm-project/pull/74200
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [mlir][affine] Check the input vector sizes to be greater than 0 (PR #65293)

2023-10-01 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe closed 
https://github.com/llvm/llvm-project/pull/65293
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [mlir][affine] Check the input vector sizes to be greater than 0 (PR #65293)

2023-10-01 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe closed 
https://github.com/llvm/llvm-project/pull/65293
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [mlir][affine] Check the input vector sizes to be greater than 0 (PR #65293)

2023-09-26 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe updated 
https://github.com/llvm/llvm-project/pull/65293

>From cd1a19a5b12cbdba2654b08b04e54c8313dda696 Mon Sep 17 00:00:00 2001
From: Kai Sasaki 
Date: Tue, 5 Sep 2023 14:40:44 +0900
Subject: [PATCH 1/2] [mlir][affine] Check the input vector sizes to be greater
 than 0

---
 mlir/include/mlir/Dialect/Affine/Passes.td| 3 ++-
 mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp | 5 +
 mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir  | 9 +
 3 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir

diff --git a/mlir/include/mlir/Dialect/Affine/Passes.td 
b/mlir/include/mlir/Dialect/Affine/Passes.td
index 891a6661ca87be7..1036e93a0392409 100644
--- a/mlir/include/mlir/Dialect/Affine/Passes.td
+++ b/mlir/include/mlir/Dialect/Affine/Passes.td
@@ -349,7 +349,8 @@ def AffineVectorize : Pass<"affine-super-vectorize", 
"func::FuncOp"> {
   let dependentDialects = ["vector::VectorDialect"];
   let options = [
 ListOption<"vectorSizes", "virtual-vector-size", "int64_t",
-   "Specify an n-D virtual vector size for vectorization">,
+   "Specify an n-D virtual vector size for vectorization. "
+   "This must be greater than zero.">,
 // Optionally, the fixed mapping from loop to fastest varying MemRef
 // dimension for all the MemRefs within a loop pattern:
 //   the index represents the loop depth, the value represents the k^th
diff --git a/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp 
b/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
index 072e858220feae3..327616d45c90189 100644
--- a/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
+++ b/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
@@ -1732,6 +1732,11 @@ void Vectorize::runOnOperation() {
 return signalPassFailure();
   }
 
+  if (llvm::any_of(vectorSizes, [](int64_t size) { return size == 0; })) {
+f.emitError("Vectorization factor must be greater than zero.");
+return signalPassFailure();
+  }
+
   DenseSet parallelLoops;
   ReductionLoopMap reductionLoops;
 
diff --git a/mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir 
b/mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir
new file mode 100644
index 000..396cc933e25bcf4
--- /dev/null
+++ b/mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir
@@ -0,0 +1,9 @@
+// RUN: mlir-opt %s -verify-diagnostics 
--affine-super-vectorize=virtual-vector-size=0
+
+// expected-error@+1 {{Vectorization factor must be greater than zero}} 
+func.func @with_zero_vector_size(%arg0: memref<21x12x12xi1>) {
+  affine.for %arg1 = 0 to 84 step 4294967295 {
+  }
+  return
+}
+

>From c0cc18d4d5631ec586d719eb21c37d0610f5ee31 Mon Sep 17 00:00:00 2001
From: Kai Sasaki 
Date: Tue, 26 Sep 2023 15:25:05 +0900
Subject: [PATCH 2/2] Post review follow-up

---
 mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp   | 2 +-
 .../SuperVectorize/{invalid.mlir => invalid-zero-size.mlir} | 0
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename mlir/test/Dialect/Affine/SuperVectorize/{invalid.mlir => 
invalid-zero-size.mlir} (100%)

diff --git a/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp 
b/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
index 327616d45c90189..82623126e49006b 100644
--- a/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
+++ b/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
@@ -1732,7 +1732,7 @@ void Vectorize::runOnOperation() {
 return signalPassFailure();
   }
 
-  if (llvm::any_of(vectorSizes, [](int64_t size) { return size == 0; })) {
+  if (llvm::any_of(vectorSizes, [](int64_t size) { return size <= 0; })) {
 f.emitError("Vectorization factor must be greater than zero.");
 return signalPassFailure();
   }
diff --git a/mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir 
b/mlir/test/Dialect/Affine/SuperVectorize/invalid-zero-size.mlir
similarity index 100%
rename from mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir
rename to mlir/test/Dialect/Affine/SuperVectorize/invalid-zero-size.mlir

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [mlir][affine] Check the input vector sizes to be greater than 0 (PR #65293)

2023-09-26 Thread Kai Sasaki via cfe-commits

https://github.com/Lewuathe updated 
https://github.com/llvm/llvm-project/pull/65293

>From cd1a19a5b12cbdba2654b08b04e54c8313dda696 Mon Sep 17 00:00:00 2001
From: Kai Sasaki 
Date: Tue, 5 Sep 2023 14:40:44 +0900
Subject: [PATCH 1/2] [mlir][affine] Check the input vector sizes to be greater
 than 0

---
 mlir/include/mlir/Dialect/Affine/Passes.td| 3 ++-
 mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp | 5 +
 mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir  | 9 +
 3 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir

diff --git a/mlir/include/mlir/Dialect/Affine/Passes.td 
b/mlir/include/mlir/Dialect/Affine/Passes.td
index 891a6661ca87be7..1036e93a0392409 100644
--- a/mlir/include/mlir/Dialect/Affine/Passes.td
+++ b/mlir/include/mlir/Dialect/Affine/Passes.td
@@ -349,7 +349,8 @@ def AffineVectorize : Pass<"affine-super-vectorize", 
"func::FuncOp"> {
   let dependentDialects = ["vector::VectorDialect"];
   let options = [
 ListOption<"vectorSizes", "virtual-vector-size", "int64_t",
-   "Specify an n-D virtual vector size for vectorization">,
+   "Specify an n-D virtual vector size for vectorization. "
+   "This must be greater than zero.">,
 // Optionally, the fixed mapping from loop to fastest varying MemRef
 // dimension for all the MemRefs within a loop pattern:
 //   the index represents the loop depth, the value represents the k^th
diff --git a/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp 
b/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
index 072e858220feae3..327616d45c90189 100644
--- a/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
+++ b/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
@@ -1732,6 +1732,11 @@ void Vectorize::runOnOperation() {
 return signalPassFailure();
   }
 
+  if (llvm::any_of(vectorSizes, [](int64_t size) { return size == 0; })) {
+f.emitError("Vectorization factor must be greater than zero.");
+return signalPassFailure();
+  }
+
   DenseSet parallelLoops;
   ReductionLoopMap reductionLoops;
 
diff --git a/mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir 
b/mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir
new file mode 100644
index 000..396cc933e25bcf4
--- /dev/null
+++ b/mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir
@@ -0,0 +1,9 @@
+// RUN: mlir-opt %s -verify-diagnostics 
--affine-super-vectorize=virtual-vector-size=0
+
+// expected-error@+1 {{Vectorization factor must be greater than zero}} 
+func.func @with_zero_vector_size(%arg0: memref<21x12x12xi1>) {
+  affine.for %arg1 = 0 to 84 step 4294967295 {
+  }
+  return
+}
+

>From c0cc18d4d5631ec586d719eb21c37d0610f5ee31 Mon Sep 17 00:00:00 2001
From: Kai Sasaki 
Date: Tue, 26 Sep 2023 15:25:05 +0900
Subject: [PATCH 2/2] Post review follow-up

---
 mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp   | 2 +-
 .../SuperVectorize/{invalid.mlir => invalid-zero-size.mlir} | 0
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename mlir/test/Dialect/Affine/SuperVectorize/{invalid.mlir => 
invalid-zero-size.mlir} (100%)

diff --git a/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp 
b/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
index 327616d45c90189..82623126e49006b 100644
--- a/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
+++ b/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
@@ -1732,7 +1732,7 @@ void Vectorize::runOnOperation() {
 return signalPassFailure();
   }
 
-  if (llvm::any_of(vectorSizes, [](int64_t size) { return size == 0; })) {
+  if (llvm::any_of(vectorSizes, [](int64_t size) { return size <= 0; })) {
 f.emitError("Vectorization factor must be greater than zero.");
 return signalPassFailure();
   }
diff --git a/mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir 
b/mlir/test/Dialect/Affine/SuperVectorize/invalid-zero-size.mlir
similarity index 100%
rename from mlir/test/Dialect/Affine/SuperVectorize/invalid.mlir
rename to mlir/test/Dialect/Affine/SuperVectorize/invalid-zero-size.mlir

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits