Skip to content

Commit f6be482

Browse files
verwaestV8 LUCI CQ
authored andcommitted
Reland "[maglev] Turn add into stringconcat if string input"
... forgot about turbolev Bug: 406843931 Change-Id: I295bb9c44f318995270a436723689386f55be78d Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6414389 Reviewed-by: Olivier Flückiger <[email protected]> Commit-Queue: Olivier Flückiger <[email protected]> Auto-Submit: Toon Verwaest <[email protected]> Commit-Queue: Toon Verwaest <[email protected]> Cr-Commit-Position: refs/heads/main@{#99571}
1 parent e9e0462 commit f6be482

7 files changed

Lines changed: 83 additions & 26 deletions

File tree

src/builtins/builtins-string.tq

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ transitioning macro ToStringImpl(context: Context, o: JSAny): String {
3939
unreachable;
4040
}
4141

42+
transitioning builtin ToStringForAdd(context: Context, o: JSAny): String {
43+
return ToStringImpl(context, ToPrimitiveDefault(o));
44+
}
45+
4246
transitioning builtin ToString(context: Context, o: JSAny): String {
4347
return ToStringImpl(context, o);
4448
}

src/compiler/turboshaft/assembler.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3627,6 +3627,20 @@ class TurboshaftAssemblerOpInterface
36273627
return CallBuiltin<typename BuiltinCallDescriptor::ToString>(
36283628
isolate, frame_state, context, {input}, lazy_deopt_on_throw);
36293629
}
3630+
V<String> CallBuiltin_ToStringConvertSymbol(
3631+
Isolate* isolate, V<turboshaft::FrameState> frame_state,
3632+
V<Context> context, V<Object> input,
3633+
LazyDeoptOnThrow lazy_deopt_on_throw) {
3634+
return CallBuiltin<typename BuiltinCallDescriptor::ToStringConvertSymbol>(
3635+
isolate, frame_state, context, {input}, lazy_deopt_on_throw);
3636+
}
3637+
V<String> CallBuiltin_ToStringForAdd(Isolate* isolate,
3638+
V<turboshaft::FrameState> frame_state,
3639+
V<Context> context, V<Object> input,
3640+
LazyDeoptOnThrow lazy_deopt_on_throw) {
3641+
return CallBuiltin<typename BuiltinCallDescriptor::ToStringForAdd>(
3642+
isolate, frame_state, context, {input}, lazy_deopt_on_throw);
3643+
}
36303644
V<Number> CallBuiltin_PlainPrimitiveToNumber(Isolate* isolate,
36313645
V<PlainPrimitive> input) {
36323646
return CallBuiltin<typename BuiltinCallDescriptor::PlainPrimitiveToNumber>(

src/compiler/turboshaft/builtin-call-descriptors.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,28 @@ struct BuiltinCallDescriptor {
293293
static constexpr OpEffects kEffects = base_effects.CanCallAnything();
294294
};
295295

296+
struct ToStringConvertSymbol : public Descriptor<ToString> {
297+
static constexpr auto kFunction = Builtin::kToStringConvertSymbol;
298+
using arguments_t = std::tuple<V<Object>>;
299+
using results_t = std::tuple<V<String>>;
300+
301+
static constexpr bool kNeedsFrameState = true;
302+
static constexpr bool kNeedsContext = true;
303+
static constexpr Operator::Properties kProperties = Operator::kNoProperties;
304+
static constexpr OpEffects kEffects = base_effects.CanCallAnything();
305+
};
306+
307+
struct ToStringForAdd : public Descriptor<ToString> {
308+
static constexpr auto kFunction = Builtin::kToStringForAdd;
309+
using arguments_t = std::tuple<V<Object>>;
310+
using results_t = std::tuple<V<String>>;
311+
312+
static constexpr bool kNeedsFrameState = true;
313+
static constexpr bool kNeedsContext = true;
314+
static constexpr Operator::Properties kProperties = Operator::kNoProperties;
315+
static constexpr OpEffects kEffects = base_effects.CanCallAnything();
316+
};
317+
296318
struct PlainPrimitiveToNumber : public Descriptor<PlainPrimitiveToNumber> {
297319
static constexpr auto kFunction = Builtin::kPlainPrimitiveToNumber;
298320
using arguments_t = std::tuple<V<PlainPrimitive>>;

src/compiler/turboshaft/turbolev-graph-builder.cc

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,21 +2833,24 @@ class GraphBuildingNodeProcessor {
28332833

28342834
GOTO_IF(__ ObjectIsString(value), done, V<String>::Cast(value));
28352835

2836-
IF_NOT (__ IsSmi(value)) {
2837-
if (node->mode() == maglev::ToString::ConversionMode::kConvertSymbol) {
2838-
V<i::Map> map = __ LoadMapField(value);
2839-
V<Word32> instance_type = __ LoadInstanceTypeField(map);
2840-
IF (__ Word32Equal(instance_type, SYMBOL_TYPE)) {
2841-
GOTO(done, __ CallRuntime_SymbolDescriptiveString(
2842-
isolate_, frame_state, Map(node->context()),
2843-
V<Symbol>::Cast(value), ShouldLazyDeoptOnThrow(node)));
2844-
}
2845-
}
2846-
}
2836+
switch (node->mode()) {
2837+
case maglev::ToString::kConvertSymbol:
2838+
GOTO(done, __ CallBuiltin_ToStringConvertSymbol(
2839+
isolate_, frame_state, Map(node->context()), value,
2840+
ShouldLazyDeoptOnThrow(node)));
2841+
break;
2842+
case maglev::ToString::kForAdd:
2843+
GOTO(done, __ CallBuiltin_ToStringForAdd(isolate_, frame_state,
2844+
Map(node->context()), value,
2845+
ShouldLazyDeoptOnThrow(node)));
2846+
break;
2847+
case maglev::ToString::kThrowOnSymbol:
2848+
GOTO(done, __ CallBuiltin_ToString(isolate_, frame_state,
2849+
Map(node->context()), value,
2850+
ShouldLazyDeoptOnThrow(node)));
28472851

2848-
GOTO(done,
2849-
__ CallBuiltin_ToString(isolate_, frame_state, Map(node->context()),
2850-
value, ShouldLazyDeoptOnThrow(node)));
2852+
break;
2853+
}
28512854

28522855
BIND(done, result);
28532856
SetMap(node, result);

src/maglev/maglev-graph-builder.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,6 +2837,8 @@ template <Operation kOperation>
28372837
ReduceResult MaglevGraphBuilder::VisitBinaryOperation() {
28382838
FeedbackNexus nexus = FeedbackNexusForOperand(1);
28392839
BinaryOperationHint feedback_hint = nexus.GetBinaryOperationFeedback();
2840+
ValueNode* left = LoadRegister(0);
2841+
ValueNode* right = GetAccumulator();
28402842
switch (feedback_hint) {
28412843
case BinaryOperationHint::kNone:
28422844
return EmitUnconditionalDeopt(
@@ -2866,8 +2868,6 @@ ReduceResult MaglevGraphBuilder::VisitBinaryOperation() {
28662868
}
28672869
case BinaryOperationHint::kString:
28682870
if constexpr (kOperation == Operation::kAdd) {
2869-
ValueNode* left = LoadRegister(0);
2870-
ValueNode* right = GetAccumulator();
28712871
return BuildStringConcat(left, right);
28722872
}
28732873
break;
@@ -2876,8 +2876,6 @@ ReduceResult MaglevGraphBuilder::VisitBinaryOperation() {
28762876
if (broker()
28772877
->dependencies()
28782878
->DependOnStringWrapperToPrimitiveProtector()) {
2879-
ValueNode* left = LoadRegister(0);
2880-
ValueNode* right = GetAccumulator();
28812879
BuildCheckStringOrStringWrapper(left);
28822880
BuildCheckStringOrStringWrapper(right);
28832881
left = BuildUnwrapStringWrapper(left);
@@ -2892,6 +2890,15 @@ ReduceResult MaglevGraphBuilder::VisitBinaryOperation() {
28922890
// Fallback to generic node.
28932891
break;
28942892
}
2893+
if constexpr (kOperation == Operation::kAdd) {
2894+
if (CheckType(left, NodeType::kString)) {
2895+
right = BuildToString(right, ToString::kForAdd);
2896+
return BuildStringConcat(left, right);
2897+
} else if (CheckType(right, NodeType::kString)) {
2898+
left = BuildToString(left, ToString::kForAdd);
2899+
return BuildStringConcat(left, right);
2900+
}
2901+
}
28952902
BuildGenericBinaryOperationNode<kOperation>();
28962903
return ReduceResult::Done();
28972904
}

src/maglev/maglev-ir.cc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5529,12 +5529,19 @@ void ToString::GenerateCode(MaglevAssembler* masm,
55295529
__ JumpIfSmi(value, &call_builtin, Label::Distance::kNear);
55305530
__ JumpIfString(value, &done, Label::Distance::kNear);
55315531
__ bind(&call_builtin);
5532-
if (mode() == kConvertSymbol) {
5533-
__ CallBuiltin<Builtin::kToStringConvertSymbol>(context(), // context
5534-
value_input()); // input
5535-
} else {
5536-
__ CallBuiltin<Builtin::kToString>(context(), // context
5537-
value_input()); // input
5532+
switch (mode()) {
5533+
case kConvertSymbol:
5534+
__ CallBuiltin<Builtin::kToStringConvertSymbol>(context(), // context
5535+
value_input()); // input
5536+
break;
5537+
case kForAdd:
5538+
__ CallBuiltin<Builtin::kToStringForAdd>(context(), // context
5539+
value_input()); // input
5540+
break;
5541+
case kThrowOnSymbol:
5542+
__ CallBuiltin<Builtin::kToString>(context(), // context
5543+
value_input()); // input
5544+
break;
55385545
}
55395546
masm->DefineExceptionHandlerAndLazyDeoptPoint(this);
55405547
__ bind(&done);

src/maglev/maglev-ir.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5123,7 +5123,7 @@ class ToString : public FixedInputValueNodeT<2, ToString> {
51235123
using Base = FixedInputValueNodeT<2, ToString>;
51245124

51255125
public:
5126-
enum ConversionMode { kConvertSymbol, kThrowOnSymbol };
5126+
enum ConversionMode { kConvertSymbol, kThrowOnSymbol, kForAdd };
51275127
explicit ToString(uint64_t bitfield, ConversionMode mode)
51285128
: Base(ConversionModeBitField::update(bitfield, mode)) {}
51295129

@@ -5144,7 +5144,7 @@ class ToString : public FixedInputValueNodeT<2, ToString> {
51445144
void PrintParams(std::ostream&, MaglevGraphLabeller*) const {}
51455145

51465146
private:
5147-
using ConversionModeBitField = NextBitField<ConversionMode, 1>;
5147+
using ConversionModeBitField = NextBitField<ConversionMode, 2>;
51485148
};
51495149

51505150
class NumberToString : public FixedInputValueNodeT<1, NumberToString> {

0 commit comments

Comments
 (0)