427 Commits

Author SHA1 Message Date
Johannes Doerfert
8b4e1287ac [Attributor][FIX] Consistently use the access/remote instruction
We use the actual access (=remote) instruction when reasoning about
accesses, except for one leftover use case of the local instruction.
This caused us to potentially ignore the dominating write if the read
and write were in a different function than the (stack) allocation.

Reported by @ye-luo
2023-01-12 15:21:34 -08:00
Johannes Doerfert
2d9f44512e [Attributor] Make AAIsDeadFunction lazy 2023-01-12 00:14:06 -08:00
Guillaume Chatelet
8fd5558b29 [NFC] Use TypeSize::geFixedValue() instead of TypeSize::getFixedSize()
This change is one of a series to implement the discussion from
https://reviews.llvm.org/D141134.
2023-01-11 16:49:38 +00:00
Johannes Doerfert
b84ea7a475 [Attributor] Improve use of dominating writes during reasoning
This resolves a recent regression introduced by a bug fix and allows us
to use dominating write information (formerly HasBeenWrittenTo
information) to skip potential interfering accesses.

Generally, there are two changes here:
1) If we have dominating writes they form a chain and we can look at the
   least one to minimize the distance between the write and the (read)
   access in question.
2) If such a least dominating write exists, we can ignore writes in
   other functions as long as they cannot be reached from code between
   this write and the (read) access in question.

We have all the tools available to make such queries and the positive
tests show the result. Note that the negative test from the bug fix is
still in tree and not affected.

As a side-effect, we can remove the (arbitrary) treshold now on the
number of interfering accesses since we do not iterate over dominating
ones anymore.
2023-01-10 14:21:53 -08:00
Johannes Doerfert
31ad4dbcb9 Reapply "[Attributor] Introduce AA[Intra/Inter]Reachability"
This reverts commit e425a4c45618fcfa8ffb13be4ddfaa5d28aa38f1 after the
memory leak has been fixed.
2023-01-10 12:29:24 -08:00
Johannes Doerfert
1b9ba5856a [Attributor] Allow cfg reasoning for thread-local objects
If an object (=memory) is thread-local we do not need to worry about
threading effects.
2023-01-09 16:40:20 -08:00
Caroline Tice
2700d0d13a [AAUnderlyingObjects] Initialize an uninitialized boolean.
Recent commit introducing AA for getting underluying objects of a
pointer created an uninitialized boolean, which causes tests to fail
when built unter asan/ubsan. This initialized that variable.
2023-01-09 15:46:15 -08:00
Shilei Tian
acd22b2751 [AAUnderlyingObjects] Introduce an AA for getting underlying objects of a pointer
This patch introduces a new AA `AAUnderlyingObjects`. It is basically like a wrapper
AA of the function `AA::getAssumedUnderlyingObjects`, but it can recursively do
query if the underlying object is an indirect access, such as a phi node or a select
instruction.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D141164
2023-01-08 16:45:50 -05:00
Sameer Sahasrabuddhe
a73e0c306c [AAPointerInfo] fix assertion at the pass-through use of a pointer
HandlePassthroughUser may sometimes create a new entry for the OffsetInfo of a
user in the OffsetInfoMap. This can invalidate outstanding references into the
map, including the one which needs to be copied into the new entry. This
produces invalid offset info that can trigger assertions.

Fixed this by not using references at this point. The bug was originally
introduced in commit ID 0dc0a441323d41b4860668f38d290579e0de130c.

Reviewed By: ronlieb

Differential Revision: https://reviews.llvm.org/D140837
2023-01-04 16:53:55 +05:30
Johannes Doerfert
b0cee1f4ba [Attributor][FIX] Treat undef as zero offset in AAPointerInfoFloating 2023-01-03 13:09:23 -08:00
Sameer Sahasrabuddhe
e467a42943 [Attributor] potential constant values for PHI and Load
AAPotentialConstantValues now works for PHI and Load by simply examinig
AAPotentialValues for the instruction itself.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D140371
2022-12-21 15:59:00 +05:30
Johannes Doerfert
4e0f464ce2 Reapply "[OpenMP][FIX] Restrict more unsound assmptions about threading"
This reverts commit 3b052558125cbedf18c2ddb65780b50d6f437d54.

