[Lldb-commits] [lldb] [lldb][bytecode] Remove tracking of stack temps in compiler (PR #187401)

via lldb-commits lldb-commits at lists.llvm.org
Wed Mar 18 16:23:06 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Dave Lee (kastiglione)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/187401.diff


1 Files Affected:

- (modified) lldb/examples/python/formatter_bytecode.py (+5-30) 


``````````diff
diff --git a/lldb/examples/python/formatter_bytecode.py b/lldb/examples/python/formatter_bytecode.py
index da114a964f55c..c195c363d6e9d 100644
--- a/lldb/examples/python/formatter_bytecode.py
+++ b/lldb/examples/python/formatter_bytecode.py
@@ -844,10 +844,6 @@ class Compiler(ast.NodeVisitor):
     # methods are compiled.
     attrs: list[str]
 
-    # Temporaries currently on the stack above the locals/attrs frame.
-    # Always 0 at statement boundaries.
-    num_temps: int
-
     # Bytecode signature of the method being compiled, or None for top-level
     # functions.
     current_sig: Optional[str]
@@ -857,7 +853,6 @@ class Compiler(ast.NodeVisitor):
     def __init__(self) -> None:
         self.locals = []
         self.attrs = []
-        self.num_temps = 0
         self.current_sig = None
         self.buffer = io.StringIO()
 
@@ -891,7 +886,6 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None:
 
     def _compile_method(self, node: ast.FunctionDef) -> None:
         self.current_sig = _METHOD_SIGS[node.name]
-        self.num_temps = 0
 
         # Strip 'self' (and 'internal_dict' for __init__) from the arg list;
         # the remaining args become the initial locals.
@@ -930,18 +924,13 @@ def visit_Compare(self, node: ast.Compare) -> None:
         # XXX: Does not handle multiple comparisons, ex: `0 < x < 10`
         self.visit(node.comparators[0])
         self._output(_COMPS[type(node.ops[0])])
-        # The comparison consumes two values and produces one.
-        self.num_temps -= 1
 
     def visit_If(self, node: ast.If) -> None:
         self.visit(node.test)
-        # `if`/`ifelse` consumes the condition.
-        self.num_temps = 0
 
         self._output("{")
         self._visit_each(node.body)
         if node.orelse:
-            self.num_temps = 0
             self._output("} {")
             self._visit_each(node.orelse)
             self._output("} ifelse")
@@ -949,7 +938,6 @@ def visit_If(self, node: ast.If) -> None:
             self._output("} if")
 
     def visit_Return(self, node: ast.Return) -> None:
-        self.num_temps = 0
         if node.value:
             self.visit(node.value)
         self._output("return")
@@ -961,7 +949,6 @@ def visit_Constant(self, node: ast.Constant) -> None:
             self._output(int(node.value))
         else:
             self._output(node.value)
-        self.num_temps += 1
 
     def visit_Call(self, node: ast.Call) -> None:
         func = node.func
@@ -978,8 +965,6 @@ def visit_Call(self, node: ast.Call) -> None:
                 self.visit(receiver)
                 self._visit_each(node.args)
                 self._output(f"{selector} call")
-                # `call` pops the receiver and all args, and pushes one result.
-                self.num_temps -= len(node.args)
                 return
             raise CompilerError(f"unsupported method: {method}", node)
 
@@ -989,8 +974,6 @@ def visit_Call(self, node: ast.Call) -> None:
         raise CompilerError("unsupported function call expression", node)
 
     def visit_Assign(self, node: ast.Assign) -> None:
-        self.num_temps = 0
-
         target = node.targets[0]
 
         # Handle self.attr = expr (attribute assignment).
@@ -1057,17 +1040,14 @@ def visit_Attribute(self, node: ast.Attribute) -> None:
             raise CompilerError(
                 "unsupported attribute access (only self.attr is supported)", node
             )
-        attr_idx = self._attr_index(node.attr, node)
-        pick_idx = self.num_temps + attr_idx
+        pick_idx = self._attr_index(node.attr, node)
         self._output(f"{pick_idx}u pick")  # "# self.{node.attr}"
-        self.num_temps += 1
 
     def visit_Name(self, node: ast.Name) -> None:
-        idx = self._stack_index(node)
+        idx = self._local_index(node)
         if idx is None:
             raise CompilerError(f"unknown local variable: {node.id}", node)
         self._output(f"{idx}u pick")  # "# {node.id}"
-        self.num_temps += 1
 
     def _visit_each(self, nodes: Sequence[ast.AST]) -> None:
         for child in nodes:
@@ -1081,16 +1061,11 @@ def _attr_index(self, name: str, node: ast.expr) -> int:
         except ValueError:
             raise CompilerError(f"unknown attribute: {name}", node)
 
-    def _stack_index(self, name: ast.Name) -> Optional[int]:
-        # Offset past all attrs and any in-flight temporaries.
-        idx = self._local_index(name)
-        if idx is None:
-            return None
-        return len(self.attrs) + idx + self.num_temps
-
     def _local_index(self, name: ast.Name) -> Optional[int]:
         try:
-            return self.locals.index(name.id)
+            idx = self.locals.index(name.id)
+            # Offset past all attrs.
+            return len(self.attrs) + idx
         except ValueError:
             return None
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/187401


More information about the lldb-commits mailing list