Commit Graph

1360 Commits

Author SHA1 Message Date
Benjamin Kramer 02c75e8465 [ADT] Enable structured bindings for iterating StringMap
const references only for now, we can add overloads to have a mutable or
movable `second` if the need arises.
2022-12-04 18:33:16 +01:00
Benjamin Kramer 8073a6bb0c [ADT] Allow structured bindings on PointerIntPair
Apart from simplifying code, this has the advantage of making the
interface between std::pair and PointerIntPair more uniform.
2022-12-04 16:16:04 +01:00
Kazu Hirata b6a01caa64 [llvm/unittests] 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 22:10:37 -08:00
Kazu Hirata f64d4a26ce [llvm] Add support for hashing std::optional
The credit for the hashing code, taken from D138934, goes to Ramkumar
Ramachandra.  The test comes from OptionalTest.cpp and updated for
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

Differential Revision: https://reviews.llvm.org/D139240
2022-12-02 18:53:00 -08:00
Jakub Kuderski 21d434d997 [ADT] Add `zip_equal` for iteratees of equal lengths
Add a new version of `zip` that assumes that all iteratees have equal
lengths. The difference compared to `zip_first` is that `zip_equal`
checks this assumption in builds with assertions enabled.

This will allow us to clearly express the intent when working with
equally-sized ranges without having to write this assertion manually.

This is similar to Python's `zip(..., equal=True)` [1] or
`more_itertools.zip_equal` [2].

I saw this first suggested by @benvanik.

[1] https://peps.python.org/pep-0618/
[2] https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.zip_equal

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D138865
2022-11-29 19:56:25 -05:00
Jakub Kuderski aa7a3d4d3d [ADT] Clarify `zip` behavior with iteratees of different lengths
Update the documentation comment and add a new test case.

Add an assertion in `zip_first` checking the iteratee length precondition.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D138858
2022-11-29 19:50:16 -05:00
Kazu Hirata 589725f6e8 [llvm] Use std::size (NFC)
std::size, introduced in C++17, allows us to directly obtain the
number of elements of an array.
2022-11-26 13:47:32 -08:00
Reed 88eb3c62f2 Add FP8 E4M3 support to APFloat.
NVIDIA, ARM, and Intel recently introduced two new FP8 formats, as described in the paper: https://arxiv.org/abs/2209.05433. The first of the two FP8 dtypes, E5M2, was added in https://reviews.llvm.org/D133823. This change adds the second of the two: E4M3.

There is an RFC for adding the FP8 dtypes here: https://discourse.llvm.org/t/rfc-add-apfloat-and-mlir-type-support-for-fp8-e5m2/65279. I spoke with the RFC's author, Stella, and she gave me the go ahead to implement the E4M3 type. The name of the E4M3 type in APFloat is Float8E4M3FN, as discussed in the RFC. The "FN" means only Finite and NaN values are supported.

Unlike E5M2, E4M3 has different behavior from IEEE types in regards to Inf and NaN values. There are no Inf values, and NaN is represented when the exponent and mantissa bits are all 1s. To represent these differences in APFloat, I added an enum field, fltNonfiniteBehavior, to the fltSemantics struct. The possible enum values are IEEE754 and NanOnly. Only Float8E4M3FN has the NanOnly behavior.

After this change is submitted, I plan on adding the Float8E4M3FN type to MLIR, in the same way as E5M2 was added in https://reviews.llvm.org/D133823.

Reviewed By: bkramer

Differential Revision: https://reviews.llvm.org/D137760
2022-11-15 20:26:42 +01:00
WANG Xuerui d480271729 [ADT][Triple] Add environment kinds for LoongArch GNU multiarch tuples
The canonical multiarch tuples for LoongArch are defined in [the
LoongArch toolchain conventions][1] document. As the musl port is still
WIP, only the GNU triples are added for now.

