lang: Reduce resolver and lowering lookups
67959d7010020652627cd4a07a1b3ac96baa00e1f93d962e3b78764f3408e878
Group interned types by declaration and index function names by symbol. Read each node once to select its source or coerced type. Reuse a known nominal layout before computing its size and slots. When a predecessor search folds a block parameter, update other blocks only if the search read the provisional register. Assisted-by: Codex:gpt-6 Change-Id: I556cf4fa2da5de368421b95ab0064f540be52580
1 parent
70292a01
lib/std/lang/lower.rad
+50 -41
| 465 | 465 | data: *mut [il::Data], |
|
| 466 | 466 | /// Package and linkage of each entry of `data`, in the same order. |
|
| 467 | 467 | dataOrigins: *mut [Origin], |
|
| 468 | 468 | /// First data entry owned by the package currently being lowered. |
|
| 469 | 469 | packageDataStart: u32, |
|
| 470 | - | /// Map of function symbols to qualified names. |
|
| 471 | - | fnSyms: *mut [FnSymEntry], |
|
| 470 | + | /// Qualified function names indexed by resolver symbol ID. |
|
| 471 | + | fnSyms: *mut [?*[u8]], |
|
| 472 | 472 | /// Cache of error type tags. A tag depends only on the canonical name of its type. |
|
| 473 | 473 | errTags: *mut [ErrTagEntry], |
|
| 474 | 474 | /// Lowering options. |
|
| 475 | 475 | options: LowerOptions, |
|
| 476 | 476 |
| 541 | 541 | ||
| 542 | 542 | /// Current debug source location, set when processing AST nodes. |
|
| 543 | 543 | srcLoc: il::SrcLoc, |
|
| 544 | 544 | } |
|
| 545 | 545 | ||
| 546 | - | /// Entry mapping a function symbol to its qualified name. |
|
| 547 | - | record FnSymEntry: Copy { |
|
| 548 | - | sym: resolver::SymbolId, |
|
| 549 | - | qualName: *[u8], |
|
| 550 | - | } |
|
| 551 | - | ||
| 552 | 546 | /// Entry in the error tag cache. |
|
| 553 | 547 | record ErrTagEntry: Copy { |
|
| 554 | 548 | /// The type of this error. |
|
| 555 | 549 | ty: resolver::Type, |
|
| 556 | 550 | /// The tag of this error type: non-zero and derived from its canonical name. |
| 963 | 957 | preds: *mut [u32], |
|
| 964 | 958 | /// The current SSA value of each variable in this block. Indexed by variable |
|
| 965 | 959 | /// id. A `nil` means the variable wasn't assigned in this block. Updated by |
|
| 966 | 960 | /// [`defVar`], queried by [`useVarInBlock`]. |
|
| 967 | 961 | vars: *mut [?il::Val], |
|
| 962 | + | /// Whether a variable lookup read this block during a predecessor search. |
|
| 963 | + | cacheRead: bool, |
|
| 968 | 964 | /// Sealing state. Once sealed, all predecessors are known and we can resolve |
|
| 969 | 965 | /// variable uses that need to pull values from predecessors. |
|
| 970 | 966 | sealState: Sealed, |
|
| 971 | 967 | /// Loop nesting depth when this block was created. |
|
| 972 | 968 | loopDepth: u32, |
| 1394 | 1390 | ||
| 1395 | 1391 | /// Register a function symbol with its qualified name. |
|
| 1396 | 1392 | /// Called when lowering function declarations, so cross-package calls can find |
|
| 1397 | 1393 | /// the function by name. |
|
| 1398 | 1394 | fn registerFnSym(self: &mut Lowerer, sym: resolver::SymbolId, qualName: *[u8]) throws (LowerError) { |
|
| 1399 | - | try self.fnSyms.append(FnSymEntry { sym, qualName }, self.resolver.arena) catch { |
|
| 1400 | - | throw LowerError::AllocationFailed; |
|
| 1401 | - | }; |
|
| 1402 | - | } |
|
| 1403 | - | ||
| 1404 | - | /// Look up a function's qualified name by its symbol. |
|
| 1405 | - | /// Returns `nil` if the symbol wasn't registered (e.g. callee's module is not yet lowered). |
|
| 1406 | - | // TODO: This is kind of dubious as an optimization, if it depends on the order |
|
| 1407 | - | // in which modules are lowered. |
|
| 1408 | - | // TODO: Use a hash table here? |
|
| 1409 | - | fn lookupFnSym(self: &Lowerer, sym: resolver::SymbolId) -> ?*[u8] { |
|
| 1410 | - | for entry in &self.fnSyms[..] { |
|
| 1411 | - | if entry.sym == sym { |
|
| 1412 | - | return entry.qualName; |
|
| 1413 | - | } |
|
| 1395 | + | if *sym >= self.fnSyms.len { |
|
| 1396 | + | try self.fnSyms.resize(*sym + 1, nil, self.resolver.arena) catch { |
|
| 1397 | + | throw LowerError::AllocationFailed; |
|
| 1398 | + | }; |
|
| 1414 | 1399 | } |
|
| 1415 | - | return nil; |
|
| 1400 | + | set self.fnSyms[*sym] = qualName; |
|
| 1416 | 1401 | } |
|
| 1417 | 1402 | ||
| 1418 | 1403 | /// Set the package context for lowering. |
|
| 1419 | 1404 | /// Called before lowering each package. |
|
| 1420 | 1405 | /// The module table must outlive later uses of the lowerer. |
| 3070 | 3055 | return reg; |
|
| 3071 | 3056 | } |
|
| 3072 | 3057 | ||
| 3073 | 3058 | /// Look up the resolved type of an AST node, or throw `MissingType`. |
|
| 3074 | 3059 | fn typeOf(self: &mut Lowerer, node: *ast::Node) -> resolver::Type throws (LowerError) { |
|
| 3075 | - | let ty = resolver::typeFor(&self.resolver, node) else throw LowerError::MissingType(node); |
|
| 3076 | - | if let conversion = resolver::coercionFor(&self.resolver, node) { |
|
| 3077 | - | match conversion { |
|
| 3078 | - | case resolver::Coercion::Borrow { from, .. } => return from, |
|
| 3079 | - | case resolver::Coercion::OptionalMap { from, .. } => return *from, |
|
| 3060 | + | let mut ty: resolver::Type = undefined; |
|
| 3061 | + | { |
|
| 3062 | + | let data = resolver::nodeData(&self.resolver, node); |
|
| 3063 | + | set ty = data.ty; |
|
| 3064 | + | if ty == resolver::Type::Unknown { |
|
| 3065 | + | throw LowerError::MissingType(node); |
|
| 3066 | + | } |
|
| 3067 | + | match data.coercion { |
|
| 3068 | + | case resolver::Coercion::Borrow { from, .. } => set ty = from, |
|
| 3069 | + | case resolver::Coercion::OptionalMap { from, .. } => set ty = *from, |
|
| 3080 | 3070 | else => {} |
|
| 3081 | 3071 | } |
|
| 3082 | 3072 | } |
|
| 3083 | 3073 | return ty; |
|
| 3084 | 3074 | } |
| 3191 | 3181 | paramVars: &mut [], |
|
| 3192 | 3182 | instrs, |
|
| 3193 | 3183 | locs: &mut [], |
|
| 3194 | 3184 | preds, |
|
| 3195 | 3185 | vars, |
|
| 3186 | + | cacheRead: false, |
|
| 3196 | 3187 | sealState: Sealed::No, |
|
| 3197 | 3188 | loopDepth: self.loopDepth, |
|
| 3198 | 3189 | }, |
|
| 3199 | 3190 | self.fnArena |
|
| 3200 | 3191 | ) catch { |
| 4306 | 4297 | /// definitions, it creates a block parameter to unify them. |
|
| 4307 | 4298 | fn useVarInBlock(self: &mut Lowerer, block: BlockId, v: Var) -> il::Val throws (LowerError) { |
|
| 4308 | 4299 | assert *v < self.vars.len; |
|
| 4309 | 4300 | ||
| 4310 | 4301 | if let val = self.blockData[*block].vars[*v] { |
|
| 4302 | + | set self.blockData[*block].cacheRead = true; |
|
| 4311 | 4303 | return val; |
|
| 4312 | 4304 | } |
|
| 4313 | 4305 | // Entry block cannot have block parameters. If variable isn't defined |
|
| 4314 | 4306 | // in entry, we return undefined. |
|
| 4315 | 4307 | if block == self.entryBlock { |
| 4410 | 4402 | ||
| 4411 | 4403 | if self.blockData[*block].sealState == Sealed::Yes { |
|
| 4412 | 4404 | // Block sealed: check for trivial phi before committing. If all |
|
| 4413 | 4405 | // predecessors provide the same value, we can remove the param we |
|
| 4414 | 4406 | // just created and use that value directly. |
|
| 4407 | + | // The search follows one variable. Only a visit back to this block |
|
| 4408 | + | // can copy its provisional register into another block or edge. |
|
| 4409 | + | set self.blockData[*block].cacheRead = false; |
|
| 4415 | 4410 | if let trivial = try getTrivialPhiVal(self, block, v) { |
|
| 4416 | 4411 | let provisional = il::Val::Reg(reg); |
|
| 4417 | 4412 | removeLastBlockParam(self, block); |
|
| 4418 | - | rewriteCachedVarValue(self, v, provisional, trivial); |
|
| 4413 | + | if self.blockData[*block].cacheRead { |
|
| 4414 | + | rewriteCachedVarValue(self, v, provisional, trivial); |
|
| 4415 | + | } |
|
| 4419 | 4416 | set self.blockData[*block].vars[*v] = trivial; |
|
| 4420 | 4417 | return trivial; |
|
| 4421 | 4418 | } |
|
| 4422 | 4419 | // Non-trivial phi: patch predecessors to pass their values. |
|
| 4423 | 4420 | try resolveBlockArgs(self, block, v, paramIdx); |
| 6171 | 6168 | /// Return the effective type of a node after any coercion applied by |
|
| 6172 | 6169 | /// the resolver. `lowerExpr` already materializes the coercion in the |
|
| 6173 | 6170 | /// IL value, so the lowerer must use the post-coercion type when |
|
| 6174 | 6171 | /// choosing how to compare or store that value. |
|
| 6175 | 6172 | fn effectiveType(self: &mut Lowerer, node: *ast::Node) -> resolver::Type throws (LowerError) { |
|
| 6176 | - | let ty = try typeOf(self, node); |
|
| 6177 | - | if let coerce = resolver::coercionFor(&self.resolver, node) { |
|
| 6178 | - | match coerce { |
|
| 6179 | - | case resolver::Coercion::OptionalLift { target, .. } => return target, |
|
| 6180 | - | case resolver::Coercion::Borrow { to, .. } => return to, |
|
| 6181 | - | case resolver::Coercion::OptionalMap { to, .. } => return to, |
|
| 6173 | + | let mut ty: resolver::Type = undefined; |
|
| 6174 | + | { |
|
| 6175 | + | let data = resolver::nodeData(&self.resolver, node); |
|
| 6176 | + | set ty = data.ty; |
|
| 6177 | + | if ty == resolver::Type::Unknown { |
|
| 6178 | + | throw LowerError::MissingType(node); |
|
| 6179 | + | } |
|
| 6180 | + | match data.coercion { |
|
| 6181 | + | case resolver::Coercion::OptionalLift { target, .. } => set ty = target, |
|
| 6182 | + | case resolver::Coercion::Borrow { to, .. } => set ty = to, |
|
| 6183 | + | case resolver::Coercion::OptionalMap { to, .. } => set ty = to, |
|
| 6182 | 6184 | case resolver::Coercion::TraitObject { traitInfo, class, mutable, .. } => |
|
| 6183 | - | return resolver::Type::TraitObject { class, traitInfo, mutable }, |
|
| 6185 | + | set ty = resolver::Type::TraitObject { class, traitInfo, mutable }, |
|
| 6184 | 6186 | else => {} |
|
| 6185 | 6187 | } |
|
| 6186 | 6188 | } |
|
| 6187 | 6189 | return ty; |
|
| 6188 | 6190 | } |
| 10990 | 10992 | if let case ast::NodeValue::FnDecl(_) = self.resolver.symbols[*sym].node.value { |
|
| 10991 | 10993 | let modId = resolver::moduleIdForSymbol(&self.resolver, sym); |
|
| 10992 | 10994 | // First try to look up the symbol in our registered functions. |
|
| 10993 | 10995 | // This handles cross-package calls correctly, since packages are |
|
| 10994 | 10996 | // lowered in dependency order. |
|
| 10995 | - | let mut qualName = lookupFnSym(self, sym); |
|
| 10997 | + | let mut qualName: ?*[u8] = nil; |
|
| 10998 | + | if *sym < self.fnSyms.len { |
|
| 10999 | + | set qualName = self.fnSyms[*sym]; |
|
| 11000 | + | } |
|
| 10996 | 11001 | if qualName == nil { |
|
| 10997 | 11002 | // Fall back to computing the qualified name from the module graph. |
|
| 10998 | 11003 | // This works for functions in the current package. |
|
| 10999 | 11004 | let id = modId else throw LowerError::MissingMetadata; |
|
| 11000 | 11005 | let symName = self.resolver.symbols[*sym].name; |
| 11950 | 11955 | if let case resolver::Type::Optional(inner) = typ { |
|
| 11951 | 11956 | if resolver::isNullableType(*inner) { |
|
| 11952 | 11957 | return try layoutIndex(self, *inner); |
|
| 11953 | 11958 | } |
|
| 11954 | 11959 | } |
|
| 11960 | + | if let case resolver::Type::Nominal(id) = typ { |
|
| 11961 | + | if *id < self.tables.nominalLayouts.len { |
|
| 11962 | + | if let index = self.tables.nominalLayouts[*id].index { |
|
| 11963 | + | return index; |
|
| 11964 | + | } |
|
| 11965 | + | } |
|
| 11966 | + | } |
|
| 11955 | 11967 | let layout = resolver::getTypeLayout(&self.resolver, typ); |
|
| 11956 | 11968 | if not resolver::hasSlots(&mut self.resolver, typ) { |
|
| 11957 | 11969 | let regions = resolver::refs::positions(&self.resolver, typ); |
|
| 11958 | 11970 | return try internLayout(self, layout.size, layout.alignment, &[], regions); |
|
| 11959 | 11971 | } |
|
| 11960 | 11972 | if let case resolver::Type::Nominal(id) = typ { |
|
| 11961 | 11973 | try growNominals(self); |
|
| 11962 | - | if let index = self.tables.nominalLayouts[*id].index { |
|
| 11963 | - | return index; |
|
| 11964 | - | } |
|
| 11965 | 11974 | let index = try numberSlotted(self, typ, layout); |
|
| 11966 | 11975 | set self.tables.nominalLayouts[*id].index = index; |
|
| 11967 | 11976 | return index; |
|
| 11968 | 11977 | } |
|
| 11969 | 11978 | let key = typeKey(typ); |
lib/std/lang/resolver.rad
+31 -6
| 1268 | 1268 | nodeData: metadata::Table, |
|
| 1269 | 1269 | /// Source types of move-only places bound by `let` at a different type. |
|
| 1270 | 1270 | /// The place node keeps the binding type, so the linear checker reads the |
|
| 1271 | 1271 | /// source type from here to see the move. Storage comes from `arena`. |
|
| 1272 | 1272 | movedSources: *mut [MovedSource], |
|
| 1273 | - | /// Linked list of interned types. |
|
| 1274 | - | types: ?*TypeNode, |
|
| 1273 | + | /// Collision chains for interned types. |
|
| 1274 | + | types: *mut [?*TypeNode], |
|
| 1275 | 1275 | /// Diagnostics recorded so far. |
|
| 1276 | 1276 | errors: DiagnosticBuffer, |
|
| 1277 | 1277 | /// Frozen module entries for the packages being resolved, set by [`resolve`]. |
|
| 1278 | 1278 | moduleTable: module::ModuleTable, |
|
| 1279 | 1279 | /// Cache of module scopes indexed by module ID. |
| 1296 | 1296 | /// Internal error sentinel thrown when analysis cannot proceed. |
|
| 1297 | 1297 | export union ResolveError: Copy { |
|
| 1298 | 1298 | Failure, |
|
| 1299 | 1299 | } |
|
| 1300 | 1300 | ||
| 1301 | + | /// Number of collision chains in the type table. |
|
| 1302 | + | constant TYPE_BUCKETS: u32 = 1024; |
|
| 1303 | + | ||
| 1304 | + | /// Select a collision chain from the declaration ID of a type or its item. |
|
| 1305 | + | fn typeBucket(ty: Type) -> u32 { |
|
| 1306 | + | let mut item = ty; |
|
| 1307 | + | match ty { |
|
| 1308 | + | case Type::Pointer { target, .. } => set item = *target, |
|
| 1309 | + | case Type::Slice { item: inner, .. } => set item = *inner, |
|
| 1310 | + | case Type::Optional(inner) => set item = *inner, |
|
| 1311 | + | case Type::Array(array) => set item = *array.item, |
|
| 1312 | + | else => {} |
|
| 1313 | + | } |
|
| 1314 | + | match item { |
|
| 1315 | + | case Type::Nominal(id) => return *id % TYPE_BUCKETS, |
|
| 1316 | + | case Type::Fn(sig) => return sig.paramTypes.len % TYPE_BUCKETS, |
|
| 1317 | + | else => return 0, |
|
| 1318 | + | } |
|
| 1319 | + | } |
|
| 1320 | + | ||
| 1301 | 1321 | /// Node in the type interning linked list. |
|
| 1302 | 1322 | record TypeNode: Copy { |
|
| 1323 | + | /// Canonical type stored at this address. |
|
| 1303 | 1324 | ty: Type, |
|
| 1325 | + | /// Next type in the same bucket. |
|
| 1304 | 1326 | next: ?*TypeNode, |
|
| 1305 | 1327 | } |
|
| 1306 | 1328 | ||
| 1307 | 1329 | /// Allocate and intern a type in the arena, returning a pointer for deduplication. |
|
| 1308 | 1330 | export fn allocType(self: &mut Resolver, ty: Type) -> *Type { |
|
| 1309 | 1331 | // Search existing types for a match. |
|
| 1310 | - | let mut cursor = self.types; |
|
| 1332 | + | let bucket = typeBucket(ty); |
|
| 1333 | + | let mut cursor = self.types[bucket]; |
|
| 1311 | 1334 | while let node = cursor { |
|
| 1312 | 1335 | if node.ty == ty { |
|
| 1313 | 1336 | return &node.ty; |
|
| 1314 | 1337 | } |
|
| 1315 | 1338 | set cursor = node.next; |
|
| 1316 | 1339 | } |
|
| 1317 | 1340 | // Allocate a new type node from the arena. A node is its own one-element |
|
| 1318 | 1341 | // slice, so its address stays fixed. |
|
| 1319 | 1342 | let mut nodes: *mut [TypeNode] = &mut []; |
|
| 1320 | - | try! nodes.append(TypeNode { ty, next: self.types }, self.arena); |
|
| 1343 | + | try! nodes.append(TypeNode { ty, next: self.types[bucket] }, self.arena); |
|
| 1321 | 1344 | let frozen: *TypeNode = &mut nodes[0]; |
|
| 1322 | - | set self.types = frozen; |
|
| 1345 | + | set self.types[bucket] = frozen; |
|
| 1323 | 1346 | ||
| 1324 | 1347 | return &frozen.ty; |
|
| 1325 | 1348 | } |
|
| 1326 | 1349 | ||
| 1327 | 1350 | /// Append a nominal type descriptor and return its ID. |
| 1419 | 1442 | config: Config |
|
| 1420 | 1443 | ) -> Resolver { |
|
| 1421 | 1444 | let case ResolverStorage { arena: initialArena, nodeData, errors } = storage |
|
| 1422 | 1445 | else panic "resolver: expected resolver storage"; |
|
| 1423 | 1446 | let mut arena = initialArena; |
|
| 1447 | + | let mut interned: *mut [?*TypeNode] = &mut []; |
|
| 1448 | + | try! interned.resize(TYPE_BUCKETS, nil, arena); |
|
| 1424 | 1449 | let metadata = metadata::new(nodeData, arena); |
|
| 1425 | 1450 | ||
| 1426 | 1451 | // Create the package scope, which always has the ID `PKG_SCOPE_ID`. |
|
| 1427 | 1452 | let mut pkgSymbols: *mut [SymbolId] = &mut []; |
|
| 1428 | 1453 | try! pkgSymbols.reserve(MAX_MODULE_SYMBOLS, arena); |
| 1467 | 1492 | phase: Phase::Graph, |
|
| 1468 | 1493 | nominals, |
|
| 1469 | 1494 | traits, |
|
| 1470 | 1495 | nodeData: metadata, |
|
| 1471 | 1496 | movedSources: &mut [], |
|
| 1472 | - | types: nil, |
|
| 1497 | + | types: interned, |
|
| 1473 | 1498 | errors: DiagnosticBuffer { entries: errors, len: 0 }, |
|
| 1474 | 1499 | moduleTable: module::ModuleTable { entries: &[] }, |
|
| 1475 | 1500 | moduleScopes, |
|
| 1476 | 1501 | instances, |
|
| 1477 | 1502 | methods, |