Skip to content

Commit 21239c1

Browse files
authored
[NFC] Make fields of BrandTypeIterator constexpr (#8118)
Make enough constructors of Type and Field constexpr that we can make the fieldOptions array in BrandTypeIterator constexpr as well. This lets us remove logic for initializing this array at runtime.
1 parent 56f8f4f commit 21239c1

File tree

2 files changed

+28
-45
lines changed

2 files changed

+28
-45
lines changed

src/passes/MinimizeRecGroups.cpp

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,27 @@ struct TypeSCCs
107107
// provides an infinite sequence of possible brand types, prioritizing those
108108
// with the most compact encoding.
109109
struct BrandTypeIterator {
110-
// See `initFieldOptions` for the 18 options.
111110
static constexpr Index optionCount = 18;
112-
static std::array<Field, optionCount> fieldOptions;
113-
static void initFieldOptions();
111+
static constexpr std::array<Field, optionCount> fieldOptions = {{
112+
Field(Field::i8, Mutable),
113+
Field(Field::i16, Mutable),
114+
Field(Type::i32, Mutable),
115+
Field(Type::i64, Mutable),
116+
Field(Type::f32, Mutable),
117+
Field(Type::f64, Mutable),
118+
Field(Type(HeapType::any, Nullable), Mutable),
119+
Field(Type(HeapType::func, Nullable), Mutable),
120+
Field(Type(HeapType::ext, Nullable), Mutable),
121+
Field(Type(HeapType::none, Nullable), Mutable),
122+
Field(Type(HeapType::nofunc, Nullable), Mutable),
123+
Field(Type(HeapType::noext, Nullable), Mutable),
124+
Field(Type(HeapType::any, NonNullable), Mutable),
125+
Field(Type(HeapType::func, NonNullable), Mutable),
126+
Field(Type(HeapType::ext, NonNullable), Mutable),
127+
Field(Type(HeapType::none, NonNullable), Mutable),
128+
Field(Type(HeapType::nofunc, NonNullable), Mutable),
129+
Field(Type(HeapType::noext, NonNullable), Mutable),
130+
}};
114131

115132
struct FieldInfo {
116133
uint8_t index = 0;
@@ -316,32 +333,6 @@ void GroupClassInfo::permute(RecGroupInfo& info) {
316333
}
317334
}
318335

319-
std::array<Field, BrandTypeIterator::optionCount>
320-
BrandTypeIterator::fieldOptions = {{}};
321-
322-
void BrandTypeIterator::initFieldOptions() {
323-
BrandTypeIterator::fieldOptions = {{
324-
Field(Field::i8, Mutable),
325-
Field(Field::i16, Mutable),
326-
Field(Type::i32, Mutable),
327-
Field(Type::i64, Mutable),
328-
Field(Type::f32, Mutable),
329-
Field(Type::f64, Mutable),
330-
Field(Type(HeapType::any, Nullable), Mutable),
331-
Field(Type(HeapType::func, Nullable), Mutable),
332-
Field(Type(HeapType::ext, Nullable), Mutable),
333-
Field(Type(HeapType::none, Nullable), Mutable),
334-
Field(Type(HeapType::nofunc, Nullable), Mutable),
335-
Field(Type(HeapType::noext, Nullable), Mutable),
336-
Field(Type(HeapType::any, NonNullable), Mutable),
337-
Field(Type(HeapType::func, NonNullable), Mutable),
338-
Field(Type(HeapType::ext, NonNullable), Mutable),
339-
Field(Type(HeapType::none, NonNullable), Mutable),
340-
Field(Type(HeapType::nofunc, NonNullable), Mutable),
341-
Field(Type(HeapType::noext, NonNullable), Mutable),
342-
}};
343-
}
344-
345336
struct MinimizeRecGroups : Pass {
346337
// The types we are optimizing and their indices in this list.
347338
std::vector<HeapType> types;
@@ -387,8 +378,6 @@ struct MinimizeRecGroups : Pass {
387378
return;
388379
}
389380

390-
initBrandOptions();
391-
392381
auto typeInfo = ModuleUtils::collectHeapTypeInfo(
393382
*module,
394383
ModuleUtils::TypeInclusion::AllTypes,
@@ -445,15 +434,6 @@ struct MinimizeRecGroups : Pass {
445434
rewriteTypes(*module);
446435
}
447436

448-
void initBrandOptions() {
449-
// Initialize the field options for brand types lazily here to avoid
450-
// depending on global constructor ordering.
451-
[[maybe_unused]] static bool fieldsInitialized = []() {
452-
BrandTypeIterator::initFieldOptions();
453-
return true;
454-
}();
455-
}
456-
457437
void updateShapes() {
458438
while (!shapesToUpdate.empty()) {
459439
auto index = shapesToUpdate.back();

src/wasm-type.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ class Type {
324324
constexpr Type(BasicType id) : id(id) {}
325325

326326
// But converting raw TypeID is more dangerous, so make it explicit
327-
explicit Type(TypeID id) : id(id) {}
327+
explicit constexpr Type(TypeID id) : id(id) {}
328328

329329
// Construct tuple from a list of single types
330330
Type(std::initializer_list<Type>);
@@ -335,7 +335,9 @@ class Type {
335335

336336
// Construct from a heap type description. Also covers construction from
337337
// Signature, Struct or Array via implicit conversion to HeapType.
338-
Type(HeapType heapType, Nullability nullable, Exactness exact = Inexact)
338+
constexpr Type(HeapType heapType,
339+
Nullability nullable,
340+
Exactness exact = Inexact)
339341
: Type(heapType.getID() | (nullable == Nullable ? NullMask : 0) |
340342
(exact == Exact ? ExactMask : 0)) {
341343
assert(!(heapType.getID() &
@@ -659,10 +661,11 @@ struct Field {
659661
Mutability mutable_;
660662

661663
// Arbitrary defaults for convenience.
662-
Field() : type(Type::i32), packedType(not_packed), mutable_(Mutable) {}
663-
Field(Type type, Mutability mutable_)
664+
constexpr Field()
665+
: type(Type::i32), packedType(not_packed), mutable_(Mutable) {}
666+
constexpr Field(Type type, Mutability mutable_)
664667
: type(type), packedType(not_packed), mutable_(mutable_) {}
665-
Field(PackedType packedType, Mutability mutable_)
668+
constexpr Field(PackedType packedType, Mutability mutable_)
666669
: type(Type::i32), packedType(packedType), mutable_(mutable_) {}
667670

668671
constexpr bool isPacked() const {

0 commit comments

Comments
 (0)