Skip to content

Commit ab38df1

Browse files
authored
Merge pull request #1715 from Thirumalai-Shaktivel/intrinsics_03
Sync from LFortran IntrinsicFunctions
2 parents 90be4b0 + d8e96db commit ab38df1

10 files changed

+113
-52
lines changed

src/libasr/codegen/asr_to_c.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <libasr/pass/class_constructor.h>
1414
#include <libasr/pass/array_op.h>
1515
#include <libasr/pass/subroutine_from_function.h>
16-
#include <libasr/pass/intrinsic_function.h>
1716

1817
#include <map>
1918
#include <utility>

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <libasr/asr_utils.h>
2424
#include <libasr/string_utils.h>
2525
#include <libasr/pass/unused_functions.h>
26+
#include <libasr/pass/intrinsic_function_registry.h>
2627

2728
#include <map>
2829
#include <tuple>
@@ -670,7 +671,7 @@ R"(#include <stdio.h>
670671
const_vars_count += 1;
671672
const_name = current_scope->get_unique_name(const_name);
672673
std::string indent(indentation_level*indentation_spaces, ' ');
673-
tmp_buffer_src.push_back(check_tmp_buffer() + indent + c_ds_api->get_dict_type(dict_type) +
674+
tmp_buffer_src.push_back(check_tmp_buffer() + indent + c_ds_api->get_dict_type(dict_type) +
674675
" " + const_name + " = " + src + ";\n");
675676
src = const_name;
676677
return;
@@ -2147,6 +2148,32 @@ R"(#include <stdio.h>
21472148
src = out;
21482149
}
21492150

2151+
#define SET_INTRINSIC_NAME(X, func_name) \
2152+
case (static_cast<int64_t>(ASRUtils::IntrinsicFunctions::X)) : { \
2153+
out += func_name; break; \
2154+
}
2155+
2156+
void visit_IntrinsicFunction(const ASR::IntrinsicFunction_t &x) {
2157+
LCOMPILERS_ASSERT(x.n_args == 1)
2158+
std::string out;
2159+
switch (x.m_intrinsic_id) {
2160+
SET_INTRINSIC_NAME(Sin, "sin");
2161+
SET_INTRINSIC_NAME(Cos, "cos");
2162+
SET_INTRINSIC_NAME(Tan, "tan");
2163+
SET_INTRINSIC_NAME(Asin, "asin");
2164+
SET_INTRINSIC_NAME(Acos, "acos");
2165+
SET_INTRINSIC_NAME(Atan, "atan");
2166+
SET_INTRINSIC_NAME(Abs, "abs");
2167+
default : {
2168+
throw LCompilersException("IntrinsicFunction: `"
2169+
+ ASRUtils::get_intrinsic_name(x.m_intrinsic_id)
2170+
+ "` is not implemented");
2171+
}
2172+
}
2173+
this->visit_expr(*x.m_args[0]);
2174+
out += "(" + src + ")";
2175+
src = out;
2176+
}
21502177
};
21512178

21522179
} // namespace LCompilers

src/libasr/codegen/asr_to_cpp.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <libasr/asr_utils.h>
1111
#include <libasr/string_utils.h>
1212
#include <libasr/pass/unused_functions.h>
13-
#include <libasr/pass/intrinsic_function.h>
1413

1514

