Skip to content

[NFC] Address bit-field storage sizes to ensure ideal packing #139825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clang/include/clang/AST/DeclTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,8 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl,
/// This needs to be cached as deduction is performed during declaration,
/// and we need the information to be preserved so that it is consistent
/// during instantiation.
bool StrictPackMatch : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned StrictPackMatch : 1;

protected:
ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Expand Down
4 changes: 3 additions & 1 deletion clang/include/clang/Analysis/Analyses/ThreadSafetyTIL.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -1664,7 +1665,8 @@ class BasicBlock : public SExpr {
unsigned BlockID : 31;

// Bit to determine if a block has been visited during a traversal.
bool Visited : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned Visited : 1;

// Predecessor blocks in the CFG.
BlockArray Predecessors;
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticCategories.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace clang {
namespace diag {
enum {
enum DiagCategory {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured naming this would not hurt, as I'm updating its storage and not specifying the type made me feel it was at risk of slipping through the cracks.

I've removed the storage sizes changes I was going to make so it could just be a correctly typed field as well on the basis that once I'm done with this round of changes I may go through and start trying to reduce the use of preferred type enums

#define GET_CATEGORY_TABLE
#define CATEGORY(X, ENUM) ENUM,
#include "clang/Basic/DiagnosticGroups.inc"
Expand Down
6 changes: 4 additions & 2 deletions clang/include/clang/Sema/Overload.h
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,8 @@ class Sema;
/// Have we matched any packs on the parameter side, versus any non-packs on
/// the argument side, in a context where the opposite matching is also
/// allowed?
bool StrictPackMatch : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned StrictPackMatch : 1;

/// True if the candidate was found using ADL.
LLVM_PREFERRED_TYPE(CallExpr::ADLCallKind)
Expand All @@ -996,7 +997,8 @@ class Sema;

/// FailureKind - The reason why this candidate is not viable.
/// Actually an OverloadFailureKind.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I remove this comment now that there's a preferred type annotation?

unsigned char FailureKind;
LLVM_PREFERRED_TYPE(OverloadFailureKind)
unsigned FailureKind : 8;

/// The number of call arguments that were explicitly provided,
/// to be used while performing partial ordering of function templates.
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/ScopeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ enum class FirstCoroutineStmtKind { CoReturn, CoAwait, CoYield };
/// currently being parsed.
class FunctionScopeInfo {
protected:
enum ScopeKind {
enum ScopeKind : uint8_t {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the restricted uses, making the storage type explicit here seemed like the best call

SK_Function,
SK_Block,
SK_Lambda,
Expand Down
6 changes: 4 additions & 2 deletions clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Compiler.h"
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -269,7 +270,8 @@ class AnalyzerOptions {
unsigned NoRetryExhausted : 1;

/// Emit analyzer warnings as errors.
bool AnalyzerWerror : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned AnalyzerWerror : 1;

/// The inlining stack depth limit.
unsigned InlineMaxStackDepth;
Expand Down Expand Up @@ -410,7 +412,7 @@ class AnalyzerOptions {
// an alias to the new verbose filename option because this
// closely mimics the behavior under the old option.
ShouldWriteStableReportFilename || ShouldWriteVerboseReportFilename,
AnalyzerWerror,
static_cast<bool>(AnalyzerWerror),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast is needed before clang20 as we generate a warning about narrowing AnalyzerWerror from an int1 to a bool :D

ShouldApplyFixIts,
ShouldDisplayCheckerNameForText};
}
Expand Down
17 changes: 10 additions & 7 deletions clang/lib/Basic/DiagnosticIDs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringTable.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Path.h"
#include <map>
Expand Down Expand Up @@ -74,19 +75,21 @@ enum DiagnosticClass {
struct StaticDiagInfoRec {
uint16_t DiagID;
LLVM_PREFERRED_TYPE(diag::Severity)
uint8_t DefaultSeverity : 3;
uint16_t DefaultSeverity : 3;
LLVM_PREFERRED_TYPE(DiagnosticClass)
uint8_t Class : 3;
uint16_t Class : 3;
LLVM_PREFERRED_TYPE(DiagnosticIDs::SFINAEResponse)
uint8_t SFINAE : 2;
uint8_t Category : 6;
uint16_t SFINAE : 2;
LLVM_PREFERRED_TYPE(diag::DiagCategory)
uint16_t Category : 6;
LLVM_PREFERRED_TYPE(bool)
uint8_t WarnNoWerror : 1;
uint16_t WarnNoWerror : 1;
LLVM_PREFERRED_TYPE(bool)
uint8_t WarnShowInSystemHeader : 1;
uint16_t WarnShowInSystemHeader : 1;
LLVM_PREFERRED_TYPE(bool)
uint8_t WarnShowInSystemMacro : 1;
uint16_t WarnShowInSystemMacro : 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really wish we had some way of directly asserting the size/packing/offsets, imagine a hyopthetical

static_assert(__builtin_current_offset() == x)

It would make reasoning about layout significantly easier


LLVM_PREFERRED_TYPE(diag::Group)
uint16_t OptionGroupIndex : 15;
LLVM_PREFERRED_TYPE(bool)
uint16_t Deferrable : 1;
Expand Down
10 changes: 7 additions & 3 deletions llvm/include/llvm/ADT/ImmutableSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/iterator.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include <cassert>
#include <cstdint>
Expand Down Expand Up @@ -213,9 +214,12 @@ class ImutAVLTree {
ImutAVLTree *next = nullptr;

unsigned height : 28;
bool IsMutable : 1;
bool IsDigestCached : 1;
bool IsCanonicalized : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned IsMutable : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned IsDigestCached : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned IsCanonicalized : 1;

value_type value;
uint32_t digest = 0;
Expand Down
12 changes: 9 additions & 3 deletions llvm/include/llvm/Bitstream/BitCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Bitstream/BitCodeEnums.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/ErrorHandling.h"
#include <cassert>
Expand All @@ -31,9 +32,6 @@ namespace llvm {
/// 2. It could be an encoding specification ("this operand encoded like so").
///
class BitCodeAbbrevOp {
uint64_t Val; // A literal value or data for an encoding.
bool IsLiteral : 1; // Indicate whether this is a literal value or not.
unsigned Enc : 3; // The encoding to use.
public:
enum Encoding {
Fixed = 1, // A fixed width field, Val specifies number of bits.
Expand All @@ -43,6 +41,14 @@ class BitCodeAbbrevOp {
Blob = 5 // 32-bit aligned array of 8-bit characters.
};

protected:
uint64_t Val; // A literal value or data for an encoding.
LLVM_PREFERRED_TYPE(bool)
uint64_t IsLiteral : 1; // Indicate whether this is a literal value or not.
LLVM_PREFERRED_TYPE(Encoding)
uint64_t Enc : 3; // The encoding to use.

public:
static bool isValidEncoding(uint64_t E) {
return E >= 1 && E <= 5;
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/CodeGen/MachineInstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class MachineInstr
/// Various bits of information used by the AsmPrinter to emit helpful
/// comments. This is *not* semantic information. Do not use this for
/// anything other than to convey comment information to AsmPrinter.
uint8_t AsmPrinterFlags : LLVM_MI_ASMPRINTERFLAGS_BITS;
uint32_t AsmPrinterFlags : LLVM_MI_ASMPRINTERFLAGS_BITS;

/// Internal implementation detail class that provides out-of-line storage for
/// extra info used by the machine instruction when this info cannot be stored
Expand Down
7 changes: 4 additions & 3 deletions llvm/include/llvm/Demangle/ItaniumDemangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "DemangleConfig.h"
#include "StringViewExtras.h"
#include "Utility.h"
#include "llvm/Support/Compiler.h"
#include <algorithm>
#include <cctype>
#include <cstdio>
Expand Down Expand Up @@ -164,18 +165,18 @@ class NodeArray;
// traversed by the printLeft/Right functions to produce a demangled string.
class Node {
public:
enum Kind : unsigned char {
enum Kind : uint8_t {
#define NODE(NodeKind) K##NodeKind,
#include "ItaniumNodes.def"
};

/// Three-way bool to track a cached value. Unknown is possible if this node
/// has an unexpanded parameter pack below it that may affect this cache.
enum class Cache : unsigned char { Yes, No, Unknown, };
enum class Cache : uint8_t { Yes, No, Unknown, };

/// Operator precedence for expression nodes. Used to determine required
/// parens in expression emission.
enum class Prec {
enum class Prec : uint8_t {
Primary,
Postfix,
Unary,
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/IR/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -1076,8 +1076,8 @@ class MDNode : public Metadata {
/// Explicity set alignment because bitfields by default have an
/// alignment of 1 on z/OS.
struct alignas(alignof(size_t)) Header {
bool IsResizable : 1;
bool IsLarge : 1;
size_t IsResizable : 1;
size_t IsLarge : 1;
size_t SmallSize : 4;
size_t SmallNumOps : 4;
size_t : sizeof(size_t) * CHAR_BIT - 10;
Expand Down
4 changes: 3 additions & 1 deletion llvm/include/llvm/IR/ModuleSummaryIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/InterleavedRange.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/ScaledNumber.h"
Expand Down Expand Up @@ -72,7 +73,8 @@ struct CalleeInfo {
uint32_t Hotness : 3;

// True if at least one of the calls to the callee is a tail call.
bool HasTailCall : 1;
LLVM_PREFERRED_TYPE(bool)
uint32_t HasTailCall : 1;

/// The value stored in RelBlockFreq has to be interpreted as the digits of
/// a scaled number with a scale of \p -ScaleShift.
Expand Down
6 changes: 4 additions & 2 deletions llvm/include/llvm/IR/User.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ class User : public Value {
struct AllocInfo {
public:
const unsigned NumOps : NumUserOperandsBits;
const bool HasHungOffUses : 1;
const bool HasDescriptor : 1;
LLVM_PREFERRED_TYPE(bool)
const unsigned HasHungOffUses : 1;
LLVM_PREFERRED_TYPE(bool)
const unsigned HasDescriptor : 1;

AllocInfo() = delete;

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
int DataOffset : 31;

/// Non-zero if this is a piece of an aggregate.
uint16_t IsSubfield : 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ideally would not be necessary as I believe this field should be 2 byte aligned here and so ms abi would not add unneeded padding, but when making this diagnostic I found that diagnosing this correctly required duplicating the record layout to essentially track the addition of otherwise unneeded padding, and I was concerned that you would get surprising behavior from otherwise "simple" changes (e.g. moving a bit-field around could suddenly unalign another field and trigger terrible padding)

uint32_t IsSubfield : 1;

/// Offset into aggregate.
uint16_t StructOffset : 15;
uint32_t StructOffset : 15;

/// Register containing the data or the register base of the memory
/// location containing the data.
Expand Down
9 changes: 6 additions & 3 deletions llvm/lib/Target/AArch64/AArch64CollectLOH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,12 @@ static int mapRegToGPRIndex(MCRegister Reg) {
/// datastructure for each tracked general purpose register.
struct LOHInfo {
MCLOHType Type : 8; ///< "Best" type of LOH possible.
bool IsCandidate : 1; ///< Possible LOH candidate.
bool OneUser : 1; ///< Found exactly one user (yet).
bool MultiUsers : 1; ///< Found multiple users.
LLVM_PREFERRED_TYPE(bool)
unsigned IsCandidate : 1; ///< Possible LOH candidate.
LLVM_PREFERRED_TYPE(bool)
unsigned OneUser : 1; ///< Found exactly one user (yet).
LLVM_PREFERRED_TYPE(bool)
unsigned MultiUsers : 1; ///< Found multiple users.
const MachineInstr *MI0; ///< First instruction involved in the LOH.
const MachineInstr *MI1; ///< Second instruction involved in the LOH
/// (if any).
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/ARM/ARMConstantIslandPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ namespace {
struct ImmBranch {
MachineInstr *MI;
unsigned MaxDisp : 31;
bool isCond : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned isCond : 1;
unsigned UncondBr;

ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, unsigned ubr)
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/CSKY/CSKYConstantIslandPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ class CSKYConstantIslands : public MachineFunctionPass {
struct ImmBranch {
MachineInstr *MI;
unsigned MaxDisp : 31;
bool IsCond : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned IsCond : 1;
int UncondBr;

ImmBranch(MachineInstr *Mi, unsigned Maxdisp, bool Cond, int Ubr)
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/Mips/MipsConstantIslandPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ namespace {
struct ImmBranch {
MachineInstr *MI;
unsigned MaxDisp : 31;
bool isCond : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned isCond : 1;
int UncondBr;

ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
Expand Down
Loading