Skip to content

[Bug]: Decompiler.generic_jump_if incorrect end_index selection causes TypeError with torch.compile bytecode #90

@cennn

Description

@cennn

Your current environment

Collecting environment information...
PyTorch version: 2.9.0a0+145a3a7bda.nv25.10

Python version: 3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.8.0-60-generic-x86_64-with-glibc2.39

Versions of relevant libraries:
[pip3] depyf==0.20.0
[pip3] pytorch-triton==3.4.0+gitc817b9b6
[pip3] torch==2.9.0a0+145a3a7bda.nv25.10
[pip3] torch_tensorrt==2.9.0a0
[pip3] torchao==0.14.0+git
[pip3] torchprofile==0.0.4
[pip3] torchvision==0.24.0a0+094e7af5
[conda] Could not collect

🐛 Describe the bug

Reproduction Code

import torch
import depyf

@torch.compile
def minimal_repro(a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
    if b.sum() < 0:
        b = b * -1
    return a * b

# Trigger compilation and get the transformed bytecode
torch._dynamo.reset()
minimal_repro(torch.randn(3), torch.randn(3))
cache_entries = torch._dynamo.eval_frame._debug_get_cache_entry_list(minimal_repro)
transformed_code = cache_entries[0].code

# Attempt decompilation (will fail with TypeError)
try:
    decompiled = depyf.decompiler.Decompiler(transformed_code).decompile()
    print("Decompilation succeeded (unexpected):", decompiled)
except Exception as e:
    print(f"Decompilation failed with error:\n{type(e).__name__}: {e}")
    # Full traceback will show:
    # TypeError: sequence item 1: expected str instance, NoneType found

Pure Bytecode Reproduction

import types
import depyf
import dis

# Bytecode constants (Python 3.12)
RESUME = 151; LOAD_FAST = 124; LOAD_CONST = 100; PUSH_NULL = 2
POP_JUMP_IF_FALSE = 114; RETURN_VALUE = 83; BUILD_LIST = 103

def word(op, arg=0):
    return bytes([op, arg])

# Construct synthetic bytecode that triggers the bug
bc  = word(RESUME, 0)              # offset 0
bc += word(LOAD_FAST, 0)           # offset 2: push x (extra stack element)
bc += word(LOAD_FAST, 0)           # offset 4: push x (condition value)
bc += word(POP_JUMP_IF_FALSE, 3)   # offset 6: jump to offset 14 if False
bc += word(PUSH_NULL, 0)           # offset 8: push None (simulate NULL marker)
bc += word(LOAD_CONST, 1)          # offset 10: push 42
bc += word(RETURN_VALUE, 0)        # offset 12: return 42
bc += word(LOAD_FAST, 1)           # offset 14: push y (else-body start)
bc += word(BUILD_LIST, 2)          # offset 16: pop 2 items to build list
bc += word(RETURN_VALUE, 0)        # offset 18: return list

# Create CodeType object
synthetic_code = types.CodeType(
    2, 0, 0, 2, 10, 0, bc,
    (None, 42),                    # co_consts
    ('x', 'y'),                    # co_names
    ('x', 'y'),                    # co_varnames
    '<synthetic>', 'synthetic_bug', 'synthetic_bug',
    1, b'', b'', (), ()
)

# Decompile (fails with TypeError)
try:
    result = depyf.decompiler.Decompiler(synthetic_code).decompile()
    print(f"Success (unexpected): {result}")
except Exception as e:
    print(f"Failed as expected: {type(e).__name__}: {e}")
Failed as expected: DecompilationError: DecompilationError: Failed to decompile instruction Instruction(opcode=103, opname='BUILD_LIST', arg=2, argval=2, argrepr='', offset=16, starts_line=None, is_jump_target=False) in synthetic_bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions