compiler: Store debug source paths once

a6c5cbd186ea8ce64bb7744e4b636ed8224419b4f0728057815c77c02d1b7f47
The native `.rv64.debug` sidecar repeated the full source path, with a
zero terminator, in every entry, so most of its bytes were copies of a
few paths. The package `locations` table stored each function's name
and block count, although its records follow the image's function order
and each block's location count already matches its instructions.

Write the native sidecar as a path table, the same count and
length-prefixed paths that start a `locations` table, followed by
entries of a code offset, a source offset, and a path index. Drop the
function names and block counts from `locations`. This needs the
emulator change that reads the path table.

Assisted-by: Claude:opus-5.5
Change-Id: Ib281b0001f95d1a73ca19bf6a3a35b4dc1e8e944
Alexis Sellier committed ago 1 parent 47cdde9e
compiler/radiance/build.rad +5 -4
1168 1168
    }
1169 1169
    let mut locationWriter = writer::new(debug);
1170 1170
    let root = pkg.rootModuleId else panic "compile: missing root";
1171 1171
    if locationWriter.bytes.len > 0 {
1172 1172
        let listed: &[artifact::Module] = &declarations[..];
1173 -
        try writer::word(&mut locationWriter, listed.len as u64) catch {
1174 -
            throw super::Error::Other;
1175 -
        };
1173 +
        let mut paths: *mut [*[u8]] = &mut [];
1176 1174
        for entry in listed {
1177 1175
            let path = try artifact::join(work.artifacts, &[
1178 1176
                &entry.path[.. entry.path.len - 4], ".rad"
1179 1177
            ]) catch {
1180 1178
                throw super::Error::Other;
1181 1179
            };
1182 -
            try writer::bytes(&mut locationWriter, path) catch {
1180 +
            try paths.append(path, work.artifacts) catch {
1183 1181
                throw super::Error::Other;
1184 1182
            };
1185 1183
        }
1184 +
        try locations::writePaths(&mut locationWriter, &paths[..]) catch {
1185 +
            throw super::Error::Other;
1186 +
        };
1186 1187
    }
1187 1188
    let entryIndex = ctx.entryPkgIdx else panic "compile: missing entry package";
1188 1189
    let mut sink = locations::Sink {
1189 1190
        ril: initial,
1190 1191
        locations: locationWriter,
compiler/radiance/locations.rad +33 -34
17 17
export constant MAX_SOURCE_INDEX: u32 = 65535;
18 18
/// Limits for decoding a function from a package spool.
19 19
export constant FUNCTION_LIMITS: binary::Limits = binary::Limits {
20 20
    registers: 1048576, blocks: 65536
21 21
};
22 +
/// Bytes in a 32-bit sidecar field.
23 +
constant WORD_BYTES: u32 = 4;
22 24
/// Bytes in a source location pair in the sidecar.
23 -
constant LOCATION_WIRE_BYTES: u32 = 8;
25 +
constant LOCATION_WIRE_BYTES: u32 = 2 * WORD_BYTES;
24 26
25 27
/// Native function output for the invocation.
26 28
export union Native {
27 29
    /// Check or export packages without native generation.
28 30
    None,
71 73
        try target.emit(func, role, origin, layouts, arena);
72 74
    }
73 75
}
74 76
75 77
/// Write package-local instruction locations in RIL function and block order.
78 +
/// Each block has an instruction location count, which is zero or the block's
79 +
/// instruction count, followed by that many module and offset pairs.
76 80
fn encode(output: &mut writer::Writer, first: u16, func: *il::Fn) throws (binary::Error) {
77 -
    try writer::bytes(output, func.name);
78 -
    try writer::word(output, func.blocks.len as u64);
79 81
    for block in func.blocks {
80 82
        try writer::word(output, block.locs.len as u64);
81 83
        for location in block.locs {
82 84
            if location.moduleId < first {
83 85
                throw binary::Error::Invalid;
90 92
91 93
/// Read and attach one function's source locations from its matching sidecar.
92 94
export fn attach(
93 95
    input: &mut reader::Reader['wire], func: *il::Fn, first: u32, count: u32, arena: &heap::Heap
94 96
) -> *il::Fn throws (binary::Error) {
95 -
    if not mem::eq(try input.bytes(arena), func.name)
96 -
        or try input.word() <> func.blocks.len as u64
97 -
    {
98 -
        throw binary::Error::Invalid;
99 -
    }
100 97
    let mut blocks: *mut [il::Block] = &mut [];
101 98
    for block in func.blocks {
102 99
        let length = try input.count(LOCATION_WIRE_BYTES);
103 100
        if length <> 0 and length <> block.instrs.len {
104 101
            throw binary::Error::Invalid;
127 124
        throw binary::Error::Storage;
128 125
    };
129 126
    return &functions[0];
130 127
}
131 128
132 -
/// Serialize debug entries and write the `.debug` file.
133 -
/// Resolve module identifiers through the retained source paths.
134 -
/// Format per entry is `{pc: u32, offset: u32, filePath: [u8], NULL}`.
135 -
/// The bytes occupy permanent allocator storage. Empty data truncates a stale sidecar.
129 +
/// Serialize debug entries and write the `.debug` file: the source path table
130 +
/// as a `u32` count and length-prefixed paths, followed by one entry of three
131 +
/// `u32` values for each location: code offset, source byte offset, and path
132 +
/// index. The bytes occupy permanent allocator storage. Empty data truncates a
133 +
/// stale sidecar.
136 134
export fn write(
137 135
    entries: &[types::DebugEntry], paths: &[*[u8]], output: *[u8], arena: &heap::Heap
138 136
) throws (sys::Error) {
139 -
    // Reserve the whole buffer first. Each entry is a pc, an offset, the path, and
140 -
    // a NULL byte.
141 -
    let mut size: u32 = 0;
142 -
    for entry in entries {
143 -
        if entry.moduleId as u32 >= paths.len {
144 -
            throw sys::Error::Invalid;
145 -
        }
146 -
        let path = paths[entry.moduleId as u32];
147 -
        if path.len > mem::U32_MAX - 9 or size > mem::U32_MAX - 9 - path.len {
148 -
            throw sys::Error::TooLarge;
149 -
        }
150 -
        set size += 9 + path.len;
137 +
    let mut size: u64 = WORD_BYTES as u64 + entries.len as u64 * 3 * WORD_BYTES as u64;
138 +
    for path in paths {
139 +
        set size += WORD_BYTES as u64 + path.len as u64;
140 +
    }
141 +
    if size > mem::U32_MAX as u64 {
142 +
        throw sys::Error::TooLarge;
151 143
    }
152 144
    let mut bytes: *mut [u8] = &mut [];
153 -
    try bytes.reserve(size, arena) catch {
145 +
    try bytes.resize(size as u32, 0, arena) catch {
146 +
        throw sys::Error::TooLarge;
147 +
    };
148 +
    let mut out = writer::new(bytes.take());
149 +
    try writePaths(&mut out, paths) catch {
154 150
        throw sys::Error::TooLarge;
155 151
    };
156 -
    // Grow within the reserved capacity, so the writes below do not allocate.
157 152
    for entry in entries {
158 153
        if entry.moduleId as u32 >= paths.len {
159 154
            throw sys::Error::Invalid;
160 155
        }
161 -
        let path = paths[entry.moduleId as u32];
162 -
        let start = bytes.len;
163 -
        try bytes.resize(start + 8, 0, arena) catch {
156 +
        try writer::word(&mut out, entry.pc as u64) catch {
164 157
            throw sys::Error::TooLarge;
165 158
        };
166 -
        mem::store32Le(&mut bytes[..], start, entry.pc);
167 -
        mem::store32Le(&mut bytes[..], start + 4, entry.offset);
168 -
        try bytes.extend(path, arena) catch {
159 +
        try writer::word(&mut out, entry.offset as u64) catch {
169 160
            throw sys::Error::TooLarge;
170 161
        };
171 -
        try bytes.append(0, arena) catch {
162 +
        try writer::word(&mut out, entry.moduleId as u64) catch {
172 163
            throw sys::Error::TooLarge;
173 164
        };
174 165
    }
175 166
    let name = try alloc::concatBytes(arena, &[output, ".debug"]) catch {
176 167
        throw sys::Error::TooLarge;
177 168
    };
178 -
    try fs::writeFile(&name[..], &bytes[..]);
169 +
    try fs::writeFile(&name[..], &out.bytes[.. out.offset]);
170 +
}
171 +
172 +
/// Write a source path table: a `u32` count and length-prefixed paths.
173 +
export fn writePaths(out: &mut writer::Writer, paths: &[*[u8]]) throws (binary::Error) {
174 +
    try writer::word(out, paths.len as u64);
175 +
    for path in paths {
176 +
        try writer::bytes(out, path);
177 +
    }
179 178
}