Flutter Engine
The Flutter Engine
Static Public Member Functions | Static Public Attributes | List of all members
dart::Compiler Class Reference

#include <compiler.h>

Inheritance diagram for dart::Compiler:
dart::AllStatic

Static Public Member Functions

static bool IsBackgroundCompilation ()
 
static bool CanOptimizeFunction (Thread *thread, const Function &function)
 
static ObjectPtr CompileFunction (Thread *thread, const Function &function)
 
static ErrorPtr EnsureUnoptimizedCode (Thread *thread, const Function &function)
 
static ObjectPtr CompileOptimizedFunction (Thread *thread, const Function &function, intptr_t osr_id=kNoOSRDeoptId)
 
static void ComputeLocalVarDescriptors (const Code &code)
 
static ErrorPtr CompileAllFunctions (const Class &cls)
 
static void AbortBackgroundCompilation (intptr_t deopt_id, const char *msg)
 

Static Public Attributes

static constexpr intptr_t kNoOSRDeoptId = DeoptId::kNone
 

Detailed Description

Definition at line 71 of file compiler.h.

Member Function Documentation

◆ AbortBackgroundCompilation()

void dart::Compiler::AbortBackgroundCompilation ( intptr_t  deopt_id,
const char *  msg 
)
static

Definition at line 971 of file compiler.cc.

971 {
972 if (FLAG_trace_compiler) {
973 THR_Print("ABORT background compilation: %s\n", msg);
974 }
975#if !defined(PRODUCT)
976 TimelineStream* stream = Timeline::GetCompilerStream();
977 ASSERT(stream != nullptr);
978 TimelineEvent* event = stream->StartEvent();
979 if (event != nullptr) {
980 event->Instant("AbortBackgroundCompilation");
981 event->SetNumArguments(1);
982 event->CopyArgument(0, "reason", msg);
983 event->Complete();
984 }
985#endif // !defined(PRODUCT)
988 deopt_id, Object::background_compilation_error());
989}
static bool IsBackgroundCompilation()
Definition: compiler.cc:298
DART_NORETURN void Jump(int value, const Error &error)
Definition: longjump.cc:22
LongJumpScope * long_jump_base() const
Definition: thread_state.h:47
static Thread * Current()
Definition: thread.h:362
#define THR_Print(format,...)
Definition: log.h:20
#define ASSERT(E)
FlKeyEvent * event

◆ CanOptimizeFunction()

bool dart::Compiler::CanOptimizeFunction ( Thread thread,
const Function function 
)
static

Definition at line 229 of file compiler.cc.

229 {
230#if !defined(PRODUCT)
231 if (thread->isolate_group()->debugger()->IsDebugging(thread, function)) {
232 // We cannot set breakpoints and single step in optimized code,
233 // so do not optimize the function. Bump usage counter down to avoid
234 // repeatedly entering the runtime for an optimization attempt.
235 function.SetUsageCounter(0);
236
237 // If the optimization counter = 1, the unoptimized code will come back here
238 // immediately, causing an infinite compilation loop. The compiler raises
239 // the threshold for functions with breakpoints, so we drop the unoptimized
240 // to force it to be recompiled.
241 if (thread->isolate_group()->optimization_counter_threshold() < 2) {
242 function.ClearCode();
243 }
244 return false;
245 }
246#endif
247 if (function.deoptimization_counter() >=
248 FLAG_max_deoptimization_counter_threshold) {
249 if (FLAG_trace_failed_optimization_attempts ||
250 FLAG_stop_on_excessive_deoptimization) {
251 THR_Print("Too many deoptimizations: %s\n",
252 function.ToFullyQualifiedCString());
253 if (FLAG_stop_on_excessive_deoptimization) {
254 FATAL("Stop on excessive deoptimization");
255 }
256 }
257 // The function will not be optimized any longer. This situation can occur
258 // mostly with small optimization counter thresholds.
259 function.SetIsOptimizable(false);
260 function.SetUsageCounter(INT32_MIN);
261 return false;
262 }
263 if (FLAG_optimization_filter != nullptr) {
264 // FLAG_optimization_filter is a comma-separated list of strings that are
265 // matched against the fully-qualified function name.
266 char* save_ptr; // Needed for strtok_r.
267 const char* function_name = function.ToFullyQualifiedCString();
268 intptr_t len = strlen(FLAG_optimization_filter) + 1; // Length with \0.
269 char* filter = new char[len];
270 strncpy(filter, FLAG_optimization_filter, len); // strtok modifies arg 1.
271 char* token = strtok_r(filter, ",", &save_ptr);
272 bool found = false;
273 while (token != nullptr) {
274 if (strstr(function_name, token) != nullptr) {
275 found = true;
276 break;
277 }
278 token = strtok_r(nullptr, ",", &save_ptr);
279 }
280 delete[] filter;
281 if (!found) {
282 function.SetUsageCounter(INT32_MIN);
283 return false;
284 }
285 }
286 if (!function.IsOptimizable()) {
287 // Huge methods (code size above --huge_method_cutoff_in_code_size) become
288 // non-optimizable only after the code has been generated.
289 if (FLAG_trace_failed_optimization_attempts) {
290 THR_Print("Not optimizable: %s\n", function.ToFullyQualifiedCString());
291 }
292 function.SetUsageCounter(INT32_MIN);
293 return false;
294 }
295 return true;
296}
#define FATAL(error)
Dart_NativeFunction function
Definition: fuchsia.cc:51
const char *const function_name

