Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
cd1f2be
Tests: Add the ir-first-feature skill and the globals C reference
r41k0u Sep 1, 2026
3d8a211
Core: Register @bpfglobal variables in a symbol table
r41k0u Sep 1, 2026
e95ee71
Core: Resolve @bpfglobal reads in expressions and f-strings
r41k0u Sep 1, 2026
930594d
Core: Support writes to @bpfglobal variables via the global statement
r41k0u Sep 1, 2026
4ee96e7
Core: Support augmented assignment by desugaring to x = x op v
r41k0u Sep 1, 2026
26ee4d4
Core: Emit debug info for globals so llc produces BTF VAR and DATASEC
r41k0u Sep 1, 2026
c0fda58
Tests: Cover global variable reads, writes, and the error cases
r41k0u Sep 1, 2026
20b9bb9
Fix skill to ask Python path
r41k0u Sep 3, 2026
7f5773e
Core: Make XDP return names respect local and global shadowing
r41k0u Sep 3, 2026
274bac9
Tests: Pin name-resolution order under vmlinux collisions
r41k0u Sep 3, 2026
aa321db
Core: Lower augmented assignment directly instead of desugaring
r41k0u Sep 3, 2026
dc9aecf
Core: Coerce integer width when assigning to a struct field
r41k0u Sep 3, 2026
1b9adc7
Tests: Pin augmented assignment on struct fields and the shadowing error
r41k0u Sep 3, 2026
e8ab118
Tests: Record the lower-do-not-desugar house style in the skill
r41k0u Sep 3, 2026
f5c4dec
Core: Unify the Python-operator tables in expr/operators.py
r41k0u Sep 3, 2026
f4ec12d
Core: Resolve assignment targets local-first, and reject global on a …
r41k0u Sep 3, 2026
2b77c40
Core: Bind declared globals in local_sym_tab instead of a per-functio…
r41k0u Sep 3, 2026
526f113
Core: Give every symbol kind a common Symbol base class
r41k0u Sep 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions .claude/skills/ir-first-feature/SKILL.md
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.
16 changes: 13 additions & 3 deletions pythonbpf/allocation_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import ctypes
from llvmlite import ir
from .local_symbol import LocalSymbol
from .symbols import LocalSymbol
from pythonbpf.helper import HelperHandlerRegistry
from pythonbpf.vmlinux_parser.dependency_node import Field
from .expr import VmlinuxHandlerRegistry
Expand Down Expand Up @@ -49,11 +49,21 @@ def handle_assign_allocation(compilation_context, builder, stmt, local_sym_tab):
continue

var_name = target.id
# Skip if already allocated

# Already bound in this scope: a parameter, an earlier assignment, or a
# `global` declaration (whose slot is the GlobalVariable). No slot needed.
if var_name in local_sym_tab:
logger.debug(f"Variable {var_name} already allocated, skipping")
logger.debug(f"'{var_name}' already bound, no allocation needed")
continue

# Not declared `global`, yet named like one: in real Python this would
# create a shadowing local. Refuse rather than guess which was meant.
if var_name in compilation_context.bpf_globals:
raise SyntaxError(
f"assignment to '{var_name}' shadows the BPF global of the same "
f"name — add 'global {var_name}' to write to it"
)

