Skip to content

Commit 1b2819a

Browse files
committed
Remove cross-type bitcast constant folding rules
These rules fire without knowing the target type, so they also fire on same-type bitcasts (e.g. float→float), polluting e-classes with wrong-type values (IntToExpr in a FloatExpr context). This caused type mismatch errors in the difftests (matrix_ops, vector_extract_insert). Same-type elimination via SameTypeBitcast is retained.
1 parent e2dadb1 commit 1b2819a

2 files changed

Lines changed: 4 additions & 49 deletions

File tree

rust/spirv-tools-opt/src/egglog_opt/mod.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -495,25 +495,6 @@ fn float_neg_bits(a: i64) -> i64 {
495495
}
496496
}
497497

498-
/// Bitcast 32-bit integer constant to f64 (reinterpret lower 32 bits as f32, promote to f64).
499-
fn bitcast_int_to_float32(v: i64) -> f64 {
500-
f32::from_bits(v as u32) as f64
501-
}
502-
503-
/// Bitcast 64-bit integer constant to f64 (reinterpret bits as f64).
504-
fn bitcast_int_to_float64(v: i64) -> f64 {
505-
f64::from_bits(v as u64)
506-
}
507-
508-
/// Bitcast f64 constant to 32-bit integer (demote to f32, get u32 bits).
509-
fn bitcast_float_to_int32(v: f64) -> i64 {
510-
(v as f32).to_bits() as i64
511-
}
512-
513-
/// Bitcast f64 constant to 64-bit integer (get u64 bits).
514-
fn bitcast_float_to_int64(v: f64) -> i64 {
515-
v.to_bits() as i64
516-
}
517498

518499
/// SConvert constant fold: sign-extend or truncate to target width.
519500
fn sconvert_fold(v: i64, dst_width: i64) -> i64 {
@@ -1134,24 +1115,6 @@ pub fn create_spirv_egraph() -> Result<EGraph, EgglogOptError> {
11341115
"uconvert-fold" = |v: i64, sw: i64, dw: i64| -> i64 { uconvert_fold(v, sw, dw) }
11351116
);
11361117

1137-
// Bitcast constant folding primitives (cross int/float reinterpretation)
1138-
add_primitive!(
1139-
&mut egraph,
1140-
"bitcast-i32-to-f" = |v: i64| -> F { F::from(OrderedFloat(bitcast_int_to_float32(v))) }
1141-
);
1142-
add_primitive!(
1143-
&mut egraph,
1144-
"bitcast-i64-to-f" = |v: i64| -> F { F::from(OrderedFloat(bitcast_int_to_float64(v))) }
1145-
);
1146-
add_primitive!(
1147-
&mut egraph,
1148-
"bitcast-f-to-i32" = |v: F| -> i64 { bitcast_float_to_int32(v.0 .0) }
1149-
);
1150-
add_primitive!(
1151-
&mut egraph,
1152-
"bitcast-f-to-i64" = |v: F| -> i64 { bitcast_float_to_int64(v.0 .0) }
1153-
);
1154-
11551118
// Now load the base SPIR-V language and rules (which use the primitives above)
11561119
egraph
11571120
.parse_and_run_program(None, SPIRV_EGGLOG_PROGRAM)

rust/spirv-tools-opt/src/rules/primitives.egg

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -492,18 +492,10 @@
492492
(rule ((= e (BitCast (IntToExpr (Const a)))))
493493
((union e (IntToExpr (Const a)))))
494494

495-
; Cross-type bitcast: reinterpret integer bits as float (32-bit)
496-
(rule ((= e (Bitcast (IntToExpr (Const a))))
497-
(= 32 (ResultWidth (Const a))))
498-
((union e (FloatToExpr (FConst (bitcast-i32-to-f a))))))
499-
500-
; Cross-type bitcast: reinterpret integer bits as float (64-bit)
501-
(rule ((= e (Bitcast (IntToExpr (Const64 a)))))
502-
((union e (FloatToExpr (FConst (bitcast-i64-to-f a))))))
503-
504-
; Cross-type bitcast: reinterpret float bits as integer (32-bit)
505-
(rule ((= e (Bitcast (FloatToExpr (FConst a)))))
506-
((union e (IntToExpr (Const (bitcast-f-to-i32 a))))))
495+
; Cross-type bitcast constant folding is intentionally omitted.
496+
; Without target type info in the egraph, these rules fire for same-type
497+
; bitcasts too, polluting e-classes with wrong-type values (e.g., putting
498+
; IntToExpr in a FloatExpr context). Use SameTypeBitcast for elimination.
507499

508500
; =============================================================================
509501
; SConvert/UConvert Constant Folding

0 commit comments

Comments
 (0)