Skip to content
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

Print explicit typeuses for non-MVP function types #6851

Merged
merged 1 commit into from
Aug 19, 2024
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
13 changes: 11 additions & 2 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2804,13 +2804,21 @@ bool PrintSExpression::maybePrintUnreachableOrNullReplacement(Expression* curr,
return maybePrintUnreachableReplacement(curr, type);
}

static bool requiresExplicitFuncType(HeapType type) {
// When the `(type $f)` in a function's typeuse is omitted, the typeuse
// matches or declares an MVP function type. When the intended type is not an
// MVP function type, we therefore need the explicit `(type $f)`.
return type.isOpen() || type.isShared() || type.getRecGroup().size() > 1;
}

void PrintSExpression::handleSignature(HeapType curr, Name name) {
Signature sig = curr.getSignature();
o << "(func";
if (name.is()) {
o << ' ';
name.print(o);
if (currModule && currModule->features.hasGC()) {
if ((currModule && currModule->features.hasGC()) ||
requiresExplicitFuncType(curr)) {
o << " (type ";
printHeapType(curr) << ')';
}
Expand Down Expand Up @@ -2947,7 +2955,8 @@ void PrintSExpression::visitDefinedFunction(Function* curr) {
o << '(';
printMajor(o, "func ");
curr->name.print(o);
if (currModule && currModule->features.hasGC()) {
if ((currModule && currModule->features.hasGC()) ||
requiresExplicitFuncType(curr->type)) {
o << " (type ";
printHeapType(curr->type) << ')';
}
Expand Down
51 changes: 51 additions & 0 deletions test/lit/basic/print-explicit-typeuse.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; RUN: wasm-opt %s --no-validation -S -o - | filecheck %s

;; Check that we print explicit type uses for function signatures when the
;; function type uses non-MVP features, whether or not those features are
;; actually enabled.

(module
;; CHECK: (type $mvp (func))
(type $mvp (func))
;; CHECK: (type $open (sub (func)))
(type $open (sub (func)))
;; CHECK: (type $shared (shared (func)))
(type $shared (shared (func)))
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $rec (func))
(type $rec (func))
;; CHECK: (type $other (struct))
(type $other (struct))
)

;; CHECK: (import "" "" (func $mvp-import))
(import "" "" (func $mvp-import))

;; CHECK: (import "" "" (func $open-import (type $open)))
(import "" "" (func $open-import (type $open)))

;; CHECK: (import "" "" (func $shared-import (type $shared)))
(import "" "" (func $shared-import (type $shared)))

;; CHECK: (import "" "" (func $rec-import (type $rec)))
(import "" "" (func $rec-import (type $rec)))

;; CHECK: (func $mvp
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $mvp (type $mvp))
;; CHECK: (func $open (type $open)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $open (type $open))
;; CHECK: (func $shared (type $shared)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $shared (type $shared))
;; CHECK: (func $rec (type $rec)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $rec (type $rec))
)
Loading