authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-30 12:18:37+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-30 11:02:12-07:00
log5723fcaac1b473acee2fb3f5ea5486527eac5359
tree1f16033f5935491182cce49844b6108ca18b9613
parentaaca4ff74dab5d749cd292d31957ce19ab5901c7

llvm: Pass EmitOptions to libzigcpp by pointer.

Passing it by value means that bringup on new architectures is harder for no real benefit. Passing it by pointer allows to get the compiler running without needing to figure out the C calling convention details first. This manifested in practice on LoongArch, for example.

4 files changed, 31 insertions(+), 31 deletions(-)

src/codegen/llvm.zig+2-2
......@@ -1325,7 +1325,7 @@ pub const Object = struct {
13251325 },
13261326 };
13271327 if (options.asm_path != null and options.bin_path != null) {
1328 if (target_machine.emitToFile(module, &error_message, lowered_options)) {
1328 if (target_machine.emitToFile(module, &error_message, &lowered_options)) {
13291329 defer llvm.disposeMessage(error_message);
13301330 log.err("LLVM failed to emit bin={s} ir={s}: {s}", .{
13311331 emit_bin_msg, post_llvm_ir_msg, error_message,
......@@ -1337,7 +1337,7 @@ pub const Object = struct {
13371337 }
13381338
13391339 lowered_options.asm_filename = options.asm_path;
1340 if (target_machine.emitToFile(module, &error_message, lowered_options)) {
1340 if (target_machine.emitToFile(module, &error_message, &lowered_options)) {
13411341 defer llvm.disposeMessage(error_message);
13421342 log.err("LLVM failed to emit asm={s} bin={s} ir={s} bc={s}: {s}", .{
13431343 emit_asm_msg, emit_bin_msg, post_llvm_ir_msg, post_llvm_bc_msg,
src/codegen/llvm/bindings.zig+1-1
......@@ -130,7 +130,7 @@ pub const TargetMachine = opaque {
130130 T: *TargetMachine,
131131 M: *Module,
132132 ErrorMessage: *[*:0]const u8,
133 options: EmitOptions,
133 options: *const EmitOptions,
134134 ) bool;
135135
136136 pub const createTargetDataLayout = LLVMCreateTargetDataLayout;
src/zig_llvm.cpp+27-27
......@@ -213,33 +213,33 @@ static SanitizerCoverageOptions getSanCovOptions(ZigLLVMCoverageOptions z) {
213213}
214214
215215ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,
216 char **error_message, struct ZigLLVMEmitOptions options)
216 char **error_message, const struct ZigLLVMEmitOptions *options)
217217{
218 TimePassesIsEnabled = options.time_report;
218 TimePassesIsEnabled = options->time_report;
219219
220220 raw_fd_ostream *dest_asm_ptr = nullptr;
221221 raw_fd_ostream *dest_bin_ptr = nullptr;
222222 raw_fd_ostream *dest_bitcode_ptr = nullptr;
223223
224 if (options.asm_filename) {
224 if (options->asm_filename) {
225225 std::error_code EC;
226 dest_asm_ptr = new(std::nothrow) raw_fd_ostream(options.asm_filename, EC, sys::fs::OF_None);
226 dest_asm_ptr = new(std::nothrow) raw_fd_ostream(options->asm_filename, EC, sys::fs::OF_None);
227227 if (EC) {
228228 *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());
229229 return true;
230230 }
231231 }
232 if (options.bin_filename) {
232 if (options->bin_filename) {
233233 std::error_code EC;
234 dest_bin_ptr = new(std::nothrow) raw_fd_ostream(options.bin_filename, EC, sys::fs::OF_None);
234 dest_bin_ptr = new(std::nothrow) raw_fd_ostream(options->bin_filename, EC, sys::fs::OF_None);
235235 if (EC) {
236236 *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());
237237 return true;
238238 }
239239 }
240 if (options.bitcode_filename) {
240 if (options->bitcode_filename) {
241241 std::error_code EC;
242 dest_bitcode_ptr = new(std::nothrow) raw_fd_ostream(options.bitcode_filename, EC, sys::fs::OF_None);
242 dest_bitcode_ptr = new(std::nothrow) raw_fd_ostream(options->bitcode_filename, EC, sys::fs::OF_None);
243243 if (EC) {
244244 *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());
245245 return true;
......@@ -255,7 +255,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
255255 std::string ProcName = "zig-";
256256 ProcName += std::to_string(PID);
257257 TimeTracerRAII TimeTracer(ProcName,
258 options.bin_filename? options.bin_filename : options.asm_filename);
258 options->bin_filename? options->bin_filename : options->asm_filename);
259259
260260 TargetMachine &target_machine = *reinterpret_cast<TargetMachine*>(targ_machine_ref);
261261 target_machine.setO0WantsFastISel(true);
......@@ -264,11 +264,11 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
264264
265265 // Pipeline configurations
266266 PipelineTuningOptions pipeline_opts;
267 pipeline_opts.LoopUnrolling = !options.is_debug;
268 pipeline_opts.SLPVectorization = !options.is_debug;
269 pipeline_opts.LoopVectorization = !options.is_debug;
270 pipeline_opts.LoopInterleaving = !options.is_debug;
271 pipeline_opts.MergeFunctions = !options.is_debug;
267 pipeline_opts.LoopUnrolling = !options->is_debug;
268 pipeline_opts.SLPVectorization = !options->is_debug;
269 pipeline_opts.LoopVectorization = !options->is_debug;
270 pipeline_opts.LoopInterleaving = !options->is_debug;
271 pipeline_opts.MergeFunctions = !options->is_debug;
272272
273273 // Instrumentations
274274 PassInstrumentationCallbacks instr_callbacks;
......@@ -306,7 +306,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
306306 module_pm.addPass(VerifierPass());
307307 }
308308
309 if (!options.is_debug) {
309 if (!options->is_debug) {
310310 module_pm.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
311311 }
312312 });
......@@ -316,12 +316,12 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
316316
317317 pass_builder.registerOptimizerLastEPCallback([&](ModulePassManager &module_pm, OptimizationLevel level) {
318318 // Code coverage instrumentation.
319 if (options.sancov) {
320 module_pm.addPass(SanitizerCoveragePass(getSanCovOptions(options.coverage)));
319 if (options->sancov) {
320 module_pm.addPass(SanitizerCoveragePass(getSanCovOptions(options->coverage)));
321321 }
322322
323323 // Thread sanitizer
324 if (options.tsan) {
324 if (options->tsan) {
325325 module_pm.addPass(ModuleThreadSanitizerPass());
326326 module_pm.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
327327 }
......@@ -335,17 +335,17 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
335335 ModulePassManager module_pm;
336336 OptimizationLevel opt_level;
337337 // Setting up the optimization level
338 if (options.is_debug)
338 if (options->is_debug)
339339 opt_level = OptimizationLevel::O0;
340 else if (options.is_small)
340 else if (options->is_small)
341341 opt_level = OptimizationLevel::Oz;
342342 else
343343 opt_level = OptimizationLevel::O3;
344344
345345 // Initialize the PassManager
346346 if (opt_level == OptimizationLevel::O0) {
347 module_pm = pass_builder.buildO0DefaultPipeline(opt_level, options.lto);
348 } else if (options.lto) {
347 module_pm = pass_builder.buildO0DefaultPipeline(opt_level, options->lto);
348 } else if (options->lto) {
349349 module_pm = pass_builder.buildLTOPreLinkDefaultPipeline(opt_level);
350350 } else {
351351 module_pm = pass_builder.buildPerModuleDefaultPipeline(opt_level);
......@@ -356,7 +356,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
356356 codegen_pm.add(
357357 createTargetTransformInfoWrapperPass(target_machine.getTargetIRAnalysis()));
358358
359 if (dest_bin && !options.lto) {
359 if (dest_bin && !options->lto) {
360360 if (target_machine.addPassesToEmitFile(codegen_pm, *dest_bin, nullptr, CodeGenFileType::ObjectFile)) {
361361 *error_message = strdup("TargetMachine can't emit an object file");
362362 return true;
......@@ -375,20 +375,20 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
375375 // Code generation phase
376376 codegen_pm.run(llvm_module);
377377
378 if (options.llvm_ir_filename) {
379 if (LLVMPrintModuleToFile(module_ref, options.llvm_ir_filename, error_message)) {
378 if (options->llvm_ir_filename) {
379 if (LLVMPrintModuleToFile(module_ref, options->llvm_ir_filename, error_message)) {
380380 return true;
381381 }
382382 }
383383
384 if (dest_bin && options.lto) {
384 if (dest_bin && options->lto) {
385385 WriteBitcodeToFile(llvm_module, *dest_bin);
386386 }
387387 if (dest_bitcode) {
388388 WriteBitcodeToFile(llvm_module, *dest_bitcode);
389389 }
390390
391 if (options.time_report) {
391 if (options->time_report) {
392392 TimerGroup::printAll(errs());
393393 }
394394
src/zig_llvm.h+1-1
......@@ -67,7 +67,7 @@ struct ZigLLVMEmitOptions {
6767};
6868
6969ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,
70 char **error_message, struct ZigLLVMEmitOptions options);
70 char **error_message, const struct ZigLLVMEmitOptions *options);
7171
7272enum ZigLLVMABIType {
7373 ZigLLVMABITypeDefault, // Target-specific (either soft or hard depending on triple, etc).