diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index 3a8764eed9..5e95474569 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -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 diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index e82d75311e..418bcc4a26 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -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, diff --git a/src/main.zig b/src/main.zig index 1075993846..1e9b9a512e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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