Skip to content

Commit b81e14f

Browse files
committed
Same-type bitcast elimination via SameTypeBitcast relation
When OpBitcast's input and output types are identical, the operation is redundant (identity). Detect this during parsing by comparing type IDs and seed a SameTypeBitcast(Expr) relation via additional_facts. A rule in type_conversion.egg then eliminates: Bitcast(x) where SameTypeBitcast → x. This matches the C++ RedundantBitcast optimization. The existing Bitcast(Bitcast(x)) → Bitcast(x) chain collapse rule is preserved.
1 parent 5a5f25f commit b81e14f

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

rust/spirv-tools-opt/src/direct/context.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ impl EgglogContext {
9696
}
9797
}
9898
}
99+
100+
// Detect same-type bitcast for redundant bitcast elimination
101+
if inst.class.opcode == Op::Bitcast {
102+
if let Some(result_type) = inst.result_type {
103+
let src_id = inst.operands.iter().find_map(|op| op.id_ref_any());
104+
if let Some(src_id) = src_id {
105+
if let Some(src_type) = self.id_to_type.get(&src_id) {
106+
if *src_type == result_type {
107+
self.additional_facts.push(format!(
108+
"(SameTypeBitcast id{})",
109+
result_id
110+
));
111+
}
112+
}
113+
}
114+
}
115+
}
99116
}
100117
}
101118
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,12 @@
376376
(function VecSize (Expr) i64 :merge (min old new))
377377

378378
; ResultWidth — tracks the bit width of an integer expression's result type
379-
; Used for SConvert/UConvert constant folding
379+
; Used for SConvert/UConvert constant folding and bitcast constant folding
380380
(function ResultWidth (IntExpr) i64 :merge (min old new))
381381

382+
; SameTypeBitcast — seeded by parser when OpBitcast input/output types are identical
383+
(relation SameTypeBitcast (Expr))
384+
382385
; Vector arithmetic (component-wise)
383386
(constructor VecAdd (Expr Expr) Expr)
384387
(constructor VecSub (Expr Expr) Expr)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
; Bitcast Rules
5555
; =============================================================================
5656

57+
; Same-type bitcast is identity (seeded from parser type checking) - ONE-DIRECTIONAL
58+
(rule ((= e (Bitcast x)) (SameTypeBitcast e))
59+
((union e x)))
60+
5761
; Bitcast of bitcast is bitcast (two reinterpretations)
5862
; Note: In general bitcast(bitcast(x)) may equal x if types align, but
5963
; without type info we can only say it's still a bitcast - ONE-DIRECTIONAL

0 commit comments

Comments
 (0)