Skip to content
Merged
37 changes: 36 additions & 1 deletion mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,41 @@ struct UIToFPI1Pattern final : public OpConversionPattern<arith::UIToFPOp> {
}
};

//===----------------------------------------------------------------------===//
// IndexCastOp
//===----------------------------------------------------------------------===//

/// Converts arith.index_cast to spirv.Select if the type of source is i1 or
/// vector of i1.
struct IndexCastI1Pattern final : public OpConversionPattern<arith::IndexCastOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(arith::IndexCastOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Type srcType = adaptor.getOperands().front().getType();
if (!srcType.isInteger(1))
return failure();

Type dstType = getTypeConverter()->convertType(op.getType());
if (!dstType)
return getTypeConversionFailure(rewriter, op);
// if (!dstType.isIndex()) {
// llvm::errs() << "why doesnt this work?\n";
// return failure();
// }

auto *converter = this->template getTypeConverter<SPIRVTypeConverter>();
Location loc = op.getLoc();
Type spirvI32T = converter->getIndexType();
Value zero = spirv::ConstantOp::getZero(spirvI32T, loc, rewriter);
Value one = spirv::ConstantOp::getOne(spirvI32T, loc, rewriter);
auto newOp = rewriter.replaceOpWithNewOp<spirv::SelectOp>(
op, dstType, adaptor.getOperands().front(), one, zero);
return success();
}
};

//===----------------------------------------------------------------------===//
// ExtSIOp
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -1328,7 +1363,7 @@ void mlir::arith::populateArithToSPIRVPatterns(
TypeCastingOpPattern<arith::SIToFPOp, spirv::ConvertSToFOp>,
TypeCastingOpPattern<arith::FPToUIOp, spirv::ConvertFToUOp>,
TypeCastingOpPattern<arith::FPToSIOp, spirv::ConvertFToSOp>,
TypeCastingOpPattern<arith::IndexCastOp, spirv::SConvertOp>,
TypeCastingOpPattern<arith::IndexCastOp, spirv::SConvertOp>, IndexCastI1Pattern,
TypeCastingOpPattern<arith::IndexCastUIOp, spirv::UConvertOp>,
TypeCastingOpPattern<arith::BitcastOp, spirv::BitcastOp>,
CmpIOpBooleanPattern, CmpIOpPattern,
Expand Down
7 changes: 7 additions & 0 deletions mlir/test/Conversion/ArithToSPIRV/arith-to-spirv.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,13 @@ func.func @index_castui4(%arg0: index) {
return
}

// CHECK-LABEL: index_casti1_1
func.func @index_casti1_1(%arg0 : i1) -> index {
// CHECK: spirv.Select %{{.+}}, %{{.+}}, %{{.+}} : i1, i32
%0 = arith.index_cast %arg0 : i1 to index
return %0 : index
}

// CHECK-LABEL: @bit_cast
func.func @bit_cast(%arg0: vector<2xf32>, %arg1: i64) {
// CHECK: spirv.Bitcast %{{.+}} : vector<2xf32> to vector<2xi32>
Expand Down