Skip to content
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

ENH: support runtime dim for intrinsic sum #3617

Merged
merged 5 commits into from
Mar 12, 2024

Conversation

Pranavchiku
Copy link
Contributor

Fixes #3540

@Pranavchiku Pranavchiku added the stdlib Issues related to compiling fortran-lang/stdlib label Mar 10, 2024
}
}
return ret_type;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be same, I do not think there can be any other variations possible for this function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this function do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensures return type do not have variables from other scope. As it is passed from create_Sum to instantiate_Sum, it has parameters from different scope.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you document this as a comment?

Comment on lines +294 to +295
if (func2intrinsicid[x_m_name] == ASRUtils::IntrinsicArrayFunctions::Sum) {
PassUtils::allocate_res_var(al, x, new_args, result_var_, pass_result, {0, 0, 1});
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will have to map corresponding arguments based on intrinsic function we are replacing.

@Pranavchiku Pranavchiku marked this pull request as ready for review March 10, 2024 12:18
return_char->m_len = -2; return_char->m_len_expr = nullptr;

}
std::string new_name = "_lcompilers_merge_" + get_type_code(tsource_type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated: I wonder if merge can be just an elemental function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge is an elemental function.


ASR::stmt_t* allocate_stmt = ASRUtils::STMT(ASR::make_Allocate_t(al,
x->base.base.loc, alloc_args.p, alloc_args.n, nullptr, nullptr, nullptr));
pass_result.push_back(al, allocate_stmt);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this deallocated properly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it gets implicitly deallocated in pass insert_deallocate

deallocate(__libasr__created__var__0_Sum_4_2_1_res) ! Implicit deallocate

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is problematic in general (not here only but other places as well). We need to allocate only when extremely necessary like when there is no other way left. Same for creating auxiliary variables, create only when extremely necessary. Otherwise they lead to a lot of bugs.

@certik
Copy link
Contributor

certik commented Mar 10, 2024

How is this runtime dim feature implemented? I see just a slight modification there to use merge for the return argument, which is good. But how is the loop structure modified? My understanding is that currently we decide at compile time what the loop structure will be, but now we need to decide at runtime, but I don't see any such runtime implementation.

We also need to improve tests, as indicated above. Those more robust tests would reveal any possible issues with the implementation.

Overall great job, thanks for tackling this difficult feature! Let's iron out these details.

@Pranavchiku
Copy link
Contributor Author

Pranavchiku commented Mar 11, 2024

But how is the loop structure modified?

Implementation of sum is independent of compile time value of dim, so we need not to modify anything.

void generate_reduction_intrinsic_stmts_for_array_output(const Location& loc,
ASR::expr_t* array, ASR::expr_t* dim, SymbolTable* fn_scope,
Vec<ASR::stmt_t*>& fn_body, Vec<ASR::expr_t*>& idx_vars,
Vec<ASR::expr_t*>& target_idx_vars, Vec<ASR::stmt_t*>& doloop_body,
INIT init_stmts, LOOP_BODY loop_body) {
init_stmts();
int n_dims = ASRUtils::extract_n_dims_from_ttype(ASRUtils::expr_type(array));
ASR::stmt_t** else_ = nullptr;
size_t else_n = 0;
idx_vars.reserve(al, n_dims);
PassUtils::create_idx_vars(idx_vars, n_dims, loc, al, fn_scope, "_j");
for( int i = 1; i <= n_dims; i++ ) {
ASR::expr_t* current_dim = i32(i);
ASR::expr_t* test_expr = make_Compare(make_IntegerCompare_t, dim,
Eq, current_dim);
Vec<ASR::expr_t*> loop_vars;

we decide at compile time what the loop structure will be

For intrinsic Count, we have this problem, but it is problem in implementation which needs to resolved separately.

Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is good enough to merge. If there are bugs, we'll implement them.

Thanks for implementing this!

@Pranavchiku Pranavchiku merged commit 9abef30 into lfortran:main Mar 12, 2024
21 checks passed
@czgdp1807
Copy link
Member

This is fine in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Issues related to compiling fortran-lang/stdlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

runtime values for dim argument is not supported yet
3 participants