authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-06-04 10:42:30-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-06-06 23:42:14-07:00
loge19886a898eeafd5f81eccaa4c7decba8cea70b4
tree6e5bf7254f65a60ea26ac084dafbf4383b2ffe33
parentd6b1ff7533295eab7d6cebe75491bc4bb21e2c21

Compilation.Config: prefer_llvm depends on pie


1 files changed, 106 insertions(+), 102 deletions(-)

src/Compilation/Config.zig+106-102
...@@ -191,95 +191,6 @@ pub fn resolve(options: Options) ResolveError!Config {...@@ -191,95 +191,6 @@ pub fn resolve(options: Options) ResolveError!Config {
191191
192 const root_optimize_mode = options.root_optimize_mode orelse .Debug;192 const root_optimize_mode = options.root_optimize_mode orelse .Debug;
193193
194 // Make a decision on whether to use LLVM backend for machine code generation.
195 // Note that using the LLVM backend does not necessarily mean using LLVM libraries.
196 // For example, Zig can emit .bc and .ll files directly, and this is still considered
197 // using "the LLVM backend".
198 const prefer_llvm = b: {
199 // If emitting to LLVM bitcode object format, must use LLVM backend.
200 if (options.emit_llvm_ir or options.emit_llvm_bc) {
201 if (options.use_llvm == false)
202 return error.EmittingLlvmModuleRequiresLlvmBackend;
203 if (!target_util.hasLlvmSupport(target, target.ofmt))
204 return error.LlvmLacksTargetSupport;
205
206 break :b true;
207 }
208
209 // If LLVM does not support the target, then we can't use it.
210 if (!target_util.hasLlvmSupport(target, target.ofmt)) {
211 if (options.use_llvm == true) return error.LlvmLacksTargetSupport;
212 break :b false;
213 }
214
215 // If Zig does not support the target, then we can't use it.
216 if (target_util.zigBackend(target, false) == .other) {
217 if (options.use_llvm == false) return error.ZigLacksTargetSupport;
218 break :b true;
219 }
220
221 if (options.use_llvm) |x| break :b x;
222
223 // If we cannot use LLVM libraries, then our own backends will be a
224 // better default since the LLVM backend can only produce bitcode
225 // and not an object file or executable.
226 if (!use_lib_llvm and options.emit_bin) break :b false;
227
228 // Prefer LLVM for release builds.
229 if (root_optimize_mode != .Debug) break :b true;
230
231 // At this point we would prefer to use our own self-hosted backend,
232 // because the compilation speed is better than LLVM. But only do it if
233 // we are confident in the robustness of the backend.
234 break :b !target_util.selfHostedBackendIsAsRobustAsLlvm(target);
235 };
236
237 const use_llvm = b: {
238 // If we have no zig code to compile, no need for LLVM.
239 if (!options.have_zcu) break :b false;
240
241 break :b prefer_llvm;
242 };
243
244 if (options.emit_bin and options.have_zcu) {
245 if (!use_lib_llvm and use_llvm) {
246 // Explicit request to use LLVM to produce an object file, but without
247 // using LLVM libraries. Impossible.
248 return error.EmittingBinaryRequiresLlvmLibrary;
249 }
250
251 if (target_util.zigBackend(target, use_llvm) == .other) {
252 // There is no compiler backend available for this target.
253 return error.ZigLacksTargetSupport;
254 }
255 }
256
257 // Make a decision on whether to use LLD or our own linker.
258 const use_lld = b: {
259 if (!target_util.hasLldSupport(target.ofmt)) {
260 if (options.use_lld == true) return error.LldIncompatibleObjectFormat;
261 break :b false;
262 }
263
264 if (!build_options.have_llvm) {
265 if (options.use_lld == true) return error.LldUnavailable;
266 break :b false;
267 }
268
269 if (options.lto != null and options.lto != .none) {
270 if (options.use_lld == false) return error.LtoRequiresLld;
271 break :b true;
272 }
273
274 if (options.use_llvm == false) {
275 if (options.use_lld == true) return error.LldCannotIncrementallyLink;
276 break :b false;
277 }
278
279 if (options.use_lld) |x| break :b x;
280 break :b prefer_llvm;
281 };
282
283 // Make a decision on whether to use Clang or Aro for translate-c and compiling C files.194 // Make a decision on whether to use Clang or Aro for translate-c and compiling C files.
284 const c_frontend: CFrontend = b: {195 const c_frontend: CFrontend = b: {
285 if (!build_options.have_llvm) {196 if (!build_options.have_llvm) {
...@@ -292,19 +203,6 @@ pub fn resolve(options: Options) ResolveError!Config {...@@ -292,19 +203,6 @@ pub fn resolve(options: Options) ResolveError!Config {
292 break :b .clang;203 break :b .clang;
293 };204 };
294205
295 const lto: std.zig.LtoMode = b: {
296 if (!use_lld) {
297 // zig ld LTO support is tracked by
298 // https://github.com/ziglang/zig/issues/8680
299 if (options.lto != null and options.lto != .none) return error.LtoRequiresLld;
300 break :b .none;
301 }
302
303 if (options.lto) |x| break :b x;
304
305 break :b .none;
306 };
307
308 const link_libcpp = b: {206 const link_libcpp = b: {
309 if (options.link_libcpp == true) break :b true;207 if (options.link_libcpp == true) break :b true;
310 if (options.any_sanitize_thread) {208 if (options.any_sanitize_thread) {
...@@ -447,6 +345,112 @@ pub fn resolve(options: Options) ResolveError!Config {...@@ -447,6 +345,112 @@ pub fn resolve(options: Options) ResolveError!Config {
447 } else false;345 } else false;
448 };346 };
449347
348 // Make a decision on whether to use LLVM backend for machine code generation.
349 // Note that using the LLVM backend does not necessarily mean using LLVM libraries.
350 // For example, Zig can emit .bc and .ll files directly, and this is still considered
351 // using "the LLVM backend".
352 const prefer_llvm = b: {
353 // If emitting to LLVM bitcode object format, must use LLVM backend.
354 if (options.emit_llvm_ir or options.emit_llvm_bc) {
355 if (options.use_llvm == false)
356 return error.EmittingLlvmModuleRequiresLlvmBackend;
357 if (!target_util.hasLlvmSupport(target, target.ofmt))
358 return error.LlvmLacksTargetSupport;
359
360 break :b true;
361 }
362
363 // If LLVM does not support the target, then we can't use it.
364 if (!target_util.hasLlvmSupport(target, target.ofmt)) {
365 if (options.use_llvm == true) return error.LlvmLacksTargetSupport;
366 break :b false;
367 }
368
369 // If Zig does not support the target, then we can't use it.
370 if (target_util.zigBackend(target, false) == .other) {
371 if (options.use_llvm == false) return error.ZigLacksTargetSupport;
372 break :b true;
373 }
374
375 if (options.use_llvm) |x| break :b x;
376
377 // If we cannot use LLVM libraries, then our own backends will be a
378 // better default since the LLVM backend can only produce bitcode
379 // and not an object file or executable.
380 if (!use_lib_llvm and options.emit_bin) break :b false;
381
382 // Prefer LLVM for release builds.
383 if (root_optimize_mode != .Debug) break :b true;
384
385 // Self-hosted backends can't handle the inline assembly in std.pie yet
386 // https://github.com/ziglang/zig/issues/24046
387 if (pie) break :b true;
388
389 // At this point we would prefer to use our own self-hosted backend,
390 // because the compilation speed is better than LLVM. But only do it if
391 // we are confident in the robustness of the backend.
392 break :b !target_util.selfHostedBackendIsAsRobustAsLlvm(target);
393 };
394
395 const use_llvm = b: {
396 // If we have no zig code to compile, no need for LLVM.
397 if (!options.have_zcu) break :b false;
398
399 break :b prefer_llvm;
400 };
401
402 if (options.emit_bin and options.have_zcu) {
403 if (!use_lib_llvm and use_llvm) {
404 // Explicit request to use LLVM to produce an object file, but without
405 // using LLVM libraries. Impossible.
406 return error.EmittingBinaryRequiresLlvmLibrary;
407 }
408
409 if (target_util.zigBackend(target, use_llvm) == .other) {
410 // There is no compiler backend available for this target.
411 return error.ZigLacksTargetSupport;
412 }
413 }
414
415 // Make a decision on whether to use LLD or our own linker.
416 const use_lld = b: {
417 if (!target_util.hasLldSupport(target.ofmt)) {
418 if (options.use_lld == true) return error.LldIncompatibleObjectFormat;
419 break :b false;
420 }
421
422 if (!build_options.have_llvm) {
423 if (options.use_lld == true) return error.LldUnavailable;
424 break :b false;
425 }
426
427 if (options.lto != null and options.lto != .none) {
428 if (options.use_lld == false) return error.LtoRequiresLld;
429 break :b true;
430 }
431
432 if (options.use_llvm == false) {
433 if (options.use_lld == true) return error.LldCannotIncrementallyLink;
434 break :b false;
435 }
436
437 if (options.use_lld) |x| break :b x;
438 break :b prefer_llvm;
439 };
440
441 const lto: std.zig.LtoMode = b: {
442 if (!use_lld) {
443 // zig ld LTO support is tracked by
444 // https://github.com/ziglang/zig/issues/8680
445 if (options.lto != null and options.lto != .none) return error.LtoRequiresLld;
446 break :b .none;
447 }
448
449 if (options.lto) |x| break :b x;
450
451 break :b .none;
452 };
453
450 const root_strip = b: {454 const root_strip = b: {
451 if (options.root_strip) |x| break :b x;455 if (options.root_strip) |x| break :b x;
452 if (root_optimize_mode == .ReleaseSmall) break :b true;456 if (root_optimize_mode == .ReleaseSmall) break :b true;