Skip to content

Conversation

lhutton1
Copy link
Contributor

Previoulsy the error_if check for controlflow operators would silently fail on valid controflow operators. This was due to incorrect return logic in the validation function. This commit fixes that logic.

@llvmbot
Copy link
Member

llvmbot commented Sep 19, 2025

@llvm/pr-subscribers-mlir

Author: Luke Hutton (lhutton1)

Changes

Previoulsy the error_if check for controlflow operators would silently fail on valid controflow operators. This was due to incorrect return logic in the validation function. This commit fixes that logic.


Full diff: https://github.com/llvm/llvm-project/pull/159754.diff

2 Files Affected:

  • (modified) mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp (+4-4)
  • (added) mlir/test/Dialect/Tosa/tosa-validation-valid-strict.mlir (+15)
diff --git a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
index 790bbf77877bc..e6091df367754 100644
--- a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
+++ b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
@@ -1257,8 +1257,8 @@ bool checkErrorIfCondIf(Operation *op) {
   //   tosa.yield %arg4
   // }
 
-  return failed(checkIsolatedRegion(op, ifOp.getThenGraph(), "then")) ||
-         failed(checkIsolatedRegion(op, ifOp.getElseGraph(), "else"));
+  return succeeded(checkIsolatedRegion(op, ifOp.getThenGraph(), "then")) &&
+         succeeded(checkIsolatedRegion(op, ifOp.getElseGraph(), "else"));
 }
 
 bool checkErrorIfWhileLoop(Operation *op) {
@@ -1266,8 +1266,8 @@ bool checkErrorIfWhileLoop(Operation *op) {
   if (!whileOp)
     return true;
 
-  return failed(checkIsolatedRegion(op, whileOp.getCondGraph(), "cond")) ||
-         failed(checkIsolatedRegion(op, whileOp.getBodyGraph(), "body"));
+  return succeeded(checkIsolatedRegion(op, whileOp.getCondGraph(), "cond")) &&
+         succeeded(checkIsolatedRegion(op, whileOp.getBodyGraph(), "body"));
 }
 
 bool checkErrorIfScatter(Operation *op) {
diff --git a/mlir/test/Dialect/Tosa/tosa-validation-valid-strict.mlir b/mlir/test/Dialect/Tosa/tosa-validation-valid-strict.mlir
new file mode 100644
index 0000000000000..fe423104359ab
--- /dev/null
+++ b/mlir/test/Dialect/Tosa/tosa-validation-valid-strict.mlir
@@ -0,0 +1,15 @@
+// RUN: mlir-opt %s -split-input-file -verify-diagnostics --tosa-validate="profile=pro_int,pro_fp extension=int16,int4,bf16,fp8e4m3,fp8e5m2,fft,variable,controlflow,doubleround,inexactround strict-op-spec-alignment" | FileCheck %s
+
+// -----
+
+// CHECK-LABEL: test_cond_if
+func.func @test_cond_if(%arg0: tensor<i8>, %arg1: tensor<i8>, %arg2: tensor<i1>) -> tensor<i8> {
+  %0 = tosa.cond_if %arg2 (%arg3 = %arg0, %arg4 = %arg1) : tensor<i1> (tensor<i8>, tensor<i8>) -> tensor<i8> {
+  ^bb0(%arg3: tensor<i8>, %arg4: tensor<i8>):
+    tosa.yield %arg3 : tensor<i8>
+  } else {
+  ^bb0(%arg3: tensor<i8>, %arg4: tensor<i8>):
+    tosa.yield %arg4 : tensor<i8>
+  }
+  return %0 : tensor<i8>
+}

@llvmbot
Copy link
Member

llvmbot commented Sep 19, 2025

@llvm/pr-subscribers-mlir-tosa

Author: Luke Hutton (lhutton1)

Changes

Previoulsy the error_if check for controlflow operators would silently fail on valid controflow operators. This was due to incorrect return logic in the validation function. This commit fixes that logic.


Full diff: https://github.com/llvm/llvm-project/pull/159754.diff

2 Files Affected:

  • (modified) mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp (+4-4)
  • (added) mlir/test/Dialect/Tosa/tosa-validation-valid-strict.mlir (+15)
diff --git a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
index 790bbf77877bc..e6091df367754 100644
--- a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
+++ b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
@@ -1257,8 +1257,8 @@ bool checkErrorIfCondIf(Operation *op) {
   //   tosa.yield %arg4
   // }
 
-  return failed(checkIsolatedRegion(op, ifOp.getThenGraph(), "then")) ||
-         failed(checkIsolatedRegion(op, ifOp.getElseGraph(), "else"));
+  return succeeded(checkIsolatedRegion(op, ifOp.getThenGraph(), "then")) &&
+         succeeded(checkIsolatedRegion(op, ifOp.getElseGraph(), "else"));
 }
 
 bool checkErrorIfWhileLoop(Operation *op) {
@@ -1266,8 +1266,8 @@ bool checkErrorIfWhileLoop(Operation *op) {
   if (!whileOp)
     return true;
 
-  return failed(checkIsolatedRegion(op, whileOp.getCondGraph(), "cond")) ||
-         failed(checkIsolatedRegion(op, whileOp.getBodyGraph(), "body"));
+  return succeeded(checkIsolatedRegion(op, whileOp.getCondGraph(), "cond")) &&
+         succeeded(checkIsolatedRegion(op, whileOp.getBodyGraph(), "body"));
 }
 
 bool checkErrorIfScatter(Operation *op) {
diff --git a/mlir/test/Dialect/Tosa/tosa-validation-valid-strict.mlir b/mlir/test/Dialect/Tosa/tosa-validation-valid-strict.mlir
new file mode 100644
index 0000000000000..fe423104359ab
--- /dev/null
+++ b/mlir/test/Dialect/Tosa/tosa-validation-valid-strict.mlir
@@ -0,0 +1,15 @@
+// RUN: mlir-opt %s -split-input-file -verify-diagnostics --tosa-validate="profile=pro_int,pro_fp extension=int16,int4,bf16,fp8e4m3,fp8e5m2,fft,variable,controlflow,doubleround,inexactround strict-op-spec-alignment" | FileCheck %s
+
+// -----
+
+// CHECK-LABEL: test_cond_if
+func.func @test_cond_if(%arg0: tensor<i8>, %arg1: tensor<i8>, %arg2: tensor<i1>) -> tensor<i8> {
+  %0 = tosa.cond_if %arg2 (%arg3 = %arg0, %arg4 = %arg1) : tensor<i1> (tensor<i8>, tensor<i8>) -> tensor<i8> {
+  ^bb0(%arg3: tensor<i8>, %arg4: tensor<i8>):
+    tosa.yield %arg3 : tensor<i8>
+  } else {
+  ^bb0(%arg3: tensor<i8>, %arg4: tensor<i8>):
+    tosa.yield %arg4 : tensor<i8>
+  }
+  return %0 : tensor<i8>
+}

Copy link
Contributor

@RoboTux RoboTux left a comment

Choose a reason for hiding this comment

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

Can we have some negative tests as well that show when the verification fail on invalid tosa.cond_if and tosa.while_loop. Also a positive test for tosa.while_loop please.

Previoulsy the error_if check for controlflow operators would
silently fail on valid controflow operators. This was due to
incorrect return logic in the validation function. This commit
fixes that logic.

Change-Id: I3c69c726028ff387202eef1d1f56b3c40ce80157
@lhutton1 lhutton1 force-pushed the fix-validation-controlflow branch from cd5c325 to 1ae1cf8 Compare September 19, 2025 11:14
@lhutton1
Copy link
Contributor Author

Thanks, negative tests already exist here: https://github.com/llvm/llvm-project/blob/main/mlir/test/Dialect/Tosa/error_if_check.mlir#L231. I actually realised some positive tests also existed here, but they weren't being checked correctly since FileCheck wasn't being run. I've moved them to the new test file I created.

@RoboTux
Copy link
Contributor

RoboTux commented Sep 19, 2025

Thanks, negative tests already exist here: https://github.com/llvm/llvm-project/blob/main/mlir/test/Dialect/Tosa/error_if_check.mlir#L231. I actually realised some positive tests also existed here, but they weren't being checked correctly since FileCheck wasn't being run. I've moved them to the new test file I created.

Gotcha, thanks.

@lhutton1
Copy link
Contributor Author

Will merge this based on approval in #160052, I'll then rebase #160052

@lhutton1 lhutton1 merged commit 14f5504 into llvm:main Sep 24, 2025
9 checks passed
Copy link
Contributor

@RoboTux RoboTux left a comment

Choose a reason for hiding this comment

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

Forgot to push these. Submitting them in case someone wants to create a follow-up patch but it's not the end of the world.


// -----

// CHECK-LABEL: test_cond_if_isolated_from_above
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure what is the value in those CHECK-LABEL since there are no CHECK between them. Since these tests try to check the absence of an error, the mlir-opt line should be enough and FileCheck is not needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Likewise in error_if_check.mlir by the way which doesn't even run FileCheck. Can you do a separate patch to remove the CHECK directives in that file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, you're more familiar with FileCheck/LIT so appreciate any suggestion. Without this fix, these tests still passed without FileCheck. I suspect it's because they were returning a silent failure? So I used FileCheck here to help make sure that something was returned from mlir-opt and that the test would fail as expected without the fix.

I think you're correct that I didn't need to use CHECK-LABEL, I was just following the pattern used by most tests under TOSA.

Copy link
Contributor

Choose a reason for hiding this comment

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

For checking the absence of an error you could just remove the --split-input-files and then mlir-opt should return an error if any of the test fail to compile. On the other hand having CHECK-LABEL means you'll get a more precise error of which test fail so maybe worth keeping. The CHECK-LABEL without a FileCheck invocation though is waiting for trouble because someone might add some tests thinking that FileCheck is being run and wrongly conclude that their test work.

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

Successfully merging this pull request may close these issues.

4 participants