Skip to content

Commit a51f4f8

Browse files
authored
Change the ABI for "join" on variants (#199)
This commit changes the ABI for variants such as `union { f32, f64 }` which in the prior version of the canonical ABI would look like `i32 f64` but with the current canonical ABI draft it would instead look like `i32 i64`. The previous `unify` function is updated to the new name used in the canonical ABI explainer, `join`, the contents are modified, and one form of bitcast was removed as a result. Additionally I updated the bless'd output for `wasmlink` to ensure everything has newlines at the end since I was getting some otherwise odd changes locally. This is hopefully a one-time change for those baselines.
1 parent 1eccdd8 commit a51f4f8

File tree

12 files changed

+21
-26
lines changed

12 files changed

+21
-26
lines changed

crates/gen-c/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,6 @@ impl Bindgen for FunctionBindgen<'_> {
13351335
results
13361336
.push(format!("((union {{ float a; int32_t b; }}){{ {} }}).b", op));
13371337
}
1338-
Bitcast::F32ToF64 | Bitcast::F64ToF32 => results.push(op.to_string()),
13391338
Bitcast::I64ToF64 => {
13401339
results.push(format!(
13411340
"((union {{ int64_t a; double b; }}){{ {} }}).b",

crates/gen-js/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,6 @@ impl Bindgen for FunctionBindgen<'_> {
12971297
let cvt = self.gen.intrinsic(Intrinsic::F32ToI32);
12981298
results.push(format!("{}({})", cvt, op));
12991299
}
1300-
Bitcast::F32ToF64 | Bitcast::F64ToF32 => results.push(op.clone()),
13011300
Bitcast::I64ToF64 => {
13021301
let cvt = self.gen.intrinsic(Intrinsic::I64ToF64);
13031302
results.push(format!("{}({})", cvt, op));

crates/gen-rust/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,11 +956,9 @@ pub fn bitcast(casts: &[Bitcast], operands: &[String], results: &mut Vec<String>
956956
for (cast, operand) in casts.iter().zip(operands) {
957957
results.push(match cast {
958958
Bitcast::None => operand.clone(),
959-
Bitcast::F32ToF64 => format!("f64::from({})", operand),
960959
Bitcast::I32ToI64 => format!("i64::from({})", operand),
961960
Bitcast::F32ToI32 => format!("({}).to_bits() as i32", operand),
962961
Bitcast::F64ToI64 => format!("({}).to_bits() as i64", operand),
963-
Bitcast::F64ToF32 => format!("{} as f32", operand),
964962
Bitcast::I64ToI32 => format!("{} as i32", operand),
965963
Bitcast::I32ToF32 => format!("f32::from_bits({} as u32)", operand),
966964
Bitcast::I64ToF64 => format!("f64::from_bits({} as u64)", operand),

crates/gen-wasmtime-py/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,6 @@ impl Bindgen for FunctionBindgen<'_> {
14021402
self.gen.needs_f32_to_i32 = true;
14031403
results.push(format!("_f32_to_i32({})", op));
14041404
}
1405-
Bitcast::F32ToF64 | Bitcast::F64ToF32 => results.push(op.clone()),
14061405
Bitcast::I64ToF64 => {
14071406
self.gen.needs_i64_to_f64 = true;
14081407
results.push(format!("_i64_to_f64({})", op));

crates/parser/src/abi.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ pub enum WasmType {
2626
// e.g. externref, so we don't need to define them here.
2727
}
2828

29-
fn unify(a: WasmType, b: WasmType) -> WasmType {
29+
fn join(a: WasmType, b: WasmType) -> WasmType {
3030
use WasmType::*;
3131

3232
match (a, b) {
33-
(I64, _) | (_, I64) | (I32, F64) | (F64, I32) => I64,
33+
(I32, I32) | (I64, I64) | (F32, F32) | (F64, F64) => a,
3434

35-
(I32, I32) | (I32, F32) | (F32, I32) => I32,
35+
(I32, F32) | (F32, I32) => I32,
3636

37-
(F32, F32) => F32,
38-
(F64, F64) | (F32, F64) | (F64, F32) => F64,
37+
(_, I64 | F64) | (I64 | F64, _) => I64,
3938
}
4039
}
4140

@@ -638,14 +637,12 @@ def_instruction! {
638637
#[derive(Debug, PartialEq)]
639638
pub enum Bitcast {
640639
// Upcasts
641-
F32ToF64,
642640
F32ToI32,
643641
F64ToI64,
644642
I32ToI64,
645643
F32ToI64,
646644

647645
// Downcasts
648-
F64ToF32,
649646
I32ToF32,
650647
I64ToF64,
651648
I64ToI32,
@@ -893,7 +890,7 @@ impl Interface {
893890

894891
for (i, ty) in temp.drain(..).enumerate() {
895892
match result.get_mut(start + i) {
896-
Some(prev) => *prev = unify(*prev, ty),
893+
Some(prev) => *prev = join(*prev, ty),
897894
None => result.push(ty),
898895
}
899896
}
@@ -1846,17 +1843,16 @@ fn cast(from: WasmType, to: WasmType) -> Bitcast {
18461843
(I32, I32) | (I64, I64) | (F32, F32) | (F64, F64) => Bitcast::None,
18471844

18481845
(I32, I64) => Bitcast::I32ToI64,
1849-
(F32, F64) => Bitcast::F32ToF64,
18501846
(F32, I32) => Bitcast::F32ToI32,
18511847
(F64, I64) => Bitcast::F64ToI64,
18521848

18531849
(I64, I32) => Bitcast::I64ToI32,
1854-
(F64, F32) => Bitcast::F64ToF32,
18551850
(I32, F32) => Bitcast::I32ToF32,
18561851
(I64, F64) => Bitcast::I64ToF64,
18571852

18581853
(F32, I64) => Bitcast::F32ToI64,
18591854
(I64, F32) => Bitcast::I64ToF32,
1860-
(F64, I32) | (I32, F64) => unreachable!(),
1855+
1856+
(F32, F64) | (F64, F32) | (F64, I32) | (I32, F64) => unreachable!(),
18611857
}
18621858
}

crates/wasmlink/tests/no-interface.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
(type (;0;) (func (param i32 i64 f32 f64) (result i32)))
33
(func (;0;) (type 0) (param i32 i64 f32 f64) (result i32)
44
unreachable)
5-
(export "f1" (func 0)))
5+
(export "f1" (func 0)))

crates/wasmlink/tests/not-adapted.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
(type (;0;) (func (param i32 i64 f32 f64) (result i32)))
33
(func (;0;) (type 0) (param i32 i64 f32 f64) (result i32)
44
unreachable)
5-
(export "f1" (func 0)))
5+
(export "f1" (func 0)))

crates/wasmlink/tests/retptr.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
i64.load (memory 1) offset=8
2828
i64.store offset=8)
2929
(export "memory" (memory 1))
30-
(export "f1" (func 1)))
30+
(export "f1" (func 1)))

crates/wasmlink/tests/run.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ fn wasmlink_file_tests() -> Result<()> {
3939
let mut wit_path = path.clone();
4040
assert!(wit_path.set_extension("wit"));
4141

42-
let output = match adapt(stem, &bytes, &wit_path) {
42+
let mut output = match adapt(stem, &bytes, &wit_path) {
4343
Ok(adapted) => print_bytes(&adapted.finish())?,
4444
Err(e) => e.to_string(),
4545
};
4646

4747
let baseline_path = path.with_extension("baseline");
4848
if env::var_os("BLESS").is_some() {
49+
if !output.ends_with("\n") {
50+
output.push_str("\n");
51+
}
52+
4953
fs::write(&baseline_path, output)?;
5054
} else {
5155
let expected = fs::read_to_string(&baseline_path)

crates/wasmlink/tests/string.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,4 @@
8383
(export "memory" (memory 1))
8484
(export "canonical_abi_realloc" (func 1))
8585
(export "canonical_abi_free" (func 2))
86-
(export "f1" (func 4)))
86+
(export "f1" (func 4)))

0 commit comments

Comments
 (0)