compiler: Retain completed provider exports
1ccd7e0c791db76eaeb3d43e25d7e969e627e8303178a04a19e2eb8fbd0436a5
Keep public definitions from freshly encoded packages for later import selection. Decode cached and explicit provider headers only when a fresh compilation needs their exports, once per provider. Borrow the completed contracts in the source request so the driver can append each new provider after compilation finishes. Assisted-by: Codex:gpt-6 Change-Id: I2dcb58ceaee6b8c31c1b09a2abdf381658b4d8d9
1 parent
213af9c4
compiler/radiance/build.rad
+66 -39
| 96 | 96 | /// Source snapshots or a complete compiled artifact. |
|
| 97 | 97 | contents: Contents, |
|
| 98 | 98 | } |
|
| 99 | 99 | ||
| 100 | 100 | /// Source request and non-Radiance inputs for a fresh package. |
|
| 101 | - | record Request: Copy { |
|
| 101 | + | record Request['providers]: Copy { |
|
| 102 | 102 | /// Declared provider indices in the selected graph. |
|
| 103 | 103 | declared: *[u16], |
|
| 104 | + | /// Completed provider contracts used to select imports. |
|
| 105 | + | providers: &'providers [link::Provider], |
|
| 104 | 106 | /// Content key for this source request. |
|
| 105 | 107 | key: binary::Id, |
|
| 106 | 108 | /// Captured assembly inputs. |
|
| 107 | 109 | files: *[artifact::File], |
|
| 108 | 110 | /// Whether this package supplies the program entry. |
| 111 | 113 | ||
| 112 | 114 | /// Outputs and retained semantic state of one source compilation. |
|
| 113 | 115 | record Compilation { |
|
| 114 | 116 | /// Complete cache and export artifacts. |
|
| 115 | 117 | package: artifact::Package, |
|
| 118 | + | /// Public definitions retained for later package imports. |
|
| 119 | + | exports: *[binary::Export], |
|
| 116 | 120 | /// Resolver with all declarations and checked bodies retained. |
|
| 117 | 121 | resolver: resolver::Resolver, |
|
| 118 | 122 | /// Native generator after consuming the fresh lowered functions. |
|
| 119 | 123 | native: locations::Native, |
|
| 120 | 124 | /// Global data used by the native program. |
| 643 | 647 | if ctx.debug { |
|
| 644 | 648 | set debug = try buffer(work.main, 8388608); |
|
| 645 | 649 | } |
|
| 646 | 650 | let mut retained: ?resolver::Resolver = nil; |
|
| 647 | 651 | let mut compiled: *mut [artifact::Package] = &mut []; |
|
| 652 | + | let mut published: *mut [link::Provider] = &mut []; |
|
| 648 | 653 | for input, index in selected { |
|
| 649 | 654 | set ctx.inputs[index] = input.options; |
|
| 650 | 655 | set ctx.packageCount = index + 1; |
|
| 651 | 656 | set ctx.entryPkgIdx = index; |
|
| 652 | 657 | let providerIds = try providers(&input, &selected[.. index], driverArena) catch { |
| 678 | 683 | io::printError("radiance: cache hit "); |
|
| 679 | 684 | io::printError(package.name); |
|
| 680 | 685 | io::printError("\n"); |
|
| 681 | 686 | set built = package; |
|
| 682 | 687 | } else { |
|
| 688 | + | for i in published.len .. compiled.len { |
|
| 689 | + | let provider = compiled[i]; |
|
| 690 | + | let checkpoint = heap::mark(work.functions); |
|
| 691 | + | let mut input = reader::new(provider.image); |
|
| 692 | + | let head = try program::header(&mut input, work.functions) catch { |
|
| 693 | + | throw super::error(&[ |
|
| 694 | + | "package encoding storage exhausted", provider.name |
|
| 695 | + | ]); |
|
| 696 | + | }; |
|
| 697 | + | let mut exports: *mut [binary::Export] = &mut []; |
|
| 698 | + | for item in head.exports { |
|
| 699 | + | let name = try keep(work.emitter, item.name) catch { |
|
| 700 | + | throw super::Error::Other; |
|
| 701 | + | }; |
|
| 702 | + | let mut item = item; |
|
| 703 | + | set item.name = name; |
|
| 704 | + | try exports.append(item, work.emitter) catch { |
|
| 705 | + | throw super::error(&[ |
|
| 706 | + | "package encoding storage exhausted", provider.name |
|
| 707 | + | ]); |
|
| 708 | + | }; |
|
| 709 | + | } |
|
| 710 | + | try published.append( |
|
| 711 | + | link::Provider { |
|
| 712 | + | name: provider.name, image: provider.identity, exports: &exports[..] |
|
| 713 | + | }, |
|
| 714 | + | work.emitter |
|
| 715 | + | ) catch { |
|
| 716 | + | throw super::Error::Other; |
|
| 717 | + | }; |
|
| 718 | + | // Only the copied export descriptors survive header decoding. |
|
| 719 | + | unsafe { |
|
| 720 | + | heap::rewind(work.functions, checkpoint); |
|
| 721 | + | } |
|
| 722 | + | } |
|
| 683 | 723 | try loadInterfaces(ctx, graph, selected, &compiled[..], driverArena); |
|
| 684 | 724 | try load(ctx, graph, index, source.modules, module::Role::Source, providerIds); |
|
| 685 | 725 | if ctx.config.buildTest and mem::eq(input.options.name, entryName) { |
|
| 686 | 726 | try super::generateTestRunner(ctx, graph, driverArena); |
|
| 687 | 727 | } |
| 711 | 751 | res, |
|
| 712 | 752 | &compiled[..], |
|
| 713 | 753 | work, |
|
| 714 | 754 | Request { |
|
| 715 | 755 | declared: providerIds, |
|
| 756 | + | providers: &published[..], |
|
| 716 | 757 | key: request, |
|
| 717 | 758 | files: source.files, |
|
| 718 | 759 | entry: mem::eq(input.options.name, entryName), |
|
| 719 | 760 | }, |
|
| 720 | 761 | native, |
|
| 721 | 762 | debug.take() |
|
| 722 | 763 | ); |
|
| 723 | 764 | let case Compilation { |
|
| 724 | - | package, resolver: resolved, native: generated, data, debug: debugBuffer |
|
| 765 | + | package, |
|
| 766 | + | exports, |
|
| 767 | + | resolver: resolved, |
|
| 768 | + | native: generated, |
|
| 769 | + | data, |
|
| 770 | + | debug: debugBuffer, |
|
| 725 | 771 | } = |
|
| 726 | 772 | result else panic "buildPackages: expected compilation"; |
|
| 727 | 773 | set built = package; |
|
| 774 | + | try published.append( |
|
| 775 | + | link::Provider { |
|
| 776 | + | name: built.name, image: built.identity, exports |
|
| 777 | + | }, |
|
| 778 | + | work.emitter |
|
| 779 | + | ) catch { |
|
| 780 | + | throw super::Error::Other; |
|
| 781 | + | }; |
|
| 728 | 782 | set retained = resolved; |
|
| 729 | 783 | set native = generated; |
|
| 730 | 784 | try allData.extend(data, work.emitter) catch { |
|
| 731 | 785 | throw super::error(&["native data storage exhausted"]); |
|
| 732 | 786 | }; |
| 884 | 938 | ctx: &mut super::CompileContext, |
|
| 885 | 939 | modules: module::ModuleTable, |
|
| 886 | 940 | res: resolver::Resolver, |
|
| 887 | 941 | providers: &[artifact::Package], |
|
| 888 | 942 | work: &mut Workspace, |
|
| 889 | - | selection: Request, |
|
| 943 | + | selection: Request['providers], |
|
| 890 | 944 | target: locations::Native, |
|
| 891 | 945 | debug: *mut [u8] |
|
| 892 | 946 | ) -> Compilation throws (super::Error) { |
|
| 893 | 947 | let index = ctx.entryPkgIdx else panic "compile: missing package"; |
|
| 894 | 948 | let pkg = ctx.packages[index]; |
| 940 | 994 | heap::reset(work.functions); |
|
| 941 | 995 | } |
|
| 942 | 996 | let initial = try codegen::ril(work.emitter, work.spool.take(), pkg.name) catch { |
|
| 943 | 997 | throw super::error(&["package encoding storage exhausted", pkg.name]); |
|
| 944 | 998 | }; |
|
| 945 | - | let mut published: *mut [link::Provider] = &mut []; |
|
| 946 | 999 | let mut low = lower::lowerer(res, modules, pkg.name, work.functions, lower::LowerOptions { |
|
| 947 | 1000 | debug: ctx.debug, buildTest: ctx.config.buildTest, tableScope: lower::TableScope::Package |
|
| 948 | 1001 | }); |
|
| 949 | - | for provider in providers { |
|
| 950 | - | let checkpoint = heap::mark(work.functions); |
|
| 951 | - | let mut input = reader::new(provider.image); |
|
| 952 | - | let head = try program::header(&mut input, work.functions) catch { |
|
| 953 | - | throw super::error(&["package encoding storage exhausted", pkg.name]); |
|
| 954 | - | }; |
|
| 955 | - | let mut exports: *mut [binary::Export] = &mut []; |
|
| 956 | - | for item in head.exports { |
|
| 957 | - | let name = try keep(initial.arena, item.name) catch { |
|
| 958 | - | throw super::Error::Other; |
|
| 959 | - | }; |
|
| 960 | - | let mut item = item; |
|
| 961 | - | set item.name = name; |
|
| 962 | - | try exports.append(item, initial.arena) catch { |
|
| 963 | - | throw super::error(&["package encoding storage exhausted", pkg.name]); |
|
| 964 | - | }; |
|
| 965 | - | } |
|
| 966 | - | try published.append( |
|
| 967 | - | link::Provider { |
|
| 968 | - | name: provider.name, image: provider.identity, exports: &exports[..] |
|
| 969 | - | }, |
|
| 970 | - | initial.arena |
|
| 971 | - | ) catch { |
|
| 972 | - | throw super::Error::Other; |
|
| 973 | - | }; |
|
| 974 | - | // Only the copied export descriptors survive header decoding. |
|
| 975 | - | unsafe { |
|
| 976 | - | heap::rewind(work.functions, checkpoint); |
|
| 977 | - | } |
|
| 978 | - | } |
|
| 979 | 1002 | let mut locationWriter = writer::new(debug); |
|
| 980 | 1003 | let root = pkg.rootModuleId else panic "compile: missing root"; |
|
| 981 | 1004 | if ctx.debug { |
|
| 982 | 1005 | let count = modules.entries.len - root as u32; |
|
| 983 | 1006 | try writer::word(&mut locationWriter, count as u64) catch { |
| 996 | 1019 | native: target, |
|
| 997 | 1020 | entry: selection.entry, |
|
| 998 | 1021 | }; |
|
| 999 | 1022 | try super::lowerPackage(ctx, modules, &mut low, &pkg, true, &mut sink); |
|
| 1000 | 1023 | return try finishPackage( |
|
| 1001 | - | ctx, low, sink, &declarations[..], providers, &published[..], work, selection |
|
| 1024 | + | ctx, low, sink, &declarations[..], providers, work, selection |
|
| 1002 | 1025 | ); |
|
| 1003 | 1026 | } |
|
| 1004 | 1027 | ||
| 1005 | 1028 | /// Encode the tables, exact pins, and metadata after a package's functions are consumed. |
|
| 1006 | 1029 | fn finishPackage( |
|
| 1007 | 1030 | ctx: &super::CompileContext, |
|
| 1008 | 1031 | low: lower::Lowerer, |
|
| 1009 | 1032 | sink: locations::Sink, |
|
| 1010 | 1033 | declarations: *[artifact::Module], |
|
| 1011 | 1034 | providers: &[artifact::Package], |
|
| 1012 | - | published: &[link::Provider], |
|
| 1013 | 1035 | work: &mut Workspace, |
|
| 1014 | - | selection: Request |
|
| 1036 | + | selection: Request['providers] |
|
| 1015 | 1037 | ) -> Compilation throws (super::Error) { |
|
| 1016 | 1038 | let mut lowering = low; |
|
| 1017 | 1039 | let index = ctx.entryPkgIdx else panic "finishPackage: missing package"; |
|
| 1018 | 1040 | let pkg = ctx.packages[index]; |
|
| 1019 | 1041 | let case Request { declared, key: request, files, .. } = selection |
| 1050 | 1072 | layouts: &tables.layouts[..], |
|
| 1051 | 1073 | signatures: &tables.signatures[..], |
|
| 1052 | 1074 | traits: &tables.traits[..], |
|
| 1053 | 1075 | }; |
|
| 1054 | 1076 | let plan = try link::prepare( |
|
| 1055 | - | &mut spool, &whole, &dataOrigins[..], published, &references[..], index as u16 |
|
| 1077 | + | &mut spool, &whole, &dataOrigins[..], selection.providers, &references[..], index as u16 |
|
| 1056 | 1078 | ) catch { |
|
| 1057 | 1079 | throw super::error(&["cannot resolve package imports"]); |
|
| 1058 | 1080 | }; |
|
| 1059 | 1081 | super::pkgLog(&pkg, &["encoding package tables"]); |
|
| 1060 | 1082 | let mut head = try buffer(work.functions, HEADER_BYTES); |
| 1125 | 1147 | heap::reset(work.functions); |
|
| 1126 | 1148 | } |
|
| 1127 | 1149 | let case writer::Writer { bytes: debugBuffer, .. } = locationOutput |
|
| 1128 | 1150 | else panic "finishPackage: expected location output"; |
|
| 1129 | 1151 | return Compilation { |
|
| 1130 | - | package: result, resolver: resolved, native, data: whole.data, debug: debugBuffer |
|
| 1152 | + | package: result, |
|
| 1153 | + | exports: plan.image.exports, |
|
| 1154 | + | resolver: resolved, |
|
| 1155 | + | native, |
|
| 1156 | + | data: whole.data, |
|
| 1157 | + | debug: debugBuffer, |
|
| 1131 | 1158 | }; |
|
| 1132 | 1159 | } |
|
| 1133 | 1160 | ||
| 1134 | 1161 | /// Assemble a captured native input and register its code and immutable data. |
|
| 1135 | 1162 | fn assembly( |