From 60d7c8aa142d5653997ac6f663f5cf82779877cf Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 12 Mar 2022 02:35:07 +0100 Subject: [PATCH] Add timings. --- src/compiler/compiler.c | 42 ++++++++++++++++++++++++++++++-- src/compiler/compiler.h | 7 ++++++ src/compiler/semantic_analyser.c | 3 +++ src/main.c | 3 +++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 4e2263c35..3e1b5fd2c 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -5,6 +5,8 @@ #include "compiler_internal.h" #ifndef _MSC_VER #include +#include + #endif #define MAX_OUTPUT_FILES 1000000 @@ -16,11 +18,22 @@ Vmem ast_arena; Vmem expr_arena; Vmem decl_arena; Vmem type_info_arena; - - +double compiler_init_time; +double compiler_parsing_time; +double compiler_sema_time; +double compiler_ir_gen_time; +double compiler_codegen_time; +double compiler_link_time; void compiler_init(const char *std_lib_dir) { + compiler_init_time = -1; + compiler_parsing_time = -1; + compiler_sema_time = -1; + compiler_ir_gen_time = -1; + compiler_codegen_time = -1; + compiler_link_time = -1; + DEBUG_LOG("Version: %s", COMPILER_VERSION); global_context = (GlobalContext ){ .in_panic_mode = false }; @@ -155,6 +168,20 @@ static void free_arenas(void) if (debug_stats) print_arena_status(); } +static void compiler_print_bench(void) +{ + if (debug_stats) + { + printf("Timings\n"); + printf("-------\n"); + if (compiler_init_time >= 0) printf("Initialization took: %.4f ms\n", compiler_init_time * 1000); + if (compiler_parsing_time >= 0) printf("Parsing took: %.4f ms\n", (compiler_parsing_time - compiler_init_time) * 1000); + if (compiler_sema_time >= 0) printf("Analysis took: %.4f ms\n", (compiler_sema_time - compiler_parsing_time) * 1000); + if (compiler_ir_gen_time >= 0) printf("Ir gen took: %.4f ms\n", (compiler_ir_gen_time - compiler_sema_time) * 1000); + if (compiler_codegen_time >= 0) printf("Codegen took: %.4f ms\n", (compiler_codegen_time - compiler_ir_gen_time) * 1000); + if (compiler_link_time >= 0) printf("Linking took: %.4f ms\n", (compiler_link_time - compiler_codegen_time) * 1000); + } +} void compiler_compile(void) { @@ -213,6 +240,7 @@ void compiler_compile(void) default: UNREACHABLE } + compiler_ir_gen_time = bench_mark(); free_arenas(); @@ -273,6 +301,7 @@ void compiler_compile(void) output_file_count += cfiles; free(compile_data); + compiler_codegen_time = bench_mark(); if (create_exe) { @@ -280,9 +309,12 @@ void compiler_compile(void) if (active_target.arch_os_target == default_target) { platform_linker(output_name, obj_files, output_file_count); + compiler_link_time = bench_mark(); + compiler_print_bench(); } else { + compiler_print_bench(); if (!obj_format_linking_supported(platform_target.object_format) || !linker(output_name, obj_files, output_file_count)) { @@ -290,6 +322,7 @@ void compiler_compile(void) active_target.run_after_compile = false; } } + if (active_target.run_after_compile) { printf("Launching %s...\n", output_name); @@ -482,15 +515,20 @@ void compile() type_init_cint(); + compiler_init_time = bench_mark(); + if (!vec_size(active_target.sources)) error_exit("No files to compile."); if (active_target.lex_only) { compiler_lex(); + compiler_parsing_time = bench_mark(); + return; } if (active_target.parse_only) { compiler_parse(); + compiler_parsing_time = bench_mark(); return; } compiler_compile(); diff --git a/src/compiler/compiler.h b/src/compiler/compiler.h index 7dc6f6988..ea2ec342e 100644 --- a/src/compiler/compiler.h +++ b/src/compiler/compiler.h @@ -15,3 +15,10 @@ void init_default_build_target(BuildTarget *target, BuildOptions *options); void symtab_init(uint32_t max_size); void symtab_destroy(); void print_syntax(BuildOptions *options); + +extern double compiler_init_time; +extern double compiler_parsing_time; +extern double compiler_sema_time; +extern double compiler_ir_gen_time; +extern double compiler_codegen_time; +extern double compiler_link_time; \ No newline at end of file diff --git a/src/compiler/semantic_analyser.c b/src/compiler/semantic_analyser.c index 23ea2fa88..5c0a6dba8 100644 --- a/src/compiler/semantic_analyser.c +++ b/src/compiler/semantic_analyser.c @@ -2,6 +2,7 @@ // Use of this source code is governed by the GNU LGPLv3.0 license // a copy of which can be found in the LICENSE file. +#include #include "sema_internal.h" void sema_shadow_error(Decl *decl, Decl *old) @@ -286,6 +287,7 @@ void sema_analysis_run(void) if (!parse_file(file)) has_error = true; } if (has_error) exit_compiler(EXIT_FAILURE); + compiler_parsing_time = bench_mark(); // All global defines are added to the std module global_context.std_module_path = (Path) { .module = kw_std, .span = INVALID_SPAN, .len = (uint32_t) strlen(kw_std) }; @@ -316,6 +318,7 @@ void sema_analysis_run(void) { sema_analyze_to_stage(stage); } + compiler_sema_time = bench_mark(); } diff --git a/src/main.c b/src/main.c index c10f93d06..fd66e697b 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include #include "compiler/compiler.h" #include "build/build_options.h" #include "build/project_creation.h" @@ -24,6 +25,8 @@ static void cleanup() int main_real(int argc, const char *argv[]) { + bench_begin(); + int result = setjmp(on_error_jump); if (result) {