Commit fbe5737c84

Andrew Kelley <superjoe30@gmail.com>
2018-09-10 15:46:15
stage1: always optimize blake and softfloat even in debug mode
1 parent c0bdcc7
src/all_types.hpp
@@ -10,6 +10,7 @@
 
 #include "list.hpp"
 #include "buffer.hpp"
+#include "cache_hash.hpp"
 #include "zig_llvm.h"
 #include "hash_map.hpp"
 #include "errmsg.hpp"
@@ -1613,7 +1614,10 @@ struct CodeGen {
         ZigType *entry_promise;
     } builtin_types;
 
+    CacheHash cache_hash;
+
     //////////////////////////// Participates in Input Parameter Cache Hash
+    Buf *compiler_id;
     ZigList<LinkLib *> link_libs_list;
     // add -framework [name] args to linker
     ZigList<Buf *> darwin_frameworks;
src/cache_hash.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "cache_hash.hpp"
+#include "all_types.hpp"
 #include "buffer.hpp"
 #include "os.hpp"
 
@@ -219,6 +220,9 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
 
     buf_append_str(ch->manifest_file_path, ".txt");
 
+    if ((err = os_make_path(ch->manifest_dir)))
+        return err;
+
     if ((err = os_file_open_lock_rw(ch->manifest_file_path, &ch->manifest_file)))
         return err;
 
src/cache_hash.hpp
@@ -8,10 +8,11 @@
 #ifndef ZIG_CACHE_HASH_HPP
 #define ZIG_CACHE_HASH_HPP
 
-#include "all_types.hpp"
 #include "blake2.h"
 #include "os.hpp"
 
