201 Commits

Author SHA1 Message Date
Erich Keane
5283f46615
[CIR] Add tons of function infra, plus a handful of attributes (#179811)
This patch puts together a lot more of the CIR infrastructure for
function attributes, plus adds a bunch of 'TODO' messages for areas that
have been skipped.

Along the way, we also implement 8 attributes in some way: -Convergent
gets a little more work, to make the `noconvergent` C attribute have an
effect

-optsize/minsize are implemented, sourced from the command line

-nobuiltin is a call-only attribute that tells not to replace the
individual call with a builtin. This is a touch confusing, since
no-builtins is an attribute that means "don't replace anything in the
body of this function with builtins (from this list)". The spelling
confusion is existing, and it seems that changing the names away from
LLVM would be confusing.

-save_reg_params & zero_call_used_regs are boht pretty simple registers

-temp-func-name just passes a string to LLVM, consistent with existing
implementation.

-default-func-attrs is a difficult one. It takes command line arguments
and passes them as LLVM-IR attributes directly on functions/calls. In
the dialect, we are capturing these in their own attribute to pass them
on correctly. However, this is one we cannot recover from LLVM-IR for
obvious reasons, so we instead choose to let the 'passthrough' mechanism
work for those.
2026-02-06 14:37:48 +00:00
Matt Arsenault
2502e3b7ba
IR: Promote "denormal-fp-math" to a first class attribute (#174293)
Convert "denormal-fp-math" and "denormal-fp-math-f32" into a first
class denormal_fpenv attribute. Previously the query for the effective
denormal mode involved two string attribute queries with parsing. I'm
introducing more uses of this, so it makes sense to convert this
to a more efficient encoding. The old representation was also awkward
since it was split across two separate attributes. The new encoding
just stores the default and float modes as bitfields, largely avoiding
the need to consider if the other mode is set.

The syntax in the common cases looks like this:
  `denormal_fpenv(preservesign,preservesign)`
  `denormal_fpenv(float: preservesign,preservesign)`
  `denormal_fpenv(dynamic,dynamic float: preservesign,preservesign)`

I wasn't sure about reusing the float type name instead of adding a
new keyword. It's parsed as a type but only accepts float. I'm also
debating switching the name to subnormal to match the current
preferred IEEE terminology (also used by nofpclass and other
contexts).

This has a behavior change when using the command flag debug
options to set the denormal mode. The behavior of the flag
ignored functions with an explicit attribute set, per
the default and f32 version. Now that these are one attribute,
the flag logic can't distinguish which of the two components
were explicitly set on the function. Only one test appeared to
rely on this behavior, so I just avoided using the flags in it.

This also does not perform all the code cleanups this enables.
In particular the attributor handling could be cleaned up.

I also guessed at how to support this in MLIR. I followed
MemoryEffects as a reference; it appears bitfields are expanded
into arguments to attributes, so the representation there is
a bit uglier with the 2 2-element fields flattened into 4 arguments.
2026-02-05 13:31:26 +00:00
Erich Keane
3db2fd8bf0
[CIR] Implement 'allocsize' function/call attribute lowering (#179342)
The alloc_size attribute takes the argument number(normalized to the
    index!) of the element size and count, for things like 'malloc' or
'calloc'.

This ends up being slightly more complicated than others, as this has
data that we have to decide on a format for. LLVM chooses to pack both
of these 32 bit values into a single i64, but unpacks it for the purpose
of input/output. The second value, the number of elements, is optional.

This patch uses a DenseI32ArrayAttr to store them for the LLVMIR
dialect, which gets us the packed nature, but doesn't require us doing
any work to unpack it.
2026-02-03 12:54:28 -08:00
Erich Keane
8cf99c263e
[CIR] Implement lowering for 'no-builtins' attributes (#178899)
This patch adds the 'no-builtins' and 'no-builtin-XXX' attributes from
LLVM-IR to both LLVMIR-MLIR and Clang lowering. However, I've done a
slightly different implementation of them.

LLVM-IR represents them as 'no-builtins' and 'no-builtin-NAME', where
the latter can be multiple names. This is problematic for the MLIR for a
variety of reasons, most particularly is our preference for explicit
attribute (of which the latter is an infinite list). Additionally of
course, our inability to have dashes in attribute names is troublesome.

Therefore, I've lowered them instead as `nobuiltins` for both, which is
an array attribute.

IF the array attribute is empty, it is intended to mean 'all functions'
(ie, the same as `no-builtins`), else it is a list of StringAttrs that
contain the variants of `NAME`.

I considered using nobuiltins=['*'] for the 'all functions', but that
seemed like a differentiation without purpose.
2026-02-02 15:04:52 +00:00
Erich Keane
6e0881c541
[CIR] 3 more 'quick' function attribute lowering through LLVMIRDialect (#178443)
This patch lowers 3 more attributes, two of which are trivial, and one
which has a touch of a complication.

The two trivial ones are no_caller_saved_registers and nocallback, which
are language-level attributes that are effectively just passed on.

The final one is a touch more complicated, as it is a 'string'
attribute: modular-format. Also, it has a dash in the LLVM-IR version,
but that isn't possible to add as a name in the LLVM-IR MLIR Dialect
(see the comment inline). It also has a string of some consequence (that
is checked in LLVM), but that is just passed to LLVM directly.
2026-01-30 06:08:41 -08:00
Erich Keane
6682681fd2
[CIR] Implement returns_twice, cold, hot, noduplicate, convergent func attrs (#178289)
Continuing my quest to get most of the attributes completed, this patch
implements 5 attributes for CIR/Clang CIR codegen.

4 of the 5 are also implemented in LLVM-MLIR, since 'convergent' was
already there.

As a part of this, we also had to make sure that attributes were handled
properly for Call operation lowering, like we do for function
attributes.
2026-01-28 06:06:56 -08:00
Erich Keane
c183af34af
[CIR] Implement 'noreturn' attribute for functions/calls. (#177978)
This mirrors what LLVM does, and requires propagating into the LLVM
dialect: When the user specifies 'noreturn' we propagate this down
throughout the stack.

Note the similar 'willreturn' is too strong of a guarantee (in that they
are not opposites of each other, as there is a 'unknown' implied by all
others), so we cannot use that on non-noreturn functions.
2026-01-27 11:00:39 -08:00
Victor Chernyakin
c438773432
[LLVM][ADT] Migrate users of make_scope_exit to CTAD (#174030)
This is a followup to #173131, which introduced the CTAD functionality.
2026-01-02 20:42:56 -08:00
Bala_Bhuvan_Varma
dc8fde0821
[MLIR][LLVMIR] Fix LLVM IR import of ZeroInitializers to constant zero (#171107)
Constant zero aggregate structures are imported to from llvm IR as
undef.
This includes for example LandingPad Instructions which have zero value
filters, structs.

This fixes the import to use the zeroOp to materialize a
zero-initialized constant.
2025-12-10 07:41:54 +01:00
Longsheng Mou
f817a1b039
[NFC] Fix typo of integer (#169325) 2025-11-25 16:06:13 +08:00
Tobias Gysi
31127b9e22
[mlir][llvm] Handle debug record import edge cases (#168774)
This commit enables the direct import of debug records by default and
fixes issues with two edge cases:
- Detect early on if the address operand is an argument list (calling
getAddress() for argument lists asserts)
- Use getAddress() to check if the address operand is null, which means
the address operand is an empty metadata node, which currently is not
supported.
- Add support for debug label records.

This is a follow-up to:
https://github.com/llvm/llvm-project/pull/167812
2025-11-21 08:54:45 +01:00
darkbuck
2f6f045ea8
[mlir][LLVM] Resync memory effect attribute with LLVM IR (#168568)
- Add missing locations, namely 'ErrnoMem', 'TargetMem0', and
'TargetMem1'.
2025-11-19 11:56:04 -05:00
Bruno Cardoso Lopes
3f0ef2765c
[MLIR][LLVM] Debug info: import debug records directly (#167812)
Effectively means we don't need to call into
`llvmModule->convertFromNewDbgValues()` anymore. Added a flag to allow
users to access the old behavior.
2025-11-14 10:22:56 -08:00
paperchalice
b0c89a9295
[mlir][LLVMIR] Remove "unsafe-fp-math" attribute support (#162782)
These global flags block furthur improvements for clang, users should
always use fast-math flags
see also
https://discourse.llvm.org/t/rfc-honor-pragmas-with-ffp-contract-fast/80797
Remove them incrementally, this is the mlir part.
2025-10-21 21:27:34 +08:00
Morris Hafner
0f35df35d1
[MLIR][LLVM] Add inline_hint as a first class function attribute (#163324)
We have `noinline` and `alwaysinline` present as first class function
attributes. Add `inline_hint` to the list of function attributes as
well.

Update the module import and translation to support the new attribute.

The verifier does not need to be changed as `inlinehint` does not
conflict with `noinline` or `alwaysinline`.

`inline_hint` is needed to support the `inline` C/C++ keyword in CIR.
2025-10-16 17:38:24 +07:00
Mehdi Amini
c6f22b4cdc [MLIR] Apply clang-tidy fixes for bugprone-argument-comment in ModuleImport.cpp (NFC) 2025-09-27 02:59:22 -07:00
Mehdi Amini
fc525bf422 [MLIR] Apply clang-tidy fixes for llvm-qualified-auto in ModuleImport.cpp (NFC) 2025-09-24 07:08:10 -07:00
Vadim Curcă
4b226318cc
[MLIR] Add target_specific_attrs attribute to mlir.global (#154706)
Adds a `target_specific_attrs` optional array attribute to
`mlir.global`, as well as conversions to and from LLVM attributes on
`llvm::GlobalVariable` objects. This is necessary to preserve unknown
attributes on global variables when converting to and from the LLVM
Dialect. Previously, any attributes on an `llvm::GlobalVariable` not
explicitly modeled by `mlir.global` were dropped during conversion.
2025-09-01 12:39:58 +02:00
paperchalice
205d461a19
[IR][CodeGen] Remove "approx-func-fp-math" attribute (#155740)
Remove "approx-func-fp-math" attribute and related command line option,
users should always use afn flag in IR.
Resolve FIXME in `TargetMachine::resetTargetOptions` partially.
2025-08-29 09:52:07 +08:00
Rolf Morel
cbfa265e98
[MLIR][LLVMIR][DLTI] Add LLVM::TargetAttrInterface and #llvm.target attr (#145899)
Adds the `#llvm.target<triple = $TRIPLE, chip = $CHIP, features =
$FEATURES>` attribute and along with a `-llvm-target-to-data-layout`
pass to derive a MLIR data layout from the LLVM data layout string
(using the existing `DataLayoutImporter`). The attribute implements the
relevant DLTI-interfaces, to expose the `triple`, `chip` (AKA `cpu`) and
`features` on `#llvm.target` and the full `DataLayoutSpecInterface`. The
pass combines the generated `#dlti.dl_spec` with an existing `dl_spec`
in case one is already present, e.g. a `dl_spec` which is there to
specify size of the `index` type.

Adds a `TargetAttrInterface` which can be implemented by all attributes
representing LLVM targets.

Similar to the Draft PR https://github.com/llvm/llvm-project/pull/78073.

RFC on which this PR is based:
https://discourse.llvm.org/t/mandatory-data-layout-in-the-llvm-dialect/85875
2025-08-20 22:00:30 +01:00
gitoleg
49b0014742
[mlir][llvm] adds an attribute for the module level assembly (#151318)
Adds support for the module level assembly in the LLVM IR dialect.

---------

Co-authored-by: Tobias Gysi <tobias.gysi@nextsilicon.com>
2025-07-31 14:10:21 +02:00
Tobias Gysi
deced287ad
Reapply "[mlir][llvm] Add intrinsic arg and result attribute support … (#151324)
…(… (#151099)

This reverts commit 2780b8f22058b35a8e70045858b87a1966df8df3 and relands
b7bfbc0c4c7b20d6623a5b0b4a7fea8ae08a62da.

Adds the following fixes compared to the original PR
(https://github.com/llvm/llvm-project/pull/150783):
- A bazel fix
- Use `let methods` instead of `list<InterfaceMethod> methods`

The missing forward declaration has been added in meantime:
9164d206b3.
2025-07-30 15:26:41 +02:00
Mehdi Amini
97fa9a1f53
Revert "Reland "[mlir][llvm] Add intrinsic arg and result attribute support (…" (#151316)
Reverts llvm/llvm-project#151125

Broke the gcc-7 build:

include/mlir/Target/LLVMIR/ModuleTranslation.h:318:34: error: no type
named 'CallBase' in namespace 'llvm'
                           llvm::CallBase *call,
                           ~~~~~~^
2025-07-30 13:50:12 +02:00
Tobias Gysi
b7bfbc0c4c
Reland "[mlir][llvm] Add intrinsic arg and result attribute support (… (#151125)
…… (#151099)

This reverts commit 2780b8f22058b35a8e70045858b87a1966df8df3 to reland
59013d44058ef423a117f95092150e16e16fdb09.

In addition to the original commit this one includes:
- This includes a bazel fix
- Use `let methods` instead of `list<InterfaceMethod> methods`

The original commit message was:

This patch extends the LLVM dialect's intrinsic infra to support
argument and result attributes. Initial support is added for the memory
intrinsics llvm.intr.memcpy, llvm.intr.memmove, and llvm.intr.memset.

Additionally, an ArgAndResultAttrsOpInterface is factored out of
CallOpInterface and CallableOpInterface, enabling operations to have
argument and result attributes without requiring them to be a call or a
callable operation.
2025-07-30 13:35:20 +02:00
Tobias Gysi
2780b8f220
Revert "[mlir][llvm] Add intrinsic arg and result attribute support (… (#151099)
…#150783)"

This reverts commit 59013d44058ef423a117f95092150e16e16fdb09.

The change breaks a flang build bot:
https://lab.llvm.org/buildbot/#/builders/207/builds/4441
2025-07-29 10:15:08 +02:00
Tobias Gysi
59013d4405
[mlir][llvm] Add intrinsic arg and result attribute support (#150783)
This patch extends the LLVM dialect's intrinsic infra to support
argument and result attributes. Initial support is added for the memory
intrinsics `llvm.intr.memcpy`, `llvm.intr.memmove`, and
`llvm.intr.memset`.

Additionally, an ArgAndResultAttrsOpInterface is factored out of
CallOpInterface and CallableOpInterface, enabling operations to have
argument and result attributes without requiring them to be a call or a
callable operation.
2025-07-29 09:19:06 +02:00
Maksim Levental
258daf5395
[mlir][NFC] update mlir create APIs (34/n) (#150660)
See https://github.com/llvm/llvm-project/pull/147168 for more info.
2025-07-25 12:36:54 -05:00
Maksim Levental
2736fbd832
[mlir][NFC] update mlir/lib create APIs (26/n) (#149933)
See https://github.com/llvm/llvm-project/pull/147168 for more info.

---------

Co-authored-by: Tobias Gysi <tobias.gysi@nextsilicon.com>
2025-07-22 08:40:42 -04:00
Robert Konicar
46c059f925
[mlir][LLVMIR] Add IFuncOp to LLVM dialect (#147697)
Add IFunc to LLVM dialect and add support for lifting/exporting LLVMIR
IFunc.
2025-07-17 19:20:31 +02:00
Kazu Hirata
38b8ef16f7
[mlir] Remove unused includes (NFC) (#147158)
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-05 10:38:27 -07: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
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
05494f3bad
[MLIR][LLVM] Tail call support for inline asm op (#140826) 2025-05-22 15:30:31 -07:00
Kazu Hirata
6074d83664
[mlir] Remove unused local variables (NFC) (#140990) 2025-05-21 20:33:53 -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
Rahul Joshi
b17f3c63de
[NFC][MLIR] Add {} for else when if body has {} (#139422) 2025-05-12 10:29:03 -07: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
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
Anchu Rajendran S
9764938224
[llvm][mlir] Adding instrument function entry and instrument function exit attributes (#137856) 2025-04-30 06:20:25 -07: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
Bruno Cardoso Lopes
a5024cd0d7
[MLIR][LLVM] More on CG Profile: support null function entries (#137269) 2025-04-28 15:03:09 -07:00
Bruno Cardoso Lopes
c0bc775124
[MLIR][LLVM] Add CG Profile module flags support (#137115)
Dialect only accept arbitrary module flag values in face of simple types
like int and string. Whenever metadata is a bit more complex use
specific attributes to map functionality. This PR adds an attribute to
represent "CG Profile" entries, verifiers, import / translate support.
2025-04-24 16:42:54 -07:00
Bruno Cardoso Lopes
e646642050
[MLIR][LLVM] Allow strings in module flag value (#136793)
Expand support a bit beyond integers.

Next step is to support more complex metadata values (e.g. !"CG Profile"
and !"ProfileSummary"), but that's a bit more complex and deserves it
own PR.
2025-04-23 21:11:36 -07:00
Bruno Cardoso Lopes
c179847113
Reapply [MLIR][LLVM] Support for indirectbr (#136378)
Fix msan issue that caused revert in
https://github.com/llvm/llvm-project/pull/135695

### Original message

Now that LLVM dialect has `blockaddress` support, introduce
import/translation for `indirectbr` instruction.
2025-04-21 10:25:56 -07:00
Will Froom
046a1e629c
[MLIR:LLVM] Add UWTableKind attribute (#135811)
Add `UWTableKind` enum and corresponding attribute to `llvm.func`
including translation to `llvm::Function` attribute.
2025-04-17 09:45:43 +01:00
Bruno Cardoso Lopes
76db259080
Revert "[MLIR][LLVM] Support for indirectbr" (#135695)
Reverts llvm/llvm-project#135092, broke
https://lab.llvm.org/buildbot/#/builders/169/builds/10469
2025-04-14 16:08:49 -07:00
Bruno Cardoso Lopes
52166339f1
[MLIR][LLVM] Support for indirectbr (#135092)
Now that LLVM dialect has `blockaddress` support, introduce import/translation for `indirectbr` instruction.
2025-04-14 14:11:30 -07:00
Jean-Didier PAILLEUX
aeb06c6152
[MLIR] Adding 'inline_hint' attribute on LLMV::CallOp (#134582)
Addition of `inlinehint` attributes for CallOps in MLIR in order to be
able to say to a function call that the inlining is desirable without
having the attribute on the FuncOp.
2025-04-11 09:31:18 +02:00
Matthias Springer
a0d449016b
[mlir][LLVM] Delete getVectorElementType (#134981)
The LLVM dialect no longer has its own vector types. It uses
`mlir::VectorType` everywhere. Remove `LLVM::getVectorElementType` and
use `cast<VectorType>(ty).getElementType()` instead. This commit
addresses a
[comment](https://github.com/llvm/llvm-project/pull/133286#discussion_r2022192500)
on the PR that deleted the LLVM vector types.

Also improve vector type constraints by specifying the
`mlir::VectorType` C++ class, so that explicit casts to `VectorType` can
be avoided in some places.
2025-04-09 21:35:32 +02:00