[Mlir-commits] [mlir] 5acab1b - [mlir][SPIRV] `IfOpConversion`: Compute result types earlier (#134380)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Apr 4 09:37:18 PDT 2025


Author: Matthias Springer
Date: 2025-04-04T09:37:15-07:00
New Revision: 5acab1bd15004e0ab7af60d2c4919c189bd38520

URL: https://github.com/llvm/llvm-project/commit/5acab1bd15004e0ab7af60d2c4919c189bd38520
DIFF: https://github.com/llvm/llvm-project/commit/5acab1bd15004e0ab7af60d2c4919c189bd38520.diff

LOG: [mlir][SPIRV] `IfOpConversion`: Compute result types earlier (#134380)

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.

Added: 
    

Modified: 
    mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp

Removed: 
    


################################################################################
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();


        


More information about the Mlir-commits mailing list