Skip to content

[mlir][SPIRV] IfOpConversion: Compute result types earlier #134380

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

Merged
merged 1 commit into from
Apr 4, 2025

Conversation

matthias-springer
Copy link
Member

Compute the result types and bail out before modifying any IR. That is more efficient when type conversion failed, because no modifications must be rolled back.

Note: This is in preparation of the One-Shot Dialect Conversion refactoring.

@llvmbot
Copy link
Member

llvmbot commented Apr 4, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-spirv

Author: Matthias Springer (matthias-springer)

Changes

Compute the result types and bail out before modifying any IR. That is more efficient when type conversion failed, because no modifications must be rolled back.

Note: This is in preparation of the One-Shot Dialect Conversion refactoring.


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

1 Files Affected:

  • (modified) mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp (+12-10)
diff --git a/mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp b/mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp
index 31d8cd2206148..baac1b374b126 100644
--- a/mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp
+++ b/mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp
@@ -225,6 +225,18 @@ struct IfOpConversion : SCFToSPIRVPattern<scf::IfOp> {
     // subsequently converges.
     auto loc = ifOp.getLoc();
 
+    // Compute return types.
+    SmallVector<Type, 8> returnTypes;
+    for (auto result : ifOp.getResults()) {
+      auto convertedType = typeConverter.convertType(result.getType());
+      if (!convertedType)
+        return rewriter.notifyMatchFailure(
+            loc,
+            llvm::formatv("failed to convert type '{0}'", result.getType()));
+
+      returnTypes.push_back(convertedType);
+    }
+
     // Create `spirv.selection` operation, selection header block and merge
     // block.
     auto selectionOp =
@@ -261,16 +273,6 @@ struct IfOpConversion : SCFToSPIRVPattern<scf::IfOp> {
                                                 thenBlock, ArrayRef<Value>(),
                                                 elseBlock, ArrayRef<Value>());
 
-    SmallVector<Type, 8> returnTypes;
-    for (auto result : ifOp.getResults()) {
-      auto convertedType = typeConverter.convertType(result.getType());
-      if (!convertedType)
-        return rewriter.notifyMatchFailure(
-            loc,
-            llvm::formatv("failed to convert type '{0}'", result.getType()));
-
-      returnTypes.push_back(convertedType);
-    }
     replaceSCFOutputValue(ifOp, selectionOp, rewriter, scfToSPIRVContext,
                           returnTypes);
     return success();

@matthias-springer matthias-springer merged commit 5acab1b into main Apr 4, 2025
14 checks passed
@matthias-springer matthias-springer deleted the users/matthias-springer/spriv-return-types branch April 4, 2025 16:37
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.

3 participants