Skip to content

[mlir][tosa] Check for isolated regions in tosa.while_loop #144865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lhutton1
Copy link
Contributor

Similarly to tosa.cond_if, this patch checks that the cond/body regions of tosa.while_loop are isolated from above. This is required since the specification requires all values used in the cond/body regions are explicitly declared within the regions.

Note: this change is dependent on #143772

lhutton1 added 2 commits June 11, 2025 19:23
This commit fixes a check in the validation pass which intended
to validate whether a `tosa.cond_if` operation was conformant to
the specification. The specification requires all values used in
the then/else regions are explicitly declared within the regions.
This change checks that these regions are 'isolated from above',
to ensure this requirement is true.

Change-Id: I1b6eac1ed571e6b1eda4a58f0677c80e22977e58
Similarly to `tosa.cond_if`, this patch checks that the cond/body
regions of `tosa.while_loop` are isolated from above. This is required
since the specification requires all values used in the cond/body
regions are explicitly declared within the regions.

Change-Id: Ia7396b9811db54805ec33befd24ab97d1b605905
Copy link
Contributor

@FranklandJack FranklandJack left a comment

Choose a reason for hiding this comment

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

LGTM % a few pedantic comments :D

bool isElseGraphNullaryRegion = isNullaryRegion(elseGraph);
bool isInputListEmpty = ifOp.getInputList().size() == 0;
bool checkErrorIfWhileLoop(Operation *op) {
auto whileOp = dyn_cast<tosa::WhileOp>(op);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be const?


if ((isInputListEmpty != isThenGraphNullaryRegion) ||
(isInputListEmpty != isElseGraphNullaryRegion)) {
Region &condGraph = whileOp.getCondGraph();
Copy link
Contributor

Choose a reason for hiding this comment

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

const?

(isInputListEmpty != isElseGraphNullaryRegion)) {
Region &condGraph = whileOp.getCondGraph();
Region &bodyGraph = whileOp.getBodyGraph();
bool isCondGraphIsolatedRegion = isRegionIsolatedFromAbove(condGraph);
Copy link
Contributor

Choose a reason for hiding this comment

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

const?

if ((isInputListEmpty != isThenGraphNullaryRegion) ||
(isInputListEmpty != isElseGraphNullaryRegion)) {
Region &condGraph = whileOp.getCondGraph();
Region &bodyGraph = whileOp.getBodyGraph();
Copy link
Contributor

Choose a reason for hiding this comment

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

const?

Region &condGraph = whileOp.getCondGraph();
Region &bodyGraph = whileOp.getBodyGraph();
bool isCondGraphIsolatedRegion = isRegionIsolatedFromAbove(condGraph);
bool isBodyGraphIsolatedRegion = isRegionIsolatedFromAbove(bodyGraph);
Copy link
Contributor

Choose a reason for hiding this comment

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

const?

op->emitOpError()
<< "the current simplified form is not strictly conformant to the "
"spec, please use the generic format\n";
<< "is not conformant to the TOSA specification. It requires the "
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly to the if case, we could provide more information here if we were to split the checks into two. Only a week preference for this though.

%0 = "tosa.const"() {values = dense<0> : tensor<i32>} : () -> tensor<i32>
// expected-error@+1 {{'tosa.while_loop' op is not conformant to the TOSA specification. It requires the cond/body regions are isolated from above.}}
%1 = "tosa.while_loop"(%0) ({
^bb0(%arg3: tensor<i32>):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the blocks have the same ID bb0?

func.func @test_while_loop_isolated_from_above(%arg0: tensor<f32>, %arg1: tensor<i32>) {
%0 = "tosa.const"() {values = dense<0> : tensor<i32>} : () -> tensor<i32>
%1:3 = "tosa.while_loop"(%0, %arg0, %arg1) ({
^bb0(%arg3: tensor<i32>, %arg4: tensor<f32>, %arg5: tensor<i32>):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the blocks have the same ID bb0?


// -----

// COM: Check isolated while_loops are valid
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to use the COM directive here if there are no strings in the comment that could be interpreted as a FileCheck directive?

%3 = "tosa.logical_not"(%2) : (tensor<i1>) -> tensor<i1>
tosa.yield %3 : tensor<i1>
}, {
^bb0(%arg3: tensor<i32>):
Copy link
Contributor

Choose a reason for hiding this comment

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

Here it looks like the body graph is isolated from above, and it is only the condition graph that isn't. Should we add another test to check the error is triggered when the body graph isn't isolated from above?

isa<tosa::YieldOp>(op) || isa<tosa::VariableOp>(op))
return true;
return false;
static bool isOpIsolatedFromAbove(Operation *op, Region &region) {
Copy link
Contributor

@udaya-ranga udaya-ranga Jul 16, 2025

Choose a reason for hiding this comment

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

Will there just be 1 copy after merging this and the other review for cond.if or is this patch built on top of the other so that it appears here as well ? I guess the latter ?

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.

3 participants