◆ CompileAllFunctions()

ErrorPtr dart::Compiler::CompileAllFunctions ( const Class cls)
static

Definition at line 948 of file compiler.cc.

948 {
949 Thread* thread = Thread::Current();
950 Zone* zone = thread->zone();
951 Object& result = Object::Handle(zone);
952 // We don't expect functions() to change as the class was finalized.
953 ASSERT(cls.is_finalized());
954 Array& functions = Array::Handle(zone, cls.current_functions());
955 Function& func = Function::Handle(zone);
956 // Compile all the regular functions.
957 for (int i = 0; i < functions.Length(); i++) {
958 func ^= functions.At(i);
959 ASSERT(!func.IsNull());
960 if (!func.HasCode() && !func.is_abstract()) {
961 result = CompileFunction(thread, func);
962 if (result.IsError()) {
963 return Error::Cast(result).ptr();
964 }
965 ASSERT(!result.IsNull());
966 }
967 }
968 return Error::null();
969}
static ObjectPtr CompileFunction(Thread *thread, const Function &function)
Definition: compiler.cc:824
static ObjectPtr null()
Definition: object.h:433
static Object & Handle()
Definition: object.h:407
GAsyncResult * result

◆ CompileFunction()

ObjectPtr dart::Compiler::CompileFunction ( Thread thread,
const Function function 
)
static

Definition at line 824 of file compiler.cc.

824 {
825#if defined(DART_PRECOMPILER) && !defined(TARGET_ARCH_IA32)
826 RELEASE_ASSERT(!FLAG_precompiled_mode);
827#endif
828
829#if defined(DART_PRECOMPILED_RUNTIME)
830 FATAL("Precompilation missed function %s (%s, %s)\n",
831 function.ToLibNamePrefixedQualifiedCString(),
832 function.token_pos().ToCString(),
834#endif // defined(DART_PRECOMPILED_RUNTIME)
835
836 VMTagScope tagScope(thread, VMTag::kCompileUnoptimizedTagId);
837#if defined(SUPPORT_TIMELINE)
838 const char* event_name;
840 event_name = "CompileFunctionUnoptimizedBackground";
841 } else {
842 event_name = "CompileFunction";
843 }
845#endif // defined(SUPPORT_TIMELINE)
846
847 CompilationPipeline* pipeline =
848 CompilationPipeline::New(thread->zone(), function);
849
850 const bool optimized = function.ForceOptimize();
851 return CompileFunctionHelper(pipeline, function, optimized, kNoOSRDeoptId);
852}
#define RELEASE_ASSERT(cond)
Definition: assert.h:327
static CompilationPipeline * New(Zone *zone, const Function &function)
Definition: compiler.cc:201
static constexpr intptr_t kNoOSRDeoptId
Definition: compiler.h:73
static const char * KindToCString(UntaggedFunction::Kind kind)
Definition: object.cc:8419
static ObjectPtr CompileFunctionHelper(CompilationPipeline *pipeline, const Function &function, volatile bool optimized, intptr_t osr_id)
Definition: compiler.cc:682
#define TIMELINE_FUNCTION_COMPILATION_DURATION(thread, name, function)
Definition: timeline.h:40

◆ CompileOptimizedFunction()

ObjectPtr dart::Compiler::CompileOptimizedFunction ( Thread thread,
const Function function,
intptr_t  osr_id = kNoOSRDeoptId 
)
static

Definition at line 886 of file compiler.cc.

