Skip to content

[Simplifier] [Regression] Bounds checks for variable-extent loops not eliminated by simplifier #3770

@ajtulloch

Description

@ajtulloch

Reproduction script:

import tvm

n = tvm.var('n')
X = tvm.placeholder(shape=(n,), name="x")
W = tvm.placeholder(shape=(n + 1,), dtype="int32", name="w")

def f(i):
    start = W[i]
    extent = W[i+1] - W[i]
    rv = tvm.reduce_axis((0, extent))
    return tvm.sum(X[rv + start], axis=rv)
Y = tvm.compute(X.shape, f, name="y")
s = tvm.create_schedule([Y.op])
with tvm.build_config(add_lower_pass=[(2, lambda stmt: tvm.ir_pass.LoopPartition(stmt, False))]):
    print(tvm.lower(s, [X, W, Y], simple_mode=True))

At 60607ef, the output is:

produce y {
  for (i, 0, n) {
    y[i] = 0f
    for (rv, 0, (w[(i + 1)] - w[i])) {
      if ((rv < (w[(i + 1)] - w[i]))) {
        y[i] = (y[i] + x[(rv + w[i])])
      }
    }
  }
}

Where we have an unnecessary bounds check on the inner loop.

at 5217933, the output is:

produce y {
  for (i, 0, n) {
    y[i] = 0.000000f
    for (rv, 0, (w[(i + 1)] - w[i])) {
      y[i] = (y[i] + x[(w[i] + rv)])
    }
  }
}

That is, we avoid any bounds checks in the inner loop. The loop partition pass makes no difference here.

This seems to be a bug in the new simplifier infrastructure? cc @tqchen.

https://github.com/ajtulloch/tvm/tree/fix-cond-in-variable-extent-loo is a fix, but it's quite ugly and there should be a better fix in the simplifier.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions