Skip to content

Replace Type::expand() with an iterator-based approach #3061

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

Merged
merged 8 commits into from
Aug 19, 2020
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
4 changes: 2 additions & 2 deletions src/asmjs/asm_v_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ std::string getSig(Type results, Type params) {
assert(!results.isMulti());
std::string sig;
sig += getSig(results);
for (Type t : params.expand()) {
sig += getSig(t);
for (auto& param : params) {
sig += getSig(param);
}
return sig;
}
Expand Down
7 changes: 4 additions & 3 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ BinaryenType BinaryenTypeCreate(BinaryenType* types, uint32_t numTypes) {
uint32_t BinaryenTypeArity(BinaryenType t) { return Type(t).size(); }

void BinaryenTypeExpand(BinaryenType t, BinaryenType* buf) {
const std::vector<Type>& types = Type(t).expand();
for (size_t i = 0; i < types.size(); ++i) {
buf[i] = types[i].getID();
Type types(t);
size_t i = 0;
for (auto& type : types) {
buf[i++] = type.getID();
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/passes/Asyncify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1304,10 +1304,9 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
if (!relevantLiveLocals.count(i)) {
continue;
}
const auto& types = func->getLocalType(i).expand();
auto localType = func->getLocalType(i);
SmallVector<Expression*, 1> loads;
for (Index j = 0; j < types.size(); j++) {
auto type = types[j];
for (auto& type : localType) {
auto size = type.getByteSize();
assert(size % STACK_ALIGN == 0);
// TODO: higher alignment?
Expand All @@ -1323,7 +1322,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
Expression* load;
if (loads.size() == 1) {
load = loads[0];
} else if (types.size() > 1) {
} else if (localType.size() > 1) {
load = builder->makeTupleMake(std::move(loads));
} else {
WASM_UNREACHABLE("Unexpected empty type");
Expand All @@ -1350,12 +1349,11 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
continue;
}
auto localType = func->getLocalType(i);
const auto& types = localType.expand();
for (Index j = 0; j < types.size(); j++) {
auto type = types[j];
size_t j = 0;
for (auto& type : localType) {
auto size = type.getByteSize();
Expression* localGet = builder->makeLocalGet(i, localType);
if (types.size() > 1) {
if (localType.size() > 1) {
localGet = builder->makeTupleExtract(localGet, j);
}
assert(size % STACK_ALIGN == 0);
Expand All @@ -1368,6 +1366,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
localGet,
type));
offset += size;
++j;
}
}
block->list.push_back(builder->makeIncStackPos(offset));
Expand Down
2 changes: 1 addition & 1 deletion src/passes/DeadArgumentElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ struct DAE : public Pass {
// Remove the parameter from the function. We must add a new local
// for uses of the parameter, but cannot make it use the same index
// (in general).
std::vector<Type> params = func->sig.params.expand();
std::vector<Type> params(func->sig.params.begin(), func->sig.params.end());
auto type = params[i];
params.erase(params.begin() + i);
func->sig.params = Type(params);
Expand Down
6 changes: 3 additions & 3 deletions src/passes/FuncCastEmulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ struct FuncCastEmulation : public Pass {
}
// The item in the table may be a function or a function import.
auto* func = module->getFunction(name);
const std::vector<Type>& params = func->sig.params.expand();
Type type = func->sig.results;
Builder builder(*module);
std::vector<Expression*> callOperands;
for (Index i = 0; i < params.size(); i++) {
Index i = 0;
for (auto& param : func->sig.params) {
callOperands.push_back(
fromABI(builder.makeLocalGet(i, Type::i64), params[i], module));
fromABI(builder.makeLocalGet(i++, Type::i64), param, module));
}
auto* call = builder.makeCall(name, callOperands, type);
std::vector<Type> thunkParams;
Expand Down
2 changes: 1 addition & 1 deletion src/passes/I64ToI32Lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ struct I64ToI32Lowering : public WalkerPass<PostWalker<I64ToI32Lowering>> {
visitGenericCall<CallIndirect>(
curr, [&](std::vector<Expression*>& args, Type results) {
std::vector<Type> params;
for (auto param : curr->sig.params.expand()) {
for (auto& param : curr->sig.params) {
if (param == Type::i64) {
params.push_back(Type::i32);
params.push_back(Type::i32);
Expand Down
16 changes: 8 additions & 8 deletions src/passes/LegalizeJSInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ struct LegalizeJSInterface : public Pass {
std::map<Name, Name> illegalImportsToLegal;

template<typename T> bool isIllegal(T* t) {
for (auto param : t->sig.params.expand()) {
for (auto& param : t->sig.params) {
if (param == Type::i64) {
return true;
}
Expand Down Expand Up @@ -222,9 +222,8 @@ struct LegalizeJSInterface : public Pass {
call->target = func->name;
call->type = func->sig.results;

const std::vector<Type>& params = func->sig.params.expand();
std::vector<Type> legalParams;
for (auto param : params) {
for (auto& param : func->sig.params) {
if (param == Type::i64) {
call->operands.push_back(I64Utilities::recreateI64(
builder, legalParams.size(), legalParams.size() + 1));
Expand Down Expand Up @@ -277,18 +276,19 @@ struct LegalizeJSInterface : public Pass {
auto* call = module->allocator.alloc<Call>();
call->target = legalIm->name;

const std::vector<Type>& imParams = im->sig.params.expand();
std::vector<Type> params;
for (size_t i = 0; i < imParams.size(); ++i) {
if (imParams[i] == Type::i64) {
Index i = 0;
for (auto& param : im->sig.params) {
if (param == Type::i64) {
call->operands.push_back(I64Utilities::getI64Low(builder, i));
call->operands.push_back(I64Utilities::getI64High(builder, i));
params.push_back(Type::i32);
params.push_back(Type::i32);
} else {
call->operands.push_back(builder.makeLocalGet(i, imParams[i]));
params.push_back(imParams[i]);
call->operands.push_back(builder.makeLocalGet(i, param));
params.push_back(param);
}
++i;
}

if (im->sig.results == Type::i64) {
Expand Down
32 changes: 16 additions & 16 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ struct SExprType {
static std::ostream& operator<<(std::ostream& o, const SExprType& localType) {
Type type = localType.type;
if (type.isMulti()) {
const std::vector<Type>& types = type.expand();
o << '(' << types[0];
for (size_t i = 1; i < types.size(); ++i) {
o << ' ' << types[i];
o << '(';
auto sep = "";
for (auto& t : type) {
o << sep << t;
sep = " ";
}
o << ')';
return o;
} else {
o << type;
}
o << type;
return o;
}

Expand All @@ -90,12 +91,10 @@ std::ostream& operator<<(std::ostream& os, SigName sigName) {
if (type == Type::none) {
os << "none";
} else {
const std::vector<Type>& types = type.expand();
for (size_t i = 0; i < types.size(); ++i) {
if (i != 0) {
os << '_';
}
os << types[i];
auto sep = "";
for (auto& t : type) {
os << sep << t;
sep = "_";
}
}
};
Expand Down Expand Up @@ -2169,14 +2168,15 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> {
if (!printStackIR && curr->stackIR && !minify) {
o << " (; has Stack IR ;)";
}
const std::vector<Type>& params = curr->sig.params.expand();
if (params.size() > 0) {
for (size_t i = 0; i < params.size(); i++) {
if (curr->sig.params.size() > 0) {
Index i = 0;
for (auto& param : curr->sig.params) {
o << maybeSpace;
o << '(';
printMinor(o, "param ");
printLocal(i, currFunction, o);
o << ' ' << params[i] << ')';
o << ' ' << param << ')';
++i;
}
}
if (curr->sig.results != Type::none) {
Expand Down
8 changes: 4 additions & 4 deletions src/shell-interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ struct ShellExternalInterface : ModuleInstance::ExternalInterface {
if (sig != func->sig) {
trap("callIndirect: function signatures don't match");
}
const std::vector<Type>& params = func->sig.params.expand();
if (params.size() != arguments.size()) {
if (func->sig.params.size() != arguments.size()) {
trap("callIndirect: bad # of arguments");
}
for (size_t i = 0; i < params.size(); i++) {
if (!Type::isSubType(arguments[i].type, params[i])) {
size_t i = 0;
for (auto& param : func->sig.params) {
if (!Type::isSubType(arguments[i++].type, param)) {
trap("callIndirect: bad argument type");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/execution-results.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ struct ExecutionResults {
instance.callFunction(ex->value, arguments);
}
// call the method
for (Type param : func->sig.params.expand()) {
for (auto& param : func->sig.params) {
// zeros in arguments TODO: more?
arguments.push_back(Literal::makeSingleZero(param));
}
Expand Down
23 changes: 12 additions & 11 deletions src/tools/fuzzing.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class TranslateToFuzzReader {
Type getSubType(Type type) {
if (type.isMulti()) {
std::vector<Type> types;
for (auto t : type.expand()) {
for (auto& t : type) {
types.push_back(getSubType(t));
}
return Type(types);
Expand Down Expand Up @@ -770,7 +770,7 @@ class TranslateToFuzzReader {
std::vector<Expression*> invocations;
while (oneIn(2) && !finishedInput) {
std::vector<Expression*> args;
for (auto type : func->sig.params.expand()) {
for (auto& type : func->sig.params) {
args.push_back(makeConst(type));
}
Expression* invoke =
Expand Down Expand Up @@ -1166,7 +1166,7 @@ class TranslateToFuzzReader {
}
// we found one!
std::vector<Expression*> args;
for (auto argType : target->sig.params.expand()) {
for (auto& argType : target->sig.params) {
args.push_back(make(argType));
}
return builder.makeCall(target->name, args, type, isReturn);
Expand Down Expand Up @@ -1210,7 +1210,7 @@ class TranslateToFuzzReader {
target = make(Type::i32);
}
std::vector<Expression*> args;
for (auto type : targetFn->sig.params.expand()) {
for (auto& type : targetFn->sig.params) {
args.push_back(make(type));
}
return builder.makeCallIndirect(target, args, targetFn->sig, isReturn);
Expand Down Expand Up @@ -1267,7 +1267,7 @@ class TranslateToFuzzReader {
assert(wasm.features.hasMultivalue());
assert(type.isMulti());
std::vector<Expression*> elements;
for (auto t : type.expand()) {
for (auto& t : type) {
elements.push_back(make(t));
}
return builder.makeTupleMake(std::move(elements));
Expand All @@ -1277,20 +1277,21 @@ class TranslateToFuzzReader {
assert(wasm.features.hasMultivalue());
assert(type.isSingle() && type.isConcrete());
Type tupleType = getTupleType();
auto& elements = tupleType.expand();

// Find indices from which we can extract `type`
std::vector<size_t> extractIndices;
for (size_t i = 0; i < elements.size(); ++i) {
if (elements[i] == type) {
size_t i = 0;
for (auto& t : tupleType) {
if (t == type) {
extractIndices.push_back(i);
}
++i;
}

// If there are none, inject one
if (extractIndices.size() == 0) {
auto newElements = elements;
size_t injected = upTo(elements.size());
std::vector<Type> newElements(tupleType.begin(), tupleType.end());
size_t injected = upTo(newElements.size());
newElements[injected] = type;
tupleType = Type(newElements);
extractIndices.push_back(injected);
Expand Down Expand Up @@ -1766,7 +1767,7 @@ class TranslateToFuzzReader {
}
if (type.isMulti()) {
std::vector<Expression*> operands;
for (auto t : type.expand()) {
for (auto& t : type) {
operands.push_back(makeConst(t));
}
return builder.makeTupleMake(std::move(operands));
Expand Down
2 changes: 1 addition & 1 deletion src/tools/js-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static std::string generateJSWrapper(Module& wasm) {
}
ret += std::string("instance.exports.") + exp->name.str + "(";
bool first = true;
for (Type param : func->sig.params.expand()) {
for (auto& param : func->sig.params) {
// zeros in arguments TODO more?
if (first) {
first = false;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/spec-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static std::string generateSpecWrapper(Module& wasm) {
}
ret += std::string("(invoke \"hangLimitInitializer\") (invoke \"") +
exp->name.str + "\" ";
for (Type param : func->sig.params.expand()) {
for (auto& param : func->sig.params) {
// zeros in arguments TODO more?
TODO_SINGLE_COMPOUND(param);
switch (param.getBasic()) {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/wasm-shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static void run_asserts(Name moduleName,
std::cerr << "Unknown entry " << entry << std::endl;
} else {
LiteralList arguments;
for (Type param : function->sig.params.expand()) {
for (auto& param : function->sig.params) {
arguments.push_back(Literal(param));
}
try {
Expand Down
11 changes: 5 additions & 6 deletions src/tools/wasm2c-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ int main(int argc, char** argv) {

// Emit the callee's name with wasm2c name mangling.
ret += std::string("(*Z_") + exp->name.str + "Z_";
auto params = func->sig.params.expand();

auto wasm2cSignature = [](Type type) {
TODO_SINGLE_COMPOUND(type);
Expand All @@ -165,18 +164,18 @@ int main(int argc, char** argv) {
};

ret += wasm2cSignature(result);
if (params.empty()) {
ret += wasm2cSignature(Type::none);
} else {
for (auto param : params) {
if (func->sig.params.isMulti()) {
for (auto& param : func->sig.params) {
ret += wasm2cSignature(param);
}
} else {
ret += wasm2cSignature(func->sig.params);
}
ret += ")(";

// Emit the parameters (all 0s, like the other wrappers).
bool first = true;
for (auto param : params) {
for (auto& param : func->sig.params) {
WASM_UNUSED(param);
if (!first) {
ret += ", ";
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ class Builder {
// only ok to add a param if no vars, otherwise indices are invalidated
assert(func->localIndices.size() == func->sig.params.size());
assert(name.is());
std::vector<Type> params = func->sig.params.expand();
std::vector<Type> params(func->sig.params.begin(), func->sig.params.end());
params.push_back(type);
func->sig.params = Type(params);
Index index = func->localNames.size();
Expand Down
Loading