Skip to content

Commit 01e99f2

Browse files
committed
deps: V8: backport 8521e2e77535
Original commit message: [riscv]Fix unordered FP comparison and emit at the consumer The lowering for kFloatLessThanOrUnordered and kFloatLessThanOrEqualOrUnordered incorrectly mapped to (LT, true) and (LE, true), respectively. Since ComputeBranchInfo can negate a branch condition after emitting the comparison, complementary conditions must use the same comparison and invert its predicate. The incorrect mappings dropped the negation, breaking NaN semantics. For example, optimized Wasm loops like `do { x += 1.0; } while (!(x > 0.0));` produced wrong results. Fix the mappings: kFloatLessThanOrUnordered now uses negated GE, and kFloatLessThanOrEqualOrUnordered uses negated GT. To prevent this class of bug, also convert kRiscvCmpS and kRiscvCmpD into pseudo-instructions like the integer kRiscvCmp. The actual comparison is now emitted at the single flags consumer (branch, boolean materialization, or select) through the new EmitFPCompare helper, using the consumer's final FlagsCondition. This ensures the predicate and machine condition are resolved consistently. Generated code remains byte-identical. Bug: 557938134 Change-Id: I230e181753c5e9c6e7fbea651e551192a76e4005 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8360836 Reviewed-by: Ji Qiu <qiuji@iscas.ac.cn> Auto-Submit: Yahan Lu (LuYahan) <yahan@iscas.ac.cn> Commit-Queue: Ji Qiu <qiuji@iscas.ac.cn> Cr-Commit-Position: refs/heads/main@{#109690} Refs: v8/v8@8521e2e Co-authored-by: LuYahan <yahan@iscas.ac.cn>
1 parent b4f5d35 commit 01e99f2

2 files changed

Lines changed: 53 additions & 47 deletions

File tree

‎common.gypi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
# Reset this number to 0 on major V8 upgrades.
4545
# Increment by one for each non-official patch applied to deps/v8.
46-
'v8_embedder_string': '-node.32',
46+
'v8_embedder_string': '-node.33',
4747

4848
##### V8 defaults for Node.js #####
4949

‎deps/v8/src/compiler/backend/riscv/code-generator-riscv.cc‎

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -433,24 +433,62 @@ FPUCondition FlagsConditionToConditionCmpFPU(bool* predicate,
433433
*predicate = true;
434434
return GE;
435435
case kFloatLessThanOrUnordered:
436-
*predicate = true;
437-
return LT;
436+
*predicate = false;
437+
return GE;
438438
case kFloatGreaterThanOrUnordered:
439439
*predicate = false;
440440
return LE;
441441
case kFloatGreaterThanOrEqualOrUnordered:
442442
*predicate = false;
443443
return LT;
444444
case kFloatLessThanOrEqualOrUnordered:
445-
*predicate = true;
446-
return LE;
445+
*predicate = false;
446+
return GT;
447447
default:
448448
*predicate = true;
449449
break;
450450
}
451451
UNREACHABLE();
452452
}
453453

