Skip to content

[BUG] Augmented assignments double-evaluate RHS expression #1230

Closed

Description

@adenzler-nvidia

Bug Description

The codegen for augmented assignments (x += expr, x *= expr, etc.) evaluates the RHS expression twice when the target is a simple scalar variable.

In emit_AugAssign, the RHS is evaluated at line 3348 via rhs = adj.eval(node.value), but for simple ast.Name targets this result is unused — the code falls through to make_new_assign_statement() which rewrites the augmented assignment as x = x + expr via a new ast.BinOp node, re-evaluating node.value a second time.

Reproducer:

  @wp.func
  def side_effect_add(counter: wp.array(dtype=int), a: float, b: float) -> float:
      wp.atomic_add(counter, 0, 1)
      return a + b

  @wp.kernel
  def test_kernel(counter: wp.array(dtype=int), result: wp.array(dtype=float)):
      total = float(0.0)
      total += side_effect_add(counter, 1.0, 2.0)
      total += side_effect_add(counter, 3.0, 4.0)
      result[0] = total

  counter = wp.zeros(1, dtype=int, device="cuda:0")
  result = wp.zeros(1, dtype=float, device="cuda:0")
  wp.launch(test_kernel, dim=1, inputs=[counter, result], device="cuda:0")
  print(counter.numpy()[0])  # Expected: 2, Actual: 4

Impact:

  • Redundant loads and arithmetic in the generated CUDA/CPU code for any kernel using +=, *=, etc. on scalar variables
  • Approximately doubles the instruction count for the RHS of each affected augmented assignment
  • Particularly noticeable in tight loops where augmented assignments accumulate values (e.g., reductions, dot products)

System Information

No response

Activity

  1. added theissue type on Feb 13, 2026
  2. added this to the 1.12.0 milestone on Feb 13, 2026
  3. shi-eric commented on Feb 17, 2026

    @shi-eric
    Contributor

    Added in 7af3419

    Filed #1233 for more issues we should eventually fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions