Skip to content

Commit 5acab1b

Browse files
[mlir][SPIRV] IfOpConversion: Compute result types earlier (llvm#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.
1 parent 3d24046 commit 5acab1b

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ struct IfOpConversion : SCFToSPIRVPattern<scf::IfOp> {
225225
// subsequently converges.
226226
auto loc = ifOp.getLoc();
227227

228+
// Compute return types.
229+
SmallVector<Type, 8> returnTypes;
230+
for (auto result : ifOp.getResults()) {
231+
auto convertedType = typeConverter.convertType(result.getType());
232+
if (!convertedType)
233+
return rewriter.notifyMatchFailure(
234+
loc,
235+
llvm::formatv("failed to convert type '{0}'", result.getType()));
236+
237+
returnTypes.push_back(convertedType);
238+
}
239+
228240
// Create `spirv.selection` operation, selection header block and merge
229241
// block.
230242
auto selectionOp =
@@ -261,16 +273,6 @@ struct IfOpConversion : SCFToSPIRVPattern<scf::IfOp> {
261273
thenBlock, ArrayRef<Value>(),
262274
elseBlock, ArrayRef<Value>());
263275

264-
SmallVector<Type, 8> returnTypes;
265-
for (auto result : ifOp.getResults()) {
266-
auto convertedType = typeConverter.convertType(result.getType());
267-
if (!convertedType)
268-
return rewriter.notifyMatchFailure(
269-
loc,
270-
llvm::formatv("failed to convert type '{0}'", result.getType()));
271-
272-
returnTypes.push_back(convertedType);
273-
}
274276
replaceSCFOutputValue(ifOp, selectionOp, rewriter, scfToSPIRVContext,
275277
returnTypes);
276278
return success();

0 commit comments

Comments
 (0)