Skip to content
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

Move Type-related functions into Type class (NFC) #2556

Merged
merged 4 commits into from
Dec 30, 2019
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
2 changes: 1 addition & 1 deletion src/asm2wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2008,7 +2008,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
ret->offset = 0;
ret->align = view.bytes;
ret->ptr = processUnshifted(ast[2], view.bytes);
ret->type = getType(view.bytes, !view.integer);
ret->type = Type::get(view.bytes, !view.integer);
return ret;
} else if (what == UNARY_PREFIX) {
if (ast[1] == PLUS) {
Expand Down
2 changes: 1 addition & 1 deletion src/ir/ExpressionAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ template<typename T> void visitImmediates(Expression* curr, T& visitor) {
}
void visitLoad(Load* curr) {
visitor.visitInt(curr->bytes);
if (curr->type != unreachable && curr->bytes < getTypeSize(curr->type)) {
if (curr->type != unreachable && curr->bytes < curr->type.getByteSize()) {
visitor.visitInt(curr->signed_);
}
visitor.visitAddress(curr->offset);
Expand Down
2 changes: 1 addition & 1 deletion src/ir/load-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ inline bool isSignRelevant(Load* load) {
if (load->type == unreachable) {
return false;
}
return !type.isFloat() && load->bytes < getTypeSize(type);
return !type.isFloat() && load->bytes < type.getByteSize();
}

// check if a load can be signed (which some opts want to do)
Expand Down
6 changes: 3 additions & 3 deletions src/passes/Asyncify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
Index total = 0;
for (Index i = 0; i < numPreservableLocals; i++) {
auto type = func->getLocalType(i);
auto size = getTypeSize(type);
auto size = type.getByteSize();
total += size;
}
auto* block = builder->makeBlock();
Expand All @@ -1101,7 +1101,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
Index offset = 0;
for (Index i = 0; i < numPreservableLocals; i++) {
auto type = func->getLocalType(i);
auto size = getTypeSize(type);
auto size = type.getByteSize();
assert(size % STACK_ALIGN == 0);
// TODO: higher alignment?
block->list.push_back(builder->makeLocalSet(
Expand Down Expand Up @@ -1130,7 +1130,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
Index offset = 0;
for (Index i = 0; i < numPreservableLocals; i++) {
auto type = func->getLocalType(i);
auto size = getTypeSize(type);
auto size = type.getByteSize();
assert(size % STACK_ALIGN == 0);
// TODO: higher alignment?
block->list.push_back(
Expand Down
11 changes: 6 additions & 5 deletions src/passes/AvoidReinterprets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ static bool canReplaceWithReinterpret(Load* load) {
// a reinterpret of the same address. A partial load would see
// more bytes and possibly invalid data, and an unreachable
// pointer is just not interesting to handle.
return load->type != unreachable && load->bytes == getTypeSize(load->type);
return load->type != Type::unreachable &&
load->bytes == load->type.getByteSize();
}

static Load* getSingleLoad(LocalGraph* localGraph, LocalGet* get) {
Expand Down Expand Up @@ -116,7 +117,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
// We should use another load here, to avoid reinterprets.
info.ptrLocal = Builder::addVar(func, i32);
info.reinterpretedLocal =
Builder::addVar(func, reinterpretType(load->type));
Builder::addVar(func, load->type.reinterpret());
} else {
unoptimizables.insert(load);
}
Expand Down Expand Up @@ -150,8 +151,8 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
auto& info = iter->second;
// A reinterpret of a get of a load - use the new local.
Builder builder(*module);
replaceCurrent(builder.makeLocalGet(
info.reinterpretedLocal, reinterpretType(load->type)));
replaceCurrent(builder.makeLocalGet(info.reinterpretedLocal,
load->type.reinterpret()));
}
}
}
Expand Down Expand Up @@ -185,7 +186,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
load->offset,
load->align,
ptr,
reinterpretType(load->type));
load->type.reinterpret());
}
} finalOptimizer(infos, localGraph, getModule());

Expand Down
2 changes: 1 addition & 1 deletion src/passes/ConstHoisting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct ConstHoisting : public WalkerPass<PostWalker<ConstHoisting>> {
}
case f32:
case f64: {
size = getTypeSize(value.type);
size = value.type.getByteSize();
break;
}
case v128: // v128 not implemented yet
Expand Down
8 changes: 4 additions & 4 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ struct PrintExpressionContents
o << ".atomic";
}
o << ".load";
if (curr->type != unreachable && curr->bytes < getTypeSize(curr->type)) {
if (curr->type != unreachable && curr->bytes < curr->type.getByteSize()) {
if (curr->bytes == 1) {
o << '8';
} else if (curr->bytes == 2) {
Expand Down Expand Up @@ -233,7 +233,7 @@ struct PrintExpressionContents
}
static void printRMWSize(std::ostream& o, Type type, uint8_t bytes) {
prepareColor(o) << forceConcrete(type) << ".atomic.rmw";
if (type != unreachable && bytes != getTypeSize(type)) {
if (type != unreachable && bytes != type.getByteSize()) {
if (bytes == 1) {
o << '8';
} else if (bytes == 2) {
Expand Down Expand Up @@ -269,7 +269,7 @@ struct PrintExpressionContents
o << "xchg";
break;
}
if (curr->type != unreachable && curr->bytes != getTypeSize(curr->type)) {
if (curr->type != unreachable && curr->bytes != curr->type.getByteSize()) {
o << "_u";
}
restoreNormalColor(o);
Expand All @@ -281,7 +281,7 @@ struct PrintExpressionContents
prepareColor(o);
printRMWSize(o, curr->type, curr->bytes);
o << "cmpxchg";
if (curr->type != unreachable && curr->bytes != getTypeSize(curr->type)) {
if (curr->type != unreachable && curr->bytes != curr->type.getByteSize()) {
o << "_u";
}
restoreNormalColor(o);
Expand Down
4 changes: 2 additions & 2 deletions src/passes/SafeHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct SafeHeap : public Pass {
load.type = type;
for (Index bytes : {1, 2, 4, 8, 16}) {
load.bytes = bytes;
if (bytes > getTypeSize(type) || (type == f32 && bytes != 4) ||
if (bytes > type.getByteSize() || (type == f32 && bytes != 4) ||
(type == f64 && bytes != 8) || (type == v128 && bytes != 16)) {
continue;
}
Expand Down Expand Up @@ -212,7 +212,7 @@ struct SafeHeap : public Pass {
store.type = none;
for (Index bytes : {1, 2, 4, 8, 16}) {
store.bytes = bytes;
if (bytes > getTypeSize(valueType) ||
if (bytes > valueType.getByteSize() ||
(valueType == f32 && bytes != 4) ||
(valueType == f64 && bytes != 8) ||
(valueType == v128 && bytes != 16)) {
Expand Down
8 changes: 4 additions & 4 deletions src/passes/SpillPointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct SpillPointers
PointerMap pointerMap;
for (Index i = 0; i < func->getNumLocals(); i++) {
if (func->getLocalType(i) == ABI::PointerType) {
auto offset = pointerMap.size() * getTypeSize(ABI::PointerType);
auto offset = pointerMap.size() * ABI::PointerType.getByteSize();
pointerMap[i] = offset;
}
}
Expand Down Expand Up @@ -140,7 +140,7 @@ struct SpillPointers
// get the stack space, and set the local to it
ABI::getStackSpace(spillLocal,
func,
getTypeSize(ABI::PointerType) * pointerMap.size(),
ABI::PointerType.getByteSize() * pointerMap.size(),
*getModule());
}
}
Expand Down Expand Up @@ -184,9 +184,9 @@ struct SpillPointers
// add the spills
for (auto index : toSpill) {
block->list.push_back(
builder.makeStore(getTypeSize(ABI::PointerType),
builder.makeStore(ABI::PointerType.getByteSize(),
pointerMap[index],
getTypeSize(ABI::PointerType),
ABI::PointerType.getByteSize(),
builder.makeLocalGet(spillLocal, ABI::PointerType),
builder.makeLocalGet(index, ABI::PointerType),
ABI::PointerType));
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,7 @@ template<typename GlobalManager, typename SubType> class ModuleInstanceBase {
if (timeout.breaking()) {
return timeout;
}
auto bytes = getTypeSize(curr->expectedType);
auto bytes = curr->expectedType.getByteSize();
auto addr = instance.getFinalAddress(ptr.value, bytes);
auto loaded = instance.doAtomicLoad(addr, bytes, curr->expectedType);
NOTE_EVAL1(loaded);
Expand Down
20 changes: 15 additions & 5 deletions src/wasm-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ class Type {

// Allows for using Types in switch statements
constexpr operator uint32_t() const { return id; }

// Returns the type size in bytes. Only single types are supported.
unsigned getByteSize() const;

// Reinterpret an integer type to a float type with the same size and vice
// versa. Only single integer and float types are supported.
Type reinterpret() const;

// Returns the feature set required to use this type.
FeatureSet getFeatures() const;

// Returns a number type based on its size in bytes and whether it is a float
// type.
static Type get(unsigned byteSize, bool float_);

std::string toString() const;
};

Expand Down Expand Up @@ -123,11 +138,6 @@ constexpr Type anyref = Type::anyref;
constexpr Type exnref = Type::exnref;
constexpr Type unreachable = Type::unreachable;

unsigned getTypeSize(Type type);
FeatureSet getFeatures(Type type);
Type getType(unsigned size, bool float_);
Type reinterpretType(Type type);

} // namespace wasm

template<> class std::hash<wasm::Signature> {
Expand Down
4 changes: 2 additions & 2 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2974,7 +2974,7 @@ bool WasmBinaryBuilder::maybeVisitAtomicWait(Expression*& out, uint8_t code) {
curr->ptr = popNonVoidExpression();
Address readAlign;
readMemoryAccess(readAlign, curr->offset);
if (readAlign != getTypeSize(curr->expectedType)) {
if (readAlign != curr->expectedType.getByteSize()) {
throwError("Align of AtomicWait must match size");
}
curr->finalize();
Expand All @@ -2994,7 +2994,7 @@ bool WasmBinaryBuilder::maybeVisitAtomicNotify(Expression*& out, uint8_t code) {
curr->ptr = popNonVoidExpression();
Address readAlign;
readMemoryAccess(readAlign, curr->offset);
if (readAlign != getTypeSize(curr->type)) {
if (readAlign != curr->type.getByteSize()) {
throwError("Align of AtomicNotify must match size");
}
curr->finalize();
Expand Down
6 changes: 3 additions & 3 deletions src/wasm/wasm-s-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ SExpressionWasmBuilder::makeLoad(Element& s, Type type, bool isAtomic) {
auto* ret = allocator.alloc<Load>();
ret->isAtomic = isAtomic;
ret->type = type;
ret->bytes = parseMemBytes(extra, getTypeSize(type));
ret->bytes = parseMemBytes(extra, type.getByteSize());
ret->signed_ = extra[0] && extra[1] == 's';
size_t i = parseMemAttributes(s, &ret->offset, &ret->align, ret->bytes);
ret->ptr = parseExpression(s[i]);
Expand All @@ -1282,7 +1282,7 @@ SExpressionWasmBuilder::makeStore(Element& s, Type type, bool isAtomic) {
auto ret = allocator.alloc<Store>();
ret->isAtomic = isAtomic;
ret->valueType = type;
ret->bytes = parseMemBytes(extra, getTypeSize(type));
ret->bytes = parseMemBytes(extra, type.getByteSize());
size_t i = parseMemAttributes(s, &ret->offset, &ret->align, ret->bytes);
ret->ptr = parseExpression(s[i]);
ret->value = parseExpression(s[i + 1]);
Expand All @@ -1294,7 +1294,7 @@ Expression* SExpressionWasmBuilder::makeAtomicRMWOrCmpxchg(Element& s,
Type type) {
const char* extra = findMemExtra(
*s[0], 11 /* after "type.atomic.rmw" */, /* isAtomic = */ false);
auto bytes = parseMemBytes(extra, getTypeSize(type));
auto bytes = parseMemBytes(extra, type.getByteSize());
extra = strchr(extra, '.'); // after the optional '_u' and before the opcode
if (!extra) {
throw ParseException("malformed atomic rmw instruction");
Expand Down
Loading