888 {
889 VMTagScope tag_scope(thread, VMTag::kCompileOptimizedTagId);
890
891#if defined(SUPPORT_TIMELINE)
892 const char* event_name;
893 if (osr_id != kNoOSRDeoptId) {
894 event_name = "CompileFunctionOptimizedOSR";
895 } else if (IsBackgroundCompilation()) {
896 event_name = "CompileFunctionOptimizedBackground";
897 } else {
898 event_name = "CompileFunctionOptimized";
899 }
901#endif // defined(SUPPORT_TIMELINE)
902
903 CompilationPipeline* pipeline =
904 CompilationPipeline::New(thread->zone(), function);
905 return CompileFunctionHelper(pipeline, function, /* optimized = */ true,
906 osr_id);
907}

◆ ComputeLocalVarDescriptors()

void dart::Compiler::ComputeLocalVarDescriptors ( const Code code)
static

Definition at line 909 of file compiler.cc.

909 {
910 ASSERT(!code.is_optimized());
911 ASSERT(!FLAG_precompiled_mode);
912 const Function& function = Function::Handle(code.function());
913 ASSERT(code.var_descriptors() == Object::null());
914 // IsIrregexpFunction have eager var descriptors generation.
915 ASSERT(!function.IsIrregexpFunction());
916 // In background compilation, parser can produce 'errors": bailouts
917 // if state changed while compiling in background.
918 Thread* thread = Thread::Current();
919 Zone* zone = thread->zone();
920 CompilerState state(thread, /*is_aot=*/false, /*is_optimizing=*/false);
921 LongJumpScope jump;
922 if (setjmp(*jump.Set()) == 0) {
923 ParsedFunction* parsed_function =
924 new ParsedFunction(thread, Function::ZoneHandle(zone, function.ptr()));
925 ZoneGrowableArray<const ICData*>* ic_data_array =
926 new ZoneGrowableArray<const ICData*>();
927 ZoneGrowableArray<intptr_t>* context_level_array =
928 new ZoneGrowableArray<intptr_t>();
929
930 kernel::FlowGraphBuilder builder(
931 parsed_function, ic_data_array, context_level_array,
932 /* not inlining */ nullptr, false, Compiler::kNoOSRDeoptId);
933 builder.BuildGraph();
934
935 auto& var_descs = LocalVarDescriptors::Handle(zone);
936
937 var_descs = parsed_function->scope()->GetVarDescriptors(
938 function, context_level_array);
939
940 ASSERT(!var_descs.IsNull());
941 code.set_var_descriptors(var_descs);
942 } else {
943 // Only possible with background compilation.
945 }
946}
static Object & ZoneHandle()
Definition: object.h:419
AtkStateType state

◆ EnsureUnoptimizedCode()

ErrorPtr dart::Compiler::EnsureUnoptimizedCode ( Thread thread,
const Function function 
)
static

Definition at line 854 of file compiler.cc.

855 {
856 ASSERT(!function.ForceOptimize());
857 if (function.unoptimized_code() != Object::null()) {
858 return Error::null();
859 }
860 Code& original_code = Code::ZoneHandle(thread->zone());
861 if (function.HasCode()) {
862 original_code = function.CurrentCode();
863 }
864 CompilationPipeline* pipeline =
865 CompilationPipeline::New(thread->zone(), function);
866 const Object& result = Object::Handle(
867 CompileFunctionHelper(pipeline, function, false, /* not optimized */
869 if (result.IsError()) {
870 return Error::Cast(result).ptr();
871 }
872 // Since CompileFunctionHelper replaces the current code, re-attach the
873 // the original code if the function was already compiled.
874 if (!original_code.IsNull() && result.ptr() == function.CurrentCode() &&
875 !original_code.IsDisabled()) {
876 function.AttachCode(original_code);
877 }
878 ASSERT(function.unoptimized_code() != Object::null());
879 ASSERT(function.unoptimized_code() == result.ptr());
880 if (FLAG_trace_compiler) {
881 THR_Print("Ensure unoptimized code for %s\n", function.ToCString());
882 }
883 return Error::null();
884}

◆ IsBackgroundCompilation()

bool dart::Compiler::IsBackgroundCompilation ( )
static

Definition at line 298 of file compiler.cc.

298 {
299 // For now: compilation in non mutator thread is the background compilation.
301}
bool IsDartMutatorThread() const
Definition: thread.h:551

Member Data Documentation

◆ kNoOSRDeoptId

constexpr intptr_t dart::Compiler::kNoOSRDeoptId = DeoptId::kNone
staticconstexpr

Definition at line 73 of file compiler.h.


The documentation for this class was generated from the following files: