Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VM] Add support for UI64 to F32 casts #19556

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,9 @@ struct UIToFPOpConversion : public OpConversionPattern<arith::UIToFPOp> {
}
if (srcType.isUnsignedInteger(64) || srcType.isSignlessInteger(64)) {
if (dstType.isF32()) {
return rewriter.notifyMatchFailure(srcOp, "unsupported type");
rewriter.replaceOpWithNewOp<IREE::VM::CastUI64F32Op>(srcOp, resultType,
input);
return success();
}

rewriter.replaceOpWithNewOp<IREE::VM::CastUI64F64Op>(srcOp, resultType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,18 @@ module @uitofp_i32_f32 {

// -----

// CHECK-LABEL: @uitofp_i64_f32
module @uitofp_i64_f32 {
// CHECK: vm.func private @fn(%[[ARG0:.+]]: i64)
func.func @fn(%arg0: i64) -> f32 {
// CHECK: vm.cast.ui64.f32 %[[ARG0]] : i64 -> f32
%0 = arith.uitofp %arg0 : i64 to f32
return %0 : f32
}
}

// -----

// CHECK-LABEL: @fptosi_fp32_i8
module @fptosi_fp32_i8 {
// CHECK: vm.func private @fn(%[[ARG0:.+]]: f32)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4522,6 +4522,7 @@ void populateVMToEmitCPatterns(ConversionTarget &conversionTarget,
ADD_GENERIC_PATTERN(IREE::VM::CastF32UI64Op, "vm_cast_f32ui64");
ADD_GENERIC_PATTERN(IREE::VM::CastSI32F32Op, "vm_cast_si32f32");
ADD_GENERIC_PATTERN(IREE::VM::CastSI64F32Op, "vm_cast_si64f32");
ADD_GENERIC_PATTERN(IREE::VM::CastUI64F32Op, "vm_cast_ui64f32");
ADD_GENERIC_PATTERN(IREE::VM::CastUI32F32Op, "vm_cast_ui32f32");
ADD_GENERIC_PATTERN(IREE::VM::CeilF32Op, "vm_ceil_f32");
ADD_GENERIC_PATTERN(IREE::VM::CmpEQF32OOp, "vm_cmp_eq_f32o");
Expand Down
10 changes: 10 additions & 0 deletions compiler/src/iree/compiler/Dialect/VM/IR/VMOpFolders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,16 @@ OpFoldResult CastSI64F32Op::fold(FoldAdaptor operands) {
});
}

OpFoldResult CastUI64F32Op::fold(FoldAdaptor operands) {
return constFoldCastOp<IntegerAttr, FloatAttr>(
Float32Type::get(getContext()), operands.getOperand(),
[&](const APInt &a) {
APFloat b = APFloat(0.0f);
b.convertFromAPInt(a, /*IsSigned=*/false, APFloat::rmNearestTiesToAway);
return b;
});
}

OpFoldResult CastUI32F32Op::fold(FoldAdaptor operands) {
return constFoldCastOp<IntegerAttr, FloatAttr>(
Float32Type::get(getContext()), operands.getOperand(),
Expand Down
1 change: 1 addition & 0 deletions compiler/src/iree/compiler/Dialect/VM/IR/VMOpcodesF32.td
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def VM_OPC_MaxF32 : VM_OPC<0x38, "MaxF32">;

def VM_OPC_CastSI32F32 : VM_OPC<0x14, "CastSI32F32">;
def VM_OPC_CastSI64F32 : VM_OPC<0x3C, "CastSI64F32">;
def VM_OPC_CastUI64F32 : VM_OPC<0x3D, "CastUI64F32">;
def VM_OPC_CastUI32F32 : VM_OPC<0x15, "CastUI32F32">;
def VM_OPC_CastF32SI32 : VM_OPC<0x16, "CastF32SI32">;
def VM_OPC_CastF32SI64 : VM_OPC<0x3A, "CastF32SI64">;
Expand Down
8 changes: 8 additions & 0 deletions compiler/src/iree/compiler/Dialect/VM/IR/VMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -3174,6 +3174,14 @@ def VM_CastSI64F32Op :
let hasFolder = 1;
}

def VM_CastUI64F32Op :
VM_ConversionOp<I64, F32, "cast.ui64.f32", VM_OPC_CastUI64F32,
[VM_ExtF32]> {
let summary = [{cast from an unsigned integer to a float-point value}];
let hasFolder = 1;
}


def VM_CastUI64F64Op :
VM_ConversionOp<I64, F64, "cast.ui64.f64", VM_OPC_CastUI64F64,
[VM_ExtF64]> {
Expand Down
5 changes: 5 additions & 0 deletions runtime/src/iree/vm/bytecode/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,11 @@ static iree_status_t iree_vm_bytecode_dispatch(
float* result = VM_DecResultRegF32("result");
*result = vm_cast_si64f32(operand);
});
DISPATCH_OP(EXT_F32, CastUI64F32, {
int64_t operand = (int64_t)VM_DecOperandRegI64("operand");
float* result = VM_DecResultRegF32("result");
*result = vm_cast_ui64f32(operand);
});
DISPATCH_OP(EXT_F32, CastUI32F32, {
int32_t operand = (int32_t)VM_DecOperandRegI32("operand");
float* result = VM_DecResultRegF32("result");
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/iree/vm/bytecode/utils/generated/op_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ typedef enum {
IREE_VM_OP_EXT_F32_CastF32SI64 = 0x3A,
IREE_VM_OP_EXT_F32_CastF32UI64 = 0x3B,
IREE_VM_OP_EXT_F32_CastSI64F32 = 0x3C,
IREE_VM_OP_EXT_F32_RSV_0x3D,
IREE_VM_OP_EXT_F32_CastUI64F32 = 0x3D,
IREE_VM_OP_EXT_F32_RSV_0x3E,
IREE_VM_OP_EXT_F32_RSV_0x3F,
IREE_VM_OP_EXT_F32_RSV_0x40,
Expand Down
4 changes: 4 additions & 0 deletions runtime/src/iree/vm/bytecode/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,10 @@ static iree_status_t iree_vm_bytecode_function_verify_bytecode_op(
VM_VerifyOperandRegI64(operand);
VM_VerifyResultRegF32(result);
});
VERIFY_OP(EXT_F32, CastUI64F32, {
VM_VerifyOperandRegI64(operand);
VM_VerifyResultRegF32(result);
});
VERIFY_OP(EXT_F32, CastUI32F32, {
VM_VerifyOperandRegI32(operand);
VM_VerifyResultRegF32(result);
Expand Down
5 changes: 2 additions & 3 deletions runtime/src/iree/vm/ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,8 @@ static inline float vm_erf_f32(float operand) { return erff(operand); }

static inline float vm_cast_si32f32(int32_t operand) { return (float)operand; }
static inline float vm_cast_si64f32(int64_t operand) { return (float)operand; }
static inline float vm_cast_ui32f32(int32_t operand) {
return (float)(uint32_t)operand;
}
static inline float vm_cast_ui64f32(uint64_t operand) { return (float)operand; }
static inline float vm_cast_ui32f32(uint32_t operand) { return (float)operand; }
static inline int32_t vm_cast_f32si32(float operand) {
return (int32_t)lroundf(operand);
}
Expand Down
Loading