Skip to content

Commit a30f1df

Browse files
authored
Move Type-related functions into Type class (NFC) (#2556)
Several type-related functions currently exist outside of `Type` class and thus in the `wasm`, effectively global, namespace. This moves these functions into `Type` class, making them either member functions or static functions. Also this renames `getSize` to `getByteSize` to make it not to be confused with `size`, which returns the number of types in multiple types. This also reorders the order of functions in `wasm-type.cpp` to match that of `wasm-type.h`.
1 parent f2ba91b commit a30f1df

15 files changed

+143
-128
lines changed

src/asm2wasm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2008,7 +2008,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
20082008
ret->offset = 0;
20092009
ret->align = view.bytes;
20102010
ret->ptr = processUnshifted(ast[2], view.bytes);
2011-
ret->type = getType(view.bytes, !view.integer);
2011+
ret->type = Type::get(view.bytes, !view.integer);
20122012
return ret;
20132013
} else if (what == UNARY_PREFIX) {
20142014
if (ast[1] == PLUS) {

src/ir/ExpressionAnalyzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ template<typename T> void visitImmediates(Expression* curr, T& visitor) {
151151
}
152152
void visitLoad(Load* curr) {
153153
visitor.visitInt(curr->bytes);
154-
if (curr->type != unreachable && curr->bytes < getTypeSize(curr->type)) {
154+
if (curr->type != unreachable && curr->bytes < curr->type.getByteSize()) {
155155
visitor.visitInt(curr->signed_);
156156
}
157157
visitor.visitAddress(curr->offset);

src/ir/load-utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ inline bool isSignRelevant(Load* load) {
3131
if (load->type == unreachable) {
3232
return false;
3333
}
34-
return !type.isFloat() && load->bytes < getTypeSize(type);
34+
return !type.isFloat() && load->bytes < type.getByteSize();
3535
}
3636

3737
// check if a load can be signed (which some opts want to do)

src/passes/Asyncify.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
10901090
Index total = 0;
10911091
for (Index i = 0; i < numPreservableLocals; i++) {
10921092
auto type = func->getLocalType(i);
1093-
auto size = getTypeSize(type);
1093+
auto size = type.getByteSize();
10941094
total += size;
10951095
}
10961096
auto* block = builder->makeBlock();
@@ -1101,7 +1101,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
11011101
Index offset = 0;
11021102
for (Index i = 0; i < numPreservableLocals; i++) {
11031103
auto type = func->getLocalType(i);
1104-
auto size = getTypeSize(type);
1104+
auto size = type.getByteSize();
11051105
assert(size % STACK_ALIGN == 0);
11061106
// TODO: higher alignment?
11071107
block->list.push_back(builder->makeLocalSet(
@@ -1130,7 +1130,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
11301130
Index offset = 0;
11311131
for (Index i = 0; i < numPreservableLocals; i++) {
11321132
auto type = func->getLocalType(i);
1133-
auto size = getTypeSize(type);
1133+
auto size = type.getByteSize();
11341134
assert(size % STACK_ALIGN == 0);
11351135
// TODO: higher alignment?
11361136
block->list.push_back(

src/passes/AvoidReinterprets.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ static bool canReplaceWithReinterpret(Load* load) {
3232
// a reinterpret of the same address. A partial load would see
3333
// more bytes and possibly invalid data, and an unreachable
3434
// pointer is just not interesting to handle.
35-
return load->type != unreachable && load->bytes == getTypeSize(load->type);
35+
return load->type != Type::unreachable &&
36+
load->bytes == load->type.getByteSize();
3637
}
3738

3839
static Load* getSingleLoad(LocalGraph* localGraph, LocalGet* get) {
@@ -116,7 +117,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
116117
// We should use another load here, to avoid reinterprets.
117118
info.ptrLocal = Builder::addVar(func, i32);
118119
info.reinterpretedLocal =
119-
Builder::addVar(func, reinterpretType(load->type));
120+
Builder::addVar(func, load->type.reinterpret());
120121
} else {
121122
unoptimizables.insert(load);
122123
}
@@ -150,8 +151,8 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
150151
auto& info = iter->second;
151152
// A reinterpret of a get of a load - use the new local.
152153
Builder builder(*module);
153-
replaceCurrent(builder.makeLocalGet(
154-
info.reinterpretedLocal, reinterpretType(load->type)));
154+
replaceCurrent(builder.makeLocalGet(info.reinterpretedLocal,
155+
load->type.reinterpret()));
155156
}
156157
}
157158
}
@@ -185,7 +186,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
185186
load->offset,
186187
load->align,
187188
ptr,
188-
reinterpretType(load->type));
189+
load->type.reinterpret());
189190
}
190191
} finalOptimizer(infos, localGraph, getModule());
191192

src/passes/ConstHoisting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct ConstHoisting : public WalkerPass<PostWalker<ConstHoisting>> {
8888
}
8989
case f32:
9090
case f64: {
91-
size = getTypeSize(value.type);
91+
size = value.type.getByteSize();
9292
break;
9393
}
9494
case v128: // v128 not implemented yet

src/passes/Print.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ struct PrintExpressionContents
186186
o << ".atomic";
187187
}
188188
o << ".load";
189-
if (curr->type != unreachable && curr->bytes < getTypeSize(curr->type)) {
189+
if (curr->type != unreachable && curr->bytes < curr->type.getByteSize()) {
190190
if (curr->bytes == 1) {
191191
o << '8';
192192
} else if (curr->bytes == 2) {
@@ -233,7 +233,7 @@ struct PrintExpressionContents
233233
}
234234
static void printRMWSize(std::ostream& o, Type type, uint8_t bytes) {
235235
prepareColor(o) << forceConcrete(type) << ".atomic.rmw";
236-
if (type != unreachable && bytes != getTypeSize(type)) {
236+
if (type != unreachable && bytes != type.getByteSize()) {
237237
if (bytes == 1) {
238238
o << '8';
239239
} else if (bytes == 2) {
@@ -269,7 +269,7 @@ struct PrintExpressionContents
269269
o << "xchg";
270270
break;
271271
}
272-
if (curr->type != unreachable && curr->bytes != getTypeSize(curr->type)) {
272+
if (curr->type != unreachable && curr->bytes != curr->type.getByteSize()) {
273273
o << "_u";
274274
}
275275
restoreNormalColor(o);
@@ -281,7 +281,7 @@ struct PrintExpressionContents
281281
prepareColor(o);
282282
printRMWSize(o, curr->type, curr->bytes);
283283
o << "cmpxchg";
284-
if (curr->type != unreachable && curr->bytes != getTypeSize(curr->type)) {
284+
if (curr->type != unreachable && curr->bytes != curr->type.getByteSize()) {
285285
o << "_u";
286286
}
287287
restoreNormalColor(o);

src/passes/SafeHeap.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ struct SafeHeap : public Pass {
176176
load.type = type;
177177
for (Index bytes : {1, 2, 4, 8, 16}) {
178178
load.bytes = bytes;
179-
if (bytes > getTypeSize(type) || (type == f32 && bytes != 4) ||
179+
if (bytes > type.getByteSize() || (type == f32 && bytes != 4) ||
180180
(type == f64 && bytes != 8) || (type == v128 && bytes != 16)) {
181181
continue;
182182
}
@@ -212,7 +212,7 @@ struct SafeHeap : public Pass {
212212
store.type = none;
213213
for (Index bytes : {1, 2, 4, 8, 16}) {
214214
store.bytes = bytes;
215-
if (bytes > getTypeSize(valueType) ||
215+
if (bytes > valueType.getByteSize() ||
216216
(valueType == f32 && bytes != 4) ||
217217
(valueType == f64 && bytes != 8) ||
218218
(valueType == v128 && bytes != 16)) {

src/passes/SpillPointers.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct SpillPointers
7878
PointerMap pointerMap;
7979
for (Index i = 0; i < func->getNumLocals(); i++) {
8080
if (func->getLocalType(i) == ABI::PointerType) {
81-
auto offset = pointerMap.size() * getTypeSize(ABI::PointerType);
81+
auto offset = pointerMap.size() * ABI::PointerType.getByteSize();
8282
pointerMap[i] = offset;
8383
}
8484
}
@@ -140,7 +140,7 @@ struct SpillPointers
140140
// get the stack space, and set the local to it
141141
ABI::getStackSpace(spillLocal,
142142
func,
143-
getTypeSize(ABI::PointerType) * pointerMap.size(),
143+
ABI::PointerType.getByteSize() * pointerMap.size(),
144144
*getModule());
145145
}
146146
}
@@ -184,9 +184,9 @@ struct SpillPointers
184184
// add the spills
185185
for (auto index : toSpill) {
186186
block->list.push_back(
187-
builder.makeStore(getTypeSize(ABI::PointerType),
187+
builder.makeStore(ABI::PointerType.getByteSize(),
188188
pointerMap[index],
189-
getTypeSize(ABI::PointerType),
189+
ABI::PointerType.getByteSize(),
190190
builder.makeLocalGet(spillLocal, ABI::PointerType),
191191
builder.makeLocalGet(index, ABI::PointerType),
192192
ABI::PointerType));

src/wasm-interpreter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,7 @@ template<typename GlobalManager, typename SubType> class ModuleInstanceBase {
17191719
if (timeout.breaking()) {
17201720
return timeout;
17211721
}
1722-
auto bytes = getTypeSize(curr->expectedType);
1722+
auto bytes = curr->expectedType.getByteSize();
17231723
auto addr = instance.getFinalAddress(ptr.value, bytes);
17241724
auto loaded = instance.doAtomicLoad(addr, bytes, curr->expectedType);
17251725
NOTE_EVAL1(loaded);

src/wasm-type.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ class Type {
7979

8080
// Allows for using Types in switch statements
8181
constexpr operator uint32_t() const { return id; }
82+
83+
// Returns the type size in bytes. Only single types are supported.
84+
unsigned getByteSize() const;
85+
86+
// Reinterpret an integer type to a float type with the same size and vice
87+
// versa. Only single integer and float types are supported.
88+
Type reinterpret() const;
89+
90+
// Returns the feature set required to use this type.
91+
FeatureSet getFeatures() const;
92+
93+
// Returns a number type based on its size in bytes and whether it is a float
94+
// type.
95+
static Type get(unsigned byteSize, bool float_);
96+
8297
std::string toString() const;
8398
};
8499

@@ -123,11 +138,6 @@ constexpr Type anyref = Type::anyref;
123138
constexpr Type exnref = Type::exnref;
124139
constexpr Type unreachable = Type::unreachable;
125140

126-
unsigned getTypeSize(Type type);
127-
FeatureSet getFeatures(Type type);
128-
Type getType(unsigned size, bool float_);
129-
Type reinterpretType(Type type);
130-
131141
} // namespace wasm
132142

133143
template<> class std::hash<wasm::Signature> {

src/wasm/wasm-binary.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2974,7 +2974,7 @@ bool WasmBinaryBuilder::maybeVisitAtomicWait(Expression*& out, uint8_t code) {
29742974
curr->ptr = popNonVoidExpression();
29752975
Address readAlign;
29762976
readMemoryAccess(readAlign, curr->offset);
2977-
if (readAlign != getTypeSize(curr->expectedType)) {
2977+
if (readAlign != curr->expectedType.getByteSize()) {
29782978
throwError("Align of AtomicWait must match size");
29792979
}
29802980
curr->finalize();
@@ -2994,7 +2994,7 @@ bool WasmBinaryBuilder::maybeVisitAtomicNotify(Expression*& out, uint8_t code) {
29942994
curr->ptr = popNonVoidExpression();
29952995
Address readAlign;
29962996
readMemoryAccess(readAlign, curr->offset);
2997-
if (readAlign != getTypeSize(curr->type)) {
2997+
if (readAlign != curr->type.getByteSize()) {
29982998
throwError("Align of AtomicNotify must match size");
29992999
}
30003000
curr->finalize();

src/wasm/wasm-s-parser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ SExpressionWasmBuilder::makeLoad(Element& s, Type type, bool isAtomic) {
12681268
auto* ret = allocator.alloc<Load>();
12691269
ret->isAtomic = isAtomic;
12701270
ret->type = type;
1271-
ret->bytes = parseMemBytes(extra, getTypeSize(type));
1271+
ret->bytes = parseMemBytes(extra, type.getByteSize());
12721272
ret->signed_ = extra[0] && extra[1] == 's';
12731273
size_t i = parseMemAttributes(s, &ret->offset, &ret->align, ret->bytes);
12741274
ret->ptr = parseExpression(s[i]);
@@ -1282,7 +1282,7 @@ SExpressionWasmBuilder::makeStore(Element& s, Type type, bool isAtomic) {
12821282
auto ret = allocator.alloc<Store>();
12831283
ret->isAtomic = isAtomic;
12841284
ret->valueType = type;
1285-
ret->bytes = parseMemBytes(extra, getTypeSize(type));
1285+
ret->bytes = parseMemBytes(extra, type.getByteSize());
12861286
size_t i = parseMemAttributes(s, &ret->offset, &ret->align, ret->bytes);
12871287
ret->ptr = parseExpression(s[i]);
12881288
ret->value = parseExpression(s[i + 1]);
@@ -1294,7 +1294,7 @@ Expression* SExpressionWasmBuilder::makeAtomicRMWOrCmpxchg(Element& s,
12941294
Type type) {
12951295
const char* extra = findMemExtra(
12961296
*s[0], 11 /* after "type.atomic.rmw" */, /* isAtomic = */ false);
1297-
auto bytes = parseMemBytes(extra, getTypeSize(type));
1297+
auto bytes = parseMemBytes(extra, type.getByteSize());
12981298
extra = strchr(extra, '.'); // after the optional '_u' and before the opcode
12991299
if (!extra) {
13001300
throw ParseException("malformed atomic rmw instruction");

0 commit comments

Comments
 (0)