From adf32560a07bac502af5b228215815fd7ebe7d6c Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Wed, 22 Oct 2025 03:45:54 +0530 Subject: [PATCH] bpf passthrough gen in codegen Signed-off-by: varun-r-mallya --- pythonbpf/codegen.py | 19 +++++++++++++++++++ pythonbpf/vmlinux_parser/import_detector.py | 19 ------------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/pythonbpf/codegen.py b/pythonbpf/codegen.py index e97b194..c3dd45f 100644 --- a/pythonbpf/codegen.py +++ b/pythonbpf/codegen.py @@ -36,6 +36,23 @@ def finalize_module(original_str): replacement = r'\1 "btf_ama"' return re.sub(pattern, replacement, original_str) +def bpf_passthrough_gen(module): + i32_ty = ir.IntType(32) + ptr_ty = ir.PointerType(ir.IntType(8)) + fnty = ir.FunctionType(ptr_ty, [i32_ty, ptr_ty]) + + # Declare the intrinsic + passthrough = ir.Function(module, fnty, "llvm.bpf.passthrough.p0.p0") + + # Set function attributes + # TODO: the ones commented are supposed to be there but cannot be added due to llvmlite limitations at the moment + # passthrough.attributes.add("nofree") + # passthrough.attributes.add("nosync") + passthrough.attributes.add("nounwind") + # passthrough.attributes.add("memory(none)") + + return passthrough + def find_bpf_chunks(tree): """Find all functions decorated with @bpf in the AST.""" @@ -57,6 +74,8 @@ def processor(source_code, filename, module): for func_node in bpf_chunks: logger.info(f"Found BPF function/struct: {func_node.name}") + bpf_passthrough_gen(module) + vmlinux_symtab = vmlinux_proc(tree, module) if vmlinux_symtab: handler = VmlinuxHandler.initialize(vmlinux_symtab) diff --git a/pythonbpf/vmlinux_parser/import_detector.py b/pythonbpf/vmlinux_parser/import_detector.py index 7cfbeff..549e5f2 100644 --- a/pythonbpf/vmlinux_parser/import_detector.py +++ b/pythonbpf/vmlinux_parser/import_detector.py @@ -77,28 +77,9 @@ def detect_import_statement(tree: ast.AST) -> list[tuple[str, ast.ImportFrom]]: return vmlinux_imports -def bpf_passthrough_gen(module): - i32_ty = ir.IntType(32) - ptr_ty = ir.PointerType(ir.IntType(8)) - fnty = ir.FunctionType(ptr_ty, [i32_ty, ptr_ty]) - - # Declare the intrinsic - passthrough = ir.Function(module, fnty, "llvm.bpf.passthrough.p0.p0") - - # Set function attributes - # TODO: the ones commented are supposed to be there but cannot be added due to llvmlite limitations at the moment - # passthrough.attributes.add("nofree") - # passthrough.attributes.add("nosync") - passthrough.attributes.add("nounwind") - # passthrough.attributes.add("memory(none)") - - return passthrough - - def vmlinux_proc(tree: ast.AST, module): import_statements = detect_import_statement(tree) - bpf_passthrough_gen(module) # initialise dependency handler handler = DependencyHandler() # initialise assignment dictionary of name to type