-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WebAssembly] Deduplicate imports of the same module name, field name…
…, and type When two symbols import the same thing, only one import should be emitted in the Wasm file. Fixes https://bugs.llvm.org/show_bug.cgi?id=50938 Reviewed By: sbc100 Differential Revision: https://reviews.llvm.org/D105519
- Loading branch information
Showing
7 changed files
with
366 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %s -o %t.o | ||
# RUN: wasm-ld -o %t1.wasm %t.o | ||
# RUN: obj2yaml %t1.wasm | FileCheck %s | ||
|
||
.globl f1 | ||
.import_module f1, env | ||
.import_name f1, f | ||
.functype f1 () -> (i32) | ||
|
||
# Same import module/name/type as `f1`, should be de-duped. | ||
.globl f2 | ||
.import_module f2, env | ||
.import_name f2, f | ||
.functype f2 () -> (i32) | ||
|
||
# Same import module/name, but different type. Should not be de-duped. | ||
.globl f3 | ||
.import_module f3, env | ||
.import_name f3, f | ||
.functype f3 () -> (f32) | ||
|
||
.globl _start | ||
_start: | ||
.functype _start () -> () | ||
call f1 | ||
drop | ||
call f2 | ||
drop | ||
call f3 | ||
drop | ||
end_function | ||
|
||
|
||
# CHECK: - Type: TYPE | ||
# CHECK-NEXT: Signatures: | ||
# CHECK-NEXT: - Index: 0 | ||
# CHECK-NEXT: ParamTypes: [] | ||
# CHECK-NEXT: ReturnTypes: | ||
# CHECK-NEXT: - I32 | ||
# CHECK-NEXT: - Index: 1 | ||
# CHECK-NEXT: ParamTypes: [] | ||
# CHECK-NEXT: ReturnTypes: | ||
# CHECK-NEXT: - F32 | ||
# CHECK-NEXT: - Index: 2 | ||
# CHECK-NEXT: ParamTypes: [] | ||
# CHECK-NEXT: ReturnTypes: [] | ||
# CHECK-NEXT: - Type: IMPORT | ||
# CHECK-NEXT: Imports: | ||
# CHECK-NEXT: - Module: env | ||
# CHECK-NEXT: Field: f | ||
# CHECK-NEXT: Kind: FUNCTION | ||
# CHECK-NEXT: SigIndex: 0 | ||
# CHECK-NEXT: - Module: env | ||
# CHECK-NEXT: Field: f | ||
# CHECK-NEXT: Kind: FUNCTION | ||
# CHECK-NEXT: SigIndex: 1 | ||
# CHECK-NEXT: - Type: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %s -o %t.o | ||
# RUN: wasm-ld --no-check-features -o %t1.wasm %t.o | ||
# RUN: obj2yaml %t1.wasm | FileCheck %s | ||
|
||
.global g1 | ||
.import_module g1, env | ||
.import_name g1, g | ||
.globaltype g1, i64, immutable | ||
|
||
# Same import module/name/type as `g1`, should be de-duped. | ||
.global g2 | ||
.import_module g2, env | ||
.import_name g2, g | ||
.globaltype g2, i64, immutable | ||
|
||
# Imported as an i32 instead of i64, so should not be de-duped. | ||
.global g3 | ||
.import_module g3, env | ||
.import_name g3, g | ||
.globaltype g3, i32, immutable | ||
|
||
# Imported as mutable instead of immutable, so should not be de-duped. | ||
.global g4 | ||
.import_module g4, env | ||
.import_name g4, g | ||
.globaltype g4, i64 | ||
|
||
.globl _start | ||
_start: | ||
.functype _start () -> () | ||
global.get g1 | ||
drop | ||
global.get g2 | ||
drop | ||
global.get g3 | ||
drop | ||
global.get g4 | ||
drop | ||
end_function | ||
|
||
|
||
# CHECK: - Type: IMPORT | ||
# CHECK-NEXT: Imports: | ||
# CHECK-NEXT: - Module: env | ||
# CHECK-NEXT: Field: g | ||
# CHECK-NEXT: Kind: GLOBAL | ||
# CHECK-NEXT: GlobalType: I64 | ||
# CHECK-NEXT: GlobalMutable: false | ||
# CHECK-NEXT: - Module: env | ||
# CHECK-NEXT: Field: g | ||
# CHECK-NEXT: Kind: GLOBAL | ||
# CHECK-NEXT: GlobalType: I32 | ||
# CHECK-NEXT: GlobalMutable: false | ||
# CHECK-NEXT: - Module: env | ||
# CHECK-NEXT: Field: g | ||
# CHECK-NEXT: Kind: GLOBAL | ||
# CHECK-NEXT: GlobalType: I64 | ||
# CHECK-NEXT: GlobalMutable: true | ||
# CHECK-NEXT: - Type: | ||
|
||
# CHECK: GlobalNames: | ||
# CHECK-NEXT: - Index: 0 | ||
# CHECK-NEXT: Name: g1 | ||
# CHECK-NEXT: - Index: 1 | ||
# CHECK-NEXT: Name: g3 | ||
# CHECK-NEXT: - Index: 2 | ||
# CHECK-NEXT: Name: g4 | ||
# CHECK-NEXT: - Index: 3 | ||
# CHECK-NEXT: Name: __stack_pointer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# RUN: llvm-mc -mattr=+reference-types -triple=wasm32-unknown-unknown -filetype=obj -o %t.o %s | ||
# RUN: wasm-ld --allow-undefined -o %t1.wasm %t.o | ||
# RUN: obj2yaml %t1.wasm | FileCheck %s | ||
|
||
.tabletype t1, funcref | ||
.import_module t1, env | ||
.import_name t1, t | ||
.globl t1 | ||
|
||
# Same import module/name/type as `t1`, should be de-duped. | ||
.tabletype t2, funcref | ||
.import_module t2, env | ||
.import_name t2, t | ||
.globl t2 | ||
|
||
# Imported as an externref instead of funcref, so should not be de-duped. | ||
.tabletype t3, externref | ||
.import_module t3, env | ||
.import_name t3, t | ||
.globl t3 | ||
|
||
.globl _start | ||
_start: | ||
.functype _start () -> () | ||
|
||
# Read from `t1` | ||
i32.const 0 | ||
table.get t1 | ||
drop | ||
|
||
# Read from `t2` | ||
i32.const 0 | ||
table.get t2 | ||
drop | ||
|
||
# Read from `t3` | ||
i32.const 0 | ||
table.get t3 | ||
drop | ||
|
||
end_function | ||
|
||
# XXX: the second imported table has index 1, not 0. I've verified by hand (with | ||
# `wasm2wat`) that the resulting Wasm file is correct: `t3` does end up at index | ||
# 1 and our `table.get` instructions are using the proper table index | ||
# immediates. This is also asserted (less legibly) in the hexdump of the code | ||
# body below. It looks like there's a bug in how `obj2yaml` disassembles | ||
# multiple table imports. | ||
|
||
# CHECK: - Type: IMPORT | ||
# CHECK-NEXT: Imports: | ||
# CHECK-NEXT: - Module: env | ||
# CHECK-NEXT: Field: t | ||
# CHECK-NEXT: Kind: TABLE | ||
# CHECK-NEXT: Table: | ||
# CHECK-NEXT: Index: 0 | ||
# CHECK-NEXT: ElemType: FUNCREF | ||
# CHECK-NEXT: Limits: | ||
# CHECK-NEXT: Minimum: 0x0 | ||
# CHECK-NEXT: - Module: env | ||
# CHECK-NEXT: Field: t | ||
# CHECK-NEXT: Kind: TABLE | ||
# CHECK-NEXT: Table: | ||
# CHECK-NEXT: Index: 0 | ||
# CHECK-NEXT: ElemType: EXTERNREF | ||
# CHECK-NEXT: Limits: | ||
# CHECK-NEXT: Minimum: 0x0 | ||
# CHECK-NEXT: - Type: | ||
|
||
# CHECK: - Type: CODE | ||
# CHECK-NEXT: Functions: | ||
# CHECK-NEXT: - Index: 0 | ||
# CHECK-NEXT: Locals: [] | ||
# CHECK-NEXT: Body: 41002580808080001A41002580808080001A41002581808080001A0B | ||
# CHECK-NEXT: - Type: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.