Skip to content

Conversation

@tean-lai
Copy link
Contributor

Resolves #506

Copy link
Owner

@sampsyo sampsyo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @tean-lai—I'm glad you got something working! It's too bad that you weren't able to do an empirical evaluation, but I hope you learned something about generating x86 assembly.



## The Tricky Parts
Although at a high level, things were simple, the details were tricky to get right. The first big hurdle was actually getting a compiled program to run. This required support for several Bril instructions, you would need to support constants, and either printing or returning in order to observe that some changes had happened. But it also required support for some calling conventions to be supported, otherwise the program wouldn't terminate. What helped got me over this hurdle proved to be quite useful in general for developing this compiler. I spent a lot of time modeling Bril programs as C programs and comparing my Bril -> x86 results with Clang's C -> x86 results. This helped me find stuff like the following snippet of text, which I would include at the beginning of every file for macs:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What helped got me over this hurdle proved to be quite useful in general for developing this compiler.

I don't understand this sentence… what exactly was helpful, and with what?



## The Tricky Parts
Although at a high level, things were simple, the details were tricky to get right. The first big hurdle was actually getting a compiled program to run. This required support for several Bril instructions, you would need to support constants, and either printing or returning in order to observe that some changes had happened. But it also required support for some calling conventions to be supported, otherwise the program wouldn't terminate. What helped got me over this hurdle proved to be quite useful in general for developing this compiler. I spent a lot of time modeling Bril programs as C programs and comparing my Bril -> x86 results with Clang's C -> x86 results. This helped me find stuff like the following snippet of text, which I would include at the beginning of every file for macs:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for macs

To be more precise: "when generating code to target macOS"


These directives seemed to vary quite a bit device-to-device; but I found comparing with a reference compiler was a reliable way of finding out what directives are necessary. Online sources often lack the specific things you need for your own device.

It was also quite tricky to get printing right. I implemented Prof. Sampson's suggestion of linking the printing with a helper [rt.c file](https://github.com/sampsyo/bril/blob/main/brilift/rt.c) to avoid printing Booleans by hand. To get linking right, again it helped a lot to model the behavior I would expect in an equivalent c program, and guide my implementation to match that output.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c -> C

ret;
}
```
This program takes in a single int as a command line argument, and keeps printing its value and incrementing it until it hits 10. The tricky part comes from the fact that this function also calls itself. So this means the assembly code for this function must somehow handle both command line parsing and support proper calling conventions when it's called as a normal function. More concretely, in x86, it's expected that the main function's first and second parameters are `argc` and `argv` respectively. But we also want the main function's first function to be `x`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've stated the problem here, but what was the solution?

FWIW, the solution used in other native compilers is to add a "main wrapper" function that calls the true Bril @main function. The "main wrapper" parses the CLI arguments and then immediately calls @main.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up coming to the same solution, I could include that in the blog post

This program takes in a single int as a command line argument, and keeps printing its value and incrementing it until it hits 10. The tricky part comes from the fact that this function also calls itself. So this means the assembly code for this function must somehow handle both command line parsing and support proper calling conventions when it's called as a normal function. More concretely, in x86, it's expected that the main function's first and second parameters are `argc` and `argv` respectively. But we also want the main function's first function to be `x`.

## Correctness
I wanted to automate checking for correctness in comparison to the interpreter, but I didn't get to it in time.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's too bad that you didn't get to test for correctness, but is there a way you could give a brief summary of how complete you think your implementation is? As in, how much of the Bril language do you think you covered, and what do you think is still not working? Just a couple of sentences of theorizing about this could help clarify to readers where you think things stand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woah, I'm sorry, that section is completely wrong. I had rephrased it but I guess it didn't save. I didn't have time to compare to other AOT compilers like you suggested in the proposal PR, but I did test it for correctness and the output matches every core benchmark when compared to the interpreter.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great news!

@tean-lai tean-lai marked this pull request as draft May 15, 2025 18:17
@sampsyo
Copy link
Owner

sampsyo commented May 16, 2025

I see you've marked this PR as a draft—let me know when it's ready for me to take another look and I'll publish it.

@tean-lai tean-lai marked this pull request as ready for review May 16, 2025 14:47
@sampsyo sampsyo added the 2025sp label May 16, 2025
@tean-lai
Copy link
Contributor Author

Hi, I think this is ready for another look

@sampsyo
Copy link
Owner

sampsyo commented May 19, 2025

Looks good; I'll publish this now!

@sampsyo sampsyo merged commit 26a6124 into sampsyo:2025sp May 19, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Project Proposal: Bril2x86

2 participants