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 {
516516 opt_level,
517517 reloc_mode,
518518 llvm.CodeModelDefault,
519 false // TODO: add -ffunction-sections option
519520 ) orelse return error.OutOfMemory;
520521 defer llvm.DisposeTargetMachine(comp.target_machine);
521522
src-self-hosted/llvm.zig+3-3
......@@ -163,8 +163,8 @@ extern fn LLVMCopyStringRepOfTargetData(TD: *TargetData) ?[*]u8;
163163pub const CreateTargetDataLayout = LLVMCreateTargetDataLayout;
164164extern fn LLVMCreateTargetDataLayout(T: *TargetMachine) ?*TargetData;
165165
166pub const CreateTargetMachine = LLVMCreateTargetMachine;
167extern fn LLVMCreateTargetMachine(
166pub const CreateTargetMachine = ZigLLVMCreateTargetMachine;
167extern fn ZigLLVMCreateTargetMachine(
168168 T: *Target,
169169 Triple: [*]const u8,
170170 CPU: [*]const u8,
......@@ -172,6 +172,7 @@ extern fn LLVMCreateTargetMachine(
172172 Level: CodeGenOptLevel,
173173 Reloc: RelocMode,
174174 CodeModel: CodeModel,
175 function_sections: bool,
175176) ?*TargetMachine;
176177
177178pub const GetHostCPUName = LLVMGetHostCPUName;
......@@ -283,7 +284,6 @@ extern fn ZigLLVMTargetMachineEmitToFile(
283284 error_message: *[*]u8,
284285 is_debug: bool,
285286 is_small: bool,
286 function_sections: bool,
287287) bool;
288288
289289pub const BuildCall = ZigLLVMBuildCall;
src/codegen.cpp+7-5
......@@ -7042,7 +7042,7 @@ static void zig_llvm_emit_output(CodeGen *g) {
70427042 case EmitFileTypeBinary:
70437043 if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path),
70447044 ZigLLVM_EmitBinary, &err_msg, g->build_mode == BuildModeDebug, is_small,
7045 g->enable_time_report, g->function_sections))
7045 g->enable_time_report))
70467046 {
70477047 zig_panic("unable to write object file %s: %s", buf_ptr(output_path), err_msg);
70487048 }
......@@ -7058,7 +7058,7 @@ static void zig_llvm_emit_output(CodeGen *g) {
70587058 case EmitFileTypeAssembly:
70597059 if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path),
70607060 ZigLLVM_EmitAssembly, &err_msg, g->build_mode == BuildModeDebug, is_small,
7061 g->enable_time_report, g->function_sections))
7061 g->enable_time_report))
70627062 {
70637063 zig_panic("unable to write assembly file %s: %s", buf_ptr(output_path), err_msg);
70647064 }
......@@ -7068,7 +7068,7 @@ static void zig_llvm_emit_output(CodeGen *g) {
70687068 case EmitFileTypeLLVMIr:
70697069 if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path),
70707070 ZigLLVM_EmitLLVMIr, &err_msg, g->build_mode == BuildModeDebug, is_small,
7071 g->enable_time_report, g->function_sections))
7071 g->enable_time_report))
70727072 {
70737073 zig_panic("unable to write llvm-ir file %s: %s", buf_ptr(output_path), err_msg);
70747074 }
......@@ -8130,8 +8130,9 @@ static void init(CodeGen *g) {
81308130 target_specific_features = "";
81318131 }
81328132
8133 g->target_machine = LLVMCreateTargetMachine(target_ref, buf_ptr(&g->triple_str),
8134 target_specific_cpu_args, target_specific_features, opt_level, reloc_mode, LLVMCodeModelDefault);
8133 g->target_machine = ZigLLVMCreateTargetMachine(target_ref, buf_ptr(&g->triple_str),
8134 target_specific_cpu_args, target_specific_features, opt_level, reloc_mode,
8135 LLVMCodeModelDefault, g->function_sections);
81358136
81368137 g->target_data_ref = LLVMCreateTargetDataLayout(g->target_machine);
81378138
......@@ -9448,6 +9449,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
94489449 cache_bool(ch, g->have_dynamic_link);
94499450 cache_bool(ch, g->have_stack_probing);
94509451 cache_bool(ch, g->is_dummy_so);
9452 cache_bool(ch, g->function_sections);
94519453 cache_buf_opt(ch, g->mmacosx_version_min);
94529454 cache_buf_opt(ch, g->mios_version_min);
94539455 cache_usize(ch, g->version_major);
src/zig_llvm.cpp+57-3
......@@ -39,7 +39,9 @@
3939#include <llvm/Support/TargetParser.h>
4040#include <llvm/Support/Timer.h>
4141#include <llvm/Support/raw_ostream.h>
42#include <llvm/Support/TargetRegistry.h>
4243#include <llvm/Target/TargetMachine.h>
44#include <llvm/Target/CodeGenCWrappers.h>
4345#include <llvm/Transforms/Coroutines.h>
4446#include <llvm/Transforms/IPO.h>
4547#include <llvm/Transforms/IPO/AlwaysInliner.h>
......@@ -93,9 +95,63 @@ static const bool assertions_on = true;
9395static const bool assertions_on = false;
9496#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
96152bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,
97153 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)
99155{
100156 TimePassesIsEnabled = time_report;
101157
......@@ -108,8 +164,6 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
108164 TargetMachine* target_machine = reinterpret_cast<TargetMachine*>(targ_machine_ref);
109165 target_machine->setO0WantsFastISel(true);
110166
111 target_machine->Options.FunctionSections = function_sections;
112
113167 Module* module = unwrap(module_ref);
114168
115169 PassManagerBuilder *PMBuilder = new(std::nothrow) PassManagerBuilder();
src/zig_llvm.h+5-1
......@@ -56,7 +56,11 @@ enum ZigLLVM_EmitOutputType {
5656
5757ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,
5858 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
6165ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref);
6266