From 5d15ec23bb76e672d78ff8486644f77256263a60 Mon Sep 17 00:00:00 2001 From: Dmitry Atamanov Date: Mon, 29 Aug 2022 14:10:29 +0500 Subject: [PATCH] Rename `$$fabs` to `$$abs` --- lib/std/math.c3 | 10 ++++---- src/compiler/enums.h | 2 +- src/compiler/llvm_codegen.c | 2 +- src/compiler/llvm_codegen_expr.c | 25 ++++++++++++++++--- src/compiler/sema_expr.c | 8 ++++-- src/compiler/symtab.c | 2 +- .../builtins/builtin_vector_abs.c3t | 18 +++++++++++++ .../builtins/builtin_vector_abs.c3t | 18 +++++++++++++ 8 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 test/test_suite/builtins/builtin_vector_abs.c3t create mode 100644 test/test_suite2/builtins/builtin_vector_abs.c3t diff --git a/lib/std/math.c3 b/lib/std/math.c3 index 2f46c93a3..e3e9afd9f 100644 --- a/lib/std/math.c3 +++ b/lib/std/math.c3 @@ -81,6 +81,11 @@ macro min(x, y) @builtin return x < y ? x : y; } +macro abs(x) @builtin +{ + return $$abs(x); +} + fn double log10(double x) @inline { return $$log10(x); @@ -126,11 +131,6 @@ fn double pow(double x, double y) @inline return $$pow(x, y); } -fn double fabs(double x) @inline -{ - return $$fabs(x); -} - fn double trunc(double x) @inline { return $$trunc(x); diff --git a/src/compiler/enums.h b/src/compiler/enums.h index c0ca33f98..6af1218ef 100644 --- a/src/compiler/enums.h +++ b/src/compiler/enums.h @@ -767,7 +767,7 @@ typedef enum BUILTIN_MIN, BUILTIN_POW, BUILTIN_EXP, - BUILTIN_FABS, + BUILTIN_ABS, BUILTIN_FMA, BUILTIN_FSHR, BUILTIN_FSHL, diff --git a/src/compiler/llvm_codegen.c b/src/compiler/llvm_codegen.c index 715426adc..4584e5b3e 100644 --- a/src/compiler/llvm_codegen.c +++ b/src/compiler/llvm_codegen.c @@ -654,7 +654,7 @@ void llvm_codegen_setup() intrinsic_id.lrint = lookup_intrinsic("llvm.lrint"); intrinsic_id.llrint = lookup_intrinsic("llvm.llrint"); - //intrinsic_id.abs = lookup_intrinsic("llvm.abs"); + intrinsic_id.abs = lookup_intrinsic("llvm.abs"); intrinsic_id.smax = lookup_intrinsic("llvm.smax"); intrinsic_id.smin = lookup_intrinsic("llvm.smin"); intrinsic_id.umax = lookup_intrinsic("llvm.umax"); diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 90ef6e754..c2a1921c8 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -4126,7 +4126,7 @@ static void llvm_emit_intrinsic_expr(GenContext *c, unsigned intrinsic, BEValue llvm_value_rvalue(c, be_value); arg_results[i] = be_value->value; } - if (intrinsic == intrinsic_id.ctlz || intrinsic == intrinsic_id.cttz) + if (intrinsic == intrinsic_id.ctlz || intrinsic == intrinsic_id.cttz || intrinsic == intrinsic_id.abs) { arg_results[1] = llvm_get_zero_raw(c->bool_type); arguments++; @@ -4326,6 +4326,7 @@ unsigned llvm_get_intrinsic(BuiltinFunction func) case BUILTIN_NONE: case BUILTIN_UNREACHABLE: case BUILTIN_STACKTRACE: + case BUILTIN_ABS: UNREACHABLE case BUILTIN_SYSCLOCK: return intrinsic_id.readcyclecounter; @@ -4349,8 +4350,6 @@ unsigned llvm_get_intrinsic(BuiltinFunction func) return intrinsic_id.maxnum; case BUILTIN_MIN: return intrinsic_id.minnum; - case BUILTIN_FABS: - return intrinsic_id.fabs; case BUILTIN_FMA: return intrinsic_id.fma; case BUILTIN_FSHL: @@ -4587,6 +4586,26 @@ void llvm_emit_builtin_call(GenContext *c, BEValue *result_value, Expr *expr) UNREACHABLE } } + else if (func == BUILTIN_ABS) + { + Type *type = type_flatten(expr->call_expr.arguments[0]->type); + RETRY3: + switch (type->type_kind) + { + case TYPE_BOOL: + case ALL_INTS: + intrinsic = intrinsic_id.abs; + break; + case ALL_FLOATS: + intrinsic = intrinsic_id.fabs; + break; + case TYPE_VECTOR: + type = type->array.base; + goto RETRY3; + default: + UNREACHABLE + } + } else { intrinsic = llvm_get_intrinsic(func); diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 79425c935..f7df7a2ac 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -2469,7 +2469,7 @@ static inline unsigned builtin_expected_args(BuiltinFunction func) case BUILTIN_LOG: case BUILTIN_LOG2: case BUILTIN_LOG10: - case BUILTIN_FABS: + case BUILTIN_ABS: case BUILTIN_VOLATILE_LOAD: case BUILTIN_CTPOP: case BUILTIN_CTTZ: @@ -2700,7 +2700,6 @@ static inline bool sema_expr_analyse_builtin_call(SemaContext *context, Expr *ex case BUILTIN_COS: case BUILTIN_SIN: case BUILTIN_EXP: - case BUILTIN_FABS: case BUILTIN_LOG: case BUILTIN_LOG2: case BUILTIN_LOG10: @@ -2717,6 +2716,11 @@ static inline bool sema_expr_analyse_builtin_call(SemaContext *context, Expr *ex if (!sema_check_builtin_args_match(args, arg_count)) return false; rtype = args[0]->type; break; + case BUILTIN_ABS: + if (!sema_check_builtin_args(args, (BuiltinArg[]) { BA_NUMLIKE }, arg_count)) return false; + if (!sema_check_builtin_args_match(args, arg_count)) return false; + rtype = args[0]->type; + break; case BUILTIN_MAX: case BUILTIN_MIN: if (!sema_check_builtin_args(args, (BuiltinArg[]) { BA_NUMLIKE, BA_NUMLIKE }, arg_count)) return false; diff --git a/src/compiler/symtab.c b/src/compiler/symtab.c index 66bddb09a..2af34b56f 100644 --- a/src/compiler/symtab.c +++ b/src/compiler/symtab.c @@ -198,7 +198,7 @@ void symtab_init(uint32_t capacity) builtin_list[BUILTIN_MAX] = KW_DEF("max"); builtin_list[BUILTIN_MIN] = KW_DEF("min"); builtin_list[BUILTIN_FMA] = KW_DEF("fma"); - builtin_list[BUILTIN_FABS] = KW_DEF("fabs"); + builtin_list[BUILTIN_ABS] = KW_DEF("abs"); builtin_list[BUILTIN_FSHL] = KW_DEF("fshl"); builtin_list[BUILTIN_FSHR] = KW_DEF("fshr"); builtin_list[BUILTIN_VOLATILE_STORE] = KW_DEF("volatile_store"); diff --git a/test/test_suite/builtins/builtin_vector_abs.c3t b/test/test_suite/builtins/builtin_vector_abs.c3t new file mode 100644 index 000000000..2bfaff266 --- /dev/null +++ b/test/test_suite/builtins/builtin_vector_abs.c3t @@ -0,0 +1,18 @@ +// #target: macos-x64 +module test; +fn void main() +{ + { + float[<2>] vf1 = { 1, -1 }; + float[<2>] absf = $$abs(vf1); + } + { + int[<2>] v1 = { 1, -1 }; + int[<2>] absi = $$abs(v1); + } +} + +/* #expect: test.ll + + %1 = call <2 x float> @llvm.fabs.v2f32(<2 x float> %0) + %3 = call <2 x i32> @llvm.abs.v2i32(<2 x i32> %2, i1 false) diff --git a/test/test_suite2/builtins/builtin_vector_abs.c3t b/test/test_suite2/builtins/builtin_vector_abs.c3t new file mode 100644 index 000000000..2bfaff266 --- /dev/null +++ b/test/test_suite2/builtins/builtin_vector_abs.c3t @@ -0,0 +1,18 @@ +// #target: macos-x64 +module test; +fn void main() +{ + { + float[<2>] vf1 = { 1, -1 }; + float[<2>] absf = $$abs(vf1); + } + { + int[<2>] v1 = { 1, -1 }; + int[<2>] absi = $$abs(v1); + } +} + +/* #expect: test.ll + + %1 = call <2 x float> @llvm.fabs.v2f32(<2 x float> %0) + %3 = call <2 x i32> @llvm.abs.v2i32(<2 x i32> %2, i1 false)