Skip to content

Commit a18d30f

Browse files
authored
Replace Type::expand() with an iterator-based approach (#3061)
This leads to simpler code and is a prerequisite for #3012, which makes it so that not all `Type`s are backed by vectors that `expand` could return.
1 parent b43807a commit a18d30f

26 files changed

+200
-183
lines changed

src/asmjs/asm_v_wasm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ std::string getSig(Type results, Type params) {
104104
assert(!results.isMulti());
105105
std::string sig;
106106
sig += getSig(results);
107-
for (Type t : params.expand()) {
108-
sig += getSig(t);
107+
for (auto& param : params) {
108+
sig += getSig(param);
109109
}
110110
return sig;
111111
}

src/binaryen-c.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@ BinaryenType BinaryenTypeCreate(BinaryenType* types, uint32_t numTypes) {
150150
uint32_t BinaryenTypeArity(BinaryenType t) { return Type(t).size(); }
151151

152152
void BinaryenTypeExpand(BinaryenType t, BinaryenType* buf) {
153-
const std::vector<Type>& types = Type(t).expand();
154-
for (size_t i = 0; i < types.size(); ++i) {
155-
buf[i] = types[i].getID();
153+
Type types(t);
154+
size_t i = 0;
155+
for (auto& type : types) {
156+
buf[i++] = type.getID();
156157
}
157158
}
158159

src/passes/Asyncify.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,10 +1304,9 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
13041304
if (!relevantLiveLocals.count(i)) {
13051305
continue;
13061306
}
1307-
const auto& types = func->getLocalType(i).expand();
1307+
auto localType = func->getLocalType(i);
13081308
SmallVector<Expression*, 1> loads;
1309-
for (Index j = 0; j < types.size(); j++) {
1310-
auto type = types[j];
1309+
for (auto& type : localType) {
13111310
auto size = type.getByteSize();
13121311
assert(size % STACK_ALIGN == 0);
13131312
// TODO: higher alignment?
@@ -1323,7 +1322,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
13231322
Expression* load;
13241323
if (loads.size() == 1) {
13251324
load = loads[0];
1326-
} else if (types.size() > 1) {
1325+
} else if (localType.size() > 1) {
13271326
load = builder->makeTupleMake(std::move(loads));
13281327
} else {
13291328
WASM_UNREACHABLE("Unexpected empty type");
@@ -1350,12 +1349,11 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
13501349
continue;
13511350
}
13521351
auto localType = func->getLocalType(i);
1353-
const auto& types = localType.expand();
1354-
for (Index j = 0; j < types.size(); j++) {
1355-
auto type = types[j];
1352+
size_t j = 0;
1353+
for (auto& type : localType) {
13561354
auto size = type.getByteSize();
13571355
Expression* localGet = builder->makeLocalGet(i, localType);
1358-
if (types.size() > 1) {
1356+
if (localType.size() > 1) {
13591357
localGet = builder->makeTupleExtract(localGet, j);
13601358
}
13611359
assert(size % STACK_ALIGN == 0);
@@ -1368,6 +1366,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
13681366
localGet,
13691367
type));
13701368
offset += size;
1369+
++j;
13711370
}
13721371
}
13731372
block->list.push_back(builder->makeIncStackPos(offset));

src/passes/DeadArgumentElimination.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ struct DAE : public Pass {
409409
// Remove the parameter from the function. We must add a new local
410410
// for uses of the parameter, but cannot make it use the same index
411411
// (in general).
412-
std::vector<Type> params = func->sig.params.expand();
412+
std::vector<Type> params(func->sig.params.begin(), func->sig.params.end());
413413
auto type = params[i];
414414
params.erase(params.begin() + i);
415415
func->sig.params = Type(params);

src/passes/FuncCastEmulation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,13 @@ struct FuncCastEmulation : public Pass {
193193
}
194194
// The item in the table may be a function or a function import.
195195
auto* func = module->getFunction(name);
196-
const std::vector<Type>& params = func->sig.params.expand();
197196
Type type = func->sig.results;
198197
Builder builder(*module);
199198
std::vector<Expression*> callOperands;
200-
for (Index i = 0; i < params.size(); i++) {
199+
Index i = 0;
200+
for (auto& param : func->sig.params) {
201201
callOperands.push_back(
202-
fromABI(builder.makeLocalGet(i, Type::i64), params[i], module));
202+
fromABI(builder.makeLocalGet(i++, Type::i64), param, module));
203203
}
204204
auto* call = builder.makeCall(name, callOperands, type);
205205
std::vector<Type> thunkParams;

src/passes/I64ToI32Lowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ struct I64ToI32Lowering : public WalkerPass<PostWalker<I64ToI32Lowering>> {
272272
visitGenericCall<CallIndirect>(
273273
curr, [&](std::vector<Expression*>& args, Type results) {
274274
std::vector<Type> params;
275-
for (auto param : curr->sig.params.expand()) {
275+
for (auto& param : curr->sig.params) {
276276
if (param == Type::i64) {
277277
params.push_back(Type::i32);
278278
params.push_back(Type::i32);

src/passes/LegalizeJSInterface.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ struct LegalizeJSInterface : public Pass {
183183
std::map<Name, Name> illegalImportsToLegal;
184184

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

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

280-
const std::vector<Type>& imParams = im->sig.params.expand();
281279
std::vector<Type> params;
282-
for (size_t i = 0; i < imParams.size(); ++i) {
283-
if (imParams[i] == Type::i64) {
280+
Index i = 0;
281+
for (auto& param : im->sig.params) {
282+
if (param == Type::i64) {
284283
call->operands.push_back(I64Utilities::getI64Low(builder, i));
285284
call->operands.push_back(I64Utilities::getI64High(builder, i));
286285
params.push_back(Type::i32);
287286
params.push_back(Type::i32);
288287
} else {
289-
call->operands.push_back(builder.makeLocalGet(i, imParams[i]));
290-
params.push_back(imParams[i]);
288+
call->operands.push_back(builder.makeLocalGet(i, param));
289+
params.push_back(param);
291290
}
291+
++i;
292292
}
293293

294294
if (im->sig.results == Type::i64) {

src/passes/Print.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,16 @@ struct SExprType {
6767
static std::ostream& operator<<(std::ostream& o, const SExprType& localType) {
6868
Type type = localType.type;
6969
if (type.isMulti()) {
70-
const std::vector<Type>& types = type.expand();
71-
o << '(' << types[0];
72-
for (size_t i = 1; i < types.size(); ++i) {
73-
o << ' ' << types[i];
70+
o << '(';
71+
auto sep = "";
72+
for (auto& t : type) {
73+
o << sep << t;
74+
sep = " ";
7475
}
7576
o << ')';
76-
return o;
77+
} else {
78+
o << type;
7779
}
78-
o << type;
7980
return o;
8081
}
8182

@@ -90,12 +91,10 @@ std::ostream& operator<<(std::ostream& os, SigName sigName) {
9091
if (type == Type::none) {
9192
os << "none";
9293
} else {
93-
const std::vector<Type>& types = type.expand();
94-
for (size_t i = 0; i < types.size(); ++i) {
95-
if (i != 0) {
96-
os << '_';
97-
}
98-
os << types[i];
94+
auto sep = "";
95+
for (auto& t : type) {
96+
os << sep << t;
97+
sep = "_";
9998
}
10099
}
101100
};
@@ -2169,14 +2168,15 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> {
21692168
if (!printStackIR && curr->stackIR && !minify) {
21702169
o << " (; has Stack IR ;)";
21712170
}
2172-
const std::vector<Type>& params = curr->sig.params.expand();
2173-
if (params.size() > 0) {
2174-
for (size_t i = 0; i < params.size(); i++) {
2171+
if (curr->sig.params.size() > 0) {
2172+
Index i = 0;
2173+
for (auto& param : curr->sig.params) {
21752174
o << maybeSpace;
21762175
o << '(';
21772176
printMinor(o, "param ");
21782177
printLocal(i, currFunction, o);
2179-
o << ' ' << params[i] << ')';
2178+
o << ' ' << param << ')';
2179+
++i;
21802180
}
21812181
}
21822182
if (curr->sig.results != Type::none) {

src/shell-interface.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ struct ShellExternalInterface : ModuleInstance::ExternalInterface {
165165
if (sig != func->sig) {
166166
trap("callIndirect: function signatures don't match");
167167
}
168-
const std::vector<Type>& params = func->sig.params.expand();
169-
if (params.size() != arguments.size()) {
168+
if (func->sig.params.size() != arguments.size()) {
170169
trap("callIndirect: bad # of arguments");
171170
}
172-
for (size_t i = 0; i < params.size(); i++) {
173-
if (!Type::isSubType(arguments[i].type, params[i])) {
171+
size_t i = 0;
172+
for (auto& param : func->sig.params) {
173+
if (!Type::isSubType(arguments[i++].type, param)) {
174174
trap("callIndirect: bad argument type");
175175
}
176176
}

src/tools/execution-results.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct ExecutionResults {
144144
instance.callFunction(ex->value, arguments);
145145
}
146146
// call the method
147-
for (Type param : func->sig.params.expand()) {
147+
for (auto& param : func->sig.params) {
148148
// zeros in arguments TODO: more?
149149
arguments.push_back(Literal::makeSingleZero(param));
150150
}

0 commit comments

Comments
 (0)