@@ -457,6 +457,30 @@ pub fn dataLayout(target: std.Target) []const u8 {
457
457
};
458
458
}
459
459
460
+ // Avoid depending on `llvm.CodeModel` in the bitcode-only case.
461
+ const CodeModel = enum {
462
+ default,
463
+ tiny,
464
+ small,
465
+ kernel,
466
+ medium,
467
+ large,
468
+ };
469
+
470
+ fn 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
+
460
484
pub const Object = struct {
461
485
gpa: Allocator,
462
486
builder: Builder,
@@ -821,14 +845,17 @@ pub const Object = struct {
821
845
module_flags.appendAssumeCapacity(try o.builder.metadataModuleFlag(
822
846
behavior_error,
823
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) {
825
- .tiny => 0,
826
- .small => 1,
827
- .kernel => 2,
828
- .medium => 3,
829
- .large => 4,
830
- else => unreachable,
831
- }))),
848
+ try o.builder.metadataConstant(try o.builder.intConst(.i32, @as(
849
+ i32,
850
+ switch (codeModel(comp.root_mod.code_model, comp.root_mod.resolved_target.result)) {
851
+ .default => unreachable,
852
+ .tiny => 0,
853
+ .small => 1,
854
+ .kernel => 2,
855
+ .medium => 3,
856
+ .large => 4,
857
+ },
858
+ ))),
832
859
));
833
860
}
834
861
@@ -980,7 +1007,7 @@ pub const Object = struct {
980
1007
else
981
1008
.Static;
982
1009
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
1011
.default => .Default,
985
1012
.tiny => .Tiny,
986
1013
.small => .Small,
0 commit comments