[llvm] [CodeGen] Use 128bits for LaneBitmask. (PR #111157)
Jay Foad via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 4 07:03:16 PDT 2024
================
@@ -29,72 +29,120 @@
#ifndef LLVM_MC_LANEBITMASK_H
#define LLVM_MC_LANEBITMASK_H
+#include "llvm/ADT/APInt.h"
+#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Printable.h"
#include "llvm/Support/raw_ostream.h"
+#include <utility>
namespace llvm {
- struct LaneBitmask {
- // When changing the underlying type, change the format string as well.
- using Type = uint64_t;
- enum : unsigned { BitWidth = 8*sizeof(Type) };
- constexpr static const char *const FormatStr = "%016llX";
+struct LaneBitmask {
+ static constexpr unsigned int BitWidth = 128;
- constexpr LaneBitmask() = default;
- explicit constexpr LaneBitmask(Type V) : Mask(V) {}
-
- constexpr bool operator== (LaneBitmask M) const { return Mask == M.Mask; }
- constexpr bool operator!= (LaneBitmask M) const { return Mask != M.Mask; }
- constexpr bool operator< (LaneBitmask M) const { return Mask < M.Mask; }
- constexpr bool none() const { return Mask == 0; }
- constexpr bool any() const { return Mask != 0; }
- constexpr bool all() const { return ~Mask == 0; }
-
- constexpr LaneBitmask operator~() const {
- return LaneBitmask(~Mask);
- }
- constexpr LaneBitmask operator|(LaneBitmask M) const {
- return LaneBitmask(Mask | M.Mask);
- }
- constexpr LaneBitmask operator&(LaneBitmask M) const {
- return LaneBitmask(Mask & M.Mask);
- }
- LaneBitmask &operator|=(LaneBitmask M) {
- Mask |= M.Mask;
- return *this;
- }
- LaneBitmask &operator&=(LaneBitmask M) {
- Mask &= M.Mask;
- return *this;
+ explicit LaneBitmask(APInt V) {
+ switch (V.getBitWidth()) {
+ case BitWidth:
+ Mask[0] = V.getRawData()[0];
+ Mask[1] = V.getRawData()[1];
+ break;
+ default:
+ llvm_unreachable("Unsupported bitwidth");
}
+ }
+ constexpr explicit LaneBitmask(uint64_t Lo = 0, uint64_t Hi = 0) : Mask{Lo, Hi} {}
- constexpr Type getAsInteger() const { return Mask; }
+ constexpr bool operator==(LaneBitmask M) const {
+ return Mask[0] == M.Mask[0] && Mask[1] == M.Mask[1];
+ }
+ constexpr bool operator!=(LaneBitmask M) const {
+ return Mask[0] != M.Mask[0] || Mask[1] != M.Mask[1];
+ }
+ constexpr bool operator<(LaneBitmask M) const {
+ return Mask[1] < M.Mask[1] || Mask[0] < M.Mask[0];
+ }
+ constexpr bool none() const { return Mask[0] == 0 && Mask[1] == 0; }
+ constexpr bool any() const { return Mask[0] != 0 || Mask[1] != 0; }
+ constexpr bool all() const { return ~Mask[0] == 0 && ~Mask[1] == 0; }
- unsigned getNumLanes() const { return llvm::popcount(Mask); }
- unsigned getHighestLane() const {
- return Log2_64(Mask);
- }
+ constexpr LaneBitmask operator~() const { return LaneBitmask(~Mask[0], ~Mask[1]); }
+ constexpr LaneBitmask operator|(LaneBitmask M) const {
+ return LaneBitmask(Mask[0] | M.Mask[0], Mask[1] | M.Mask[1]);
+ }
+ constexpr LaneBitmask operator&(LaneBitmask M) const {
+ return LaneBitmask(Mask[0] & M.Mask[0], Mask[1] & M.Mask[1]);
+ }
+ LaneBitmask &operator|=(LaneBitmask M) {
+ Mask[0] |= M.Mask[0];
+ Mask[1] |= M.Mask[1];
+ return *this;
+ }
+ LaneBitmask &operator&=(LaneBitmask M) {
+ Mask[0] &= M.Mask[0];
+ Mask[1] &= M.Mask[1];
+ return *this;
+ }
- static constexpr LaneBitmask getNone() { return LaneBitmask(0); }
- static constexpr LaneBitmask getAll() { return ~LaneBitmask(0); }
- static constexpr LaneBitmask getLane(unsigned Lane) {
- return LaneBitmask(Type(1) << Lane);
- }
+ APInt getAsAPInt() const { return APInt(BitWidth, {Mask[0], Mask[1]}); }
+ constexpr std::pair<uint64_t, uint64_t> getAsPair() const { return {Mask[0], Mask[1]}; }
- private:
- Type Mask = 0;
- };
+ unsigned getNumLanes() const {
+ return Mask[1] ? llvm::popcount(Mask[1]) + llvm::popcount(Mask[0])
+ : llvm::popcount(Mask[0]);
+ }
+ unsigned getHighestLane() const {
+ return Mask[1] ? Log2_64(Mask[1]) + 64 : Log2_64(Mask[0]);
+ }
- /// Create Printable object to print LaneBitmasks on a \ref raw_ostream.
- inline Printable PrintLaneMask(LaneBitmask LaneMask) {
- return Printable([LaneMask](raw_ostream &OS) {
- OS << format(LaneBitmask::FormatStr, LaneMask.getAsInteger());
- });
+ static constexpr LaneBitmask getNone() { return LaneBitmask(0, 0); }
+ static constexpr LaneBitmask getAll() { return ~LaneBitmask(0, 0); }
+ static constexpr LaneBitmask getLane(unsigned Lane) {
+ return Lane >= 64 ? LaneBitmask(0, 1ULL << (Lane % 64))
+ : LaneBitmask(1ULL << Lane, 0);
}
+private:
+ uint64_t Mask[2];
+};
+
+/// Create Printable object to print LaneBitmasks on a \ref raw_ostream.
+/// If \p FormatAsCLiterals is true, it will print the bitmask as
----------------
jayfoad wrote:
Why do we need the option? Please can we just print it as a single hex literal with lots of digits?
https://github.com/llvm/llvm-project/pull/111157
More information about the llvm-commits
mailing list