authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-10-08 11:27:29-07:00
committergravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-10-13 12:53:20-07:00
logb992ea1b079c5967348655f8719cdadaf2df8261
tree0a3132cf40f312fd51cd61359315e83ba616172b
parent37c6fcafa27c9dc4b323ca2fc79f5a928c1b4d14

stage1: Rely on softfloat for `f16` on non-arm targets


1 files changed, 29 insertions(+), 22 deletions(-)

src/stage1/codegen.cpp+29-22
......@@ -80,6 +80,7 @@ void codegen_set_strip(CodeGen *g, bool strip) {
8080 }
8181}
8282
83static LLVMValueRef get_soft_float_fn(CodeGen *g, const char *name, int param_count, LLVMTypeRef param_type, LLVMTypeRef return_type);
8384static void render_const_val(CodeGen *g, ZigValue *const_val, const char *name);
8485static void render_const_val_global(CodeGen *g, ZigValue *const_val, const char *name);
8586static LLVMValueRef gen_const_val(CodeGen *g, ZigValue *const_val, const char *name);
......@@ -1736,12 +1737,7 @@ static LLVMValueRef gen_soft_float_widen_or_shorten(CodeGen *g, ZigType *actual_
17361737 }
17371738 }
17381739
1739 LLVMValueRef func_ref = LLVMGetNamedFunction(g->module, fn_name);
1740 if (func_ref == nullptr) {
1741 LLVMTypeRef fn_type = LLVMFunctionType(return_type, &param_type, 1, false);
1742 func_ref = LLVMAddFunction(g->module, fn_name, fn_type);
1743 }
1744
1740 LLVMValueRef func_ref = get_soft_float_fn(g, fn_name, 1, param_type, return_type);
17451741 result = LLVMBuildCall2(g->builder, LLVMGlobalGetValueType(func_ref), func_ref, &expr_val, 1, "");
17461742
17471743 // On non-Arm platforms we need to bitcast __trunc<>fhf2 result back to f16
......@@ -1766,9 +1762,12 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
17661762 uint64_t wanted_bits;
17671763 if (scalar_actual_type->id == ZigTypeIdFloat) {
17681764
1769 if ((scalar_actual_type == g->builtin_types.entry_f80
1765 if (((scalar_actual_type == g->builtin_types.entry_f80
17701766 || scalar_wanted_type == g->builtin_types.entry_f80)
1771 && !target_has_f80(g->zig_target))
1767 && !target_has_f80(g->zig_target)) ||
1768 ((scalar_actual_type == g->builtin_types.entry_f16
1769 || scalar_wanted_type == g->builtin_types.entry_f16)
1770 && !target_is_arm(g->zig_target)))
17721771 {
17731772 return gen_soft_float_widen_or_shorten(g, actual_type, wanted_type, expr_val);
17741773 }
......@@ -3100,6 +3099,7 @@ static LLVMValueRef gen_float_un_op(CodeGen *g, LLVMValueRef operand, ZigType *o
31003099 ZigType *elem_type = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.elem_type : operand_type;
31013100 if ((elem_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) ||
31023101 (elem_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target)) ||
3102 (elem_type == g->builtin_types.entry_f16 && !target_is_arm(g->zig_target)) ||
31033103 op == BuiltinFnIdTan)
31043104 {
31053105 return gen_soft_float_un_op(g, operand, operand_type, op);
......@@ -3690,7 +3690,8 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, Stage1Air *executable,
36903690 ZigType *operand_type = op1->value->type;
36913691 ZigType *scalar_type = (operand_type->id == ZigTypeIdVector) ? operand_type->data.vector.elem_type : operand_type;
36923692 if ((scalar_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) ||
3693 (scalar_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) {
3693 (scalar_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target)) ||
3694 (scalar_type == g->builtin_types.entry_f16 && !target_is_arm(g->zig_target))) {
36943695 // LLVM incorrectly lowers the soft float calls for f128 as if they operated on `long double`.
36953696 // On some targets this will be incorrect, so we manually lower the call ourselves.
36963697 LLVMValueRef op1_value = ir_llvm_value(g, op1);
......@@ -4024,7 +4025,8 @@ static LLVMValueRef ir_render_cast(CodeGen *g, Stage1Air *executable,
40244025 assert(actual_type->id == ZigTypeIdInt);
40254026 {
40264027 if ((wanted_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) ||
4027 (wanted_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) {
4028 (wanted_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target)) ||
4029 (wanted_type == g->builtin_types.entry_f16 && !target_is_arm(g->zig_target))) {
40284030 return gen_soft_int_to_float_op(g, expr_val, actual_type, wanted_type);
40294031 } else {
40304032 if (actual_type->data.integral.is_signed) {
......@@ -4042,7 +4044,8 @@ static LLVMValueRef ir_render_cast(CodeGen *g, Stage1Air *executable,
40424044
40434045 LLVMValueRef result;
40444046 if ((actual_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) ||
4045 (actual_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) {
4047 (actual_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target)) ||
4048 (actual_type == g->builtin_types.entry_f16 && !target_is_arm(g->zig_target))) {
40464049 result = gen_soft_float_to_int_op(g, expr_val, actual_type, wanted_type);
40474050 } else {
40484051 if (wanted_type->data.integral.is_signed) {
......@@ -4396,7 +4399,8 @@ static LLVMValueRef gen_negation(CodeGen *g, Stage1AirInst *inst, Stage1AirInst
43964399 operand_type->data.vector.elem_type : operand_type;
43974400
43984401 if ((scalar_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) ||
4399 (scalar_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) {
4402 (scalar_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target)) ||
4403 (scalar_type == g->builtin_types.entry_f16 && !target_is_arm(g->zig_target))) {
44004404 return gen_soft_float_neg(g, operand_type, llvm_operand);
44014405 }
44024406
......@@ -7374,7 +7378,9 @@ static LLVMValueRef ir_render_soft_mul_add(CodeGen *g, Stage1Air *executable, St
73747378 uint32_t vector_len = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.len : 0;
73757379
73767380 const char *fn_name;
7377 if (float_type == g->builtin_types.entry_f32)
7381 if (float_type == g->builtin_types.entry_f16)
7382 fn_name = "__fmah";
7383 else if (float_type == g->builtin_types.entry_f32)
73787384 fn_name = "fmaf";
73797385 else if (float_type == g->builtin_types.entry_f64)
73807386 fn_name = "fma";
......@@ -7385,13 +7391,8 @@ static LLVMValueRef ir_render_soft_mul_add(CodeGen *g, Stage1Air *executable, St
73857391 else
73867392 zig_unreachable();
73877393
7388 LLVMValueRef func_ref = LLVMGetNamedFunction(g->module, fn_name);
7389 if (func_ref == nullptr) {
7390 LLVMTypeRef float_type_ref = float_type->llvm_type;
7391 LLVMTypeRef params[3] = { float_type_ref, float_type_ref, float_type_ref };
7392 LLVMTypeRef fn_type = LLVMFunctionType(float_type_ref, params, 3, false);
7393 func_ref = LLVMAddFunction(g->module, fn_name, fn_type);
7394 }
7394 LLVMTypeRef float_type_ref = float_type->llvm_type;
7395 LLVMValueRef func_ref = get_soft_float_fn(g, fn_name, 3, float_type_ref, float_type_ref);
73957396
73967397 LLVMValueRef op1 = ir_llvm_value(g, instruction->op1);
73977398 LLVMValueRef op2 = ir_llvm_value(g, instruction->op2);
......@@ -7421,7 +7422,8 @@ static LLVMValueRef ir_render_mul_add(CodeGen *g, Stage1Air *executable, Stage1A
74217422 ZigType *operand_type = instruction->op1->value->type;
74227423 operand_type = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.elem_type : operand_type;
74237424 if ((operand_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) ||
7424 (operand_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) {
7425 (operand_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target)) ||
7426 (operand_type == g->builtin_types.entry_f16 && !target_is_arm(g->zig_target))) {
74257427 return ir_render_soft_mul_add(g, executable, instruction, operand_type);
74267428 }
74277429 LLVMValueRef op1 = ir_llvm_value(g, instruction->op1);
......@@ -9740,7 +9742,12 @@ static void define_builtin_types(CodeGen *g) {
97409742 }
97419743 }
97429744
9743 add_fp_entry(g, "f16", 16, LLVMHalfType(), &g->builtin_types.entry_f16);
9745 if (target_is_arm(g->zig_target)) {
9746 add_fp_entry(g, "f16", 16, LLVMHalfType(), &g->builtin_types.entry_f16);
9747 } else {
9748 ZigType *u16_ty = get_int_type(g, false, 16);
9749 add_fp_entry(g, "f16", 16, get_llvm_type(g, u16_ty), &g->builtin_types.entry_f16);
9750 }
97449751 add_fp_entry(g, "f32", 32, LLVMFloatType(), &g->builtin_types.entry_f32);
97459752 add_fp_entry(g, "f64", 64, LLVMDoubleType(), &g->builtin_types.entry_f64);
97469753 add_fp_entry(g, "f128", 128, LLVMFP128Type(), &g->builtin_types.entry_f128);