authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-21 03:01:20-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-21 03:01:20-05:00
log1f7babbc80211e12c9a38ff2196d6ff8c5a19302
tree1768b836888481ba8f82afa11ec6e1c6262bf429
parent0abaee79af462f4264717f88af052fb00eefde7c
signature Commit is signed but in an unrecognized format.

properly forward baseline target cpu features to llvm


5 files changed, 59 insertions(+), 34 deletions(-)

src-self-hosted/stage1.zig+53-31
......@@ -627,7 +627,7 @@ const Stage2CpuFeatures = struct {
627627
628628 const Self = @This();
629629
630 fn createBaseline(allocator: *mem.Allocator) !*Self {
630 fn createBaseline(allocator: *mem.Allocator, arch: Target.Arch) !*Self {
631631 const self = try allocator.create(Self);
632632 errdefer allocator.destroy(self);
633633
......@@ -641,10 +641,11 @@ const Stage2CpuFeatures = struct {
641641 .allocator = allocator,
642642 .cpu_features = .baseline,
643643 .llvm_cpu_name = null,
644 .llvm_features_str = null,
644 .llvm_features_str = try initLLVMFeatures(allocator, arch, arch.baselineFeatures()),
645645 .builtin_str = builtin_str,
646646 .cache_hash = cache_hash,
647647 };
648
648649 return self;
649650 }
650651
......@@ -658,7 +659,7 @@ const Stage2CpuFeatures = struct {
658659 const arch = target.Cross.arch;
659660 const cpu_features = try cpuFeaturesFromLLVM(arch, llvm_cpu_name_z, llvm_cpu_features);
660661 switch (cpu_features) {
661 .baseline => return createBaseline(allocator),
662 .baseline => return createBaseline(allocator, arch),
662663 .cpu => |cpu| return createFromCpu(allocator, arch, cpu),
663664 .features => |features| return createFromCpuFeatures(allocator, arch, features),
664665 }
......@@ -688,31 +689,13 @@ const Stage2CpuFeatures = struct {
688689 return self;
689690 }
690691
691 fn createFromCpuFeatures(
692 fn initLLVMFeatures(
692693 allocator: *mem.Allocator,
693694 arch: Target.Arch,
694695 feature_set: Target.Cpu.Feature.Set,
695 ) !*Self {
696 const self = try allocator.create(Self);
697 errdefer allocator.destroy(self);
698
699 const cache_hash = try std.fmt.allocPrint0(allocator, "\n{x}", .{feature_set});
700 errdefer allocator.free(cache_hash);
701
702 const generic_arch_name = arch.genericName();
703 var builtin_str_buffer = try std.Buffer.allocPrint(
704 allocator,
705 \\CpuFeatures{{
706 \\ .features = Target.{}.featureSet(&[_]Target.{}.Feature{{
707 \\
708 ,
709 .{ generic_arch_name, generic_arch_name },
710 );
711 defer builtin_str_buffer.deinit();
712
696 ) ![*:0]const u8 {
713697 var llvm_features_buffer = try std.Buffer.initSize(allocator, 0);
714698 defer llvm_features_buffer.deinit();
715
716699 // First, disable all features.
717700 // This way, we only get the ones the user requests.
718701 const all_features = arch.allFeaturesList();
......@@ -723,7 +706,6 @@ const Stage2CpuFeatures = struct {
723706 try llvm_features_buffer.append(",");
724707 }
725708 }
726
727709 for (all_features) |feature, index| {
728710 if (!feature_set.isEnabled(@intCast(u8, index))) continue;
729711
......@@ -732,15 +714,43 @@ const Stage2CpuFeatures = struct {
732714 try llvm_features_buffer.append(llvm_name);
733715 try llvm_features_buffer.append(",");
734716 }
735
736 try builtin_str_buffer.append(" .");
737 try builtin_str_buffer.append(feature.name);
738 try builtin_str_buffer.append(",\n");
739717 }
740718
741719 if (mem.endsWith(u8, llvm_features_buffer.toSliceConst(), ",")) {
742720 llvm_features_buffer.shrink(llvm_features_buffer.len() - 1);
743721 }
722 return llvm_features_buffer.toOwnedSlice().ptr;
723 }
724
725 fn createFromCpuFeatures(
726 allocator: *mem.Allocator,
727 arch: Target.Arch,
728 feature_set: Target.Cpu.Feature.Set,
729 ) !*Self {
730 const self = try allocator.create(Self);
731 errdefer allocator.destroy(self);
732
733 const cache_hash = try std.fmt.allocPrint0(allocator, "\n{x}", .{feature_set});
734 errdefer allocator.free(cache_hash);
735
736 const generic_arch_name = arch.genericName();
737 var builtin_str_buffer = try std.Buffer.allocPrint(
738 allocator,
739 \\CpuFeatures{{
740 \\ .features = Target.{}.featureSet(&[_]Target.{}.Feature{{
741 \\
742 ,
743 .{ generic_arch_name, generic_arch_name },
744 );
745 defer builtin_str_buffer.deinit();
746
747 for (arch.allFeaturesList()) |feature, index| {
748 if (!feature_set.isEnabled(@intCast(u8, index))) continue;
749
750 try builtin_str_buffer.append(" .");
751 try builtin_str_buffer.append(feature.name);
752 try builtin_str_buffer.append(",\n");
753 }
744754
745755 try builtin_str_buffer.append(
746756 \\ }),
......@@ -752,7 +762,7 @@ const Stage2CpuFeatures = struct {
752762 .allocator = allocator,
753763 .cpu_features = .{ .features = feature_set },
754764 .llvm_cpu_name = null,
755 .llvm_features_str = llvm_features_buffer.toOwnedSlice().ptr,
765 .llvm_features_str = try initLLVMFeatures(allocator, arch, feature_set),
756766 .builtin_str = builtin_str_buffer.toOwnedSlice(),
757767 .cache_hash = cache_hash,
758768 };
......@@ -843,13 +853,25 @@ fn parseFeatures(zig_triple: [*:0]const u8, features_text: [*:0]const u8) !*Stag
843853}
844854
845855// ABI warning
846export fn stage2_cpu_features_baseline(result: **Stage2CpuFeatures) Error {
847 result.* = Stage2CpuFeatures.createBaseline(std.heap.c_allocator) catch |err| switch (err) {
856export fn stage2_cpu_features_baseline(result: **Stage2CpuFeatures, zig_triple: [*:0]const u8) Error {
857 result.* = cpuFeaturesBaseline(zig_triple) catch |err| switch (err) {
848858 error.OutOfMemory => return .OutOfMemory,
859 error.UnknownArchitecture => return .UnknownArchitecture,
860 error.UnknownSubArchitecture => return .UnknownSubArchitecture,
861 error.UnknownOperatingSystem => return .UnknownOperatingSystem,
862 error.UnknownApplicationBinaryInterface => return .UnknownApplicationBinaryInterface,
863 error.MissingOperatingSystem => return .MissingOperatingSystem,
864 error.MissingArchitecture => return .MissingArchitecture,
849865 };
850866 return .None;
851867}
852868
869fn cpuFeaturesBaseline(zig_triple: [*:0]const u8) !*Stage2CpuFeatures {
870 const target = try Target.parse(mem.toSliceConst(u8, zig_triple));
871 const arch = target.Cross.arch;
872 return Stage2CpuFeatures.createBaseline(std.heap.c_allocator, arch);
873}
874
853875// ABI warning
854876export fn stage2_cpu_features_llvm(
855877 result: **Stage2CpuFeatures,
src/codegen.cpp+2
......@@ -8802,6 +8802,8 @@ static void init(CodeGen *g) {
88028802 target_specific_cpu_args = stage2_cpu_features_get_llvm_cpu(g->zig_target->cpu_features);
88038803 target_specific_features = stage2_cpu_features_get_llvm_features(g->zig_target->cpu_features);
88048804 }
8805 //fprintf(stderr, "name=%s target_specific_cpu_args=%s\n", buf_ptr(g->root_out_name), target_specific_cpu_args);
8806 //fprintf(stderr, "name=%s target_specific_features=%s\n", buf_ptr(g->root_out_name), target_specific_features);
88058807
88068808 g->target_machine = ZigLLVMCreateTargetMachine(target_ref, buf_ptr(&g->llvm_triple_str),
88078809 target_specific_cpu_args, target_specific_features, opt_level, reloc_mode,
src/main.cpp+1-1
......@@ -1005,7 +1005,7 @@ int main(int argc, char **argv) {
10051005 return main_exit(root_progress_node, EXIT_FAILURE);
10061006 }
10071007 } else {
1008 if ((err = stage2_cpu_features_baseline(&target.cpu_features))) {
1008 if ((err = stage2_cpu_features_baseline(&target.cpu_features, buf_ptr(&zig_triple_buf)))) {
10091009 fprintf(stderr, "unable to determine baseline CPU features: %s\n", err_str(err));
10101010 return main_exit(root_progress_node, EXIT_FAILURE);
10111011 }
src/userland.cpp+1-1
......@@ -104,7 +104,7 @@ Error stage2_cpu_features_parse_features(Stage2CpuFeatures **out, const char *zi
104104 const char *msg = "stage0 called stage2_cpu_features_parse_features";
105105 stage2_panic(msg, strlen(msg));
106106}
107Error stage2_cpu_features_baseline(Stage2CpuFeatures **out) {
107Error stage2_cpu_features_baseline(Stage2CpuFeatures **out, const char *zig_triple) {
108108 Stage2CpuFeatures *result = allocate<Stage2CpuFeatures>(1, "Stage2CpuFeatures");
109109 result->builtin_str = ".baseline;\n";
110110 result->cache_hash = "\n\n";
src/userland.h+2-1
......@@ -192,7 +192,8 @@ ZIG_EXTERN_C Error stage2_cpu_features_parse_features(struct Stage2CpuFeatures *
192192 const char *zig_triple, const char *features);
193193
194194// ABI warning
195ZIG_EXTERN_C Error stage2_cpu_features_baseline(struct Stage2CpuFeatures **result);
195ZIG_EXTERN_C Error stage2_cpu_features_baseline(struct Stage2CpuFeatures **result,
196 const char *zig_triple);
196197
197198// ABI warning
198199ZIG_EXTERN_C Error stage2_cpu_features_llvm(struct Stage2CpuFeatures **result,