Repository navigation
Add the dev workflow skill, implement support for all global vars using @bpfglobal (both const and non-const) #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,144
−87
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
cd1f2be
Tests: Add the ir-first-feature skill and the globals C reference
r41k0u 3d8a211
Core: Register @bpfglobal variables in a symbol table
r41k0u e95ee71
Core: Resolve @bpfglobal reads in expressions and f-strings
r41k0u 930594d
Core: Support writes to @bpfglobal variables via the global statement
r41k0u 4ee96e7
Core: Support augmented assignment by desugaring to x = x op v
r41k0u 26ee4d4
Core: Emit debug info for globals so llc produces BTF VAR and DATASEC
r41k0u c0fda58
Tests: Cover global variable reads, writes, and the error cases
r41k0u 20b9bb9
Fix skill to ask Python path
r41k0u 7f5773e
Core: Make XDP return names respect local and global shadowing
r41k0u 274bac9
Tests: Pin name-resolution order under vmlinux collisions
r41k0u aa321db
Core: Lower augmented assignment directly instead of desugaring
r41k0u dc9aecf
Core: Coerce integer width when assigning to a struct field
r41k0u 1b9adc7
Tests: Pin augmented assignment on struct fields and the shadowing error
r41k0u e8ab118
Tests: Record the lower-do-not-desugar house style in the skill
r41k0u f5c4dec
Core: Unify the Python-operator tables in expr/operators.py
r41k0u f4ec12d
Core: Resolve assignment targets local-first, and reject global on a …
r41k0u 2b77c40
Core: Bind declared globals in local_sym_tab instead of a per-functio…
r41k0u 526f113
Core: Give every symbol kind a common Symbol base class
r41k0u 5948971
Core: Shadow a @bpfglobal with a local when a write has no global sta…
r41k0u 46062f3
Core: Reject a name used before its global declaration, as Python does
r41k0u f0a9a40
Core: Allocate a local copied from a global with the global's type
r41k0u 3a3b4b2
Core: Say what is unsupported instead of dropping it silently
r41k0u 3c3ad8d
Tests: Shadowing is a passing case now, unbound reads are the error
r41k0u dc212c3
Tests: Pin which storage each global-variable access resolves to
r41k0u d197a11
Core: Shorten the unbound-local messages and drop a comment
r41k0u File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| --- | ||
| name: ir-first-feature | ||
| description: PythonBPF's development loop for implementing a new compiler feature — write a minimal C eBPF reference, compile it to LLVM IR and read that as the spec, stop for a human syntax decision, then implement against the reference. Use whenever adding or extending a PythonBPF language feature (new statement/expression support, map types, globals, helpers, program constructs). | ||
| --- | ||
|
|
||
| # The IR-first feature loop | ||
|
|
||
| PythonBPF targets LLVM IR via llvmlite. For any new feature, clang's output for the | ||
| equivalent C is the specification — not documentation, not intuition. Follow the loop | ||
| in order; do not skip steps because the feature "looks simple". | ||
|
|
||
| ## 1. Write the C reference | ||
|
|
||
| A minimal `.bpf.c` in `tests/c-form/` exercising **only** the target feature. Small | ||
| enough that every line of the resulting IR is attributable to the feature. Prefer no | ||
| includes (define `SEC` and the `__u*` typedefs by hand) so nothing else pollutes the IR. | ||
| Cover each variant of the feature in one file (e.g. for globals: zero-init, initialized, | ||
| const, const volatile). | ||
|
|
||
| ## 2. Compile and read the IR — this is the spec | ||
|
|
||
| ```bash | ||
| clang -target bpf -O2 -g -emit-llvm -S feature.bpf.c -o feature.ll | ||
| llc -march=bpf -filetype=obj feature.ll -o feature.o | ||
| bpftool btf dump file feature.o # what must come out the far end | ||
| ``` | ||
|
|
||
| Read `feature.ll` and answer, in writing: What top-level symbols/globals appear? What | ||
| do loads/stores/calls look like in the body? What `!DI*` debug metadata exists, and | ||
| what BTF does llc manufacture from it? What did -O2 fold away, and does that folding | ||
| carry semantics (it did for `const` globals)? | ||
|
|
||
| Version discipline: llvmlite ≥0.49 emits LLVM 21/22-era attribute spellings | ||
| (`captures(none)`, not `nocapture`). Use a clang/llc generation that accepts them, and | ||
| compare against what `pythonbpf` + the CI's LLVM actually use. | ||
|
|
||
| ## 3. Diff against current PythonBPF output | ||
|
|
||
| Compile the nearest thing PythonBPF can already express and diff the `.ll`s. The delta | ||
| is the actual work item — often smaller than expected (machinery like section placement | ||
| and BTF generation frequently comes free from llc). | ||
|
|
||
| ## 4. HARD STOP — syntax is a human decision | ||
|
|
||
| Present 2–3 Pythonic syntax candidates with trade-offs (declaration site, usage site, | ||
| failure modes, precedents from FastAPI/typing/Triton-style DSLs). **Wait for a human to | ||
| choose. Never proceed on your own judgment, and never treat silence as consent.** The | ||
| maintainers own the language surface. | ||
|
|
||
| ## 5. Implement against the reference | ||
|
|
||
| Emit IR through the existing passes (`globals_pass`, `expr_pass`, `assign_pass`, | ||
| `allocation_pass`, `debuginfo/`). Verify by **diffing your emitted `.ll` against the | ||
| clang reference for the same shapes** — "it compiles and llc accepts it" is not the | ||
| bar; llc accepts plenty of subtly wrong IR. | ||
|
|
||
| ### House style: lower, don't desugar | ||
|
|
||
| Handlers walk the AST the user wrote and emit IR directly. **Do not synthesize new AST | ||
| nodes mid-compilation and feed them back through other handlers** — no | ||
| `ast.Assign(ast.BinOp(...))` conjured to make `x += v` reuse the assignment path. | ||
|
|
||
| The temptation is legitimate, so know the argument you are declining. Desugaring is a | ||
| standard compiler move (CPython itself lowers `x += v` this way), it guarantees semantic | ||
| agreement with the composed form, it is the smallest diff, and any later fix to the | ||
| composed path applies automatically. Those are real benefits. | ||
|
|
||
| They lose in this codebase for structural reasons: the passes communicate through the | ||
| source tree. Allocation runs before codegen and walks `Assign` — a synthetic `Assign` | ||
| created during codegen is invisible to it, so the two passes silently disagree about | ||
| what the function contains (it happened to be harmless for augmented assignment only | ||
| because that statement never needs a fresh slot; that is luck, not design). Synthetic | ||
| nodes carry no source location, so diagnostics point nowhere. And `ast.dump` in the logs | ||
| shows statements the user never typed, which turns every debugging session into an | ||
| archaeology exercise. | ||
|
|
||
| The resolution is to move sharing down a level: **equivalence should come from shared | ||
| value-level helpers, not shared AST.** When two constructs must agree, extract the common | ||
| logic into a helper both call — the way binary-op evaluation and augmented assignment | ||
| both use `apply_binop` for the operator table and `get_operand_value` for operands — | ||
| and let each handler resolve its own target and emit its own store. Two handlers calling | ||
| one helper is the idiom; one handler manufacturing input for another is not. | ||
|
|
||
| The operator tables themselves — binary operators, comparisons, and the supported | ||
| unary/boolean operators — live in exactly one place, `expr/operators.py`. A new operator | ||
| is added there first; if it is not in that file, the compiler does not support it. | ||
|
|
||
| ## 6. Test at the right tier | ||
|
|
||
| - Works now → `tests/passing_tests/<category>/`. | ||
| - Documents a gap → `tests/kernel_selftest_equivalent/` with a strict xfail in | ||
| `tests/test_config.toml` (level `"ir"`, `"llc"`, or `"verifier"`). | ||
| - Wrong-input behaviour → `tests/failing_tests/` with a config entry. | ||
| - Kernel verifier level runs in CI; locally it needs the user's sudo — ask, don't | ||
| assume. | ||
|
|
||
| ## House guardrails (always) | ||
|
|
||
| - **Never read/cat/grep `vmlinux.py` or `vmlinux.h`** — generated, enormous, will | ||
| exhaust context. Probe with one-liners: | ||
| `.venv/bin/python -c "import vmlinux; print(vmlinux.struct_x._fields_[:3])"` | ||
| - Ask the dev which Python binary to use, and remember that for future. The original authors use `.venv/bin/python` as their system python has no llvmlite. | ||
| - Atomic commits, `Core:`/`Tests:` subject prefixes, one logical change each. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope this is being handles by the upcoming zext-sext fix PR