38 Commits

Author SHA1 Message Date
Md Abdullah Shahneous Bari
b9a627e6fb
[mlir][spirv] Add 8-bit float type emulation (#148811)
8-bit floats are not supported in SPIR-V. They are emulated as 8-bit
integer during conversion.
2025-07-30 17:39:49 -05:00
Maksim Levental
b0434925c9
[mlir][NFC] update Conversion create APIs (4/n) (#149879)
See https://github.com/llvm/llvm-project/pull/147168 for more info.
2025-07-23 10:49:35 -05:00
Matthias Springer
c47042c5b3
[mlir][SPRIV][NFC] Avoid rollback in TypeCastingOpPattern (#136284)
This pattern used to create an op and then attached the converted
rounding mode attribute. When the latter failed, the pattern aborted and
a rollback was triggered.

This commit inverses the logic: the converted rounding mode is computed
first, so that no changes have to be rolled back.

Note: This is in preparation of the One-Shot Dialect Conversion
refactoring.
2025-04-19 10:01:50 +02:00
Matthias Springer
6c867e27a7
[mlir] Use getSingleElement/hasSingleElement in various places (#131460)
This is a code cleanup. Update a few places in MLIR that should use
`hasSingleElement`/`getSingleElement`.

Note: `hasSingleElement` is faster than `.getSize() == 1` when it is
used with linked lists etc.

Depends on #131508.
2025-03-17 07:43:18 +01: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
Matthias Springer
206fad0e21
[mlir][NFC] Mark type converter in populate... functions as const (#111250)
This commit marks the type converter in `populate...` functions as
`const`. This is useful for debugging.

Patterns already take a `const` type converter. However, some
`populate...` functions do not only add new patterns, but also add
additional type conversion rules. That makes it difficult to find the
place where a type conversion was added in the code base. With this
change, all `populate...` functions that only populate pattern now have
a `const` type converter. Programmers can then conclude from the
function signature that these functions do not register any new type
conversion rules.

Also some minor cleanups around the 1:N dialect conversion
infrastructure, which did not always pass the type converter as a
`const` object internally.
2024-10-05 21:32:40 +02:00
Andrea Faulds
b537df9737
[mlir][arith][spirv] Convert arith.truncf rounding mode to SPIR-V (#101547)
Resolves #87050.
2024-08-02 10:30:43 -04:00
maxbartel
9ba0a77256
[mlir][spirv] Add support for dense_resource in arith to spirv (#91318)
This adds support for `dense_resource` in arith to spirv. 

Note that this inlines the blob into the IR. Another possibility would
be to add proper dense_resource support to spirv, but there is a lot of
special handling going on to convert a `DenseElementsAttr` to the
correct SPIRV type. Some of that even iterates over all the values in
the Attribute. For proper support of a `DenseResourceElementsAttr` this
probably needs a redesign. I would like to hear some opinions on that!

The test is disabled on non little Endian machines. See
https://github.com/llvm/llvm-project/issues/63469 for more information.
2024-05-17 10:29:48 -04:00
Victor Perez
8827ff92b9
[MLIR][Arith] Add rounding mode attribute to truncf (#86152)
Add rounding mode attribute to `arith`. This attribute can be used in
different FP `arith` operations to control rounding mode. Rounding modes
correspond to IEEE 754-specified rounding modes. Use in `arith.truncf` folding.

As this is not supported in dialects other than LLVM, conversion should fail for
now in case this attribute is present.

---------

Signed-off-by: Victor Perez <victor.perez@codeplay.com>
2024-04-01 11:57:14 +02:00
Kazu Hirata
d0e97fe38b
[ArithToSPIRV] Fix a warning (#86702)
mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp:995:11: error:
  unused variable 'converter' [-Werror,-Wunused-variable]
2024-03-26 10:44:20 -07:00
Ivan Butygin
f050a098b5
[mlir][spirv] Remove enableFastMathMode flag from SPIR-V conversion (#86578)
Most of arith/math ops support fastmath attribute, use it instead of
global flag.
2024-03-26 20:06:06 +03:00
Yi Wu
f7ef73ea88
[mlir] [arith] add shl overflow flag in Arith and lower to SPIR-V and LLVMIR (#79828)
There is no `SHL` used in canonicalization in `arith`

---------

Co-authored-by: Jakub Kuderski <kubakuderski@gmail.com>
Co-authored-by: Tobias Gysi <tobias.gysi@nextsilicon.com>
2024-01-30 11:11:29 +00:00
Jacques Pienaar
8934b10642
[mlir][arith] Add overflow flags support to arith ops (#78376)
Add overflow flags support to the following ops:
* `arith.addi`
* `arith.subi`
* `arith.muli`

Example of new syntax:
```
%res = arith.addi %arg1, %arg2 overflow<nsw> : i64
```
Similar to existing LLVM dialect syntax
```
%res = llvm.add %arg1, %arg2 overflow<nsw> : i64
```

Tablegen canonicalization patterns updated to always drop flags, proper
support with tests will be added later.

Updated LLVMIR translation as part of this commit as it currenly written
in a way that it will crash when new attributes added to arith ops
otherwise.

Also lower `arith` overflow flags to corresponding SPIR-V op decorations

Discussion

https://discourse.llvm.org/t/rfc-integer-overflow-flags-support-in-arith-dialect/76025

This effectively rolls forward #77211, #77700 and #77714 while adding a
test to ensure the Python usage is not broken. More follow up needed but
unrelated to the core change here. The changes here are minimal and just
correspond to "textual namespacing" ODS side, no C++ or Python changes
were needed.

---------

---------

Co-authored-by: Ivan Butygin <ivan.butygin@gmail.com>, Yi Wu <yi.wu2@arm.com>
2024-01-17 06:12:23 +03:00
Ivan Butygin
649b391799 Revert "[mlir][spirv] Lower arith overflow flags to corresponding SPIR-V op decorations (#77714)"
Temporaryly reverting as it broke python bindings

This reverts commit 4278d9b593d31a644e4be3bb9386e2c0ed6ac6f1.
2024-01-12 00:05:21 +01:00
Ivan Butygin
4278d9b593
[mlir][spirv] Lower arith overflow flags to corresponding SPIR-V op decorations (#77714) 2024-01-11 20:39:57 +03:00
Daniil Dudkin
641124a9b9
[mlir][spirv] Add conversions for Arith's maxnumf and minnumf (#66696)
This patch is part of a larger initiative aimed at fixing floating-point
`max` and `min` operations in MLIR:
https://discourse.llvm.org/t/rfc-fix-floating-point-max-and-min-operations-in-mlir/72671.

In this commit, we add conversion patterns for the newly introduced
operations `arith.minnumf` and `arith.maxnumf`. When converting to
`spirv.CL`, there is no need to insert additional guards to propagate
non-NaN values when one of the arguments is NaN because `CL` ops do
exactly the same. However, `GL` ops have undefined behavior when one of
the arguments is NaN, so we should insert additional guards to enforce
the semantics of Arith's ops.

This patch addresses the 1.5 task of the mentioned RFC.
2023-09-19 22:49:48 +03:00
Daniil Dudkin
8a6e54c9b3
[mlir][arith] Rename operations: maxfmaximumf, minfminimumf (#65800)
This patch is part of a larger initiative aimed at fixing floating-point `max` and `min` operations in MLIR: https://discourse.llvm.org/t/rfc-fix-floating-point-max-and-min-operations-in-mlir/72671.

This commit addresses Task 1.2 of the mentioned RFC. By renaming these operations, we align their names with LLVM intrinsics that have corresponding semantics.
2023-09-11 22:02:19 -07:00
Lei Zhang
2b066501b1 [mlir][spirv] Fix integer type emulation with extension/truncation
For integer extension or truncation with type emulation, we need
to make sure we perform masking or shifting to discard unwanted
bits to avoid pollute consumer ops.

Reviewed By: kuhar

Differential Revision: https://reviews.llvm.org/D157788
2023-08-12 18:41:34 -07:00
Lei Zhang
81c326ccdd [mlir][spirv] NFC: Merge ArithToSPIRV pattern decl and definition
This makes the code easier to search and read.

Reviewed By: kuhar

Differential Revision: https://reviews.llvm.org/D157782
2023-08-12 16:25:47 -07:00
Krzysztof Drewniak
3fe8ec7906 Revert "[mlir][Arith] Add arith.is_nan and arith.is_inf predicates"
This reverts commit 7c349c369847dc2f1736efb9c90d03521cd44a90.

Per discussion at
https://reviews.llvm.org/rG7c349c369847dc2f1736efb9c90d03521cd44a90
and elsewhere, the lowering to LLVM defined here isn't what it should
be and the fastmath flag usage isn't correct, so `arith.is_nan` and
`arith.is_inf` cannot exist in their current form.

It's unclear if those operations should be introduced in the future,
since they make the dialect more complex and don't add any expressive
power. Further discussion may be moved to an RFC (or I'll drop this
patch).

Differential Revision: https://reviews.llvm.org/D157543
2023-08-10 14:13:25 +00:00
Krzysztof Drewniak
7c349c3698 [mlir][Arith] Add arith.is_nan and arith.is_inf predicates
Both LLVM and SPIR-V have some form of "is this float a NaN/Inf"
operation (though LLVM's uses the rather opaque "is.fpclass"
intrinsic), which is not exposed in MLIR.

This has lead to awkward workarounds in -arith-expands-ops where a NaN
test was performed by comparing an operation to itself. This commit
resolves that issue.

Reviewed By: dcaballe, kuhar

Differential Revision: https://reviews.llvm.org/D156169
2023-08-02 14:42:25 +00:00
Lei Zhang
18ad80ea6f [mlir][spirv] Improve integer cast during type conversion
In SPIR-V, the capabilities for storage and compute are separate.
We have good handling of the storage side in general via MemRef
type conversion and various `memref` dialect ops.

Once the value was loaded properly, if the compute capability is
supported directly, we don't need to emulate like the storage side
with int32. However, we do need to make sure casting ops are
properly inserted to chain the flow to go back to the original
bitwidth.

Right now that is done in the each individual pattern directly,
which put lots of pressure that shouldn't be on the patterns and
causes duplication and trickiness w.r.t. capability check and such.

Instead, we should handle such casting within the SPIR-V conversion
framework using `addSourceMaterialization`, where we can check with
the target environment to make sure the corresponding compute
capability is allowed and then we can materialize and SPIR-V casting
op.

Along the way, we can drop all the duplicated cast materialization
registration in various places.

Reviewed By: kuhar

Differential Revision: https://reviews.llvm.org/D155118
2023-07-12 14:38:11 -07: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
Quinn Dawkins
72e8b286f0 [mlir][spirv] Allow vectors of index types in elementwise conversions
Currently the conversion of elementwise ops only checks for scalar index
types when checking for bitwidth emulation.

Differential Revision: https://reviews.llvm.org/D146307
2023-03-17 13:33:56 -04:00
Steve Suzuki
e866fb8606 [mlir][spirv] Add support for fptoui conversion in ArithToSPIRV
To enable the conversion of arith.fptoui to SPIRV OpConvertFToU

Reviewed By: kuhar

Differential Revision: https://reviews.llvm.org/D141582
2023-01-12 16:35:50 +00:00
serge-sans-paille
984b800a03
Move from llvm::makeArrayRef to ArrayRef deduction guides - last part
This is a follow-up to https://reviews.llvm.org/D140896, split into
several parts as it touches a lot of files.

Differential Revision: https://reviews.llvm.org/D141298
2023-01-10 11:47:43 +01:00
Ramkumar Ramachandra
0de16fafa5 mlir/DialectConversion: use std::optional (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional. This patch touches DialectConversion, and modifies
existing conversions and tests appropriately.

See also: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716

Signed-off-by: Ramkumar Ramachandra <r@artagnon.com>

Differential Revision: https://reviews.llvm.org/D140303
2022-12-19 18:48:59 +01:00
Jakub Kuderski
4f47677dee [mlir][arith][spirv] Account for possible type conversion failures
Check results of all type conversions in `--convert-arith-to-spirv`.

Fixes: https://github.com/llvm/llvm-project/issues/59496

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D140033
2022-12-14 19:32:40 -05:00
Jakub Kuderski
285d321a85 [mlir][arith] Define mulsi_extended op
Extend D139688 with the signed version of the extended multiplication
op. Add conversion to the SPIR-V and LLVM dialects.

This was originally proposed in:
https://discourse.llvm.org/t/rfc-arith-add-extended-multiplication-ops/66869.

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D139743
2022-12-09 20:25:31 -05:00
Jakub Kuderski
b4bdcea214 [mlir][arith] Define mului_extended op
Add conversion to the SPIR-V and LLVM dialects.

This was originally proposed in:
https://discourse.llvm.org/t/rfc-arith-add-extended-multiplication-ops/66869.

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D139688
2022-12-09 17:37:06 -05:00
Jakub Kuderski
28246b7e75 [mlir][arith] Rename addui_carry to addui_extended
The goal is to make the naming of the future `_extended` ops more
consistent. With unsigned addition, the carry value/flag and overflow
bit are the same, but this is not true when it comes to signed addition.

Also rename the second result from `carry` to `overflow`.

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D139569
2022-12-07 17:15:56 -05:00
Jakub Kuderski
c50d0fe570 [mlir][arith][spirv] Clean up arith-to-spirv. NFC.
Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D137978
2022-11-14 20:54:27 -05:00
Jakub Kuderski
afc159bbf1 [mlir][arith][spirv] Handle i1 sign extension in arith-to-spirv
Also fix some surrounding nits.

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D137974
2022-11-14 15:07:27 -05:00
Jakub Kuderski
c064545403 [mlir][spirv] Do not truncate i/f64 -> i/f32 in SPIRVConversion
This truncation can be unexpected and break program behavior.
Dedicated emulation passes should be used instead.

Also rename pass options to "emulate-lt-32-bit-scalar-types".

Fixes: https://github.com/llvm/llvm-project/issues/57917

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D137115
2022-11-04 15:10:28 -04:00
Jakub Kuderski
179978d7b8 [mlir][arith][spirv] Hard fail in -convert-arith-to-spirv
Turn legalization failures into hard failures to make sure that we do
not miss conversion pattern application failures.

Add a message on type conversion failure.

Move unsupported cases into a separate test file.

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D137102
2022-10-31 17:01:21 -04:00
Thomas Raoux
f8fafe99a4 [mlir] Add unsigned version of index_cast
This is required to be able to cast integer type to a potential larger index using zero-extend cast.

There is a larger change under discussion to move index ops in a separate dialect: https://discourse.llvm.org/t/rfc-index-dialect/65540/
Based on timing of this work this patch can be included as part of this effort but as a short term solution we may want to add this op to arithmetic dialect for now in order to fill the gap.

Reviewed By: Mogball, stellaraccident

Differential Revision: https://reviews.llvm.org/D135089
2022-10-03 18:51:15 +00: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