Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 68aa696

Browse files
Merge pull request #2287 from Thirumalai-Shaktivel/intrinsics_07
2 parents 8c42eaf + 6991154 commit 68aa696

File tree

4 files changed

+61
-117
lines changed

4 files changed

+61
-117
lines changed

src/libasr/pass/intrinsic_array_function_registry.h

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -809,11 +809,7 @@ namespace Shape {
809809

810810
static inline ASR::expr_t* instantiate_Shape(Allocator &al,
811811
const Location &loc, SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types,
812-
ASR::ttype_t *return_type, Vec<ASR::call_arg_t>& new_args,
813-
int64_t, ASR::expr_t* compile_time_value) {
814-
if (compile_time_value) {
815-
return compile_time_value;
816-
}
812+
ASR::ttype_t *return_type, Vec<ASR::call_arg_t>& new_args, int64_t) {
817813
declare_basic_variables("_lcompilers_shape");
818814
fill_func_arg("source", arg_types[0]);
819815
auto result = declare(fn_name, return_type, ReturnVar);
@@ -1020,11 +1016,7 @@ namespace Any {
10201016

10211017
static inline ASR::expr_t* instantiate_Any(Allocator &al, const Location &loc,
10221018
SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types, ASR::ttype_t *logical_return_type,
1023-
Vec<ASR::call_arg_t>& new_args, int64_t overload_id,
1024-
ASR::expr_t* compile_time_value) {
1025-
if (compile_time_value) {
1026-
return compile_time_value;
1027-
}
1019+
Vec<ASR::call_arg_t>& new_args, int64_t overload_id) {
10281020
ASRBuilder builder(al, loc);
10291021
ASRBuilder& b = builder;
10301022
ASR::ttype_t* arg_type = arg_types[0];
@@ -1142,10 +1134,7 @@ namespace Sum {
11421134
static inline ASR::expr_t* instantiate_Sum(Allocator &al,
11431135
const Location &loc, SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types,
11441136
ASR::ttype_t *return_type, Vec<ASR::call_arg_t>& new_args,
1145-
int64_t overload_id, ASR::expr_t* compile_time_value) {
1146-
if (compile_time_value) {
1147-
return compile_time_value;
1148-
}
1137+
int64_t overload_id) {
11491138
return ArrIntrinsic::instantiate_ArrIntrinsic(al, loc, scope, arg_types,
11501139
return_type, new_args, overload_id, IntrinsicArrayFunctions::Sum,
11511140
&get_constant_zero_with_given_type, &ASRBuilder::ElementalAdd);
@@ -1176,10 +1165,7 @@ namespace Product {
11761165
static inline ASR::expr_t* instantiate_Product(Allocator &al,
11771166
const Location &loc, SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types,
11781167
ASR::ttype_t *return_type, Vec<ASR::call_arg_t>& new_args,
1179-
int64_t overload_id, ASR::expr_t* compile_time_value) {
1180-
if (compile_time_value) {
1181-
return compile_time_value;
1182-
}
1168+
int64_t overload_id) {
11831169
return ArrIntrinsic::instantiate_ArrIntrinsic(al, loc, scope, arg_types,
11841170
return_type, new_args, overload_id, IntrinsicArrayFunctions::Product,
11851171
&get_constant_one_with_given_type, &ASRBuilder::ElementalMul);
@@ -1210,10 +1196,7 @@ namespace MaxVal {
12101196
static inline ASR::expr_t* instantiate_MaxVal(Allocator &al,
12111197
const Location &loc, SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types,
12121198
ASR::ttype_t *return_type, Vec<ASR::call_arg_t>& new_args,
1213-
int64_t overload_id, ASR::expr_t* compile_time_value) {
1214-
if (compile_time_value) {
1215-
return compile_time_value;
1216-
}
1199+
int64_t overload_id) {
12171200
return ArrIntrinsic::instantiate_ArrIntrinsic(al, loc, scope, arg_types,
12181201
return_type, new_args, overload_id, IntrinsicArrayFunctions::MaxVal,
12191202
&get_minimum_value_with_given_type, &ASRBuilder::ElementalMax);
@@ -1238,11 +1221,7 @@ namespace MaxLoc {
12381221
static inline ASR::expr_t *instantiate_MaxLoc(Allocator &al,
12391222
const Location &loc, SymbolTable *scope,
12401223
Vec<ASR::ttype_t*>& arg_types, ASR::ttype_t *return_type,
1241-
Vec<ASR::call_arg_t>& m_args, int64_t overload_id,
1242-
ASR::expr_t* compile_time_value) {
1243-
if (compile_time_value) {
1244-
return compile_time_value;
1245-
}
1224+
Vec<ASR::call_arg_t>& m_args, int64_t overload_id) {
12461225
return ArrIntrinsic::instantiate_MaxMinLoc(al, loc, scope,
12471226
static_cast<int>(IntrinsicArrayFunctions::MaxLoc), arg_types, return_type,
12481227
m_args, overload_id);
@@ -1366,11 +1345,7 @@ namespace Merge {
13661345
static inline ASR::expr_t* instantiate_Merge(Allocator &al,
13671346
const Location &loc, SymbolTable *scope,
13681347
Vec<ASR::ttype_t*>& arg_types, ASR::ttype_t *return_type,
1369-
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/,
1370-
ASR::expr_t* compile_time_value) {
1371-
if (compile_time_value) {
1372-
return compile_time_value;
1373-
}
1348+
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/) {
13741349
LCOMPILERS_ASSERT(arg_types.size() == 3);
13751350

13761351
// Array inputs should be elementalised in array_op pass already
@@ -1442,10 +1417,7 @@ namespace MinVal {
14421417
static inline ASR::expr_t* instantiate_MinVal(Allocator &al,
14431418
const Location &loc, SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types,
14441419
ASR::ttype_t *return_type, Vec<ASR::call_arg_t>& new_args,
1445-
int64_t overload_id, ASR::expr_t* compile_time_value) {
1446-
if (compile_time_value) {
1447-
return compile_time_value;
1448-
}
1420+
int64_t overload_id) {
14491421
return ArrIntrinsic::instantiate_ArrIntrinsic(al, loc, scope, arg_types,
14501422
return_type, new_args, overload_id, IntrinsicArrayFunctions::MinVal,
14511423
&get_maximum_value_with_given_type, &ASRBuilder::ElementalMin);
@@ -1470,11 +1442,7 @@ namespace MinLoc {
14701442
static inline ASR::expr_t *instantiate_MinLoc(Allocator &al,
14711443
const Location &loc, SymbolTable *scope,
14721444
Vec<ASR::ttype_t*>& arg_types, ASR::ttype_t *return_type,
1473-
Vec<ASR::call_arg_t>& m_args, int64_t overload_id,
1474-
ASR::expr_t* compile_time_value) {
1475-
if (compile_time_value) {
1476-
return compile_time_value;
1477-
}
1445+
Vec<ASR::call_arg_t>& m_args, int64_t overload_id) {
14781446
return ArrIntrinsic::instantiate_MaxMinLoc(al, loc, scope,
14791447
static_cast<int>(IntrinsicArrayFunctions::MinLoc), arg_types, return_type,
14801448
m_args, overload_id);

src/libasr/pass/intrinsic_function.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,21 @@ class ReplaceIntrinsicFunctions: public ASR::BaseExprReplacer<ReplaceIntrinsicFu
4141

4242

4343
void replace_IntrinsicScalarFunction(ASR::IntrinsicScalarFunction_t* x) {
44-
Vec<ASR::call_arg_t> new_args;
44+
if (x->m_value) {
45+
*current_expr = x->m_value;
46+
return;
47+
}
48+
Vec<ASR::call_arg_t> new_args; new_args.reserve(al, x->n_args);
4549
// Replace any IntrinsicScalarFunctions in the argument first:
46-
{
47-
new_args.reserve(al, x->n_args);
48-
for( size_t i = 0; i < x->n_args; i++ ) {
49-
ASR::expr_t** current_expr_copy_ = current_expr;
50-
current_expr = &(x->m_args[i]);
51-
replace_expr(x->m_args[i]);
52-
ASR::call_arg_t arg0;
53-
arg0.loc = (*current_expr)->base.loc;
54-
arg0.m_value = *current_expr; // Use the converted arg
55-
new_args.push_back(al, arg0);
56-
current_expr = current_expr_copy_;
57-
}
50+
for( size_t i = 0; i < x->n_args; i++ ) {
51+
ASR::expr_t** current_expr_copy_ = current_expr;
52+
current_expr = &(x->m_args[i]);
53+
replace_expr(x->m_args[i]);
54+
ASR::call_arg_t arg0;
55+
arg0.loc = (*current_expr)->base.loc;
56+
arg0.m_value = *current_expr; // Use the converted arg
57+
new_args.push_back(al, arg0);
58+
current_expr = current_expr_copy_;
5859
}
5960
// TODO: currently we always instantiate a new function.
6061
// Rather we should reuse the old instantiation if it has
@@ -73,7 +74,7 @@ class ReplaceIntrinsicFunctions: public ASR::BaseExprReplacer<ReplaceIntrinsicFu
7374
arg_types.push_back(al, ASRUtils::expr_type(x->m_args[i]));
7475
}
7576
ASR::expr_t* current_expr_ = instantiate_function(al, x->base.base.loc,
76-
global_scope, arg_types, x->m_type, new_args, x->m_overload_id, x->m_value);
77+
global_scope, arg_types, x->m_type, new_args, x->m_overload_id);
7778
if( ASR::is_a<ASR::ArrayPhysicalCast_t>(*(*current_expr)) ) {
7879
ASR::ArrayPhysicalCast_t* array_physical_cast_t = ASR::down_cast<ASR::ArrayPhysicalCast_t>(*current_expr);
7980
array_physical_cast_t->m_arg = current_expr_;
@@ -83,21 +84,23 @@ class ReplaceIntrinsicFunctions: public ASR::BaseExprReplacer<ReplaceIntrinsicFu
8384
}
8485

8586
void replace_IntrinsicArrayFunction(ASR::IntrinsicArrayFunction_t* x) {
86-
Vec<ASR::call_arg_t> new_args;
87+
if (x->m_value) {
88+
*current_expr = x->m_value;
89+
return;
90+
}
91+
Vec<ASR::call_arg_t> new_args; new_args.reserve(al, x->n_args);
8792
// Replace any IntrinsicArrayFunctions in the argument first:
88-
{
89-
new_args.reserve(al, x->n_args);
90-
for( size_t i = 0; i < x->n_args; i++ ) {
91-
ASR::expr_t** current_expr_copy_ = current_expr;
92-
current_expr = &(x->m_args[i]);
93-
replace_expr(x->m_args[i]);
94-
ASR::call_arg_t arg0;
95-
arg0.loc = (*current_expr)->base.loc;
96-
arg0.m_value = *current_expr; // Use the converted arg
97-
new_args.push_back(al, arg0);
98-
current_expr = current_expr_copy_;
99-
}
93+
for( size_t i = 0; i < x->n_args; i++ ) {
94+
ASR::expr_t** current_expr_copy_ = current_expr;
95+
current_expr = &(x->m_args[i]);
96+
replace_expr(x->m_args[i]);
97+
ASR::call_arg_t arg0;
98+
arg0.loc = (*current_expr)->base.loc;
99+
arg0.m_value = *current_expr; // Use the converted arg
100+
new_args.push_back(al, arg0);
101+
current_expr = current_expr_copy_;
100102
}
103+
101104
// TODO: currently we always instantiate a new function.
102105
// Rather we should reuse the old instantiation if it has
103106
// exactly the same arguments. For that we could use the
@@ -115,7 +118,7 @@ class ReplaceIntrinsicFunctions: public ASR::BaseExprReplacer<ReplaceIntrinsicFu
115118
arg_types.push_back(al, ASRUtils::expr_type(x->m_args[i]));
116119
}
117120
ASR::expr_t* current_expr_ = instantiate_function(al, x->base.base.loc,
118-
global_scope, arg_types, x->m_type, new_args, x->m_overload_id, x->m_value);
121+
global_scope, arg_types, x->m_type, new_args, x->m_overload_id);
119122
ASR::expr_t* func_call = current_expr_;
120123
if( ASR::is_a<ASR::ArrayPhysicalCast_t>(*(*current_expr)) ) {
121124
ASR::ArrayPhysicalCast_t* array_physical_cast_t = ASR::down_cast<ASR::ArrayPhysicalCast_t>(*current_expr);

src/libasr/pass/intrinsic_function_registry.h

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ inline std::string get_intrinsic_name(int x) {
131131
typedef ASR::expr_t* (*impl_function)(
132132
Allocator&, const Location &,
133133
SymbolTable*, Vec<ASR::ttype_t*>&, ASR::ttype_t *,
134-
Vec<ASR::call_arg_t>&, int64_t, ASR::expr_t*);
134+
Vec<ASR::call_arg_t>&, int64_t);
135135

136136
typedef ASR::expr_t* (*eval_intrinsic_function)(
137137
Allocator&, const Location &, ASR::ttype_t *,
@@ -709,11 +709,7 @@ namespace UnaryIntrinsicFunction {
709709
static inline ASR::expr_t* instantiate_functions(Allocator &al,
710710
const Location &loc, SymbolTable *scope, std::string new_name,
711711
ASR::ttype_t *arg_type, ASR::ttype_t *return_type,
712-
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/,
713-
ASR::expr_t *value) {
714-
if (value) {
715-
return value;
716-
}
712+
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/) {
717713
std::string c_func_name;
718714
switch (arg_type->type) {
719715
case ASR::ttypeType::Complex : {
@@ -738,7 +734,7 @@ static inline ASR::expr_t* instantiate_functions(Allocator &al,
738734
if (scope->get_symbol(new_name)) {
739735
ASR::symbol_t *s = scope->get_symbol(new_name);
740736
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(s);
741-
return b.Call(s, new_args, expr_type(f->m_return_var), value);
737+
return b.Call(s, new_args, expr_type(f->m_return_var));
742738
}
743739
fill_func_arg("x", arg_type);
744740
auto result = declare(new_name, return_type, ReturnVar);
@@ -768,7 +764,7 @@ static inline ASR::expr_t* instantiate_functions(Allocator &al,
768764
ASR::symbol_t *new_symbol = make_Function_t(fn_name, fn_symtab, dep, args,
769765
body, result, Source, Implementation, nullptr);
770766
scope->add_symbol(fn_name, new_symbol);
771-
return b.Call(new_symbol, new_args, return_type, value);
767+
return b.Call(new_symbol, new_args, return_type);
772768
}
773769

774770
static inline ASR::asr_t* create_UnaryFunction(Allocator& al, const Location& loc,
@@ -904,7 +900,9 @@ static inline ASR::asr_t* create_LogGamma(Allocator& al, const Location& loc,
904900
const std::function<void (const std::string &, const Location &)> err) {
905901
ASR::ttype_t *type = ASRUtils::expr_type(args[0]);
906902

907-
if (!ASRUtils::is_real(*type)) {
903+
if (args.n != 1) {
904+
err("Intrinsic `log_gamma` accepts exactly one argument", loc);
905+
} else if (!ASRUtils::is_real(*type)) {
908906
err("`x` argument of `log_gamma` must be real",
909907
args[0]->base.loc);
910908
}
@@ -917,12 +915,11 @@ static inline ASR::asr_t* create_LogGamma(Allocator& al, const Location& loc,
917915
static inline ASR::expr_t* instantiate_LogGamma (Allocator &al,
918916
const Location &loc, SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types,
919917
ASR::ttype_t *return_type, Vec<ASR::call_arg_t>& new_args,
920-
int64_t overload_id,ASR::expr_t* compile_time_value) {
921-
if (compile_time_value) return compile_time_value;
918+
int64_t overload_id) {
922919
LCOMPILERS_ASSERT(arg_types.size() == 1);
923920
ASR::ttype_t* arg_type = arg_types[0];
924921
return UnaryIntrinsicFunction::instantiate_functions(al, loc, scope,
925-
"log_gamma", arg_type, return_type, new_args, overload_id, nullptr);
922+
"log_gamma", arg_type, return_type, new_args, overload_id);
926923
}
927924

928925
} // namespace LogGamma
@@ -956,7 +953,9 @@ namespace X {
956953
const std::function<void (const std::string &, const Location &)> err) \
957954
{ \
958955
ASR::ttype_t *type = ASRUtils::expr_type(args[0]); \
959-
if (!ASRUtils::is_real(*type) && !ASRUtils::is_complex(*type)) { \
956+
if (args.n != 1) { \
957+
err("Intrinsic `"#X"` accepts exactly one argument", loc); \
958+
} else if (!ASRUtils::is_real(*type) && !ASRUtils::is_complex(*type)) { \
960959
err("`x` argument of `"#X"` must be real or complex", \
961960
args[0]->base.loc); \
962961
} \
@@ -967,14 +966,10 @@ namespace X {
967966
static inline ASR::expr_t* instantiate_##X (Allocator &al, \
968967
const Location &loc, SymbolTable *scope, \
969968
Vec<ASR::ttype_t*>& arg_types, ASR::ttype_t *return_type, \
970-
Vec<ASR::call_arg_t>& new_args,int64_t overload_id, \
971-
ASR::expr_t* compile_time_value) { \
972-
if (compile_time_value) return compile_time_value; \
973-
LCOMPILERS_ASSERT(arg_types.size() == 1); \
969+
Vec<ASR::call_arg_t>& new_args,int64_t overload_id) { \
974970
ASR::ttype_t* arg_type = arg_types[0]; \
975971
return UnaryIntrinsicFunction::instantiate_functions(al, loc, scope, \
976-
#lcompilers_name, arg_type, return_type, new_args, overload_id, \
977-
nullptr); \
972+
#lcompilers_name, arg_type, return_type, new_args, overload_id); \
978973
} \
979974
} // namespace X
980975

@@ -1068,10 +1063,7 @@ namespace Abs {
10681063

10691064
static inline ASR::expr_t* instantiate_Abs(Allocator &al, const Location &loc,
10701065
SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types, ASR::ttype_t *return_type,
1071-
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/, ASR::expr_t* compile_time_value) {
1072-
if (compile_time_value) {
1073-
return compile_time_value;
1074-
}
1066+
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/) {
10751067
std::string func_name = "_lcompilers_abs_" + type_to_str_python(arg_types[0]);
10761068
declare_basic_variables(func_name);
10771069
if (scope->get_symbol(func_name)) {
@@ -1238,11 +1230,7 @@ namespace Sign {
12381230

12391231
static inline ASR::expr_t* instantiate_Sign(Allocator &al, const Location &loc,
12401232
SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types, ASR::ttype_t *return_type,
1241-
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/,
1242-
ASR::expr_t* compile_time_value) {
1243-
if (compile_time_value) {
1244-
return compile_time_value;
1245-
}
1233+
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/) {
12461234
declare_basic_variables("_lcompilers_sign_" + type_to_str_python(arg_types[0]));
12471235
fill_func_arg("x", arg_types[0]);
12481236
fill_func_arg("y", arg_types[0]);
@@ -1333,11 +1321,7 @@ namespace FMA {
13331321

13341322
static inline ASR::expr_t* instantiate_FMA(Allocator &al, const Location &loc,
13351323
SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types, ASR::ttype_t *return_type,
1336-
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/,
1337-
ASR::expr_t* compile_time_value) {
1338-
if (compile_time_value) {
1339-
return compile_time_value;
1340-
}
1324+
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/) {
13411325
declare_basic_variables("_lcompilers_optimization_fma_" + type_to_str_python(arg_types[0]));
13421326
fill_func_arg("a", arg_types[0]);
13431327
fill_func_arg("b", arg_types[0]);
@@ -1880,10 +1864,7 @@ namespace Max {
18801864

18811865
static inline ASR::expr_t* instantiate_Max(Allocator &al, const Location &loc,
18821866
SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types, ASR::ttype_t *return_type,
1883-
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/, ASR::expr_t* compile_time_value) {
1884-
if (compile_time_value) {
1885-
return compile_time_value;
1886-
}
1867+
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/) {
18871868
std::string func_name = "_lcompilers_max0_" + type_to_str_python(arg_types[0]);
18881869
std::string fn_name = scope->get_unique_name(func_name);
18891870
SymbolTable *fn_symtab = al.make_new<SymbolTable>(scope);
@@ -1995,10 +1976,7 @@ namespace Min {
19951976

19961977
static inline ASR::expr_t* instantiate_Min(Allocator &al, const Location &loc,
19971978
SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types, ASR::ttype_t *return_type,
1998-
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/, ASR::expr_t* compile_time_value) {
1999-
if (compile_time_value) {
2000-
return compile_time_value;
2001-
}
1979+
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/) {
20021980
std::string func_name = "_lcompilers_min0_" + type_to_str_python(arg_types[0]);
20031981
std::string fn_name = scope->get_unique_name(func_name);
20041982
SymbolTable *fn_symtab = al.make_new<SymbolTable>(scope);
@@ -2130,11 +2108,7 @@ namespace Partition {
21302108
static inline ASR::expr_t *instantiate_Partition(Allocator &al,
21312109
const Location &loc, SymbolTable *scope,
21322110
Vec<ASR::ttype_t*>& /*arg_types*/, ASR::ttype_t *return_type,
2133-
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/,
2134-
ASR::expr_t* compile_time_value) {
2135-
if (compile_time_value) {
2136-
return compile_time_value;
2137-
}
2111+
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/) {
21382112
// TODO: show runtime error for empty separator or pattern
21392113
declare_basic_variables("_lpython_str_partition");
21402114
fill_func_arg("target_string", character(-2));

0 commit comments

Comments
 (0)