Skip to content

Commit d4dbcc0

Browse files
Javonne-liutargos
authored andcommitted
deps: V8: backport c4d06ba586f3
Original commit message: [loong64][compiler] Extend Word64Select instruction functionality Change-Id: Iba762777642d2d2d3aa904f9afc1e9005139992e Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7801520 Reviewed-by: Zhao Jiazhong <[email protected]> Commit-Queue: Liu Yu <[email protected]> Reviewed-by: Darius Mercadier <[email protected]> Auto-Submit: Liu Yu <[email protected]> Cr-Commit-Position: refs/heads/main@{#107619} Refs: v8/v8@c4d06ba Co-authored-by: liujiahui <[email protected]> PR-URL: nodejs#63731 Fixes: nodejs#63721 Reviewed-By: René <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent c15dbf0 commit d4dbcc0

7 files changed

Lines changed: 443 additions & 45 deletions

File tree

‎common.gypi‎

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

4343
# Reset this number to 0 on major V8 upgrades.
4444
# Increment by one for each non-official patch applied to deps/v8.
45-
'v8_embedder_string': '-node.19',
45+
'v8_embedder_string': '-node.20',
4646

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

‎deps/v8/src/codegen/loong64/macro-assembler-loong64.cc‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3201,6 +3201,21 @@ void MacroAssembler::Float64Mod(DoubleRegister out, DoubleRegister left,
32013201
bind(&done_mod);
32023202
}
32033203

3204+
void MacroAssembler::SelectWord(Register result, Register cond, Register v_true,
3205+
Register v_false) {
3206+
if (v_false == zero_reg) {
3207+
maskeqz(result, v_true, cond);
3208+
} else if (v_true == zero_reg) {
3209+
masknez(result, v_false, cond);
3210+
} else {
3211+
UseScratchRegisterScope temps(this);
3212+
Register scratch = temps.Acquire();
3213+
maskeqz(scratch, v_true, cond);
3214+
masknez(result, v_false, cond);
3215+
or_(result, scratch, result);
3216+
}
3217+
}
3218+
32043219
void MacroAssembler::CompareWord(Condition cond, Register dst, Register lhs,
32053220
const Operand& rhs) {
32063221
switch (cond) {

‎deps/v8/src/codegen/loong64/macro-assembler-loong64.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ class V8_EXPORT_PRIVATE MacroAssembler : public MacroAssemblerBase {
142142
// Print a message to stdout and abort execution.
143143
void Abort(AbortReason msg);
144144

145+
// Select v_true if cond is non-zero, otherwise select v_false.
146+
void SelectWord(Register result, Register cond, Register v_true,
147+
Register v_false);
148+
145149
void CompareWord(Condition cond, Register dst, Register lhs,
146150
const Operand& rhs);
147151
void Branch(Label* label, bool need_link = false);

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

Lines changed: 140 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,10 +1377,14 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction(
13771377
case kLoong64Sub_d:
13781378
__ Sub_d(i.OutputRegister(), i.InputRegister(0), i.InputOperand(1));
13791379
break;
1380-
case kLoong64SubOvf_d:
1380+
case kLoong64SubOvf_d: {
1381+
UseScratchRegisterScope temps(masm());
1382+
DCHECK(temps.hasAvailable());
1383+
temps.Exclude(t8);
13811384
__ SubOverflow_d(i.OutputRegister(), i.InputRegister(0),
13821385
i.InputOperand(1), t8);
13831386
break;
1387+
}
13841388
case kLoong64Mul_w:
13851389
__ Mul_w(i.OutputRegister(), i.InputRegister(0), i.InputOperand(1));
13861390
break;
@@ -4617,7 +4621,7 @@ void CodeGenerator::AssembleArchBoolean(Instruction* instr,
46174621
}
46184622
return;
46194623
} else {
4620-
PrintF("AssembleArchBranch Unimplemented arch_opcode is : %d\n",
4624+
PrintF("AssembleArchBoolean Unimplemented arch_opcode is : %d\n",
46214625
instr->arch_opcode());
46224626
TRACE("UNIMPLEMENTED code_generator_loong64: %s at line %d\n", __FUNCTION__,
46234627
__LINE__);
@@ -4672,56 +4676,49 @@ void CodeGenerator::AssembleArchSelect(Instruction* instr,
46724676
size_t output_index = instr->OutputCount() - 1;
46734677
// We don't know how many inputs were consumed by the condition, so we have to
46744678
// calculate the indices of the last two inputs.
4675-
DCHECK_GE(instr->InputCount(), 4);
46764679
size_t true_value_index = instr->InputCount() - 2;
46774680
size_t false_value_index = instr->InputCount() - 1;
4681+
Register result = i.OutputRegister(output_index);
4682+
Register v_true = i.InputOrZeroRegister(true_value_index);
4683+
Register v_false = i.InputOrZeroRegister(false_value_index);
4684+
4685+
DCHECK(
4686+
LocationOperand::cast(instr->OutputAt(output_index))->representation() ==
4687+
MachineRepresentation::kWord64);
46784688

46794689
if (instr->arch_opcode() == kLoong64Tst) {
4690+
DCHECK_GE(instr->InputCount(), 4);
46804691
Condition cc = FlagsConditionToConditionTst(condition);
4681-
Register result = i.OutputRegister(output_index);
4682-
Register v_true = i.InputOrZeroRegister(true_value_index);
4683-
Register v_false = i.InputOrZeroRegister(false_value_index);
4684-
if (v_true == zero_reg || v_false == zero_reg) {
4685-
if (v_true == zero_reg) {
4686-
v_true = v_false;
4687-
cc = NegateCondition(cc);
4688-
}
4689-
if (cc == eq)
4690-
__ masknez(result, v_true, t8);
4691-
else
4692-
__ maskeqz(result, v_true, t8);
4693-
} else if (result == v_true || result == v_false) {
4694-
if (result == v_false) {
4695-
v_false = v_true;
4696-
cc = NegateCondition(cc);
4697-
}
4698-
Label done;
4699-
__ Branch(&done, cc, t8, Operand(0));
4700-
__ Move(result, v_false);
4701-
__ bind(&done);
4702-
} else {
4703-
UseScratchRegisterScope temps(masm());
4704-
Register scratch = temps.Acquire();
4705-
if (cc == eq) {
4706-
Register temp = v_true;
4707-
v_true = v_false;
4708-
v_false = temp;
4709-
}
4710-
__ maskeqz(scratch, v_true, t8);
4711-
__ masknez(result, v_false, t8);
4712-
__ or_(result, scratch, result);
4692+
if (cc == eq) {
4693+
Register temp = v_true;
4694+
v_true = v_false;
4695+
v_false = temp;
47134696
}
4697+
__ SelectWord(result, t8, v_true, v_false);
47144698
UseScratchRegisterScope temps(masm());
47154699
temps.Include(t8);
47164700
return;
47174701
} else if (instr->arch_opcode() == kLoong64Cmp64 ||
4718-
instr->arch_opcode() == kLoong64Cmp32) {
4702+
instr->arch_opcode() == kLoong64Cmp32 ||
4703+
instr->arch_opcode() == kArchStackPointerGreaterThan) {
47194704
Condition cc = FlagsConditionToConditionCmp(condition);
4720-
Register left = i.InputRegister(0);
4721-
Operand right = i.InputOperand(1);
4722-
Register result = i.OutputRegister(output_index);
4723-
Register v_true = i.InputOrZeroRegister(true_value_index);
4724-
Register v_false = i.InputOrZeroRegister(false_value_index);
4705+
Register left = no_reg;
4706+
Operand right = Operand(0);
4707+
if (instr->arch_opcode() == kArchStackPointerGreaterThan) {
4708+
DCHECK_GE(instr->InputCount(), 3);
4709+
DCHECK((cc == ls) || (cc == hi));
4710+
left = sp;
4711+
right = i.InputOperand(0);
4712+
uint32_t offset;
4713+
if (ShouldApplyOffsetToStackCheck(instr, &offset)) {
4714+
left = i.TempRegister(1);
4715+
__ Sub_d(left, sp, offset);
4716+
}
4717+
} else {
4718+
DCHECK_GE(instr->InputCount(), 4);
4719+
left = i.InputRegister(0);
4720+
right = i.InputOperand(1);
4721+
}
47254722
if (v_true == zero_reg || v_false == zero_reg) {
47264723
if (v_true == zero_reg) {
47274724
v_true = v_false;
@@ -4751,6 +4748,108 @@ void CodeGenerator::AssembleArchSelect(Instruction* instr,
47514748
__ bind(&done);
47524749
}
47534750
return;
4751+
} else if (instr->arch_opcode() == kLoong64Add_d ||
4752+
instr->arch_opcode() == kLoong64Sub_d) {
4753+
DCHECK_GE(instr->InputCount(), 4);
4754+
Condition cc = FlagsConditionToConditionOvf(condition);
4755+
if (cc == eq) {
4756+
Register temp = v_true;
4757+
v_true = v_false;
4758+
v_false = temp;
4759+
}
4760+
UseScratchRegisterScope temps(masm());
4761+
Register scratch1 = temps.Acquire();
4762+
Register scratch2 = temps.Acquire();
4763+
__ srai_d(scratch1, i.OutputRegister(), 32);
4764+
__ srai_w(scratch2, i.OutputRegister(), 31);
4765+
if (v_false == zero_reg) {
4766+
__ xor_(scratch1, scratch1, scratch2);
4767+
__ maskeqz(result, v_true, scratch1);
4768+
} else if (v_true == zero_reg) {
4769+
__ xor_(scratch1, scratch1, scratch2);
4770+
__ masknez(result, v_false, scratch1);
4771+
} else if (result == v_true || result == v_false) {
4772+
if (result == v_false) {
4773+
v_false = v_true;
4774+
cc = NegateCondition(cc);
4775+
}
4776+
Label done;
4777+
__ Branch(&done, cc, scratch2, Operand(scratch1));
4778+
__ Move(result, v_false);
4779+
__ bind(&done);
4780+
} else {
4781+
Label true_label, done;
4782+
__ Branch(&true_label, cc, scratch2, Operand(scratch1));
4783+
__ Move(result, v_false);
4784+
__ Branch(&done);
4785+
__ bind(&true_label);
4786+
__ Move(result, v_true);
4787+
__ bind(&done);
4788+
}
4789+
return;
4790+
} else if (instr->arch_opcode() == kLoong64AddOvf_d ||
4791+
instr->arch_opcode() == kLoong64SubOvf_d) {
4792+
DCHECK_GE(instr->InputCount(), 4);
4793+
// Overflow occurs if overflow register is negative
4794+
Condition cc = lt;
4795+
if (condition == kNotOverflow) {
4796+
Register temp = v_true;
4797+
v_true = v_false;
4798+
v_false = temp;
4799+
}
4800+
if (v_false == zero_reg) {
4801+
__ slt(t8, t8, zero_reg);
4802+
__ maskeqz(result, v_true, t8);
4803+
} else if (v_true == zero_reg) {
4804+
__ slt(t8, t8, zero_reg);
4805+
__ masknez(result, v_false, t8);
4806+
} else if (result == v_true || result == v_false) {
4807+
if (result == v_false) {
4808+
v_false = v_true;
4809+
cc = NegateCondition(cc);
4810+
}
4811+
Label done;
4812+
__ Branch(&done, cc, t8, Operand(zero_reg));
4813+
__ Move(result, v_false);
4814+
__ bind(&done);
4815+
} else {
4816+
Label true_label, done;
4817+
__ Branch(&true_label, cc, t8, Operand(zero_reg));
4818+
__ Move(result, v_false);
4819+
__ Branch(&done);
4820+
__ bind(&true_label);
4821+
__ Move(result, v_true);
4822+
__ bind(&done);
4823+
}
4824+
UseScratchRegisterScope temps(masm());
4825+
temps.Include(t8);
4826+
} else if (instr->arch_opcode() == kLoong64MulOvf_w ||
4827+
instr->arch_opcode() == kLoong64MulOvf_d) {
4828+
DCHECK_GE(instr->InputCount(), 4);
4829+
Condition cc = FlagsConditionToConditionOvf(condition);
4830+
if (cc == eq) {
4831+
Register temp = v_true;
4832+
v_true = v_false;
4833+
v_false = temp;
4834+
}
4835+
__ SelectWord(result, t8, v_true, v_false);
4836+
UseScratchRegisterScope temps(masm());
4837+
temps.Include(t8);
4838+
return;
4839+
} else if (instr->arch_opcode() == kLoong64Float32Cmp ||
4840+
instr->arch_opcode() == kLoong64Float64Cmp) {
4841+
bool predicate;
4842+
FlagsConditionToConditionCmpFPU(&predicate, condition);
4843+
UseScratchRegisterScope temps(masm());
4844+
Register scratch = temps.Acquire();
4845+
if (!predicate) {
4846+
Register temp = v_true;
4847+
v_true = v_false;
4848+
v_false = temp;
4849+
}
4850+
__ movcf2gr(scratch, FCC0);
4851+
__ SelectWord(result, scratch, v_true, v_false);
4852+
return;
47544853
} else {
47554854
PrintF("AssembleArchSelect Unimplemented arch_opcode is : %d\n",
47564855
instr->arch_opcode());

‎deps/v8/src/compiler/backend/loong64/instruction-selector-loong64.cc‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ class Loong64OperandGenerator final : public OperandGenerator {
4848
return UseRegister(node);
4949
}
5050

51+
InstructionOperand UseRegisterAtEndOrImmediateZero(OpIndex node) {
52+
if (const ConstantOp* constant =
53+
selector()->Get(node).TryCast<ConstantOp>()) {
54+
if ((constant->IsIntegral() && constant->integral() == 0) ||
55+
(constant->kind == ConstantOp::Kind::kFloat32 &&
56+
constant->float32().get_bits() == 0) ||
57+
(constant->kind == ConstantOp::Kind::kFloat64 &&
58+
constant->float64().get_bits() == 0)) {
59+
return UseImmediate(node);
60+
}
61+
}
62+
return UseRegisterAtEnd(node);
63+
}
64+
5165
bool IsIntegerConstant(OpIndex node) {
5266
int64_t unused;
5367
return selector()->MatchSignedIntegralConstant(node, &unused);
@@ -307,7 +321,7 @@ static void VisitBinop(InstructionSelector* selector, turboshaft::OpIndex node,
307321
InstructionCode reverse_opcode,
308322
FlagsContinuation* cont) {
309323
Loong64OperandGenerator g(selector);
310-
InstructionOperand inputs[2];
324+
InstructionOperand inputs[4];
311325
size_t input_count = 0;
312326
InstructionOperand outputs[1];
313327
size_t output_count = 0;
@@ -331,6 +345,13 @@ static void VisitBinop(InstructionSelector* selector, turboshaft::OpIndex node,
331345
inputs[input_count++] = g.UseOperand(right_node, opcode);
332346
}
333347

348+
if (cont->IsSelect()) {
349+
inputs[input_count++] =
350+
g.UseRegisterAtEndOrImmediateZero(cont->true_value());
351+
inputs[input_count++] =
352+
g.UseRegisterAtEndOrImmediateZero(cont->false_value());
353+
}
354+
334355
outputs[output_count++] = g.DefineAsRegister(node);
335356

336357
DCHECK_NE(0u, input_count);
@@ -2176,9 +2197,14 @@ void InstructionSelector::VisitStackPointerGreaterThan(
21762197
? OperandGenerator::kUniqueRegister
21772198
: OperandGenerator::kRegister;
21782199

2179-
InstructionOperand inputs[] = {g.UseRegisterWithMode(value, register_mode)};
2180-
static constexpr int input_count = arraysize(inputs);
2200+
InstructionOperand inputs[3];
2201+
int input_count = 0;
2202+
inputs[input_count++] = g.UseRegisterWithMode(value, register_mode);
21812203

2204+
if (cont->IsSelect()) {
2205+
inputs[input_count++] = g.UseRegisterOrImmediateZero(cont->true_value());
2206+
inputs[input_count++] = g.UseRegisterOrImmediateZero(cont->false_value());
2207+
}
21822208
EmitWithContinuation(opcode, output_count, outputs, input_count, inputs,
21832209
temp_count, temps, cont);
21842210
}

‎deps/v8/test/cctest/BUILD.gn‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ v8_cluster_source_set("cctest_sources") {
245245
if (is_win) {
246246
sources += [ "test-stack-unwinding-win64.cc" ]
247247
}
248+
} else if (v8_current_cpu == "loong64") {
249+
if (v8_enable_turbofan) {
250+
sources += [ "compiler/test-run-machops-select-loong64.cc" ]
251+
}
248252
}
249253

250254
if (v8_use_perfetto) {

0 commit comments

Comments
 (0)