The spec mentions `loongarch64-linux-gnuf64`, which is functionally the
same as the existing `loongarch64-linux-gnu` triple, only with the
floating-point ABI part explicitly spelled out. Both forms are
supported, but normalization of one into another is not implemented in
this patch, to give the ecosystem some time to experiment and discuss.

[1]: https://loongson.github.io/LoongArch-Documentation/LoongArch-toolchain-conventions-EN.html

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D135751
2022-11-10 15:05:04 +08:00
Tomas Matheson 103bbddde6 [ARM] Move Triple::getARMCPUForArch into ARMTargetParser
This is very backend specific so either belongs in Toolchains/ARM or in
ARMTargetParser. Since it is used in lldb, ARMTargetParser made more sense.

This is part of an effort to move information about ARM/AArch64 architecture
versions, extensions and CPUs into their respective TargetParsers.

Differential Revision: https://reviews.llvm.org/D137564
2022-11-09 11:52:35 +00:00
Nathan James 6aa050a690 Reland "[llvm][NFC] Use c++17 style variable type traits"
This reverts commit 632a389f96.

This relands commit
1834a310d0.

Differential Revision: https://reviews.llvm.org/D137493
2022-11-08 14:15:15 +00:00
Nathan James 632a389f96 Revert "[llvm][NFC] Use c++17 style variable type traits"
This reverts commit 1834a310d0.
2022-11-08 13:11:41 +00:00
Nathan James 1834a310d0
[llvm][NFC] Use c++17 style variable type traits
This was done as a test for D137302 and it makes sense to push these changes

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D137493
2022-11-08 12:22:52 +00:00
Fangrui Song 6c927f2a86 Canonicalize PowerPC detection macros to __powerpc__ 2022-11-06 17:29:45 -08:00
Weverything cbdb81e60b Fix DenseMap with APInt keys
The empty key value for APInt was colliding with a valid zero-width
APInt.  Change the internal value of empty key and tombstone values
for APInt to avoid this collision.

Fixes: https://github.com/llvm/llvm-project/issues/58013

Differential Revision: https://reviews.llvm.org/D135741
2022-11-01 16:23:32 -07:00
Simon Pilgrim 66b830889d Add explicit cast to fix MSVC error: "'return': cannot convert from 'int (__cdecl *)(int)' to '`anonymous-namespace'::Plus1FunctionRef::FuncT'" 2022-10-31 12:01:06 +00:00
James Player bf738d2e77 [ADT] Make mapped_iterator copy assignable
As mentioned in https://discourse.llvm.org/t/rfc-extend-ranges-infrastructure-to-better-match-c-20/65377

Lambda objects are not copy assignable, and therefore neither are
iterator types which hold a lambda.  STL code require iterators be
copy assignable.  Users may not use mapped_iterator with a std::deque
for example: https://godbolt.org/z/4Px7odEEd

This blog post [1] explains the problem and solution.  We define a
wrapper class to store callable objects with two specialization.

1. Specialization for non-function types
    - Use a std::optional as storage for non-function callable.
    - Define operator=() implementation(s) which use
      std::optional::emplace() instead of the assignment operator.
2. Specialization for function types
    - Store as a pointer (even if template argument is a function reference).
    - Default construct pointer to nullptr.

This Callable wrapper class is now default constructible (with invalid
state) and copy/move assignable.

With these new properties available on the callable object,
mapped_iterator can define a default constructor as well.

[1] https://www.fluentcpp.com/2019/04/16/an-alternative-design-to-iterators-and-ranges-using-stdoptional/

Reviewed By: kazu

