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 {...@@ -1325,7 +1325,7 @@ pub const Object = struct {
1325 },1325 },
1326 };1326 };
1327 if (options.asm_path != null and options.bin_path != null) {1327 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)) {
1329 defer llvm.disposeMessage(error_message);1329 defer llvm.disposeMessage(error_message);
1330 log.err("LLVM failed to emit bin={s} ir={s}: {s}", .{1330 log.err("LLVM failed to emit bin={s} ir={s}: {s}", .{
1331 emit_bin_msg, post_llvm_ir_msg, error_message,1331 emit_bin_msg, post_llvm_ir_msg, error_message,
...@@ -1337,7 +1337,7 @@ pub const Object = struct {...@@ -1337,7 +1337,7 @@ pub const Object = struct {
1337 }1337 }
13381338
1339 lowered_options.asm_filename = options.asm_path;1339 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)) {
1341 defer llvm.disposeMessage(error_message);1341 defer llvm.disposeMessage(error_message);
1342 log.err("LLVM failed to emit asm={s} bin={s} ir={s} bc={s}: {s}", .{1342 log.err("LLVM failed to emit asm={s} bin={s} ir={s} bc={s}: {s}", .{
1343 emit_asm_msg, emit_bin_msg, post_llvm_ir_msg, post_llvm_bc_msg,1343 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 {...@@ -130,7 +130,7 @@ pub const TargetMachine = opaque {
130 T: *TargetMachine,130 T: *TargetMachine,
131 M: *Module,131 M: *Module,
132 ErrorMessage: *[*:0]const u8,132 ErrorMessage: *[*:0]const u8,
133 options: EmitOptions,133 options: *const EmitOptions,
134 ) bool;134 ) bool;
135135
136 pub const createTargetDataLayout = LLVMCreateTargetDataLayout;136 pub const createTargetDataLayout = LLVMCreateTargetDataLayout;
src/zig_llvm.cpp+27-27
...@@ -213,33 +213,33 @@ static SanitizerCoverageOptions getSanCovOptions(ZigLLVMCoverageOptions z) {...@@ -213,33 +213,33 @@ static SanitizerCoverageOptions getSanCovOptions(ZigLLVMCoverageOptions z) {
213}213}
214214
215ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,215ZIG_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)
217{217{
218 TimePassesIsEnabled = options.time_report;218 TimePassesIsEnabled = options->time_report;
219219
220 raw_fd_ostream *dest_asm_ptr = nullptr;220 raw_fd_ostream *dest_asm_ptr = nullptr;
221 raw_fd_ostream *dest_bin_ptr = nullptr;221 raw_fd_ostream *dest_bin_ptr = nullptr;
222 raw_fd_ostream *dest_bitcode_ptr = nullptr;222 raw_fd_ostream *dest_bitcode_ptr = nullptr;
223223
224 if (options.asm_filename) {224 if (options->asm_filename) {
225 std::error_code EC;225 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);
227 if (EC) {227 if (EC) {
228 *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());228 *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());
229 return true;229 return true;
230 }230 }
231 }231 }
232 if (options.bin_filename) {232 if (options->bin_filename) {
233 std::error_code EC;233 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);
235 if (EC) {235 if (EC) {
236 *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());236 *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());
237 return true;237 return true;
238 }238 }
239 }239 }
240 if (options.bitcode_filename) {240 if (options->bitcode_filename) {
241 std::error_code EC;241 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);
243 if (EC) {243 if (EC) {
244 *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());244 *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());
245 return true;245 return true;
...@@ -255,7 +255,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi...@@ -255,7 +255,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
255 std::string ProcName = "zig-";255 std::string ProcName = "zig-";
256 ProcName += std::to_string(PID);256 ProcName += std::to_string(PID);
257 TimeTracerRAII TimeTracer(ProcName,257 TimeTracerRAII TimeTracer(ProcName,
258 options.bin_filename? options.bin_filename : options.asm_filename);258 options->bin_filename? options->bin_filename : options->asm_filename);
259259
260 TargetMachine &target_machine = *reinterpret_cast<TargetMachine*>(targ_machine_ref);260 TargetMachine &target_machine = *reinterpret_cast<TargetMachine*>(targ_machine_ref);
261 target_machine.setO0WantsFastISel(true);261 target_machine.setO0WantsFastISel(true);
...@@ -264,11 +264,11 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi...@@ -264,11 +264,11 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
264264
265 // Pipeline configurations265 // Pipeline configurations
266 PipelineTuningOptions pipeline_opts;266 PipelineTuningOptions pipeline_opts;
267 pipeline_opts.LoopUnrolling = !options.is_debug;267 pipeline_opts.LoopUnrolling = !options->is_debug;
268 pipeline_opts.SLPVectorization = !options.is_debug;268 pipeline_opts.SLPVectorization = !options->is_debug;
269 pipeline_opts.LoopVectorization = !options.is_debug;269 pipeline_opts.LoopVectorization = !options->is_debug;
270 pipeline_opts.LoopInterleaving = !options.is_debug;270 pipeline_opts.LoopInterleaving = !options->is_debug;
271 pipeline_opts.MergeFunctions = !options.is_debug;271 pipeline_opts.MergeFunctions = !options->is_debug;
272272
273 // Instrumentations273 // Instrumentations
274 PassInstrumentationCallbacks instr_callbacks;274 PassInstrumentationCallbacks instr_callbacks;
...@@ -306,7 +306,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi...@@ -306,7 +306,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
306 module_pm.addPass(VerifierPass());306 module_pm.addPass(VerifierPass());
307 }307 }
308308
309 if (!options.is_debug) {309 if (!options->is_debug) {
310 module_pm.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));310 module_pm.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
311 }311 }
312 });312 });
...@@ -316,12 +316,12 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi...@@ -316,12 +316,12 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
316316
317 pass_builder.registerOptimizerLastEPCallback([&](ModulePassManager &module_pm, OptimizationLevel level) {317 pass_builder.registerOptimizerLastEPCallback([&](ModulePassManager &module_pm, OptimizationLevel level) {
318 // Code coverage instrumentation.318 // Code coverage instrumentation.
319 if (options.sancov) {319 if (options->sancov) {
320 module_pm.addPass(SanitizerCoveragePass(getSanCovOptions(options.coverage)));320 module_pm.addPass(SanitizerCoveragePass(getSanCovOptions(options->coverage)));
321 }321 }
322322
323 // Thread sanitizer323 // Thread sanitizer
324 if (options.tsan) {324 if (options->tsan) {
325 module_pm.addPass(ModuleThreadSanitizerPass());325 module_pm.addPass(ModuleThreadSanitizerPass());
326 module_pm.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));326 module_pm.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
327 }327 }
...@@ -335,17 +335,17 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi...@@ -335,17 +335,17 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
335 ModulePassManager module_pm;335 ModulePassManager module_pm;
336 OptimizationLevel opt_level;336 OptimizationLevel opt_level;
337 // Setting up the optimization level337 // Setting up the optimization level
338 if (options.is_debug)338 if (options->is_debug)
339 opt_level = OptimizationLevel::O0;339 opt_level = OptimizationLevel::O0;
340 else if (options.is_small)340 else if (options->is_small)
341 opt_level = OptimizationLevel::Oz;341 opt_level = OptimizationLevel::Oz;
342 else342 else
343 opt_level = OptimizationLevel::O3;343 opt_level = OptimizationLevel::O3;
344344
345 // Initialize the PassManager345 // Initialize the PassManager
346 if (opt_level == OptimizationLevel::O0) {346 if (opt_level == OptimizationLevel::O0) {
347 module_pm = pass_builder.buildO0DefaultPipeline(opt_level, options.lto);347 module_pm = pass_builder.buildO0DefaultPipeline(opt_level, options->lto);
348 } else if (options.lto) {348 } else if (options->lto) {
349 module_pm = pass_builder.buildLTOPreLinkDefaultPipeline(opt_level);349 module_pm = pass_builder.buildLTOPreLinkDefaultPipeline(opt_level);
350 } else {350 } else {
351 module_pm = pass_builder.buildPerModuleDefaultPipeline(opt_level);351 module_pm = pass_builder.buildPerModuleDefaultPipeline(opt_level);
...@@ -356,7 +356,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi...@@ -356,7 +356,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
356 codegen_pm.add(356 codegen_pm.add(
357 createTargetTransformInfoWrapperPass(target_machine.getTargetIRAnalysis()));357 createTargetTransformInfoWrapperPass(target_machine.getTargetIRAnalysis()));
358358
359 if (dest_bin && !options.lto) {359 if (dest_bin && !options->lto) {
360 if (target_machine.addPassesToEmitFile(codegen_pm, *dest_bin, nullptr, CodeGenFileType::ObjectFile)) {360 if (target_machine.addPassesToEmitFile(codegen_pm, *dest_bin, nullptr, CodeGenFileType::ObjectFile)) {
361 *error_message = strdup("TargetMachine can't emit an object file");361 *error_message = strdup("TargetMachine can't emit an object file");
362 return true;362 return true;
...@@ -375,20 +375,20 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi...@@ -375,20 +375,20 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
375 // Code generation phase375 // Code generation phase
376 codegen_pm.run(llvm_module);376 codegen_pm.run(llvm_module);
377377
378 if (options.llvm_ir_filename) {378 if (options->llvm_ir_filename) {
379 if (LLVMPrintModuleToFile(module_ref, options.llvm_ir_filename, error_message)) {379 if (LLVMPrintModuleToFile(module_ref, options->llvm_ir_filename, error_message)) {
380 return true;380 return true;
381 }381 }
382 }382 }
383383
384 if (dest_bin && options.lto) {384 if (dest_bin && options->lto) {
385 WriteBitcodeToFile(llvm_module, *dest_bin);385 WriteBitcodeToFile(llvm_module, *dest_bin);
386 }386 }
387 if (dest_bitcode) {387 if (dest_bitcode) {
388 WriteBitcodeToFile(llvm_module, *dest_bitcode);388 WriteBitcodeToFile(llvm_module, *dest_bitcode);
389 }389 }
390390
391 if (options.time_report) {391 if (options->time_report) {
392 TimerGroup::printAll(errs());392 TimerGroup::printAll(errs());
393 }393 }
394394
src/zig_llvm.h+1-1
...@@ -67,7 +67,7 @@ struct ZigLLVMEmitOptions {...@@ -67,7 +67,7 @@ struct ZigLLVMEmitOptions {
67};67};
6868
69ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,69ZIG_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
72enum ZigLLVMABIType {72enum ZigLLVMABIType {
73 ZigLLVMABITypeDefault, // Target-specific (either soft or hard depending on triple, etc).73 ZigLLVMABITypeDefault, // Target-specific (either soft or hard depending on triple, etc).