Skip to content

Fix nested def in @triton.jit to raise intended UnsupportedLanguageConstruct#10004

Open
swjng wants to merge 3 commits intotriton-lang:mainfrom
swjng:fix/nested-def-error
Open

Fix nested def in @triton.jit to raise intended UnsupportedLanguageConstruct#10004
swjng wants to merge 3 commits intotriton-lang:mainfrom
swjng:fix/nested-def-error

Conversation

@swjng
Copy link
Copy Markdown

@swjng swjng commented Apr 11, 2026

New contributor declaration

  • I am not making a trivial change, such as fixing a typo in a comment.

  • I have written a PR description following these rules.

  • I have run pre-commit run --from-ref origin/main --to-ref HEAD.

  • Select one of the following.

    • I have added tests.
      • /python/test for end-to-end tests
  • Select one of the following.

    • I have not added any lit tests.

Problem

Defining a nested function inside a @triton.jit kernel surfaces an opaque error:

@triton.jit
def kernel(ptr, n, BLOCK: tl.constexpr):
    def combine(a, b):   # <-- nested def
        return a + b
    ...

Before this change:

triton.compiler.errors.CompilationError: at 2:16:
def kernel(ptr, n, BLOCK: tl.constexpr):
    def combine(a, b):
                ^
StopIteration()

The message StopIteration() gives the user no indication of what went wrong.

Root cause

visit_FunctionDef in code_generator.py called self.visit(node.args) before checking if self.fn:. For a nested def, visit_arg calls

next(p for p in self.jit_fn.params if p.name == node.arg)

which raises StopIteration because the nested function's parameters are not in the outer JIT function's param list. The visitor's generic exception handler then wraps StopIteration as a CompilationError with the bare repr as its message.

Fix

Move the if self.fn: guard before self.visit(node.args) so that UnsupportedLanguageConstruct is raised immediately with a message directing the user to move helper functions to module level.

Nested def was already an unsupported construct (the guard existed, just in the wrong order). This change only improves the error message.

Test

test_err_nested_function_def in python/test/unit/language/test_compile_errors.py:

  • asserts StopIteration does not appear in the error message
  • asserts the error message mentions nested function

@swjng swjng requested a review from ptillet as a code owner April 11, 2026 09:59
@swjng swjng changed the title Raise UnsupportedLanguageConstruct for nested def inside @triton.jit Raise UnsupportedLanguageConstruct for nested def inside @triton.jit Apr 11, 2026
@swjng swjng force-pushed the fix/nested-def-error branch 2 times, most recently from b0f6a01 to 6cef83e Compare April 13, 2026 04:21
@swjng swjng changed the title Raise UnsupportedLanguageConstruct for nested def inside @triton.jit Replace bare StopIteration with UnsupportedLanguageConstruct for nested def in @triton.jit Apr 13, 2026
@swjng swjng changed the title Replace bare StopIteration with UnsupportedLanguageConstruct for nested def in @triton.jit Fix nested def in @triton.jit to raise intended UnsupportedLanguageConstruct Apr 13, 2026
Defining a nested function inside a @triton.jit kernel previously
produced an opaque error message:

    CompilationError: StopIteration()

Root cause: visit_FunctionDef called visit(node.args) before checking
`if self.fn:`. For a nested def, visit_arg calls

    next(p for p in self.jit_fn.params if p.name == node.arg)

which raises StopIteration because the nested function's parameters
are not in the outer JIT function's param list. The visitor's generic
exception handler then wrapped StopIteration as a CompilationError
with an uninformative message.

Fix: move the `if self.fn:` guard before visit(node.args) so that
UnsupportedLanguageConstruct is raised immediately with a message
that tells the user to move helper functions to module level.

Nested def was already an unsupported construct; this change only
improves the error message surfaced to users.
@swjng swjng force-pushed the fix/nested-def-error branch from bcb512e to 832544e Compare April 14, 2026 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants