415 Commits

Author SHA1 Message Date
Shilei Tian
70905e0afa
[RFC][IR] Remove Constant::isZeroValue (#181521)
`Constant::isZeroValue` currently behaves same as
`Constant::isNullValue` for all types except floating-point, where it
additionally returns true for negative zero (`-0.0`). However, in
practice, almost all callers operate on integer/pointer types where the
two are equivalent, and the few FP-relevant callers have no meaningful
dependence on the `-0.0` behavior.

This PR removes `isZeroValue` to eliminate the confusing API. All
callers are changed to `isNullValue` with no test failures.

`isZeroValue` will be reintroduced in a future change with clearer
semantics: when null pointers may have non-zero bit patterns,
`isZeroValue` will check for bitwise-all-zeros, while `isNullValue` will
check for the semantic null (which
may be non-zero).
2026-02-15 12:06:42 -05:00
paperchalice
5c5677d7b8
[llvm] Remove "no-infs-fp-math" attribute support (#180083)
One of global options in `TargetMachine::resetTargetOptions`, now all
backends no longer support it, remove it.
2026-02-09 08:43:33 +08:00
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
Jakub Kuderski
59e44799bd
[mlir] Fix new clang-tidy warning llvm-type-switch-case-types. NFC. (#178487)
Pre-commiting this before landing the new check in
https://github.com/llvm/llvm-project/pull/177892
2026-01-28 19:13:47 +00: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
Andy Kaylor
2a10e91d8f
[MLIR] Zero-extend unsigned and 1-bit values when translating IntegerAttr (#169751)
This updates the LLVM IR ConstantInt creation from mlir::IntegerAttr so
that unsigned integers and 1-bit integers are zero-extended rather than
sign-extended.
2025-12-04 12:05:36 -08:00
Rajat Bajpai
1748e2330e
[MLIR][Intrinsics] Add new MLIR API to automatically resolve overload types (#168188)
Add createIntrinsicCall overload that accepts return type and arguments,
automatically resolve overload types rather than requiring manual
computation. Simplifies NVVM_PrefetchOp by removing conditional overload
logic.
2025-11-27 10:33:24 +05:30
Men-cotton
30c49a4022
[mlir][LLVMIR] Handle anonymous TBAA roots during metadata emission (#169167)
This commit enhances MLIR's TBAA export with support for anonymous TBAA roots. The import for this was around for a bit but the export was missing.

Fixes: #160721
2025-11-25 08:37:36 +01:00
Nick Sarnie
4538818c79
[OpenMP][OMPIRBuilder] Use runtime CC for runtime calls (#168608)
Some targets have a specific calling convention that should be used for
generated calls to runtime functions.

Pass that down and use it.

Signed-off-by: Nick Sarnie <nick.sarnie@intel.com>
2025-11-21 15:40:20 +00: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
Mehdi Amini
03e7ce95a6 [MLIR] Apply clang-tidy fixes for llvm-else-after-return in ModuleTranslation.cpp (NFC) 2025-10-27 22:22:54 -07: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
Jakub Kuderski
0820266651
[mlir] Use llvm accumulate wrappers. NFCI. (#162957)
Use wrappers around `std::accumulate` to make the code more concise and
less bug-prone: https://github.com/llvm/llvm-project/pull/162129.

With `std::accumulate`, it's the initial value that determines the
accumulator type. `llvm::sum_of` and `llvm::product_of` pick the right
accumulator type based on the range element type.

Found some funny bugs like a local accumulate helper that calculated a
sum with initial value of 1 -- we didn't hit the bug because the code
was actually dead...
2025-10-11 11:33:18 -04:00
Alexey Bataev
cc0fecf619
[MLIR][MLIRToLLVM] Fix zero fp value to array translation (#160713)
The translator should not generate zeroinitilizer on fp all zero values,
since it may affect signedness of zeroes

Fixes #160437
2025-09-25 20:20:34 -04:00
Nick Sarnie
148e099b39
[MLIR][OpenMP] Set default address space for OpenMPIRBuilder (#158689)
Extension of https://github.com/llvm/llvm-project/pull/158152 for MLIR.

---------

Signed-off-by: Sarnie, Nick <nick.sarnie@intel.com>
2025-09-16 14:04:07 +00: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
Abid Qadeer
a44ce1b949
[mlir][debug] Convert DbgIntrOp to DebugRecords directly. (#154926)
Currently the `DbgDeclareOP/DbgValueOP/DbgLabelOp` are first converted
to llvm debug intrinsics which are later translated to debug records by
a call of `convertToNewDbgValues`. This is not only inefficient but also
makes the code that works on intermediate IR unnecessarily complicated.
The debug intrinsics are also being phased out. This PR converts these
Ops directly to debug records.

The conversion is relatively simple but there is a bit of code
repetition due to how the APIs in the `DIBuilders` are named. There are
few `cast<>` which I would like to do without but could not see a good
way around them. Any suggestions welcome here. Also noticed that
`DISubprogramAttr` is inherited from `DIScopeAttr` while in llvm, the
`DISubprogram` inherits from `DILocalScope`. I am going to fix this
separately and then we could use `FusedLocWith<LLVM::DILocalScopeAttr>`
and cast to `DILocalScope` will be much safer.

As the output remains the same, the existing tests cover this change. I
also ran the `GDB` tests with flang and there was no regression.

---------

Co-authored-by: Tobias Gysi <tobias.gysi@nextsilicon.com>
2025-08-29 18:45:28 +01: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
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
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
Kazu Hirata
c7b34b0b44
[mlir] Use a new constructor of ArrayRef (NFC) (#146009)
ArrayRef now has a new constructor that takes a parameter whose type
has data() and size().  This patch migrates:

  ArrayRef<T>(X.data(), X.size()

to:

  ArrayRef<T>(X)
2025-06-26 23:38:20 -07:00
Kazu Hirata
70dce3d987
[mlir] Migrate away from std::nullopt (NFC) (#145842)
ArrayRef has a constructor that accepts std::nullopt.  This
constructor dates back to the days when we still had llvm::Optional.

Since the use of std::nullopt outside the context of std::optional is
kind of abuse and not intuitive to new comers, I would like to move
away from the constructor and eventually remove it.

This patch replaces {} with std::nullopt.
2025-06-26 08:41:02 -07:00
Kajetan Puchalski
d3ed84ed67
[Utils][mlir] Fix interaction between CodeExtractor and OpenMPIRBuilder (#145051)
CodeExtractor can currently erroneously insert an alloca into a
different function than it inserts its users into, in cases where code
is being extracted out of a function that has already been outlined. Add
an assertion that the two blocks being inserted into are actually in the
same function.

Add a check to findAllocaInsertPoint in OpenMP to LLVMIR translation to
prevent the aforementioned scenario from happening.

OpenMPIRBuilder relies on a callback mechanism to fix-up a module later
on during the finaliser step. In some cases this results in the module
being invalid prior to the finalise step running. Remove calls to
verifyModule wrapped in LLVM_DEBUG from CodeExtractor, as the presence
of those results in the compiler crashing with -mllvm -debug due to
premature module verification where it would not crash without -debug.

Call ompBuilder->finalize() the end of mlir::translateModuleToLLVMIR, in
order to make sure the module has actually been finalized prior to
trying to verify it.

Resolves https://github.com/llvm/llvm-project/issues/138102.

---------

Signed-off-by: Kajetan Puchalski <kajetan.puchalski@arm.com>
2025-06-25 13:34:35 +01:00
Tom Eccles
cc756716cf
[mlir][NFC] Move LLVM::ModuleTranslation::SaveStack to a shared header (#144897)
This is so that we can re-use the same code in Flang.
2025-06-24 17:45:10 +01: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
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
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
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
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
Bruno Cardoso Lopes
faa4505bcb
[MLIR][LLVM][DLTI] Handle data layout token 'Fn32' (#141167) 2025-05-23 14:17:45 -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
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
Kazu Hirata
0159eb6367
[mlir] Use StringRef::consume_front (NFC) (#139459) 2025-05-11 10:46:35 -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
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