Skip to content

Commit f1cb466

Browse files
authored
[NFC] Add withInexactIfNoCustomDescs helper (#7528)
There are several places in the code where we need to make a type inexact only if custom descriptors is disabled. Add a helper to slightly simplify those places.
1 parent 27fbd31 commit f1cb466

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

src/passes/GUFA.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,8 @@ struct GUFAOptimizer
375375

376376
auto oracleType = parent.getContents(curr).getType();
377377
// Exact casts are only allowed when custom descriptors is enabled.
378-
if (oracleType.isExact() &&
379-
!getModule()->features.hasCustomDescriptors()) {
380-
oracleType = oracleType.with(Inexact);
381-
}
378+
oracleType =
379+
oracleType.withInexactIfNoCustomDescs(getModule()->features);
382380
if (oracleType.isRef() && oracleType != curr->type &&
383381
Type::isSubType(oracleType, curr->type)) {
384382
replaceCurrent(Builder(*getModule()).makeRefCast(curr, oracleType));

src/passes/RemoveUnusedBrs.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -906,11 +906,10 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
906906
// further optimizations after this, and those optimizations might even
907907
// benefit from this improvement.
908908
auto glb = Type::getGreatestLowerBound(curr->castType, refType);
909-
if (!getModule()->features.hasCustomDescriptors() && glb.isExact() &&
910-
!curr->castType.isExact()) {
909+
if (!curr->castType.isExact()) {
911910
// When custom descriptors is not enabled, nontrivial exact casts are
912911
// not allowed.
913-
glb = glb.with(Inexact);
912+
glb = glb.withInexactIfNoCustomDescs(getModule()->features);
914913
}
915914
if (curr->op == BrOnCastFail) {
916915
// BrOnCastFail sends the input type, with adjusted nullability. The

src/wasm-type.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,16 +418,21 @@ class Type {
418418
}
419419

420420
// Return a new reference type with some part updated to the specified value.
421-
Type with(HeapType heapType) {
421+
Type with(HeapType heapType) const {
422422
return Type(heapType, getNullability(), getExactness());
423423
}
424-
Type with(Nullability nullability) {
424+
Type with(Nullability nullability) const {
425425
return Type(getHeapType(), nullability, getExactness());
426426
}
427-
Type with(Exactness exactness) {
427+
Type with(Exactness exactness) const {
428428
return Type(getHeapType(), getNullability(), exactness);
429429
}
430430

431+
// Make the type inexact if custom descriptors is not enabled.
432+
Type withInexactIfNoCustomDescs(FeatureSet feats) const {
433+
return !isExact() || feats.hasCustomDescriptors() ? *this : with(Inexact);
434+
}
435+
431436
private:
432437
template<bool (Type::*pred)() const> bool hasPredicate() {
433438
for (const auto& type : *this) {

0 commit comments

Comments
 (0)