Differential Revision: https://reviews.llvm.org/D134675
2022-10-30 14:37:08 -07:00
OCHyams dbbe82c6fd [IntervalMap] Add move and copy ctors and assignment operators
And update the unittest.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D136242
2022-10-27 09:45:24 +01:00
David Blaikie 637a594f5a [ADT] Add deduction guide for llvm::Optional
Added to address some uses of implicit CTAD added in clang recently.
2022-10-26 21:45:57 +00:00
Jannik Silvanus 6608908b1b [ADT] Extend EnumeratedArray
EnumeratedArray is essentially a wrapper around a fixed-size
array that uses enum values instead of integers as indices.

 * Add iterator support (begin/end/rbegin/rend), which enables
   the use of iterator/range based algorithms on EnumeratedArrays.
 * Add common container typedefs (value_type etc.), allowing
   drop-in replacements of other containers in cases relying on these.
 * Add a constructor that takes an std::initializer_list<T>.
 * Make the size() function const.
 * Add empty().

Iterator support slightly lowers the protection non-type-safe accesses,
because iterator arithmetic is not enum-based, and one can now use
*(begin() + IntIndex). However, it is and was also always possible to
just cast arbitrary indices to the enum type.

Differential Revision: https://reviews.llvm.org/D135594
2022-10-18 17:08:38 +02:00
Kazu Hirata 1b97645e56 [ADT] Introduce StringRef::{starts,ends}_width{,_insensitive}
This patch introduces:

  StringRef::starts_with
  StringRef::starts_with_insensitive
  StringRef::ends_with
  StringRef::ends_with_insensitive

to be more compatible with std::string and std::string_view.

I'm planning to deprecate the existing functions in favor of the new
ones.

Differential Revision: https://reviews.llvm.org/D136030
2022-10-15 15:06:37 -07:00
Tyker adf2972610 Revert "Remove sign comparaison warning in APFixedPointTest.cpp"
has already been fixed

This reverts commit 1a60a35769.
2022-10-14 13:40:12 -07:00
Tyker 1a60a35769 Remove sign comparaison warning in APFixedPointTest.cpp 2022-10-14 13:39:18 -07:00
Nico Weber 34bbe6548d Fix a -Wsign-compare warning 2022-10-14 15:19:30 -04:00
Tyker 2c7b7eca85 [ADT] Fix Incorrect rounding for APFixedPoint::getIntPart 2022-10-14 11:01:20 -07:00
Tyker 1654b22ac0 [ADT] Add support for more formats in APFixedPoint
Prior to this patch FixedPointSemantics and APFixedPoint only support semantics where
the Scale larger or equal to zero and the Width is larger or equal to the Scale.
This patch removes both those requirements while staying API compatible.
2022-10-06 17:55:31 +02:00
Stella Laurenzo e28b15b572 Add APFloat and MLIR type support for fp8 (e5m2).
(Re-Apply with fixes to clang MicrosoftMangle.cpp)

This is a first step towards high level representation for fp8 types
that have been built in to hardware with near term roadmaps. Like the
BFLOAT16 type, the family of fp8 types are inspired by IEEE-754 binary
floating point formats but, due to the size limits, have been tweaked in
various ways in order to maximally use the range/precision in various
scenarios. The list of variants is small/finite and bounded by real
hardware.

This patch introduces the E5M2 FP8 format as proposed by Nvidia, ARM,
and Intel in the paper: https://arxiv.org/pdf/2209.05433.pdf

As the more conformant of the two implemented datatypes, we are plumbing
it through LLVM's APFloat type and MLIR's type system first as a
template. It will be followed by the range optimized E4M3 FP8 format
described in the paper. Since that format deviates further from the
IEEE-754 norms, it may require more debate and implementation
complexity.

Given that we see two parts of the FP8 implementation space represented
by these cases, we are recommending naming of:

* `F8M<N>` : For FP8 types that can be conceived of as following the
  same rules as FP16 but with a smaller number of mantissa/exponent
  bits. Including the number of mantissa bits in the type name is enough
  to fully specify the type. This naming scheme is used to represent
  the E5M2 type described in the paper.
* `F8M<N>F` : For FP8 types such as E4M3 which only support finite
  values.

