kernelfoundry.algorithm.utils.code_editing

Classes

CodeEditCommand(start, end, replacement)

Object describing a code edit operation.

CodeEditing(source_code[, parse_header_files])

class kernelfoundry.algorithm.utils.code_editing.CodeEditCommand(start: int, end: int, replacement: str)[source]

Object describing a code edit operation.

start: int

Alias for field number 0

end: int

Alias for field number 1

replacement: str

Alias for field number 2

static apply_all(commands: list[CodeEditCommand], source_code: str) str[source]
class kernelfoundry.algorithm.utils.code_editing.CodeEditing(source_code: str, parse_header_files: bool = True)[source]
__init__(source_code: str, parse_header_files: bool = True)[source]

CodeEditing class for manipulating C++ source code using libclang :param source_code: The C++ source code to manipulate. :type source_code: str :param parse_header_files: If True, header files will be parsed. :type parse_header_files: bool

property translation_unit: TranslationUnit
property source_code: str
print_nodes(nodes: list[Cursor])[source]
print_ast()[source]
get_node_str(node: Cursor) str[source]
find_nodes_by_kind(parent: Cursor, kind: CursorKind, recursive: bool = True) list[Cursor][source]
find_enclosing_node(node: Cursor, kind: CursorKind | None) Cursor | None[source]
find_sycl_queue() list[Cursor][source]

Find SYCL queue variable declaration nodes in the AST. :returns: A list of Cursor nodes representing SYCL queue variable declarations. :rtype: list[Cursor]

find_sycl_wait(queue_node: Cursor) list[Cursor][source]

Find SYCL queue wait call expressions in the AST for a given queue variable. :param queue_node: The Cursor node representing the SYCL queue variable. :type queue_node: Cursor

Returns:

A list of Cursor nodes representing SYCL queue wait call expressions.

Return type:

list[Cursor]

static replace_node_with_str(node: Cursor, replacement_str: str, source_code: str, remove_semicolon: bool = False) CodeEditCommand[source]

Helper function for manipulating the source code by replacing a node with a string. :param node: The node to replace. :type node: Cursor :param replacement_str: The string to replace the node with. :type replacement_str: str :param source_code: The source code to manipulate. :type source_code: str :param remove_semicolon: If True, removes a trailing semicolon after the node’s extent. :type remove_semicolon: bool

Returns:

The modified source code with the node replaced by the string.

Return type:

str

replace_queue_with_torch_queue_and_remove_wait()[source]

This method finds the SYCL queue declaration in the source code and replaces it with a declaration that uses the torch XPU stream queue. It removes the last queue.wait() call and adds the necessary include for c10/xpu/XPUStream.h.