Skip to content

Commit 207f714

Browse files
committed
Use scratch space to store consts passed to helpers
1 parent 5dcf670 commit 207f714

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

pythonbpf/functions/functions_pass.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from typing import Any
55
from dataclasses import dataclass
66

7-
from pythonbpf.helper import HelperHandlerRegistry, handle_helper_call
7+
from pythonbpf.helper import (
8+
HelperHandlerRegistry,
9+
handle_helper_call,
10+
reset_scratch_pool,
11+
)
812
from pythonbpf.type_deducer import ctypes_to_ir
913
from pythonbpf.binary_ops import handle_binary_op
1014
from pythonbpf.expr import eval_expr, handle_expr, convert_to_bool
@@ -353,6 +357,7 @@ def process_stmt(
353357
ret_type=ir.IntType(64),
354358
):
355359
logger.info(f"Processing statement: {ast.dump(stmt)}")
360+
reset_scratch_pool()
356361
if isinstance(stmt, ast.Expr):
357362
handle_expr(
358363
func,

pythonbpf/helper/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from .helper_utils import HelperHandlerRegistry
1+
from .helper_utils import HelperHandlerRegistry, reset_scratch_pool
22
from .bpf_helper_handler import handle_helper_call
33
from .helpers import ktime, pid, deref, XDP_DROP, XDP_PASS
44

55
__all__ = [
66
"HelperHandlerRegistry",
7+
"reset_scratch_pool",
78
"handle_helper_call",
89
"ktime",
910
"pid",

pythonbpf/helper/helper_utils.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def get_next_temp(self, local_sym_tab):
5858
f"Current counter: {self._counter}"
5959
)
6060

61+
return local_sym_tab[temp_name].var, temp_name
62+
6163

6264
_temp_pool_manager = ScratchPoolManager() # Singleton instance
6365

@@ -67,25 +69,21 @@ def reset_scratch_pool():
6769
_temp_pool_manager.reset()
6870

6971

70-
def get_next_scratch_temp(local_sym_tab):
71-
"""Get the next temporary variable name from the scratch pool"""
72-
return _temp_pool_manager.get_next_temp(local_sym_tab)
73-
74-
7572
def get_var_ptr_from_name(var_name, local_sym_tab):
7673
"""Get a pointer to a variable from the symbol table."""
7774
if local_sym_tab and var_name in local_sym_tab:
7875
return local_sym_tab[var_name].var
7976
raise ValueError(f"Variable '{var_name}' not found in local symbol table")
8077

8178

82-
def create_int_constant_ptr(value, builder, int_width=64):
79+
def create_int_constant_ptr(value, builder, local_sym_tab, int_width=64):
8380
"""Create a pointer to an integer constant."""
81+
8482
# Default to 64-bit integer
85-
int_type = ir.IntType(int_width)
86-
ptr = builder.alloca(int_type)
87-
ptr.align = int_type.width // 8
88-
builder.store(ir.Constant(int_type, value), ptr)
83+
ptr, temp_name = _temp_pool_manager.get_next_temp(local_sym_tab)
84+
logger.debug(f"Using temp variable '{temp_name}' for int constant {value}")
85+
const_val = ir.Constant(ir.IntType(int_width), value)
86+
builder.store(const_val, ptr)
8987
return ptr
9088

9189

@@ -95,7 +93,7 @@ def get_or_create_ptr_from_arg(arg, builder, local_sym_tab):
9593
if isinstance(arg, ast.Name):
9694
ptr = get_var_ptr_from_name(arg.id, local_sym_tab)
9795
elif isinstance(arg, ast.Constant) and isinstance(arg.value, int):
98-
ptr = create_int_constant_ptr(arg.value, builder)
96+
ptr = create_int_constant_ptr(arg.value, builder, local_sym_tab)
9997
else:
10098
raise NotImplementedError(
10199
"Only simple variable names are supported as args in map helpers."

0 commit comments

Comments
 (0)