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

[RISC-V] Synthesize some floating constants inline #111529

Merged
merged 4 commits into from
Feb 10, 2025
Merged
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
43 changes: 29 additions & 14 deletions src/coreclr/jit/codegenriscv64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1090,29 +1090,44 @@ void CodeGen::genSetRegToConst(regNumber targetReg, var_types targetType, GenTre
emitAttr size = emitActualTypeSize(tree);
double constValue = tree->AsDblCon()->DconValue();

// Make sure we use "fmv.w.x reg, zero" only for positive zero (0.0)
// and not for negative zero (-0.0)
assert(emitter::isFloatReg(targetReg));

// Make sure we use "fmv.w.x reg, zero" only for positive zero (0.0) and not for negative zero (-0.0)
if (FloatingPointUtils::isPositiveZero(constValue))
{
// A faster/smaller way to generate 0.0
// We will just zero out the entire vector register for both float and double
// We will just zero out the entire register for both float and double
emit->emitIns_R_R(size == EA_4BYTE ? INS_fmv_w_x : INS_fmv_d_x, size, targetReg, REG_R0);
break;
}
else

int64_t bits =
(size == EA_4BYTE)
? (int32_t)BitOperations::SingleToUInt32Bits(FloatingPointUtils::convertToSingle(constValue))
: (int64_t)BitOperations::DoubleToUInt64Bits(constValue);
bool fitsInLui = ((bits & 0xfff) == 0) && emitter::isValidSimm20(bits >> 12);
if (fitsInLui || emitter::isValidSimm12(bits)) // can we synthesize bits with a single instruction?
{
// Get a temp integer register to compute long address.
// regNumber addrReg = internalRegisters.GetSingle(tree);
regNumber temp = internalRegisters.GetSingle(tree);
if (fitsInLui)
{
emit->emitIns_R_I(INS_lui, size, temp, bits >> 12);
}
else
{
emit->emitIns_R_R_I(INS_addi, size, temp, REG_ZERO, bits);
}

// We must load the FP constant from the constant pool
// Emit a data section constant for the float or double constant.
CORINFO_FIELD_HANDLE hnd = emit->emitFltOrDblConst(constValue, size);
emit->emitIns_R_R(size == EA_4BYTE ? INS_fmv_w_x : INS_fmv_d_x, size, targetReg, temp);
break;
}

// Load the FP constant.
assert(emit->isFloatReg(targetReg));
// We must load the FP constant from the constant pool
// Emit a data section constant for the float or double constant.
CORINFO_FIELD_HANDLE hnd = emit->emitFltOrDblConst(constValue, size);

// Compute the address of the FP constant and load the data.
emit->emitIns_R_C(size == EA_4BYTE ? INS_flw : INS_fld, size, targetReg, REG_NA, hnd, 0);
}
// Compute the address of the FP constant and load the data.
emit->emitIns_R_C(size == EA_4BYTE ? INS_flw : INS_fld, size, targetReg, REG_NA, hnd, 0);
}
break;

Expand Down
20 changes: 16 additions & 4 deletions src/coreclr/jit/lsrariscv64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,22 @@ int LinearScan::BuildNode(GenTree* tree)

case GT_CNS_DBL:
{
// There is no instruction for loading float/double imm directly into FPR.
// Reserve int to load constant from memory (IF_LARGELDC)
buildInternalIntRegisterDefForNode(tree);
buildInternalRegisterUses();
emitAttr size = emitActualTypeSize(tree);

double constValue = tree->AsDblCon()->DconValue();
if (!FloatingPointUtils::isPositiveZero(constValue))
{
int64_t bits =
(size == EA_4BYTE)
? (int32_t)BitOperations::SingleToUInt32Bits(FloatingPointUtils::convertToSingle(constValue))
: (int64_t)BitOperations::DoubleToUInt64Bits(constValue);
bool fitsInLui = ((bits & 0xfff) == 0) && emitter::isValidSimm20(bits >> 12);
if (fitsInLui || emitter::isValidSimm12(bits)) // can we synthesize bits with a single instruction?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the logic is not trivial, I presume it'd be better to extract it into a function and use in both LSRA and codegen, but it's just a nit

{
buildInternalIntRegisterDefForNode(tree);
buildInternalRegisterUses();
}
}
}
FALLTHROUGH;

Expand Down
Loading