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

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

/// Converts arith.index_cast to spirv.Select if the type of source is index.
struct IndexCastIndexI1Pattern 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();
// Indexes have already been converted to its respective spirv type:
Type indexType = getTypeConverter<SPIRVTypeConverter>()->getIndexType();
if (srcType != indexType || !op.getType().isInteger(1))
return failure();

Type dstType = rewriter.getI1Type();
Location loc = op.getLoc();
Value zero = spirv::ConstantOp::getZero(dstType, loc, rewriter);
Value one = spirv::ConstantOp::getOne(dstType, loc, rewriter);
Value zeroIdx = spirv::ConstantOp::getZero(srcType, loc, rewriter);
auto isZero = spirv::IEqualOp::create(rewriter, loc, dstType, zeroIdx,
adaptor.getOperands().front());
// spriv.IEqual outputs i32, spirv.Select is used to truncate to i1:
rewriter.replaceOpWithNewOp<spirv::SelectOp>(op, dstType, isZero, zero,
one);
return success();
}
};

//===----------------------------------------------------------------------===//
// ExtSIOp
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -1328,7 +1360,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>, IndexCastIndexI1Pattern,
TypeCastingOpPattern<arith::IndexCastUIOp, spirv::UConvertOp>,
TypeCastingOpPattern<arith::BitcastOp, spirv::BitcastOp>,
CmpIOpBooleanPattern, CmpIOpPattern,
Expand Down
11 changes: 11 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,17 @@ func.func @index_castui4(%arg0: index) {
return
}

// CHECK-LABEL: index_castindexi1
func.func @index_castindexi1(%arg0 : index) {
// CHECK: %[[FALSE:.+]] = spirv.Constant false
// CHECK: %[[TRUE:.+]] = spirv.Constant true
// CHECK: %[[ZERO:.+]] = spirv.Constant 0 : i32
// CHECK: %[[IS_ZERO:.+]] = spirv.IEqual %[[ZERO]], %{{.+}} : i32
// CHECK: spirv.Select %[[IS_ZERO]], %[[FALSE]], %[[TRUE]] : i1, i1
%0 = arith.index_cast %arg0 : index to i1
return
}

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