The first of these (this patch) seems fairly non-controversial. The
second is previewed here to illustrate options for extending to the
other known variant (but can be discussed in detail in the patch
which implements it).

Many conversations about these types focus on the Machine-Learning
ecosystem where they are used to represent mixed-datatype computations
at a high level. At that level (which is why we also expose them in
MLIR), it is important to retain the actual type definition so that when
lowering to actual kernels or target specific code, the correct
promotions, casts and rescalings can be done as needed. We expect that
most LLVM backends will only experience these types as opaque `I8`
values that are applicable to some instructions.

MLIR does not make it particularly easy to add new floating point types
(i.e. the FloatType hierarchy is not open). Given the need to fully
model FloatTypes and make them interop with tooling, such types will
always be "heavy-weight" and it is not expected that a highly open type
system will be particularly helpful. There are also a bounded number of
floating point types in use for current and upcoming hardware, and we
can just implement them like this (perhaps looking for some cosmetic
ways to reduce the number of places that need to change). Creating a
more generic mechanism for extending floating point types seems like it
wouldn't be worth it and we should just deal with defining them one by
one on an as-needed basis when real hardware implements a new scheme.
Hopefully, with some additional production use and complete software
stacks, hardware makers will converge on a set of such types that is not
terribly divergent at the level that the compiler cares about.

(I cleaned up some old formatting and sorted some items for this case:
If we converge on landing this in some form, I will NFC commit format
only changes as a separate commit)

Differential Revision: https://reviews.llvm.org/D133823
2022-10-04 17:18:17 -07:00
Vitaly Buka e68c7a9917 Revert "Add APFloat and MLIR type support for fp8 (e5m2)."
Breaks bots https://lab.llvm.org/buildbot/#/builders/37/builds/17086

This reverts commit 2dc68b5398.
2022-10-02 21:22:44 -07:00
Stella Laurenzo 2dc68b5398 Add APFloat and MLIR type support for fp8 (e5m2).
This is a first step towards high level representation for fp8 types
that have been built in to hardware with near term roadmaps. Like the
BFLOAT16 type, the family of fp8 types are inspired by IEEE-754 binary
floating point formats but, due to the size limits, have been tweaked in
various ways in order to maximally use the range/precision in various
scenarios. The list of variants is small/finite and bounded by real
hardware.

This patch introduces the E5M2 FP8 format as proposed by Nvidia, ARM,
and Intel in the paper: https://arxiv.org/pdf/2209.05433.pdf

As the more conformant of the two implemented datatypes, we are plumbing
it through LLVM's APFloat type and MLIR's type system first as a
template. It will be followed by the range optimized E4M3 FP8 format
described in the paper. Since that format deviates further from the
IEEE-754 norms, it may require more debate and implementation
complexity.

Given that we see two parts of the FP8 implementation space represented
by these cases, we are recommending naming of:

* `F8M<N>` : For FP8 types that can be conceived of as following the
  same rules as FP16 but with a smaller number of mantissa/exponent
  bits. Including the number of mantissa bits in the type name is enough
  to fully specify the type. This naming scheme is used to represent
  the E5M2 type described in the paper.
* `F8M<N>F` : For FP8 types such as E4M3 which only support finite
  values.

The first of these (this patch) seems fairly non-controversial. The
second is previewed here to illustrate options for extending to the
other known variant (but can be discussed in detail in the patch
which implements it).

Many conversations about these types focus on the Machine-Learning
ecosystem where they are used to represent mixed-datatype computations
at a high level. At that level (which is why we also expose them in
MLIR), it is important to retain the actual type definition so that when
lowering to actual kernels or target specific code, the correct
promotions, casts and rescalings can be done as needed. We expect that
most LLVM backends will only experience these types as opaque `I8`
values that are applicable to some instructions.

