For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
Style Guide
Rules that PRs must adhere to.
Code style
- Look for existing mechanisms in the codebase.
- Consider whether any specific logic you’re trying to achieve might already need to be done by other parts of the codebase. Make sure to reuse those existing mechanisms instead of repeating heavy logic.
- Watch out for known omissions and past trials.
- Why was an existing mechanism not applied in this specific place?
- Did someone try something like this before and reverted it?
- Ensure the solution fits the larger picture.
- Is this problem better solved at another layer of abstraction?
- Does this problem benefit from more semantic information from earlier passes?
- Is this an optimization, or fulfilling something that the language semantics guaranteed?
- If someone familiar with the overall architecture of the compiler came in and guessed at where this piece of logic is implemented, is this where they would most expect it?
Comments
- Do not repeat what is obvious in the code already.
- Respect abstraction boundaries.
- Do not talk about implementation details when describing an interface.
- Do not talk about details inside another far-away component (e.g. another pass). The need to talk about it is an architectural design smell.
- Use existing vocabulary.
- Do not come up with new terms that are not used elsewhere in this codebase.
- If an existing vocabulary is really undesirable, a dedicated rewording PR is required.
- State the current behavior, not the history.
- Comments should describe the current code. Either describe an interface, or explain logic that is unexpected. Do not use it to keep a log.
- Practice human-oriented writing style.
- Comments should be pleasant to read: Be concise, straightforward, clear, and use simple flow of logic and simple grammar.
PR stack organization
When submitting changes, make sure to split up your changes into multiple PRs.
A PR stack is used to organize related diffs in an ordered way that makes it easy for humans to follow.
- Split out mechanical changes
- Changes that merely re-organize code should be separated out so that actual logic changes aren't lost in a sea of mechanical changes. Preferably land them first in a stack of PRs, or land them as follow-ups to a stack.
- Land narrower-scoped “set up” code first. The main feature you want to achieve should simply drop in as a simple last step.
- Each PR should target a single component so that its changes are tested and
reviewed in isolation. If a change touches multiple components due to a shared
interface, only one component should contain non-mechanical changes. Major
components in the compiler include:
- Parser
- Elaborator
- Interpreter
- An individual pass
- Key utilities
- MLIR dialects (new ops, attrs, types, interfaces, etc.)
- Interface with target backends
- Please also consider using multiple stacks.
- Each stack should be review-able by one person in one sitting. Do not “ready” multiple dependent stacks at a time.
- At the top of the stack, explain the entire stack succinctly.
- One bullet point per PR in the stack. One sentence per bullet point.
Tests
- Tests are required for each non-NFC PR. E.g.
- Pass changes require a pass kgen-opt test.
- MLIR changes require a roundtrip test.
- Parser changes require a kgen-translate test.
- Tests must fail before the change.
- Minimal test surface.
- E.g. Do not rely on too much behavior that’s unrelated to the repro.
- Do not explain each test in the PR description.