Skip to content

Commit affe9b7

Browse files
authored
RemoveUnusedModuleElements: Referenced elems refer to their offsets (#8750)
1 parent f526098 commit affe9b7

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/passes/RemoveUnusedModuleElements.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,9 @@ struct Analyzer {
764764
} else if (kind == ModuleElementKind::ElementSegment) {
765765
// TODO: We could empty out parts of the segment we don't need.
766766
auto* segment = module->getElementSegment(value);
767+
if (segment->offset) {
768+
addReferences(segment->offset);
769+
}
767770
for (auto* item : segment->data) {
768771
addReferences(item);
769772
}

test/lit/passes/remove-unused-module-elements_tnh.wast

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,45 @@
236236
;; T_N_H: (export "mem" (memory $mem))
237237
(export "mem" (memory $mem))
238238
)
239+
240+
;; The exported function has a call_indirect, which refers to the table and
241+
;; elem. The elem refers to the global, so it must not be removed (in either TNH
242+
;; or not).
243+
(module
244+
;; CHECK: (type $nop (func))
245+
;; T_N_H: (type $nop (func))
246+
(type $nop (func))
247+
248+
;; CHECK: (global $g i32 (i32.const 0))
249+
;; T_N_H: (global $g i32 (i32.const 0))
250+
(global $g i32 (i32.const 0))
251+
252+
;; CHECK: (table $table 50 50 funcref)
253+
;; T_N_H: (table $table 50 50 funcref)
254+
(table $table 50 50 funcref)
255+
256+
;; CHECK: (elem $elem (global.get $g) $func)
257+
;; T_N_H: (elem $elem (global.get $g) $func)
258+
(elem $elem (global.get $g) $func)
259+
260+
;; CHECK: (export "func" (func $func))
261+
262+
;; CHECK: (func $func (type $nop)
263+
;; CHECK-NEXT: (call_indirect $table (type $nop)
264+
;; CHECK-NEXT: (i32.const 0)
265+
;; CHECK-NEXT: )
266+
;; CHECK-NEXT: )
267+
;; T_N_H: (export "func" (func $func))
268+
269+
;; T_N_H: (func $func (type $nop)
270+
;; T_N_H-NEXT: (call_indirect $table (type $nop)
271+
;; T_N_H-NEXT: (i32.const 0)
272+
;; T_N_H-NEXT: )
273+
;; T_N_H-NEXT: )
274+
(func $func (export "func") (type $nop)
275+
(call_indirect (type $nop)
276+
(i32.const 0)
277+
)
278+
)
279+
)
280+

0 commit comments

Comments
 (0)