84 Commits

Author SHA1 Message Date
Kazu Hirata
1a0f482de8
[mlir] Remove unused includes (NFC) (#150476)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-07-24 11:23:53 -07:00
Maksim Levental
eaa67a3cf0
[mlir][NFC] update Conversion create APIs (5/n) (#149887)
See https://github.com/llvm/llvm-project/pull/147168 for more info.
2025-07-22 10:40:45 -04:00
Kazu Hirata
fa9adbfda9
[mlir] Remove unused includes (NFC) (#147101)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-07-04 13:30:21 -07:00
lorenzo chelini
0b63dfb066
[MLIR][NFC] Use base alias for constructor inheritance (#127756)
During my previous cleanup (#127403), I did not notice that we defined a
type alias for the base class. This type alias allows us to use the
shorter form Base::Base, and this PR switches to that.
2025-02-19 16:05:25 +01:00
lorenzo chelini
c1a2292526
[MLIR][NFC] Retire let constructor for passes in Conversion directory (part1) (#127403)
`let constructor` is deprecated since the table gen backend emits most
of the glue logic to build a pass. This PR retires the td method for
most (I need another pass) passes in the Conversion directory.
2025-02-17 10:55:27 +01:00
s-watanabe314
1935f84856
[mlir][complex] Add complex-range option and select complex division … (#127010)
…algorithm

This patch adds the `complex-range` option and two calculation methods
for complex number division (algebraic method and Smith's algorithm) to
both the `ComplexToLLVM` and `ComplexToStandard` passes, allowing the
calculation method to be controlled by the option.

See also the discussion in the following discourse post.


https://discourse.llvm.org/t/question-and-proposal-regarding-complex-number-division-algorithm-in-the-complex-dialect/83772
2025-02-13 13:06:04 +01:00
Benoit Jacob
bdd365825d
[MLIR] Fix ComplexToStandard lowering of complex::MulOp (#119591)
A complex multiplication should lower simply to the familiar 4 real
multiplications, 1 real addition, 1 real subtraction. No special-casing
of infinite or NaN values should be made, instead the complex numbers
should be thought as just vectors of two reals, naturally bottoming out
on the reals' semantics, IEEE754 or otherwise. That is what nearly
everybody else is doing ("nearly" because at the end of this PR
description we pinpoint the actual source of this in C99 `_Complex`),
and this pattern, by trying to do something different, was generating
much larger code, which was much slower and a departure from the
naturally expected floating-point behavior.

This code had originally been introduced in
https://reviews.llvm.org/D105270, which stated this rationale:
> The lowering handles special cases with NaN or infinity like C++.

I don't think that the C++ standard is a particularly important thing to
follow in this instance. What matters more is what people actually do in
practice with complex numbers, which rarely involves the C++
`std::complex` library type.

But out of curiosity, I checked, and the above statement seems
incorrect. The [current C++
standard](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/n4928.pdf)
library specification for `std::complex` does not say anything about the
implementation of complex multiplication: paragraph `[complex.ops]`
falls back on `[complex.member.ops]` which says:
> Effects: Multiplies the complex value rhs by the complex value *this
and stores the product in *this.

I also checked cppreference which often has useful information in case
something changed in a c++ language revision, but likewise, nothing at
all there:
https://en.cppreference.com/w/cpp/numeric/complex/operator_arith3

Finally, I checked in Compiler Explorer what Clang 19 currently
generates:
https://godbolt.org/z/oY7Ks4j95
That is just the familiar 4 multiplications.... and then there is some
weird check (`fcmp`) and conditionally a call to an external `__mulsc3`.
Googled that, found this StackOverflow answer:
https://stackoverflow.com/a/49438578

Summary: this is not about C++ (this post confirms my reading of the C++
standard not mandating anything about this). This is about C, and it
just happens that this C++ standard library implementation bottoms out
on code shared with the C `_Complex` implementation.

Another nuance missing in that SO answer: this is actually
[implementation-defined
behavior](https://en.cppreference.com/w/c/preprocessor/impl). There are
two modes, controlled by
```c
#pragma STDC CX_LIMITED_RANGE {ON,OFF,DEFAULT}
```
It is implementation-defined which is the default. Clang defaults to
OFF, but that's just Clang. In that mode, the check is required:

https://en.cppreference.com/w/c/language/arithmetic_types#Complex_floating_types
And the specific point in the [C99
standard](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf)
is: `G.5.1 Multiplicative operators`.

But set it to ON and the check is gone:
https://godbolt.org/z/aG8fnbYoP

Summary: the argument has moved from C++ to C --- and even there, to
implementation-defined behavior with a standard opt-out mechanism.

Like with C++, I maintain that the C standard is not a particularly
meaningful thing for MLIR to follow here, because people doing business
with complex numbers tend to lower them to real numbers themselves, or
have their own specialized complex types, either way not relying on
C99's `_Complex` type --- and the very poor performance of the
`CX_LIMITED_RANGE OFF` behavior (default in Clang) is certainly a key
reason why people who care prefer to stay away from `_Complex` and
`std::complex`.

A good example that's relevant to MLIR's space is CUDA's `cuComplex`
type (used in the cuBLAS CGEMM interface). Here is its multiplication
function. The comment about competitiveness is interesting: it's not a
quirk of this particular function, it's the spirit underpinning
numerical code that matters.

1bf5cd15c5/v8.0/include/cuComplex.h (L106-L120)
```c

/* This implementation could suffer from intermediate overflow even though
 * the final result would be in range. However, various implementations do
 * not guard against this (presumably to avoid losing performance), so we 
 * don't do it either to stay competitive.
 */
__host__ __device__ static __inline__ cuFloatComplex cuCmulf (cuFloatComplex x,
                                                              cuFloatComplex y)
{
    cuFloatComplex prod;
    prod = make_cuFloatComplex  ((cuCrealf(x) * cuCrealf(y)) - 
                                 (cuCimagf(x) * cuCimagf(y)),
                                 (cuCrealf(x) * cuCimagf(y)) + 
                                 (cuCimagf(x) * cuCrealf(y)));
    return prod;
}
```

Another instance in CUTLASS:
https://github.com/NVIDIA/cutlass/blob/main/include/cutlass/complex.h#L231-L236

Signed-off-by: Benoit Jacob <jacob.benoit.1@gmail.com>
2024-12-12 11:11:39 -05:00
Jie Fu
06011fee3a [mlir] Fix -Wsign-compare in ComplexToStandard.cpp (NFC)
/llvm-project/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp:529:21:
 error: comparison of integers of different signs: 'int' and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare]
  529 |   for (int i = 1; i < coefficients.size(); ++i) {
      |                   ~ ^ ~~~~~~~~~~~~~~~~~~~
1 error generated.
2024-11-18 10:34:16 +08:00
Alexander Belyaev
18ee00323f
[mlir][complex] Add a numerically-stable lowering for complex.expm1. (#115082)
The current conversion to Standard in the MLIR repo is not stable for
small imag(arg).
2024-11-17 17:11:23 -08:00
Johannes Reifferscheid
9b225d01f8
Fix complex abs with nnan/ninf. (#95080)
The current logic tests for inf/inf and 0/0 inputs using a NaN check.
This doesn't work with all fastmath flags. With nnan and ninf, we can
just check for a 0 maximum. With only nnan, we have to check for both
cases separately.
2024-06-11 13:44:29 +02:00
Kazu Hirata
e0a293d12f [mlir] Fix a warning
This patch fixes:

  mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp:964:26:
  error: missing 'typename' prior to dependent type name Op::Adaptor;
  implicit 'typename' is a C++20 extension
  [-Werror,-Wc++20-extensions]
2024-05-17 00:10:00 -07:00
Johannes Reifferscheid
f43deca253
Fix Tan inaccuracies on extreme complex inputs. (#92443)
Specifically, those with small/large absolute values. This ports
https://github.com/openxla/xla/pull/10525 and was verified with XLA's
test suite.
2024-05-17 08:44:07 +02:00
Christian Sigg
fac349a169
Reapply "[mlir] Mark isa/dyn_cast/cast/... member functions depreca… (#90406)
…ted. (#89998)" (#90250)

This partially reverts commit 7aedd7dc754c74a49fe84ed2640e269c25414087.

This change removes calls to the deprecated member functions. It does
not mark the functions deprecated yet and does not disable the
deprecation warning in TypeSwitch. This seems to cause problems with
MSVC.
2024-04-28 22:01:42 +02:00
dyung
7aedd7dc75
Revert "[mlir] Mark isa/dyn_cast/cast/... member functions deprecated. (#89998)" (#90250)
This reverts commit 950b7ce0b88318f9099e9a7c9817d224ebdc6337.

This change is causing build failures on a bot
https://lab.llvm.org/buildbot/#/builders/216/builds/38157
2024-04-26 12:09:13 -07:00
Christian Sigg
950b7ce0b8
[mlir] Mark isa/dyn_cast/cast/... member functions deprecated. (#89998)
See https://mlir.llvm.org/deprecation and
https://discourse.llvm.org/t/preferred-casting-style-going-forward.
2024-04-26 16:28:30 +02:00
Kai Sasaki
8c9d814b66
[mlir][complex] Fastmath flag for complex angle (#88658)
See
https://discourse.llvm.org/t/rfc-fastmath-flags-support-in-complex-dialect/71981
2024-04-17 09:19:15 +09:00
Johannes Reifferscheid
9da0ef165e
Fix complex tanh overflows. (#88708)
This ports the XLA lowering and was verified using XLA's
exhaustive_unary_test_complex test.
2024-04-15 13:13:01 +02:00
Johannes Reifferscheid
33e60f358c
Fix rsqrt inaccuracies. (#88707)
The current lowering has issues with large/subnormal values. This po
XLA's lowering and was verified using XLA's test suite and the
MLIR-based emitters.

This updates https://github.com/llvm/llvm-project/pull/88691 to also
update the correctness test for rsqrt(0). I checked C++ and Python, they
both agree the result should be (inf, nan). Updated the correctness test
to match this.
2024-04-15 12:04:36 +02:00
Johannes Reifferscheid
b4e7b56403
Revert "Fix rsqrt inaccuracies." (#88705)
Reverts llvm/llvm-project#88691
2024-04-15 11:48:22 +02:00
Johannes Reifferscheid
8ddaf75074
Fix rsqrt inaccuracies. (#88691)
The current lowering has issues with large/subnormal values. This ports
XLA's lowering and was verified using XLA's test suite and the
MLIR-based emitters.
2024-04-15 11:24:00 +02:00
Kai Sasaki
8891fd5acb
[mlir][complex] Fastmath flag support for complex.tanh (#88571) 2024-04-14 19:52:03 +09:00
Johannes Reifferscheid
ff9bc3a0fa
Fix overflows in complex sqrt lowering. (#88480)
This ports XLA's complex sqrt lowering. The accuracy was tested with its
exhaustive_unary_test_complex test.

Note: rsqrt is still broken.
2024-04-12 09:42:04 +02:00
Johannes Reifferscheid
77dd43570b
Fix complex power for large inputs. (#88387)
For example, 1e30^1.2 currently overflows.

Also forward fastmath flags.

This ports XLA's logic and was verified with its test suite. Note that
rsqrt and sqrt are still broken.
2024-04-11 15:08:54 +02:00
Johannes Reifferscheid
9d9bb7b1b6
Fix complex abs corner cases. (#88373)
The current implementation fails for very small and very large values.
For example, (0, -inf) should return inf, but it returns -inf.

This ports the logic used in XLA. Tested with XLA's
exhaustive_binary_test_f32_f64.
2024-04-11 12:57:46 +02:00
Johannes Reifferscheid
5c9315f575
Fix complex log1p accuracy with large abs values. (#88364)
This ports openxla/xla#10503 by @pearu. In addition to the filecheck
test here, the accuracy was tested with XLA's complex_unary_op_test and
its MLIR emitters.

This is a fixed version of
https://github.com/llvm/llvm-project/pull/88260. The previous version
relied on implementation-specific behavior in the order of evaluation of
maxAbsOfRealPlusOneAndImagMinusOne's operands.
2024-04-11 10:54:50 +02:00
Mehdi Amini
43b2b2ebce
Revert "Fix complex log1p accuracy with large abs values." (#88290)
Reverts llvm/llvm-project#88260

The test fails on the GCC7 buildbot.
2024-04-10 18:25:16 +02:00
Johannes Reifferscheid
49ef12a08c
Fix complex log1p accuracy with large abs values. (#88260)
This ports https://github.com/openxla/xla/pull/10503 by @pearu. The new
implementation matches mpmath's results for most inputs, see caveats in
the linked pull request. In addition to the filecheck test here, the
accuracy was tested with XLA's complex_unary_op_test and its MLIR
emitters.
2024-04-10 14:55:56 +02:00
Kai Sasaki
51089e360e
[mlir][complex] Support fast math flag for complex.tan op (#87919)
See
https://discourse.llvm.org/t/rfc-fastmath-flags-support-in-complex-dialect/71981
2024-04-09 15:22:43 +09:00
Kai Sasaki
a522dbbd62
[mlir][complex] Support fast math flag for complex.sign op (#87148)
We are going to support the fast math flag given in `complex.sign` op in
the conversion to standard dialect.

See:
https://discourse.llvm.org/t/rfc-fastmath-flags-support-in-complex-dialect/71981
2024-04-06 15:35:10 +09:00
Kai Sasaki
7d2d8e2a72
[mlir][complex] Fastmath flag for the trigonometric ops in complex (#85563)
Support Fastmath flag to convert trigonometric ops in the complex
dialect.

See:
https://discourse.llvm.org/t/rfc-fastmath-flags-support-in-complex-dialect/71981
2024-03-25 10:59:42 +09:00
Kai Sasaki
34ba90745f
[mlir][complex] Support Fastmath flag in conversion of complex.sqrt to standard (#85019)
When converting complex.sqrt op to standard, we need to keep the fast
math flag given to the op.

See:
https://discourse.llvm.org/t/rfc-fastmath-flags-support-in-complex-dialect/71981
2024-03-14 15:53:28 +09:00
Kai Sasaki
b930b14d5d
[mlir][complex] Support fast math flag in converting complex.atan2 op (#82101)
When converting complex.atan2 op to standard, we need to keep the fast
math flag given to the op.

See:
https://discourse.llvm.org/t/rfc-fastmath-flags-support-in-complex-dialect/71981
2024-03-06 13:33:06 +09:00
Kai Sasaki
288d317fff
[mlir][complex] Support Fastmath flag in conversion of complex.div to standard (#82729)
Support Fastmath flag to convert `complex.div` to standard dialects. 

See:
https://discourse.llvm.org/t/rfc-fastmath-flags-support-in-complex-dialect/71981
2024-02-27 18:51:24 +09:00
Kai Sasaki
b17348c3b5
[mlir][complex] Prevent underflow in complex.abs (#79786) (#81092) 2024-02-11 07:35:19 +09:00
Benjamin Kramer
70fb96a286 Revert "[mlir][complex] Prevent underflow in complex.abs (#79786)"
This reverts commit 4effff21fb2f3462e06fcbd7812562f4771b0487. It makes
`complex.abs(-1)` return `-1`.
2024-01-31 15:42:10 +01:00
Kai Sasaki
4effff21fb
[mlir][complex] Prevent underflow in complex.abs (#79786)
The previous PR was not correct on the way to handle the negative value.
It is necessary to take the absolute value of the given real (or
imaginary) part to be multiplied with the sqrt part.

See: https://github.com/llvm/llvm-project/pull/76316
2024-01-31 09:59:05 +09:00
Mehdi Amini
c41bb1120f
Revert "[mlir][complex] Prevent underflow in complex.abs" (#79722)
Reverts llvm/llvm-project#76316

Buildbot test is broken.
2024-01-27 19:25:01 -08:00
Kai Sasaki
69f99cd20f
[mlir][complex] Prevent underflow in complex.abs (#76316) 2024-01-27 12:50:06 +09:00
Kai Sasaki
eee71ed3f7
[mlir][complex] Support Fastmath flag for complex.mulf (#74554)
Support fast math flag in the conversion of `complex.mulf` op to
standard dialect.

See:
https://discourse.llvm.org/t/rfc-fastmath-flags-support-in-complex-dialect/71981
2024-01-09 09:29:27 +09:00
Kai Sasaki
8aaa2cb833
[mlir][complex] Support Fastmath flag for complex log ops (#69798)
Progressive support of fastmath flag in the conversion of log type ops.

See more detail
https://discourse.llvm.org/t/rfc-fastmath-flags-support-in-complex-dialect/71981
2023-11-01 18:58:33 +09:00
Kai Sasaki
d230bf3fce
[mlir][complex] Support Fastmath flag in the conversion of exp,expm1 (#67001)
See:

https://discourse.llvm.org/t/rfc-fastmath-flags-support-in-complex-dialect/71981
2023-09-23 10:27:42 +09:00
Kai Sasaki
be5b66670d
[mlir][complex] Support fastmath in the binary op conversion. (#65702)
Complex dialect arithmetic operations are now able to recognize the
given fastmath flags. This PR lets the conversion from complex to
standard keep the fastmath flag passed to arith dialect ops.

See:

https://discourse.llvm.org/t/rfc-fastmath-flags-support-in-complex-dialect/71981
2023-09-11 15:57:24 +09:00
Kai Sasaki
2e53e15480
[mlir][complex] Convert complex.abs to arith with fastmath flag
This reverts commit 8e946fec0441e7a4eb4604c806afed20c6930cc4 after
restoring the test-pbp.exe file.

Reviewed By: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D158692
2023-08-29 09:18:01 +09:00
Muhammad Omair Javaid
8e946fec04 Revert "[mlir][complex] Convert complex.abs to arith with fastmath flag"
This reverts commit 653f77690bb2473ff405ccc681c73217fae8b748.

This breaks lldb-aarch64-windows buildbot.
I have reproduced the issue on x86_64 Windows as well.
https://lab.llvm.org/buildbot/#/builders/219/builds/5130
2023-08-29 02:01:20 +05:00
Kai Sasaki
653f77690b
[mlir][complex] Convert complex.abs to arith with fastmath flag 2023-08-25 12:10:43 +09:00
Tres Popp
5550c82189 [mlir] Move casting calls from methods to function calls
The MLIR classes Type/Attribute/Operation/Op/Value support
cast/dyn_cast/isa/dyn_cast_or_null functionality through llvm's doCast
functionality in addition to defining methods with the same name.
This change begins the migration of uses of the method to the
corresponding function call as has been decided as more consistent.

Note that there still exist classes that only define methods directly,
such as AffineExpr, and this does not include work currently to support
a functional cast/isa call.

Caveats include:
- This clang-tidy script probably has more problems.
- This only touches C++ code, so nothing that is being generated.

Context:
- https://mlir.llvm.org/deprecation/ at "Use the free function variants
  for dyn_cast/cast/isa/…"
- Original discussion at https://discourse.llvm.org/t/preferred-casting-style-going-forward/68443

Implementation:
This first patch was created with the following steps. The intention is
to only do automated changes at first, so I waste less time if it's
reverted, and so the first mass change is more clear as an example to
other teams that will need to follow similar steps.

Steps are described per line, as comments are removed by git:
0. Retrieve the change from the following to build clang-tidy with an
   additional check:
   https://github.com/llvm/llvm-project/compare/main...tpopp:llvm-project:tidy-cast-check
1. Build clang-tidy
2. Run clang-tidy over your entire codebase while disabling all checks
   and enabling the one relevant one. Run on all header files also.
3. Delete .inc files that were also modified, so the next build rebuilds
   them to a pure state.
4. Some changes have been deleted for the following reasons:
   - Some files had a variable also named cast
   - Some files had not included a header file that defines the cast
     functions
   - Some files are definitions of the classes that have the casting
     methods, so the code still refers to the method instead of the
     function without adding a prefix or removing the method declaration
     at the same time.

```
ninja -C $BUILD_DIR clang-tidy

run-clang-tidy -clang-tidy-binary=$BUILD_DIR/bin/clang-tidy -checks='-*,misc-cast-functions'\
               -header-filter=mlir/ mlir/* -fix

rm -rf $BUILD_DIR/tools/mlir/**/*.inc

git restore mlir/lib/IR mlir/lib/Dialect/DLTI/DLTI.cpp\
            mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp\
            mlir/lib/**/IR/\
            mlir/lib/Dialect/SparseTensor/Transforms/SparseVectorization.cpp\
            mlir/lib/Dialect/Vector/Transforms/LowerVectorMultiReduction.cpp\
            mlir/test/lib/Dialect/Test/TestTypes.cpp\
            mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp\
            mlir/test/lib/Dialect/Test/TestAttributes.cpp\
            mlir/unittests/TableGen/EnumsGenTest.cpp\
            mlir/test/python/lib/PythonTestCAPI.cpp\
            mlir/include/mlir/IR/
```

Differential Revision: https://reviews.llvm.org/D150123
2023-05-12 11:21:25 +02:00
Jakub Kuderski
abc362a107 [mlir][arith] Change dialect name from Arithmetic to Arith
Suggested by @lattner in https://discourse.llvm.org/t/rfc-define-precise-arith-semantics/65507/22.

Tested with:
`ninja check-mlir check-mlir-integration check-mlir-mlir-spirv-cpu-runner check-mlir-mlir-vulkan-runner check-mlir-examples`

and `bazel build --config=generic_clang @llvm-project//mlir:all`.

Reviewed By: lattner, Mogball, rriddle, jpienaar, mehdi_amini

Differential Revision: https://reviews.llvm.org/D134762
2022-09-29 11:23:28 -04:00
Michele Scuttari
67d0d7ac0a
[MLIR] Update pass declarations to new autogenerated files
The patch introduces the required changes to update the pass declarations and definitions to use the new autogenerated files and allow dropping the old infrastructure.

Reviewed By: mehdi_amini, rriddle

Differential Review: https://reviews.llvm.org/D132838
2022-08-31 12:28:45 +02:00
Michele Scuttari
039b969b32
Revert "[MLIR] Update pass declarations to new autogenerated files"
This reverts commit 2be8af8f0e0780901213b6fd3013a5268ddc3359.
2022-08-30 22:21:55 +02:00
Michele Scuttari
2be8af8f0e
[MLIR] Update pass declarations to new autogenerated files
The patch introduces the required changes to update the pass declarations and definitions to use the new autogenerated files and allow dropping the old infrastructure.

Reviewed By: mehdi_amini, rriddle

Differential Review: https://reviews.llvm.org/D132838
2022-08-30 21:56:31 +02:00