iter hs.1: static-Str layout migration — packed-struct globals + sentinel rc-header + len

First iter of the heap-Str ABI milestone. Pure codegen refactor; every existing program produces byte-identical stdout.

Static-Str LLVM globals migrate from `[N x i8] c"…\00"` to `<{ i64, i64, [N x i8] }> <{ i64 -1, i64 N-1, [N x i8] c"…\00" }>`, carrying the UINT64_MAX sentinel rc-header (slot exploited in hs.2 via runtime short-circuit) and an explicit byte length. Two parallel intern paths (`intern_string` for raw format scaffolding, new `intern_str_literal` for language Str values) keep format strings untouched in `[N x i8]` shape.

IR-Str pointers now uniformly land on the len-field (offset +8 from rc-header, -8 from bytes). Four codegen sites that hand a Str pointer to a NUL-terminated-bytes C-API consumer now emit a +8 GEP first: `@puts` (io/print_str), `@ail_str_eq` (eq__Str intercept), `@ail_str_compare` (compare__Str intercept), and `@strcmp` (lower_eq Str arm — the 4th site was not in the plan; surfaced by Task-5 e2e regression and fixed inline).

6 new IR-shape pinning tests; `hello.ll` snapshot regenerated; cross_lang.py + compile_check.py + full `cargo test --workspace` green.
This commit is contained in:
2026-05-12 16:48:24 +02:00
parent 69bb5f9952
commit c56498aca7
5 changed files with 532 additions and 12 deletions
+391 -10
View File
@@ -282,6 +282,10 @@ fn lower_workspace_inner(ws: &Workspace, alloc: AllocStrategy) -> Result<String>
let mut body = String::new();
let mut all_strings: BTreeMap<String, Vec<(String, String)>> = BTreeMap::new();
// ^ per module: list of (global-name, content). Order = insertion order.
// Iter hs.1: parallel aggregation for language `Str`-literal globals
// (packed-struct shape). Same map shape; emitted by a parallel loop
// alongside the existing one.
let mut all_str_literals: BTreeMap<String, Vec<(String, String)>> = BTreeMap::new();
// Pass 1: per-module top-level symbol tables.
// - `module_user_fns`: LLVM-typed FnSig for monomorphic fns. Used by
@@ -408,6 +412,13 @@ fn lower_workspace_inner(ws: &Workspace, alloc: AllocStrategy) -> Result<String>
// uses a monotonic counter, so alphabetic is enough).
entries.sort_by(|a, b| a.0.cmp(&b.0));
all_strings.insert(mname.clone(), entries);
// Iter hs.1: parallel collection of `Str`-literal globals.
let mut lit_entries: Vec<(String, String)> = Vec::new();
for (content, (name, _)) in &emitter.str_literals {
lit_entries.push((name.clone(), content.clone()));
}
lit_entries.sort_by(|a, b| a.0.cmp(&b.0));
all_str_literals.insert(mname.clone(), lit_entries);
}
// Trampoline: verify that the entry module has a
@@ -448,6 +459,21 @@ fn lower_workspace_inner(ws: &Workspace, alloc: AllocStrategy) -> Result<String>
emitted_global = true;
}
}
// Iter hs.1: packed-struct globals for language `Str` literals.
// The `i64 -1` slot is the `UINT64_MAX` sentinel rc-header; the
// next `i64` is the byte length (excluding the trailing NUL); the
// `[N+1 x i8]` carries the bytes followed by the terminating NUL.
for entries in all_str_literals.values() {
for (name, content) in entries {
let escaped = ll_string_literal(content);
let total = c_byte_len(content); // bytes + NUL
let bytes_len = total - 1; // bytes only
out.push_str(&format!(
"@{name} = private unnamed_addr constant <{{ i64, i64, [{total} x i8] }}> <{{ i64 -1, i64 {bytes_len}, [{total} x i8] c\"{escaped}\" }}>, align 8\n",
));
emitted_global = true;
}
}
if emitted_global {
out.push('\n');
}
@@ -532,6 +558,13 @@ struct Emitter<'a> {
body: String,
/// String constants: content -> (global name (without `@`), llvm type length incl. \0)
strings: BTreeMap<String, (String, usize)>,
/// Iter hs.1: language `Str` literals interned as packed-struct
/// globals (`<{ i64, i64, [N+1 x i8] }>`) carrying a `UINT64_MAX`
/// sentinel rc-header and an explicit `len` field. Parallel to
/// `strings` (which still serves runtime-internal format strings
/// like `%lld\n` / `true\n` in the raw `[N x i8]` shape). Same
/// key shape; the two tables coexist.
str_literals: BTreeMap<String, (String, usize)>,
/// Local symbol table per function: (name, ssa, llvm_type, ail_type).
/// The AILang type is recorded so that the codegen-side type tracker
/// can derive substitutions at polymorphic call sites without
@@ -751,6 +784,7 @@ impl<'a> Emitter<'a> {
header: String::new(),
body: String::new(),
strings: BTreeMap::new(),
str_literals: BTreeMap::new(),
locals: Vec::new(),
counter: 0,
str_counter: 0,
@@ -888,8 +922,18 @@ impl<'a> Emitter<'a> {
),
Literal::Unit => ("i8".to_string(), "0".to_string()),
Literal::Str { value } => {
let g = self.intern_string("str", value);
("ptr".to_string(), format!("@{g}"))
// Iter hs.1: emit a packed-struct global and return a
// constexpr-GEP pointer landing on the `len`-field, so
// every IR-Str pointer in this codegen pipeline has
// the same shape (header at -8, len at 0, bytes at +8).
let g = self.intern_str_literal("str", value);
let total = c_byte_len(value); // bytes + NUL
(
"ptr".to_string(),
format!(
"getelementptr inbounds (<{{ i64, i64, [{total} x i8] }}>, ptr @{g}, i32 0, i32 1)",
),
)
}
Literal::Float { bits } => ("double".to_string(), format!("0x{:016X}", bits)),
};
@@ -1200,10 +1244,18 @@ impl<'a> Emitter<'a> {
"i1".into(),
),
Literal::Str { value } => {
// Create global constant; in opaque-pointer LLVM,
// `@name` is directly a valid `ptr`.
let g = self.intern_string("str", value);
(format!("@{g}"), "ptr".into())
// Iter hs.1: language `Str` literals materialise as
// a constexpr-GEP into the packed-struct global,
// landing on the `len`-field so the IR-Str
// pointer carries header at -8 and bytes at +8.
let g = self.intern_str_literal("str", value);
let total = c_byte_len(value); // bytes + NUL
(
format!(
"getelementptr inbounds (<{{ i64, i64, [{total} x i8] }}>, ptr @{g}, i32 0, i32 1)",
),
"ptr".into(),
)
}
Literal::Unit => ("0".into(), "i8".into()),
Literal::Float { bits } => (format!("0x{:016X}", bits), "double".into()),
@@ -2161,8 +2213,15 @@ impl<'a> Emitter<'a> {
"io/print_str needs ptr".into(),
));
}
// Iter hs.1: `Str` values now flow as a pointer to the
// `len`-field of the packed-struct slab; @puts needs
// the bytes pointer 8 bytes further on.
let bytes = self.fresh_ssa();
self.body.push_str(&format!(
" {bytes} = getelementptr inbounds i8, ptr {v}, i64 8\n"
));
self.body
.push_str(&format!(" {call_kw} i32 @puts(ptr {v})\n"));
.push_str(&format!(" {call_kw} i32 @puts(ptr {bytes})\n"));
if tail {
self.body.push_str(" ret i8 0\n");
self.block_terminated = true;
@@ -2258,9 +2317,21 @@ impl<'a> Emitter<'a> {
let n = self.locals.len();
let a_ssa = self.locals[n - 2].1.clone();
let b_ssa = self.locals[n - 1].1.clone();
// Iter hs.1: IR-Str pointers now land on the
// `len`-field of the packed-struct slab; @ail_str_eq's
// strcmp-based body needs the bytes pointer 8 bytes
// further on.
let a_bytes = self.fresh_ssa();
let b_bytes = self.fresh_ssa();
self.body.push_str(&format!(
" {a_bytes} = getelementptr inbounds i8, ptr {a_ssa}, i64 8\n"
));
self.body.push_str(&format!(
" {b_bytes} = getelementptr inbounds i8, ptr {b_ssa}, i64 8\n"
));
let dst = self.fresh_ssa();
self.body.push_str(&format!(
" {dst} = call zeroext i1 @ail_str_eq(ptr {a_ssa}, ptr {b_ssa})\n"
" {dst} = call zeroext i1 @ail_str_eq(ptr {a_bytes}, ptr {b_bytes})\n"
));
self.body.push_str(&format!(" ret i1 {dst}\n"));
self.body.push_str("}\n\n");
@@ -2309,9 +2380,21 @@ impl<'a> Emitter<'a> {
let n = self.locals.len();
let a_ssa = self.locals[n - 2].1.clone();
let b_ssa = self.locals[n - 1].1.clone();
// Iter hs.1: IR-Str pointers now land on the
// `len`-field of the packed-struct slab; @ail_str_compare's
// strcmp-based body needs the bytes pointer 8 bytes
// further on.
let a_bytes = self.fresh_ssa();
let b_bytes = self.fresh_ssa();
self.body.push_str(&format!(
" {a_bytes} = getelementptr inbounds i8, ptr {a_ssa}, i64 8\n"
));
self.body.push_str(&format!(
" {b_bytes} = getelementptr inbounds i8, ptr {b_ssa}, i64 8\n"
));
let cmp_res = self.fresh_ssa();
self.body.push_str(&format!(
" {cmp_res} = call i32 @ail_str_compare(ptr {a_ssa}, ptr {b_ssa})\n"
" {cmp_res} = call i32 @ail_str_compare(ptr {a_bytes}, ptr {b_bytes})\n"
));
self.emit_compare_ladder(
&format!("icmp slt i32 {cmp_res}, 0"),
@@ -2435,9 +2518,22 @@ impl<'a> Emitter<'a> {
Ok((dst, "i1".into()))
}
"Str" => {
// Iter hs.1: IR-Str pointers now land on the
// `len`-field of the packed-struct slab; @strcmp
// needs the bytes pointer 8 bytes further on.
// Parallel to the `eq__Str` and `compare__Str`
// intercepts in `try_emit_primitive_instance_body`.
let a_bytes = self.fresh_ssa();
let b_bytes = self.fresh_ssa();
self.body.push_str(&format!(
" {a_bytes} = getelementptr inbounds i8, ptr {a}, i64 8\n"
));
self.body.push_str(&format!(
" {b_bytes} = getelementptr inbounds i8, ptr {b}, i64 8\n"
));
let cmp = self.fresh_ssa();
self.body.push_str(&format!(
" {cmp} = call i32 @strcmp(ptr {a}, ptr {b})\n"
" {cmp} = call i32 @strcmp(ptr {a_bytes}, ptr {b_bytes})\n"
));
let dst = self.fresh_ssa();
self.body.push_str(&format!(
@@ -2497,6 +2593,23 @@ impl<'a> Emitter<'a> {
name
}
/// Iter hs.1: parallel to `intern_string`, but for language `Str`
/// literals emitted as packed-struct globals (sentinel + len +
/// bytes + NUL). Shares the same monotonic `str_counter` so the
/// produced global names remain alphabetically orderable
/// alongside format-string globals.
fn intern_str_literal(&mut self, hint: &str, content: &str) -> String {
if let Some((name, _)) = self.str_literals.get(content) {
return name.clone();
}
let name = format!(".str_{}_{}_{}", self.module_name, hint, self.str_counter);
self.str_counter += 1;
let len = c_byte_len(content);
self.str_literals
.insert(content.to_string(), (name.clone(), len));
name
}
/// Iter 12b: lightweight AILang-type computation for an expression
/// in the current scope. Mirrors what the typechecker already
/// derived; we replay it here only because the typechecker doesn't
@@ -3693,4 +3806,272 @@ mod tests {
defs: vec![ordering, compare, main_def],
}
}
/// Iter hs.1: language `Str` literals emit as packed-struct
/// globals carrying a `UINT64_MAX` sentinel rc-header, an
/// explicit `len` field, and the bytes + trailing NUL. This
/// test pins the layout shape against a tiny single-Literal::Str
/// fixture.
#[test]
fn static_str_global_uses_packed_struct_with_sentinel_and_len() {
let m = Module {
schema: SCHEMA.into(),
name: "t".into(),
imports: vec![],
defs: vec![
Def::Const(ConstDef {
name: "greeting".into(),
ty: Type::str_(),
value: Term::Lit { lit: Literal::Str { value: "hello".into() } },
doc: None,
}),
Def::Fn(FnDef {
name: "main".into(),
ty: Type::Fn {
params: vec![],
ret: Box::new(Type::unit()),
effects: vec![],
param_modes: vec![],
ret_mode: ParamMode::Implicit,
},
params: vec![],
body: Term::Lit { lit: Literal::Unit },
suppress: vec![],
doc: None,
}),
],
};
let ir = emit_ir(&m).unwrap();
assert!(
ir.contains(r#"<{ i64, i64, [6 x i8] }> <{ i64 -1, i64 5, [6 x i8] c"hello\00" }>"#),
"expected packed-struct global with sentinel + len + bytes + NUL; ir was:\n{ir}"
);
}
/// Iter hs.1: a `Literal::Str` at a callsite materialises a
/// constexpr `getelementptr` landing on the `len`-field of the
/// packed-struct global, not the bare global name. This pins the
/// IR-Str-pointer convention used uniformly by all consumers.
#[test]
fn static_str_callsite_pointer_is_payload_via_constexpr_gep() {
let m = Module {
schema: SCHEMA.into(),
name: "t".into(),
imports: vec![],
defs: vec![Def::Fn(FnDef {
name: "main".into(),
ty: Type::Fn {
params: vec![],
ret: Box::new(Type::unit()),
effects: vec!["IO".into()],
param_modes: vec![],
ret_mode: ParamMode::Implicit,
},
params: vec![],
body: Term::Do {
op: "io/print_str".into(),
args: vec![Term::Lit { lit: Literal::Str { value: "hi".into() } }],
tail: false,
},
suppress: vec![],
doc: None,
})],
};
let ir = emit_ir(&m).unwrap();
assert!(
ir.contains(r#"getelementptr inbounds (<{ i64, i64, [3 x i8] }>, ptr @.str_t_str_0, i32 0, i32 1)"#),
"expected constexpr-GEP-to-len-field at callsite; ir was:\n{ir}"
);
}
/// Iter hs.1: after the layout migration, the `io/print_str`
/// path must `getelementptr i8` +8 onto the IR-Str pointer before
/// passing it to `@puts`, so `@puts` receives the bytes pointer
/// (skipping the `len` field) and produces correct output.
#[test]
fn print_str_calls_puts_with_bytes_pointer() {
let m = Module {
schema: SCHEMA.into(),
name: "t".into(),
imports: vec![],
defs: vec![Def::Fn(FnDef {
name: "main".into(),
ty: Type::Fn {
params: vec![],
ret: Box::new(Type::unit()),
effects: vec!["IO".into()],
param_modes: vec![],
ret_mode: ParamMode::Implicit,
},
params: vec![],
body: Term::Do {
op: "io/print_str".into(),
args: vec![Term::Lit { lit: Literal::Str { value: "hi".into() } }],
tail: false,
},
suppress: vec![],
doc: None,
})],
};
let ir = emit_ir(&m).unwrap();
let body_idx = ir.find("define i8 @ail_t_main").expect("main body");
let body = &ir[body_idx..];
let puts_idx = body.find("@puts(").expect("@puts call present");
let before_puts = &body[..puts_idx];
assert!(
before_puts.contains("getelementptr inbounds i8, ptr ") && before_puts.contains(", i64 8"),
"expected `getelementptr inbounds i8, ptr <v>, i64 8` before @puts call; ir body was:\n{body}"
);
}
/// Iter hs.1: `eq__Str`'s body must `getelementptr i8` +8 on
/// both operand pointers before calling `@ail_str_eq`, since the
/// IR-Str pointer now lands on the `len`-field, not the bytes.
#[test]
fn eq_str_calls_ail_str_eq_with_bytes_pointer() {
let m = Module {
schema: SCHEMA.into(),
name: "prelude".into(),
imports: vec![],
defs: vec![
Def::Fn(FnDef {
name: "eq__Str".into(),
ty: Type::Fn {
params: vec![Type::str_(), Type::str_()],
ret: Box::new(Type::bool_()),
effects: vec![],
param_modes: vec![ParamMode::Borrow, ParamMode::Borrow],
ret_mode: ParamMode::Implicit,
},
params: vec!["x".into(), "y".into()],
body: Term::Lit { lit: Literal::Bool { value: false } },
suppress: vec![],
doc: None,
}),
Def::Fn(FnDef {
name: "main".into(),
ty: Type::Fn {
params: vec![],
ret: Box::new(Type::unit()),
effects: vec![],
param_modes: vec![],
ret_mode: ParamMode::Implicit,
},
params: vec![],
body: Term::Lit { lit: Literal::Unit },
suppress: vec![],
doc: None,
}),
],
};
let ir = emit_ir(&m).unwrap();
let body_idx = ir.find("define i1 @ail_prelude_eq__Str").expect("eq__Str body");
let body = &ir[body_idx..];
let eq_idx = body.find("@ail_str_eq(").expect("@ail_str_eq call present");
let before_eq = &body[..eq_idx];
// Two GEPs (one per operand) must precede the call.
let gep_count = before_eq.matches("getelementptr inbounds i8, ptr ").count();
assert_eq!(
gep_count, 2,
"expected 2 +8 GEPs before @ail_str_eq (one per operand); ir body was:\n{body}"
);
assert!(
before_eq.matches(", i64 8").count() >= 2,
"expected both GEPs to be `, i64 8`; ir body was:\n{body}"
);
}
/// Iter hs.1: `compare__Str`'s body must `getelementptr i8` +8
/// on both operand pointers before calling `@ail_str_compare`,
/// symmetric to the `eq__Str` change.
#[test]
fn compare_str_calls_ail_str_compare_with_bytes_pointer() {
let m = synth_compare_module("compare__Str", Type::str_());
let ir = emit_ir(&m).unwrap();
let body_idx = ir
.find("define ptr @ail_prelude_compare__Str")
.expect("compare__Str body");
let body = &ir[body_idx..];
let cmp_idx = body
.find("@ail_str_compare(")
.expect("@ail_str_compare call present");
let before_cmp = &body[..cmp_idx];
let gep_count = before_cmp.matches("getelementptr inbounds i8, ptr ").count();
assert_eq!(
gep_count, 2,
"expected 2 +8 GEPs before @ail_str_compare; ir body was:\n{body}"
);
assert!(
before_cmp.matches(", i64 8").count() >= 2,
"expected both GEPs to be `, i64 8`; ir body was:\n{body}"
);
}
/// Iter hs.1: the `==` operator on `Str` lowers via an inline
/// `@strcmp` call (separate from the `eq__Str` instance-method
/// intercept used by the dictionary path). Both operand pointers
/// must be `+8` GEP'd to land on the bytes pointer before
/// `@strcmp`, symmetric to the `eq__Str` and `compare__Str`
/// fixes. Without this, `==` on Str literals reads the 8-byte
/// `len`-field as bytes and never finds the NUL terminator
/// within the expected range, breaking string equality.
#[test]
fn lower_eq_str_calls_strcmp_with_bytes_pointer() {
let m = Module {
schema: SCHEMA.into(),
name: "t".into(),
imports: vec![],
defs: vec![
Def::Fn(FnDef {
name: "test".into(),
ty: Type::Fn {
params: vec![],
ret: Box::new(Type::bool_()),
effects: vec![],
param_modes: vec![],
ret_mode: ParamMode::Implicit,
},
params: vec![],
body: Term::App {
callee: Box::new(Term::Var { name: "==".into() }),
args: vec![
Term::Lit { lit: Literal::Str { value: "a".into() } },
Term::Lit { lit: Literal::Str { value: "b".into() } },
],
tail: false,
},
suppress: vec![],
doc: None,
}),
Def::Fn(FnDef {
name: "main".into(),
ty: Type::Fn {
params: vec![],
ret: Box::new(Type::unit()),
effects: vec![],
param_modes: vec![],
ret_mode: ParamMode::Implicit,
},
params: vec![],
body: Term::Lit { lit: Literal::Unit },
suppress: vec![],
doc: None,
}),
],
};
let ir = emit_ir(&m).unwrap();
let body_idx = ir.find("define i1 @ail_t_test").expect("test body");
let body = &ir[body_idx..];
let cmp_idx = body.find("@strcmp(").expect("@strcmp call present");
let before_cmp = &body[..cmp_idx];
let gep_count = before_cmp.matches("getelementptr inbounds i8, ptr ").count();
assert_eq!(
gep_count, 2,
"expected 2 +8 GEPs before @strcmp (one per operand); ir body was:\n{body}"
);
assert!(
before_cmp.matches(", i64 8").count() >= 2,
"expected both GEPs to be `, i64 8`; ir body was:\n{body}"
);
}
}