NVIDIA / NVIDIA/cutlass

CuTeDSL: nested comprehension breaks staged control flow later in the outer comprehension (NameError on the generator variable)

Open Beginner friendly
#3,499 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

CuTe DSL
Dominant language
C++
Stars
10.5k
Forks
2.1k
Avg merge
3d 11h
Merged PRs (30d)
7

Description

Description

_visit_Comprehension in cutlass/base_dsl/ast_preprocessor.py (~lines 2424-2428) sets session_data.generator_targets for the comprehension being visited, then unconditionally resets it to [] afterwards:

self.session_data.generator_targets = targets
ele_visitor(node)
self.session_data.generator_targets = []

When the element expression contains a nested comprehension, the inner _visit_Comprehension overwrites generator_targets with the inner targets and resets it to empty on exit. The outer comprehension's remaining element processing then runs with no generator targets, so any later construct that synthesizes staged control flow (e.g. an IfExp) treats the outer generator variable as a free/global name and fails.

Minimal repro (verified against nvidia-cutlass-dsl==4.7.0; the control version without the inner comprehension compiles and runs):

import cutlass
import cutlass.cute as cute

@cute.jit
def f(flag: cutlass.Boolean) -> cutlass.Int32:
    vals = [(len([0 for _ in range(2)]), (y if flag else -y)) for y in range(4)]
    return cutlass.Int32(sum(v[0] * v[1] for v in vals))

print(f(cutlass.Boolean(True)))

Actual error:

Caused exception: name 'y' is not defined
suggestion: Variables used inside staged control flow (for/if/while) must be defined before the
control flow region. Give the variable an initial value before the loop or branch.

The suggestion is misleading: y is the comprehension variable, not a user mistake.

Suggested fix

Save the previous value of session_data.generator_targets before visiting and restore it after ele_visitor(node) returns (a try/finally, which also protects the session state on exception paths), instead of resetting to [].

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in cutlass/base_dsl/ast_preprocessor.py around _visit_Comprehension (approximately lines 2424-2428) and inspect how session_data.generator_targets is changed around ele_visitor(node). Run the minimal reproduction from the issue, then verify that nested comprehension processing restores the outer targets, including when element processing raises an exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
86/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.