From 7586f613d58db8f210649c890c467edce11643b9 Mon Sep 17 00:00:00 2001 From: Timon Kruiper Date: Fri, 28 Jun 2019 00:58:35 +0200 Subject: Added function-section functionality --- src/zig_llvm.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/zig_llvm.cpp') diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index fe1b279cf0..bd118d936b 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -95,7 +95,7 @@ static const bool assertions_on = false; bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref, const char *filename, ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug, - bool is_small, bool time_report) + bool is_small, bool time_report, bool function_sections) { TimePassesIsEnabled = time_report; @@ -108,6 +108,8 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM TargetMachine* target_machine = reinterpret_cast(targ_machine_ref); target_machine->setO0WantsFastISel(true); + target_machine->Options.FunctionSections = function_sections; + Module* module = unwrap(module_ref); PassManagerBuilder *PMBuilder = new(std::nothrow) PassManagerBuilder(); -- cgit v1.2.3 From bbc0d440b86a1783c1fddc5e594f9b4067016489 Mon Sep 17 00:00:00 2001 From: Timon Kruiper Date: Mon, 1 Jul 2019 15:22:57 +0200 Subject: Added ZigLLVMCreateTargetMachine and pass function-sections flag Also added extra cache line Added the ZigLVVMCreateTargetMachine to self hosted zig code --- src-self-hosted/compilation.zig | 1 + src-self-hosted/llvm.zig | 6 ++--- src/codegen.cpp | 12 +++++---- src/zig_llvm.cpp | 60 ++++++++++++++++++++++++++++++++++++++--- src/zig_llvm.h | 6 ++++- 5 files changed, 73 insertions(+), 12 deletions(-) (limited to 'src/zig_llvm.cpp') diff --git a/src-self-hosted/compilation.zig b/src-self-hosted/compilation.zig index bd20442d4b..a64e52a2b6 100644 --- a/src-self-hosted/compilation.zig +++ b/src-self-hosted/compilation.zig @@ -516,6 +516,7 @@ pub const Compilation = struct { opt_level, reloc_mode, llvm.CodeModelDefault, + false // TODO: add -ffunction-sections option ) orelse return error.OutOfMemory; defer llvm.DisposeTargetMachine(comp.target_machine); diff --git a/src-self-hosted/llvm.zig b/src-self-hosted/llvm.zig index 149b534ea0..40d414e5f7 100644 --- a/src-self-hosted/llvm.zig +++ b/src-self-hosted/llvm.zig @@ -163,8 +163,8 @@ extern fn LLVMCopyStringRepOfTargetData(TD: *TargetData) ?[*]u8; pub const CreateTargetDataLayout = LLVMCreateTargetDataLayout; extern fn LLVMCreateTargetDataLayout(T: *TargetMachine) ?*TargetData; -pub const CreateTargetMachine = LLVMCreateTargetMachine; -extern fn LLVMCreateTargetMachine( +pub const CreateTargetMachine = ZigLLVMCreateTargetMachine; +extern fn ZigLLVMCreateTargetMachine( T: *Target, Triple: [*]const u8, CPU: [*]const u8, @@ -172,6 +172,7 @@ extern fn LLVMCreateTargetMachine( Level: CodeGenOptLevel, Reloc: RelocMode, CodeModel: CodeModel, + function_sections: bool, ) ?*TargetMachine; pub const GetHostCPUName = LLVMGetHostCPUName; @@ -283,7 +284,6 @@ extern fn ZigLLVMTargetMachineEmitToFile( error_message: *[*]u8, is_debug: bool, is_small: bool, - function_sections: bool, ) bool; pub const BuildCall = ZigLLVMBuildCall; diff --git a/src/codegen.cpp b/src/codegen.cpp index ba6cab4285..84e70df1c7 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -7042,7 +7042,7 @@ static void zig_llvm_emit_output(CodeGen *g) { case EmitFileTypeBinary: if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path), ZigLLVM_EmitBinary, &err_msg, g->build_mode == BuildModeDebug, is_small, - g->enable_time_report, g->function_sections)) + g->enable_time_report)) { zig_panic("unable to write object file %s: %s", buf_ptr(output_path), err_msg); } @@ -7058,7 +7058,7 @@ static void zig_llvm_emit_output(CodeGen *g) { case EmitFileTypeAssembly: if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path), ZigLLVM_EmitAssembly, &err_msg, g->build_mode == BuildModeDebug, is_small, - g->enable_time_report, g->function_sections)) + g->enable_time_report)) { zig_panic("unable to write assembly file %s: %s", buf_ptr(output_path), err_msg); } @@ -7068,7 +7068,7 @@ static void zig_llvm_emit_output(CodeGen *g) { case EmitFileTypeLLVMIr: if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path), ZigLLVM_EmitLLVMIr, &err_msg, g->build_mode == BuildModeDebug, is_small, - g->enable_time_report, g->function_sections)) + g->enable_time_report)) { zig_panic("unable to write llvm-ir file %s: %s", buf_ptr(output_path), err_msg); } @@ -8130,8 +8130,9 @@ static void init(CodeGen *g) { target_specific_features = ""; } - g->target_machine = LLVMCreateTargetMachine(target_ref, buf_ptr(&g->triple_str), - target_specific_cpu_args, target_specific_features, opt_level, reloc_mode, LLVMCodeModelDefault); + g->target_machine = ZigLLVMCreateTargetMachine(target_ref, buf_ptr(&g->triple_str), + target_specific_cpu_args, target_specific_features, opt_level, reloc_mode, + LLVMCodeModelDefault, g->function_sections); g->target_data_ref = LLVMCreateTargetDataLayout(g->target_machine); @@ -9448,6 +9449,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { cache_bool(ch, g->have_dynamic_link); cache_bool(ch, g->have_stack_probing); cache_bool(ch, g->is_dummy_so); + cache_bool(ch, g->function_sections); cache_buf_opt(ch, g->mmacosx_version_min); cache_buf_opt(ch, g->mios_version_min); cache_usize(ch, g->version_major); diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index bd118d936b..f8d2ccce7e 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -39,7 +39,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -93,9 +95,63 @@ static const bool assertions_on = true; static const bool assertions_on = false; #endif +LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple, + const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, + LLVMCodeModel CodeModel, bool function_sections) +{ + Optional RM; + switch (Reloc){ + case LLVMRelocStatic: + RM = Reloc::Static; + break; + case LLVMRelocPIC: + RM = Reloc::PIC_; + break; + case LLVMRelocDynamicNoPic: + RM = Reloc::DynamicNoPIC; + break; + case LLVMRelocROPI: + RM = Reloc::ROPI; + break; + case LLVMRelocRWPI: + RM = Reloc::RWPI; + break; + case LLVMRelocROPI_RWPI: + RM = Reloc::ROPI_RWPI; + break; + default: + break; + } + + bool JIT; + Optional CM = unwrap(CodeModel, JIT); + + CodeGenOpt::Level OL; + switch (Level) { + case LLVMCodeGenLevelNone: + OL = CodeGenOpt::None; + break; + case LLVMCodeGenLevelLess: + OL = CodeGenOpt::Less; + break; + case LLVMCodeGenLevelAggressive: + OL = CodeGenOpt::Aggressive; + break; + default: + OL = CodeGenOpt::Default; + break; + } + + TargetOptions opt; + opt.FunctionSections = function_sections; + + return reinterpret_cast(const_cast( + reinterpret_cast(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM, OL, JIT))); +} + bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref, const char *filename, ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug, - bool is_small, bool time_report, bool function_sections) + bool is_small, bool time_report) { TimePassesIsEnabled = time_report; @@ -108,8 +164,6 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM TargetMachine* target_machine = reinterpret_cast(targ_machine_ref); target_machine->setO0WantsFastISel(true); - target_machine->Options.FunctionSections = function_sections; - Module* module = unwrap(module_ref); PassManagerBuilder *PMBuilder = new(std::nothrow) PassManagerBuilder(); diff --git a/src/zig_llvm.h b/src/zig_llvm.h index 985933242d..8b7b0775f7 100644 --- a/src/zig_llvm.h +++ b/src/zig_llvm.h @@ -56,7 +56,11 @@ enum ZigLLVM_EmitOutputType { ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref, const char *filename, enum ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug, - bool is_small, bool time_report, bool function_sections); + bool is_small, bool time_report); + +ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple, + const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, + LLVMCodeModel CodeModel, bool function_sections); ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref); -- cgit v1.2.3 From 4606baee072f832ef794d86f428a7f27d91c6a11 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 3 Jul 2019 15:46:27 -0400 Subject: add -ffunction-sections arg when building C objects the other changes in this commit are minor tidying up --- src/codegen.cpp | 6 +++++- src/zig_llvm.cpp | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'src/zig_llvm.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index c6038c16c3..47e5f542e2 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -8341,6 +8341,10 @@ void add_cc_args(CodeGen *g, ZigList &args, const char *out_dep_pa args.append("-nostdinc"); args.append("-fno-spell-checking"); + if (g->function_sections) { + args.append("-ffunction-sections"); + } + if (translate_c) { // this gives us access to preprocessing entities, presumably at // the cost of performance @@ -8765,10 +8769,10 @@ Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose cache_int(cache_hash, g->build_mode); cache_bool(cache_hash, g->have_pic); cache_bool(cache_hash, want_valgrind_support(g)); + cache_bool(cache_hash, g->function_sections); for (size_t arg_i = 0; arg_i < g->clang_argv_len; arg_i += 1) { cache_str(cache_hash, g->clang_argv[arg_i]); } - cache_bool(cache_hash, g->function_sections); *out_cache_hash = cache_hash; return ErrorNone; diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index f8d2ccce7e..c51c9e1a50 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -145,8 +145,9 @@ LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Tri TargetOptions opt; opt.FunctionSections = function_sections; - return reinterpret_cast(const_cast( - reinterpret_cast(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM, OL, JIT))); + TargetMachine *TM = reinterpret_cast(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM, + OL, JIT); + return reinterpret_cast(TM); } bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref, -- cgit v1.2.3