Skip to content

Commit

Permalink
deps: patch V8 to support compilation with MSVC
Browse files Browse the repository at this point in the history
This patches V8 v12.2 for Windows, by fixing multiple compilation
errors caused by V8 being a Clang-oriented project. There are various
types of errors fixed by this going from changing `using` directives
and renaming to overcoming the differences in which Clang and MSVC see
templates and metaprogramming.

The changes introduced here are strictly meant as a patch only, so they
shouldn't be pushed upstream.

Refs: #13
Refs: #14
  • Loading branch information
StefanStojanovic authored and targos committed Feb 20, 2024
1 parent 9343983 commit a05132d
Show file tree
Hide file tree
Showing 31 changed files with 129 additions and 131 deletions.
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

# Reset this number to 0 on major V8 upgrades.
# Increment by one for each non-official patch applied to deps/v8.
'v8_embedder_string': '-node.6',
'v8_embedder_string': '-node.7',

##### V8 defaults for Node.js #####

Expand Down
7 changes: 3 additions & 4 deletions deps/v8/src/builtins/builtins-collections-gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2782,10 +2782,9 @@ TNode<Word32T> WeakCollectionsBuiltinsAssembler::ShouldShrink(

TNode<IntPtrT> WeakCollectionsBuiltinsAssembler::ValueIndexFromKeyIndex(
TNode<IntPtrT> key_index) {
return IntPtrAdd(
key_index,
IntPtrConstant(EphemeronHashTable::TodoShape::kEntryValueIndex -
EphemeronHashTable::kEntryKeyIndex));
return IntPtrAdd(key_index,
IntPtrConstant(EphemeronHashTable::ShapeT::kEntryValueIndex -
EphemeronHashTable::kEntryKeyIndex));
}

TF_BUILTIN(WeakMapConstructor, WeakCollectionsBuiltinsAssembler) {
Expand Down
2 changes: 1 addition & 1 deletion deps/v8/src/codegen/code-stub-assembler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9455,7 +9455,7 @@ void CodeStubAssembler::NameDictionaryLookup(
CAST(UnsafeLoadFixedArrayElement(dictionary, index));
GotoIf(TaggedEqual(current, undefined), if_not_found);
if (mode == kFindExisting) {
if (Dictionary::TodoShape::kMatchNeedsHoleCheck) {
if (Dictionary::ShapeT::kMatchNeedsHoleCheck) {
GotoIf(TaggedEqual(current, TheHoleConstant()), &next_probe);
}
current = LoadName<Dictionary>(current);
Expand Down
6 changes: 6 additions & 0 deletions deps/v8/src/compiler/turboshaft/assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3934,8 +3934,14 @@ class TSAssembler
: public Assembler<reducer_list<TurboshaftAssemblerOpInterface, Reducers...,
TSReducerBase>> {
public:
#ifdef _WIN32
explicit TSAssembler(Graph& input_graph, Graph& output_graph,
Zone* phase_zone)
: Assembler(input_graph, output_graph, phase_zone) {}
#else
using Assembler<reducer_list<TurboshaftAssemblerOpInterface, Reducers...,
TSReducerBase>>::Assembler;
#endif
};

#include "src/compiler/turboshaft/undef-assembler-macros.inc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ void CodeEliminationAndSimplificationPhase::Run(Zone* temp_zone) {
// (which, for simplificy, doesn't use the Assembler helper
// methods, but only calls Next::ReduceLoad/Store).
DuplicationOptimizationReducer,
ValueNumberingReducer>::Run(temp_zone);
VariableReducerHotfix,
ValueNumberingReducer>::Run<false>(temp_zone);
}

} // namespace v8::internal::compiler::turboshaft
35 changes: 25 additions & 10 deletions deps/v8/src/compiler/turboshaft/copying-phase.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ struct PaddingSpace {
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
PaddingSpace padding);

template <class Next>
class VariableReducerHotfix : public Next {
public:
TURBOSHAFT_REDUCER_BOILERPLATE()

void SetVariable(Variable var, OpIndex new_index) {}
Variable NewLoopInvariantVariable(MaybeRegisterRepresentation rep) { return Variable(); }

OpIndex GetVariable(Variable var) { return OpIndex(); }
OpIndex GetPredecessorValue(Variable var, int predecessor_index) { return OpIndex(); }
};

template <typename Next>
class ReducerBaseForwarder;
template <typename Next>
Expand All @@ -46,6 +58,9 @@ class GraphVisitor : public Next {
template <typename N>
friend class ReducerBaseForwarder;

private:
bool contains_variable_reducer_;

public:
TURBOSHAFT_REDUCER_BOILERPLATE()

Expand All @@ -66,7 +81,8 @@ class GraphVisitor : public Next {
// `trace_reduction` is a template parameter to avoid paying for tracing at
// runtime.
template <bool trace_reduction>
void VisitGraph() {
void VisitGraph(bool contains_variable_reducer) {
contains_variable_reducer_ = contains_variable_reducer;
Asm().Analyze();

// Creating initial old-to-new Block mapping.
Expand Down Expand Up @@ -177,8 +193,7 @@ class GraphVisitor : public Next {
DCHECK(old_index.valid());
OpIndex result = op_mapping_[old_index];

if constexpr (reducer_list_contains<typename Next::ReducerList,
VariableReducer>::value) {
if (contains_variable_reducer_) {
if (!result.valid()) {
// {op_mapping} doesn't have a mapping for {old_index}. The assembler
// should provide the mapping.
Expand Down Expand Up @@ -1294,8 +1309,7 @@ class GraphVisitor : public Next {
DCHECK(Asm().input_graph().BelongsToThisGraph(old_index));
DCHECK_IMPLIES(new_index.valid(),
Asm().output_graph().BelongsToThisGraph(new_index));
if constexpr (reducer_list_contains<typename Next::ReducerList,
VariableReducer>::value) {
if (contains_variable_reducer_) {
if (current_block_needs_variables_) {
MaybeVariable var = GetVariableFor(old_index);
if (!var.has_value()) {
Expand Down Expand Up @@ -1393,29 +1407,30 @@ template <template <class> class... Reducers>
class CopyingPhaseImpl {
public:
static void Run(Graph& input_graph, Zone* phase_zone,
bool trace_reductions = false) {
bool contains_variable_reducer, bool trace_reductions = false) {
TSAssembler<GraphVisitor, Reducers...> phase(
input_graph, input_graph.GetOrCreateCompanion(), phase_zone);
#ifdef DEBUG
if (trace_reductions) {
phase.template VisitGraph<true>();
phase.template VisitGraph<true>(contains_variable_reducer);
} else {
phase.template VisitGraph<false>();
phase.template VisitGraph<false>(contains_variable_reducer);
}
#else
phase.template VisitGraph<false>();
phase.template VisitGraph<false>(contains_variable_reducer);
#endif // DEBUG
}
};

template <template <typename> typename... Reducers>
class CopyingPhase {
public:
template <bool contains_variable_reducer>
static void Run(Zone* phase_zone) {
PipelineData& data = PipelineData::Get();
Graph& input_graph = data.graph();
CopyingPhaseImpl<Reducers...>::Run(
input_graph, phase_zone, data.info()->turboshaft_trace_reduction());
input_graph, phase_zone, contains_variable_reducer, data.info()->turboshaft_trace_reduction());
}
};

Expand Down
10 changes: 5 additions & 5 deletions deps/v8/src/compiler/turboshaft/csa-optimize-phase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ namespace v8::internal::compiler::turboshaft {
void CsaLoadEliminationPhase::Run(Zone* temp_zone) {
CopyingPhase<VariableReducer, MachineOptimizationReducer,
RequiredOptimizationReducer,
ValueNumberingReducer>::Run(temp_zone);
ValueNumberingReducer>::Run<true>(temp_zone);

CopyingPhase<VariableReducer, LateLoadEliminationReducer,
MachineOptimizationReducer, RequiredOptimizationReducer,
ValueNumberingReducer>::Run(temp_zone);
ValueNumberingReducer>::Run<true>(temp_zone);
}

void CsaLateEscapeAnalysisPhase::Run(Zone* temp_zone) {
CopyingPhase<VariableReducer, LateEscapeAnalysisReducer,
MachineOptimizationReducer, RequiredOptimizationReducer,
ValueNumberingReducer>::Run(temp_zone);
ValueNumberingReducer>::Run<true>(temp_zone);
}

void CsaBranchEliminationPhase::Run(Zone* temp_zone) {
CopyingPhase<VariableReducer, MachineOptimizationReducer,
BranchEliminationReducer, RequiredOptimizationReducer,
ValueNumberingReducer>::Run(temp_zone);
ValueNumberingReducer>::Run<true>(temp_zone);
}

void CsaOptimizePhase::Run(Zone* temp_zone) {
Expand All @@ -51,7 +51,7 @@ void CsaOptimizePhase::Run(Zone* temp_zone) {
CopyingPhase<VariableReducer, PretenuringPropagationReducer,
MachineOptimizationReducer, MemoryOptimizationReducer,
RequiredOptimizationReducer,
ValueNumberingReducer>::Run(temp_zone);
ValueNumberingReducer>::Run<true>(temp_zone);
}

} // namespace v8::internal::compiler::turboshaft
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace v8::internal::compiler::turboshaft {

void DebugFeatureLoweringPhase::Run(Zone* temp_zone) {
#ifdef V8_ENABLE_DEBUG_CODE
turboshaft::CopyingPhase<turboshaft::DebugFeatureLoweringReducer>::Run(
turboshaft::CopyingPhase<turboshaft::DebugFeatureLoweringReducer>::Run<false>(
temp_zone);
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion deps/v8/src/compiler/turboshaft/int64-lowering-phase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void Int64LoweringPhase::Run(Zone* temp_zone) {
#if V8_TARGET_ARCH_32_BIT
turboshaft::CopyingPhase<
turboshaft::Int64LoweringReducer, turboshaft::VariableReducer,
turboshaft::RequiredOptimizationReducer>::Run(temp_zone);
turboshaft::RequiredOptimizationReducer>::Run<true>(temp_zone);
#else
UNREACHABLE();
#endif
Expand Down
2 changes: 1 addition & 1 deletion deps/v8/src/compiler/turboshaft/loop-peeling-phase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void LoopPeelingPhase::Run(Zone* temp_zone) {
turboshaft::VariableReducer,
turboshaft::MachineOptimizationReducer,
turboshaft::RequiredOptimizationReducer,
turboshaft::ValueNumberingReducer>::Run(temp_zone);
turboshaft::ValueNumberingReducer>::Run<true>(temp_zone);
}

} // namespace v8::internal::compiler::turboshaft
2 changes: 1 addition & 1 deletion deps/v8/src/compiler/turboshaft/loop-unrolling-phase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void LoopUnrollingPhase::Run(Zone* temp_zone) {
turboshaft::VariableReducer,
turboshaft::MachineOptimizationReducer,
turboshaft::RequiredOptimizationReducer,
turboshaft::ValueNumberingReducer>::Run(temp_zone);
turboshaft::ValueNumberingReducer>::Run<true>(temp_zone);
PipelineData::Get().clear_loop_unrolling_analyzer();
}
}
Expand Down
4 changes: 2 additions & 2 deletions deps/v8/src/compiler/turboshaft/machine-lowering-phase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ void MachineLoweringPhase::Run(Zone* temp_zone) {
CopyingPhase<DataViewReducer, VariableReducer, MachineLoweringReducer,
FastApiCallReducer, RequiredOptimizationReducer,
SelectLoweringReducer,
MachineOptimizationReducer>::Run(temp_zone);
MachineOptimizationReducer>::Run<true>(temp_zone);
} else {
CopyingPhase<DataViewReducer, VariableReducer, MachineLoweringReducer,
FastApiCallReducer, RequiredOptimizationReducer,
SelectLoweringReducer>::Run(temp_zone);
SelectLoweringReducer>::Run<true>(temp_zone);
}
}

Expand Down
50 changes: 10 additions & 40 deletions deps/v8/src/compiler/turboshaft/machine-optimization-reducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1333,53 +1333,23 @@ class MachineOptimizationReducer : public Next {
if (matcher.MatchConstantShiftRightArithmeticShiftOutZeros(
left, &x, rep_w, &k1) &&
matcher.MatchIntegralWordConstant(right, rep_w, &k2) &&
CountLeadingSignBits(k2, rep_w) > k1) {
if (matcher.Get(left).saturated_use_count.IsZero()) {
return __ Comparison(
x, __ WordConstant(base::bits::Unsigned(k2) << k1, rep_w), kind,
rep_w);
} else if constexpr (reducer_list_contains<
ReducerList, ValueNumberingReducer>::value) {
// If the shift has uses, we only apply the transformation if the
// result would be GVNed away.
OpIndex rhs =
__ WordConstant(base::bits::Unsigned(k2) << k1, rep_w);
static_assert(ComparisonOp::input_count == 2);
static_assert(sizeof(ComparisonOp) == 8);
base::SmallVector<OperationStorageSlot, 32> storage;
ComparisonOp* cmp =
CreateOperation<ComparisonOp>(storage, x, rhs, kind, rep_w);
if (__ WillGVNOp(*cmp)) {
return __ Comparison(x, rhs, kind, rep_w);
}
}
CountLeadingSignBits(k2, rep_w) > k1 &&
matcher.Get(left).saturated_use_count.IsZero()) {
return __ Comparison(
x, __ WordConstant(base::bits::Unsigned(k2) << k1, rep_w), kind,
rep_w);
}
// k2 </<= (x >> k1) => (k2 << k1) </<= x if shifts reversible
// Only perform the transformation if the shift is not used yet, to
// avoid keeping both the shift and x alive.
if (matcher.MatchConstantShiftRightArithmeticShiftOutZeros(
right, &x, rep_w, &k1) &&
matcher.MatchIntegralWordConstant(left, rep_w, &k2) &&
CountLeadingSignBits(k2, rep_w) > k1) {
if (matcher.Get(right).saturated_use_count.IsZero()) {
return __ Comparison(
__ WordConstant(base::bits::Unsigned(k2) << k1, rep_w), x, kind,
rep_w);
} else if constexpr (reducer_list_contains<
ReducerList, ValueNumberingReducer>::value) {
// If the shift has uses, we only apply the transformation if the
// result would be GVNed away.
OpIndex lhs =
__ WordConstant(base::bits::Unsigned(k2) << k1, rep_w);
static_assert(ComparisonOp::input_count == 2);
static_assert(sizeof(ComparisonOp) == 8);
base::SmallVector<OperationStorageSlot, 32> storage;
ComparisonOp* cmp =
CreateOperation<ComparisonOp>(storage, lhs, x, kind, rep_w);
if (__ WillGVNOp(*cmp)) {
return __ Comparison(lhs, x, kind, rep_w);
}
}
CountLeadingSignBits(k2, rep_w) > k1 &&
matcher.Get(right).saturated_use_count.IsZero()) {
return __ Comparison(
__ WordConstant(base::bits::Unsigned(k2) << k1, rep_w), x, kind,
rep_w);
}
}
// Map 64bit to 32bit comparisons.
Expand Down
2 changes: 1 addition & 1 deletion deps/v8/src/compiler/turboshaft/optimize-phase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void OptimizePhase::Run(Zone* temp_zone) {
turboshaft::MemoryOptimizationReducer,
turboshaft::MachineOptimizationReducer,
turboshaft::RequiredOptimizationReducer,
turboshaft::ValueNumberingReducer>::Run(temp_zone);
turboshaft::ValueNumberingReducer>::Run<true>(temp_zone);
}

} // namespace v8::internal::compiler::turboshaft
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace v8::internal::compiler::turboshaft {

void SimplifiedLoweringPhase::Run(Zone* temp_zone) {
CopyingPhase<SimplifiedLoweringReducer>::Run(temp_zone);
CopyingPhase<SimplifiedLoweringReducer, VariableReducerHotfix>::Run<false>(temp_zone);
}

} // namespace v8::internal::compiler::turboshaft
14 changes: 7 additions & 7 deletions deps/v8/src/compiler/turboshaft/simplified-lowering-reducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ class SimplifiedLoweringReducer : public Next {
OpIndex ig_index, const SpeculativeNumberBinopOp& op) {
DCHECK_EQ(op.kind, SpeculativeNumberBinopOp::Kind::kSafeIntegerAdd);

OpIndex frame_state = Map(op.frame_state());
V<Word32> left = ProcessInput(Map(op.left()), Rep::Word32(),
OpIndex frame_state = MapImpl(op.frame_state());
V<Word32> left = ProcessInput(MapImpl(op.left()), Rep::Word32(),
CheckKind::kSigned32, frame_state);
V<Word32> right = ProcessInput(Map(op.right()), Rep::Word32(),
V<Word32> right = ProcessInput(MapImpl(op.right()), Rep::Word32(),
CheckKind::kSigned32, frame_state);

V<Word32> result = __ OverflowCheckedBinop(
left, right, OverflowCheckedBinopOp::Kind::kSignedAdd,
WordRepresentation::Word32());

V<Word32> overflow = __ Projection(result, 1, Rep::Word32());
__ DeoptimizeIf(overflow, Map(op.frame_state()),
__ DeoptimizeIf(overflow, MapImpl(op.frame_state()),
DeoptimizeReason::kOverflow, FeedbackSource{});
return __ Projection(result, 0, Rep::Word32());
}
Expand All @@ -52,10 +52,10 @@ class SimplifiedLoweringReducer : public Next {
base::SmallVector<OpIndex, 8> return_values;
for (OpIndex input : ret.return_values()) {
return_values.push_back(
ProcessInput(Map(input), Rep::Tagged(), CheckKind::kNone, {}));
ProcessInput(MapImpl(input), Rep::Tagged(), CheckKind::kNone, {}));
}

__ Return(Map(ret.pop_count()), base::VectorOf(return_values));
__ Return(MapImpl(ret.pop_count()), base::VectorOf(return_values));
return OpIndex::Invalid();
}

Expand Down Expand Up @@ -94,7 +94,7 @@ class SimplifiedLoweringReducer : public Next {
}
}

inline OpIndex Map(OpIndex ig_index) { return __ MapToNewGraph(ig_index); }
inline OpIndex MapImpl(OpIndex ig_index) { return __ MapToNewGraph(ig_index); }
};

#include "src/compiler/turboshaft/undef-assembler-macros.inc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void StoreStoreEliminationPhase::Run(Zone* temp_zone) {
turboshaft::MachineOptimizationReducer,
turboshaft::RequiredOptimizationReducer,
turboshaft::BranchEliminationReducer,
turboshaft::ValueNumberingReducer>::Run(temp_zone);
turboshaft::ValueNumberingReducer>::Run<true>(temp_zone);
}

} // namespace v8::internal::compiler::turboshaft
5 changes: 3 additions & 2 deletions deps/v8/src/compiler/turboshaft/type-assertions-phase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ void TypeAssertionsPhase::Run(Zone* temp_zone) {
turboshaft::TypeInferenceReducerArgs::OutputGraphTyping::
kPreserveFromInputGraph};

turboshaft::CopyingPhase<turboshaft::AssertTypesReducer,
turboshaft::CopyingPhase<turboshaft::VariableReducerHotfix,
turboshaft::AssertTypesReducer,
turboshaft::ValueNumberingReducer,
turboshaft::TypeInferenceReducer>::Run(temp_zone);
turboshaft::TypeInferenceReducer>::Run<false>(temp_zone);
}

} // namespace v8::internal::compiler::turboshaft
3 changes: 2 additions & 1 deletion deps/v8/src/compiler/turboshaft/typed-optimizations-phase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ void TypedOptimizationsPhase::Run(Zone* temp_zone) {
turboshaft::TypeInferenceReducerArgs::OutputGraphTyping::kNone};

turboshaft::CopyingPhase<turboshaft::TypedOptimizationsReducer,
turboshaft::TypeInferenceReducer>::Run(temp_zone);
turboshaft::VariableReducerHotfix,
turboshaft::TypeInferenceReducer>::Run<false>(temp_zone);
}

} // namespace v8::internal::compiler::turboshaft
Loading

0 comments on commit a05132d

Please sign in to comment.