Skip to content

Commit 38b4ffe

Browse files
committed
style:Refactor vector instruction handling in Cranelift backend tests for improved readability
- Reformatted code for creating vector instructions to enhance clarity. - Adjusted formatting of terminator settings for consistency. - Improved readability of function calls by breaking lines for better alignment. - Ensured consistent use of block structure in test functions. - Minor adjustments to comments for clarity. Update ZyntaxRuntime type registration logic for better clarity - Refactored type registration logic to improve readability. - Simplified the existing ID check for registered types. - Enhanced the flow of logic when checking for existing types with fields.
1 parent b3d7bd1 commit 38b4ffe

7 files changed

Lines changed: 801 additions & 394 deletions

File tree

crates/compiler/src/cranelift_backend.rs

Lines changed: 135 additions & 83 deletions
Large diffs are not rendered by default.

crates/compiler/src/hir.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,6 @@ pub enum HirInstruction {
622622
// ========================================================================
623623
// SIMD / Vector Instructions
624624
// ========================================================================
625-
626625
/// Broadcast a scalar value to all lanes of a SIMD vector.
627626
///
628627
/// `ty` must be `HirType::Vector(elem_ty, lanes)`.

crates/compiler/src/hir_dump.rs

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -924,23 +924,63 @@ fn fmt_instruction(inst: &HirInstruction, mapper: &mut IdMapper) -> String {
924924
let s = mapper.value(scalar);
925925
format!("{}: {} = vector_splat {}", r, fmt_type(ty), s)
926926
}
927-
HirInstruction::VectorExtractLane { result, ty, vector, lane } => {
927+
HirInstruction::VectorExtractLane {
928+
result,
929+
ty,
930+
vector,
931+
lane,
932+
} => {
928933
let r = mapper.value(result);
929934
let v = mapper.value(vector);
930-
format!("{}: {} = extract_lane {}, lane {}", r, fmt_type(ty), v, lane)
935+
format!(
936+
"{}: {} = extract_lane {}, lane {}",
937+
r,
938+
fmt_type(ty),
939+
v,
940+
lane
941+
)
931942
}
932-
HirInstruction::VectorInsertLane { result, ty, vector, scalar, lane } => {
943+
HirInstruction::VectorInsertLane {
944+
result,
945+
ty,
946+
vector,
947+
scalar,
948+
lane,
949+
} => {
933950
let r = mapper.value(result);
934951
let v = mapper.value(vector);
935952
let s = mapper.value(scalar);
936-
format!("{}: {} = insert_lane {}, lane {}, {}", r, fmt_type(ty), v, lane, s)
953+
format!(
954+
"{}: {} = insert_lane {}, lane {}, {}",
955+
r,
956+
fmt_type(ty),
957+
v,
958+
lane,
959+
s
960+
)
937961
}
938-
HirInstruction::VectorHorizontalReduce { result, ty, vector, op } => {
962+
HirInstruction::VectorHorizontalReduce {
963+
result,
964+
ty,
965+
vector,
966+
op,
967+
} => {
939968
let r = mapper.value(result);
940969
let v = mapper.value(vector);
941-
format!("{}: {} = hreduce.{} {}", r, fmt_type(ty), fmt_binary_op(op), v)
970+
format!(
971+
"{}: {} = hreduce.{} {}",
972+
r,
973+
fmt_type(ty),
974+
fmt_binary_op(op),
975+
v
976+
)
942977
}
943-
HirInstruction::VectorLoad { result, ty, ptr, align } => {
978+
HirInstruction::VectorLoad {
979+
result,
980+
ty,
981+
ptr,
982+
align,
983+
} => {
944984
let r = mapper.value(result);
945985
let p = mapper.value(ptr);
946986
format!("{}: {} = vload {}, align {}", r, fmt_type(ty), p, align)

crates/compiler/src/lowering.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,16 +1181,17 @@ impl LoweringContext {
11811181
} = &receiver_type
11821182
{
11831183
// Also get the type name for matching extern impls
1184-
let receiver_type_name = self.type_registry
1184+
let receiver_type_name = self
1185+
.type_registry
11851186
.get_type_by_id(*receiver_type_id)
11861187
.map(|td| td.name);
11871188
// Look up the trait implementation
11881189
for (_trait_id, impls) in self.type_registry.iter_implementations() {
11891190
for impl_def in impls {
11901191
let impl_matches = match &impl_def.for_type {
1191-
Type::Named { id: impl_type_id, .. } => {
1192-
*impl_type_id == *receiver_type_id
1193-
}
1192+
Type::Named {
1193+
id: impl_type_id, ..
1194+
} => *impl_type_id == *receiver_type_id,
11941195
Type::Extern { name, .. } => {
11951196
receiver_type_name.map_or(false, |n| n == *name)
11961197
}
@@ -1212,7 +1213,9 @@ impl LoweringContext {
12121213
}
12131214
// Also check inherent methods on the type definition (for extern struct methods)
12141215
if matches!(expr.ty, Type::Any | Type::Unknown) {
1215-
if let Some(type_def) = self.type_registry.get_type_by_id(*receiver_type_id) {
1216+
if let Some(type_def) =
1217+
self.type_registry.get_type_by_id(*receiver_type_id)
1218+
{
12161219
for method in &type_def.methods {
12171220
if method.name == method_call.method {
12181221
expr.ty = method.return_type.clone();
@@ -1221,7 +1224,10 @@ impl LoweringContext {
12211224
}
12221225
}
12231226
}
1224-
} else if let Type::Extern { name: extern_name, .. } = &receiver_type {
1227+
} else if let Type::Extern {
1228+
name: extern_name, ..
1229+
} = &receiver_type
1230+
{
12251231
// Look up methods in impl blocks for extern types (e.g., Tensor)
12261232
// This enables chained method calls like t.reshape([2,3]).transpose()
12271233
for (_trait_id, impls) in self.type_registry.iter_implementations() {
@@ -1243,7 +1249,9 @@ impl LoweringContext {
12431249
}
12441250
// Also check inherent methods on the type definition
12451251
if matches!(expr.ty, Type::Any | Type::Unknown) {
1246-
if let Some(type_def) = self.type_registry.get_type_by_name(*extern_name) {
1252+
if let Some(type_def) =
1253+
self.type_registry.get_type_by_name(*extern_name)
1254+
{
12471255
for method in &type_def.methods {
12481256
if method.name == method_call.method {
12491257
expr.ty = method.return_type.clone();

0 commit comments

Comments
 (0)