compiler: improve target features support

This commit is contained in:
Laytan Laats
2024-05-02 00:59:52 +02:00
parent ff0973e0f5
commit 25f1d0906d
19 changed files with 1067 additions and 205 deletions
+46 -60
View File
@@ -41,6 +41,37 @@ String get_default_microarchitecture() {
return default_march;
}
String get_final_microarchitecture() {
BuildContext *bc = &build_context;
String microarch = bc->microarch;
if (microarch.len == 0) {
microarch = get_default_microarchitecture();
} else if (microarch == str_lit("native")) {
microarch = make_string_c(LLVMGetHostCPUName());
}
return microarch;
}
gb_internal String get_default_features() {
BuildContext *bc = &build_context;
int off = 0;
for (int i = 0; i < bc->metrics.arch; i += 1) {
off += target_microarch_counts[i];
}
String microarch = get_final_microarchitecture();
for (int i = off; i < off+target_microarch_counts[bc->metrics.arch]; i += 1) {
if (microarch_features_list[i].microarch == microarch) {
return microarch_features_list[i].features;
}
}
GB_PANIC("unknown microarch");
return {};
}
gb_internal void lb_add_foreign_library_path(lbModule *m, Entity *e) {
if (e == nullptr) {
return;
@@ -2468,69 +2499,24 @@ gb_internal bool lb_generate_code(lbGenerator *gen) {
code_mode = LLVMCodeModelKernel;
}
String host_cpu_name = copy_string(permanent_allocator(), make_string_c(LLVMGetHostCPUName()));
String llvm_cpu = get_default_microarchitecture();
char const *llvm_features = "";
if (build_context.microarch.len != 0) {
if (build_context.microarch == "native") {
llvm_cpu = host_cpu_name;
} else {
llvm_cpu = copy_string(permanent_allocator(), build_context.microarch);
}
if (llvm_cpu == host_cpu_name) {
llvm_features = LLVMGetHostCPUFeatures();
String llvm_cpu = get_final_microarchitecture();
gbString llvm_features = gb_string_make(temporary_allocator(), "");
String_Iterator it = {build_context.target_features_string, 0};
bool first = true;
for (;;) {
String str = string_split_iterator(&it, ',');
if (str == "") break;
if (!first) {
llvm_features = gb_string_appendc(llvm_features, ",");
}
first = false;
llvm_features = gb_string_appendc(llvm_features, "+");
llvm_features = gb_string_append_length(llvm_features, str.text, str.len);
}
// NOTE(Jeroen): Uncomment to get the list of supported microarchitectures.
/*
if (build_context.microarch == "?") {
string_set_add(&build_context.target_features_set, str_lit("+cpuhelp"));
}
*/
if (build_context.target_features_set.entries.count != 0) {
// Prefix all of the features with a `+`, because we are
// enabling additional features.
char const *additional_features = target_features_set_to_cstring(permanent_allocator(), false, true);
String f_string = make_string_c(llvm_features);
String a_string = make_string_c(additional_features);
isize f_len = f_string.len;
if (f_len == 0) {
// The common case is that llvm_features is empty, so
// the target_features_set additions can be used as is.
llvm_features = additional_features;
} else {
// The user probably specified `-microarch:native`, so
// llvm_features is populated by LLVM's idea of what
// the host CPU supports.
//
// As far as I can tell, (which is barely better than
// wild guessing), a bitset is formed by parsing the
// string left to right.
//
// So, llvm_features + ',' + additonal_features, will
// makes the target_features_set override llvm_features.
char *tmp = gb_alloc_array(permanent_allocator(), char, f_len + 1 + a_string.len + 1);
isize len = 0;
// tmp = f_string
gb_memmove(tmp, f_string.text, f_string.len);
len += f_string.len;
// tmp += ','
tmp[len++] = ',';
// tmp += a_string
gb_memmove(tmp + len, a_string.text, a_string.len);
len += a_string.len;
// tmp += NUL
tmp[len++] = 0;
llvm_features = tmp;
}
}
debugf("CPU: %.*s, Features: %s\n", LIT(llvm_cpu), llvm_features);
// GB_ASSERT_MSG(LLVMTargetHasAsmBackend(target));