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

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

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

LogicalResult
matchAndRewrite(arith::IndexCastOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
if (!isBoolScalarOrVector(adaptor.getIn().getType()))
return failure();

Type dstType = getTypeConverter()->convertType(op.getType());
if (!dstType)
return getTypeConversionFailure(rewriter, op);

Location loc = op.getLoc();
Value zero = spirv::ConstantOp::getZero(dstType, loc, rewriter);
Value one = spirv::ConstantOp::getOne(dstType, loc, rewriter);
rewriter.replaceOpWithNewOp<spirv::SelectOp>(op, dstType, adaptor.getIn(),
one, zero);
return success();
}
};

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

// CHECK-LABEL: index_casti1index_1
func.func @index_casti1index_1(%arg0 : i1) {
// CHECK: %[[ZERO:.+]] = spirv.Constant 0 : i32
// CHECK: %[[ONE:.+]] = spirv.Constant 1 : i32
// CHECK: spirv.Select %{{.+}}, %[[ONE]], %[[ZERO]] : i1, i32
%0 = arith.index_cast %arg0 : i1 to index
return
}

// CHECK-LABEL: index_casti1index_2
func.func @index_casti1index_2(%arg0 : vector<3xi1>) {
// CHECK: %[[ZERO:.+]] = spirv.Constant dense<0> : vector<3xi32>
// CHECK: %[[ONE:.+]] = spirv.Constant dense<1> : vector<3xi32>
// CHECK: spirv.Select %{{.+}}, %[[ONE]], %[[ZERO]] : vector<3xi1>, vector<3xi32>
%0 = arith.index_cast %arg0 : vector<3xi1> to vector<3xindex>
return
}

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