Skip to content

Commit 28d6e2b

Browse files
kripkenradekdoulik
authored andcommitted
subtype-exprs.h additions [NFC] (WebAssembly#6323)
This pulls out the subtype-exprs.h parts of WebAssembly#6108 These are NFC in the current codebase, but are fixes for that unlanded PR, and another unrelated PR that will be opened shortly.
1 parent d34e568 commit 28d6e2b

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

src/ir/subtype-exprs.h

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
190190
void visitRefNull(RefNull* curr) {}
191191
void visitRefIsNull(RefIsNull* curr) {}
192192
void visitRefFunc(RefFunc* curr) {}
193-
void visitRefEq(RefEq* curr) {}
193+
void visitRefEq(RefEq* curr) {
194+
self()->noteSubtype(curr->left, Type(HeapType::eq, Nullable));
195+
self()->noteSubtype(curr->right, Type(HeapType::eq, Nullable));
196+
}
194197
void visitTableGet(TableGet* curr) {}
195198
void visitTableSet(TableSet* curr) {
196199
self()->noteSubtype(curr->value,
@@ -225,12 +228,27 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
225228
void visitTupleMake(TupleMake* curr) {}
226229
void visitTupleExtract(TupleExtract* curr) {}
227230
void visitRefI31(RefI31* curr) {}
228-
void visitI31Get(I31Get* curr) {}
231+
void visitI31Get(I31Get* curr) {
232+
self()->noteSubtype(curr->i31, Type(HeapType::i31, Nullable));
233+
}
229234
void visitCallRef(CallRef* curr) {
230-
if (!curr->target->type.isSignature()) {
231-
return;
235+
// Even if we are unreachable, the target must be valid, and in particular
236+
// it cannot be funcref - it must be a proper signature type. We could
237+
// perhaps have |addStrictSubtype| to handle that, but for now just require
238+
// that the target keep its type.
239+
//
240+
// Note that even if we are reachable, there is an interaction between the
241+
// target and the the types of the parameters and results (the target's type
242+
// must support the parameter and result types properly), and so it is not
243+
// obvious how users would want to optimize here (if they are trying to
244+
// generalize, should they generalize the target more or the parameters
245+
// more? etc.), so we do the simple thing here for now of requiring the
246+
// target type not generalize.
247+
self()->noteSubtype(curr->target, curr->target->type);
248+
249+
if (curr->target->type.isSignature()) {
250+
handleCall(curr, curr->target->type.getHeapType().getSignature());
232251
}
233-
handleCall(curr, curr->target->type.getHeapType().getSignature());
234252
}
235253
void visitRefTest(RefTest* curr) {
236254
self()->noteCast(curr->ref, curr->castType);
@@ -286,13 +304,14 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
286304
self()->noteSubtype(value, array.element.type);
287305
}
288306
}
307+
289308
void visitArrayGet(ArrayGet* curr) {}
290309
void visitArraySet(ArraySet* curr) {
291310
if (!curr->ref->type.isArray()) {
292311
return;
293312
}
294313
auto array = curr->ref->type.getHeapType().getArray();
295-
self()->noteSubtype(curr->value->type, array.element.type);
314+
self()->noteSubtype(curr->value, array.element.type);
296315
}
297316
void visitArrayLen(ArrayLen* curr) {}
298317
void visitArrayCopy(ArrayCopy* curr) {
@@ -308,7 +327,7 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
308327
return;
309328
}
310329
auto array = curr->ref->type.getHeapType().getArray();
311-
self()->noteSubtype(curr->value->type, array.element.type);
330+
self()->noteSubtype(curr->value, array.element.type);
312331
}
313332
void visitArrayInitData(ArrayInitData* curr) {}
314333
void visitArrayInitElem(ArrayInitElem* curr) {
@@ -319,7 +338,11 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
319338
auto* seg = self()->getModule()->getElementSegment(curr->segment);
320339
self()->noteSubtype(seg->type, array.element.type);
321340
}
322-
void visitRefAs(RefAs* curr) {}
341+
void visitRefAs(RefAs* curr) {
342+
if (curr->op == RefAsNonNull) {
343+
self()->noteCast(curr->value, curr);
344+
}
345+
}
323346
void visitStringNew(StringNew* curr) {}
324347
void visitStringConst(StringConst* curr) {}
325348
void visitStringMeasure(StringMeasure* curr) {}

0 commit comments

Comments
 (0)