forked from WebAssembly/binaryen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnsubtyping.cpp
More file actions
1070 lines (967 loc) · 37.2 KB
/
Unsubtyping.cpp
File metadata and controls
1070 lines (967 loc) · 37.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2023 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define UNSUBTYPING_DEBUG 0
#include <cstddef>
#include <iterator>
#include <memory>
#if !UNSUBTYPING_DEBUG
#include <unordered_map>
#include <unordered_set>
#endif
#include "ir/effects.h"
#include "ir/localize.h"
#include "ir/module-utils.h"
#include "ir/names.h"
#include "ir/struct-utils.h"
#include "ir/subtype-exprs.h"
#include "ir/type-updating.h"
#include "ir/utils.h"
#include "pass.h"
#include "support/index.h"
#include "wasm-traversal.h"
#include "wasm-type.h"
#include "wasm.h"
#if UNSUBTYPING_DEBUG
#include "support/insert_ordered.h"
#endif
#if UNSUBTYPING_DEBUG
#define DBG(x) x
#else
#define DBG(x)
#endif
// Compute and use the minimal subtype (and descriptor) relations required to
// maintain module validity and behavior. This minimal relation will be a subset
// of the original subtype (and descriptor) relations. Start by walking the IR
// and collecting pairs of types that need to be in the subtype relation for
// each expression to validate (or require a type to have a descriptor). For
// example, a local.set requires that the type of its operand be a subtype of
// the local's type. Casts do not generate subtypings at this point because it
// is not necessary for the cast target to be a subtype of the cast source for
// the cast to validate.
//
// From that initial subtype relation, we then start finding new subtypings (and
// descriptors) that are required by the subtypings we have found already. These
// transitively required subtypings (and descriptors) come from three sources.
//
// The first source is type definitions. Consider these type definitions:
//
// (type $A (sub (struct (ref $X))))
// (type $B (sub $A (struct (ref $Y))))
//
// If we have determined that $B must remain a subtype of $A, then we know that
// $Y must remain a subtype of $X as well, since the type definitions would not
// be valid otherwise. Similarly, knowing that $X must remain a subtype of $Y
// may transitively require other subtypings as well based on their type
// definitions.
//
// The second source of transitive subtyping requirements is casts. Although
// casting from one type to another does not necessarily require that those
// types are related, we do need to make sure that we do not change the
// behavior of casts by removing subtype relationships they might observe. For
// example, consider this module:
//
// (module
// ;; original subtyping: $bot <: $mid <: $top
// (type $top (sub (struct)))
// (type $mid (sub $top (struct)))
// (type $bot (sub $mid (struct)))
//
// (func $f
// (local $top (ref $top))
// (local $mid (ref $mid))
//
// ;; Requires $bot <: $top
// (local.set $top (struct.new $bot))
//
// ;; Cast $top to $mid
// (local.set $mid (ref.cast (ref $mid) (local.get $top)))
// )
// )
//
// The only subtype relation directly required by the IR for this module is $bot
// <: $top. However, if we optimized the module so that $bot <: $top was the
// only subtype relation, we would change the behavior of the cast. In the
// original module, a value of type (ref $bot) is cast to (ref $mid). The cast
// succeeds because in the original module, $bot <: $mid. If we optimize so that
// we have $bot <: $top and no other subtypings, though, the cast will fail
// because the value of type (ref $bot) no longer inhabits (ref $mid). To
// prevent the cast's behavior from changing, we need to ensure that $bot <:
// $mid.
//
// The set of subtyping requirements generated by a cast from $src to $dest is
// that for every known remaining subtype $v of $src, if $v <: $dest in the
// original module, then $v <: $dest in the optimized module. In other words,
// for every type $v of values we know can flow into the cast, if the cast would
// have succeeded for values of type $v before, then we know the cast must
// continue to succeed for values of type $v. These requirements arising from
// casts can also generate transitive requirements because we learn about new
// types of values that can flow into casts as we learn about new subtypes of
// cast sources.
//
// The third source of transitive subtyping requirements is the discovery of
// required descriptors (and vice versa). Subtyping and descriptors combine to
// form this diagram, where rightward arrows mean "described by":
//
// A -> A.desc
// ^ ^
// | |
// B -> B.desc
//
// If any three of these types exist in these relations with the others, then
// the validation rules require that the fourth type also exist and be in these
// relations. The only exception is that A.desc is allowed to be missing. This
// complex and recursive relationship between subtyping and descriptor relations
// is why we optimize out unneeded descriptors in this pass rather than e.g.
// GlobalTypeOptimization.
//
// Starting with the initial subtype and descriptor relations determined by
// walking the IR, repeatedly search for new subtypings and descriptors by
// analyzing type definitions and casts until we reach a fixed point. This is
// the minimal subtype/descriptor relation that preserves module validity and
// behavior that can be found without a more precise analysis of types that
// might flow into each cast.
namespace wasm {
namespace {
#if UNSUBTYPING_DEBUG
template<typename K, typename V> using Map = InsertOrderedMap<K, V>;
template<typename T> using Set = InsertOrderedSet<T>;
#else
template<typename K, typename V> using Map = std::unordered_map<K, V>;
template<typename T> using Set = std::unordered_set<T>;
#endif
// A tree (or rather a forest) of types with the ability to query and set
// supertypes in constant time and efficiently iterate over supertypes and
// subtypes.
struct TypeTree {
struct Node {
// The type represented by this node.
HeapType type;
// The index of the parent (supertype) in the list of nodes. Set to the
// index of this node if there is no parent.
Index parent;
// The index of this node in the parent's list of children, if any, enabling
// O(1) updates.
Index indexInParent = 0;
// The indices of the children (subtypes) in the list of nodes.
std::vector<Index> children;
// The index of the described and descriptor types, if they are necessary.
std::optional<Index> described;
std::optional<Index> descriptor;
Node(HeapType type, Index index) : type(type), parent(index) {}
};
std::vector<Node> nodes;
Map<HeapType, Index> indices;
void setSupertype(HeapType sub, HeapType super) {
auto childIndex = getIndex(sub);
auto parentIndex = getIndex(super);
auto& childNode = nodes[childIndex];
auto& parentNode = nodes[parentIndex];
// Remove sub from its old supertype if necessary.
if (auto oldParentIndex = childNode.parent; oldParentIndex != childIndex) {
auto& oldParentNode = nodes[oldParentIndex];
// Move sub to the back of its parent's children and then pop it.
auto& children = oldParentNode.children;
assert(children[childNode.indexInParent] == childIndex);
auto& swappedNode = nodes[children.back()];
assert(swappedNode.indexInParent == children.size() - 1);
// Swap the indices in the parent's child vector.
std::swap(children[childNode.indexInParent], children.back());
// Swap the index in the kept child.
swappedNode.indexInParent = childNode.indexInParent;
children.pop_back();
}
childNode.parent = parentIndex;
childNode.indexInParent = parentNode.children.size();
parentNode.children.push_back(childIndex);
}
std::optional<HeapType> getSupertype(HeapType type) const {
auto index = maybeGetIndex(type);
if (!index) {
return std::nullopt;
}
auto parentIndex = nodes[*index].parent;
if (parentIndex == *index) {
return std::nullopt;
}
return nodes[parentIndex].type;
}
void setDescriptor(HeapType described, HeapType descriptor) {
auto describedIndex = getIndex(described);
auto descriptorIndex = getIndex(descriptor);
auto& describedNode = nodes[describedIndex];
auto& descriptorNode = nodes[descriptorIndex];
// We only ever set the descriptor once.
assert(!describedNode.descriptor);
assert(!descriptorNode.described);
describedNode.descriptor = descriptorIndex;
descriptorNode.described = describedIndex;
}
std::optional<HeapType> getDescriptor(HeapType type) const {
auto index = maybeGetIndex(type);
if (!index) {
return std::nullopt;
}
if (auto descIndex = nodes[*index].descriptor) {
return nodes[*descIndex].type;
}
return std::nullopt;
}
std::optional<HeapType> getDescribed(HeapType type) const {
auto index = maybeGetIndex(type);
if (!index) {
return std::nullopt;
}
if (auto descIndex = nodes[*index].described) {
return nodes[*descIndex].type;
}
return std::nullopt;
}
struct SupertypeIterator {
using value_type = const HeapType;
using difference_type = std::ptrdiff_t;
using reference = const HeapType&;
using pointer = const HeapType*;
using iterator_category = std::input_iterator_tag;
TypeTree* parent;
std::optional<Index> index;
bool operator==(const SupertypeIterator& other) const {
return index == other.index;
}
bool operator!=(const SupertypeIterator& other) const {
return !(*this == other);
}
const HeapType& operator*() const { return parent->nodes[*index].type; }
const HeapType* operator->() const { return &*(*this); }
SupertypeIterator& operator++() {
auto parentIndex = parent->nodes[*index].parent;
if (parentIndex == *index) {
index = std::nullopt;
} else {
index = parentIndex;
}
return *this;
}
SupertypeIterator operator++(int) {
auto it = *this;
++(*this);
return it;
}
};
struct Supertypes {
TypeTree* parent;
Index index;
SupertypeIterator begin() { return {parent, index}; }
SupertypeIterator end() { return {parent, std::nullopt}; }
};
Supertypes supertypes(HeapType type) { return {this, getIndex(type)}; }
struct ImmediateSubtypeIterator {
using value_type = const HeapType;
using difference_type = std::ptrdiff_t;
using reference = const HeapType&;
using pointer = const HeapType*;
using iterator_category = std::input_iterator_tag;
TypeTree* parent;
std::vector<Index>::const_iterator child;
bool operator==(const ImmediateSubtypeIterator& other) const {
return child == other.child;
}
bool operator!=(const ImmediateSubtypeIterator& other) const {
return !(*this == other);
}
const HeapType& operator*() const { return parent->nodes[*child].type; }
const HeapType* operator->() const { return &*(*this); }
ImmediateSubtypeIterator& operator++() {
++child;
return *this;
}
ImmediateSubtypeIterator operator++(int) {
auto it = *this;
++(*this);
return it;
}
};
struct ImmediateSubtypes {
TypeTree* parent;
Index index;
ImmediateSubtypeIterator begin() {
return {parent, parent->nodes[index].children.begin()};
}
ImmediateSubtypeIterator end() {
return {parent, parent->nodes[index].children.end()};
}
};
ImmediateSubtypes immediateSubtypes(HeapType type) {
return {this, getIndex(type)};
}
struct SubtypeIterator {
using value_type = const HeapType;
using difference_type = std::ptrdiff_t;
using reference = const HeapType&;
using pointer = const HeapType*;
using iterator_category = std::input_iterator_tag;
TypeTree* parent;
// DFS stack of (node index, child index) pairs.
std::vector<std::pair<Index, Index>> stack;
bool operator==(const SubtypeIterator& other) {
return stack == other.stack;
}
bool operator!=(const SubtypeIterator& other) { return !(*this == other); }
const HeapType& operator*() const {
return parent->nodes[stack.back().first].type;
}
const HeapType* operator->() const { return &*(*this); }
SubtypeIterator& operator++() {
while (true) {
if (stack.empty()) {
return *this;
}
auto& [index, childIndex] = stack.back();
auto& children = parent->nodes[index].children;
if (childIndex == children.size()) {
stack.pop_back();
} else {
auto child = children[childIndex++];
stack.push_back({child, 0u});
return *this;
}
}
}
SubtypeIterator operator++(int) {
auto it = *this;
++(*this);
return it;
}
};
struct Subtypes {
TypeTree* parent;
Index index;
SubtypeIterator begin() { return {parent, {std::make_pair(index, 0u)}}; }
SubtypeIterator end() { return {parent, {}}; }
};
Subtypes subtypes(HeapType type) { return {this, getIndex(type)}; }
#if UNSUBTYPING_DEBUG
void dump(Module& wasm) {
for (auto& node : nodes) {
std::cerr << ModuleHeapType(wasm, node.type);
if (auto super = getSupertype(node.type)) {
std::cerr << " <: " << ModuleHeapType(wasm, *super);
}
if (auto desc = getDescribed(node.type)) {
std::cerr << ", describes " << ModuleHeapType(wasm, *desc);
}
if (auto desc = getDescriptor(node.type)) {
std::cerr << ", descriptor " << ModuleHeapType(wasm, *desc);
}
std::cerr << ", children:";
for (auto child : node.children) {
std::cerr << " " << ModuleHeapType(wasm, nodes[child].type);
}
std::cerr << '\n';
}
}
#endif
private:
Index getIndex(HeapType type) {
auto [it, inserted] = indices.insert({type, nodes.size()});
if (inserted) {
nodes.emplace_back(type, nodes.size());
}
return it->second;
}
std::optional<Index> maybeGetIndex(HeapType type) const {
if (auto it = indices.find(type); it != indices.end()) {
return it->second;
}
return std::nullopt;
}
};
// There are two contexts where we have to note subtypings and casts: in the
// initial parallel analysis of the module and in the follow-on fixed point
// analysis over the type tree. Most of the logic is the same in both cases, but
// the final update of data structures is different. This CRTP utility
// deduplicates the shared logic.
template<typename Self> struct Noter {
Self& self() { return *static_cast<Self*>(this); }
void noteSubtype(HeapType sub, HeapType super) {
// Bottom types are uninteresting, but other basic heap types can be
// interesting because of their interactions with casts.
if (sub == super || sub.isBottom()) {
return;
}
DBG(std::cerr << "noting " << ModuleHeapType(*wasm, sub)
<< " <: " << ModuleHeapType(*wasm, super) << '\n');
self().doNoteSubtype(sub, super);
}
void noteSubtype(Type sub, Type super) {
if (sub.isTuple()) {
assert(super.isTuple() && sub.size() == super.size());
for (size_t i = 0, size = sub.size(); i < size; ++i) {
noteSubtype(sub[i], super[i]);
}
return;
}
if (!sub.isRef() || !super.isRef()) {
return;
}
noteSubtype(sub.getHeapType(), super.getHeapType());
}
void noteSubtype(Type sub, Expression* super) {
noteSubtype(sub, super->type);
}
void noteSubtype(Expression* sub, Type super) {
noteSubtype(sub->type, super);
}
void noteSubtype(Expression* sub, Expression* super) {
noteSubtype(sub->type, super->type);
}
void noteDescriptor(HeapType described, HeapType descriptor) {
DBG(std::cerr << "noting " << ModuleHeapType(*wasm, described) << " -> "
<< ModuleHeapType(*wasm, descriptor) << '\n');
self().doNoteDescriptor(described, descriptor);
}
void noteDescribed(HeapType type) {
auto desc = type.getDescriptorType();
assert(desc);
noteDescriptor(type, *desc);
}
void noteDescriptor(HeapType type) {
auto desc = type.getDescribedType();
assert(desc);
noteDescriptor(*desc, type);
}
void noteCast(HeapType src, Type dstType) {
auto dst = dstType.getHeapType();
// Casts to self and casts that must fail because they have incompatible
// types are uninteresting.
if (dst == src) {
return;
}
if (HeapType::isSubType(dst, src)) {
if (dstType.isExact()) {
// This cast only tests that the exact destination type is a subtype
// of the source type and does not impose additional requirements on
// subtypes of the destination type like a normal cast does.
noteSubtype(dst, src);
return;
}
self().doNoteCast(src, dst);
return;
}
if (HeapType::isSubType(src, dst)) {
// This is an upcast that will always succeed, but only if we ensure
// src <: dst.
noteSubtype(src, dst);
}
}
void noteCast(Expression* src, Type dst) {
if (src->type.isRef() && dst.isRef()) {
noteCast(src->type.getHeapType(), dst);
}
}
void noteCast(Expression* src, Expression* dst) {
if (src->type.isRef() && dst->type.isRef()) {
noteCast(src->type.getHeapType(), dst->type);
}
}
};
struct Unsubtyping : Pass, Noter<Unsubtyping> {
// The kind of work to process.
enum class Kind { Subtype, Descriptor };
// (sub, super) pairs that we have discovered but not yet processed.
std::vector<std::tuple<Kind, HeapType, HeapType>> work;
// Record the type tree with supertype and subtype relations in such a way
// that we can add new supertype relationships in constant time.
TypeTree types;
// Map from cast source types to their destinations.
Map<HeapType, std::vector<HeapType>> casts;
DBG(Module* wasm = nullptr);
void run(Module* wasm) override {
DBG(this->wasm = wasm);
if (!wasm->features.hasGC()) {
return;
}
if (!getPassOptions().closedWorld) {
Fatal() << "Unsubtyping requires --closed-world";
}
// Initialize the subtype relation based on what is immediately required to
// keep the code and public types valid.
analyzePublicTypes(*wasm);
analyzeJSCalledFunctions(*wasm);
analyzeModule(*wasm);
// Find further subtypings and iterate to a fixed point.
while (!work.empty()) {
auto [kind, a, b] = work.back();
work.pop_back();
switch (kind) {
case Kind::Subtype:
processSubtype(a, b);
break;
case Kind::Descriptor:
processDescriptor(a, b);
break;
}
}
DBG(types.dump(*wasm));
// If we removed a descriptor from a type, we may need to update its
// allocation sites accordingly.
fixupAllocations(*wasm);
rewriteTypes(*wasm);
// Cast types may be refinable if their source and target types are no
// longer related. TODO: Experiment with running this only after checking
// whether it is necessary.
ReFinalize().run(getPassRunner(), wasm);
}
void doNoteSubtype(HeapType sub, HeapType super) {
work.push_back({Kind::Subtype, sub, super});
}
void doNoteCast(HeapType src, HeapType dst) { casts[src].push_back(dst); }
void doNoteDescriptor(HeapType described, HeapType descriptor) {
work.push_back({Kind::Descriptor, described, descriptor});
}
void analyzePublicTypes(Module& wasm) {
// We cannot change supertypes for anything public.
for (auto type : ModuleUtils::getPublicHeapTypes(wasm)) {
if (auto super = type.getDeclaredSuperType()) {
noteSubtype(type, *super);
}
if (auto desc = type.getDescriptorType()) {
noteDescriptor(type, *desc);
}
}
}
void analyzeJSCalledFunctions(Module& wasm) {
if (!wasm.features.hasCustomDescriptors()) {
return;
}
Type anyref(HeapType::any, Nullable);
for (auto func : Intrinsics(wasm).getJSCalledFunctions()) {
// Parameter types flow into Wasm and are implicitly cast from any.
for (auto type : wasm.getFunction(func)->getParams()) {
if (Type::isSubType(type, anyref)) {
noteCast(HeapType::any, type);
}
}
for (auto type : wasm.getFunction(func)->getResults()) {
// Result types flow into JS and are implicitly converted from any to
// extern. They may also expose configured prototypes that we must keep.
if (Type::isSubType(type, anyref)) {
auto heapType = type.getHeapType();
noteSubtype(heapType, HeapType::any);
if (auto desc = heapType.getDescriptorType();
desc && StructUtils::hasPossibleJSPrototypeField(*desc)) {
noteDescriptor(heapType, *desc);
}
}
}
}
}
void analyzeModule(Module& wasm) {
struct Info {
// (source, target) pairs for casts.
Set<std::pair<HeapType, HeapType>> casts;
// Observed (sub, super) subtype constraints.
Set<std::pair<HeapType, HeapType>> subtypings;
// Observed (described, descriptor) requirements.
Set<std::pair<HeapType, HeapType>> descriptors;
};
struct Collector
: ControlFlowWalker<Collector, SubtypingDiscoverer<Collector>>,
Noter<Collector> {
using Super =
ControlFlowWalker<Collector, SubtypingDiscoverer<Collector>>;
Info& info;
bool trapsNeverHappen;
Collector(Info& info, bool trapsNeverHappen)
: info(info), trapsNeverHappen(trapsNeverHappen) {}
void doNoteSubtype(HeapType sub, HeapType super) {
info.subtypings.insert({sub, super});
}
void noteNonFlowSubtype(Expression* sub, Type super) {
// This expression's type must be a subtype of |super|, but the value
// does not flow anywhere - this is a static constraint. As the value
// does not flow, it cannot reach anywhere else, which means we need
// this in order to validate but it does not interact with casts. Given
// that, if super is a basic type then we can simply ignore this: we
// only remove subtyping between user types, so subtyping wrt basic
// types is unchanged, and so this constraint will never be a problem.
//
// This is sort of a hack because in general to be precise we should not
// just consider basic types here - in general, we should note for each
// constraint whether it is a flow-based one or not, and only take the
// flow-based ones into account when looking at the impact of casts.
// However, in practice this is enough as the only non-trivial case of
// |noteNonFlowSubtype| is for RefEq, which uses a basic type (eqref).
// Other cases of non-flow subtyping end up trivial, e.g., the target of
// a CallRef is compared to itself (and we ignore constraints of A :>
// A). However, if we change how |noteNonFlowSubtype| is used in
// SubtypingDiscoverer then we may need to generalize this.
if (super.isRef() && super.getHeapType().isBasic()) {
return;
}
// Otherwise, we must take this into account.
noteSubtype(sub, super);
}
void doNoteCast(HeapType src, HeapType dst) {
info.casts.insert({src, dst});
}
void doNoteDescriptor(HeapType described, HeapType descriptor) {
info.descriptors.insert({described, descriptor});
}
void visitRefGetDesc(RefGetDesc* curr) {
Super::visitRefGetDesc(curr);
if (!curr->ref->type.isStruct()) {
return;
}
noteDescribed(curr->ref->type.getHeapType());
}
void visitRefCast(RefCast* curr) {
Super::visitRefCast(curr);
if (!curr->desc || !curr->desc->type.isStruct()) {
return;
}
noteDescriptor(curr->desc->type.getHeapType());
}
void visitBrOn(BrOn* curr) {
Super::visitBrOn(curr);
if (!curr->desc || !curr->desc->type.isStruct()) {
return;
}
noteDescriptor(curr->desc->type.getHeapType());
}
void visitStructNew(StructNew* curr) {
Super::visitStructNew(curr);
if (curr->type == Type::unreachable || !curr->desc) {
return;
}
// Normally we do not treat struct.new as requiring a descriptor, even
// if it has one. We are happy to optimize out descriptors that are set
// in allocations and then never used. But if the descriptor is nullable
// and outside a function context and we assume it may be null and cause
// a trap, then we have no way to preserve that trap without keeping the
// descriptor around.
if (trapsNeverHappen || getFunction() ||
curr->desc->type.isNonNullable()) {
return;
}
// We must preserve the potential trap. When we update the instructions
// later we will move this allocation to a new global if necessary to
// preserve the potential trap even if a parent of the current
// expression is removed.
noteDescribed(curr->type.getHeapType());
}
};
bool trapsNeverHappen = getPassOptions().trapsNeverHappen;
// Collect subtyping constraints and casts from functions in parallel.
ModuleUtils::ParallelFunctionAnalysis<Info> analysis(
wasm, [&](Function* func, Info& info) {
if (!func->imported()) {
Collector(info, trapsNeverHappen).walkFunctionInModule(func, &wasm);
}
});
Info collectedInfo;
for (auto& [_, info] : analysis.map) {
collectedInfo.casts.insert(info.casts.begin(), info.casts.end());
collectedInfo.subtypings.insert(info.subtypings.begin(),
info.subtypings.end());
collectedInfo.descriptors.insert(info.descriptors.begin(),
info.descriptors.end());
}
// Collect constraints from module-level code as well.
Collector collector(collectedInfo, trapsNeverHappen);
collector.walkModuleCode(&wasm);
collector.setModule(&wasm);
for (auto& global : wasm.globals) {
collector.visitGlobal(global.get());
}
for (auto& segment : wasm.elementSegments) {
collector.visitElementSegment(segment.get());
}
// Prepare the collected information for the upcoming processing loop.
for (auto& [sub, super] : collectedInfo.subtypings) {
noteSubtype(sub, super);
}
// Combine casts we have already noted into the newly gathered casts.
for (auto& [src, dsts] : casts) {
for (auto dst : dsts) {
collectedInfo.casts.insert({src, dst});
}
dsts.clear();
}
// Record the deduplicated cast info.
for (auto [src, dst] : collectedInfo.casts) {
casts[src].push_back(dst);
}
for (auto [described, descriptor] : collectedInfo.descriptors) {
noteDescriptor(described, descriptor);
}
}
void processSubtype(HeapType sub, HeapType super) {
DBG(std::cerr << "processing " << ModuleHeapType(*wasm, sub)
<< " <: " << ModuleHeapType(*wasm, super) << '\n');
assert(HeapType::isSubType(sub, super));
auto oldSuper = types.getSupertype(sub);
if (oldSuper) {
// We already had a recorded supertype. The new supertype might be
// deeper,shallower, or equal to the old supertype. We must recursively
// note the relationship between the old and new supertypes.
if (super == *oldSuper) {
// Nothing new to do here.
return;
}
if (HeapType::isSubType(*oldSuper, super)) {
// sub <: oldSuper <: super
noteSubtype(*oldSuper, super);
// We already handled sub <: oldSuper, so we're done.
return;
}
// sub <: super <: oldSuper
// Eagerly process super <: oldSuper first. This ensures that sub and
// super will already be in the same tree when we process them below, so
// when we process casts we will know that we only need to process up to
// oldSuper.
processSubtype(super, *oldSuper);
}
types.setSupertype(sub, super);
// Complete the descriptor squares to the left and right of the new
// subtyping edge if those squares can possibly exist based on the original
// types.
if (super.getDescribedType()) {
completeDescriptorSquare(
types.getDescribed(super), super, types.getDescribed(sub), sub);
}
if (super.getDescriptorType()) {
completeDescriptorSquare(
super, types.getDescriptor(super), sub, types.getDescriptor(sub));
}
// Find the implied subtypings from the type definitions and casts.
processDefinitions(sub, super);
processCasts(sub, super, oldSuper);
}
void processDescriptor(HeapType described, HeapType descriptor) {
DBG(std::cerr << "processing " << ModuleHeapType(*wasm, described) << " -> "
<< ModuleHeapType(*wasm, descriptor) << '\n');
assert(described.getDescriptorType() &&
*described.getDescriptorType() == descriptor);
if (auto oldDesc = types.getDescriptor(described)) {
// We already know about this descriptor.
assert(*oldDesc == descriptor);
return;
}
types.setDescriptor(described, descriptor);
// Complete the descriptor squares above and below the new descriptor edge.
completeDescriptorSquare(
std::nullopt, types.getSupertype(descriptor), described, descriptor);
for (auto sub : types.immediateSubtypes(described)) {
completeDescriptorSquare(
described, descriptor, sub, types.getDescriptor(sub));
}
for (auto subDesc : types.immediateSubtypes(descriptor)) {
completeDescriptorSquare(
described, descriptor, types.getDescribed(subDesc), subDesc);
}
}
void processDefinitions(HeapType sub, HeapType super) {
if (super.isBasic()) {
return;
}
switch (sub.getKind()) {
case HeapTypeKind::Func: {
auto sig = sub.getSignature();
auto superSig = super.getSignature();
noteSubtype(superSig.params, sig.params);
noteSubtype(sig.results, superSig.results);
break;
}
case HeapTypeKind::Struct: {
const auto& fields = sub.getStruct().fields;
const auto& superFields = super.getStruct().fields;
for (size_t i = 0, size = superFields.size(); i < size; ++i) {
noteSubtype(fields[i].type, superFields[i].type);
}
break;
}
case HeapTypeKind::Array: {
auto elem = sub.getArray().element;
noteSubtype(elem.type, super.getArray().element.type);
break;
}
case HeapTypeKind::Cont:
WASM_UNREACHABLE("TODO: cont");
case HeapTypeKind::Basic:
WASM_UNREACHABLE("unexpected kind");
}
}
void
processCasts(HeapType sub, HeapType super, std::optional<HeapType> oldSuper) {
// We are either attaching the one tree rooted at `sub` under a new
// supertype in another tree, or we are reparenting `sub` below a
// descendent of `oldSuper` in the same tree. In the former case, we must
// evaluate `sub` and all its subtypes against all its new supertypes and
// their cast destinations. In the latter case, `sub` and all its subtypes
// must have already been evaluated against `oldSuper` and its supertypes,
// so we only need to additionally evaluate them against supertypes up to
// `oldSuper`.
for (auto type : types.subtypes(sub)) {
for (auto src : types.supertypes(super)) {
if (oldSuper && src == *oldSuper) {
break;
}
for (auto dst : casts[src]) {
if (HeapType::isSubType(type, dst)) {
noteSubtype(type, dst);
}
}
}
}
}
void completeDescriptorSquare(std::optional<HeapType> super,
std::optional<HeapType> superDesc,
std::optional<HeapType> sub,
std::optional<HeapType> subDesc) {
if ((super && super->isBasic()) || (superDesc && superDesc->isBasic())) {
// Basic types do not have descriptors or described types, so do not form
// descriptor squares.
return;
}
if (bool(super) + bool(superDesc) + bool(sub) + bool(subDesc) < 3) {
// We must have two adjacent edges (involving at least 3 types) for there
// to be any further requirements.
return;
}
// There may be up to one missing type. Look it up using its original
// descriptor relation with the present types and add the missing edges.
if (!super) {
super = superDesc->getDescribedType();
} else if (!sub) {
sub = subDesc->getDescribedType();
} else if (!subDesc) {
subDesc = sub->getDescriptorType();
} else if (!superDesc) {
// This is the only type that is allowed to be missing.
return;
}
// Add all the edges. Don't worry about duplicating existing edges because
// checking whether they're necessary now would be about as expensive as
// discarding them later.
// TODO: We will be able to assume this once we update the descriptor
// validation rules.
if (HeapType::isSubType(*sub, *super)) {
noteSubtype(*sub, *super);
}
noteSubtype(*subDesc, *superDesc);
noteDescriptor(*super, *superDesc);
noteDescriptor(*sub, *subDesc);
}
void rewriteTypes(Module& wasm) {
struct Rewriter : GlobalTypeRewriter {
Unsubtyping& parent;
Rewriter(Unsubtyping& parent, Module& wasm)
: GlobalTypeRewriter(wasm), parent(parent) {}
std::optional<HeapType> getDeclaredSuperType(HeapType type) override {
if (auto super = parent.types.getSupertype(type);
super && !super->isBasic()) {
return *super;
}
return std::nullopt;
}
void modifyTypeBuilderEntry(TypeBuilder& typeBuilder,
Index i,
HeapType oldType) override {
if (!parent.types.getDescribed(oldType)) {
typeBuilder[i].describes(std::nullopt);
}
if (!parent.types.getDescriptor(oldType)) {
typeBuilder[i].descriptor(std::nullopt);
}
}
};
Rewriter(*this, wasm).update();
}
void fixupAllocations(Module& wasm) {
if (!wasm.features.hasCustomDescriptors()) {
return;
}
// TODO: Consider running the fixup only if we are actually removing any
// descriptors. This would require a better way of detecting this than
// collecing and iterating over all the types, though.
struct Rewriter : WalkerPass<PostWalker<Rewriter>> {
const TypeTree& types;
// Allocations that might trap that have been removed from module-level
// initializers. These need to be placed in new globals to preserve any
// instantiation-time traps.
std::vector<Expression*> removedTrappingInits;