Skip to content

Commit 446e95b

Browse files
committed
[LangRef] Relax semantics of writeonly / memory(write)
Instead of making writes immediate undefined behavior, consider these attributes in terms of their externally observable effects. We don't care if a location is read within the function, as long as it has no impact on observed behavior. In particular, allow: * Reading a location after writing it. * Reading a location before writing it (within the function) returns a poison value. The latter could be further relaxed to also allow things like "reading the value and then writing it back", but I'm not sure how one would specify that operationally (so that proof checkers can verify it). Fixes #95152.
1 parent ccaaa00 commit 446e95b

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

llvm/docs/LangRef.rst

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,8 +1598,10 @@ Currently, only the following parameter attributes are defined:
15981598
through this pointer argument (even though it may read from the memory that
15991599
the pointer points to).
16001600

1601-
If a function reads from a writeonly pointer argument, the behavior is
1602-
undefined.
1601+
This attribute is understood in the same way as the ``memory(write)``
1602+
attribute. That is, the pointer may still be read as long as the read is
1603+
not observable outside the function. See the ``memory`` documentation for
1604+
precise semantics.
16031605

16041606
``writable``
16051607
This attribute is only meaningful in conjunction with ``dereferenceable(N)``
@@ -1973,6 +1975,21 @@ example:
19731975
- ``memory(readwrite, argmem: none)``: May access any memory apart from
19741976
argument memory.
19751977

1978+
The supported access kinds are:
1979+
1980+
- ``readwrite``: Any kind of access to the location is allowed.
1981+
- ``read``: The location is only read. Writing the location is immediate
1982+
undefined behavior. This includes the case where the location is read and
1983+
then the same value is written back.
1984+
- ``write``: Only writes to the location are observable outside the function
1985+
call. However, the function may still internally read the location after
1986+
writing it, as this is not observable. Reading the location prior to
1987+
writing it results in a poison value.
1988+
- ``none``: No reads or writes to the location are observed outside the
1989+
function. It is always valid read and write allocas, and read global
1990+
constants, even if ``memory(none)`` is used, as these effects are not
1991+
externally observable.
1992+
19761993
The supported memory location kinds are:
19771994

19781995
- ``argmem``: This refers to accesses that are based on pointer arguments

0 commit comments

Comments
 (0)