Skip to content

Commit 4c4e348

Browse files
authored
Create ir::Signatures on a per-ModuleInternedTypeIndex basis (#11356)
Not on a per `TypeIndex` basis. The former is deduplicated, the latter is not. This way we don't end up creating duplicate `ir::Signature`s in the CLIF.
1 parent 228515c commit 4c4e348

File tree

3 files changed

+112
-9
lines changed

3 files changed

+112
-9
lines changed

crates/cranelift/src/func_environ.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ wasmtime_environ::foreach_builtin_function!(declare_function_signatures);
9595
pub struct FuncEnvironment<'module_environment> {
9696
compiler: &'module_environment Compiler,
9797
isa: &'module_environment (dyn TargetIsa + 'module_environment),
98-
module: &'module_environment Module,
98+
pub(crate) module: &'module_environment Module,
9999
types: &'module_environment ModuleTypesBuilder,
100100
wasm_func_ty: &'module_environment WasmFuncType,
101101
sig_ref_to_ty: SecondaryMap<ir::SigRef, Option<&'module_environment WasmFuncType>>,
@@ -2611,10 +2611,9 @@ impl FuncEnvironment<'_> {
26112611
pub fn make_indirect_sig(
26122612
&mut self,
26132613
func: &mut ir::Function,
2614-
index: TypeIndex,
2614+
index: ModuleInternedTypeIndex,
26152615
) -> WasmResult<ir::SigRef> {
2616-
let interned_index = self.module.types[index].unwrap_module_type_index();
2617-
let wasm_func_ty = self.types[interned_index].unwrap_func();
2616+
let wasm_func_ty = self.types[index].unwrap_func();
26182617
let sig = crate::wasm_call_signature(self.isa, wasm_func_ty, &self.tunables);
26192618
let sig_ref = func.import_signature(sig);
26202619
self.sig_ref_to_ty[sig_ref] = Some(wasm_func_ty);

crates/cranelift/src/translate/state.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use cranelift_codegen::ir::{self, Block, Inst, Value};
1010
use cranelift_entity::SecondaryMap;
1111
use cranelift_entity::packed_option::PackedOption;
1212
use std::vec::Vec;
13-
use wasmtime_environ::{FuncIndex, GlobalIndex, MemoryIndex, TypeIndex, WasmResult};
13+
use wasmtime_environ::{
14+
FuncIndex, GlobalIndex, MemoryIndex, ModuleInternedTypeIndex, TypeIndex, WasmResult,
15+
};
1416

1517
/// Information about the presence of an associated `else` for an `if`, or the
1618
/// lack thereof.
@@ -235,7 +237,7 @@ pub struct FuncTranslationState {
235237
// Map of indirect call signatures that have been created by
236238
// `FuncEnvironment::make_indirect_sig()`.
237239
// Stores both the signature reference and the number of WebAssembly arguments
238-
signatures: SecondaryMap<TypeIndex, Option<(ir::SigRef, usize)>>,
240+
signatures: SecondaryMap<ModuleInternedTypeIndex, Option<(ir::SigRef, usize)>>,
239241

240242
// Imported and local functions that have been created by
241243
// `FuncEnvironment::make_direct_func()`.
@@ -511,12 +513,13 @@ impl FuncTranslationState {
511513
index: TypeIndex,
512514
environ: &mut FuncEnvironment<'_>,
513515
) -> WasmResult<(ir::SigRef, usize)> {
514-
match self.signatures[index] {
516+
let interned_index = environ.module.types[index].unwrap_module_type_index();
517+
match self.signatures[interned_index] {
515518
Some((sig, num_params)) => Ok((sig, num_params)),
516519
None => {
517-
let sig = environ.make_indirect_sig(func, index)?;
520+
let sig = environ.make_indirect_sig(func, interned_index)?;
518521
let num_params = num_wasm_parameters(environ, &func.dfg.signatures[sig]);
519-
self.signatures[index] = Some((sig, num_params));
522+
self.signatures[interned_index] = Some((sig, num_params));
520523
Ok((sig, num_params))
521524
}
522525
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
;;! target = "x86_64"
2+
3+
(module
4+
;; These two types should be deduped to the same `ir::Signature` in the
5+
;; translated CLIF.
6+
(type (func (result i32)))
7+
(type (func (result i32)))
8+
9+
(import "" "" (table 0 funcref))
10+
11+
(func (param i32) (result i32 i32)
12+
local.get 0
13+
call_indirect (type 0)
14+
local.get 0
15+
call_indirect (type 1)
16+
)
17+
)
18+
19+
;; function u0:0(i64 vmctx, i64, i32) -> i32, i32 tail {
20+
;; gv0 = vmctx
21+
;; gv1 = load.i64 notrap aligned readonly gv0+8
22+
;; gv2 = load.i64 notrap aligned gv1+16
23+
;; gv3 = vmctx
24+
;; gv4 = load.i64 notrap aligned readonly can_move gv3+48
25+
;; gv5 = load.i64 notrap aligned gv4
26+
;; gv6 = load.i64 notrap aligned gv4+8
27+
;; sig0 = (i64 vmctx, i64) -> i32 tail
28+
;; sig1 = (i64 vmctx, i32, i64) -> i64 tail
29+
;; fn0 = colocated u1:9 sig1
30+
;; stack_limit = gv2
31+
;;
32+
;; block0(v0: i64, v1: i64, v2: i32):
33+
;; @002d v64 = load.i64 notrap aligned readonly can_move v0+48
34+
;; @002d v5 = load.i64 notrap aligned v64+8
35+
;; @002d v6 = ireduce.i32 v5
36+
;; @002d v7 = icmp uge v2, v6
37+
;; @002d v8 = uextend.i64 v2
38+
;; @002d v62 = load.i64 notrap aligned readonly can_move v0+48
39+
;; @002d v9 = load.i64 notrap aligned v62
40+
;; v61 = iconst.i64 3
41+
;; @002d v10 = ishl v8, v61 ; v61 = 3
42+
;; @002d v11 = iadd v9, v10
43+
;; @002d v12 = iconst.i64 0
44+
;; @002d v13 = select_spectre_guard v7, v12, v11 ; v12 = 0
45+
;; @002d v14 = load.i64 user5 aligned table v13
46+
;; v60 = iconst.i64 -2
47+
;; @002d v15 = band v14, v60 ; v60 = -2
48+
;; @002d brif v14, block3(v15), block2
49+
;;
50+
;; block2 cold:
51+
;; @002d v17 = iconst.i32 0
52+
;; @002d v19 = uextend.i64 v2
53+
;; @002d v20 = call fn0(v0, v17, v19) ; v17 = 0
54+
;; @002d jump block3(v20)
55+
;;
56+
;; block3(v16: i64):
57+
;; @002d v22 = load.i64 notrap aligned readonly can_move v0+40
58+
;; @002d v23 = load.i32 notrap aligned readonly can_move v22
59+
;; @002d v24 = load.i32 user6 aligned readonly v16+16
60+
;; @002d v25 = icmp eq v24, v23
61+
;; @002d trapz v25, user7
62+
;; @002d v26 = load.i64 notrap aligned readonly v16+8
63+
;; @002d v27 = load.i64 notrap aligned readonly v16+24
64+
;; @002d v28 = call_indirect sig0, v26(v27, v0)
65+
;; @0032 v58 = load.i64 notrap aligned readonly can_move v0+48
66+
;; @0032 v30 = load.i64 notrap aligned v58+8
67+
;; @0032 v31 = ireduce.i32 v30
68+
;; @0032 v32 = icmp.i32 uge v2, v31
69+
;; @0032 v33 = uextend.i64 v2
70+
;; @0032 v56 = load.i64 notrap aligned readonly can_move v0+48
71+
;; @0032 v34 = load.i64 notrap aligned v56
72+
;; v55 = iconst.i64 3
73+
;; @0032 v35 = ishl v33, v55 ; v55 = 3
74+
;; @0032 v36 = iadd v34, v35
75+
;; @0032 v37 = iconst.i64 0
76+
;; @0032 v38 = select_spectre_guard v32, v37, v36 ; v37 = 0
77+
;; @0032 v39 = load.i64 user5 aligned table v38
78+
;; v54 = iconst.i64 -2
79+
;; @0032 v40 = band v39, v54 ; v54 = -2
80+
;; @0032 brif v39, block5(v40), block4
81+
;;
82+
;; block4 cold:
83+
;; @0032 v42 = iconst.i32 0
84+
;; @0032 v44 = uextend.i64 v2
85+
;; @0032 v45 = call fn0(v0, v42, v44) ; v42 = 0
86+
;; @0032 jump block5(v45)
87+
;;
88+
;; block5(v41: i64):
89+
;; @0032 v47 = load.i64 notrap aligned readonly can_move v0+40
90+
;; @0032 v48 = load.i32 notrap aligned readonly can_move v47
91+
;; @0032 v49 = load.i32 user6 aligned readonly v41+16
92+
;; @0032 v50 = icmp eq v49, v48
93+
;; @0032 trapz v50, user7
94+
;; @0032 v51 = load.i64 notrap aligned readonly v41+8
95+
;; @0032 v52 = load.i64 notrap aligned readonly v41+24
96+
;; @0032 v53 = call_indirect sig0, v51(v52, v0)
97+
;; @0035 jump block1
98+
;;
99+
;; block1:
100+
;; @0035 return v28, v53
101+
;; }

0 commit comments

Comments
 (0)