This patch got reverted due to an unrelated memory leak that has been
fixed.
2022-12-19 18:27:52 -08:00
Sameer Sahasrabuddhe
9c1b82599d [AAPointerInfo] handle multiple offsets in PHI
Previously reverted in 8b446ea2ba39e406bcf940ea35d6efb4bb9afe95

Reapplying because this commit is NOT DEPENDENT on the reverted commit
fc21f2d7bae2e0be630470cc7ca9323ed5859892, which broke the ASAN buildbot.
See https://reviews.llvm.org/rGfc21f2d7bae2e0be630470cc7ca9323ed5859892 for
more information.

The arguments to a PHI may represent a recurrence by eventually using the output
of the PHI itself. This is now handled by checking for cycles in the control
flow. If a PHI is not in a recurrence, it is now able to report multiple offsets
instead of conservatively reporting unknown.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D138991
2022-12-18 10:51:20 +05:30
Fangrui Song
53243f2a29 std::optional::value => operator*/operator->
value() has undesired exception checking semantics and calls
__throw_bad_optional_access in libc++. Moreover, the API is unavailable without
_LIBCPP_NO_EXCEPTIONS on older Mach-O platforms (see
_LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS).

This fixes check-clang-tools.
2022-12-17 03:12:04 +00:00
Mitch Phillips
e425a4c456 Revert "[Attributor] Introduce AA[Intra/Inter]Reachability"
This reverts commit fc21f2d7bae2e0be630470cc7ca9323ed5859892.

This patch broke the ASan buildbot. See
https://reviews.llvm.org/rGfc21f2d7bae2e0be630470cc7ca9323ed5859892 for
more information.
2022-12-16 17:56:48 -08:00
Mitch Phillips
3b05255812 Revert "[OpenMP][FIX] Restrict more unsound assmptions about threading"
This reverts commit 07c375348083170e39c9498a42a9679c7e08f07f.

Reason: This change is dependent on a commit that needs to be rolled
back because it broke the ASan buildbot. See
https://reviews.llvm.org/rGfc21f2d7bae2e0be630470cc7ca9323ed5859892 for
more information.
2022-12-16 17:56:38 -08:00
Mitch Phillips
525d6c54b5 Revert "[AAPointerInfo] handle multiple offsets in PHI"
This reverts commit 88db516af69619d4326edea37e52fc7321c33bb5.

Reason: This change is dependent on a commit that needs to be rolled
back because it broke the ASan buildbot. See
https://reviews.llvm.org/rGfc21f2d7bae2e0be630470cc7ca9323ed5859892 for
more information.
2022-12-16 17:55:48 -08:00
Mitch Phillips
7928a6387f Revert "Revert "[AAPointerInfo] handle multiple offsets in PHI""
This reverts commit 12696d302d146ffe616eecab3feceba9d29be2db.

Reason: This change is dependent on a commit that needs to be rolled
back because it broke the ASan buildbot. See
https://reviews.llvm.org/rGfc21f2d7bae2e0be630470cc7ca9323ed5859892 for
more information.
2022-12-16 17:55:38 -08:00
Mitch Phillips
8b446ea2ba Revert "[AAPointerInfo] handle multiple offsets in PHI"
This reverts commit 179ed8871101cd197e0a719a3629cd5077b1a999.

Reason: This change is dependent on a commit that needs to be rolled
back because it broke the ASan buildbot. See
https://reviews.llvm.org/rGfc21f2d7bae2e0be630470cc7ca9323ed5859892 for
more information.
2022-12-16 17:54:44 -08:00
Fangrui Song
21c4dc7997 std::optional::value => operator*/operator->
value() has undesired exception checking semantics and calls
__throw_bad_optional_access in libc++. Moreover, the API is unavailable without
_LIBCPP_NO_EXCEPTIONS on older Mach-O platforms (see
_LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS).

