Commit Graph

1380 Commits

Author SHA1 Message Date
Jakub Kuderski
96bbe472ef Revert "[mlir][spirv] Fix int type declaration duplication when serializing" and follow up commits (#144773)
This reverts the following PRs:
* https://github.com/llvm/llvm-project/pull/143108
* https://github.com/llvm/llvm-project/pull/144538
* https://github.com/llvm/llvm-project/pull/144685

Reverting because this disabled tests when building without the llvm
spirv backend enabled.
2025-06-18 16:15:06 -04:00
Kirill Chibisov
74687180dd [mlir][emitc] Make CExpression trait into interface (#142771)
By defining `CExpressionInterface`, we move the side effect detection
logic from `emitc.expression` into the individual operations
implementing the interface allowing operations to gradually tune the
side effect.

It also allows checking for side effects each operation individually.
2025-06-18 07:38:47 +02:00
Slava Zakharin
70343c8d44 [mlir][flang] Added Weighted[Region]BranchOpInterface's. (#142079)
The new interfaces provide getters and setters for the weight
information about the branches of BranchOpInterface and
RegionBranchOpInterface operations.

These interfaces are done the same way as LLVM dialect's
BranchWeightOpInterface.

The plan is to produce this information in Flang, e.g. mark
most probably "cold" code as such and allow LLVM to order
basic blocks accordingly. An example of such a code is
copy loops generated for arrays repacking - we can mark it
as "cold" assuming that the copy will not happen dynamically.
If the copy actually happens the overhead of the copy is probably high
enough so that we may not care about the little overhead
of jumping to the "cold" code and fetching it.
2025-06-17 16:14:13 -07:00
Davide Grohmann
549bc55cc3 [mlir][spirv] Fix int type declaration duplication when serializing (#143108)
At the MLIR level unsigned integer and signless integers are different
types. Indeed when looking up the two types in type definition cache
they do not match.

Hence when translating a SPIR-V module which contains both usign and
signless integers will contain the same type declaration twice
(something like OpTypeInt 32 0) which is not permitted in SPIR-V and
such generated modules fail validation.

This patch solves the problem by mapping unisgned integer types to
singless integer types before looking up in the type definition cache.

---------

Signed-off-by: Davide Grohmann <davide.grohmann@arm.com>
2025-06-17 10:35:14 -04:00
Tom Eccles
aa01e8e9cf [mlir][OpenMP] Fix broken insertion point for charbox with omp task (#143112)
Fixes #142365
2025-06-17 10:42:42 +01:00
Darren Wihandi
0a0960dac6 [mlir][spirv] Add bfloat16 support (#141458)
Adds bf16 support to SPIRV by using the `SPV_KHR_bfloat16` extension.
Only a few operations are supported, including loading from and storing
to memory, conversion to/from other types, cooperative matrix operations
(including coop matrix arithmetic ops) and dot product support.

This PR adds the type definition and implements the basic cast
operations. Arithmetic/coop matrix ops will be added in a separate PR.
2025-06-13 10:14:45 -04:00
Jeremy Morse
97ac6483aa [DebugInfo][RemoveDIs] Delete debug-info-format flag (#143746)
This flag was used to let us incrementally introduce debug records
into LLVM, however everything is now using records. It serves no
purpose now, so delete it.
2025-06-12 11:51:58 +01:00
Davide Grohmann
6fb2a80189 [mlir][spirv] Truncate Literal String size at max number words (#142916)
If not truncated the SPIRV serialization would not fail but instead
produce an invalid SPIR-V module.

---------

Signed-off-by: Davide Grohmann <davide.grohmann@arm.com>
2025-06-11 09:56:38 -04:00
Jeremy Morse
354cfba520 [DebugInfo][RemoveDIs] Remove scoped-dbg-format-setter (#143450)
This was a utility for flipping between intrinsic and debug record mode
-- we don't need it any more. The "IsNewDbgInfoFormat" should be true
everywhere.
2025-06-11 11:23:24 +01:00
Cameron McInally
cde1035a2f [flang] Add support for -mrecip[=<list>] (#143418)
This patch adds support for the -mrecip command line option. The parsing
of this options is equivalent to Clang's and it is implemented by
setting the "reciprocal-estimates" function attribute.

Also move the ParseMRecip(...) function to CommonArgs, so that Flang is
able to make use of it as well.

---------

Co-authored-by: Cameron McInally <cmcinally@nvidia.com>
2025-06-10 08:25:33 -06:00
Igor Wodiany
326429022f [mlir][spirv] Deserialize OpConstantComposite of type Cooperative Matrix (#142786)
Depends on #142784.
2025-06-10 15:23:06 +01:00
NimishMishra
bf1fe6eb33 [mlir][OpenMP] Reintroduce TODO for translation of linear clause (#143531)
Reintroduce a TODO for linear clause translation unless corner issues
(like linear variables being entities other than `alloca`, and support
for linear variables of types other than integer) are solved.
2025-06-10 07:06:28 -07:00
Jeremy Morse
0e4b8b8f81 [DebugInfo][RemoveDIs] Rip out the UseNewDbgInfoFormat flag (#143207)
Start removing debug intrinsics support -- starting with the flag that
controls production of their replacement, debug records. This patch
removes the command-line-flag and with it the ability to switch back to
intrinsics. The module / function / block level "IsNewDbgInfoFormat"
flags get hardcoded to true, I'll to incrementally remove things that
depend on those flags.
2025-06-09 19:36:34 +01:00
Kazu Hirata
b3b8a097fe [mlir] Use *Map::try_emplace (NFC) (#143341)
- try_emplace(Key) is shorter than insert({Key, nullptr}).
- try_emplace performs value initialization without value parameters.
- We overwrite values on successful insertion anyway.
2025-06-09 07:18:26 -07:00
Tom Eccles
b03081e9fb [mlir][OpenMP] set correct insert point after creating a barrier (#142997)
Fixes #138436
2025-06-06 10:43:13 +01:00
Igor Wodiany
3ce3281989 [mlir][spirv] Check output of getConstantInt (#140568)
This patch adds an assert to check if the result of `getConstantInt` is
non-null. Previously the code failed with Segmentation Fault if
`getConstantInt` failed to look up the value. This primarily occurrs when
the value is defined as OpSpecConstant rather than OpConstant.
2025-06-04 13:15:28 +01:00
Igor Wodiany
7797824297 [mlir][spirv] Allow disabling control flow structurization (#140561)
Currently some control flow patterns cannot be structurized into
existing SPIR-V MLIR constructs, e.g., conditional early exits (break).
Since the support for early exit cannot be currently added
(https://github.com/llvm/llvm-project/pull/138688#pullrequestreview-2830791677)
this patch enables structurizer to be disabled to keep
the control flow unstructurized. By default, the control flow is
structurized.
2025-06-03 15:41:39 +01:00
Cameron McInally
ce9cef79ea [flang] Add support for -mprefer-vector-width=<value> (#142073)
This patch adds support for the -mprefer-vector-width= command line
option. The parsing of this options is equivalent to Clang's and it is
implemented by setting the "prefer-vector-width" function attribute.

Co-authored-by: Cameron McInally <cmcinally@nvidia.com>
2025-05-30 07:50:18 -06:00
Bruno Cardoso Lopes
86685b95bf [MLIR][LLVM][DLTI] Handle data layout token 'n32:64' (#141299) 2025-05-28 11:07:03 -07:00
Vimal
b9d7ef7d5a Fix handling of integer template argument in emitc.call_opaque (#141451)
Integer attributes supplied to `emitc.call_opaque` as arguments were
treated as index into the operands list. This should be the case only
for the normal arguments but not for the template arguments which can't
refer to SSA values. This commit updates the handling of template
arguments in mlir-to-cpp by removing special handling of integer
attributes.
2025-05-27 11:47:29 +08:00
Ivan Ho
c76e2800e3 [MLIR][IRDL] Added IRDL to C++ Translation (#141248)
This PR introduces a new tool, mlir-irdl-to-cpp, that converts IRDL to
C++ definitions.

The C++ definitions allow use of the IRDL-defined dialect in MLIR C++
infrastructure, enabling the use of conversion patterns with IRDL
dialects for example. This PR also adds CMake utilities to easily
integrate the IRDL dialects into MLIR projects.

Note that most IRDL features are not supported. In general, we are only
able to define simple types and operations.

- The only type constraint supported is irdl.any.
- Variadic operands and results are not supported.
- Verifiers for the IRDL constraints are not generated.
- Attributes are not supported.

---------

Co-authored-by: Théo Degioanni <theo.degioanni.llvm.deluge062@simplelogin.fr>
Co-authored-by: Fehr Mathieu <mathieu.fehr@gmail.com>
2025-05-25 16:42:30 +02:00
Bruno Cardoso Lopes
faa4505bcb [MLIR][LLVM][DLTI] Handle data layout token 'Fn32' (#141167) 2025-05-23 14:17:45 -07:00
Adam Siemieniuk
0fa3ba7c39 [mlir][amx] Simplify intrinsic generation (#140559)
Replaces separate amx named intrinsic operations with direct calls to
LLVM intrinsic functions.
The existing amx tests are updated and expanded.

The separate conversion step translating amx intrinsics into LLVM IR is
eliminated. Instead, this step is now performed by the existing llvm
dialect infrastructure.

Related RFC:
https://discourse.llvm.org/t/rfc-simplify-x86-intrinsic-generation/85581/7
2025-05-23 14:16:09 +02:00
Bruno Cardoso Lopes
05494f3bad [MLIR][LLVM] Tail call support for inline asm op (#140826) 2025-05-22 15:30:31 -07:00
Tom Eccles
8d06d4c132 [mlir][OpenMP] Add translation of private_barrier attr to LLVMIR (#140090)
Part of a series to fix
https://github.com/llvm/llvm-project/issues/136357
2025-05-22 15:24:20 +01:00
Kazu Hirata
6074d83664 [mlir] Remove unused local variables (NFC) (#140990) 2025-05-21 20:33:53 -07:00
jeanPerier
80816e7923 [mlir][LLVM] handle ArrayAttr for constant array of structs (#139724)
While LLVM IR dialect has a way to represent arbitrary LLVM constant
array of structs via an insert chain, it is in practice very expensive
for the compilation time as soon as the array is bigger than a couple
hundred elements. This is because generating and later folding such
insert chain is really not cheap.

This patch allows representing array of struct constants via ArrayAttr in
the LLVM dialect.
2025-05-20 10:45:29 +02:00
NimishMishra
0baacd1a58 [flang][OpenMP] Support MLIR lowering of linear clause for omp.wsloop (#139385)
This patch adds support for MLIR lowering of linear clause on omp.wsloop
(except for linear modifiers).
2025-05-19 23:33:06 -07:00
Kazu Hirata
c2835e70fd [mlir] Remove unused local variables (NFC) (#140423) 2025-05-17 19:43:15 -07:00
Christian Ulmann
1001d6a6cd [MLIR][LLVM] Add import-structs-as-literals flag to the IR import (#140098)
This commit introduces the `import-structs-as-literals` option to the
MLIR import. This ensures that all struct types are imported as literal
structs, even when they are named in LLVM IR.
2025-05-16 08:43:06 +02:00
Bruno Cardoso Lopes
8d3a70770f [MLIR][LLVM] Improve inline asm importer (#139989)
Add support for importing more information into InlineAsmOp:
elementtype, side effects, align stack, asm dialect and operand attrs.
2025-05-15 17:38:05 -07:00
Sergio Afonso
0cd7e8aa91 [MLIR][OpenMP] Assert on map translation functions, NFC (#137199)
This patch adds assertions to map-related MLIR to LLVM IR translation
functions and utils to explicitly document whether they are intended for
host or device compilation only.

Over time, map-related handling has increased in complexity. This is
compounded by the fact that some handling is device-specific and some is
host-specific. By explicitly asserting on these functions on the
expected compilation pass, the flow should become slighlty easier to
follow.
2025-05-15 12:29:06 +01:00
Bruno Cardoso Lopes
d47cd5008b [MLIR][LLVM] Fix blockaddress mapping to LLVM blocks (#139814)
After each function is translated, both value and block maps are erased,
which makes the current mapping of blockaddresses to llvm blocks broken
- the patching happens only after *all* functions are translated.

Simplify the overall mapping, update comments, variable names and fix
the bug.

---------

Co-authored-by: Christian Ulmann <christianulmann@gmail.com>
2025-05-14 11:28:57 -07:00
Max Graey
8aaac80ddd [NFC] Use more isa and isa_and_nonnull instead dyn_cast for predicates (#137393)
Also fix some typos in comments

---------

Co-authored-by: Mehdi Amini <joker.eph@gmail.com>
2025-05-13 22:34:42 +08:00
Rahul Joshi
b17f3c63de [NFC][MLIR] Add {} for else when if body has {} (#139422) 2025-05-12 10:29:03 -07:00
Kazu Hirata
0159eb6367 [mlir] Use StringRef::consume_front (NFC) (#139459) 2025-05-11 10:46:35 -07:00
Igor Wodiany
c38910bbb9 [mlir][spirv] Add support for RelaxedPrecision in function arguments (#138685)
With the current implementation only one attribute is attached to the
argument and the deserializer fails if more decorations are specified,
however I believe that the spec does not prohibit having both
`Aliased`/`Restrict` and `RelaxedPrecision`. I am not sure how to attach
multiple attributes to a single argument with the current code and at
the same time I do not have a use case for it, so I think the patch in
the current state is a good starting point and can be extended in the
future.
2025-05-08 22:35:41 -07:00
Tom Eccles
e40200901c [mlir][OpenMP] cancel(lation point) taskgroup LLVMIR (#137841)
A cancel or cancellation point for taskgroup is always nested inside of
a task inside of the taskgroup. For the task which is cancelled, it is
that task which needs to be cleaned up: not the owning taskgroup.
Therefore the cancellation branch handler is done in the conversion of
the task not in conversion of taskgroup.

I added a firstprivate clause to the test for cancel taskgroup to
demonstrate that the block being branched to is the same block where
mandatory cleanup code is added. Cancellation point follows exactly the
same code path.
2025-05-08 11:15:58 +01:00
Tom Eccles
8338a3c92b [mlir][OpenMP] Convert omp.cancellation_point to LLVMIR (#137205)
This is basically identical to cancel except without the if clause.

taskgroup will be implemented in a followup PR.
2025-05-08 11:09:13 +01:00
Tom Eccles
a385c47a59 [mlir][OpenMP] convert wsloop cancellation to LLVMIR (#137194)
Taskloop support will follow in a later patch.
2025-05-08 11:08:52 +01:00
Bruno Cardoso Lopes
7682f663b5 [MLIR][LLVMIR] Import calls with mismatching signature as indirect call (#135895)
LLVM IR currently [accepts](https://godbolt.org/z/nqnEsW1ja):
```
define void @incompatible_call_and_callee_types() {
  call void @callee(i64 0)
  ret void
}

define void @callee({ptr, i64}, i32) {
  ret void
}
```

This currently fails to import. Even though these constructs are
dangerous and probably indicate some ODR violation (or optimization
bug), they are "valid" and should be imported into LLVM IR dialect. This
PR implements that by using an indirect call to represent it.
Translation already works nicely and outputs the same source llvm IR
file.

The error is now a warning, the tests in
`mlir/test/Target/LLVMIR/Import/import-failure.ll` already use `CHECK`
lines, so no need to add extra diagnostic tests.
2025-05-05 16:27:36 -07:00
Bruno Cardoso Lopes
29e4fb64b3 [MLIR][LLVM] Importer: fix void returning intrinsic calls (#138325) 2025-05-05 13:54:40 -07:00
Bruno Cardoso Lopes
28934fe4cf [MLIR][LLVM] Add ProfileSummary module flag support (#138070)
Add one more of these module flags. 

Unlike "CG Profile", LLVM proper does not verify the content of the
metadata, but returns a nullptr in case it's ill-formed (it's up to the
user to take action). This prompted me to implement warning checks,
preventing the importer to consume broken data.
2025-05-05 13:54:25 -07:00
Théo Degioanni
04e32b8a33 Revert "[MLIR][IRDL] Added IRDL to C++ Translation" (#138285)
Reverts llvm/llvm-project#133982
2025-05-02 16:36:30 +02:00
Ivan Ho
41035f4aa8 [MLIR][IRDL] Added IRDL to C++ Translation (#133982)
This PR introduces a new tool, mlir-irdl-to-cpp, that converts IRDL to
C++ definitions.

The C++ definitions allow use of the IRDL-defined dialect in MLIR C++
infrastructure, enabling the use of conversion patterns with IRDL
dialects for example. This PR also adds CMake utilities to easily
integrate the IRDL dialects into MLIR projects.

Note that most IRDL features are not supported. In general, we are only
able to define simple types and operations.
- The only type constraint supported is `irdl.any`.
- Variadic operands and results are not supported.
- Verifiers for the IRDL constraints are not generated.
- Attributes are not supported.

---------

Co-authored-by: Théo Degioanni <theo.degioanni.llvm.deluge062@simplelogin.fr>
Co-authored-by: Fehr Mathieu <mathieu.fehr@gmail.com>
2025-05-02 14:19:06 +02:00
Igor Wodiany
721c5cc327 [mlir][spirv] Allow yielding values from loop regions (#135344)
This change extends `spirv.mlir.loop` so it can yield values, the same
as `spirv.mlir.selection`.
2025-04-30 16:07:07 +01:00
Anchu Rajendran S
9764938224 [llvm][mlir] Adding instrument function entry and instrument function exit attributes (#137856) 2025-04-30 06:20:25 -07:00
Igor Wodiany
730f4a1ab3 [mlir][spirv] Split header and merge block in mlir.selections (#134875)
In the example below with the current code the first selection construct
(`if`/`else` in GLSL for simplicity) share its merge block with a header
block of the second construct.

```
bool _115;
if (_107)
{
    // ...
    _115 = _200 < _174;
}
else
{
    _115 = _107;
}
bool _123;
if (_115)
{
    // ...
    _123 = _213 < _174;
}
else
{
    _123 = _115;
}
```

This results in a malformed nesting of `mlir.selection` instructions
where one selection ends up inside a header block of another selection
construct. For example:

```
%61 = spirv.mlir.selection -> i1 {
  %80 = spirv.mlir.selection -> i1 {
    spirv.BranchConditional %60, ^bb1, ^bb2(%60 : i1)
  ^bb1:  // pred: ^bb0
    // ...
    spirv.Branch ^bb2(%101 : i1)
  ^bb2(%102: i1):  // 2 preds: ^bb0, ^bb1
    spirv.mlir.merge %102 : i1
  }
  spirv.BranchConditional %80, ^bb1, ^bb2(%80 : i1)
^bb1:  // pred: ^bb0
  // ...
  spirv.Branch ^bb2(%90 : i1)
^bb2(%91: i1):  // 2 preds: ^bb0, ^bb1
  spirv.mlir.merge %91 : i1
}
```

This change ensures that the merge block of one selection is not a
header block of another, splitting blocks if necessary. The existing
block splitting mechanism is updated to handle this case.
2025-04-30 13:38:36 +01:00
Igor Wodiany
6ae7177de8 [mlir][spirv] Update mergeInfo of blocks nested in regions (#137789)
The current code that updates mergeInfo iterates only over
constructBlocks, potentially leaving blocks nested in already structured
regions with invalid mergeInfo. This patch adds walk for each block to
ensure all nested blocks are considered.

It is not possible to add a unit test exercising this change as whether
the problem occurs depends on the structuring order that is currently
non-deterministic.
2025-04-30 11:29:58 +01:00
Peiyong Lin
96eeb6c1a9 [mlir][llvm] Support nusw and nuw in GEP (#137272)
nusw and nuw were introduced in getelementptr, this patch plumbs them in
MLIR.

Since inbounds implies nusw, this patch also adds an inboundsFlag to
represent the concept of raw inbounds with no nusw implication, and have
the inbounds literal captured as the combination of inboundsFlag and
nusw.

Fixes: iree#20482

Signed-off-by: Lin, Peiyong <linpyong@gmail.com>
2025-04-30 07:49:19 +02:00