Skip to content

Commit c51041f

Browse files
schuayCommit Bot
authored andcommitted
[nci] Replace CompilationTarget with a new Code::Kind value
With the new Turbofan variants (NCI and Turboprop), we need a way to distinguish between them both during and after compilation. We initially introduced CompilationTarget to track the variant during compilation, but decided to reuse the code kind as the canonical spot to store this information instead. Why? Because it is an established mechanism, already available in most of the necessary spots (inside the pipeline, on Code objects, in profiling traces). This CL removes CompilationTarget and adds a new NATIVE_CONTEXT_INDEPENDENT kind, plus helper functions to determine various things about a given code kind (e.g.: does this code kind deopt?). As a (very large) drive-by, refactor both Code::Kind and AbstractCode::Kind into a new CodeKind enum class. Bug: v8:8888 Change-Id: Ie858b9a53311b0731630be35cf5cd108dee95b39 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2336793 Commit-Queue: Jakob Gruber <[email protected]> Reviewed-by: Clemens Backes <[email protected]> Reviewed-by: Ross McIlroy <[email protected]> Reviewed-by: Dominik Inführ <[email protected]> Reviewed-by: Georg Neis <[email protected]> Cr-Commit-Position: refs/heads/master@{#69244}
1 parent c365959 commit c51041f

105 files changed

Lines changed: 1165 additions & 857 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,6 +2768,8 @@ v8_source_set("v8_base_without_compiler") {
27682768
"src/objects/cell-inl.h",
27692769
"src/objects/cell.h",
27702770
"src/objects/code-inl.h",
2771+
"src/objects/code-kind.cc",
2772+
"src/objects/code-kind.h",
27712773
"src/objects/code.cc",
27722774
"src/objects/code.h",
27732775
"src/objects/compilation-cache-inl.h",

src/builtins/builtins.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ Handle<Code> Builtins::GenerateOffHeapTrampolineFor(
360360
? TrampolineType::kJump
361361
: TrampolineType::kAbort);
362362

363-
return Factory::CodeBuilder(isolate, desc, Code::BUILTIN)
363+
return Factory::CodeBuilder(isolate, desc, CodeKind::BUILTIN)
364364
.set_read_only_data_container(kind_specfic_flags)
365365
.set_self_reference(generator.CodeObject())
366366
.set_is_executable(generate_jump_to_instruction_stream)

src/builtins/constants-table-builder.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void BuiltinsConstantsTableBuilder::Finalize() {
118118
for (auto it = it_scope.begin(); it != it_scope.end(); ++it) {
119119
uint32_t index = *it.entry();
120120
Object value = it.key();
121-
if (value.IsCode() && Code::cast(value).kind() == Code::BUILTIN) {
121+
if (value.IsCode() && Code::cast(value).kind() == CodeKind::BUILTIN) {
122122
// Replace placeholder code objects with the real builtin.
123123
// See also: SetupIsolateDelegate::PopulateWithPlaceholders.
124124
// TODO(jgruber): Deduplicate placeholders and their corresponding

src/builtins/setup-builtins-internal.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Handle<Code> BuildPlaceholder(Isolate* isolate, int32_t builtin_index) {
7575
}
7676
CodeDesc desc;
7777
masm.GetCode(isolate, &desc);
78-
Handle<Code> code = Factory::CodeBuilder(isolate, desc, Code::BUILTIN)
78+
Handle<Code> code = Factory::CodeBuilder(isolate, desc, CodeKind::BUILTIN)
7979
.set_self_reference(masm.CodeObject())
8080
.set_builtin_index(builtin_index)
8181
.Build();
@@ -116,7 +116,7 @@ Code BuildWithMacroAssembler(Isolate* isolate, int32_t builtin_index,
116116
masm.GetCode(isolate, &desc, MacroAssembler::kNoSafepointTable,
117117
handler_table_offset);
118118

119-
Handle<Code> code = Factory::CodeBuilder(isolate, desc, Code::BUILTIN)
119+
Handle<Code> code = Factory::CodeBuilder(isolate, desc, CodeKind::BUILTIN)
120120
.set_self_reference(masm.CodeObject())
121121
.set_builtin_index(builtin_index)
122122
.Build();
@@ -142,7 +142,7 @@ Code BuildAdaptor(Isolate* isolate, int32_t builtin_index,
142142
Builtins::Generate_Adaptor(&masm, builtin_address);
143143
CodeDesc desc;
144144
masm.GetCode(isolate, &desc);
145-
Handle<Code> code = Factory::CodeBuilder(isolate, desc, Code::BUILTIN)
145+
Handle<Code> code = Factory::CodeBuilder(isolate, desc, CodeKind::BUILTIN)
146146
.set_self_reference(masm.CodeObject())
147147
.set_builtin_index(builtin_index)
148148
.Build();
@@ -162,7 +162,7 @@ Code BuildWithCodeStubAssemblerJS(Isolate* isolate, int32_t builtin_index,
162162
const int argc_with_recv =
163163
(argc == kDontAdaptArgumentsSentinel) ? 0 : argc + 1;
164164
compiler::CodeAssemblerState state(
165-
isolate, &zone, argc_with_recv, Code::BUILTIN, name,
165+
isolate, &zone, argc_with_recv, CodeKind::BUILTIN, name,
166166
PoisoningMitigationLevel::kDontPoison, builtin_index);
167167
generator(&state);
168168
Handle<Code> code = compiler::CodeAssembler::GenerateCode(
@@ -187,7 +187,7 @@ Code BuildWithCodeStubAssemblerCS(Isolate* isolate, int32_t builtin_index,
187187
// Ensure descriptor is already initialized.
188188
DCHECK_LE(0, descriptor.GetRegisterParameterCount());
189189
compiler::CodeAssemblerState state(
190-
isolate, &zone, descriptor, Code::BUILTIN, name,
190+
isolate, &zone, descriptor, CodeKind::BUILTIN, name,
191191
PoisoningMitigationLevel::kDontPoison, builtin_index);
192192
generator(&state);
193193
Handle<Code> code = compiler::CodeAssembler::GenerateCode(

src/codegen/compiler.cc

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,13 @@ namespace internal {
5858

5959
namespace {
6060

61-
const char* CompilationTargetName(CompilationTarget target) {
62-
switch (target) {
63-
case CompilationTarget::kTurbofan:
64-
return "Turbofan";
65-
case CompilationTarget::kNativeContextIndependent:
66-
return "NativeContextIndependent";
67-
}
68-
UNREACHABLE();
69-
}
70-
71-
CompilationTarget CompilationTargetOf(OptimizedCompilationInfo* info) {
72-
return info->native_context_independent()
73-
? CompilationTarget::kNativeContextIndependent
74-
: CompilationTarget::kTurbofan;
75-
}
76-
77-
bool IsNativeContextIndependent(CompilationTarget target) {
78-
return target == CompilationTarget::kNativeContextIndependent;
79-
}
80-
81-
bool IsForNativeContextIndependentCachingOnly(CompilationTarget target) {
82-
return IsNativeContextIndependent(target) && !FLAG_turbo_nci_as_highest_tier;
61+
bool IsForNativeContextIndependentCachingOnly(CodeKind kind) {
62+
return CodeKindIsNativeContextIndependentJSFunction(kind) &&
63+
!FLAG_turbo_nci_as_highest_tier;
8364
}
8465

8566
bool IsForNativeContextIndependentCachingOnly(OptimizedCompilationInfo* info) {
86-
return IsForNativeContextIndependentCachingOnly(CompilationTargetOf(info));
67+
return IsForNativeContextIndependentCachingOnly(info->code_kind());
8768
}
8869

8970
class CompilerTracer : public AllStatic {
@@ -93,8 +74,7 @@ class CompilerTracer : public AllStatic {
9374
OptimizedCompilationInfo* info) {
9475
PrintF(scope.file(), "[%s ", header);
9576
info->closure()->ShortPrint(scope.file());
96-
PrintF(scope.file(), " (target %s)",
97-
CompilationTargetName(CompilationTargetOf(info)));
77+
PrintF(scope.file(), " (target %s)", CodeKindToString(info->code_kind()));
9878
}
9979

10080
static void PrintTracePrefix(const CodeTracer::Scope& scope,
@@ -893,8 +873,7 @@ void InsertCodeIntoOptimizedCodeCache(
893873
// Cached NCI code currently does not use the optimization marker field.
894874
if (IsForNativeContextIndependentCachingOnly(compilation_info)) return;
895875

896-
Handle<Code> code = compilation_info->code();
897-
if (code->kind() != Code::OPTIMIZED_FUNCTION) return; // Nothing to do.
876+
if (!CodeKindIsOptimizedJSFunction(compilation_info->code_kind())) return;
898877

899878
// Function context specialization folds-in the function context,
900879
// so no sharing can occur.
@@ -906,6 +885,7 @@ void InsertCodeIntoOptimizedCodeCache(
906885
}
907886

908887
// Cache optimized context-specific code.
888+
Handle<Code> code = compilation_info->code();
909889
Handle<JSFunction> function = compilation_info->closure();
910890
Handle<SharedFunctionInfo> shared(function->shared(), function->GetIsolate());
911891
Handle<NativeContext> native_context(function->context().native_context(),
@@ -922,15 +902,14 @@ void InsertCodeIntoOptimizedCodeCache(
922902

923903
void InsertCodeIntoCompilationCache(Isolate* isolate,
924904
OptimizedCompilationInfo* info) {
925-
if (!info->native_context_independent()) return;
905+
if (!CodeKindIsNativeContextIndependentJSFunction(info->code_kind())) return;
926906

927907
// TODO(jgruber,v8:8888): This should turn into a DCHECK once we
928908
// spawn dedicated NCI compile tasks.
929909
if (!info->osr_offset().IsNone()) return;
930910

931911
Handle<Code> code = info->code();
932912
DCHECK(!info->function_context_specializing());
933-
DCHECK_EQ(code->kind(), Code::OPTIMIZED_FUNCTION);
934913

935914
Handle<SharedFunctionInfo> sfi = info->shared_info();
936915
CompilationCache* cache = isolate->compilation_cache();
@@ -1004,10 +983,11 @@ bool GetOptimizedCodeLater(OptimizedCompilationJob* job, Isolate* isolate) {
1004983
}
1005984

1006985
MaybeHandle<Code> GetOptimizedCode(Handle<JSFunction> function,
1007-
ConcurrencyMode mode,
1008-
CompilationTarget compilation_target,
986+
ConcurrencyMode mode, CodeKind code_kind,
1009987
BailoutId osr_offset = BailoutId::None(),
1010988
JavaScriptFrame* osr_frame = nullptr) {
989+
DCHECK(CodeKindIsOptimizedJSFunction(code_kind));
990+
1011991
Isolate* isolate = function->GetIsolate();
1012992
Handle<SharedFunctionInfo> shared(function->shared(), isolate);
1013993

@@ -1038,7 +1018,7 @@ MaybeHandle<Code> GetOptimizedCode(Handle<JSFunction> function,
10381018
PendingOptimizationTable::FunctionWasOptimized(isolate, function);
10391019
}
10401020

1041-
if (!IsForNativeContextIndependentCachingOnly(compilation_target)) {
1021+
if (!IsForNativeContextIndependentCachingOnly(code_kind)) {
10421022
Handle<Code> cached_code;
10431023
if (GetCodeFromOptimizedCodeCache(function, osr_offset)
10441024
.ToHandle(&cached_code)) {
@@ -1051,7 +1031,7 @@ MaybeHandle<Code> GetOptimizedCode(Handle<JSFunction> function,
10511031
DCHECK(shared->is_compiled());
10521032
function->feedback_vector().set_profiler_ticks(0);
10531033

1054-
if (IsNativeContextIndependent(compilation_target)) {
1034+
if (CodeKindIsNativeContextIndependentJSFunction(code_kind)) {
10551035
// We don't generate NCI code for OSR.
10561036
DCHECK_EQ(osr_offset, BailoutId::None());
10571037
// Don't generate NCI code when we've already done so in the past.
@@ -1074,9 +1054,8 @@ MaybeHandle<Code> GetOptimizedCode(Handle<JSFunction> function,
10741054
// tolerate the lack of a script without bytecode.
10751055
DCHECK_IMPLIES(!has_script, shared->HasBytecodeArray());
10761056
std::unique_ptr<OptimizedCompilationJob> job(
1077-
compiler::Pipeline::NewCompilationJob(
1078-
isolate, function, has_script, osr_offset, osr_frame,
1079-
IsNativeContextIndependent(compilation_target)));
1057+
compiler::Pipeline::NewCompilationJob(isolate, function, code_kind,
1058+
has_script, osr_offset, osr_frame));
10801059
OptimizedCompilationInfo* compilation_info = job->compilation_info();
10811060

10821061
// In case of concurrent recompilation, all handles below this point will be
@@ -1098,7 +1077,7 @@ MaybeHandle<Code> GetOptimizedCode(Handle<JSFunction> function,
10981077
job.release(); // The background recompile job owns this now.
10991078

11001079
// Set the optimization marker and return a code object which checks it.
1101-
if (!IsForNativeContextIndependentCachingOnly(compilation_target)) {
1080+
if (!IsForNativeContextIndependentCachingOnly(code_kind)) {
11021081
// Cached NCI code currently does not use the optimization marker field.
11031082
function->SetOptimizationMarker(
11041083
OptimizationMarker::kInOptimizationQueue);
@@ -1778,7 +1757,7 @@ bool Compiler::Compile(Handle<JSFunction> function, ClearExceptionFlag flag,
17781757

17791758
Handle<Code> maybe_code;
17801759
if (GetOptimizedCode(function, ConcurrencyMode::kNotConcurrent,
1781-
DefaultCompilationTarget())
1760+
CodeKindForTopTier())
17821761
.ToHandle(&maybe_code)) {
17831762
code = maybe_code;
17841763
}
@@ -1839,15 +1818,16 @@ bool Compiler::FinalizeBackgroundCompileTask(
18391818

18401819
// static
18411820
bool Compiler::CompileOptimized(Handle<JSFunction> function,
1842-
ConcurrencyMode mode,
1843-
CompilationTarget target) {
1821+
ConcurrencyMode mode, CodeKind code_kind) {
1822+
DCHECK(CodeKindIsOptimizedJSFunction(code_kind));
1823+
18441824
if (function->IsOptimized()) return true;
18451825

18461826
Isolate* isolate = function->GetIsolate();
18471827
DCHECK(AllowCompilation::IsAllowed(isolate));
18481828

18491829
Handle<Code> code;
1850-
if (!GetOptimizedCode(function, mode, target).ToHandle(&code)) {
1830+
if (!GetOptimizedCode(function, mode, code_kind).ToHandle(&code)) {
18511831
// Optimization failed, get unoptimized code. Unoptimized code must exist
18521832
// already if we are optimizing.
18531833
DCHECK(!isolate->has_pending_exception());
@@ -1856,7 +1836,7 @@ bool Compiler::CompileOptimized(Handle<JSFunction> function,
18561836
code = BUILTIN_CODE(isolate, InterpreterEntryTrampoline);
18571837
}
18581838

1859-
if (!IsForNativeContextIndependentCachingOnly(target)) {
1839+
if (!IsForNativeContextIndependentCachingOnly(code_kind)) {
18601840
function->set_code(*code);
18611841
}
18621842

@@ -2913,7 +2893,7 @@ MaybeHandle<Code> Compiler::GetOptimizedCodeForOSR(Handle<JSFunction> function,
29132893
DCHECK(!osr_offset.IsNone());
29142894
DCHECK_NOT_NULL(osr_frame);
29152895
return GetOptimizedCode(function, ConcurrencyMode::kNotConcurrent,
2916-
CompilationTarget::kTurbofan, osr_offset, osr_frame);
2896+
CodeKindForOSR(), osr_offset, osr_frame);
29172897
}
29182898

29192899
// static

src/codegen/compiler.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@ class WorkerThreadRuntimeCallStats;
4343
using UnoptimizedCompilationJobList =
4444
std::forward_list<std::unique_ptr<UnoptimizedCompilationJob>>;
4545

46-
enum class CompilationTarget : uint8_t {
47-
kTurbofan,
48-
kNativeContextIndependent,
49-
};
50-
51-
inline CompilationTarget DefaultCompilationTarget() {
52-
return FLAG_turbo_nci_as_highest_tier
53-
? CompilationTarget::kNativeContextIndependent
54-
: CompilationTarget::kTurbofan;
55-
}
56-
5746
inline bool ShouldSpawnExtraNativeContextIndependentCompilationJob() {
5847
return FLAG_turbo_nci && !FLAG_turbo_nci_as_highest_tier;
5948
}
@@ -84,7 +73,7 @@ class V8_EXPORT_PRIVATE Compiler : public AllStatic {
8473
static bool Compile(Handle<JSFunction> function, ClearExceptionFlag flag,
8574
IsCompiledScope* is_compiled_scope);
8675
static bool CompileOptimized(Handle<JSFunction> function,
87-
ConcurrencyMode mode, CompilationTarget target);
76+
ConcurrencyMode mode, CodeKind code_kind);
8877

8978
// Collect source positions for a function that has already been compiled to
9079
// bytecode, but for which source positions were not collected (e.g. because

0 commit comments

Comments
 (0)