rv64: Name the limit in capacity errors

7efe9eddbb91e703fbd77a6075679dabda145ce56a9cd4f61699af119909b6ee
A capacity error during code generation printed only "code generation
capacity exceeded", without the limit or the function involved.

`Error::Capacity` now carries a `Limit` that says which limit was
exceeded. The compiler prints its description and, inside a function,
the name of the function being generated.

Assisted-by: Claude:opus-5.5
Alexis Sellier committed ago 1 parent 9a164eae
compiler/radiance/codegen.rad +24 -2
34 34
        fnArena: &mut alloc::Arena
35 35
    ) throws (lower::LowerError) {
36 36
        try generateFunction(ctx, func, role, layouts, fnArena) catch error {
37 37
            resetFnArena(fnArena);
38 38
            printError(error);
39 +
            io::printError("radiance: while generating function ");
40 +
            io::printError(func.name);
41 +
            io::printError("\n");
39 42
            throw lower::LowerError::OutputFailed;
40 43
        };
41 44
        resetFnArena(fnArena);
42 45
    }
43 46
}
253 256
        else => {}
254 257
    }
255 258
    try rv64::generateFunction(&mut ctx.generator, func, layouts, fnArena);
256 259
}
257 260
261 +
/// Describe the limit that code generation exceeded.
262 +
fn limitName(limit: rv64::Limit) -> *[u8] {
263 +
    match limit {
264 +
        case rv64::Limit::FrameSize =>
265 +
            return "stack frame or reserved object exceeds the 32-bit offset range",
266 +
        case rv64::Limit::StackAlignment =>
267 +
            return "reserved object alignment is invalid or above the stack alignment",
268 +
        case rv64::Limit::BlockArgs => return "too many block arguments",
269 +
        case rv64::Limit::SpillMoves =>
270 +
            return "block arguments move between dependent spill slots",
271 +
        case rv64::Limit::BlockTable    => return "too many blocks in one function",
272 +
        case rv64::Limit::FunctionTable => return "too many functions",
273 +
        case rv64::Limit::ReadOnlyData  => return "read-only data exceeds its output buffer",
274 +
    }
275 +
}
276 +
258 277
/// Report a code generation failure.
259 278
export fn printError(error: rv64::Error) {
260 279
    match error {
261 280
        case rv64::Error::Allocation =>
262 281
            io::printError("radiance: code generation allocation failed\n"),
263 -
        case rv64::Error::Capacity =>
264 -
            io::printError("radiance: code generation capacity exceeded\n"),
282 +
        case rv64::Error::Capacity(limit) => {
283 +
            io::printError("radiance: code generation capacity exceeded: ");
284 +
            io::printError(limitName(limit));
285 +
            io::printError("\n");
286 +
        }
265 287
        case rv64::Error::Symbol => io::printError("radiance: invalid code generation symbol\n"),
266 288
        case rv64::Error::Data(error) => {
267 289
            match error {
268 290
                case data::Error::Symbol => io::printError("radiance: missing data symbol\n"),
269 291
                case data::Error::Capacity =>
lib/std/arch/rv64.rad +22 -3
83 83
/// Previous privilege field mask in mstatus.
84 84
export constant MSTATUS_MPP_MASK: u64 = 3 << 11;
85 85
/// Previous privilege field value for machine mode.
86 86
export constant MSTATUS_MPP_MACHINE: u64 = 3 << 11;
87 87
88 +
/// Fixed limit that code generation exceeded.
89 +
export union Limit: Copy {
90 +
    /// A stack frame or reserved object exceeds the 32-bit offset range.
91 +
    FrameSize,
92 +
    /// A reserved object has an invalid alignment or one above the stack alignment.
93 +
    StackAlignment,
94 +
    /// A block passes more arguments than the selector supports.
95 +
    BlockArgs,
96 +
    /// Block arguments move between spill slots that depend on each other.
97 +
    SpillMoves,
98 +
    /// The block label table of one function is full.
99 +
    BlockTable,
100 +
    /// The function label table is full.
101 +
    FunctionTable,
102 +
    /// The read-only data prefix does not fit its output buffer.
103 +
    ReadOnlyData,
104 +
}
105 +
88 106
/// Recoverable code-generation failure.
89 107
export union Error: Copy {
90 108
    /// The code-generation or function arena is full.
91 109
    Allocation,
92 -
    /// A fixed output or metadata table is full.
93 -
    Capacity,
110 +
    /// A fixed output or metadata table is full, or a value exceeds a
111 +
    /// representation limit.
112 +
    Capacity(Limit),
94 113
    /// A required symbol is missing or invalid.
95 114
    Symbol,
96 115
    /// A branch or address exceeds its instruction range.
97 116
    Relocation,
98 117
    /// The IL has a form that the code generator cannot translate. IL that the
516 535
    try emit::patchCalls(&mut generator.e);
517 536
    try emit::patchAddrLoads(&mut generator.e, &dataSymMap, codeBase);
518 537
519 538
    // Emit data sections.
520 539
    if roDataPrefix.len > roDataBuf.len {
521 -
        throw Error::Capacity;
540 +
        throw Error::Capacity(Limit::ReadOnlyData);
522 541
    }
523 542
    try! mem::copy(roDataBuf, roDataPrefix);
524 543
525 544
    let roDataSize = try data::emitSectionAtOffset(
526 545
        globalData, &dataSymMap, &generator.e.labels, codeBase, roDataBuf, true, roDataPrefix.len
lib/std/arch/rv64/emit.rad +3 -3
186 186
    }
187 187
    // Compute total frame size. Includes RA and FP registers.
188 188
    let savedRegs = mem::popCount(usedCalleeSaved) + 2;
189 189
    let savedSize = savedRegs * super::DWORD_SIZE;
190 190
    if localSize > MAX_I32_ADDR as i32 - savedSize - (super::STACK_ALIGNMENT - 1) {
191 -
        throw super::Error::Capacity;
191 +
        throw super::Error::Capacity(super::Limit::FrameSize);
192 192
    }
193 193
    let totalSize = mem::alignUpI32(localSize + savedSize, super::STACK_ALIGNMENT);
194 194
    set frame.totalSize = totalSize;
195 195
196 196
    // Build list of callee-saved registers with offsets.
288 288
export fn recordBlock(e: &mut Emitter, blockIdx: u32) throws (super::Error) {
289 289
    if e.code.len > MAX_CODE_LEN {
290 290
        throw super::Error::Relocation;
291 291
    }
292 292
    try labels::recordBlock(&mut e.labels, blockIdx, e.code.len as i32 * super::INSTR_SIZE) catch {
293 -
        throw super::Error::Capacity;
293 +
        throw super::Error::Capacity(super::Limit::BlockTable);
294 294
    };
295 295
}
296 296
297 297
/// Record a function's code offset for call resolution.
298 298
export fn recordFuncOffset(e: &mut Emitter, name: *[u8]) throws (super::Error) {
304 304
export fn recordFuncOffsetAt(e: &mut Emitter, name: *[u8], index: u32) throws (super::Error) {
305 305
    if index > MAX_CODE_LEN {
306 306
        throw super::Error::Relocation;
307 307
    }
308 308
    try dict::insert(&mut e.labels.funcs, name, index as i32 * super::INSTR_SIZE) catch {
309 -
        throw super::Error::Capacity;
309 +
        throw super::Error::Capacity(super::Limit::FunctionTable);
310 310
    };
311 311
}
312 312
313 313
/// Record a function's start position for printing.
314 314
export fn recordFunc(e: &mut Emitter, name: *[u8]) throws (super::Error) {
lib/std/arch/rv64/isel.rad +7 -7
1025 1025
                    let info = layouts[layout];
1026 1026
                    if info.alignment == 0
1027 1027
                        or (info.alignment & (info.alignment - 1)) <> 0
1028 1028
                        or info.alignment > super::STACK_ALIGNMENT as u32
1029 1029
                    {
1030 -
                        throw super::Error::Capacity;
1030 +
                        throw super::Error::Capacity(super::Limit::StackAlignment);
1031 1031
                    }
1032 1032
                    let aligned = mem::checkedAlignUp(offset as u32, info.alignment) else {
1033 -
                        throw super::Error::Capacity;
1033 +
                        throw super::Error::Capacity(super::Limit::FrameSize);
1034 1034
                    };
1035 1035
                    let end = aligned as u64 + info.size as u64;
1036 1036
                    if end > I32_MAX as u64 {
1037 -
                        throw super::Error::Capacity;
1037 +
                        throw super::Error::Capacity(super::Limit::FrameSize);
1038 1038
                    }
1039 1039
                    set offset = end as i32;
1040 1040
                }
1041 1041
                else => {}
1042 1042
            }
1054 1054
    arena: &mut alloc::Arena
1055 1055
) throws (super::Error) {
1056 1056
    // Pre-scan the reserves to give them fixed frame slots.
1057 1057
    let reserved = try reserveSize(func, layouts);
1058 1058
    if reserved > I32_MAX as i32 - ralloc.spill.frameSize {
1059 -
        throw super::Error::Capacity;
1059 +
        throw super::Error::Capacity(super::Limit::FrameSize);
1060 1060
    }
1061 1061
    let isLeaf = func.isLeaf;
1062 1062
    // Compute frame layout from spill slots, reserve slots, and used callee-saved registers.
1063 1063
    let frame = try emit::computeFrame(
1064 1064
        ralloc.spill.frameSize + reserved,
2221 2221
    }
2222 2222
    if args.len == 0 {
2223 2223
        return;
2224 2224
    }
2225 2225
    if args.len > MAX_BLOCK_ARGS {
2226 -
        throw super::Error::Capacity;
2226 +
        throw super::Error::Capacity(super::Limit::BlockArgs);
2227 2227
    }
2228 2228
    // The parallel-move resolver only handles register destinations. Keep eager
2229 2229
    // stores for independent spill slots, but reject dependencies that would
2230 2230
    // require stack staging rather than silently miscompiling them.
2231 2231
    for arg, i in args {
2245 2245
                            if sourceSlot == dstSlot {
2246 2246
                                if let sourceDstSlot = regalloc::spill::spillSlot(
2247 2247
                                    &s.ralloc.spill, block.params[j].value
2248 2248
                                ) {
2249 2249
                                    if sourceDstSlot <> dstSlot and j >= i {
2250 -
                                        throw super::Error::Capacity;
2250 +
                                        throw super::Error::Capacity(super::Limit::SpillMoves);
2251 2251
                                    }
2252 2252
                                } else {
2253 -
                                    throw super::Error::Capacity;
2253 +
                                    throw super::Error::Capacity(super::Limit::SpillMoves);
2254 2254
                                }
2255 2255
                            }
2256 2256
                        }
2257 2257
                    }
2258 2258
                }
lib/std/arch/rv64/tests/bounds.rad +8 -2
60 60
    try! funcEntries.resize(2, dict::Entry { key: &[], value: 0 }, &mut arena);
61 61
    let mut e = try! emit::emitter(arena, false, labels::init(&mut [], funcEntries));
62 62
    try! emit::recordFuncOffset(&mut e, "p::first");
63 63
    let mut failed = false;
64 64
    try emit::recordFuncOffset(&mut e, "p::second") catch err {
65 -
        try testing::expect(err == rv64::Error::Capacity);
65 +
        let case rv64::Error::Capacity(limit) = err else {
66 +
            throw testing::TestError::Failed;
67 +
        };
68 +
        try testing::expect(limit == rv64::Limit::FunctionTable);
66 69
        set failed = true;
67 70
    };
68 71
    try testing::expect(failed);
69 72
    try testing::expect(dict::get(&e.labels.funcs, "p::first") == 0);
70 73
}
789 792
    let mut alignedFns: *mut [il::Fn] = &mut [];
790 793
    try! alignedFns.append(voidFn("f", &alignedBlocks[..], &[]), &mut arena4);
791 794
    let over = [il::Layout { size: 8, alignment: 0x80000000, slots: &[] }];
792 795
    let mut capacity = false;
793 796
    try generateAll(alignedFns, &over[..]) catch err {
794 -
        try testing::expect(err == rv64::Error::Capacity);
797 +
        let case rv64::Error::Capacity(limit) = err else {
798 +
            throw testing::TestError::Failed;
799 +
        };
800 +
        try testing::expect(limit == rv64::Limit::StackAlignment);
795 801
        set capacity = true;
796 802
    };
797 803
    try testing::expect(capacity);
798 804
}
799 805