<div dir="ltr"><div><div><div>Greetings,<br><br></div>I implemented a proof of concept of something I'm calling "error return traces" in my frontend (screenshot and explanation at this link): <a href="https://github.com/zig-lang/zig/pull/684">https://github.com/zig-lang/zig/pull/684</a><br><br></div>Now I'm trying to minimize the runtime size.<br><br></div>Here's some example IR that I generate:<br><div><div><div><div><br>define internal fastcc i16 @baz1(%StackTrace* nonnull) unnamed_addr #2 !dbg !69 {<br>Entry:<br>  %1 = getelementptr inbounds %StackTrace, %StackTrace* %0, i32 0, i32 0, !dbg !70<br>  %2 = getelementptr inbounds %StackTrace, %StackTrace* %0, i32 0, i32 1, !dbg !70<br>  %3 = load i64, i64* %1, align 8, !dbg !70<br>  %4 = urem i64 %3, 31, !dbg !70<br>  %5 = getelementptr inbounds [31 x i64], [31 x i64]* %2, i64 0, i64 %4, !dbg !70<br>  store i64 ptrtoint (i8* blockaddress(@baz1, %ReturnError) to i64), i64* %5, align 8, !dbg !70<br>  %6 = add i64 %3, 1, !dbg !70<br>  store i64 %6, i64* %1, align 8, !dbg !70<br>  br label %ReturnError, !dbg !70<br><br>ReturnError:                                      ; preds = %Entry<br>  ret i16 1, !dbg !70<br>}</div><div><br></div><div><br></div><div>Everywhere that we can return an error from a function, we generate code like this. Here it is in pseudocode form:<br></div><div><br></div><div>stack_trace_ptr.addresses[stack_trace_ptr.index % 31] = addressOfReturnInstruction;<br>stack_trace_ptr.index += 1;<br>return error_code;</div><div><br></div><div>I'd like to try to extract some of this out to make a smaller binary size. If we extract pseudocode into a function:<br></div><div><br></div><div><br>%0 = tail call func(ptr, addressOfReturnInstruction, error_code);</div><div>return %0;</div><div><br></div><div>func(ptr, address, error_code) {<br>  ptr.addresses[ptr.index % 31] = address;<br>  ptr.index += 1;</div><div>  return error_code;<br></div><div>}<br></div></div><div><br></div><div>The function implementation is so simple, I think we should be able to set the return value into the return register and do a single `jmp` instruction, and then have func return to the caller's return address.</div><div><br></div><div>I'd like this to happen if possible even with no optimization passes.</div><div><br></div><div>Is this the best way to represent it? Passing error_code as a parameter to the function and using tail call?</div><div><br></div><div>Thanks for looking,</div><div>Andrew </div></div></div></div>