Skip to content

Commit d15d49b

Browse files
nicoV8 LUCI CQ
authored andcommitted
Make bitfields only as wide as necessary for enums
clang now complains when a BitField for an enum is too wide. We could suppress this, but it seems kind of useful from an uninformed distance, so I made a few bitfields smaller instead. (For AddressingMode, since its size is target-dependent, I added an explicit underlying type to the enum instead, which suppresses the diag on a per-enum basis.) This is without any understanding of the code I'm touching. Especially the change in v8-internal.h feels a bit risky to me. Bug: chromium:1348574 Change-Id: I73395de593045036b72dadf4e3147b5f7e13c958 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3794708 Commit-Queue: Nico Weber <[email protected]> Reviewed-by: Leszek Swirski <[email protected]> Reviewed-by: Hannes Payer <[email protected]> Auto-Submit: Nico Weber <[email protected]> Cr-Commit-Position: refs/heads/main@{#82109}
1 parent 9182c02 commit d15d49b

8 files changed

Lines changed: 14 additions & 9 deletions

File tree

include/v8-internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ class Internals {
574574

575575
static const int kNodeClassIdOffset = 1 * kApiSystemPointerSize;
576576
static const int kNodeFlagsOffset = 1 * kApiSystemPointerSize + 3;
577-
static const int kNodeStateMask = 0x7;
577+
static const int kNodeStateMask = 0x3;
578578
static const int kNodeStateIsWeakValue = 2;
579579

580580
static const int kFirstNonstringType = 0x80;

src/ast/ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ class Literal final : public Expression {
10061006
friend class AstNodeFactory;
10071007
friend Zone;
10081008

1009-
using TypeField = Expression::NextBitField<Type, 4>;
1009+
using TypeField = Expression::NextBitField<Type, 3>;
10101010

10111011
Literal(int smi, int position) : Expression(position, kLiteral), smi_(smi) {
10121012
bit_field_ = TypeField::update(bit_field_, kSmi);

src/base/bit-field.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class BitField final {
4040
static constexpr U kNumValues = U{1} << kSize;
4141

4242
// Value for the field with all bits set.
43+
// If clang complains
44+
// "constexpr variable 'kMax' must be initialized by a constant expression"
45+
// on this line, then you're creating a BitField for an enum with more bits
46+
// than needed for the enum values. Either reduce the BitField size,
47+
// or give the enum an explicit underlying type.
4348
static constexpr T kMax = static_cast<T>(kNumValues - 1);
4449

4550
template <class T2, int size2>

src/compiler/backend/instruction-codes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
198198
V(None) \
199199
TARGET_ADDRESSING_MODE_LIST(V)
200200

201-
enum AddressingMode {
201+
enum AddressingMode : uint8_t {
202202
#define DECLARE_ADDRESSING_MODE(Name) kMode_##Name,
203203
ADDRESSING_MODE_LIST(DECLARE_ADDRESSING_MODE)
204204
#undef DECLARE_ADDRESSING_MODE
@@ -309,7 +309,7 @@ using MiscField = base::BitField<int, 22, 10>;
309309
// LaneSizeField and AccessModeField are helper types to encode/decode a lane
310310
// size, an access mode, or both inside the overlapping MiscField.
311311
using LaneSizeField = base::BitField<int, 22, 8>;
312-
using AccessModeField = base::BitField<MemoryAccessMode, 30, 2>;
312+
using AccessModeField = base::BitField<MemoryAccessMode, 30, 1>;
313313
// TODO(turbofan): {HasMemoryAccessMode} is currently only used to guard
314314
// decoding (in CodeGenerator and InstructionScheduler). Encoding (in
315315
// InstructionSelector) is not yet guarded. There are in fact instructions for

src/compiler/backend/instruction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,8 @@ class LocationOperand : public InstructionOperand {
591591
}
592592

593593
static_assert(KindField::kSize == 3);
594-
using LocationKindField = base::BitField64<LocationKind, 3, 2>;
595-
using RepresentationField = base::BitField64<MachineRepresentation, 5, 8>;
594+
using LocationKindField = base::BitField64<LocationKind, 3, 1>;
595+
using RepresentationField = LocationKindField::Next<MachineRepresentation, 8>;
596596
using IndexField = base::BitField64<int32_t, 35, 29>;
597597
};
598598

src/handles/global-handles.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ class GlobalHandles::Node final : public NodeBase<GlobalHandles::Node> {
575575

576576
// This stores three flags (independent, partially_dependent and
577577
// in_young_list) and a State.
578-
using NodeState = base::BitField8<State, 0, 3>;
578+
using NodeState = base::BitField8<State, 0, 2>;
579579
// Tracks whether the node is contained in the set of young nodes. This bit
580580
// persists across allocating and freeing a node as it's only cleaned up
581581
// when young nodes are proccessed.

src/maglev/maglev-ir.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ class OpProperties {
315315
return kNeedsRegisterSnapshotBit::decode(bitfield_);
316316
}
317317
constexpr bool is_pure() const {
318-
return (bitfield_ | kPureMask) == kPureValue;
318+
return (bitfield_ & kPureMask) == kPureValue;
319319
}
320320
constexpr bool is_required_when_unused() const {
321321
return can_write() || non_memory_side_effects();

src/wasm/wasm-code-manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ class V8_EXPORT_PRIVATE WasmCode final {
487487
int trap_handler_index_ = -1;
488488

489489
// Bits encoded in {flags_}:
490-
using KindField = base::BitField8<Kind, 0, 3>;
490+
using KindField = base::BitField8<Kind, 0, 2>;
491491
using ExecutionTierField = KindField::Next<ExecutionTier, 2>;
492492
using ForDebuggingField = ExecutionTierField::Next<ForDebugging, 2>;
493493

0 commit comments

Comments
 (0)