-
Notifications
You must be signed in to change notification settings - Fork 4
PythonBPF: Add Compilation Context to allow parallel compilation of multiple jobs #82
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
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ec4a685
PythonBPF: Add Compilation Context to allow parallel compilation of m…
r41k0u c04e32b
Core: Remove spurious comments from functions_pass
r41k0u c22911d
Core: FIx inconsistent compilation_context usage in helper/printk_for…
r41k0u 3396d84
Core: Fix incompatible logic in helper/helper_utils
r41k0u bdcfe47
Core: Fix unnecessary arg in structs/structs_pass
r41k0u 305a8ba
Core: Fix unnecessary args and changes in maps pass
r41k0u 61bca6b
Core: Fix global pass to divide internal functions
r41k0u ccbdfee
Core: Remove unsused args in assign_pass
r41k0u 0e087b9
Core: Revert unnecessary changes to allocation_pass
r41k0u f2b9767
Core: Fix args in helper/printk_formatter
r41k0u 3f4f951
Core: Pass compilation_context to _prepare_expr_args as it calls eva…
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 |
|---|---|---|
|
|
@@ -26,9 +26,7 @@ def create_targets_and_rvals(stmt): | |
| return stmt.targets, [stmt.value] | ||
|
|
||
|
|
||
| def handle_assign_allocation( | ||
| builder, stmt, local_sym_tab, map_sym_tab, structs_sym_tab | ||
| ): | ||
| def handle_assign_allocation(compilation_context, builder, stmt, local_sym_tab): | ||
| """Handle memory allocation for assignment statements.""" | ||
|
|
||
| logger.info(f"Handling assignment for allocation: {ast.dump(stmt)}") | ||
|
|
@@ -59,7 +57,7 @@ def handle_assign_allocation( | |
| # Determine type and allocate based on rval | ||
| if isinstance(rval, ast.Call): | ||
| _allocate_for_call( | ||
| builder, var_name, rval, local_sym_tab, map_sym_tab, structs_sym_tab | ||
| builder, var_name, rval, local_sym_tab, compilation_context | ||
| ) | ||
| elif isinstance(rval, ast.Constant): | ||
| _allocate_for_constant(builder, var_name, rval, local_sym_tab) | ||
|
|
@@ -71,18 +69,17 @@ def handle_assign_allocation( | |
| elif isinstance(rval, ast.Attribute): | ||
| # Struct field-to-variable assignment (a = dat.fld) | ||
| _allocate_for_attribute( | ||
| builder, var_name, rval, local_sym_tab, structs_sym_tab | ||
| builder, var_name, rval, local_sym_tab, compilation_context | ||
| ) | ||
| else: | ||
| logger.warning( | ||
| f"Unsupported assignment value type for {var_name}: {type(rval).__name__}" | ||
| ) | ||
|
|
||
|
|
||
| def _allocate_for_call( | ||
| builder, var_name, rval, local_sym_tab, map_sym_tab, structs_sym_tab | ||
| ): | ||
| def _allocate_for_call(builder, var_name, rval, local_sym_tab, compilation_context): | ||
| """Allocate memory for variable assigned from a call.""" | ||
| structs_sym_tab = compilation_context.structs_sym_tab | ||
|
|
||
| if isinstance(rval.func, ast.Name): | ||
| call_type = rval.func.id | ||
|
|
@@ -149,17 +146,19 @@ def _allocate_for_call( | |
| elif isinstance(rval.func, ast.Attribute): | ||
| # Map method calls - need double allocation for ptr handling | ||
| _allocate_for_map_method( | ||
| builder, var_name, rval, local_sym_tab, map_sym_tab, structs_sym_tab | ||
| builder, var_name, rval, local_sym_tab, compilation_context | ||
| ) | ||
|
|
||
| else: | ||
| logger.warning(f"Unsupported call function type for {var_name}") | ||
|
|
||
|
|
||
| def _allocate_for_map_method( | ||
| builder, var_name, rval, local_sym_tab, map_sym_tab, structs_sym_tab | ||
| builder, var_name, rval, local_sym_tab, compilation_context | ||
| ): | ||
| """Allocate memory for variable assigned from map method (double alloc).""" | ||
| map_sym_tab = compilation_context.map_sym_tab | ||
| structs_sym_tab = compilation_context.structs_sym_tab | ||
|
|
||
| map_name = rval.func.value.id | ||
| method_name = rval.func.attr | ||
|
|
@@ -299,6 +298,15 @@ def allocate_temp_pool(builder, max_temps, local_sym_tab): | |
| logger.debug(f"Allocated temp variable: {temp_name}") | ||
|
|
||
|
|
||
| def _get_alignment(tmp_type): | ||
| """Return alignment for a given type.""" | ||
| if isinstance(tmp_type, ir.PointerType): | ||
| return 8 | ||
| elif isinstance(tmp_type, ir.IntType): | ||
| return tmp_type.width // 8 | ||
| return 8 | ||
|
|
||
|
|
||
| def _allocate_for_name(builder, var_name, rval, local_sym_tab): | ||
| """Allocate memory for variable-to-variable assignment (b = a).""" | ||
| source_var = rval.id | ||
|
|
@@ -321,8 +329,22 @@ def _allocate_for_name(builder, var_name, rval, local_sym_tab): | |
| ) | ||
|
|
||
|
|
||
| def _allocate_for_attribute(builder, var_name, rval, local_sym_tab, structs_sym_tab): | ||
| def _allocate_with_type(builder, var_name, ir_type): | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, unnecessary |
||
| """Allocate memory for a variable with a specific type.""" | ||
| var = builder.alloca(ir_type, name=var_name) | ||
| if isinstance(ir_type, ir.IntType): | ||
| var.align = ir_type.width // 8 | ||
| elif isinstance(ir_type, ir.PointerType): | ||
| var.align = 8 | ||
| return var | ||
|
|
||
|
|
||
| def _allocate_for_attribute( | ||
| builder, var_name, rval, local_sym_tab, compilation_context | ||
| ): | ||
| """Allocate memory for struct field-to-variable assignment (a = dat.fld).""" | ||
| structs_sym_tab = compilation_context.structs_sym_tab | ||
|
|
||
| if not isinstance(rval.value, ast.Name): | ||
| logger.warning(f"Complex attribute access not supported for {var_name}") | ||
| return | ||
|
|
@@ -455,20 +477,3 @@ def _allocate_for_attribute(builder, var_name, rval, local_sym_tab, structs_sym_ | |
| logger.info( | ||
| f"Pre-allocated {var_name} from {struct_var}.{field_name} with type {alloc_type}" | ||
| ) | ||
|
|
||
|
|
||
| def _allocate_with_type(builder, var_name, ir_type): | ||
| """Allocate variable with appropriate alignment for type.""" | ||
| var = builder.alloca(ir_type, name=var_name) | ||
| var.align = _get_alignment(ir_type) | ||
| return var | ||
|
|
||
|
|
||
| def _get_alignment(ir_type): | ||
| """Get appropriate alignment for IR type.""" | ||
| if isinstance(ir_type, ir.IntType): | ||
| return ir_type.width // 8 | ||
| elif isinstance(ir_type, ir.ArrayType) and isinstance(ir_type.element, ir.IntType): | ||
| return ir_type.element.width // 8 | ||
| else: | ||
| return 8 # Default: pointer size | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| from llvmlite import ir | ||
| import logging | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pythonbpf.structs.struct_type import StructType | ||
| from pythonbpf.maps.maps_utils import MapSymbol | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ScratchPoolManager: | ||
| """Manage the temporary helper variables in local_sym_tab""" | ||
|
|
||
| def __init__(self): | ||
| self._counters = {} | ||
|
|
||
| @property | ||
| def counter(self): | ||
| return sum(self._counters.values()) | ||
|
|
||
| def reset(self): | ||
| self._counters.clear() | ||
| logger.debug("Scratch pool counter reset to 0") | ||
|
|
||
| def _get_type_name(self, ir_type): | ||
| if isinstance(ir_type, ir.PointerType): | ||
| return "ptr" | ||
| elif isinstance(ir_type, ir.IntType): | ||
| return f"i{ir_type.width}" | ||
| elif isinstance(ir_type, ir.ArrayType): | ||
| return f"[{ir_type.count}x{self._get_type_name(ir_type.element)}]" | ||
| else: | ||
| return str(ir_type).replace(" ", "") | ||
|
|
||
| def get_next_temp(self, local_sym_tab, expected_type=None): | ||
| # Default to i64 if no expected type provided | ||
| type_name = self._get_type_name(expected_type) if expected_type else "i64" | ||
| if type_name not in self._counters: | ||
| self._counters[type_name] = 0 | ||
|
|
||
| counter = self._counters[type_name] | ||
| temp_name = f"__helper_temp_{type_name}_{counter}" | ||
| self._counters[type_name] += 1 | ||
|
|
||
| if temp_name not in local_sym_tab: | ||
| raise ValueError( | ||
| f"Scratch pool exhausted or inadequate: {temp_name}. " | ||
| f"Type: {type_name} Counter: {counter}" | ||
| ) | ||
|
|
||
| logger.debug(f"Using {temp_name} for type {type_name}") | ||
| return local_sym_tab[temp_name].var, temp_name | ||
|
|
||
|
|
||
| class CompilationContext: | ||
| """ | ||
| Holds the state for a single compilation run. | ||
| This replaces global mutable state modules. | ||
| """ | ||
|
|
||
| def __init__(self, module: ir.Module): | ||
| self.module = module | ||
|
|
||
| # Symbol tables | ||
| self.global_sym_tab: list[ir.GlobalVariable] = [] | ||
| self.structs_sym_tab: dict[str, "StructType"] = {} | ||
| self.map_sym_tab: dict[str, "MapSymbol"] = {} | ||
|
|
||
| # Helper management | ||
| self.scratch_pool = ScratchPoolManager() | ||
|
|
||
| # Vmlinux handling (optional, specialized) | ||
| self.vmlinux_handler = None # Can be VmlinuxHandler instance | ||
|
|
||
| # Current function context (optional, if needed globally during function processing) | ||
| self.current_func = None | ||
|
|
||
| def reset(self): | ||
| """Reset state between functions if necessary, though new context per compile is preferred.""" | ||
| self.scratch_pool.reset() | ||
| self.current_func = None |
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.
unnecessary to change this