plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
b517517
— Peter John
2026-11-10T11:39:09+05:30
Uniqueness: compile time (FKdTree's in-place pass 5 s -> 0.4-0.9 s release)
- compiler/core/src/drive.rs +1 -1
- compiler/core/src/uniqueness.rs +107 -34
- compiler/driver/src/lib.rs +8 -4
compiler/core/src/drive.rs
CHANGED
|
@@ -624,7 +624,7 @@ impl Driver {
|
|
|
624
624
|
let mut body = self.fused[i].body.clone();
|
|
625
625
|
self.bound = boundNames(&self.fused[i].params, &body);
|
|
626
626
|
self.vtypes = self.varTypes(&self.fused[i]);
|
|
627
|
-
self.scope = self.scopeOf(&self.fused[i]
|
|
627
|
+
self.scope = self.scopeOf(&self.fused[i]);
|
|
628
628
|
self.cur = self.origins.get(&self.fused[i].name).cloned().unwrap_or_default();
|
|
629
629
|
self.specLeft = SPEC_BUDGET;
|
|
630
630
|
self.rewriteBody(&mut body);
|
compiler/core/src/uniqueness.rs
CHANGED
|
@@ -830,7 +830,7 @@ impl An {
|
|
|
830
830
|
return Err(why.unwrap_or_else(|| format!("a caller of `{}` may pass a shared `{v}`", u.name)));
|
|
831
831
|
}
|
|
832
832
|
}
|
|
833
|
-
let mut s = Scan { an: self, unit, v, strict, boundDepth: if param.is_some() { Some(0) } else { None }, err: None };
|
|
833
|
+
let mut s = Scan { an: self, unit, v, strict, boundDepth: if param.is_some() { Some(0) } else { None }, err: None, mentioned: RefCell::new(HashMap::new()) };
|
|
834
834
|
s.block(&u.block, true, false, 0);
|
|
835
835
|
if let Some(e) = s.err {
|
|
836
836
|
return Err(e);
|
|
@@ -1030,6 +1030,9 @@ struct Scan<'a> {
|
|
|
1030
1030
|
strict: bool,
|
|
1031
1031
|
boundDepth: Option<usize>,
|
|
1032
1032
|
err: Option<String>,
|
|
1033
|
+
/// `mentionsStmt` per statement (by address): a nested statement is asked again at each
|
|
1034
|
+
/// level above it.
|
|
1035
|
+
mentioned: RefCell<HashMap<*const Stmt, bool>>,
|
|
1033
1036
|
}
|
|
1034
1037
|
|
|
1035
1038
|
impl<'a> Scan<'a> {
|
|
@@ -1058,18 +1061,37 @@ impl<'a> Scan<'a> {
|
|
|
1058
1061
|
}
|
|
1059
1062
|
|
|
1060
1063
|
fn mentionsStmts(&self, ss: &[Stmt]) -> bool {
|
|
1061
|
-
let mut found = false;
|
|
1062
|
-
for s in ss {
|
|
1063
|
-
walkStmt(s, &mut |n| {
|
|
1064
|
-
if let Node::E(x) = n {
|
|
1065
|
-
|
|
1064
|
+
ss.iter().any(|s| self.mentionsStmt(s))
|
|
1066
|
-
found = true;
|
|
1067
|
-
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
/// Whether `s` mentions the variable (as `walkStmt` visits it): its own expressions, then
|
|
1068
|
+
/// its blocks' statements, each answered once.
|
|
1069
|
+
fn mentionsStmt(&self, s: &Stmt) -> bool {
|
|
1068
|
-
|
|
1070
|
+
let key = s as *const Stmt;
|
|
1071
|
+
if let Some(&m) = self.mentioned.borrow().get(&key) {
|
|
1069
|
-
|
|
1072
|
+
return m;
|
|
1070
|
-
});
|
|
1071
1073
|
}
|
|
1074
|
+
let m = match &s.kind {
|
|
1075
|
+
StmtKind::Assign(a) => {
|
|
1076
|
+
a.targets.iter().any(|t| matches!(t, AssignTarget::Field(o, _) if self.mentions(o))) || a.values.iter().any(|v| self.mentions(v))
|
|
1077
|
+
}
|
|
1078
|
+
StmtKind::Assert(c) => self.mentions(&c.cond),
|
|
1079
|
+
StmtKind::While(w) => self.mentions(&w.condition) || self.mentionsStmts(&w.body.stmts),
|
|
1080
|
+
StmtKind::If(i) => {
|
|
1081
|
+
self.mentions(&i.condition)
|
|
1082
|
+
|| self.mentionsStmts(&i.body.stmts)
|
|
1083
|
+
|| i.else_ifs.iter().any(|e| self.mentions(&e.condition) || self.mentionsStmts(&e.body.stmts))
|
|
1084
|
+
|| i.else_.as_ref().is_some_and(|b| self.mentionsStmts(&b.stmts))
|
|
1085
|
+
}
|
|
1086
|
+
StmtKind::Match(m) => {
|
|
1087
|
+
m.subjects.iter().any(|e| self.mentions(e))
|
|
1088
|
+
|| m.cases.iter().any(|c| c.guard.as_ref().is_some_and(|g| self.mentions(g)) || self.mentionsStmts(&c.body.stmts))
|
|
1089
|
+
}
|
|
1090
|
+
StmtKind::Return(Some(e)) | StmtKind::Crash(e) | StmtKind::Expr(e, _, _) => self.mentions(e),
|
|
1091
|
+
StmtKind::Return(None) | StmtKind::Todo => false,
|
|
1072
|
-
|
|
1092
|
+
};
|
|
1093
|
+
self.mentioned.borrow_mut().insert(key, m);
|
|
1094
|
+
m
|
|
1073
1095
|
}
|
|
1074
1096
|
|
|
1075
1097
|
fn mentionsBlock(&self, b: &Block) -> bool {
|
|
@@ -1088,9 +1110,13 @@ impl<'a> Scan<'a> {
|
|
|
1088
1110
|
/// `live`: the variable is mentioned after the block, in the order things run.
|
|
1089
1111
|
fn block(&mut self, b: &Block, tail: bool, live: bool, depth: usize) {
|
|
1090
1112
|
let n = b.stmts.len();
|
|
1113
|
+
// later[i]: a statement after the i-th mentions the variable (one pass from the end)
|
|
1114
|
+
let mut later = vec![false; n];
|
|
1115
|
+
for i in (0..n.saturating_sub(1)).rev() {
|
|
1116
|
+
later[i] = later[i + 1] || self.mentionsStmts(std::slice::from_ref(&b.stmts[i + 1]));
|
|
1117
|
+
}
|
|
1091
1118
|
for (i, s) in b.stmts.iter().enumerate() {
|
|
1092
|
-
let later = self.mentionsStmts(&b.stmts[i + 1..]);
|
|
1093
|
-
self.stmt(s, tail && i + 1 == n, live || later, depth);
|
|
1119
|
+
self.stmt(s, tail && i + 1 == n, live || later[i], depth);
|
|
1094
1120
|
}
|
|
1095
1121
|
}
|
|
1096
1122
|
|
|
@@ -1460,9 +1486,13 @@ fn fieldsOf(enums: &HashMap<String, Vec<String>>, t: &str) -> Option<Vec<String>
|
|
|
1460
1486
|
pub fn markUnique(src: &mut Source) -> UniqueReport {
|
|
1461
1487
|
// The analysis tables are keyed by node id; simplify and drive leave new nodes unnumbered.
|
|
1462
1488
|
numberIds(src);
|
|
1463
|
-
let versions = versionCallers(src);
|
|
1489
|
+
let (versions, solved) = versionCallers(src);
|
|
1490
|
+
// The versions' last analysis, when the program has not changed since.
|
|
1491
|
+
let an = solved.unwrap_or_else(|| {
|
|
1464
|
-
|
|
1492
|
+
let an = build(src);
|
|
1465
|
-
|
|
1493
|
+
solve(&an);
|
|
1494
|
+
an
|
|
1495
|
+
});
|
|
1466
1496
|
// Report and mark.
|
|
1467
1497
|
let mut report = UniqueReport { versions, ..UniqueReport::default() };
|
|
1468
1498
|
let isFip = |ui: usize| matches!(&src.items[an.units[ui].itemIdx], Item::Fn(f) if f.is_fip);
|
|
@@ -1554,18 +1584,35 @@ fn lineOf(src: &Source, u: &Unit) -> usize {
|
|
|
1554
1584
|
}
|
|
1555
1585
|
}
|
|
1556
1586
|
|
|
1557
|
-
///
|
|
1587
|
+
/// What `typeParams` reads off the program's types, computed once: each type's generics, and
|
|
1588
|
+
/// the single-letter field types of every variant.
|
|
1589
|
+
struct TypeParamInfo {
|
|
1590
|
+
gens: HashMap<String, Vec<String>>,
|
|
1591
|
+
letters: HashSet<String>,
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
impl TypeParamInfo {
|
|
1558
|
-
fn
|
|
1595
|
+
fn of(items: &[Item]) -> TypeParamInfo {
|
|
1559
|
-
|
|
1596
|
+
let mut gens: HashMap<String, Vec<String>> = HashMap::new();
|
|
1597
|
+
let mut letters = HashSet::new();
|
|
1560
|
-
|
|
1598
|
+
for item in items {
|
|
1561
|
-
|
|
1599
|
+
if let Item::Enum(e) = item {
|
|
1562
|
-
if f.type_param.as_deref() == Some(e.name.as_str()) {
|
|
1563
|
-
|
|
1600
|
+
gens.entry(e.name.clone()).or_default().extend(e.generics.iter().map(|g| g.name.clone()));
|
|
1601
|
+
letters.extend(e.variants.iter().flat_map(|v| v.fields.iter()).filter(|t| t.name.len() == 1).map(|t| t.name.clone()));
|
|
1564
1602
|
}
|
|
1565
|
-
out.extend(e.variants.iter().flat_map(|v| v.fields.iter()).filter(|t| t.name.len() == 1).map(|t| t.name.clone()));
|
|
1566
1603
|
}
|
|
1604
|
+
TypeParamInfo { gens, letters }
|
|
1605
|
+
}
|
|
1606
|
+
|
|
1607
|
+
/// The names that mean a type parameter in `f`: its own, its type's, and any single letter.
|
|
1608
|
+
fn typeParams(&self, f: &Fn) -> HashSet<String> {
|
|
1609
|
+
let mut out: HashSet<String> = f.generics.iter().cloned().collect();
|
|
1610
|
+
if let Some(g) = f.type_param.as_ref().and_then(|t| self.gens.get(t)) {
|
|
1611
|
+
out.extend(g.iter().cloned());
|
|
1612
|
+
}
|
|
1613
|
+
out.extend(self.letters.iter().cloned());
|
|
1614
|
+
out
|
|
1567
1615
|
}
|
|
1568
|
-
out
|
|
1569
1616
|
}
|
|
1570
1617
|
|
|
1571
1618
|
/// What is known of each local and parameter's type (see `An.ownerOf`). A name bound more than once,
|
|
@@ -1633,6 +1680,7 @@ fn build(src: &Source) -> An {
|
|
|
1633
1680
|
// Fields that may hold a closure (`f: fn(..)`, or a bare type parameter): `x.f(..)` can be a
|
|
1634
1681
|
// closure call, so a method of the same name is not resolvable.
|
|
1635
1682
|
let mut callable: HashSet<String> = HashSet::new();
|
|
1683
|
+
let tpInfo = TypeParamInfo::of(&src.items);
|
|
1636
1684
|
for (idx, item) in src.items.iter().enumerate() {
|
|
1637
1685
|
match item {
|
|
1638
1686
|
Item::Enum(e) => {
|
|
@@ -1664,6 +1712,7 @@ fn build(src: &Source) -> An {
|
|
|
1664
1712
|
Some(t) => format!("{}.{}", t, f.name),
|
|
1665
1713
|
None => f.name.clone(),
|
|
1666
1714
|
};
|
|
1715
|
+
let tps = tpInfo.typeParams(f);
|
|
1667
1716
|
units.push(Unit {
|
|
1668
1717
|
label,
|
|
1669
1718
|
kind: Kind::Fn,
|
|
@@ -1682,7 +1731,7 @@ fn build(src: &Source) -> An {
|
|
|
1682
1731
|
plain: matches!(p.ty, ParamType::Type(_)),
|
|
1683
1732
|
default: p.default.clone(),
|
|
1684
1733
|
ty: match &p.ty {
|
|
1685
|
-
ParamType::Type(t) if !
|
|
1734
|
+
ParamType::Type(t) if !tps.contains(&t.name) => Some(t.name.clone()),
|
|
1686
1735
|
_ => None,
|
|
1687
1736
|
},
|
|
1688
1737
|
}))
|
|
@@ -3023,6 +3072,11 @@ fn solve(an: &An) {
|
|
|
3023
3072
|
}
|
|
3024
3073
|
}
|
|
3025
3074
|
}
|
|
3075
|
+
let byCallee = sitesByCallee(an);
|
|
3076
|
+
let mut spreadAt: HashMap<(usize, NodeId), Vec<&SpreadSite>> = HashMap::new();
|
|
3077
|
+
for s in &an.spreads {
|
|
3078
|
+
spreadAt.entry((s.unit, s.node)).or_default().push(s);
|
|
3079
|
+
}
|
|
3026
3080
|
loop {
|
|
3027
3081
|
an.clearCache();
|
|
3028
3082
|
let mut removeUp: Vec<((usize, usize), String)> = vec![];
|
|
@@ -3030,7 +3084,7 @@ fn solve(an: &An) {
|
|
|
3030
3084
|
for (g, j) in candidates {
|
|
3031
3085
|
let mut why: Option<String> = None;
|
|
3032
3086
|
let mut okFrom: Option<usize> = None;
|
|
3033
|
-
for site in
|
|
3087
|
+
for site in byCallee.get(&g).into_iter().flatten().map(|&i| &an.sites[i]) {
|
|
3034
3088
|
match an.siteArg(site, g, j) {
|
|
3035
3089
|
Err(m) => {
|
|
3036
3090
|
let from = &an.units[site.caller].label;
|
|
@@ -3071,7 +3125,7 @@ fn solve(an: &An) {
|
|
|
3071
3125
|
}
|
|
3072
3126
|
}
|
|
3073
3127
|
for (f, ui, node) in &an.moves {
|
|
3074
|
-
let proved =
|
|
3128
|
+
let proved = spreadAt.get(&(*ui, *node)).is_some_and(|v| v.iter().any(|s| an.spreadProved(s)));
|
|
3075
3129
|
if !proved {
|
|
3076
3130
|
poisoned.insert(f.clone());
|
|
3077
3131
|
}
|
|
@@ -3106,6 +3160,15 @@ fn solve(an: &An) {
|
|
|
3106
3160
|
an.clearCache();
|
|
3107
3161
|
}
|
|
3108
3162
|
|
|
3163
|
+
/// The indices of `an.sites` per callee unit, in order.
|
|
3164
|
+
fn sitesByCallee(an: &An) -> HashMap<usize, Vec<usize>> {
|
|
3165
|
+
let mut out: HashMap<usize, Vec<usize>> = HashMap::new();
|
|
3166
|
+
for (i, s) in an.sites.iter().enumerate() {
|
|
3167
|
+
out.entry(s.callee).or_default().push(i);
|
|
3168
|
+
}
|
|
3169
|
+
out
|
|
3170
|
+
}
|
|
3171
|
+
|
|
3109
3172
|
// ---------- unique versions ----------
|
|
3110
3173
|
|
|
3111
3174
|
/// A fn called with both unique and shared values gets a second copy for the unique calls.
|
|
@@ -3216,6 +3279,7 @@ fn versionPlan(an: &An) -> Vec<(usize, Vec<(usize, NodeId)>)> {
|
|
|
3216
3279
|
break;
|
|
3217
3280
|
}
|
|
3218
3281
|
}
|
|
3282
|
+
let byCallee = sitesByCallee(an);
|
|
3219
3283
|
let mut plan = vec![];
|
|
3220
3284
|
for (g, u) in an.units.iter().enumerate() {
|
|
3221
3285
|
if u.kind != Kind::Fn || !(u.free || u.method) || an.escaped.contains(&g) || !gains.contains(&g) {
|
|
@@ -3224,7 +3288,7 @@ fn versionPlan(an: &An) -> Vec<(usize, Vec<(usize, NodeId)>)> {
|
|
|
3224
3288
|
if IMPLICIT.contains(&u.name.as_str()) || u.name == "main" || u.name.ends_with(VERSION_SUFFIX) {
|
|
3225
3289
|
continue;
|
|
3226
3290
|
}
|
|
3227
|
-
let sites: Vec<&Site> =
|
|
3291
|
+
let sites: Vec<&Site> = byCallee.get(&g).into_iter().flatten().map(|&i| &an.sites[i]).collect();
|
|
3228
3292
|
if sites.len() < 2 {
|
|
3229
3293
|
continue;
|
|
3230
3294
|
}
|
|
@@ -3263,22 +3327,26 @@ fn versionPlan(an: &An) -> Vec<(usize, Vec<(usize, NodeId)>)> {
|
|
|
3263
3327
|
plan
|
|
3264
3328
|
}
|
|
3265
3329
|
|
|
3266
|
-
/// Makes the unique versions that pay off, and returns the notes for `--explain-fusion`
|
|
3330
|
+
/// Makes the unique versions that pay off, and returns the notes for `--explain-fusion`, and the
|
|
3331
|
+
/// solved analysis of the program it leaves, when it has one.
|
|
3267
|
-
fn versionCallers(src: &mut Source) -> Vec<String> {
|
|
3332
|
+
fn versionCallers(src: &mut Source) -> (Vec<String>, Option<An>) {
|
|
3268
3333
|
let nFns = src.items.iter().filter(|i| matches!(i, Item::Fn(_))).count();
|
|
3269
3334
|
let budget = (nFns / 8).max(16);
|
|
3270
3335
|
let mut made: HashMap<String, String> = HashMap::new();
|
|
3271
3336
|
let mut notes = vec![];
|
|
3272
3337
|
let mut cut = false;
|
|
3338
|
+
let mut last: Option<An> = None;
|
|
3273
3339
|
for round in 0..VERSION_ROUNDS {
|
|
3274
3340
|
let an = build(src);
|
|
3275
3341
|
solve(&an);
|
|
3276
3342
|
let plan = versionPlan(&an);
|
|
3277
3343
|
if plan.is_empty() {
|
|
3344
|
+
last = Some(an);
|
|
3278
3345
|
break;
|
|
3279
3346
|
}
|
|
3280
3347
|
if round + 1 == VERSION_ROUNDS {
|
|
3281
3348
|
cut = true;
|
|
3349
|
+
last = Some(an);
|
|
3282
3350
|
break;
|
|
3283
3351
|
}
|
|
3284
3352
|
let mut changed = false;
|
|
@@ -3319,6 +3387,10 @@ fn versionCallers(src: &mut Source) -> Vec<String> {
|
|
|
3319
3387
|
}
|
|
3320
3388
|
}
|
|
3321
3389
|
if !changed {
|
|
3390
|
+
// No call moved; with no version made, nothing was added either.
|
|
3391
|
+
if made.is_empty() {
|
|
3392
|
+
last = Some(an);
|
|
3393
|
+
}
|
|
3322
3394
|
break;
|
|
3323
3395
|
}
|
|
3324
3396
|
}
|
|
@@ -3326,7 +3398,7 @@ fn versionCallers(src: &mut Source) -> Vec<String> {
|
|
|
3326
3398
|
notes.push(format!("versions: stopped after {} rounds, calls further down a long call chain keep the shared version", VERSION_ROUNDS - 1));
|
|
3327
3399
|
}
|
|
3328
3400
|
if made.is_empty() {
|
|
3329
|
-
return notes;
|
|
3401
|
+
return (notes, last);
|
|
3330
3402
|
}
|
|
3331
3403
|
// Keep a version only when its code proves more spreads than the original's, or it hands
|
|
3332
3404
|
// values to a kept version.
|
|
@@ -3379,8 +3451,9 @@ fn versionCallers(src: &mut Source) -> Vec<String> {
|
|
|
3379
3451
|
let methods: HashSet<(Option<String>, String)> = dropped.iter().filter(|(t, _)| t.is_some()).cloned().collect();
|
|
3380
3452
|
renameCalls(src, &methods, &strip);
|
|
3381
3453
|
src.items.retain(|it| !matches!(it, Item::Fn(f) if dropped.contains(&(f.type_param.clone(), f.name.clone()))));
|
|
3454
|
+
return (notes, None);
|
|
3382
3455
|
}
|
|
3383
|
-
notes
|
|
3456
|
+
(notes, Some(an))
|
|
3384
3457
|
}
|
|
3385
3458
|
|
|
3386
3459
|
fn unitKey_(origKey: &str, cloneName: &str) -> String {
|
compiler/driver/src/lib.rs
CHANGED
|
@@ -339,12 +339,16 @@ fn recheck(copy: Source) -> (Result<(), Vec<Diagnostic>>, TypedInfo, Rc<Source>)
|
|
|
339
339
|
}
|
|
340
340
|
|
|
341
341
|
fn adopt(pass: &Pass, st: &mut State, kept: Rc<Source>, mut i2: TypedInfo) {
|
|
342
|
-
if pass.target == Target::Pre {
|
|
343
|
-
st.pre = (*kept).clone();
|
|
344
|
-
}
|
|
345
342
|
// A pre-union program is lowered by its check; an already lowered one is kept as is.
|
|
346
|
-
|
|
343
|
+
let keep = pass.target == Target::Lowered || i2.lowered.is_none();
|
|
344
|
+
if keep {
|
|
345
|
+
if pass.target == Target::Pre {
|
|
346
|
+
st.pre = (*kept).clone();
|
|
347
|
+
}
|
|
347
348
|
i2.lowered = Some(kept);
|
|
349
|
+
} else if pass.target == Target::Pre {
|
|
350
|
+
// held nowhere else: moved, not copied
|
|
351
|
+
st.pre = Rc::try_unwrap(kept).unwrap_or_else(|rc| (*rc).clone());
|
|
348
352
|
}
|
|
349
353
|
st.result = Ok(());
|
|
350
354
|
st.info = i2;
|