Commit Graph

11972 Commits

Author SHA1 Message Date
Fangrui Song a996cc217c Remove unused #include "llvm/ADT/Optional.h" 2022-12-05 06:31:11 +00:00
Fangrui Song 89fae41ef1 [IR] llvm::Optional => std::optional
Many llvm/IR/* files have been migrated by other contributors.
This migrates most remaining files.
2022-12-05 04:13:11 +00:00
Kazu Hirata 9f252e5567 [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:31:17 -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
Benjamin Kramer 856f7937c7 Compress a few pairs using PointerIntPairs
Use the uniform structured bindings interface where possible. NFCI.
2022-12-04 16:55:16 +01:00
Krzysztof Parzyszek ab672e9173 FPEnv: convert Optional to std::optional 2022-12-03 13:55:56 -06:00
David Green 16a72a0f87 [AArch64] Enable the select optimize pass for AArch64
This enabled the select optimize patch for ARM Out of order AArch64
cores. It is trying to solve a problem that is difficult for the
compiler to fix. The criteria for when a csel is better or worse than a
branch depends heavily on whether the branch is well predicted and the
amount of ILP in the loop (as well as other criteria like the core in
question and the relative performance of the branch predictor).  The
pass seems to do a decent job though, with the inner loop heuristics
being well implemented and doing a better job than I had expected in
general, even without PGO information.

I've been doing quite a bit of benchmarking. The headline numbers are
these for SPEC2017 on a Neoverse N1:
  500.perlbench_r   -0.12%
  502.gcc_r         0.02%
  505.mcf_r         6.02%
  520.omnetpp_r     0.32%
  523.xalancbmk_r   0.20%
  525.x264_r        0.02%
  531.deepsjeng_r   0.00%
  541.leela_r       -0.09%
  548.exchange2_r   0.00%
  557.xz_r          -0.20%

Running benchmarks with a combination of the llvm-test-suite plus
several versions of SPEC gave between a 0.2% and 0.4% geomean
improvement depending on the core/run. The instruction count went down
by 0.1% too, which is a good sign, but the results can be a little
noisy.  Some issues from other benchmarks I had ran were improved in
rGca78b5601466f8515f5f958ef8e63d787d9d812e. In summary well predicted
branches will see in improvement, badly predicted branches may get
worse, and on average performance seems to be a little better overall.

This patch enables the pass for AArch64 under -O3 for cores that will
benefit for it. i.e. not in-order cores that do not fit into the "Assume
infinite resources that allow to fully exploit the available
instruction-level parallelism" cost model. It uses a subtarget feature
for specifying when the pass will be enabled, which I have enabled under
cpu=generic as the performance increases for out of order cores seems
larger than any decreases for inorder, which were minor.

Differential Revision: https://reviews.llvm.org/D138990
2022-12-03 16:08:58 +00:00
Kazu Hirata 19aff0f37d [Analysis] 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 19:43:04 -08:00
Jan Svoboda abf0c6c0c0 Use CTAD on llvm::SaveAndRestore
Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D139229
2022-12-02 15:36:12 -08:00
Kazu Hirata 2d6ec146dd [ModuleInliner] Add MLPriority
This patch adds MLPriority as the first step toward the ML-based
function inlining with the module inliner.

For now, MLPriority is completely identical to CostPriority.

Once this patch lands, I'm planning to:

- integrate NoInferenceModelRunner,

- memoize the priority computation so that the priority remains the
  same for given values of metrics even with the noise injected during
  training, and

- port/take more features into account.

Differential Revision: https://reviews.llvm.org/D139140
2022-12-02 14:25:13 -08:00
Kazu Hirata ba7cf9d18a [ModuleInliner] Initialize variables (NFC)
This patch initializes all class variables in InlineOrder.cpp for
safety just in case we miss them in constructors.  Currently, all
these variables are properly initialized in their respective
constructors.

Differential Revision: https://reviews.llvm.org/D139225
2022-12-02 13:31:13 -08:00
Krzysztof Parzyszek 86fe4dfdb6 TargetTransformInfo: convert Optional to std::optional
Recommit: added missing "#include <cstdint>".
2022-12-02 11:42:15 -08:00
Krzysztof Parzyszek 4e12d1836a Revert "TargetTransformInfo: convert Optional to std::optional"
This reverts commit b83711248c.

Some buildbots are failing.
2022-12-02 11:34:04 -08:00
Krzysztof Parzyszek b83711248c TargetTransformInfo: convert Optional to std::optional 2022-12-02 11:27:12 -08:00
Krzysztof Parzyszek 26424c96c0 Attributes: convert Optional to std::optional 2022-12-02 08:15:45 -06:00
tentzen db6a979ae8 Revert "[Windows SEH]: HARDWARE EXCEPTION HANDLING (MSVC -EHa) - Part 2"
This reverts commit 1a949c871a.
2022-12-02 02:44:18 -08:00
tentzen 1a949c871a [Windows SEH]: HARDWARE EXCEPTION HANDLING (MSVC -EHa) - Part 2
This patch is the Part-2 (BE LLVM) implementation of HW Exception handling.
Part-1 (FE Clang) was committed in 797ad70152.

This new feature adds the support of Hardware Exception for Microsoft Windows
SEH (Structured Exception Handling).

Compiler options:
  For clang-cl.exe, the option is -EHa, the same as MSVC.
  For clang.exe, the extra option is -fasync-exceptions,
  plus -triple x86_64-windows -fexceptions and -fcxx-exceptions as usual.

NOTE:: Without the -EHa or -fasync-exceptions, this patch is a NO-DIFF change.

The rules for C code:
For C-code, one way (MSVC approach) to achieve SEH -EHa semantic is to follow three rules:
  First, no exception can move in or out of _try region., i.e., no "potential faulty
    instruction can be moved across _try boundary.
  Second, the order of exceptions for instructions 'directly' under a _try must be preserved
    (not applied to those in callees).
  Finally, global states (local/global/heap variables) that can be read outside of _try region
    must be updated in memory (not just in register) before the subsequent exception occurs.

The impact to C++ code:
  Although SEH is a feature for C code, -EHa does have a profound effect on C++
  side. When a C++ function (in the same compilation unit with option -EHa ) is
  called by a SEH C function, a hardware exception occurs in C++ code can also
  be handled properly by an upstream SEH _try-handler or a C++ catch(...).
  As such, when that happens in the middle of an object's life scope, the dtor
  must be invoked the same way as C++ Synchronous Exception during unwinding process.

Design:
A natural way to achieve the rules above in LLVM today is to allow an EH edge
added on memory/computation instruction (previous iload/istore idea) so that
exception path is modeled in Flow graph preciously. However, tracking every
single memory instruction and potential faulty instruction can create many
Invokes, complicate flow graph and possibly result in negative performance
impact for downstream optimization and code generation. Making all
optimizations be aware of the new semantic is also substantial.

This design does not intend to model exception path at instruction level.
Instead, the proposed design tracks and reports EH state at BLOCK-level to
reduce the complexity of flow graph and minimize the performance-impact on CPP
code under -EHa option.
One key element of this design is the ability to compute State number at
block-level. Our algorithm is based on the following rationales:

A _try scope is always a SEME (Single Entry Multiple Exits) region as jumping
into a _try is not allowed. The single entry must start with a seh_try_begin()
invoke with a correct State number that is the initial state of the SEME.
Through control-flow, state number is propagated into all blocks. Side exits
marked by seh_try_end() will unwind to parent state based on existing SEHUnwindMap[].
Note side exits can ONLY jump into parent scopes (lower state number).
Thus, when a block succeeds various states from its predecessors, the lowest
State triumphs others.  If some exits flow to unreachable, propagation on those
paths terminate, not affecting remaining blocks.
For CPP code, object lifetime region is usually a SEME as SEH _try.
However there is one rare exception: jumping into a lifetime that has Dtor but
has no Ctor is warned, but allowed:

Warning: jump bypasses variable with a non-trivial destructor

In that case, the region is actually a MEME (multiple entry multiple exits).
Our solution is to inject a eha_scope_begin() invoke in the side entry block to
ensure a correct State.
Implementation:
Part-1: Clang implementation (already in):
Please see commit 797ad70152).

Part-2 : LLVM implementation described below.

For both C++ & C-code, the state of each block is computed at the same place in
BE (WinEHPreparing pass) where all other EH tables/maps are calculated.
In addition to _scope_begin & _scope_end, the computation of block state also
rely on the existing State tracking code (UnwindMap and InvokeStateMap).

For both C++ & C-code, the state of each block with potential trap instruction
is marked and reported in DAG Instruction Selection pass, the same place where
the state for -EHsc (synchronous exceptions) is done.
If the first instruction in a reported block scope can trap, a Nop is injected
before this instruction. This nop is needed to accommodate LLVM Windows EH
implementation, in which the address in IPToState table is offset by +1.
(note the purpose of that is to ensure the return address of a call is in the
same scope as the call address.

The handler for catch(...) for -EHa must handle HW exception. So it is
'adjective' flag is reset (it cannot be IsStdDotDot (0x40) that only catches
C++ exceptions).
Suppress push/popTerminate() scope (from noexcept/noTHrow) so that HW
exceptions can be passed through.

Original llvm-dev [RFC] discussions can be found in these two threads below:
https://lists.llvm.org/pipermail/llvm-dev/2020-March/140541.html
https://lists.llvm.org/pipermail/llvm-dev/2020-April/141338.html

Differential Revision: https://reviews.llvm.org/D102817/new/
2022-12-01 23:44:25 -08:00
Mircea Trofin f291667d61 [mlgo][nfc] Virtualize Logger implementation
This is in preparation for dropping the dependency on protobuf. This
first step allows us to subsequently introduce the non-protobuf
implementation behind a flag. After that we can update the training side
to ingest the new format, after which we can drop the protobuf
implementation and de-virtualize everything.

Differential Revision: https://reviews.llvm.org/D139062
2022-12-01 16:03:08 -08:00
Krzysztof Parzyszek 467432899b MemoryLocation: convert Optional to std::optional 2022-12-01 15:36:20 -08:00
Mircea Trofin 1ee3bb17c3 [mlgo][nfc] Make `LoggedFeatureSpec` an implementation detail
It's an artifact very specific to using TFAgents during training, so it
belongs with ModelUnderTrainingRunner.

Differential Revision: https://reviews.llvm.org/D139031
2022-11-30 15:57:58 -08:00
Sanjay Patel 47f5da47f5 [InstSimplify] (X && Y) ? X : Y --> Y
Similar to the recent fold that was added for 'or' in D138815:
https://alive2.llvm.org/ce/z/PBapTJ
2022-11-30 15:44:48 -05:00
David Stuttard 62498962e4 ConstantFolding: Guard use of getFunction
Add additional guards for a use of getFunction on an Instruction
In some cases constanfFoldCanonicalize can be called with a cloned instruction
that doesn't have a parent (or associated function), causing a seg fault.

Differential Revision: https://reviews.llvm.org/D138642
2022-11-30 14:09:40 +00:00
chenglin.bi f297332749 [InstSimplify] Fold (X || Y) ? X : Y --> X
(X || Y) ? X : Y --> X
https://alive2.llvm.org/ce/z/oRQJee

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D138815
2022-11-30 10:14:17 +08:00
Vasileios Porpodas 8a1ccb8ae0 [NFC] Removed call to getInstList() from range loops on BBs.
Differential Revision: https://reviews.llvm.org/D138605
2022-11-29 17:33:10 -08:00
chenglin.bi 1fd4d91fa6 [InstSimplify] Fold !(X || Y) && X --> false
!(X || Y) && X --> false
https://alive2.llvm.org/ce/z/693Jgv

Fix: [56654](https://github.com/llvm/llvm-project/issues/56654)
Fix: [56780](https://github.com/llvm/llvm-project/issues/56780)

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D138853
2022-11-29 22:45:24 +08:00
chenglin.bi 0752fb57e4 [InstSimplify] Fold (X || Y) ? false : X --> false
(X || Y) ? false : X --> false
https://alive2.llvm.org/ce/z/y93yUm

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D138700
2022-11-29 22:08:50 +08:00
Kazu Hirata 55378ae87c [Analysis] Remove unused fields in MemorySSA.cpp (NFC)
The last uses of AR were removed on July 28, 2022 in commit
f96ea53e89.

Differential Revision: https://reviews.llvm.org/D138730
2022-11-28 15:39:32 -08:00
Slava Zakharin 5bd8175dd7 [AA] A global cannot escape through nocapture/nocallback call.
When an internal global is passed to a 'nocallback' call as
a 'nocapture' pointer, it cannot escape through this call and
be indirectly referenced in this module.
So it must not alias with any pointer in the module.

This may provide some remedy for Fortran module-private array descriptors
that are usually passed by address to some runtime functions
(e.g. to allocation/deallocation functions). In general, a good aliasing
information derived from Fortran language rules would solve the same issue,
but I think this change may be beneficial as-is (given that nocapture,
nocallback attributes are properly set).

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D138336
2022-11-28 12:50:31 -08:00
Max Kazantsev 0b74cb4231 [SCEV] Introduce field for storing SymbolicMaxNotTaken. NFCI
ritht is initialized with either exact (if available) or
with constant max exit count. In the future, this can be improved.

Hypothetically this is not an NFC (it is possible that exact is not
known and max is known for a particular exit), but for how we use
it now it seems be an NFC (or at least I could not find an example
where it differs). constant max exit count. In the future, this can
be improved.

Differential Revision: https://reviews.llvm.org/D138699
Reviewed By: lebedev.ri
2022-11-28 17:07:33 +07:00
chenglin.bi b400dde473 [InstSimplify] Use dominate condtion to simplify instructions
Fix #56795

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D138542
2022-11-26 21:41:41 +08:00
Kazu Hirata 5ea708375f [Analysis] Use std::optional in ValueTracking.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 11:30:34 -08:00
Kazu Hirata ee8959d098 [Analysis] Use std::optional in ScalarEvolution.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 11:25:45 -08:00
Kazu Hirata 2a324cb261 [Analysis] Use std::optional in MemoryLocation.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 10:32:21 -08:00
Kazu Hirata 68db72714f [Analysis] Use std::optional in LoopAnalysisManager.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 10:28:54 -08:00
Kazu Hirata eb0b5a36be [Analysis] Use std::optional in LazyValueInfo.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 10:25:36 -08:00
Kazu Hirata 4a124fd9d1 [Analysis] Use std::optional in InlineCost.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 10:22:33 -08:00
Kazu Hirata d49613f4d8 [Analysis] Use std::optional in CGSCCPassManager.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 10:18:53 -08:00
Kazu Hirata e51b18f006 [Analysis] Use std::optional in CFLAndersAliasAnalysis.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 10:16:32 -08:00
Kazu Hirata 1d74b24743 [Analysis] Use std::optional in BasicAliasAnalysis.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 10:11:42 -08:00
Evgeniy Brevnov 721f975d35 Use PassGate from LLVMContext if any otherwise global one
Differential Revision: https://reviews.llvm.org/D137149
2022-11-25 15:13:04 +07:00
Max Kazantsev eb95ab5745 Revert "[Test] Add couple more tests where we can compute symbolic max exit count"
This reverts commit 7e3373c9e1.

Some changes that were not supposed to be commited came with it.
2022-11-25 13:37:24 +07:00
Max Kazantsev 7e3373c9e1 [Test] Add couple more tests where we can compute symbolic max exit count 2022-11-25 13:35:16 +07:00
Max Kazantsev 98307381d4 [SCEV][NFC] Rename constructor parameter to match its field name 2022-11-25 12:59:05 +07:00
Max Kazantsev 04b9a70fec [SCEV][NFC] Get rid of redundant constructor, replace with default parameter 2022-11-25 12:07:41 +07:00
Max Kazantsev 4496d553bd [SCEV] Fix misplaced \n in printout of max symbolic exit counts 2022-11-25 11:41:36 +07:00
Fangrui Song fa71c16455 [Inliner] Move cl::opt inside llvm:: 2022-11-24 20:31:13 -08:00
Florian Hahn ae852750b3
[MemoryLocation] Support memcpy_chk in getForArgument.
Similar to 9f9e8ba114, add support for memcyp_chk to
MemoryLocation::getForArgument.

The size argument for memcpy_chk is an upper bound for the size of the
pointer argument. memcpy_chk may read/write less than the specified length,
if it exceeds the specified max size and aborts.

Reviewed By: xbolva00, jdoerfert

Differential Revision: https://reviews.llvm.org/D138613
2022-11-24 19:17:48 +00:00
Max Kazantsev e5fa7eb120 [SCEV] Add printout of symbolic max backedge-taken and block exit count
We do compute it and use in optimizations, but never print it out. We need
to do it in order to be able to track improvements in its computation.
2022-11-24 19:29:58 +07:00
Max Kazantsev fc986e38d2 [SCEV][NFC] Call getConstantMaxBackedgeTakenCount once in printout 2022-11-24 18:48:30 +07:00
Max Kazantsev 211d941188 [SCEV] Rename max backedge-taken count -> constant max backedge taken-count in printout
This is a preparatory step for introducing symbolic max backedge-taken count.
2022-11-24 18:43:42 +07:00