Skip to content

Conversation

avik-pal
Copy link
Collaborator

@avik-pal avik-pal commented Oct 5, 2025

No description provided.

auto step = info.getConstantStep().value();

// currently only removes remainder of induction variable
whileBody.walk([&](stablehlo::RemOp remOp) -> WalkResult {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead could we just look at all users of the induction var (that likely will be faster than a walk of the body)

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EnzymeJAX Benchmarks

Benchmark suite Current: e358839 Previous: 54edf38 Ratio
scatter_sum / JaX / cpu / Primal 0.000004446296999958577 s 0.000004485886999464128 s 0.99
scatter_sum / JaXPipe / cpu / Primal 0.000004340780000347877 s 0.000004439690998697188 s 0.98
scatter_sum / JaX / tpu / Primal 0.0001532494204999 s 0.0001577955517001 s 0.97
scatter_sum / JaXPipe / tpu / Primal 0.0001514654375001 s 0.0001410659988992 s 1.07

This comment was automatically generated by workflow using github-action-benchmark.

}

// Initialize bounds map with induction variable bounds
DenseMap<Value, Bounds> boundsMap;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min/max only applies if step >= 0, if negative this needs to be changed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the induction var is initialized as

    APInt minBound(bitWidth, std::min(start, limit), true);
    APInt maxBound(bitWidth, std::max(start, limit), true);
    boundsMap[inductionVar] = Bounds(minBound, maxBound);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, is min/max inclusive or exclusive then. If step > 0, range is [start, limit). If negative it's (limit, start]

Copy link
Collaborator Author

@avik-pal avik-pal Oct 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inclusive in the subsequent usage, needs to be fixed here

return false;
};

bool rewriteCompareOp(PatternRewriter &rewriter,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems somewhat (but not wholly) similar to:

bool valueCmp(Cmp cmp, Value bval, ValueOrInt val) {

@avik-pal avik-pal changed the title feat: remove remainder in whileop feat: propagate bounds in whileop Oct 5, 2025
DenseMap<Value, Bounds> boundsMap;
if (step > 0) {
APInt minBound(bitWidth, start, true);
APInt maxBound(bitWidth, limit - 1, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't necessarily correct, since start == limit is a valid case [where the loop body isn't executed]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though I suppose if the body is never executed, what is done in the after region doesn't matter anyways

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a check for loop count >= 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing where since if step > limit - start, loop count is still zero [and step only checked for sign]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit above

    if (!info.isValid() || !info.isConstant() ||
        info.getConstantNumIters() <= 0)
      return failure();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok

@avik-pal avik-pal merged commit 2064d83 into main Oct 6, 2025
21 of 23 checks passed
@avik-pal avik-pal deleted the ap/while_noop branch October 6, 2025 00:58
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