MLIR does not make it particularly easy to add new floating point types
(i.e. the FloatType hierarchy is not open). Given the need to fully
model FloatTypes and make them interop with tooling, such types will
always be "heavy-weight" and it is not expected that a highly open type
system will be particularly helpful. There are also a bounded number of
floating point types in use for current and upcoming hardware, and we
can just implement them like this (perhaps looking for some cosmetic
ways to reduce the number of places that need to change). Creating a
more generic mechanism for extending floating point types seems like it
wouldn't be worth it and we should just deal with defining them one by
one on an as-needed basis when real hardware implements a new scheme.
Hopefully, with some additional production use and complete software
stacks, hardware makers will converge on a set of such types that is not
terribly divergent at the level that the compiler cares about.

(I cleaned up some old formatting and sorted some items for this case:
If we converge on landing this in some form, I will NFC commit format
only changes as a separate commit)

Differential Revision: https://reviews.llvm.org/D133823
2022-10-02 17:17:08 -07:00
chenglin.bi 0346f78a6f [ARM64EC] Add arm64ec for getArchName
Followup D125412, return the correct arch name for Arm64EC

Reviewed By: efriedma, mstorsjo

Differential Revision: https://reviews.llvm.org/D134787
2022-09-29 09:05:17 +08:00
Carlos Alberto Enciso bab129f2d3 [ADT] Add IntervalTree - light tree data structure to hold intervals.
Fix build failure in:

  https://lab.llvm.org/buildbot/#/builders/36/builds/25424
  error: comparison of integers of different signs:
  'const unsigned long' and 'const int' [-Werror,-Wsign-compare]

  Reviewed By: Orlando

  Differential Revision: https://reviews.llvm.org/D125776
2022-09-27 12:48:44 +01:00
Carlos Alberto Enciso 6584d1f930 [ADT] Add IntervalTree - light tree data structure to hold intervals.
It allows finding all intervals that overlap with any given point.
At this time, it does not support any deletion or rebalancing
operations.

The IntervalTree is designed to be set up once, and then queried
without any further additions.

Reviewed By: psamolysov, probinson

Differential Revision: https://reviews.llvm.org/D125776
2022-09-27 08:22:28 +01:00
Kazu Hirata 6b49f30fca [llvm] Deprecate llvm::empty (NFC)
This patch deprecates llvm::empty as I've migrated all known uses of
llvm::empty(x) to x.empty().

Differential Revision: https://reviews.llvm.org/D134141
2022-09-18 22:01:32 -07:00
Chris Bieneman 4b96f8996a [DX] DXContainer does not support COMDAT
The DXContainer is pretty primitive, but doesn't support COMDAT. We need
to set that in the Triple so that Clang won't try to emit COMDATs.
2022-09-13 13:59:47 -05:00
Joe Loser 5e96cea1db [llvm] Use std::size instead of llvm::array_lengthof
LLVM contains a helpful function for getting the size of a C-style
array: `llvm::array_lengthof`. This is useful prior to C++17, but not as
helpful for C++17 or later: `std::size` already has support for C-style
arrays.

Change call sites to use `std::size` instead.

Differential Revision: https://reviews.llvm.org/D133429
2022-09-08 09:01:53 -06:00
bzcheeseman 716b9f7a1a [LLVM][Support/ADT] Add assert for isPresent to dyn_cast.
This change adds an assert to dyn_cast that the value passed-in is present. In the past, this relied on the isa_impl assertion (which still works in many cases) but which we can tighten up for a better QoI.

