Skip to content

Commit 34a57d1

Browse files
committed
fix(compiler): GEP byte offset handling and F32 float literals
- Add U8/I8 scalar type handling in Cranelift GEP for byte-level offsets - Add float_literal() method to TypedASTBuilder (defaults to F32) - Add translate_literal_with_type() for type-aware literal conversion - Float literals now correctly use F32 for ML workloads This fixes tensor array construction which was storing F64 values into F32 slots, causing the tensor data to be corrupted.
1 parent eab71ed commit 34a57d1

4 files changed

Lines changed: 41 additions & 7 deletions

File tree

crates/compiler/src/cranelift_backend.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,8 +1162,15 @@ impl CraneliftBackend {
11621162
warn!(" No struct layout found for GEP");
11631163
}
11641164
}
1165+
// For scalar types (U8, I8, etc.), treat as byte-level pointer arithmetic
1166+
// This is used when the SSA layer wants to do direct byte offset calculation
1167+
HirType::U8 | HirType::I8 => {
1168+
// Index is already a byte offset, just add it
1169+
current_ptr = builder.ins().iadd(current_ptr, index);
1170+
// Type stays the same for subsequent indices
1171+
}
11651172
_ => {
1166-
warn!(" GEP on unsupported type");
1173+
warn!(" GEP on unsupported type: {:?}", current_type);
11671174
break;
11681175
}
11691176
}

crates/compiler/src/ssa.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,8 +1330,8 @@ impl SsaBuilder {
13301330
if let TypedLiteral::String(s) = lit {
13311331
Ok(self.create_string_global(*s))
13321332
} else {
1333-
let constant = self.translate_literal(lit);
13341333
let ty = self.convert_type(&expr.ty);
1334+
let constant = self.translate_literal_with_type(lit, &ty);
13351335
Ok(self.create_value(ty, HirValueKind::Constant(constant)))
13361336
}
13371337
}
@@ -3168,16 +3168,27 @@ impl SsaBuilder {
31683168
}
31693169
}
31703170

3171-
/// Translate literal to constant
3171+
/// Translate literal to constant (legacy - always uses default types)
31723172
fn translate_literal(&self, lit: &zyntax_typed_ast::typed_ast::TypedLiteral) -> crate::hir::HirConstant {
3173+
self.translate_literal_with_type(lit, &HirType::I32)
3174+
}
3175+
3176+
/// Translate literal to constant with target type information
3177+
fn translate_literal_with_type(&self, lit: &zyntax_typed_ast::typed_ast::TypedLiteral, target_ty: &HirType) -> crate::hir::HirConstant {
31733178
use zyntax_typed_ast::typed_ast::TypedLiteral;
31743179
use crate::hir::HirConstant;
31753180

31763181
match lit {
31773182
TypedLiteral::Bool(b) => HirConstant::Bool(*b),
31783183
// Use I32 for integers (TypedAST builder defaults to I32)
31793184
TypedLiteral::Integer(i) => HirConstant::I32(*i as i32),
3180-
TypedLiteral::Float(f) => HirConstant::F64(*f),
3185+
// Float literals use the target type (F32 or F64)
3186+
TypedLiteral::Float(f) => {
3187+
match target_ty {
3188+
HirType::F32 => HirConstant::F32(*f as f32),
3189+
_ => HirConstant::F64(*f),
3190+
}
3191+
}
31813192
TypedLiteral::String(s) => HirConstant::String(*s),
31823193
TypedLiteral::Char(c) => HirConstant::I32(*c as i32),
31833194
TypedLiteral::Unit => HirConstant::Struct(vec![]),

crates/typed_ast/src/typed_builder.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ impl TypedASTBuilder {
103103
)
104104
}
105105

106+
/// Build float literal (defaults to F32 for ML-focused usage)
107+
pub fn float_literal(&mut self, value: f64, span: Span) -> TypedNode<TypedExpression> {
108+
typed_node(
109+
TypedExpression::Literal(TypedLiteral::Float(value)),
110+
Type::Primitive(PrimitiveType::F32),
111+
span,
112+
)
113+
}
114+
115+
/// Build F64 float literal
116+
pub fn float64_literal(&mut self, value: f64, span: Span) -> TypedNode<TypedExpression> {
117+
typed_node(
118+
TypedExpression::Literal(TypedLiteral::Float(value)),
119+
Type::Primitive(PrimitiveType::F64),
120+
span,
121+
)
122+
}
123+
106124
/// Build string literal
107125
pub fn string_literal(&mut self, value: &str, span: Span) -> TypedNode<TypedExpression> {
108126
let interned = self.intern(value);

crates/zyn_peg/src/runtime.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,9 +1482,7 @@ impl AstHostFunctions for TypedAstBuilder {
14821482

14831483
fn create_float_literal(&mut self, value: f64) -> NodeHandle {
14841484
let span = self.default_span();
1485-
// TypedASTBuilder doesn't have float_literal, use string_literal workaround for now
1486-
// or we could add a float literal type
1487-
let expr = self.inner.int_literal(value as i128, span);
1485+
let expr = self.inner.float_literal(value, span);
14881486
self.store_expr(expr)
14891487
}
14901488

0 commit comments

Comments
 (0)