aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/Attributes.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-09-01[MLIR] Add target_specific_attrs attribute to mlir.global (#154706)Vadim Curcă1-0/+13
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-07-02[IR] Introduce `dead_on_return` attributeAntonio Frighetto1-1/+2
Add `dead_on_return` attribute, which is meant to be taken advantage by the frontend, and states that the memory pointed to by the argument is dead upon function return. As with `byval`, it is supposed to be used for passing aggregates by value. The difference lies in the ABI: `byval` implies that the pointer is explicitly passed as argument to the callee (during codegen the copy is emitted as per byval contract), whereas a `dead_on_return`-marked argument implies that the copy already exists in the IR, is located at a specific stack offset within the caller, and this memory will not be read further by the caller upon callee return – or otherwise poison, if read before being written. RFC: https://discourse.llvm.org/t/rfc-add-dead-on-return-attribute/86871.
2025-06-13[llvm] Compare std::optional<T> to values directly (NFC) (#143913)Kazu Hirata1-1/+1
This patch transforms: X && *X == Y to: X == Y where X is of std::optional<T>, and Y is of T or similar.
2025-06-04[IR][DSE] Support non-malloc functions in malloc+memset->calloc fold (#138299)clubby7891-0/+4
Add a `alloc-variant-zeroed` function attribute which can be used to inform folding allocation+memset. This addresses https://github.com/rust-lang/rust/issues/104847, where LLVM does not know how to perform this transformation for non-C languages. Co-authored-by: Jamie <jamie@osec.io>
2025-05-12[NFCI][LLVM/MLIR] Adopt `TrailingObjects` convenience API (#138554)Rahul Joshi1-2/+2
Adopt `TrailingObjects` convenience API that was added in https://github.com/llvm/llvm-project/pull/138970 in LLVM and MLIR code.
2025-03-31[IR] Fix -Wunused-but-set-variableFangrui Song1-2/+2
2025-04-01llvm-reduce: Try to preserve instruction metadata as argument attributes ↵Matt Arsenault1-0/+32
(#133557) Fixes #131825
2025-02-13[IR][ModRef] Introduce `errno` memory locationAntonio Frighetto1-0/+3
Model C/C++ `errno` macro by adding a corresponding `errno` memory location kind to the IR. Preliminary work to separate `errno` writes from other memory accesses, to the benefit of alias analyses and optimization correctness. Previous discussion: https://discourse.llvm.org/t/rfc-modelling-errno-memory-effects/82972.
2025-01-29[IR] Convert from nocapture to captures(none) (#123181)Nikita Popov1-1/+4
This PR removes the old `nocapture` attribute, replacing it with the new `captures` attribute introduced in #116990. This change is intended to be essentially NFC, replacing existing uses of `nocapture` with `captures(none)` without adding any new analysis capabilities. Making use of non-`none` values is left for a followup. Some notes: * `nocapture` will be upgraded to `captures(none)` by the bitcode reader. * `nocapture` will also be upgraded by the textual IR reader. This is to make it easier to use old IR files and somewhat reduce the test churn in this PR. * Helper APIs like `doesNotCapture()` will check for `captures(none)`. * MLIR import will convert `captures(none)` into an `llvm.nocapture` attribute. The representation in the LLVM IR dialect should be updated separately.
2025-01-13[IR] Introduce captures attribute (#116990)Nikita Popov1-1/+33
This introduces the `captures` attribute as described in: https://discourse.llvm.org/t/rfc-improvements-to-capture-tracking/81420 This initial patch only introduces the IR/bitcode support for the attribute and its in-memory representation as `CaptureInfo`. This will be followed by a patch to upgrade and remove the `nocapture` attribute, and then by actual inference/analysis support. Based on the RFC feedback, I've used a syntax similar to the `memory` attribute, though the only "location" that can be specified is `ret`. I've added some pretty extensive documentation to LangRef on the semantics. One non-obvious bit here is that using ptrtoint will not result in a "return-only" capture, even if the ptrtoint result is only used in the return value. Without this requirement we wouldn't be able to continue ordinary capture analysis on the return value.
2024-10-17Reapply "[Inliner] Propagate more attributes to params when inlining ↵goldsteinn1-0/+15
(#91101)" (2nd Attempt) (#112749) Root cause of the bug was code hanging onto `range` attr after changing BitWidth. This was fixed in PR #112633.
2024-10-17[SimplifyLibCall][Attribute] Fix bug where we may keep `range` attr with ↵goldsteinn1-1/+6
incompatible type (#112649) In a variety of places we change the bitwidth of a parameter but don't update the attributes. The issue in this case is from the `range` attribute when inlining `__memset_chk`. `optimizeMemSetChk` will replace an `i32` with an `i8`, and if the `i32` had a `range` attr assosiated it will cause an error. Fixes #112633
2024-10-16Revert "[Inliner] Propagate more attributes to params when inlining (#91101)"Arthur Eubanks1-15/+0
This reverts commit ae778ae7ce72219270c30d5c8b3d88c9a4803f81. Creates broken IR, see comments in #91101.
2024-10-16[Inliner] Propagate more attributes to params when inlining (#91101)goldsteinn1-0/+15
- **[Inliner] Add tests for propagating more parameter attributes; NFC** - **[Inliner] Propagate more attributes to params when inlining** Add support for propagating: - `derefereancable` - `derefereancable_or_null` - `align` - `nonnull` - `range` These are only propagated if the parameter to the to-be-inlined callsite match the exact parameter used in the to-be-inlined function.
2024-10-04[IR] Fix '-Wparentheses' warnings. NFCMichael Liao1-5/+5
2024-10-02[SimplifyCFG][Attributes] Enabling sinking calls with differing number of ↵Noah Goldstein1-5/+3
attrsets Prior impl would fail if the number of attribute sets on the two calls wasn't the same which is unnecessary as long as we aren't throwing away and must-preserve attrs. Closes #110896
2024-10-02[IR] Allow fast math flags on calls with homogeneous FP struct types (#110506)Benjamin Maxwell1-5/+2
This extends FPMathOperator to allow calls that return literal structs of homogeneous floating-point or vector-of-floating-point types. The intended use case for this is to support FP intrinsics that return multiple values (such as `llvm.sincos`).
2024-10-01[IR][Attribute] Add support for intersecting AttributeLists; NFC (#109719)goldsteinn1-14/+219
Add support for taking the intersection of two AttributeLists s.t the result list contains attributes that are valid in the context of both inputs. i.e if we have `nonnull align(32) noundef` intersected with `nonnull align(16) dereferenceable(10)`, the result is `nonnull align(16)`. Further it handles attributes that are not-droppable. For example dropping `byval` can change the nature of a callsite/function so its impossible to correct a correct intersection if its dropped from the result. i.e `nonnull byval(i64)` intersected with `nonnull` is invalid. The motivation for the infrastructure is to enable sinking/hoisting callsites with differing attributes.
2024-09-23[IR][Attribute] Add missing `const` def on `addAllocSizeParamAttr()`; NFCNoah Goldstein1-1/+1
2024-08-08Allow empty range attribute and add assert for full range (#100601)Andreas Jonson1-0/+4
fix #99619
2024-08-04[llvm] Construct SmallVector with ArrayRef (NFC) (#101872)Kazu Hirata1-1/+1
2024-06-21Add the 'initializes' attribute langref and support (#84803)Haopeng Liu1-3/+87
We propose adding a new LLVM attribute, `initializes((Lo1,Hi1),(Lo2,Hi2),...)`, which expresses the notion of memory space (i.e., intervals, in bytes) that the argument pointing to is initialized in the function. Will commit the attribute inferring in the follow-up PRs. https://discourse.llvm.org/t/rfc-llvm-new-initialized-parameter-attribute-for-improved-interprocedural-dse/77337
2024-06-02[IR] Do not set `none` for function uwtable (#93387)Joshua Cao1-7/+2
This avoids the pitfall where we set the uwtable to none: ``` func.setUWTableKind(llvm::UWTableKind::None) ``` `Attribute::getAsString()` would see an unknown attribute and fail an assertion. In this patch, we assert that we do not see a None uwtable kind. This also skips the check of `UWTableKind::Async`. It is dominated by the check of `UWTableKind::Default`, which has the same enum value (nfc).
2024-05-02[NFC] Reduce copies created of ConstantRange when getting ↵Andreas Jonson1-4/+4
ConstantRangeAttributes (#90335) Think that it can be good to reduce the number of copies created when working with ConstantRangeAttributes.
2024-04-30[IR] Use StringRef::operator== instead of StringRef::equals (NFC) (#90550)Kazu Hirata1-1/+1
I'm planning to remove StringRef::equals in favor of StringRef::operator==. - StringRef::operator== outnumbers StringRef::equals by a factor of 22 under llvm/ in terms of their usage. - The elimination of StringRef::equals brings StringRef closer to std::string_view, which has operator== but not equals. - S == "foo" is more readable than S.equals("foo"), especially for !Long.Expression.equals("str") vs Long.Expression != "str".
2024-04-30[NFC] Remove method from FoldingSet that already existed in APInt. (#90486)Andreas Jonson1-2/+2
Noticed that there already was a function in APInt that updated a FoldingSet so there was no need for me to add it in https://github.com/llvm/llvm-project/pull/84617.
2024-04-25[InstCombine] Swap out range metadata to range attribute for cttz/ctlz/ctpop ↵Andreas Jonson1-0/+7
(#88776) Since all optimizations that use range metadata now also handle range attribute, this patch replaces writes of range metadata for call instructions to range attributes.
2024-03-09Reapply [IR] Add new Range attribute using new ConstantRange Attribute type ↵Andreas Jonson1-2/+79
(#84617) The only change from https://github.com/llvm/llvm-project/pull/83171 is the change of the allocator so the destructor is called for ConstantRangeAttributeImpl. reverts https://github.com/llvm/llvm-project/pull/84549
2024-03-08Revert "[IR] Add new Range attribute using new ConstantRange Attribute type" ↵Florian Mayer1-78/+2
(#84549) Reverts llvm/llvm-project#83171 broke sanitizer buildbot https://lab.llvm.org/buildbot/#/builders/168/builds/19110/steps/10/logs/stdio
2024-03-08[IR] Add new Range attribute using new ConstantRange Attribute type (#83171)Andreas Jonson1-2/+78
implementation as discussed in https://discourse.llvm.org/t/rfc-metadata-attachments-for-function-arguments/76420
2024-02-25[NFC] Turn the StrictFP attribute check to a CompatRule. (#82600)Dani1-0/+7
2024-02-23[llvm][AArch64] Do not inline a function with different signing scheme. (#80642)Dani1-0/+5
If the signing scheme is different that maybe the functions assumes different behaviours and dangerous to inline them without analysing them. This should be a rare case.
2023-12-14[IR] Add dead_on_unwind attribute (#74289)Nikita Popov1-1/+2
Add the `dead_on_unwind` attribute, which states that the caller will not read from this argument if the call unwinds. This allows eliding stores that could otherwise be visible on the unwind path, for example: ``` declare void @may_unwind() define void @src(ptr noalias dead_on_unwind %out) { store i32 0, ptr %out call void @may_unwind() store i32 1, ptr %out ret void } define void @tgt(ptr noalias dead_on_unwind %out) { call void @may_unwind() store i32 1, ptr %out ret void } ``` The optimization is not valid without `dead_on_unwind`, because the `i32 0` value might be read if `@may_unwind` unwinds. This attribute is primarily intended to be used on sret arguments. In fact, I previously wanted to change the semantics of sret to include this "no read after unwind" property (see D116998), but based on the feedback there it is better to keep these attributes orthogonal (sret is an ABI attribute, dead_on_unwind is an optimization attribute). This is a reboot of that change with a separate attribute.
2023-11-01[IR] Add writable attributeNikita Popov1-1/+2
This adds a writable attribute, which in conjunction with dereferenceable(N) states that a spurious store of N bytes is introduced on function entry. This implies that this many bytes are writable without trapping or introducing data races. See https://llvm.org/docs/Atomics.html#optimization-outside-atomic for why the second point is important. This attribute can be added to sret arguments. I believe Rust will also be able to use it for by-value (moved) arguments. Rust likely won't be able to use it for &mut arguments (tree borrows does not appear to allow spurious stores). In this patch the new attribute is only used by LICM scalar promotion. However, the actual motivation for this is to fix a correctness issue in call slot optimization, which needs this attribute to avoid optimization regressions. Followup to the discussion on D157499. Differential Revision: https://reviews.llvm.org/D158081
2023-07-03[MemoryEffects][NFCI] Make the MemoryEffects class reusableJohannes Doerfert1-4/+4
In a follow up we will reuse the logic in MemoryEffectsBase to merge AAMemoryLocation and AAMemoryBehavior without duplicating all the bit fiddling code already available in MemoryEffectsBase. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D153305
2023-06-27[llvm] Move AttributeMask to a separate headerElliot Goodrich1-0/+1
Move `AttributeMask` out of `llvm/IR/Attributes.h` to a new file `llvm/IR/AttributeMask.h`. After doing this we can remove the `#include <bitset>` and `#include <set>` directives from `Attributes.h`. Since there are many headers including `Attributes.h`, but not needing the definition of `AttributeMask`, this causes unnecessary bloating of the translation units and slows down compilation. This commit adds in the include directive for `llvm/IR/AttributeMask.h` to the handful of source files that need to see the definition. This reduces the total number of preprocessing tokens across the LLVM source files in lib from (roughly) 1,917,509,187 to 1,902,982,273 - a reduction of ~0.76%. This should result in a small improvement in compilation time. Differential Revision: https://reviews.llvm.org/D153728
2023-06-09Inline: Fix case of not inlining with denormal-fp-math-f32Matt Arsenault1-0/+4
This was failing to inline the opencl libraries with daz enabled. As a modifier to the base mode, denormal-fp-mode-f32 is weird and has no meaning if it's missing.
2023-04-29LangRef: Add "dynamic" option to "denormal-fp-math"Matt Arsenault1-0/+31
This is stricter than the default "ieee", and should probably be the default. This patch leaves the default alone. I can change this in a future patch. There are non-reversible transforms I would like to perform which are legal under IEEE denormal handling, but illegal with flushing zero behavior. Namely, conversions between llvm.is.fpclass and fcmp with zeroes. Under "ieee" handling, it is legal to translate between llvm.is.fpclass(x, fcZero) and fcmp x, 0. Under "preserve-sign" handling, it is legal to translate between llvm.is.fpclass(x, fcSubnormal|fcZero) and fcmp x, 0. I would like to compile and distribute some math library functions in a mode where it's callable from code with and without denormals enabled, which requires not changing the compares with denormals or zeroes. If an IEEE function transforms an llvm.is.fpclass call into an fcmp 0, it is no longer possible to call the function from code with denormals enabled, or write an optimization to move the function into a denormal flushing mode. For the original function, if x was a denormal, the class would evaluate to false. If the function compiled with denormal handling was converted to or called from a preserve-sign function, the fcmp now evaluates to true. This could also be of use for strictfp handling, where code may be changing the denormal mode. Alternative name could be "unknown". Replaces the old AMDGPU custom inlining logic with more conservative logic which tries to permit inlining for callees with dynamic handling and avoids inlining other mismatched modes.
2023-03-15clang: Emit nofpclass(nan inf) for -ffinite-math-onlyMatt Arsenault1-10/+13
Set this on any source level floating-point type argument, return value, call return or outgoing parameter which is lowered to a valid IR type for the attribute. Currently this isn't applied to emitted intrinsics since those don't go through ABI code.
2023-03-03ADT: Move FPClassTest printing functions to common placeMatt Arsenault1-52/+6
2023-02-24IR: Add nofpclass parameter attributeMatt Arsenault1-0/+101
This carries a bitmask indicating forbidden floating-point value kinds in the argument or return value. This will enable interprocedural -ffinite-math-only optimizations. This is primarily to cover the no-nans and no-infinities cases, but also covers the other floating point classes for free. Textually, this provides a number of names corresponding to bits in FPClassTest, e.g. call nofpclass(nan inf) @must_be_finite() call nofpclass(snan) @cannot_be_snan() This is more expressive than the existing nnan and ninf fast math flags. As an added bonus, you can represent fun things like nanf: declare nofpclass(inf zero sub norm) float @only_nans() Compared to nnan/ninf: - Can be applied to individual call operands as well as the return value - Can distinguish signaling and quiet nans - Distinguishes the sign of infinities - Can be safely propagated since it doesn't imply anything about other operands. - Does not apply to FP instructions; it's not a flag This is one step closer to being able to retire "no-nans-fp-math" and "no-infs-fp-math". The one remaining situation where we have no way to represent no-nans/infs is for loads (if we wanted to solve this we could introduce !nofpclass metadata, following along with noundef/!noundef). This is to help simplify the GPU builtin math library distribution. Currently the library code has explicit finite math only checks, read from global constants the compiler driver needs to set based on the compiler flags during linking. We end up having to internalize the library into each translation unit in case different linked modules have different math flags. By propagating known-not-nan and known-not-infinity information, we can automatically prune the edge case handling in most functions if the function is only reached from fast math uses.
2023-01-09[Attributes] Avoid duplicate hasAttribute() query (NFC)Nikita Popov1-4/+6
removeAttribute() already performs a hasAttribute() check, so no need to also do it in the caller. Instead check whether the attribute set was changed. This makes the implementations in line with removeAttributesAtIndex().
2023-01-09[Attributes] Avoid repeated attribute set lookup (NFC)Nikita Popov1-8/+8
Perform the hasAttribute() check on the AttributeSet we need to fetch anyway, rather than going through hasAttributeAtIndex().
2023-01-05[Attributes] Remove trailing empty attribute sets (PR59746)Nikita Popov1-21/+12
In setAttributesAtIndex(), remove any trailing empty attribute sets. Also make sure that all the different attribute removal APIs go through that method. Fixes https://github.com/llvm/llvm-project/issues/59746.
2023-01-05Move from llvm::makeArrayRef to ArrayRef deduction guides - llvm/ partserge-sans-paille1-1/+1
Use deduction guides instead of helper functions. The only non-automatic changes have been: 1. ArrayRef(some_uint8_pointer, 0) needs to be changed into ArrayRef(some_uint8_pointer, (size_t)0) to avoid an ambiguous call with ArrayRef((uint8_t*), (uint8_t*)) 2. CVSymbol sym(makeArrayRef(symStorage)); needed to be rewritten as CVSymbol sym{ArrayRef(symStorage)}; otherwise the compiler is confused and thinks we have a (bad) function prototype. There was a few similar situation across the codebase. 3. ADL doesn't seem to work the same for deduction-guides and functions, so at some point the llvm namespace must be explicitly stated. 4. The "reference mode" of makeArrayRef(ArrayRef<T> &) that acts as no-op is not supported (a constructor cannot achieve that). Per reviewers' comment, some useless makeArrayRef have been removed in the process. This is a follow-up to https://reviews.llvm.org/D140896 that introduced the deduction guides. Differential Revision: https://reviews.llvm.org/D140955
2022-12-02[IR] Use std::nullopt instead of None (NFC)Kazu Hirata1-4/+4
This patch mechanically replaces None with std::nullopt where the compiler would warn if None were deprecated. The intent is to reduce the amount of manual work required in migrating from Optional to std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-02Attributes: convert Optional to std::optionalKrzysztof Parzyszek1-33/+35
2022-11-26[llvm] Use std::size (NFC)Kazu Hirata1-2/+1
std::size, introduced in C++17, allows us to directly obtain the number of elements of an array.
2022-11-14[TableGen] Use MemoryEffects to represent intrinsic memory effects (NFCI)Nikita Popov1-1/+1
The TableGen implementation was using a homegrown implementation of FunctionModRefInfo. This switches it to use MemoryEffects instead. This makes the code simpler, and will allow exposing the full representational power of MemoryEffects in the future. Among other things, this will allow us to map IntrHasSideEffects to an inaccessiblemem readwrite, rather than just ignoring it entirely in most cases. To avoid layering issues, this moves the ModRef.h header from IR to Support, so that it can be included in the TableGen layer. Differential Revision: https://reviews.llvm.org/D137641
2022-10-31[Attributes] Add additional MemoryEffects APIs (NFC)Nikita Popov1-0/+19
This adds the usual complement of APIs for creating and fetching a non-trivial attribute. Split out from D135780.