From 2c3c1cdeb5297348146f92b80e27a285a55c3526 Mon Sep 17 00:00:00 2001 From: Layne Gustafson Date: Wed, 29 May 2019 22:35:54 -0400 Subject: [PATCH 1/4] Add --cpu and --features options for LLVM --- src/all_types.hpp | 3 +++ src/codegen.cpp | 53 +++++++++++++++++++++++++++++++++++++---------- src/codegen.hpp | 4 +++- src/link.cpp | 5 +++-- src/main.cpp | 16 ++++++++++---- std/build.zig | 22 ++++++++++++++++++++ 6 files changed, 85 insertions(+), 18 deletions(-) diff --git a/src/all_types.hpp b/src/all_types.hpp index 5aa1c78ea1ef..d4619b37c14f 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1894,6 +1894,9 @@ struct CodeGen { const char **clang_argv; size_t clang_argv_len; + + const char *llvm_cpu; + const char *llvm_features; }; struct ZigVar { diff --git a/src/codegen.cpp b/src/codegen.cpp index 3dd6995c61cc..b14338aa555f 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -93,6 +93,7 @@ static const char *symbols_that_llvm_depends_on[] = { }; CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget *target, + const char *llvm_cpu, const char *llvm_features, OutType out_type, BuildMode build_mode, Buf *override_lib_dir, Buf *override_std_dir, ZigLibCInstallation *libc, Buf *cache_dir) { @@ -105,6 +106,9 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget g->zig_target = target; g->cache_dir = cache_dir; + g->llvm_cpu = llvm_cpu; + g->llvm_features = llvm_features; + if (override_lib_dir == nullptr) { g->zig_lib_dir = get_zig_lib_dir(); } else { @@ -220,6 +224,14 @@ void codegen_set_llvm_argv(CodeGen *g, const char **args, size_t len) { g->llvm_argv_len = len; } +void codegen_set_llvm_cpu(CodeGen *g, const char *llvm_cpu) { + g->llvm_cpu = llvm_cpu; +} + +void codegen_set_llvm_features(CodeGen *g, const char *llvm_features) { + g->llvm_features = llvm_features; +} + void codegen_set_test_filter(CodeGen *g, Buf *filter) { g->test_filter = filter; } @@ -8113,20 +8125,39 @@ static void init(CodeGen *g) { const char *target_specific_cpu_args; const char *target_specific_features; - if (g->zig_target->is_native) { - // LLVM creates invalid binaries on Windows sometimes. - // See https://github.com/ziglang/zig/issues/508 - // As a workaround we do not use target native features on Windows. - if (g->zig_target->os == OsWindows || g->zig_target->os == OsUefi) { - target_specific_cpu_args = ""; - target_specific_features = ""; + // If the user has overridden one of either cpu or features, + // use that in place of empty string or native. + if (g->llvm_cpu != nullptr) { + target_specific_cpu_args = g->llvm_cpu; + } else { + if (g->zig_target->is_native) { + // LLVM creates invalid binaries on Windows sometimes. + // See https://github.com/ziglang/zig/issues/508 + // As a workaround we do not use target native features on Windows. + if (g->zig_target->os == OsWindows || g->zig_target->os == OsUefi) { + target_specific_cpu_args = ""; + } else { + target_specific_cpu_args = ZigLLVMGetHostCPUName(); + } } else { - target_specific_cpu_args = ZigLLVMGetHostCPUName(); - target_specific_features = ZigLLVMGetNativeFeatures(); + target_specific_cpu_args = ""; } + } + if (g->llvm_features != nullptr) { + target_specific_features = g->llvm_features; } else { - target_specific_cpu_args = ""; - target_specific_features = ""; + if (g->zig_target->is_native) { + // LLVM creates invalid binaries on Windows sometimes. + // See https://github.com/ziglang/zig/issues/508 + // As a workaround we do not use target native features on Windows. + if (g->zig_target->os == OsWindows || g->zig_target->os == OsUefi) { + target_specific_features = ""; + } else { + target_specific_features = ZigLLVMGetNativeFeatures(); + } + } else { + target_specific_features = ""; + } } g->target_machine = LLVMCreateTargetMachine(target_ref, buf_ptr(&g->triple_str), diff --git a/src/codegen.hpp b/src/codegen.hpp index 9a340d720507..06f2e69d11df 100644 --- a/src/codegen.hpp +++ b/src/codegen.hpp @@ -17,11 +17,13 @@ #include CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget *target, - OutType out_type, BuildMode build_mode, Buf *zig_lib_dir, Buf *override_std_dir, + const char *llvm_cpu, const char *llvm_features, OutType out_type, BuildMode build_mode, Buf *zig_lib_dir, Buf *override_std_dir, ZigLibCInstallation *libc, Buf *cache_dir); 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); +void codegen_set_llvm_cpu(CodeGen *codegen, const char *llvm_cpu); +void codegen_set_llvm_features(CodeGen *codegen, const char *llvm_features); void codegen_set_is_test(CodeGen *codegen, bool is_test); void codegen_set_each_lib_rpath(CodeGen *codegen, bool each_lib_rpath); diff --git a/src/link.cpp b/src/link.cpp index 277dcbc5c631..524ccdfb3228 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -22,8 +22,9 @@ struct LinkJob { static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, OutType out_type, ZigLibCInstallation *libc) { - CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type, - parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path()); + CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, + parent_gen->llvm_cpu, parent_gen->llvm_features, out_type, parent_gen->build_mode, + parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path()); child_gen->disable_gen_h = true; child_gen->want_stack_check = WantStackCheckDisabled; child_gen->verbose_tokenize = parent_gen->verbose_tokenize; diff --git a/src/main.cpp b/src/main.cpp index 9b1892061bc9..be3afc83539c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -84,6 +84,8 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " -dirafter [dir] same as -isystem but do it last\n" " -isystem [dir] add additional search path for other .h files\n" " -mllvm [arg] forward an arg to LLVM's option processing\n" + " --cpu [cpu] set the LLVM cpu option (-mcpu)\n" + " --features [features] set the LLVM cpu features option (-mattr)\n" " --override-std-dir [arg] use an alternate Zig standard library\n" "\n" "Link Options:\n" @@ -450,6 +452,8 @@ int main(int argc, char **argv) { ValgrindSupport valgrind_support = ValgrindSupportAuto; WantPIC want_pic = WantPICAuto; WantStackCheck want_stack_check = WantStackCheckAuto; + const char *llvm_cpu = nullptr; + const char *llvm_features = nullptr; ZigList llvm_argv = {0}; llvm_argv.append("zig (LLVM option parsing)"); @@ -520,7 +524,7 @@ int main(int argc, char **argv) { full_cache_dir = os_path_resolve(&cache_dir_buf, 1); } - CodeGen *g = codegen_create(main_pkg_path, build_runner_path, &target, OutTypeExe, + CodeGen *g = codegen_create(main_pkg_path, build_runner_path, &target, llvm_cpu, llvm_features, OutTypeExe, BuildModeDebug, override_lib_dir, override_std_dir, nullptr, &full_cache_dir); g->valgrind_support = valgrind_support; g->enable_time_report = timing_info; @@ -745,6 +749,10 @@ int main(int argc, char **argv) { clang_argv.append(argv[i]); llvm_argv.append(argv[i]); + } else if (strcmp(arg, "--cpu") == 0) { + llvm_cpu = argv[i]; + } else if (strcmp(arg, "--features") == 0) { + llvm_features = argv[i]; } else if (strcmp(arg, "--override-std-dir") == 0) { override_std_dir = buf_create_from_str(argv[i]); } else if (strcmp(arg, "--override-lib-dir") == 0) { @@ -955,7 +963,7 @@ int main(int argc, char **argv) { return EXIT_SUCCESS; } case CmdBuiltin: { - CodeGen *g = codegen_create(main_pkg_path, nullptr, &target, + CodeGen *g = codegen_create(main_pkg_path, nullptr, &target, llvm_cpu, llvm_features, out_type, build_mode, override_lib_dir, override_std_dir, nullptr, nullptr); codegen_set_strip(g, strip); g->subsystem = subsystem; @@ -1055,8 +1063,8 @@ int main(int argc, char **argv) { } else { cache_dir_buf = buf_create_from_str(cache_dir); } - CodeGen *g = codegen_create(main_pkg_path, zig_root_source_file, &target, out_type, build_mode, - override_lib_dir, override_std_dir, libc, cache_dir_buf); + CodeGen *g = codegen_create(main_pkg_path, zig_root_source_file, &target, llvm_cpu, llvm_features, + out_type, build_mode, override_lib_dir, override_std_dir, libc, cache_dir_buf); if (llvm_argv.length >= 2) codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2); g->valgrind_support = valgrind_support; g->want_pic = want_pic; diff --git a/std/build.zig b/std/build.zig index de319197a4ba..9375bf8a88be 100644 --- a/std/build.zig +++ b/std/build.zig @@ -965,6 +965,8 @@ pub const LibExeObjStep = struct { name_prefix: []const u8, filter: ?[]const u8, single_threaded: bool, + llvm_cpu: ?[]const u8, + llvm_features: ?[]const u8, root_src: ?[]const u8, out_h_filename: []const u8, @@ -1071,6 +1073,8 @@ pub const LibExeObjStep = struct { .output_dir = null, .need_system_paths = false, .single_threaded = false, + .llvm_cpu = null, + .llvm_features = null, }; self.computeOutFileNames(); return self; @@ -1241,6 +1245,14 @@ pub const LibExeObjStep = struct { self.disable_gen_h = value; } + pub fn setLlvmCpu(self: *LibExeObjStep, cpu: []const u8) void { + self.llvm_cpu = cpu; + } + + pub fn setLlvmFeatures(self: *LibExeObjStep, features: []const u8) void { + self.llvm_features = features; + } + /// Unless setOutputDir was called, this function must be called only in /// the make step, from a step that has declared a dependency on this one. /// To run an executable built with zig build, use `run`, or create an install step and invoke it. @@ -1445,6 +1457,16 @@ pub const LibExeObjStep = struct { try zig_args.append("--single-threaded"); } + if (self.llvm_cpu) |llvm_cpu| { + try zig_args.append("--cpu"); + try zig_args.append(llvm_cpu); + } + + if (self.llvm_features) |llvm_features| { + try zig_args.append("--features"); + try zig_args.append(llvm_features); + } + switch (self.build_mode) { builtin.Mode.Debug => {}, builtin.Mode.ReleaseSafe => zig_args.append("--release-safe") catch unreachable, From 9ece3436cda4cc2797a50852e0302d3c3bb76e7c Mon Sep 17 00:00:00 2001 From: Layne Gustafson Date: Wed, 29 May 2019 22:54:14 -0400 Subject: [PATCH 2/4] Remove cpu, features from codegen_create --- src/codegen.cpp | 12 ------------ src/codegen.hpp | 4 +--- src/link.cpp | 7 ++++--- src/main.cpp | 8 +++++--- 4 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index b14338aa555f..2e9906133247 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -93,7 +93,6 @@ static const char *symbols_that_llvm_depends_on[] = { }; CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget *target, - const char *llvm_cpu, const char *llvm_features, OutType out_type, BuildMode build_mode, Buf *override_lib_dir, Buf *override_std_dir, ZigLibCInstallation *libc, Buf *cache_dir) { @@ -106,9 +105,6 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget g->zig_target = target; g->cache_dir = cache_dir; - g->llvm_cpu = llvm_cpu; - g->llvm_features = llvm_features; - if (override_lib_dir == nullptr) { g->zig_lib_dir = get_zig_lib_dir(); } else { @@ -224,14 +220,6 @@ void codegen_set_llvm_argv(CodeGen *g, const char **args, size_t len) { g->llvm_argv_len = len; } -void codegen_set_llvm_cpu(CodeGen *g, const char *llvm_cpu) { - g->llvm_cpu = llvm_cpu; -} - -void codegen_set_llvm_features(CodeGen *g, const char *llvm_features) { - g->llvm_features = llvm_features; -} - void codegen_set_test_filter(CodeGen *g, Buf *filter) { g->test_filter = filter; } diff --git a/src/codegen.hpp b/src/codegen.hpp index 06f2e69d11df..9a340d720507 100644 --- a/src/codegen.hpp +++ b/src/codegen.hpp @@ -17,13 +17,11 @@ #include CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget *target, - const char *llvm_cpu, const char *llvm_features, OutType out_type, BuildMode build_mode, Buf *zig_lib_dir, Buf *override_std_dir, + OutType out_type, BuildMode build_mode, Buf *zig_lib_dir, Buf *override_std_dir, ZigLibCInstallation *libc, Buf *cache_dir); 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); -void codegen_set_llvm_cpu(CodeGen *codegen, const char *llvm_cpu); -void codegen_set_llvm_features(CodeGen *codegen, const char *llvm_features); void codegen_set_is_test(CodeGen *codegen, bool is_test); void codegen_set_each_lib_rpath(CodeGen *codegen, bool each_lib_rpath); diff --git a/src/link.cpp b/src/link.cpp index 524ccdfb3228..65e1e5952353 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -22,9 +22,8 @@ struct LinkJob { static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, OutType out_type, ZigLibCInstallation *libc) { - CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, - parent_gen->llvm_cpu, parent_gen->llvm_features, out_type, parent_gen->build_mode, - parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path()); + CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type, + parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path()); child_gen->disable_gen_h = true; child_gen->want_stack_check = WantStackCheckDisabled; child_gen->verbose_tokenize = parent_gen->verbose_tokenize; @@ -35,6 +34,8 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou child_gen->verbose_cimport = parent_gen->verbose_cimport; child_gen->verbose_cc = parent_gen->verbose_cc; child_gen->llvm_argv = parent_gen->llvm_argv; + child_gen->llvm_cpu = parent_gen->llvm_cpu; + child_gen->llvm_features = parent_gen->llvm_features; child_gen->dynamic_linker_path = parent_gen->dynamic_linker_path; codegen_set_strip(child_gen, parent_gen->strip_debug_symbols); diff --git a/src/main.cpp b/src/main.cpp index be3afc83539c..eb181224b451 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -524,7 +524,7 @@ int main(int argc, char **argv) { full_cache_dir = os_path_resolve(&cache_dir_buf, 1); } - CodeGen *g = codegen_create(main_pkg_path, build_runner_path, &target, llvm_cpu, llvm_features, OutTypeExe, + CodeGen *g = codegen_create(main_pkg_path, build_runner_path, &target, OutTypeExe, BuildModeDebug, override_lib_dir, override_std_dir, nullptr, &full_cache_dir); g->valgrind_support = valgrind_support; g->enable_time_report = timing_info; @@ -963,7 +963,7 @@ int main(int argc, char **argv) { return EXIT_SUCCESS; } case CmdBuiltin: { - CodeGen *g = codegen_create(main_pkg_path, nullptr, &target, llvm_cpu, llvm_features, + CodeGen *g = codegen_create(main_pkg_path, nullptr, &target, out_type, build_mode, override_lib_dir, override_std_dir, nullptr, nullptr); codegen_set_strip(g, strip); g->subsystem = subsystem; @@ -1063,9 +1063,11 @@ int main(int argc, char **argv) { } else { cache_dir_buf = buf_create_from_str(cache_dir); } - CodeGen *g = codegen_create(main_pkg_path, zig_root_source_file, &target, llvm_cpu, llvm_features, + CodeGen *g = codegen_create(main_pkg_path, zig_root_source_file, &target, out_type, build_mode, override_lib_dir, override_std_dir, libc, cache_dir_buf); if (llvm_argv.length >= 2) codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2); + g->llvm_cpu = llvm_cpu; + g->llvm_features = llvm_features; g->valgrind_support = valgrind_support; g->want_pic = want_pic; g->want_stack_check = want_stack_check; From 61f3b827499dd951378352dacc2f810c610ff437 Mon Sep 17 00:00:00 2001 From: Layne Gustafson Date: Thu, 30 May 2019 10:32:57 -0400 Subject: [PATCH 3/4] Change --cpu,features to --llvm-* --- src/main.cpp | 8 ++++---- std/build.zig | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index eb181224b451..41ad854aaa52 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -84,8 +84,8 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " -dirafter [dir] same as -isystem but do it last\n" " -isystem [dir] add additional search path for other .h files\n" " -mllvm [arg] forward an arg to LLVM's option processing\n" - " --cpu [cpu] set the LLVM cpu option (-mcpu)\n" - " --features [features] set the LLVM cpu features option (-mattr)\n" + " --llvm-cpu [cpu] set the LLVM cpu option (-mcpu)\n" + " --llvm-features [features] set the LLVM cpu features option (-mattr)\n" " --override-std-dir [arg] use an alternate Zig standard library\n" "\n" "Link Options:\n" @@ -749,9 +749,9 @@ int main(int argc, char **argv) { clang_argv.append(argv[i]); llvm_argv.append(argv[i]); - } else if (strcmp(arg, "--cpu") == 0) { + } else if (strcmp(arg, "--llvm-cpu") == 0) { llvm_cpu = argv[i]; - } else if (strcmp(arg, "--features") == 0) { + } else if (strcmp(arg, "--llvm-features") == 0) { llvm_features = argv[i]; } else if (strcmp(arg, "--override-std-dir") == 0) { override_std_dir = buf_create_from_str(argv[i]); diff --git a/std/build.zig b/std/build.zig index 9375bf8a88be..1f003d16e3d1 100644 --- a/std/build.zig +++ b/std/build.zig @@ -1458,12 +1458,12 @@ pub const LibExeObjStep = struct { } if (self.llvm_cpu) |llvm_cpu| { - try zig_args.append("--cpu"); + try zig_args.append("--llvm-cpu"); try zig_args.append(llvm_cpu); } if (self.llvm_features) |llvm_features| { - try zig_args.append("--features"); + try zig_args.append("--llvm-features"); try zig_args.append(llvm_features); } From b7db046e11f70716e63ffca8b93982cb8a93f62a Mon Sep 17 00:00:00 2001 From: Layne Gustafson Date: Sun, 23 Jun 2019 10:49:29 -0400 Subject: [PATCH 4/4] Fix spacing on wrapped lines --- src/link.cpp | 2 +- src/main.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/link.cpp b/src/link.cpp index 65e1e5952353..18f636c165d8 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -22,7 +22,7 @@ struct LinkJob { static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, OutType out_type, ZigLibCInstallation *libc) { - CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type, + CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type, parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path()); child_gen->disable_gen_h = true; child_gen->want_stack_check = WantStackCheckDisabled; diff --git a/src/main.cpp b/src/main.cpp index 41ad854aaa52..c68d9a9e3463 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -963,7 +963,7 @@ int main(int argc, char **argv) { return EXIT_SUCCESS; } case CmdBuiltin: { - CodeGen *g = codegen_create(main_pkg_path, nullptr, &target, + CodeGen *g = codegen_create(main_pkg_path, nullptr, &target, out_type, build_mode, override_lib_dir, override_std_dir, nullptr, nullptr); codegen_set_strip(g, strip); g->subsystem = subsystem;