Index: mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp =================================================================== --- mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp +++ mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp @@ -91,13 +91,13 @@ static void replaceSCFOutputValue(ScfOp scfOp, OpTy newOp, SPIRVTypeConverter &typeConverter, ConversionPatternRewriter &rewriter, - ScfToSPIRVContextImpl *scfToSPIRVContext) { + ScfToSPIRVContextImpl *scfToSPIRVContext, + ArrayRef returnTypes) { Location loc = scfOp.getLoc(); auto &allocas = scfToSPIRVContext->outputVars[newOp]; SmallVector resultValue; - for (Value result : scfOp.results()) { - auto convertedType = typeConverter.convertType(result.getType()); + for (Type convertedType : returnTypes) { auto pointerType = spirv::PointerType::get(convertedType, spirv::StorageClass::Function); rewriter.setInsertionPoint(newOp); @@ -185,8 +185,12 @@ loc, newIndVar.getType(), newIndVar, forOperands.step()); rewriter.create(loc, header, updatedIndVar); + // Infer the return types from the init operands. + SmallVector initTypes; + for (auto arg : forOperands.initArgs()) + initTypes.push_back(arg.getType()); replaceSCFOutputValue(forOp, loopOp, typeConverter, rewriter, - scfToSPIRVContext); + scfToSPIRVContext, initTypes); return success(); } @@ -238,8 +242,13 @@ thenBlock, ArrayRef(), elseBlock, ArrayRef()); + SmallVector returnTypes; + for (auto result : ifOp.results()) { + auto convertedType = typeConverter.convertType(result.getType()); + returnTypes.push_back(convertedType); + } replaceSCFOutputValue(ifOp, selectionOp, typeConverter, rewriter, - scfToSPIRVContext); + scfToSPIRVContext, returnTypes); return success(); }