LeiWang1999 opened a new issue, #19825:
URL: https://github.com/apache/tvm/issues/19825

   
   The rewrite added in apache/tvm#11287:
   
   ```cpp
   floormod(x * c1 + y, c2) -> floormod(x * floormod(c1, c2) + y, c2)
   ```
   
   is algebraically valid for one expression, but it can break `DetectIterMap` 
when
   the modulo expression is paired with a matching quotient expression from the
   same fused index.
   
   For example, this map is a straightforward split of one flat index:
   
   ```text
   flat = i * 192 + j
   lane = flat % 128
   reg  = flat // 128
   ```
   
   `DetectIterMap([lane, reg])` succeeds before the rewrite.  After simplifying
   only `lane` to `(i * 64 + j) % 128`, the tuple no longer exposes the same 
fused
   source expression, and iter-map detection fails.
   
   
   ## Minimal Reproducer
   
   ```python
   import tvm
   from tvm import tirx as tir
   from tvm.arith.iter_affine_map import IterMapLevel
   
   from tvm.ir import Range
   
   i = tir.Var("i", "int32")
   j = tir.Var("j", "int32")
   
   input_iters = {
       i: Range(0, 64),
       j: Range(0, 192),
   }
   
   flat = i * 192 + j
   lane = flat % 128
   reg = flat // 128
   
   analyzer = tvm.arith.Analyzer()
   simplified_lane = analyzer.simplify(lane)
   
   print("lane            =", lane)
   print("simplified_lane =", simplified_lane)
   
   raw = tvm.arith.detect_iter_map(
       [lane, reg],
       input_iters,
       predicate=True,
       check_level=IterMapLevel.Bijective,
   )
   print("raw errors      =", list(raw.errors))
   
   simplified = tvm.arith.detect_iter_map(
       [simplified_lane, reg],
       input_iters,
       predicate=True,
       check_level=IterMapLevel.Bijective,
   )
   print("simplified errors =", list(simplified.errors))
   ```
   
   
   
   ## Why This Matters
   
   This pattern appears in layout analysis for GPU code generation.  A layout 
often
   splits a flat logical index into:
   
   ```text
   thread = flat % num_threads
   index  = flat // num_threads
   ```
   
   The original pair:
   
   ```text
   [(i * 192 + j) % 128, (i * 192 + j) // 128]
   ```
   
   is recognized as a structured bijective map.  After local simplification:
   
   ```text
   [(i * 64 + j) % 128, (i * 192 + j) // 128]
   ```
   
   the two outputs no longer share the same visible fused source, so iter-map
   analysis rejects the map even though the mapping is still semantically valid.
   
   
   TileLang carries a local patch that restores the old conservative condition:
   
   ```diff
    TVM_TRY_REWRITE_IF(
        floormod(x * c1 + y, c2),
        floormod(x * floormod(c1, c2) + y, c2),
   -    c2.Eval()->value > 0);
   +    c2.Eval()->value > 0 && c1.Eval()->value % c2.Eval()->value == 0);
   ```
   
   If there is a better way to preserve this optimization while keeping 
iter-map detection robust, I would be happy to discuss it.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to