Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float emulation start for more embedded support #4143

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions base/runtime/internal.odin
Original file line number Diff line number Diff line change
Expand Up @@ -1103,3 +1103,24 @@ __read_bits :: proc "contextless" (dst, src: [^]byte, offset: uintptr, size: uin
dst[j>>3] |= the_bit<<(j&7)
}
}

// when !F64_SUPPORTED {
@(link_name="__truncdfsf2", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
truncdfsf2 :: proc "c" (value: f64) -> f32 {
return 0
}
// }

// when !F32_SUPPORTED {
CMP_RESULT :: i32 when ODIN_ARCH == .arm64 else i64

@(link_name="__mulsf3", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
mulsf3 :: proc "c" (a, b: f32) -> f32 {
return 0
}

@(link_name="__gesf2", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
gesf2 :: proc "c" (a, b: f32) -> CMP_RESULT {
return 0
}
// }
16 changes: 16 additions & 0 deletions src/build_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,22 @@ gb_internal bool check_target_feature_is_superset_of(String const &superset, Str
return true;
}

gb_internal bool target_supports_f32() {
switch (build_context.metrics.arch) {
case TargetArch_riscv64:
return check_target_feature_is_enabled(str_lit("f"), nullptr);
}
return true;
}

gb_internal bool target_supports_f64() {
switch (build_context.metrics.arch) {
case TargetArch_riscv64:
return check_target_feature_is_enabled(str_lit("d"), nullptr);
}
return true;
}

// NOTE(Jeroen): Set/create the output and other paths and report an error as appropriate.
// We've previously called `parse_build_flags`, so `out_filepath` should be set.
gb_internal bool init_build_paths(String init_filename) {
Expand Down
17 changes: 17 additions & 0 deletions src/check_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,14 @@ gb_internal bool check_binary_op(CheckerContext *c, Operand *o, Token op) {
case Token_Mul:
case Token_MulEq:
case Token_AddEq:
switch (op.kind) {
case Token_Mul:
case Token_MulEq:
if (ct == t_f32 && !target_supports_f32()) {
add_package_dependency(c, "runtime", "mulsf3", true);
}
}

if (is_type_bit_set(type)) {
return true;
} else if (!is_type_numeric(type)) {
Expand Down Expand Up @@ -2923,6 +2931,9 @@ gb_internal void check_comparison(CheckerContext *c, Ast *node, Operand *x, Oper
if (!is_type_untyped(x->type)) size = gb_max(size, type_size_of(x->type));
if (!is_type_untyped(y->type)) size = gb_max(size, type_size_of(y->type));

Type *xcore = core_type(x->type);
Type *ycore = core_type(y->type);

if (is_type_cstring(x->type) && is_type_cstring(y->type)) {
switch (op) {
case Token_CmpEq: add_package_dependency(c, "runtime", "cstring_eq"); break;
Expand Down Expand Up @@ -2971,6 +2982,10 @@ gb_internal void check_comparison(CheckerContext *c, Ast *node, Operand *x, Oper
}
break;
}
} else if ((xcore == t_f32 || ycore == t_f32) && !target_supports_f32()) {
switch (op) {
case Token_GtEq: add_package_dependency(c, "runtime", "gesf2", true); break;
}
}
}

Expand Down Expand Up @@ -3446,6 +3461,8 @@ gb_internal void check_cast(CheckerContext *c, Operand *x, Type *type, bool forb
add_package_dependency(c, "runtime", "truncsfhf2", REQUIRE);
add_package_dependency(c, "runtime", "truncdfhf2", REQUIRE);
add_package_dependency(c, "runtime", "gnu_f2h_ieee", REQUIRE);
} else if (src == t_f64 && dst == t_f32 && !target_supports_f64()) {
add_package_dependency(c, "runtime", "truncdfsf2", REQUIRE);
}
}
// If we check polymorphic procedures, we risk erring on
Expand Down
20 changes: 19 additions & 1 deletion src/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,16 +465,34 @@ gb_internal i32 linker_stage(LinkerData *gen) {
#endif // GB_ARCH_*_BIT

if (build_context.metrics.arch == TargetArch_riscv64) {
gbString clang_march = gb_string_make(heap_allocator(), "rv64i");
if (check_target_feature_is_enabled(str_lit("m"), nullptr)) {
clang_march = gb_string_appendc(clang_march, "m");
}
if (check_target_feature_is_enabled(str_lit("a"), nullptr)) {
clang_march = gb_string_appendc(clang_march, "a");
}
if (check_target_feature_is_enabled(str_lit("f"), nullptr)) {
clang_march = gb_string_appendc(clang_march, "f");
}
if (check_target_feature_is_enabled(str_lit("d"), nullptr)) {
clang_march = gb_string_appendc(clang_march, "d");
}
if (check_target_feature_is_enabled(str_lit("c"), nullptr)) {
clang_march = gb_string_appendc(clang_march, "c");
}

result = system_exec_command_line_app("clang",
"%s \"%.*s\" "
"-c -o \"%.*s\" "
"-target %.*s -march=rv64gc "
"-target %.*s -march=%s "
"%.*s "
"",
clang_path,
LIT(asm_file),
LIT(obj_file),
LIT(build_context.metrics.target_triplet),
clang_march,
LIT(build_context.extra_assembler_flags)
);
} else if (is_osx) {
Expand Down
14 changes: 6 additions & 8 deletions src/llvm_abi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1573,14 +1573,12 @@ namespace lbAbiRiscv64 {

int xlen = 8; // 8 byte int register size for riscv64.

// NOTE: we are requiring both of these to be enabled so we can just hard-code 8.
// int flen = 0;
// if (check_target_feature_is_enabled(str_lit("d"), nullptr)) {
// flen = 8; // Double precision floats are enabled.
// } else if (check_target_feature_is_enabled(str_lit("f"), nullptr)) {
// flen = 4; // Single precision floats are enabled.
// }
int flen = 8;
int flen = 0;
if (check_target_feature_is_enabled(str_lit("d"), nullptr)) {
flen = 8; // Double precision floats are enabled.
} else if (check_target_feature_is_enabled(str_lit("f"), nullptr)) {
flen = 4; // Single precision floats are enabled.
}

LLVMTypeKind kind = LLVMGetTypeKind(type);
i64 size = lb_sizeof(type);
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3248,7 +3248,7 @@ int main(int arg_count, char const **arg_ptr) {
// NOTE(laytan): on riscv64 we want to enforce some features.
if (build_context.metrics.arch == TargetArch_riscv64) {
String disabled;
if (!check_target_feature_is_enabled(str_lit("64bit,f,d,m"), &disabled)) { // 64bit, floats, doubles, integer multiplication.
if (!check_target_feature_is_enabled(str_lit("64bit,m"), &disabled)) { // 64bit, integer multiplication.
gb_printf_err("missing required target feature: \"%.*s\", enable it by setting a different -microarch or explicitly adding it through -target-features\n", LIT(disabled));
gb_exit(1);
}
Expand Down