+struct LinkLib;
+
 struct CacheHashFile {
     Buf *path;
     OsTimeStamp mtime;
src/codegen.cpp
@@ -88,12 +88,13 @@ static const char *symbols_that_llvm_depends_on[] = {
 };
 
 CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,
-    Buf *zig_lib_dir)
+    Buf *zig_lib_dir, Buf *compiler_id)
 {
     CodeGen *g = allocate<CodeGen>(1);
 
     codegen_add_time_event(g, "Initialize");
 
+    g->compiler_id = compiler_id;
     g->zig_lib_dir = zig_lib_dir;
 
     g->zig_std_dir = buf_alloc();
@@ -7675,44 +7676,62 @@ void codegen_add_time_event(CodeGen *g, const char *name) {
 }
 
 
-//// Called before init()
-//static bool build_with_cache(CodeGen *g) {
-//    // TODO zig exe & dynamic libraries
-//    // should be in main.cpp I think. only needs to happen
-//    // once on startup.
-//
-//    CacheHash comp;
-//    cache_init(&comp);
-//
-//    add_cache_buf(&blake, g->root_out_name);
-//    add_cache_buf_opt(&blake, get_resolved_root_src_path(g)); // Root source file
-//    add_cache_list_of_link_lib(&blake, g->link_libs_list.items, g->link_libs_list.length);
-//    add_cache_list_of_buf(&blake, g->darwin_frameworks.items, g->darwin_frameworks.length);
-//    add_cache_list_of_buf(&blake, g->rpath_list.items, g->rpath_list.length);
-//    add_cache_int(&blake, g->emit_file_type);
-//    add_cache_int(&blake, g->build_mode);
-//    add_cache_int(&blake, g->out_type);
-//    // TODO the rest of the struct CodeGen fields
-//
-//    uint8_t bin_digest[48];
-//    rc = blake2b_final(&blake, bin_digest, 48);
-//    assert(rc == 0);
-//
-//    Buf b64_digest = BUF_INIT;
-//    buf_resize(&b64_digest, 64);
-//    base64_encode(buf_to_slice(&b64_digest), {bin_digest, 48});
-//
-//    fprintf(stderr, "input params hash: %s\n", buf_ptr(&b64_digest));
-//    // TODO next look for a manifest file that has all the files from the input parameters
-//    // use that to construct the real hash, which looks up the output directory and another manifest file
-//
-//    return false;
-//}
+// Called before init()
+static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
+    Error err;
+
+    CacheHash *ch = &g->cache_hash;
+    cache_init(ch, manifest_dir);
+
+    cache_buf(ch, g->compiler_id);
+    cache_buf(ch, g->root_out_name);
+    cache_file(ch, get_resolved_root_src_path(g)); // Root source file
+    cache_list_of_link_lib(ch, g->link_libs_list.items, g->link_libs_list.length);
+    cache_list_of_buf(ch, g->darwin_frameworks.items, g->darwin_frameworks.length);
+    cache_list_of_buf(ch, g->rpath_list.items, g->rpath_list.length);
+    cache_int(ch, g->emit_file_type);
+    cache_int(ch, g->build_mode);
+    cache_int(ch, g->out_type);
+    // TODO the rest of the struct CodeGen fields
+
+    buf_resize(digest, 0);
+    if ((err = cache_hit(ch, digest)))
+        return err;
+
+    return ErrorNone;
+}
 
 void codegen_build(CodeGen *g) {
+    Error err;
     assert(g->out_type != OutTypeUnknown);
-    //if (build_with_cache(g))
-    //    return;
+
+    Buf app_data_dir = BUF_INIT;
+    if ((err = os_get_app_data_dir(&app_data_dir, "zig"))) {
+        fprintf(stderr, "Unable to get app data dir: %s\n", err_str(err));
+        exit(1);
+    }
+    Buf *stage1_dir = buf_alloc();
+    os_path_join(&app_data_dir, buf_create_from_str("stage1"), stage1_dir);
+
+    Buf *manifest_dir = buf_alloc();
+    os_path_join(stage1_dir, buf_create_from_str("build"), manifest_dir);
+
+    Buf digest = BUF_INIT;
+    if ((err = check_cache(g, manifest_dir, &digest))) {
+        fprintf(stderr, "Unable to check cache: %s\n", err_str(err));
+        exit(1);
+    }
+    if (buf_len(&digest) != 0) {
+        Buf *artifact_dir = buf_alloc();
+        os_path_join(stage1_dir, buf_create_from_str("artifact"), artifact_dir);
+
+        Buf *this_artifact_dir = buf_alloc();
+        os_path_join(artifact_dir, &digest, this_artifact_dir);
+
+        fprintf(stderr, "copy artifacts from %s\n", buf_ptr(this_artifact_dir));
+        return;
+    }
+
     init(g);
 
     codegen_add_time_event(g, "Semantic Analysis");
src/codegen.hpp
@@ -15,7 +15,7 @@
 #include <stdio.h>
 
 CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,
-    Buf *zig_lib_dir);
+    Buf *zig_lib_dir, Buf *compiler_id);
 
 void codegen_set_clang_argv(CodeGen *codegen, const char **args, size_t len);
 void codegen_set_llvm_argv(CodeGen *codegen, const char **args, size_t len);
src/link.cpp
@@ -34,7 +34,7 @@ static const char *get_libc_static_file(CodeGen *g, const char *file) {
 static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) {
     ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target;
     CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeObj, parent_gen->build_mode,
-        parent_gen->zig_lib_dir);
+        parent_gen->zig_lib_dir, parent_gen->compiler_id);
 
     child_gen->want_h_file = false;
     child_gen->verbose_tokenize = parent_gen->verbose_tokenize;