454+
// Emits the actual floating-point comparison for a kRiscvCmpS/kRiscvCmpD
455+
// instruction, materializing its boolean result (0 or 1) into {dst}.
456+
// The comparison itself is a pseudo-instruction (like the integer kRiscvCmp):
457+
// nothing is emitted at the instruction site. Instead, the comparison and the
458+
// FPUCondition-to-machine mapping are resolved together here, at the single
459+
// flags consumer (branch, boolean materialization, or select), using the
460+
// consumer's final FlagsCondition {condition} (which ComputeBranchInfo may
461+
// have negated for branches). This makes it structurally impossible for
462+
// complementary conditions to observe a comparison that was emitted for a
463+
// different condition.
464+
// The returned predicate tells the consumer how to interpret {dst}: when
465+
// false, the comparison result must be inverted to obtain the condition's
466+
// truth value (this is how conditions involving "unordered" are expressed
467+
// with RISC-V's NaN-false flt/fle/feq instructions).
468+
void EmitFPCompare(MacroAssembler* masm, RiscvOperandConverter& i,
469+
Instruction* instr, FlagsCondition condition, Register dst,
470+
bool* predicate) {
471+
FPUCondition cc = FlagsConditionToConditionCmpFPU(predicate, condition);
472+
if (instr->arch_opcode() == kRiscvCmpS) {
473+
FPURegister left = i.InputOrZeroSingleRegister(0);
474+
FPURegister right = i.InputOrZeroSingleRegister(1);
475+
if ((left == kSingleRegZero || right == kSingleRegZero) &&
476+
!masm->IsSingleZeroRegSet()) {
477+
masm->LoadFPRImmediate(kSingleRegZero, 0.0f);
478+
}
479+
masm->CompareF32(dst, cc, left, right);
480+
} else {
481+
DCHECK_EQ(instr->arch_opcode(), kRiscvCmpD);
482+
FPURegister left = i.InputOrZeroDoubleRegister(0);
483+
FPURegister right = i.InputOrZeroDoubleRegister(1);
484+
if ((left == kDoubleRegZero || right == kDoubleRegZero) &&
485+
!masm->IsDoubleZeroRegSet()) {
486+
masm->LoadFPRImmediate(kDoubleRegZero, 0.0);
487+
}
488+
masm->CompareF64(dst, cc, left, right);
489+
}
490+
}
491+
454492
#if V8_ENABLE_WEBASSEMBLY
455493
class WasmOutOfLineTrap : public OutOfLineCode {
456494
public:
@@ -1597,26 +1635,10 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction(
15971635
// Pseudo-instruction used for cmpzero/branch. No opcode emitted here.
15981636
break;
15991637

1600-
case kRiscvCmpS: {
1601-
FPURegister left = i.InputOrZeroSingleRegister(0);
1602-
FPURegister right = i.InputOrZeroSingleRegister(1);
1603-
bool predicate;
1604-
FPUCondition cc =
1605-
FlagsConditionToConditionCmpFPU(&predicate, instr->flags_condition());
1606-
1607-
if ((left == kSingleRegZero || right == kSingleRegZero) &&
1608-
!__ IsSingleZeroRegSet()) {
1609-
__ LoadFPRImmediate(kSingleRegZero, 0.0f);
1610-
}
1611-
switch (FlagsModeField::decode(instr->opcode())) {
1612-
case kFlags_set:
1613-
__ CompareF32(i.OutputRegister(), cc, left, right);
1614-
break;
1615-
default:
1616-
__ CompareF32(kScratchReg, cc, left, right);
1617-
break;
1618-
}
1619-
} break;
1638+
case kRiscvCmpS:
1639+
// Pseudo-instruction used for FP cmp/branch. No opcode emitted here;
1640+
// the comparison is emitted by the flags consumer via EmitFPCompare.
1641+
break;
16201642
case kRiscvAddS:
16211643
// TODO(plind): add special case: combine mult & add.
16221644
__ fadd_s(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
@@ -1645,25 +1667,10 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction(
16451667
__ fsqrt_s(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
16461668
break;
16471669
}
1648-
case kRiscvCmpD: {
1649-
FPURegister left = i.InputOrZeroDoubleRegister(0);
1650-
FPURegister right = i.InputOrZeroDoubleRegister(1);
1651-
bool predicate;
1652-
FPUCondition cc =
1653-
FlagsConditionToConditionCmpFPU(&predicate, instr->flags_condition());
1654-
if ((left == kDoubleRegZero || right == kDoubleRegZero) &&
1655-
!__ IsDoubleZeroRegSet()) {
1656-
__ LoadFPRImmediate(kDoubleRegZero, 0.0);
1657-
}
1658-
switch (FlagsModeField::decode(instr->opcode())) {
1659-
case kFlags_set:
1660-
__ CompareF64(i.OutputRegister(), cc, left, right);
1661-
break;
1662-
default:
1663-
__ CompareF64(kScratchReg, cc, left, right);
1664-
break;
1665-
}
1666-
} break;
1670+
case kRiscvCmpD:
1671+
// Pseudo-instruction used for FP cmp/branch. No opcode emitted here;
1672+
// the comparison is emitted by the flags consumer via EmitFPCompare.
1673+
break;
16671674
#if V8_TARGET_ARCH_RISCV32
16681675
case kRiscvAddPair:
16691676
__ AddPair(i.OutputRegister(0), i.OutputRegister(1), i.InputRegister(0),
@@ -4600,8 +4607,7 @@ void AssembleBranchToLabels(CodeGenerator* gen, MacroAssembler* masm,
46004607
} else if (instr->arch_opcode() == kRiscvCmpS ||
46014608
instr->arch_opcode() == kRiscvCmpD) {
46024609
bool predicate;
4603-
FlagsConditionToConditionCmpFPU(&predicate, condition);
4604-
// floating-point compare result is set in kScratchReg
4610+
EmitFPCompare(masm, i, instr, condition, kScratchReg, &predicate);
46054611
if (predicate) {
46064612
__ BranchTrueF(kScratchReg, tlabel);
46074613
} else {
@@ -5005,7 +5011,7 @@ void CodeGenerator::AssembleArchBoolean(Instruction* instr,
50055011
} else if (instr->arch_opcode() == kRiscvCmpD ||
50065012
instr->arch_opcode() == kRiscvCmpS) {
50075013
bool predicate;
5008-
FlagsConditionToConditionCmpFPU(&predicate, condition);
5014+
EmitFPCompare(masm(), i, instr, condition, result, &predicate);
50095015
// RISCV compare returns 0 or 1, do nothing when predicate; otherwise
50105016
// toggle result (i.e., 0 -> 1, 1 -> 0)
50115017
if (!predicate) {

0 commit comments

Comments
 (0)