diff --git a/src/ir/metadata.cpp b/src/ir/metadata.cpp index a260ade165b..6ac749b1fff 100644 --- a/src/ir/metadata.cpp +++ b/src/ir/metadata.cpp @@ -44,7 +44,8 @@ void copyBetweenFunctions(Expression* origin, Function* originFunc, Function* copyFunc) { if (originFunc->debugLocations.empty() && - originFunc->codeAnnotations.empty()) { + originFunc->codeAnnotations.empty() && + originFunc->funcAnnotations.empty()) { // Nothing to copy. return; } @@ -75,7 +76,7 @@ void copyBetweenFunctions(Expression* origin, } } - // Also copy function-level annotations, if any. + // Also copy function-level annotations. copyFunc->funcAnnotations = originFunc->funcAnnotations; } diff --git a/src/wasm.h b/src/wasm.h index 2731986bde5..3c84e02b984 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -2279,6 +2279,9 @@ struct CodeAnnotation { return removableIfUnused == other.removableIfUnused && jsCalled == other.jsCalled && idempotent == other.idempotent; } + + // Checks if no annotation is actually set. + bool empty() { return *this == CodeAnnotation(); } }; class Function : public Importable { diff --git a/test/lit/merge/annotations-func-only.wat b/test/lit/merge/annotations-func-only.wat new file mode 100644 index 00000000000..90f01d08362 --- /dev/null +++ b/test/lit/merge/annotations-func-only.wat @@ -0,0 +1,44 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. + +;; RUN: wasm-merge %s first %s.second second -all -S -o - | filecheck %s + +;; Test that we handle code annotations properly when they appear *only* in +;; functions (not code). Both the first and second wasm files have an annotation +;; that should be preserved. + +(module + ;; CHECK: (type $0 (func)) + + ;; CHECK: (export "first" (func $first)) + + ;; CHECK: (export "second" (func $second)) + + ;; CHECK: (@binaryen.js.called) + ;; CHECK-NEXT: (func $first (type $0) + ;; CHECK-NEXT: (if + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: (then + ;; CHECK-NEXT: (return) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (@binaryen.js.called) + (func $first (export "first") + (if + (i32.const 0) + (then + (return) + ) + ) + ) +) + +;; CHECK: (@binaryen.js.called) +;; CHECK-NEXT: (func $second (type $0) +;; CHECK-NEXT: (if +;; CHECK-NEXT: (i32.const 0) +;; CHECK-NEXT: (then +;; CHECK-NEXT: (return) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) diff --git a/test/lit/merge/annotations-func-only.wat.second b/test/lit/merge/annotations-func-only.wat.second new file mode 100644 index 00000000000..2bc67fd5c0e --- /dev/null +++ b/test/lit/merge/annotations-func-only.wat.second @@ -0,0 +1,12 @@ +(module + (@binaryen.js.called) + (func $second (export "second") + (if + (i32.const 0) + (then + (return) + ) + ) + ) +) +