Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Cranelift inliner: fix two mapping bugs. (#14679)

Chris Fallin53b74b7ba07c
diff --git a/cranelift/codegen/src/inline.rs b/cranelift/codegen/src/inline.rs
index 86ede41113..712a49b91a 100644
--- a/cranelift/codegen/src/inline.rs
+++ b/cranelift/codegen/src/inline.rs
@@ -315,6 +315,23 @@ impl InliningAllocs {
calls_needing_exception_table_fixup.clear();
}
+ /// Translate a callee `ir::ExternalName` into the caller.
+ ///
+ /// Must only be called after `create_user_external_name_refs`.
+ fn inlined_external_name(&self, name: &ir::ExternalName) -> ir::ExternalName {
+ match name {
+ ir::ExternalName::User(name_ref) => {
+ ir::ExternalName::User(self.user_external_name_refs[*name_ref].expect(
+ "should have translated all `ir::UserExternalNameRef`s before translating \
+ entities that reference them",
+ ))
+ }
+ ir::ExternalName::TestCase(_)
+ | ir::ExternalName::LibCall(_)
+ | ir::ExternalName::KnownSymbol(_) => name.clone(),
+ }
+ }
+
fn set_inlined_value(
&mut self,
callee: &ir::Function,
@@ -1364,9 +1381,9 @@ fn create_entities(
let mut entity_map = EntityMap::default();
entity_map.block_offset = Some(create_blocks(allocs, func, callee));
- entity_map.global_value_offset = Some(create_global_values(func, callee)?);
- entity_map.sig_ref_offset = Some(create_sig_refs(func, callee));
create_user_external_name_refs(allocs, func, callee);
+ entity_map.global_value_offset = Some(create_global_values(allocs, func, callee)?);
+ entity_map.sig_ref_offset = Some(create_sig_refs(func, callee));
entity_map.func_ref_offset = Some(create_func_refs(allocs, func, callee, &entity_map));
entity_map.stack_slot_offset = Some(create_stack_slots(func, callee));
entity_map.dynamic_type_offset = Some(create_dynamic_types(func, callee, &entity_map));
@@ -1417,7 +1434,11 @@ fn create_blocks(
}
/// Copy and translate global values from the callee into the caller.
-fn create_global_values(func: &mut ir::Function, callee: &ir::Function) -> CodegenResult<u32> {
+fn create_global_values(
+ allocs: &InliningAllocs,
+ func: &mut ir::Function,
+ callee: &ir::Function,
+) -> CodegenResult<u32> {
let gv_offset = func.global_values.len();
let gv_offset = u32::try_from(gv_offset).unwrap();
@@ -1467,11 +1488,25 @@ fn create_global_values(func: &mut ir::Function, callee: &ir::Function) -> Codeg
global_type: *global_type,
},
- // These kinds of global values do not reference other global
- // values, so we can just clone them.
- ir::GlobalValueData::VMContext
- | ir::GlobalValueData::Symbol { .. }
- | ir::GlobalValueData::DynScaleTargetConst { .. } => gv.clone(),
+ // Symbols may reference a user external name, which must be
+ // translated into the caller's user external name table.
+ ir::GlobalValueData::Symbol {
+ name,
+ offset,
+ colocated,
+ tls,
+ } => ir::GlobalValueData::Symbol {
+ name: allocs.inlined_external_name(name),
+ offset: *offset,
+ colocated: *colocated,
+ tls: *tls,
+ },
+
+ // These kinds of global values do not reference other entities,
+ // so we can just clone them.
+ ir::GlobalValueData::VMContext | ir::GlobalValueData::DynScaleTargetConst { .. } => {
+ gv.clone()
+ }
});
}
@@ -1521,17 +1556,7 @@ fn create_func_refs(
} in callee.dfg.ext_funcs.values()
{
func.dfg.ext_funcs.push(ir::ExtFuncData {
- name: match name {
- ir::ExternalName::User(name_ref) => {
- ir::ExternalName::User(allocs.user_external_name_refs[*name_ref].expect(
- "should have translated all `ir::UserExternalNameRef`s before translating \
- `ir::FuncRef`s",
- ))
- }
- ir::ExternalName::TestCase(_)
- | ir::ExternalName::LibCall(_)
- | ir::ExternalName::KnownSymbol(_) => name.clone(),
- },
+ name: allocs.inlined_external_name(name),
signature: entity_map.inlined_sig_ref(*signature),
colocated: *colocated,
patchable: *patchable,
@@ -1561,7 +1586,7 @@ fn create_dynamic_types(
callee: &ir::Function,
entity_map: &EntityMap,
) -> u32 {
- let offset = func.dynamic_stack_slots.len();
+ let offset = func.dfg.dynamic_types.len();
let offset = u32::try_from(offset).unwrap();
func.dfg
diff --git a/cranelift/filetests/filetests/inline/dynamic-stack-slots.clif b/cranelift/filetests/filetests/inline/dynamic-stack-slots.clif
index ac12b842ff..2161c29dbd 100644
--- a/cranelift/filetests/filetests/inline/dynamic-stack-slots.clif
+++ b/cranelift/filetests/filetests/inline/dynamic-stack-slots.clif
@@ -55,3 +55,102 @@ block0:
; return
; }
+;; The caller has more dynamic stack slots than dynamic types.
+
+function %f2() {
+ gv0 = dyn_scale_target_const.i64x2
+ dt0 = i64x2*gv0
+ dss0 = explicit_dynamic_slot dt0
+ dss1 = explicit_dynamic_slot dt0
+ fn0 = %f0(i32)
+block0:
+ v0 = iconst.i64 99
+ v1 = splat.dt0 v0
+ v2 = dynamic_stack_addr.i64 dss0
+ store.dt0 notrap aligned v1, v2
+ v3 = dynamic_stack_addr.i64 dss1
+ store.dt0 notrap aligned v1, v3
+ v4 = iconst.i32 1
+ call fn0(v4)
+ return
+}
+
+; function %f2() fast {
+; dss0 = explicit_dynamic_slot dt0
+; dss1 = explicit_dynamic_slot dt0
+; dss2 = explicit_dynamic_slot dt1
+; gv0 = dyn_scale_target_const.i64x2
+; gv1 = dyn_scale_target_const.i32x4
+; sig0 = (i32) fast
+; fn0 = %f0 sig0
+;
+; block0:
+; v0 = iconst.i64 99
+; v1 = splat.types::I64x2xN v0 ; v0 = 99
+; v2 = dynamic_stack_addr.i64 dss0
+; store notrap aligned v1, v2
+; v3 = dynamic_stack_addr.i64 dss1
+; store notrap aligned v1, v3
+; v4 = iconst.i32 1
+; jump block1
+;
+; block1:
+; v5 = splat.types::I32x4xN v4 ; v4 = 1
+; v6 = dynamic_stack_addr.i64 dss2
+; store notrap aligned v5, v6
+; jump block2
+;
+; block2:
+; return
+; }
+
+;; The caller has more dynamic types than dynamic stack slots.
+
+function %f3() {
+ gv0 = dyn_scale_target_const.i64x2
+ gv1 = dyn_scale_target_const.i8x16
+ dt0 = i64x2*gv0
+ dt1 = i8x16*gv1
+ dss0 = explicit_dynamic_slot dt0
+ fn0 = %f0(i32)
+block0:
+ v0 = iconst.i64 99
+ v1 = splat.dt0 v0
+ v2 = dynamic_stack_addr.i64 dss0
+ store.dt0 notrap aligned v1, v2
+ v3 = iconst.i8 7
+ v4 = splat.dt1 v3
+ v5 = iconst.i32 1
+ call fn0(v5)
+ return
+}
+
+; function %f3() fast {
+; dss0 = explicit_dynamic_slot dt0
+; dss1 = explicit_dynamic_slot dt2
+; gv0 = dyn_scale_target_const.i64x2
+; gv1 = dyn_scale_target_const.i8x16
+; gv2 = dyn_scale_target_const.i32x4
+; sig0 = (i32) fast
+; fn0 = %f0 sig0
+;
+; block0:
+; v0 = iconst.i64 99
+; v1 = splat.types::I64x2xN v0 ; v0 = 99
+; v2 = dynamic_stack_addr.i64 dss0
+; store notrap aligned v1, v2
+; v3 = iconst.i8 7
+; v4 = splat.types::I8x16xN v3 ; v3 = 7
+; v5 = iconst.i32 1
+; jump block1
+;
+; block1:
+; v6 = splat.types::I32x4xN v5 ; v5 = 1
+; v7 = dynamic_stack_addr.i64 dss1
+; store notrap aligned v6, v7
+; jump block2
+;
+; block2:
+; return
+; }
+
diff --git a/cranelift/filetests/filetests/inline/user-external-name-refs.clif b/cranelift/filetests/filetests/inline/user-external-name-refs.clif
index e517a72e75..3c932a6efd 100644
--- a/cranelift/filetests/filetests/inline/user-external-name-refs.clif
+++ b/cranelift/filetests/filetests/inline/user-external-name-refs.clif
@@ -53,3 +53,46 @@ block0(v0: i32):
; return v3
; }
+;; Symbol global values that reference user external names must have those
+;; names translated into the caller's user external name table.
+
+function %symbol_callee() -> i64 {
+ gv0 = symbol colocated u7:7
+block0:
+ v0 = symbol_value.i64 gv0
+ return v0
+}
+
+; (no functions inlined into %symbol_callee)
+
+function %symbol_caller() -> i64 {
+ gv0 = symbol colocated u1:1
+ sig0 = () -> i64
+ fn0 = %symbol_callee sig0
+block0:
+ v0 = symbol_value.i64 gv0
+ v1 = call fn0()
+ v2 = iadd v0, v1
+ return v2
+}
+
+; function %symbol_caller() -> i64 fast {
+; gv0 = symbol colocated userextname0
+; gv1 = symbol colocated userextname1
+; sig0 = () -> i64 fast
+; fn0 = %symbol_callee sig0
+;
+; block0:
+; v0 = symbol_value.i64 gv0
+; jump block1
+;
+; block1:
+; v4 = symbol_value.i64 gv1
+; jump block2(v4)
+;
+; block2(v3: i64):
+; v1 -> v3
+; v2 = iadd.i64 v0, v1
+; return v2
+; }
+