# Determine type and allocate based on rval
if isinstance(rval, ast.Call):
_allocate_for_call(
Expand Down
8 changes: 8 additions & 0 deletions pythonbpf/assign_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def handle_struct_field_assignment(
logger.info(f"Copied string to char array {var_name}.{field_name}")
return

# Same implicit widening/truncation as assignment to a local: expressions
# evaluate in i64, but a field may be narrower.
if isinstance(val_type, ir.IntType) and isinstance(field_type, ir.IntType):
if val_type.width < field_type.width:
val = builder.sext(val, field_type)
elif val_type.width > field_type.width:
val = builder.trunc(val, field_type)

# Regular assignment
builder.store(val, field_ptr)
logger.info(f"Assigned to struct field {var_name}.{field_name}")
Expand Down
2 changes: 2 additions & 0 deletions pythonbpf/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
if TYPE_CHECKING:
from pythonbpf.structs.struct_type import StructType
from pythonbpf.maps.maps_utils import MapSymbol
from pythonbpf.symbols import BpfGlobalSymbol

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -66,6 +67,7 @@ def __init__(self, module: ir.Module):
self.global_sym_tab: list[ir.GlobalVariable] = []
self.structs_sym_tab: dict[str, "StructType"] = {}
self.map_sym_tab: dict[str, "MapSymbol"] = {}
self.bpf_globals: dict[str, "BpfGlobalSymbol"] = {}

# Helper management
self.scratch_pool = ScratchPoolManager()
Expand Down
2 changes: 2 additions & 0 deletions pythonbpf/expr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .expr_pass import eval_expr, handle_expr, get_operand_value
from .type_normalization import convert_to_bool, get_base_type_and_depth
from .ir_ops import deref_to_depth, access_struct_field
from .operators import apply_binop
from .call_registry import CallHandlerRegistry
from .vmlinux_registry import VmlinuxHandlerRegistry

Expand All @@ -10,6 +11,7 @@
"convert_to_bool",
"get_base_type_and_depth",
"deref_to_depth",
"apply_binop",
"access_struct_field",
"get_operand_value",
"CallHandlerRegistry",
Expand Down
60 changes: 33 additions & 27 deletions pythonbpf/expr/expr_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pythonbpf.type_deducer import ctypes_to_ir, is_ctypes
from .call_registry import CallHandlerRegistry
from .ir_ops import deref_to_depth, access_struct_field
from .operators import apply_binop, UNARY_OPS, BOOL_OPS
from .type_normalization import (
convert_to_bool,
handle_comparator,
Expand All @@ -22,12 +23,19 @@
# ============================================================================


def _handle_name_expr(expr: ast.Name, local_sym_tab: Dict, builder: ir.IRBuilder):
def _handle_name_expr(
expr: ast.Name, compilation_context, local_sym_tab: Dict, builder: ir.IRBuilder
):
"""Handle ast.Name expressions."""
if expr.id in local_sym_tab:
var = local_sym_tab[expr.id].var
val = builder.load(var)
return val, local_sym_tab[expr.id].ir_type
elif expr.id in compilation_context.bpf_globals:
# A @bpfglobal
sym = compilation_context.bpf_globals[expr.id]
val = builder.load(sym.var)
return val, sym.ir_type
else:
# Check if it's a vmlinux enum/constant
vmlinux_result = VmlinuxHandlerRegistry.handle_name(expr.id)
Expand Down Expand Up @@ -78,7 +86,9 @@ def _handle_attribute_expr(
var_ptr, var_type, var_metadata = local_sym_tab[var_name]
logger.info(f"Loading attribute {attr_name} from variable {var_name}")
logger.info(
f"Variable type: {var_type}, Variable ptr: {var_ptr}, Variable Metadata: {var_metadata}"
f"Variable type: {var_type}, Variable ptr: {
var_ptr
}, Variable Metadata: {var_metadata}"
)
if (
hasattr(var_metadata, "__module__")
Expand All @@ -96,7 +106,9 @@ def _handle_attribute_expr(

elif isinstance(var_metadata, Field):
logger.error(
f"Cannot access field '{attr_name}' on already-loaded field value '{var_name}'"
f"Cannot access field '{attr_name}' on already-loaded field value '{
var_name
}'"
)
return None

Expand Down Expand Up @@ -175,6 +187,9 @@ def get_operand_value(func, compilation_context, operand, builder, local_sym_tab
else:
val = deref_to_depth(func, builder, var, depth)
return val
elif operand.id in compilation_context.bpf_globals:
# A @bpfglobal: plain load off the global symbol.
return builder.load(compilation_context.bpf_globals[operand.id].var)
else:
# Check if it's a vmlinux enum/constant
vmlinux_result = VmlinuxHandlerRegistry.handle_name(operand.id)
Expand Down Expand Up @@ -223,25 +238,7 @@ def _handle_binary_op_impl(func, compilation_context, rval, builder, local_sym_t
right = builder.sext(right, ir.IntType(64))

# Map AST operation nodes to LLVM IR builder methods
op_map = {
ast.Add: builder.add,
ast.Sub: builder.sub,
ast.Mult: builder.mul,
ast.Div: builder.sdiv,
ast.Mod: builder.srem,
ast.LShift: builder.shl,
ast.RShift: builder.lshr,
ast.BitOr: builder.or_,
ast.BitXor: builder.xor,
ast.BitAnd: builder.and_,
ast.FloorDiv: builder.udiv,
}

if type(op) in op_map:
result = op_map[type(op)](left, right)
return result
else:
raise SyntaxError("Unsupported binary operation")
return apply_binop(builder, op, left, right)


def _handle_binary_op(
Expand Down Expand Up @@ -304,7 +301,9 @@ def _handle_ctypes_call(
# Get the IR type from the value itself
actual_ir_type = value.type
logger.info(
f"Converting vmlinux field {val_type.name} (IR type: {actual_ir_type}) to {call_type}"
f"Converting vmlinux field {val_type.name} (IR type: {actual_ir_type}) to {
call_type
}"
)
else:
actual_ir_type = val_type
Expand All @@ -317,7 +316,9 @@ def _handle_ctypes_call(
if actual_ir_type.width < expected_type.width:
value = builder.sext(value, expected_type)
logger.info(
f"Sign-extended from i{actual_ir_type.width} to i{expected_type.width}"
f"Sign-extended from i{actual_ir_type.width} to i{
expected_type.width
}"
)
elif actual_ir_type.width > expected_type.width:
value = builder.trunc(value, expected_type)
Expand All @@ -329,7 +330,9 @@ def _handle_ctypes_call(
pass
else:
raise ValueError(
f"Type mismatch: expected {expected_type}, got {actual_ir_type} (original type: {val_type})"
f"Type mismatch: expected {expected_type}, got {
actual_ir_type
} (original type: {val_type})"
)

return value, expected_type
Expand Down Expand Up @@ -373,7 +376,7 @@ def _handle_unary_op(
local_sym_tab,
):
"""Handle ast.UnaryOp expressions."""
if not isinstance(expr.op, ast.Not) and not isinstance(expr.op, ast.USub):
if not isinstance(expr.op, UNARY_OPS):
logger.error("Only 'not' and '-' unary operators are supported")
return None

Expand Down Expand Up @@ -516,6 +519,9 @@ def _handle_boolean_op(
):
"""Handle `and` and `or` boolean operations."""

if not isinstance(expr.op, BOOL_OPS):
logger.error(f"Unsupported boolean operator: {type(expr.op).__name__}")
return None
if isinstance(expr.op, ast.And):
return _handle_and_op(func, builder, expr, local_sym_tab, compilation_context)
elif isinstance(expr.op, ast.Or):
Expand Down Expand Up @@ -662,7 +668,7 @@ def eval_expr(

logger.info(f"Evaluating expression: {ast.dump(expr)}")
if isinstance(expr, ast.Name):
return _handle_name_expr(expr, local_sym_tab, builder)
return _handle_name_expr(expr, compilation_context, local_sym_tab, builder)
elif isinstance(expr, ast.Constant):
return _handle_constant_expr(compilation_context, builder, expr)
elif isinstance(expr, ast.Call):
Expand Down
55 changes: 55 additions & 0 deletions pythonbpf/expr/operators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Every Python operator PythonBPF understands, and the IR it maps to.

This is the single registry. Binary operators map to IRBuilder method names,
comparisons map to icmp predicates, and the unary/boolean operators are listed
here even though their lowering is structural (they need convert_to_bool or
short-circuit control flow, so they live in expr_pass): if an operator is not
in this file, the compiler does not support it. Add new operators here first.
"""

import ast

# ast.BinOp.op class -> llvmlite IRBuilder method name.
# Shared by binary-op evaluation and augmented assignment.
BINOP_METHODS = {
ast.Add: "add",
ast.Sub: "sub",
ast.Mult: "mul",
ast.Div: "sdiv",
ast.Mod: "srem",
ast.LShift: "shl",
ast.RShift: "lshr",
ast.BitOr: "or_",
ast.BitXor: "xor",
ast.BitAnd: "and_",
ast.FloorDiv: "udiv",
}

# ast.Compare op class -> icmp predicate string.
COMPARISON_OPS = {
ast.Eq: "==",
ast.NotEq: "!=",
ast.Lt: "<",
ast.LtE: "<=",
ast.Gt: ">",
ast.GtE: ">=",
ast.Is: "==",
ast.IsNot: "!=",
}

# Lowered structurally in expr_pass (need convert_to_bool / control flow).
UNARY_OPS = (ast.Not, ast.USub)
BOOL_OPS = (ast.And, ast.Or)


def apply_binop(builder, op, left, right):
"""Emit the LLVM instruction for a Python binary operator."""
method = BINOP_METHODS.get(type(op))
if method is None:
raise SyntaxError(f"Unsupported binary operation: {type(op).__name__}")
return getattr(builder, method)(left, right)


def comparison_predicate(op):
"""icmp predicate for a Python comparison operator, or None if unsupported."""
return COMPARISON_OPS.get(type(op))
Loading
Loading