authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-18 21:30:06+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-19 03:15:16+01:00
log03123916e55a9c0c8b66f748a62b7a4a64203535
tree0f131e75bc299b5c567e9e3f30ba3b9b7fff70d3
parent5105c3c7fa46969dc731b7447415436fd7572e87

compiler: Support more GCC code models and fix the mapping to LLVM code models.

Closes #22517.

3 files changed, 56 insertions(+), 15 deletions(-)

lib/std/builtin.zig+8-3
...@@ -141,11 +141,16 @@ pub const AtomicRmwOp = enum {...@@ -141,11 +141,16 @@ pub const AtomicRmwOp = enum {
141/// therefore must be kept in sync with the compiler implementation.141/// therefore must be kept in sync with the compiler implementation.
142pub const CodeModel = enum {142pub const CodeModel = enum {
143 default,143 default,
144 tiny,144 extreme,
145 small,
146 kernel,145 kernel,
147 medium,
148 large,146 large,
147 medany,
148 medium,
149 medlow,
150 medmid,
151 normal,
152 small,
153 tiny,
149};154};
150155
151/// This data structure is used by the Zig language code generation and156/// This data structure is used by the Zig language code generation and
src/codegen/llvm.zig+36-9
...@@ -457,6 +457,30 @@ pub fn dataLayout(target: std.Target) []const u8 {...@@ -457,6 +457,30 @@ pub fn dataLayout(target: std.Target) []const u8 {
457 };457 };
458}458}
459459
460// Avoid depending on `llvm.CodeModel` in the bitcode-only case.
461const CodeModel = enum {
462 default,
463 tiny,
464 small,
465 kernel,
466 medium,
467 large,
468};
469
470fn codeModel(model: std.builtin.CodeModel, target: std.Target) CodeModel {
471 // Roughly match Clang's mapping of GCC code models to LLVM code models.
472 return switch (model) {
473 .default => .default,
474 .extreme, .large => .large,
475 .kernel => .kernel,
476 .medany => if (target.cpu.arch.isRISCV()) .medium else .large,
477 .medium => if (target.os.tag == .aix) .large else .medium,
478 .medmid => .medium,
479 .normal, .medlow, .small => .small,
480 .tiny => .tiny,
481 };
482}
483
460pub const Object = struct {484pub const Object = struct {
461 gpa: Allocator,485 gpa: Allocator,
462 builder: Builder,486 builder: Builder,
...@@ -821,14 +845,17 @@ pub const Object = struct {...@@ -821,14 +845,17 @@ pub const Object = struct {
821 module_flags.appendAssumeCapacity(try o.builder.metadataModuleFlag(845 module_flags.appendAssumeCapacity(try o.builder.metadataModuleFlag(
822 behavior_error,846 behavior_error,
823 try o.builder.metadataString("Code Model"),847 try o.builder.metadataString("Code Model"),
824 try o.builder.metadataConstant(try o.builder.intConst(.i32, @as(i32, switch (comp.root_mod.code_model) {848 try o.builder.metadataConstant(try o.builder.intConst(.i32, @as(
825 .tiny => 0,849 i32,
826 .small => 1,850 switch (codeModel(comp.root_mod.code_model, comp.root_mod.resolved_target.result)) {
827 .kernel => 2,851 .default => unreachable,
828 .medium => 3,852 .tiny => 0,
829 .large => 4,853 .small => 1,
830 else => unreachable,854 .kernel => 2,
831 }))),855 .medium => 3,
856 .large => 4,
857 },
858 ))),
832 ));859 ));
833 }860 }
834861
...@@ -980,7 +1007,7 @@ pub const Object = struct {...@@ -980,7 +1007,7 @@ pub const Object = struct {
980 else1007 else
981 .Static;1008 .Static;
9821009
983 const code_model: llvm.CodeModel = switch (comp.root_mod.code_model) {1010 const code_model: llvm.CodeModel = switch (codeModel(comp.root_mod.code_model, comp.root_mod.resolved_target.result)) {
984 .default => .Default,1011 .default => .Default,
985 .tiny => .Tiny,1012 .tiny => .Tiny,
986 .small => .Small,1013 .small => .Small,
src/main.zig+12-3
...@@ -499,9 +499,18 @@ const usage_build_generic =...@@ -499,9 +499,18 @@ const usage_build_generic =
499 \\ hex (planned feature) Intel IHEX499 \\ hex (planned feature) Intel IHEX
500 \\ raw (planned feature) Dump machine code directly500 \\ raw (planned feature) Dump machine code directly
501 \\ -mcpu [cpu] Specify target CPU and feature set501 \\ -mcpu [cpu] Specify target CPU and feature set
502 \\ -mcmodel=[default|tiny| Limit range of code and data virtual addresses502 \\ -mcmodel=[model] Limit range of code and data virtual addresses
503 \\ small|kernel|503 \\ default
504 \\ medium|large]504 \\ extreme
505 \\ kernel
506 \\ large
507 \\ medany
508 \\ medium
509 \\ medlow
510 \\ medmid
511 \\ normal
512 \\ small
513 \\ tiny
505 \\ -mred-zone Force-enable the "red-zone"514 \\ -mred-zone Force-enable the "red-zone"
506 \\ -mno-red-zone Force-disable the "red-zone"515 \\ -mno-red-zone Force-disable the "red-zone"
507 \\ -fomit-frame-pointer Omit the stack frame pointer516 \\ -fomit-frame-pointer Omit the stack frame pointer