src/main.cpp
@@ -274,8 +274,6 @@ static Error get_compiler_id(Buf **result) {
     Buf *manifest_dir = buf_alloc();
     os_path_join(stage1_dir, buf_create_from_str("exe"), manifest_dir);
 
-    if ((err = os_make_path(manifest_dir)))
-        return err;
     CacheHash cache_hash;
     CacheHash *ch = &cache_hash;
     cache_init(ch, manifest_dir);
@@ -432,8 +430,14 @@ int main(int argc, char **argv) {
         Buf *build_runner_path = buf_alloc();
         os_path_join(special_dir, buf_create_from_str("build_runner.zig"), build_runner_path);
 
+        Buf *compiler_id;
+        if ((err = get_compiler_id(&compiler_id))) {
+            fprintf(stderr, "Unable to determine compiler id: %s\n", err_str(err));
+            return EXIT_FAILURE;
+        }
 
-        CodeGen *g = codegen_create(build_runner_path, nullptr, OutTypeExe, BuildModeDebug, zig_lib_dir_buf);
+        CodeGen *g = codegen_create(build_runner_path, nullptr, OutTypeExe, BuildModeDebug, zig_lib_dir_buf,
+                compiler_id);
         codegen_set_out_name(g, buf_create_from_str("build"));
 
         Buf *build_file_buf = buf_create_from_str(build_file);
@@ -796,7 +800,7 @@ int main(int argc, char **argv) {
     switch (cmd) {
     case CmdBuiltin: {
         Buf *zig_lib_dir_buf = resolve_zig_lib_dir();
-        CodeGen *g = codegen_create(nullptr, target, out_type, build_mode, zig_lib_dir_buf);
+        CodeGen *g = codegen_create(nullptr, target, out_type, build_mode, zig_lib_dir_buf, nullptr);
         Buf *builtin_source = codegen_generate_builtin_source(g);
         if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) {
             fprintf(stderr, "unable to write to stdout: %s\n", strerror(ferror(stdout)));
@@ -871,7 +875,14 @@ int main(int argc, char **argv) {
 
             Buf *zig_lib_dir_buf = resolve_zig_lib_dir();
 
-            CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, zig_lib_dir_buf);
+            Buf *compiler_id;
+            if ((err = get_compiler_id(&compiler_id))) {
+                fprintf(stderr, "Unable to determine compiler id: %s\n", err_str(err));
+                return EXIT_FAILURE;
+            }
+
+            CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, zig_lib_dir_buf,
+                    compiler_id);
             codegen_set_out_name(g, buf_out_name);
             codegen_set_lib_version(g, ver_major, ver_minor, ver_patch);
             codegen_set_is_test(g, cmd == CmdTest);
CMakeLists.txt
@@ -390,7 +390,7 @@ if(MSVC)
     )
 else()
     set_target_properties(embedded_softfloat PROPERTIES
-        COMPILE_FLAGS "-std=c99"
+        COMPILE_FLAGS "-std=c99 -O3"
     )
 endif()
 target_include_directories(embedded_softfloat PUBLIC
@@ -407,10 +407,9 @@ set(ZIG_SOURCES
     "${CMAKE_SOURCE_DIR}/src/ast_render.cpp"
     "${CMAKE_SOURCE_DIR}/src/bigfloat.cpp"
     "${CMAKE_SOURCE_DIR}/src/bigint.cpp"
-    "${CMAKE_SOURCE_DIR}/src/blake2b.cpp"
     "${CMAKE_SOURCE_DIR}/src/buffer.cpp"
-    "${CMAKE_SOURCE_DIR}/src/cache_hash.cpp"
     "${CMAKE_SOURCE_DIR}/src/c_tokenizer.cpp"
+    "${CMAKE_SOURCE_DIR}/src/cache_hash.cpp"
     "${CMAKE_SOURCE_DIR}/src/codegen.cpp"
     "${CMAKE_SOURCE_DIR}/src/errmsg.cpp"
     "${CMAKE_SOURCE_DIR}/src/error.cpp"
@@ -426,6 +425,9 @@ set(ZIG_SOURCES
     "${CMAKE_SOURCE_DIR}/src/util.cpp"
     "${CMAKE_SOURCE_DIR}/src/translate_c.cpp"
 )
+set(ZIG_SOURCES_O3
+    "${CMAKE_SOURCE_DIR}/src/blake2b.cpp"
+)
 set(ZIG_CPP_SOURCES
     "${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
     "${CMAKE_SOURCE_DIR}/src/windows_sdk.cpp"
@@ -813,6 +815,11 @@ set_target_properties(zig_cpp PROPERTIES
     COMPILE_FLAGS ${EXE_CFLAGS}
 )
 
+add_library(zig_O3 STATIC ${ZIG_SOURCES_O3})
+set_target_properties(zig_O3 PROPERTIES
+    COMPILE_FLAGS "${EXE_CFLAGS} -O3"
+)
+
 add_executable(zig ${ZIG_SOURCES})
 set_target_properties(zig PROPERTIES
     COMPILE_FLAGS ${EXE_CFLAGS}
@@ -821,6 +828,7 @@ set_target_properties(zig PROPERTIES
 
 target_link_libraries(zig LINK_PUBLIC
     zig_cpp
+    zig_O3
     ${SOFTFLOAT_LIBRARIES}
     ${CLANG_LIBRARIES}
     ${LLD_LIBRARIES}