[CodeAnnotations] Copy function-level annotations, fixing wasm-merge (#8345) We copied expression annotations but not function ones. As a result, wasm-merge would copy functions between modules and drop their hints.
diff --git a/src/ir/metadata.cpp b/src/ir/metadata.cpp index 61c5da9..32d5e0a 100644 --- a/src/ir/metadata.cpp +++ b/src/ir/metadata.cpp
@@ -74,6 +74,12 @@ } } } + + // Also copy function-level annotations, if any. + auto iter = originAnnotations.find(nullptr); + if (iter != originAnnotations.end()) { + copyAnnotations[nullptr] = iter->second; + } } #pragma GCC diagnostic push
diff --git a/test/lit/merge/annotations.wat b/test/lit/merge/annotations.wat new file mode 100644 index 0000000..52bdc49 --- /dev/null +++ b/test/lit/merge/annotations.wat
@@ -0,0 +1,46 @@ +;; 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. Both the first and second +;; wasm files have annotations 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: (@metadata.code.branch_hint "\00") + ;; 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") + (@metadata.code.branch_hint "\00") + (if + (i32.const 0) + (then + (return) + ) + ) + ) +) + +;; CHECK: (@binaryen.js.called) +;; CHECK-NEXT: (func $second (type $0) +;; CHECK-NEXT: (@metadata.code.branch_hint "\01") +;; 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.wat.second b/test/lit/merge/annotations.wat.second new file mode 100644 index 0000000..39cccc4 --- /dev/null +++ b/test/lit/merge/annotations.wat.second
@@ -0,0 +1,13 @@ +(module + (@binaryen.js.called) + (func $second (export "second") + (@metadata.code.branch_hint "\01") + (if + (i32.const 0) + (then + (return) + ) + ) + ) +) +