Skip to content

Commit

Permalink
[LangRef] Make !range, !nonnull and !align return poison instead of IUB
Browse files Browse the repository at this point in the history
Make violation of !range, !nonnull and !align metadata return poison
instead of causing immediate undefined behavior. This makes the
behavior match that of the nonnull and align parameter and return
value attributes. The previous behavior can be restored by additionally
specifying the !noundef metadata, same as with parameters.

Some benefits of this change are:

 * This is needed to fix #59888.
   Under current semantics, it is illegal to add !range annotations
   based on known bits. Unless we want to drop that optimization
   entirely, we need to change the !range semantics.
 * This allows preserving range/nonnull/align metadata on
   speculated loads. !noundef metadata needs to be dropped, but
   the poison-generating metadata can be retained.

I don't think there are really disadvantages to the change (apart
from the need to review and adjust optimizations for the new
semantics), as the old behavior is still available via !noundef,
so it should be strictly more flexible.

Differential Revision: https://reviews.llvm.org/D141386
  • Loading branch information
nikic committed Jan 12, 2023
1 parent 91181db commit 84a5d93
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions llvm/docs/LangRef.rst
Expand Up @@ -6391,10 +6391,10 @@ representing the maximum relative error, for example:
``range`` metadata may be attached only to ``load``, ``call`` and ``invoke`` of
integer types. It expresses the possible ranges the loaded value or the value
returned by the called function at this call site is in. If the loaded or
returned value is not in the specified range, the behavior is undefined. The
ranges are represented with a flattened list of integers. The loaded value or
the value returned is known to be in the union of the ranges defined by each
consecutive pair. Each pair has the following properties:
returned value is not in the specified range, a poison value is returned
instead. The ranges are represented with a flattened list of integers. The
loaded value or the value returned is known to be in the union of the ranges
defined by each consecutive pair. Each pair has the following properties:

- The type must match the type loaded by the instruction.
- The pair ``a,b`` represents the range ``[a,b)``.
Expand Down Expand Up @@ -10198,9 +10198,9 @@ The optional ``!nonnull`` metadata must reference a single
metadata name ``<empty_node>`` corresponding to a metadata node with no
entries. The existence of the ``!nonnull`` metadata on the
instruction tells the optimizer that the value loaded is known to
never be null. If the value is null at runtime, the behavior is undefined.
This is analogous to the ``nonnull`` attribute on parameters and return
values. This metadata can only be applied to loads of a pointer type.
never be null. If the value is null at runtime, a poison value is returned
instead. This is analogous to the ``nonnull`` attribute on parameters and
return values. This metadata can only be applied to loads of a pointer type.

The optional ``!dereferenceable`` metadata must reference a single metadata
name ``<deref_bytes_node>`` corresponding to a metadata node with one ``i64``
Expand All @@ -10220,13 +10220,16 @@ optimizer that the value loaded is known to be aligned to a boundary specified
by the integer value in the metadata node. The alignment must be a power of 2.
This is analogous to the ''align'' attribute on parameters and return values.
This metadata can only be applied to loads of a pointer type. If the returned
value is not appropriately aligned at runtime, the behavior is undefined.
value is not appropriately aligned at runtime, a poison value is returned
instead.

The optional ``!noundef`` metadata must reference a single metadata name
``<empty_node>`` corresponding to a node with no entries. The existence of
``!noundef`` metadata on the instruction tells the optimizer that the value
loaded is known to be :ref:`well defined <welldefinedvalues>`.
If the value isn't well defined, the behavior is undefined.
If the value isn't well defined, the behavior is undefined. If the ``!noundef``
metadata is combined with poison-generating metadata like ``!nonnull``,
violation of that metadata constraint will also result in undefined behavior.

Semantics:
""""""""""
Expand Down

0 comments on commit 84a5d93

Please sign in to comment.