1615
namespace LCompilers {
@@ -746,7 +745,6 @@ Result<std::string> asr_to_cpp(Allocator &al, ASR::TranslationUnit_t &asr,
746745
LCompilers::PassOptions pass_options;
747746
pass_options.always_run = true;
748747
pass_unused_functions(al, asr, pass_options);
749-
pass_replace_intrinsic_function(al, asr, pass_options);
750748
ASRToCPPVisitor v(diagnostics, co, default_lower_bound);
751749
try {
752750
v.visit_asr((ASR::asr_t &)asr);

src/libasr/codegen/asr_to_julia.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#include "libasr/asr.h"
2-
#include "libasr/asr_utils.h"
3-
#include "libasr/diagnostics.h"
1+
#include <libasr/asr.h>
2+
#include <libasr/asr_utils.h>
3+
#include <libasr/pass/intrinsic_function_registry.h>
4+
#include <libasr/diagnostics.h>
45
#include <libasr/codegen/asr_to_julia.h>
56

67
namespace LCompilers {
@@ -1874,6 +1875,33 @@ class ASRToJuliaVisitor : public ASR::BaseVisitor<ASRToJuliaVisitor>
18741875
std::string out = indent + "// FIXME: File Read\n";
18751876
src = out;
18761877
}
1878+
1879+
#define SET_INTRINSIC_NAME(X, func_name) \
1880+
case (static_cast<int64_t>(ASRUtils::IntrinsicFunctions::X)) : { \
1881+
out += func_name; break; \
1882+
}
1883+
1884+
void visit_IntrinsicFunction(const ASR::IntrinsicFunction_t &x) {
1885+
std::string out;
1886+
LCOMPILERS_ASSERT(x.n_args == 1);
1887+
visit_expr(*x.m_args[0]);
1888+
switch (x.m_intrinsic_id) {
1889+
SET_INTRINSIC_NAME(Sin, "sin");
1890+
SET_INTRINSIC_NAME(Cos, "cos");
1891+
SET_INTRINSIC_NAME(Tan, "tan");
1892+
SET_INTRINSIC_NAME(Asin, "asin");
1893+
SET_INTRINSIC_NAME(Acos, "acos");
1894+
SET_INTRINSIC_NAME(Atan, "atan");
1895+
SET_INTRINSIC_NAME(Abs, "abs");
1896+
default : {
1897+
throw LCompilersException("IntrinsicFunction: `"
1898+
+ ASRUtils::get_intrinsic_name(x.m_intrinsic_id)
1899+
+ "` is not implemented");
1900+
}
1901+
}
1902+
out += "(" + src + ")";
1903+
src = out;
1904+
}
18771905
};
18781906

18791907
Result<std::string>

src/libasr/pass/intrinsic_function_registry.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ typedef ASR::asr_t* (*create_intrinsic_function)(
4040
enum class IntrinsicFunctions : int64_t {
4141
Sin,
4242
Cos,
43+
Tan,
44+
Asin,
45+
Acos,
46+
Atan,
4347
Gamma,
4448
LogGamma,
4549
Abs,
@@ -266,6 +270,10 @@ namespace X {
266270

267271
create_trig(Sin, sin, sin)
268272
create_trig(Cos, cos, cos)
273+
create_trig(Tan, tan, tan)
274+
create_trig(Asin, asin, asin)
275+
create_trig(Acos, acos, acos)
276+
create_trig(Atan, atan, atan)
269277

270278
namespace Abs {
271279

@@ -514,6 +522,14 @@ namespace IntrinsicFunctionRegistry {
514522
&Sin::instantiate_Sin},
515523
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Cos),
516524
&Cos::instantiate_Cos},
525+
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Tan),
526+
&Tan::instantiate_Tan},
527+
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Asin),
528+
&Asin::instantiate_Asin},
529+
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Acos),
530+
&Acos::instantiate_Acos},
531+
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Atan),
532+
&Atan::instantiate_Atan},
517533
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Abs),
518534
&Abs::instantiate_Abs}
519535
};
@@ -526,6 +542,14 @@ namespace IntrinsicFunctionRegistry {
526542
"sin"},
527543
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Cos),
528544
"cos"},
545+
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Tan),
546+
"tan"},
547+
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Asin),
548+
"asin"},
549+
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Acos),
550+
"acos"},
551+
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Atan),
552+
"atan"},
529553
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Abs),
530554
"abs"},
531555
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::ListIndex),
@@ -538,6 +562,10 @@ namespace IntrinsicFunctionRegistry {
538562
{"log_gamma", {&LogGamma::create_LogGamma, &LogGamma::eval_log_gamma}},
539563
{"sin", {&Sin::create_Sin, &Sin::eval_Sin}},
540564
{"cos", {&Cos::create_Cos, &Cos::eval_Cos}},
565+
{"tan", {&Tan::create_Tan, &Tan::eval_Tan}},
566+
{"asin", {&Asin::create_Asin, &Asin::eval_Asin}},
567+
{"acos", {&Acos::create_Acos, &Acos::eval_Acos}},
568+
{"atan", {&Atan::create_Atan, &Atan::eval_Atan}},
541569
{"abs", {&Abs::create_Abs, &Abs::eval_Abs}},
542570
{"list.index", {&ListIndex::create_ListIndex, &ListIndex::eval_list_index}},
543571
};
@@ -580,9 +608,14 @@ inline std::string get_intrinsic_name(int x) {
580608
switch (x) {
581609
INTRINSIC_NAME_CASE(Sin)
582610
INTRINSIC_NAME_CASE(Cos)
611+
INTRINSIC_NAME_CASE(Tan)
612+
INTRINSIC_NAME_CASE(Asin)
613+
INTRINSIC_NAME_CASE(Acos)
614+
INTRINSIC_NAME_CASE(Atan)
583615
INTRINSIC_NAME_CASE(Gamma)
584616
INTRINSIC_NAME_CASE(LogGamma)
585617
INTRINSIC_NAME_CASE(Abs)
618+
INTRINSIC_NAME_CASE(ListIndex)
586619
default : {
587620
throw LCompilersException("pickle: intrinsic_id not implemented");
588621
}

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6408,7 +6408,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
64086408
}
64096409

64106410
if (!s) {
6411-
std::set<std::string> not_cpython_builtin = {"sin", "cos", "gamma"};
6411+
std::set<std::string> not_cpython_builtin = {
6412+
"sin", "cos", "gamma", "tan", "asin", "acos", "atan"
6413+
};
64126414
if (ASRUtils::IntrinsicFunctionRegistry::is_intrinsic_function(call_name)
64136415
&& not_cpython_builtin.find(call_name) == not_cpython_builtin.end()) {
64146416
ASRUtils::create_intrinsic_function create_func =

tests/reference/asr-test_list_index-6b8f30b.json

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/reference/asr-test_list_index-6b8f30b.stdout

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/reference/cpp-test_builtin_pow-56b3f92.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-test_builtin_pow-56b3f92.stdout",
9-
"stdout_hash": "5166ac6604298274143e45e115e575ddc0650db2dcfd91a332bdfdf4",
9+
"stdout_hash": "f8f9efb8e2d7a1d7d111706825bfbff30d437c52a56d4d42041fc75e",
1010
"stderr": "cpp-test_builtin_pow-56b3f92.stderr",
1111
"stderr_hash": "859ce76c74748f2d32c7eab92cfbba789a78d4cbf5818646b99806ea",
1212
"returncode": 0

tests/reference/cpp-test_builtin_pow-56b3f92.stdout

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ struct dimension_descriptor
2323
int32_t lower_bound, length;
2424
};
2525
// Forward declarations
26-
double _lcompilers_abs_f64(double x);
2726
void _lpython_main_program();
2827
void test_pow();
2928
double __lpython_overloaded_0__pow(int32_t x, int32_t y);
@@ -49,17 +48,6 @@ namespace {
4948
}
5049

5150
// Implementations
52-
double _lcompilers_abs_f64(double x)
53-
{
54-
double _lcompilers_abs_f64;
55-
if (x >= 0.00000000000000000e+00) {
56-
_lcompilers_abs_f64 = x;
57-
} else {
58-
_lcompilers_abs_f64 = -x;
59-
}
60-
return _lcompilers_abs_f64;
61-
}
62-
6351
double __lpython_overloaded_0__pow(int32_t x, int32_t y)
6452
{
6553
double _lpython_return_variable;
@@ -267,26 +255,26 @@ void test_pow()
267255
assert (__lpython_overloaded_8__pow(false, false) == 1);
268256
a1 = 4.50000000000000000e+00;
269257
a2 = 2.29999999999999982e+00;
270-
assert (_lcompilers_abs_f64(__lpython_overloaded_3__pow(a1, a2) - 3.17971929089206000e+01) < eps);
271-
assert (_lcompilers_abs_f64(__lpython_overloaded_3__pow(a2, a1) - 4.24399889427765871e+01) < eps);
258+
assert (abs(__lpython_overloaded_3__pow(a1, a2) - 3.17971929089206000e+01) < eps);
259+
assert (abs(__lpython_overloaded_3__pow(a2, a1) - 4.24399889427765871e+01) < eps);
272260
x = 3;
273261
y = 2.29999999999999982e+00;
274-
assert (_lcompilers_abs_f64(__lpython_overloaded_6__pow(x, y) - 1.25135025328431819e+01) < eps);
275-
assert (_lcompilers_abs_f64(__lpython_overloaded_7__pow(y, x) - 1.21669999999999980e+01) < eps);
276-
assert (_lcompilers_abs_f64(__lpython_overloaded_6__pow(x, 5.50000000000000000e+00) - 4.20888346239237194e+02) < eps);
277-
assert (_lcompilers_abs_f64(__lpython_overloaded_1__pow(2, -1) - 5.00000000000000000e-01) < eps);
278-
assert (_lcompilers_abs_f64(__lpython_overloaded_1__pow(6, -4) - 7.71604938271604895e-04) < eps);
279-
assert (_lcompilers_abs_f64(__lpython_overloaded_1__pow(-3, -5) + 4.11522633744856002e-03) < eps);
280-
assert (_lcompilers_abs_f64(__lpython_overloaded_1__pow(6, -4) - 7.71604938271604895e-04) < eps);
281-
assert (_lcompilers_abs_f64(__lpython_overloaded_3__pow( 4.50000000000000000e+00, 2.29999999999999982e+00) - 3.17971929089206000e+01) < eps);
282-
assert (_lcompilers_abs_f64(__lpython_overloaded_3__pow( 2.29999999999999982e+00, 0.00000000000000000e+00) - 1.00000000000000000e+00) < eps);
283-
assert (_lcompilers_abs_f64(__lpython_overloaded_3__pow( 2.29999999999999982e+00, - 1.50000000000000000e+00) - 2.86687162345994395e-01) < eps);
284-
assert (_lcompilers_abs_f64(__lpython_overloaded_6__pow(2, 3.39999999999999991e+00) - 1.05560632861831536e+01) < eps);
285-
assert (_lcompilers_abs_f64(__lpython_overloaded_6__pow(2, - 3.39999999999999991e+00) - 9.47322854068998882e-02) < eps);
286-
assert (_lcompilers_abs_f64(__lpython_overloaded_7__pow( 3.39999999999999991e+00, 9) - 6.07169927664639836e+04) < eps);
287-
assert (_lcompilers_abs_f64(__lpython_overloaded_7__pow( 0.00000000000000000e+00, 53) - 0.00000000000000000e+00) < eps);
262+
assert (abs(__lpython_overloaded_6__pow(x, y) - 1.25135025328431819e+01) < eps);
263+
assert (abs(__lpython_overloaded_7__pow(y, x) - 1.21669999999999980e+01) < eps);
264+
assert (abs(__lpython_overloaded_6__pow(x, 5.50000000000000000e+00) - 4.20888346239237194e+02) < eps);
265+
assert (abs(__lpython_overloaded_1__pow(2, -1) - 5.00000000000000000e-01) < eps);
266+
assert (abs(__lpython_overloaded_1__pow(6, -4) - 7.71604938271604895e-04) < eps);
267+
assert (abs(__lpython_overloaded_1__pow(-3, -5) + 4.11522633744856002e-03) < eps);
268+
assert (abs(__lpython_overloaded_1__pow(6, -4) - 7.71604938271604895e-04) < eps);
269+
assert (abs(__lpython_overloaded_3__pow( 4.50000000000000000e+00, 2.29999999999999982e+00) - 3.17971929089206000e+01) < eps);
270+
assert (abs(__lpython_overloaded_3__pow( 2.29999999999999982e+00, 0.00000000000000000e+00) - 1.00000000000000000e+00) < eps);
271+
assert (abs(__lpython_overloaded_3__pow( 2.29999999999999982e+00, - 1.50000000000000000e+00) - 2.86687162345994395e-01) < eps);
272+
assert (abs(__lpython_overloaded_6__pow(2, 3.39999999999999991e+00) - 1.05560632861831536e+01) < eps);
273+
assert (abs(__lpython_overloaded_6__pow(2, - 3.39999999999999991e+00) - 9.47322854068998882e-02) < eps);
274+
assert (abs(__lpython_overloaded_7__pow( 3.39999999999999991e+00, 9) - 6.07169927664639836e+04) < eps);
275+
assert (abs(__lpython_overloaded_7__pow( 0.00000000000000000e+00, 53) - 0.00000000000000000e+00) < eps);
288276
assert ((int32_t)(__lpython_overloaded_0__pow(4, 2)) == 16);
289-
assert (_lcompilers_abs_f64(__lpython_overloaded_7__pow(- 4.23500000000000000e+03, 52) - 3.94800380598526379e+188) < eps);
277+
assert (abs(__lpython_overloaded_7__pow(- 4.23500000000000000e+03, 52) - 3.94800380598526379e+188) < eps);
290278
i = 7;
291279
j = 2;
292280
k = 5;

0 commit comments

Comments
 (0)