The PointerUnion change is because it seems like (based on the call sites) the semantics of the member dyn_cast are actually dyn_cast_if_present.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D133221
2022-09-06 13:58:56 -07:00
Kadir Cetinkaya 4940f205d4
[llvm][Support] Add DenseMapInfo for std::variant
Differential Revision: https://reviews.llvm.org/D133200
2022-09-02 15:36:10 +02:00
Wei Yi Tee 72ebcf1a53 [llvm][ADT] Fix formatting for files relevant to `StringMap`.
Differential Revision: https://reviews.llvm.org/D132744
2022-08-29 06:57:29 +00:00
Wei Yi Tee af6a35597f Revert "[llvm][ADT] Fix formatting for files relevant to `StringMap`."
This reverts commit d23df9c9e8.
Revert due to missing review link.
2022-08-29 06:43:48 +00:00
Wei Yi Tee d23df9c9e8 [llvm][ADT] Fix formatting for files relevant to `StringMap`. 2022-08-29 06:40:07 +00:00
Simon Pilgrim 336a4e03a4 [ADT] Add llvm::has_single_bit helper similar to the c++20 std::has_single_bit implementation
Converted the llvm::isPowerOf2_32/64 helpers into wrappers
2022-08-23 19:51:05 +01:00
Jakub Kuderski 6fa87ec10f [ADT] Deprecate is_splat and replace all uses with all_equal
See the discussion thread for more details:
https://discourse.llvm.org/t/adt-is-splat-and-empty-ranges/64692

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D132335
2022-08-23 11:36:27 -04:00
Simon Pilgrim 134d017b88 [ADT] Add unittest coverage for ADT/bit.h header 2022-08-23 12:31:17 +01:00
Jakub Kuderski c9e52fbe4d [ADT] Add all_equal predicate
`llvm::all_equal` checks if all values in the given range are equal, i.e., there are no two elements that are not equal.
Similar to `llvm::all_of`, it returns `true` when the range is empty.

`llvm::all_equal` is intended to supersede `llvm::is_splat`, which will be deprecated and removed in future patches.
See the discussion thread for more details:
https://discourse.llvm.org/t/adt-is-splat-and-empty-ranges/64692.

Reviewed By: dblaikie, shchenz

Differential Revision: https://reviews.llvm.org/D132334
2022-08-22 23:55:23 -04:00
Joe Loser 4a51b0c05b [ADT] Remove `is_invocable` from `STLExtras.h`
As a follow-up of https://reviews.llvm.org/D132318, now that the callers have
been adjusted to use `std::is_invocable`, remove `llvm::is_invocable` and its
tests.

Differential Revision: https://reviews.llvm.org/D132321
2022-08-21 18:15:38 -06:00
Kazu Hirata e15359debf [ADT] Implement Any::has_value
This patch implements Any::has_value for consistency with std::any in
C++17.

My plan is to deprecate Any::hasValue after migrating all of its uses
to Any::has_value.  Since I am about to do so, this patch simply
replaces hasValue with has_value in the unit test instead of adding
tests for has_value.

Differential Revision: https://reviews.llvm.org/D132278
2022-08-20 07:28:04 -07:00
Zijia Zhu 8719faafdb [ADT] Make SmallSet::insert(const T &) return const_iterator
This patch makes `SmallSet::insert(const T &)` return
`std::pair<const_iterator, bool>` instead of
`std::pair<NoneType, bool>`. This will exactly match std::set's behavior
and make deduplicating items with SmallSet easier.

Reviewed By: dblaikie, lattner

Differential Revision: https://reviews.llvm.org/D131549
2022-08-15 13:53:34 +08:00
David Green b2ac153ba4 [Support] Fix no-stat build. NFC
After 9144e49334 this performs the same
transform inside the other ifdef.
2022-08-14 23:55:31 +01:00
Kazu Hirata 9144e49334 [Support] Drop unnecessary const from a return type (NFC)
Identified with readability-const-return-type.
2022-08-14 12:51:56 -07:00
Kazu Hirata 2a4748576e [ADT] Implement Optional::transform
This patch implements Optional::transform for consistency with
std::optional::transform in C++23.

Note that the new function is identical to Optional::map.  My plan is
to deprecate Optional::map after migrating all of its uses to
Optional::transform.

Differential Revision: https://reviews.llvm.org/D131829
2022-08-13 11:48:25 -07:00