authorgravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2019-07-01 15:22:57+02:00
committergravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2019-07-02 19:54:29+02:00
logbbc0d440b86a1783c1fddc5e594f9b4067016489
tree6669783a72d03a9818e581a1896bd455ef3251d6
parent7586f613d58db8f210649c890c467edce11643b9

Added ZigLLVMCreateTargetMachine and pass function-sections flag

Also added extra cache line Added the ZigLVVMCreateTargetMachine to self hosted zig code

5 files changed, 73 insertions(+), 12 deletions(-)

src-self-hosted/compilation.zig+1
...@@ -516,6 +516,7 @@ pub const Compilation = struct {...@@ -516,6 +516,7 @@ pub const Compilation = struct {
516 opt_level,516 opt_level,
517 reloc_mode,517 reloc_mode,
518 llvm.CodeModelDefault,518 llvm.CodeModelDefault,
519 false // TODO: add -ffunction-sections option
519 ) orelse return error.OutOfMemory;520 ) orelse return error.OutOfMemory;
520 defer llvm.DisposeTargetMachine(comp.target_machine);521 defer llvm.DisposeTargetMachine(comp.target_machine);
521522
src-self-hosted/llvm.zig+3-3
...@@ -163,8 +163,8 @@ extern fn LLVMCopyStringRepOfTargetData(TD: *TargetData) ?[*]u8;...@@ -163,8 +163,8 @@ extern fn LLVMCopyStringRepOfTargetData(TD: *TargetData) ?[*]u8;
163pub const CreateTargetDataLayout = LLVMCreateTargetDataLayout;163pub const CreateTargetDataLayout = LLVMCreateTargetDataLayout;
164extern fn LLVMCreateTargetDataLayout(T: *TargetMachine) ?*TargetData;164extern fn LLVMCreateTargetDataLayout(T: *TargetMachine) ?*TargetData;
165165
166pub const CreateTargetMachine = LLVMCreateTargetMachine;166pub const CreateTargetMachine = ZigLLVMCreateTargetMachine;
167extern fn LLVMCreateTargetMachine(167extern fn ZigLLVMCreateTargetMachine(
168 T: *Target,168 T: *Target,
169 Triple: [*]const u8,169 Triple: [*]const u8,
170 CPU: [*]const u8,170 CPU: [*]const u8,
...@@ -172,6 +172,7 @@ extern fn LLVMCreateTargetMachine(...@@ -172,6 +172,7 @@ extern fn LLVMCreateTargetMachine(
172 Level: CodeGenOptLevel,172 Level: CodeGenOptLevel,
173 Reloc: RelocMode,173 Reloc: RelocMode,
174 CodeModel: CodeModel,174 CodeModel: CodeModel,
175 function_sections: bool,
175) ?*TargetMachine;176) ?*TargetMachine;
176177
177pub const GetHostCPUName = LLVMGetHostCPUName;178pub const GetHostCPUName = LLVMGetHostCPUName;
...@@ -283,7 +284,6 @@ extern fn ZigLLVMTargetMachineEmitToFile(...@@ -283,7 +284,6 @@ extern fn ZigLLVMTargetMachineEmitToFile(
283 error_message: *[*]u8,284 error_message: *[*]u8,
284 is_debug: bool,285 is_debug: bool,
285 is_small: bool,286 is_small: bool,
286 function_sections: bool,
287) bool;287) bool;
288288
289pub const BuildCall = ZigLLVMBuildCall;289pub const BuildCall = ZigLLVMBuildCall;
src/codegen.cpp+7-5
...@@ -7042,7 +7042,7 @@ static void zig_llvm_emit_output(CodeGen *g) {...@@ -7042,7 +7042,7 @@ static void zig_llvm_emit_output(CodeGen *g) {
7042 case EmitFileTypeBinary:7042 case EmitFileTypeBinary:
7043 if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path),7043 if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path),
7044 ZigLLVM_EmitBinary, &err_msg, g->build_mode == BuildModeDebug, is_small,7044 ZigLLVM_EmitBinary, &err_msg, g->build_mode == BuildModeDebug, is_small,
7045 g->enable_time_report, g->function_sections))7045 g->enable_time_report))
7046 {7046 {
7047 zig_panic("unable to write object file %s: %s", buf_ptr(output_path), err_msg);7047 zig_panic("unable to write object file %s: %s", buf_ptr(output_path), err_msg);
7048 }7048 }
...@@ -7058,7 +7058,7 @@ static void zig_llvm_emit_output(CodeGen *g) {...@@ -7058,7 +7058,7 @@ static void zig_llvm_emit_output(CodeGen *g) {
7058 case EmitFileTypeAssembly:7058 case EmitFileTypeAssembly:
7059 if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path),7059 if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path),
7060 ZigLLVM_EmitAssembly, &err_msg, g->build_mode == BuildModeDebug, is_small,7060 ZigLLVM_EmitAssembly, &err_msg, g->build_mode == BuildModeDebug, is_small,
7061 g->enable_time_report, g->function_sections))7061 g->enable_time_report))
7062 {7062 {
7063 zig_panic("unable to write assembly file %s: %s", buf_ptr(output_path), err_msg);7063 zig_panic("unable to write assembly file %s: %s", buf_ptr(output_path), err_msg);
7064 }7064 }
...@@ -7068,7 +7068,7 @@ static void zig_llvm_emit_output(CodeGen *g) {...@@ -7068,7 +7068,7 @@ static void zig_llvm_emit_output(CodeGen *g) {
7068 case EmitFileTypeLLVMIr:7068 case EmitFileTypeLLVMIr:
7069 if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path),7069 if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path),
7070 ZigLLVM_EmitLLVMIr, &err_msg, g->build_mode == BuildModeDebug, is_small,7070 ZigLLVM_EmitLLVMIr, &err_msg, g->build_mode == BuildModeDebug, is_small,
7071 g->enable_time_report, g->function_sections))7071 g->enable_time_report))
7072 {7072 {
7073 zig_panic("unable to write llvm-ir file %s: %s", buf_ptr(output_path), err_msg);7073 zig_panic("unable to write llvm-ir file %s: %s", buf_ptr(output_path), err_msg);
7074 }7074 }
...@@ -8130,8 +8130,9 @@ static void init(CodeGen *g) {...@@ -8130,8 +8130,9 @@ static void init(CodeGen *g) {
8130 target_specific_features = "";8130 target_specific_features = "";
8131 }8131 }
81328132
8133 g->target_machine = LLVMCreateTargetMachine(target_ref, buf_ptr(&g->triple_str),8133 g->target_machine = ZigLLVMCreateTargetMachine(target_ref, buf_ptr(&g->triple_str),
8134 target_specific_cpu_args, target_specific_features, opt_level, reloc_mode, LLVMCodeModelDefault);8134 target_specific_cpu_args, target_specific_features, opt_level, reloc_mode,
8135 LLVMCodeModelDefault, g->function_sections);
81358136
8136 g->target_data_ref = LLVMCreateTargetDataLayout(g->target_machine);8137 g->target_data_ref = LLVMCreateTargetDataLayout(g->target_machine);
81378138
...@@ -9448,6 +9449,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {...@@ -9448,6 +9449,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
9448 cache_bool(ch, g->have_dynamic_link);9449 cache_bool(ch, g->have_dynamic_link);
9449 cache_bool(ch, g->have_stack_probing);9450 cache_bool(ch, g->have_stack_probing);
9450 cache_bool(ch, g->is_dummy_so);9451 cache_bool(ch, g->is_dummy_so);
9452 cache_bool(ch, g->function_sections);
9451 cache_buf_opt(ch, g->mmacosx_version_min);9453 cache_buf_opt(ch, g->mmacosx_version_min);
9452 cache_buf_opt(ch, g->mios_version_min);9454 cache_buf_opt(ch, g->mios_version_min);
9453 cache_usize(ch, g->version_major);9455 cache_usize(ch, g->version_major);
src/zig_llvm.cpp+57-3
...@@ -39,7 +39,9 @@...@@ -39,7 +39,9 @@
39#include <llvm/Support/TargetParser.h>39#include <llvm/Support/TargetParser.h>
40#include <llvm/Support/Timer.h>40#include <llvm/Support/Timer.h>
41#include <llvm/Support/raw_ostream.h>41#include <llvm/Support/raw_ostream.h>
42#include <llvm/Support/TargetRegistry.h>
42#include <llvm/Target/TargetMachine.h>43#include <llvm/Target/TargetMachine.h>
44#include <llvm/Target/CodeGenCWrappers.h>
43#include <llvm/Transforms/Coroutines.h>45#include <llvm/Transforms/Coroutines.h>
44#include <llvm/Transforms/IPO.h>46#include <llvm/Transforms/IPO.h>
45#include <llvm/Transforms/IPO/AlwaysInliner.h>47#include <llvm/Transforms/IPO/AlwaysInliner.h>
...@@ -93,9 +95,63 @@ static const bool assertions_on = true;...@@ -93,9 +95,63 @@ static const bool assertions_on = true;
93static const bool assertions_on = false;95static const bool assertions_on = false;
94#endif96#endif
9597
98LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
99 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
100 LLVMCodeModel CodeModel, bool function_sections)
101{
102 Optional<Reloc::Model> RM;
103 switch (Reloc){
104 case LLVMRelocStatic:
105 RM = Reloc::Static;
106 break;
107 case LLVMRelocPIC:
108 RM = Reloc::PIC_;
109 break;
110 case LLVMRelocDynamicNoPic:
111 RM = Reloc::DynamicNoPIC;
112 break;
113 case LLVMRelocROPI:
114 RM = Reloc::ROPI;
115 break;
116 case LLVMRelocRWPI:
117 RM = Reloc::RWPI;
118 break;
119 case LLVMRelocROPI_RWPI:
120 RM = Reloc::ROPI_RWPI;
121 break;
122 default:
123 break;
124 }
125
126 bool JIT;
127 Optional<CodeModel::Model> CM = unwrap(CodeModel, JIT);
128
129 CodeGenOpt::Level OL;
130 switch (Level) {
131 case LLVMCodeGenLevelNone:
132 OL = CodeGenOpt::None;
133 break;
134 case LLVMCodeGenLevelLess:
135 OL = CodeGenOpt::Less;
136 break;
137 case LLVMCodeGenLevelAggressive:
138 OL = CodeGenOpt::Aggressive;
139 break;
140 default:
141 OL = CodeGenOpt::Default;
142 break;
143 }
144
145 TargetOptions opt;
146 opt.FunctionSections = function_sections;
147
148 return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(
149 reinterpret_cast<Target*>(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM, OL, JIT)));
150}
151
96bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,152bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,
97 const char *filename, ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug,153 const char *filename, ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug,
98 bool is_small, bool time_report, bool function_sections)154 bool is_small, bool time_report)
99{155{
100 TimePassesIsEnabled = time_report;156 TimePassesIsEnabled = time_report;
101157
...@@ -108,8 +164,6 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM...@@ -108,8 +164,6 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
108 TargetMachine* target_machine = reinterpret_cast<TargetMachine*>(targ_machine_ref);164 TargetMachine* target_machine = reinterpret_cast<TargetMachine*>(targ_machine_ref);
109 target_machine->setO0WantsFastISel(true);165 target_machine->setO0WantsFastISel(true);
110166
111 target_machine->Options.FunctionSections = function_sections;
112
113 Module* module = unwrap(module_ref);167 Module* module = unwrap(module_ref);
114168
115 PassManagerBuilder *PMBuilder = new(std::nothrow) PassManagerBuilder();169 PassManagerBuilder *PMBuilder = new(std::nothrow) PassManagerBuilder();
src/zig_llvm.h+5-1
...@@ -56,7 +56,11 @@ enum ZigLLVM_EmitOutputType {...@@ -56,7 +56,11 @@ enum ZigLLVM_EmitOutputType {
5656
57ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,57ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,
58 const char *filename, enum ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug,58 const char *filename, enum ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug,
59 bool is_small, bool time_report, bool function_sections);59 bool is_small, bool time_report);
60
61ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
62 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
63 LLVMCodeModel CodeModel, bool function_sections);
6064
61ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref);65ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref);
6266