This fixes clang.
2022-12-17 00:42:05 +00:00
Sameer Sahasrabuddhe
179ed88711 [AAPointerInfo] handle multiple offsets in PHI
Previously reverted in 12696d302d146ffe616eecab3feceba9d29be2db

The arguments to a PHI may represent a recurrence by eventually using the output
of the PHI itself. This is now handled by checking for cycles in the control
flow. If a PHI is not in a recurrence, it is now able to report multiple offsets
instead of conservatively reporting unknown.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D138991
2022-12-15 12:23:50 +05:30
Sameer Sahasrabuddhe
12696d302d Revert "[AAPointerInfo] handle multiple offsets in PHI"
This reverts commit 88db516af69619d4326edea37e52fc7321c33bb5.
2022-12-15 10:14:39 +05:30
Sameer Sahasrabuddhe
88db516af6 [AAPointerInfo] handle multiple offsets in PHI
The arguments to a PHI may represent a recurrence by eventually using the output
of the PHI itself. This is now handled by checking for cycles in the control
flow. If a PHI is not in a recurrence, it is now able to report multiple offsets
instead of conservatively reporting unknown.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D138991
2022-12-15 08:48:38 +05:30
Johannes Doerfert
07c3753480 [OpenMP][FIX] Restrict more unsound assmptions about threading
Even if all loads and stores are in `nosync` functions we cannot
guarantee there is no synchronization going on between them. As such, we
cannot use CFG reasoning. We could check the entire module, or, what
happens now to minimize test churn, is to check if all accesses are in
the same function that is `nosync`. A follow up will undo some of the
regressions where possible.

Similarly, reachability cannot be used to exclude an access if the
access is not known to be executed by the same thread as the given
instruction.

The OpenMP-opt test was added for the latter problem.
2022-12-13 22:58:33 -08:00
Johannes Doerfert
fc21f2d7ba [Attributor] Introduce AA[Intra/Inter]Reachability
We had two AAs for reachability but it was very cumbersome to extend
them. We also had some fallback to use LLVM-core mechanisms and cache
the result. The new design shares the query code and interface nicely
between AAIntraFnReachability and AAInterFnReachability.

As part of the rewrite we also added the ExclusionSet to the queries.
2022-12-13 19:38:15 -08:00
Johannes Doerfert
8be3133ecd [OpenMP][FIX] Remove unsound reasoning about written to values
Even if a value is for sure written we need to visit the call sites as
they might end up inside the function that reads and writes the value.
In a follow up we can introduce correct reasoning to avoid the backwards
traversal in this case and instead check if any call site between the
write and the read might reach a potential write we want to exclude.
2022-12-13 18:44:20 -08:00
Kazu Hirata
59686bb3fc [IPO] Fix a warning
This patch fixes:

  llvm/lib/Transforms/IPO/AttributorAttributes.cpp:1028:21: error:
  unused function 'operator<<' [-Werror,-Wunused-function]
2022-12-13 17:25:01 -08:00
Sameer Sahasrabuddhe
6a2305484e [AAPointerInfo] track multiple constant offsets for each use
An expression of the form `gep(base, select(pred, const1, const2))` can result
in a set of offsets instead of just one. PointerInfo can now track these sets
instead of conservatively modeling them as Unknown. In general, AAPointerInfo
now uses AAPotentialConstantValues to examine the operands of the GEP.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D138646
2022-12-13 22:27:25 +05:30
Johannes Doerfert
12e14bc42b [Attributor] Make non-side-effect inline asm be "no-call"
If we have inline asm with side effects we assume any function might be
called. For non-side-effect asm we now assume no function is called.
2022-12-12 20:55:35 -08:00
Sameer Sahasrabuddhe
2fdeb27790 Revert "[AAPointerInfo] track multiple constant offsets for each use"
Assertion fired in openmp-offload-amdgpu-runtime:
https://lab.llvm.org/buildbot/#/builders/193/builds/23177

