Skip to content

Commit

Permalink
Apply global.get const optimization to f32 values (#719)
Browse files Browse the repository at this point in the history
* apply global.get const opt to f32 values

* update docs
  • Loading branch information
Robbepop authored May 23, 2023
1 parent fd0bc97 commit b3544f2
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion crates/wasmi/src/engine/func_builder/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,20 @@ impl<'parser> FuncTranslator<'parser> {
) -> Result<Option<Instruction>, TranslationError> {
if let (Mutability::Const, Some(init_expr)) = (global_type.mutability(), init_value) {
if let Some(value) = init_expr.eval_const() {
// We can optimize `global.get` to the constant value.
if global_type.content() == ValueType::I32 {
return Ok(Some(Instruction::i32_const(i32::from(value))));
}
if global_type.content() == ValueType::F32 {
return Ok(Some(Instruction::f32_const(F32::from(value))));
}
if global_type.content() == ValueType::I64 {
if let Ok(value) = i32::try_from(i64::from(value)) {
return Ok(Some(Instruction::I64Const32(value)));
}
}
// We can optimize `global.get` to the constant value.
// No optimized case was applicable so we have to allocate
// a constant value in the const pool and reference it.
let cref = engine.alloc_const(value)?;
return Ok(Some(Instruction::ConstRef(cref)));
}
Expand Down

0 comments on commit b3544f2

Please sign in to comment.