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

Closes #22517.
This commit is contained in:
Alex Rønne Petersen 2025-03-18 21:30:06 +01:00 committed by Andrew Kelley
parent fbdf64a7da
commit bfc554b542
3 changed files with 56 additions and 15 deletions

View File

@ -141,11 +141,16 @@ pub const AtomicRmwOp = enum {
/// therefore must be kept in sync with the compiler implementation.
pub const CodeModel = enum {
default,
tiny,
small,
extreme,
kernel,
medium,
large,
medany,
medium,
medlow,
medmid,
normal,
small,
tiny,
};
/// This data structure is used by the Zig language code generation and

View File

@ -771,6 +771,30 @@ const DataLayoutBuilder = struct {
}
};
// Avoid depending on `llvm.CodeModel` in the bitcode-only case.
const CodeModel = enum {
default,
tiny,
small,
kernel,
medium,
large,
};
fn codeModel(model: std.builtin.CodeModel, target: std.Target) CodeModel {
// Roughly match Clang's mapping of GCC code models to LLVM code models.
return switch (model) {
.default => .default,
.extreme, .large => .large,
.kernel => .kernel,
.medany => if (target.cpu.arch.isRISCV()) .medium else .large,
.medium => if (target.os.tag == .aix) .large else .medium,
.medmid => .medium,
.normal, .medlow, .small => .small,
.tiny => .tiny,
};
}
pub const Object = struct {
gpa: Allocator,
builder: Builder,
@ -1135,14 +1159,17 @@ pub const Object = struct {
module_flags.appendAssumeCapacity(try o.builder.metadataModuleFlag(
behavior_error,
try o.builder.metadataString("Code Model"),
try o.builder.metadataConstant(try o.builder.intConst(.i32, @as(i32, switch (comp.root_mod.code_model) {
.tiny => 0,
.small => 1,
.kernel => 2,
.medium => 3,
.large => 4,
else => unreachable,
}))),
try o.builder.metadataConstant(try o.builder.intConst(.i32, @as(
i32,
switch (codeModel(comp.root_mod.code_model, comp.root_mod.resolved_target.result)) {
.default => unreachable,
.tiny => 0,
.small => 1,
.kernel => 2,
.medium => 3,
.large => 4,
},
))),
));
}
@ -1294,7 +1321,7 @@ pub const Object = struct {
else
.Static;
const code_model: llvm.CodeModel = switch (comp.root_mod.code_model) {
const code_model: llvm.CodeModel = switch (codeModel(comp.root_mod.code_model, comp.root_mod.resolved_target.result)) {
.default => .Default,
.tiny => .Tiny,
.small => .Small,

View File

@ -499,9 +499,18 @@ const usage_build_generic =
\\ hex (planned feature) Intel IHEX
\\ raw (planned feature) Dump machine code directly
\\ -mcpu [cpu] Specify target CPU and feature set
\\ -mcmodel=[default|tiny| Limit range of code and data virtual addresses
\\ small|kernel|
\\ medium|large]
\\ -mcmodel=[model] Limit range of code and data virtual addresses
\\ default
\\ extreme
\\ kernel
\\ large
\\ medany
\\ medium
\\ medlow
\\ medmid
\\ normal
\\ small
\\ tiny
\\ -mred-zone Force-enable the "red-zone"
\\ -mno-red-zone Force-disable the "red-zone"
\\ -fomit-frame-pointer Omit the stack frame pointer