This reverts commit c2a0baad1fbb21fe111fef83ec93c2d7923b9b0c.
2022-12-12 15:39:18 +05:30
Sameer Sahasrabuddhe
c2a0baad1f [AAPointerInfo] track multiple constant offsets for each use
An expression of the form `gep(base, select(pred, const1, const2))` can result
in a set of offsets instead of just one. PointerInfo can now track these sets
instead of conservatively modeling them as Unknown. In general, AAPointerInfo
now uses AAPotentialConstantValues to examine the operands of the GEP.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D138646
2022-12-12 13:36:45 +05:30
Kazu Hirata
e5ef6aced2 [Analysis] Use std::optional in MemoryBuiltins.cpp (NFC)
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-11 01:32:26 -08:00
Johannes Doerfert
142e38007d [OpenMP][FIX] Do not use reachability reasoning in parallel contexts
Similar to dominance reasoning, we cannot use CFG reachability if the
instructions might be executed by different threads. A follow up will
improve our sensitivity for situations when it is OK to use graph
reasoning.
2022-12-09 14:27:55 -08:00
Sameer Sahasrabuddhe
0dc0a44132 [AAPointerInfo] rearrange code in preparation for further changes
AAPointerInfoFloating::updateImpl
  Move the member function definition out of its class before modifying it.

translateAndAddState
  Split the function definition into two along the FromCallee boolean argument.

handleAccess
  This should only be called when Size is not known. In other cases, replaced
  with a direct call to addAccess.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D138645
2022-12-09 10:58:28 +05:30
Haojian Wu
2d7e186021 [Attributor] Fix a break in relase build caused by
61318fa5c764d6af60baeb1c00d793f1bd95f5ea.
2022-12-08 21:07:58 +01:00
Haojian Wu
61318fa5c7 Fix an unused-variable warning in release build, NFC 2022-12-08 09:56:32 +01:00
Johannes Doerfert
1eab2d699e [Attributor] Keep complex select and PHI instructions in AAPotentialValues
If a select or PHI instruction cannot be simplified to a single value it
is often beneficial to keep the value intact rather than looking at all
the operands. The reason is that various users require a single value
and consequently can deal with the select or PHI but not multiple
operands. Recursive calls of `Attributor::getAssumedSimplifiedValues`
will be required to take such select and PHI instructions apart.
2022-12-07 22:57:52 -08:00
Sameer Sahasrabuddhe
5ac633d2de [NFC][AAPointerInfo] rename OffsetAndSize to RangeTy
This is in preparation for future changes that introduce an actual list of
ranges per Access, to be called a RangeList.

Differential Revision: https://reviews.llvm.org/D138644
2022-12-08 12:14:28 +05:30
Johannes Doerfert
2a607d912e [Attributor] Delete stores only required for assumes (with the assumes)
We keep loads if they feed into assumes but even if we cannot predict
their value we should delete them if the associated stores are deleted
as well. This is not perfect but prioritizes deleting stores now.
2022-12-07 22:26:11 -08:00
Johannes Doerfert
db86f45478 [Attributor] Introduce assumption accesses in AAPointerInfo
Assumptions can help us reason about memory content. This patch teaches
AAPointerInfo to reason about memory assumptions of the following form:

```
%x = load %ptr
... code not writing memory, may include branches ...
%c = %x == %val
... code not writing memory, may include branches ...
llvm.assume(%c)
```

Assumption accesses are recognized from the involved load (%x above).

