Skip to content

Commit 5045571

Browse files
authored
[NFC] Represent exactness and sharedness with one bit (#7500)
Since the Type representation only needs to include a bit for sharedness for basic heap types and only needs to include a bit for exactness for non-basic heap types, they can use the same bit. Using fewer bits in the Type representation is beneficial because it reduces the minimum alignment of HeapTypeInfos required to avoid trampling the reserved bits.
1 parent ac98ba6 commit 5045571

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

src/wasm-type.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ class HeapType {
9696
// should also be passed by value.
9797
uintptr_t id;
9898

99-
static constexpr int TypeBits = 3;
99+
static constexpr int TypeBits = 2;
100100
static constexpr int UsedBits = TypeBits + 1;
101101
static constexpr int SharedMask = 1 << TypeBits;
102+
friend class Type;
102103

103104
public:
104105
// Bits 0-2 are used by the Type representation, so need to be left free.
@@ -290,6 +291,11 @@ class Type {
290291
static constexpr int NullMask = 1 << 1;
291292
static constexpr int ExactMask = 1 << 2;
292293

294+
// Only abstract heap types store sharedness as a bit in the representation
295+
// and only non-abstract heap types can be exact, so exactness and sharedness
296+
// can use the same bit.
297+
static_assert(ExactMask == HeapType::SharedMask);
298+
293299
public:
294300
enum BasicType : uint32_t {
295301
none = 0,
@@ -322,7 +328,8 @@ class Type {
322328
Type(HeapType heapType, Nullability nullable, Exactness exact = Inexact)
323329
: Type(heapType.getID() | (nullable == Nullable ? NullMask : 0) |
324330
(exact == Exact ? ExactMask : 0)) {
325-
assert(!(heapType.getID() & (TupleMask | NullMask | ExactMask)));
331+
assert(!(heapType.getID() &
332+
(TupleMask | NullMask | (heapType.isBasic() ? 0 : ExactMask))));
326333
assert(!heapType.isBasic() || exact == Inexact);
327334
}
328335

@@ -372,11 +379,18 @@ class Type {
372379
bool isRef() const { return !isBasic() && !(id & TupleMask); }
373380
bool isNullable() const { return isRef() && (id & NullMask); }
374381
bool isNonNullable() const { return isRef() && !(id & NullMask); }
375-
bool isExact() const { return isRef() && (id & ExactMask); }
382+
bool isExact() const {
383+
return isRef() && !getHeapType().isBasic() && (id & ExactMask);
384+
}
376385
bool isInexact() const { return isRef() && !(id & ExactMask); }
377386
HeapType getHeapType() const {
378387
assert(isRef());
379-
return HeapType(id & ~(NullMask | ExactMask));
388+
HeapType masked(id & ~NullMask);
389+
// Avoid masking off the shared bit on basic heap types.
390+
if (!masked.isBasic()) {
391+
masked = HeapType(masked.id & ~ExactMask);
392+
}
393+
return masked;
380394
}
381395

382396
bool isFunction() const { return isRef() && getHeapType().isFunction(); }

test/example/c-api-kitchen-sink.txt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ BinaryenTypeAuto: -1
2020
BinaryenPackedTypeNotPacked: 0
2121
BinaryenPackedTypeInt8: 1
2222
BinaryenPackedTypeInt16: 2
23-
BinaryenHeapTypeExt: 16
24-
BinaryenHeapTypeFunc: 32
25-
BinaryenHeapTypeAny: 64
26-
BinaryenHeapTypeEq: 80
27-
BinaryenHeapTypeI31: 96
28-
BinaryenHeapTypeStruct: 112
29-
BinaryenHeapTypeArray: 128
30-
BinaryenHeapTypeString: 160
31-
BinaryenHeapTypeNone: 176
32-
BinaryenHeapTypeNoext: 192
33-
BinaryenHeapTypeNofunc: 208
23+
BinaryenHeapTypeExt: 8
24+
BinaryenHeapTypeFunc: 16
25+
BinaryenHeapTypeAny: 32
26+
BinaryenHeapTypeEq: 40
27+
BinaryenHeapTypeI31: 48
28+
BinaryenHeapTypeStruct: 56
29+
BinaryenHeapTypeArray: 64
30+
BinaryenHeapTypeString: 80
31+
BinaryenHeapTypeNone: 88
32+
BinaryenHeapTypeNoext: 96
33+
BinaryenHeapTypeNofunc: 104
3434
BinaryenFeatureMVP: 0
3535
BinaryenFeatureAtomics: 1
3636
BinaryenFeatureBulkMemory: 16

0 commit comments

Comments
 (0)