Skip to content

Update SignFromValue pass #2317

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

Draft
wants to merge 4 commits into
base: main
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
20 changes: 18 additions & 2 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,17 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
generate_fma(args.p);
break;
}
case ASRUtils::IntrinsicScalarFunctions::SignFromValue: {
Vec<ASR::call_arg_t> args;
args.reserve(al, 2);
ASR::call_arg_t arg0_, arg1_;
arg0_.loc = x.m_args[0]->base.loc, arg0_.m_value = x.m_args[0];
args.push_back(al, arg0_);
arg1_.loc = x.m_args[1]->base.loc, arg1_.m_value = x.m_args[1];
args.push_back(al, arg1_);
generate_sign_from_value(args.p);
break;
}
default: {
throw CodeGenError( ASRUtils::IntrinsicScalarFunctionRegistry::
get_intrinsic_function_name(x.m_intrinsic_id) +
Expand Down Expand Up @@ -8325,8 +8336,13 @@ Result<std::unique_ptr<LLVMModule>> asr_to_llvm(ASR::TranslationUnit_t &asr,
pass_options.always_run = false;
pass_options.verbose = co.verbose;
std::vector<int64_t> skip_optimization_func_instantiation;
skip_optimization_func_instantiation.push_back(static_cast<int64_t>(ASRUtils::IntrinsicScalarFunctions::FlipSign));
skip_optimization_func_instantiation.push_back(static_cast<int64_t>(ASRUtils::IntrinsicScalarFunctions::FMA));
skip_optimization_func_instantiation.push_back(static_cast<int64_t>(
ASRUtils::IntrinsicScalarFunctions::FlipSign));
skip_optimization_func_instantiation.push_back(static_cast<int64_t>(
ASRUtils::IntrinsicScalarFunctions::FMA));
skip_optimization_func_instantiation.push_back(static_cast<int64_t>(
ASRUtils::IntrinsicScalarFunctions::SignFromValue));

pass_options.skip_optimization_func_instantiation = skip_optimization_func_instantiation;
pass_manager.rtlib = co.rtlib;

Expand Down
101 changes: 101 additions & 0 deletions src/libasr/pass/intrinsic_function_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ enum class IntrinsicScalarFunctions : int64_t {
Max,
Min,
Sign,
SignFromValue,
SymbolicSymbol,
SymbolicAdd,
SymbolicSub,
Expand Down Expand Up @@ -109,6 +110,7 @@ inline std::string get_intrinsic_name(int x) {
INTRINSIC_NAME_CASE(Max)
INTRINSIC_NAME_CASE(Min)
INTRINSIC_NAME_CASE(Sign)
INTRINSIC_NAME_CASE(SignFromValue)
INTRINSIC_NAME_CASE(SymbolicSymbol)
INTRINSIC_NAME_CASE(SymbolicAdd)
INTRINSIC_NAME_CASE(SymbolicSub)
Expand Down Expand Up @@ -1345,6 +1347,101 @@ namespace FMA {

} // namespace FMA

namespace SignFromValue {

static inline void verify_args(const ASR::IntrinsicScalarFunction_t& x, diag::Diagnostics& diagnostics) {
ASRUtils::require_impl(x.n_args == 2,
"ASR Verify: Call to SignFromValue must have exactly 2 arguments",
x.base.base.loc, diagnostics);
ASR::ttype_t *type1 = ASRUtils::expr_type(x.m_args[0]);
ASR::ttype_t *type2 = ASRUtils::expr_type(x.m_args[1]);
bool eq_type = ASRUtils::types_equal(type1, type2);
ASRUtils::require_impl(((is_real(*type1) || is_integer(*type1)) &&
(is_real(*type2) || is_integer(*type2)) && eq_type),
"ASR Verify: Arguments to SignFromValue must be of equal type and "
"should be either real or integer",
x.base.base.loc, diagnostics);
}

static ASR::expr_t *eval_SignFromValue(Allocator &al, const Location &loc,
ASR::ttype_t* t1, Vec<ASR::expr_t*> &args) {
if (is_real(*t1)) {
double a = ASR::down_cast<ASR::RealConstant_t>(args[0])->m_r;
double b = ASR::down_cast<ASR::RealConstant_t>(args[1])->m_r;
a = (b < 0 ? -a : a);
return make_ConstantWithType(make_RealConstant_t, a, t1, loc);
}
int64_t a = ASR::down_cast<ASR::IntegerConstant_t>(args[0])->m_n;
int64_t b = ASR::down_cast<ASR::IntegerConstant_t>(args[1])->m_n;
a = (b < 0 ? -a : a);
return make_ConstantWithType(make_IntegerConstant_t, a, t1, loc);

}

static inline ASR::asr_t* create_SignFromValue(Allocator& al, const Location& loc,
Vec<ASR::expr_t*>& args,
const std::function<void (const std::string &, const Location &)> err) {
if (args.size() != 2) {
err("Intrinsic SignFromValue function accepts exactly 2 arguments", loc);
}
ASR::ttype_t *type1 = ASRUtils::expr_type(args[0]);
ASR::ttype_t *type2 = ASRUtils::expr_type(args[1]);
bool eq_type = ASRUtils::types_equal(type1, type2);
if (!((is_real(*type1) || is_integer(*type1)) &&
(is_real(*type2) || is_integer(*type2)) && eq_type)) {
err("Argument of the SignFromValue function must be either Real or Integer "
"and must be of equal type",
args[0]->base.loc);
}
ASR::expr_t *m_value = nullptr;
if (all_args_evaluated(args)) {
Vec<ASR::expr_t*> arg_values; arg_values.reserve(al, 2);
arg_values.push_back(al, expr_value(args[0]));
arg_values.push_back(al, expr_value(args[1]));
m_value = eval_SignFromValue(al, loc, expr_type(args[0]), arg_values);
}
return ASR::make_IntrinsicScalarFunction_t(al, loc,
static_cast<int64_t>(IntrinsicScalarFunctions::SignFromValue),
args.p, args.n, 0, ASRUtils::expr_type(args[0]), m_value);
}

static inline ASR::expr_t* instantiate_SignFromValue(Allocator &al, const Location &loc,
SymbolTable *scope, Vec<ASR::ttype_t*>& arg_types, ASR::ttype_t *return_type,
Vec<ASR::call_arg_t>& new_args, int64_t /*overload_id*/) {
declare_basic_variables("_lcompilers_optimization_signfromvalue_" + type_to_str_python(arg_types[0]));
fill_func_arg("a", arg_types[0]);
fill_func_arg("b", arg_types[1]);
auto result = declare(fn_name, return_type, ReturnVar);
/*
elemental real(real32) function signfromvaluer32r32(a, b) result(d)
real(real32), intent(in) :: a, b
d = a * asignr32(1.0_real32, b)
end function
*/
if (is_real(*arg_types[0])) {
ASR::expr_t *zero = f(0.0, arg_types[1]);
body.push_back(al, b.If(fLt(args[1], zero), {
b.Assignment(result, f32_neg(args[0], arg_types[0]))
}, {
b.Assignment(result, args[0])
}));
} else {
ASR::expr_t *zero = i(0, arg_types[1]);
body.push_back(al, b.If(iLt(args[1], zero), {
b.Assignment(result, i32_neg(args[0], arg_types[0]))
}, {
b.Assignment(result, args[0])
}));
}
ASR::symbol_t *f_sym = make_Function_t(fn_name, fn_symtab, dep, args,
body, result, Source, Implementation, nullptr);
scope->add_symbol(fn_name, f_sym);
return b.Call(f_sym, new_args, return_type, nullptr);
}

} // namespace SignFromValue


namespace FlipSign {

static inline void verify_args(const ASR::IntrinsicScalarFunction_t& x, diag::Diagnostics& diagnostics) {
Expand Down Expand Up @@ -2452,6 +2549,8 @@ namespace IntrinsicScalarFunctionRegistry {
{&FMA::instantiate_FMA, &FMA::verify_args}},
{static_cast<int64_t>(IntrinsicScalarFunctions::FlipSign),
{&FlipSign::instantiate_FlipSign, &FlipSign::verify_args}},
{static_cast<int64_t>(IntrinsicScalarFunctions::SignFromValue),
{&SignFromValue::instantiate_SignFromValue, &SignFromValue::verify_args}},
{static_cast<int64_t>(IntrinsicScalarFunctions::Abs),
{&Abs::instantiate_Abs, &Abs::verify_args}},
{static_cast<int64_t>(IntrinsicScalarFunctions::Partition),
Expand Down Expand Up @@ -2542,6 +2641,8 @@ namespace IntrinsicScalarFunctionRegistry {
"fma"},
{static_cast<int64_t>(IntrinsicScalarFunctions::FlipSign),
"flipsign"},
{static_cast<int64_t>(IntrinsicScalarFunctions::SignFromValue),
"signfromvalue"},
{static_cast<int64_t>(IntrinsicScalarFunctions::Expm1),
"expm1"},
{static_cast<int64_t>(IntrinsicScalarFunctions::ListIndex),
Expand Down
33 changes: 23 additions & 10 deletions src/libasr/pass/pass_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ namespace LCompilers {

ASR::expr_t* get_flipsign(ASR::expr_t* arg0, ASR::expr_t* arg1,
Allocator& al, ASR::TranslationUnit_t& unit, const Location& loc,
PassOptions pass_options){
PassOptions& pass_options){
ASR::ttype_t* type = ASRUtils::expr_type(arg1);
int64_t fp_s = static_cast<int64_t>(ASRUtils::IntrinsicScalarFunctions::FlipSign);
if (skip_instantiation(pass_options, fp_s)) {
Expand Down Expand Up @@ -688,7 +688,7 @@ namespace LCompilers {

ASR::expr_t* get_fma(ASR::expr_t* arg0, ASR::expr_t* arg1, ASR::expr_t* arg2,
Allocator& al, ASR::TranslationUnit_t& unit, Location& loc,
PassOptions pass_options){
PassOptions& pass_options){
int64_t fma_id = static_cast<int64_t>(ASRUtils::IntrinsicScalarFunctions::FMA);
ASR::ttype_t* type = ASRUtils::expr_type(arg0);
if (skip_instantiation(pass_options, fma_id)) {
Expand Down Expand Up @@ -818,21 +818,34 @@ namespace LCompilers {
}

ASR::expr_t* get_sign_from_value(ASR::expr_t* arg0, ASR::expr_t* arg1,
Allocator& al, ASR::TranslationUnit_t& unit, LCompilers::PassOptions& pass_options,
SymbolTable*& current_scope, Location& loc,
const std::function<void (const std::string &, const Location &)> err) {
ASR::symbol_t *v = import_generic_procedure("sign_from_value", "lfortran_intrinsic_optimization",
al, unit, pass_options, current_scope, arg0->base.loc);
Allocator& al, ASR::TranslationUnit_t& unit, Location& loc,
PassOptions& pass_options) {
int64_t sfv_id = static_cast<int64_t>(ASRUtils::IntrinsicScalarFunctions::SignFromValue);
ASR::ttype_t* type = ASRUtils::expr_type(arg0);
if (skip_instantiation(pass_options, sfv_id)) {
Vec<ASR::expr_t*> args;
args.reserve(al, 2);
args.push_back(al, arg0);
args.push_back(al, arg1);
return ASRUtils::EXPR(ASRUtils::make_IntrinsicScalarFunction_t_util(al, loc, sfv_id,
args.p, args.n, 0, type, nullptr));
}
ASRUtils::impl_function instantiate_function =
ASRUtils::IntrinsicScalarFunctionRegistry::get_instantiate_function(
static_cast<int64_t>(ASRUtils::IntrinsicScalarFunctions::FMA));
Vec<ASR::ttype_t*> arg_types;
arg_types.reserve(al, 2);
arg_types.push_back(al, ASRUtils::expr_type(arg0));
arg_types.push_back(al, ASRUtils::expr_type(arg1));
Vec<ASR::call_arg_t> args;
args.reserve(al, 2);
ASR::call_arg_t arg0_, arg1_;
arg0_.loc = arg0->base.loc, arg0_.m_value = arg0;
args.push_back(al, arg0_);
arg1_.loc = arg1->base.loc, arg1_.m_value = arg1;
args.push_back(al, arg1_);
return ASRUtils::EXPR(
ASRUtils::symbol_resolve_external_generic_procedure_without_eval(
loc, v, args, current_scope, al, err));
return instantiate_function(al, loc,
unit.m_global_scope, arg_types, type, args, 0);
}

Vec<ASR::stmt_t*> replace_doloop(Allocator &al, const ASR::DoLoop_t &loop,
Expand Down
8 changes: 3 additions & 5 deletions src/libasr/pass/pass_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace LCompilers {

ASR::expr_t* get_flipsign(ASR::expr_t* arg0, ASR::expr_t* arg1,
Allocator& al, ASR::TranslationUnit_t& unit, const Location& loc,
PassOptions pass_options);
PassOptions& pass_options);

ASR::expr_t* to_int32(ASR::expr_t* x, ASR::ttype_t* int32type, Allocator& al);

Expand All @@ -88,13 +88,11 @@ namespace LCompilers {

ASR::expr_t* get_fma(ASR::expr_t* arg0, ASR::expr_t* arg1, ASR::expr_t* arg2,
Allocator& al, ASR::TranslationUnit_t& unit, Location& loc,
PassOptions pass_options);
PassOptions& pass_options);

ASR::expr_t* get_sign_from_value(ASR::expr_t* arg0, ASR::expr_t* arg1,
Allocator& al, ASR::TranslationUnit_t& unit,
LCompilers::PassOptions& pass_options,
SymbolTable*& current_scope, Location& loc,
const std::function<void (const std::string &, const Location &)> err);
Location& loc, PassOptions& pass_options);

ASR::stmt_t* get_vector_copy(ASR::expr_t* array0, ASR::expr_t* array1, ASR::expr_t* start,
ASR::expr_t* end, ASR::expr_t* step, ASR::expr_t* vector_length,
Expand Down
3 changes: 1 addition & 2 deletions src/libasr/pass/sign_from_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ class SignFromValueVisitor : public PassUtils::SkipOptimizationFunctionVisitor<S
}

sign_from_value_var = PassUtils::get_sign_from_value(first_arg, second_arg,
al, unit, pass_options, current_scope, x.base.base.loc,
[&](const std::string &msg, const Location &) { throw LCompilersException(msg); });
al, unit, x.base.base.loc, pass_options);
from_sign_from_value = false;
}

Expand Down