Assumption accesses are treated special and neither as ordinary read or
write. We use read encoding with an extra flag. Reads are not impacting
other reads or writes. Writes could do that. We don't want assumptions
to impact other writes as they themselves only confirm a value, not
write it. So the "other" write might be required as the assumption only
confirms the effect of that write.
2022-12-07 15:39:07 -08:00
Kazu Hirata
1f421b6d7e [llvm] Use std::nullopt instead of None in comments (NFC)
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-06 22:45:17 -08:00
Fangrui Song
75801e3b45 Transforms/IPO: llvm::Optional => std::optional 2022-12-05 07:07:19 +00:00
Kazu Hirata
595f1a6aaf [llvm] Use std::nullopt instead of None in comments (NFC)
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-04 19:47:13 -08:00
Kazu Hirata
3c09ed006a [llvm] Use std::nullopt instead of None in comments (NFC)
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-04 17:12:44 -08:00
Kazu Hirata
343de6856e [Transforms] Use std::nullopt instead of None (NFC)
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-02 21:11:37 -08:00
Krzysztof Parzyszek
467432899b MemoryLocation: convert Optional to std::optional 2022-12-01 15:36:20 -08:00
Kazu Hirata
629819cb66 [IPO] Use std::optional in AttributorAttributes.cpp (NFC)
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-11-25 23:23:40 -08:00
Sameer Sahasrabuddhe
376d0469b9 [AAPointerInfo] refactor how offsets and Access objects are tracked
This restores commit b756096b0cbef0918394851644649b3c28a886e2, which was
originally reverted in 00b09a7b18abb253d36b3d3e1c546007288f6e89.

AAPointerInfo now maintains a list of all Access objects that it owns, along
with the following maps:

- OffsetBins: OffsetAndSize -> { Access }
- InstTupleMap: RemoteI x LocalI -> Access

A RemoteI is any instruction that accesses memory. RemoteI is different from
LocalI if and only if LocalI is a call; then RemoteI is some instruction in the
callgraph starting from LocalI.

Motivation: When AAPointerInfo recomputes the offset for an instruction, it sets
the value to Unknown if the new offset is not the same as the old offset. The
instruction must now be moved from its current bin to the bin corresponding to
the new offset. This happens for example, when:

- A PHINode has operands that result in different offsets.
- The same remote inst is reachable from the same local inst via different paths
  in the callgraph:

```
               A (local inst)
               |
               B
              / \
             C1  C2
              \ /
               D (remote inst)

```
This fixes a bug where a store is incorrectly eliminated in a lit test.

Reviewed By: jdoerfert, ye-luo

Differential Revision: https://reviews.llvm.org/D136526
2022-11-15 18:52:11 +05:30
Nikita Popov
304f1d59ca [IR] Switch everything to use memory attribute
This switches everything to use the memory attribute proposed in
https://discourse.llvm.org/t/rfc-unify-memory-effect-attributes/65579.
The old argmemonly, inaccessiblememonly and inaccessiblemem_or_argmemonly
attributes are dropped. The readnone, readonly and writeonly attributes
are restricted to parameters only.

The old attributes are auto-upgraded both in bitcode and IR.
The bitcode upgrade is a policy requirement that has to be retained
indefinitely. The IR upgrade is mainly there so it's not necessary
to update all tests using memory attributes in this patch, which
is already large enough. We could drop that part after migrating
tests, or retain it longer term, to make it easier to import IR
from older LLVM versions.

High-level Function/CallBase APIs like doesNotAccessMemory() or
setDoesNotAccessMemory() are mapped transparently to the memory
attribute. Code that directly manipulates attributes (e.g. via
AttributeList) on the other hand needs to switch to working with
the memory attribute instead.

Differential Revision: https://reviews.llvm.org/D135780
2022-11-04 10:21:38 +01:00
Ye Luo
00b09a7b18 Revert "[AAPointerInfo] refactor how offsets and Access objects are tracked"
This reverts commit b756096b0cbef0918394851644649b3c28a886e2.
See regression https://github.com/llvm/llvm-project/issues/58774
2022-11-03 00:01:51 -05:00