Skip to content

Enable C test for test_math.py #1357

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 6 commits into
base: main
Choose a base branch
from

Conversation

czgdp1807
Copy link
Collaborator

Closes #1246

Related discussion - #1243 (comment)

@@ -4742,13 +4742,30 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
ASRUtils::expr_value(enum_member_variable->m_symbolic_value));
} else if (ASR::is_a<ASR::Module_t>(*t)) {
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
std::string sym_name = value + "@" + x.m_attr;
std::string sym_name = value + "_" + x.m_attr + "_external__";
ASR::symbol_t *sym = current_scope->resolve_symbol(sym_name);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we insert this in the global scope itself?

Copy link
Collaborator Author

@czgdp1807 czgdp1807 Dec 16, 2022

Choose a reason for hiding this comment

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

Not needed IMO. Otherwise it would get declared in global scope in the C code as well and will mix things up with other scopes.

@czgdp1807 czgdp1807 marked this pull request as ready for review December 16, 2022 10:34
@@ -687,6 +688,7 @@ def log1p(x: f64) -> f64:
return log(1.0 + x)


@overload
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is kind of hacky and shouldn't we use some name tweaks at the ASR level?

Copy link
Collaborator Author

@czgdp1807 czgdp1807 Dec 16, 2022

Choose a reason for hiding this comment

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

I guess overload just does that. We can do it by hand but that will be needed only for fmod and log and nothing else. So ASR name tweaks will be hackish as well. The problem is that the log which is called in lfortran_intrinsics.h is defined in our generated C code as well and hence gets linked to our definition instead of the one in cmath. That’s what causes infinite call loop leading to segmentation fault. Now overload automatically does name mangling and hence linker correctly links because now log is not present instead it is present as a mangled name. Doing the same mangling by hand in ASR is more hackish. Also overloaded log/fmod are harmless as far as I can see.

Copy link
Collaborator Author

@czgdp1807 czgdp1807 Dec 16, 2022

Choose a reason for hiding this comment

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

The other way is to do it for every imported symbol. For example, from math import log, fabs should be stored as math_log__external_, math_fabs__external_. Then it will be uniform for every imported symbol, so no hacks then. However, I am not sure that's worth the effort as compared to harmless overload decorators in this PR. @certik Do you have any other approach in mind?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The other way is to do it for every imported symbol. For example, from math import log, fabs should be stored as math_log__external_, math_fabs__external_. Then it will be uniform for every imported symbol, so no hacks then

This sounds better to me. The reason is that the users might not be aware of this and may end up writing a function without an overload decorator (and it's quite intuitive not to use overload for a single function definition.) We need to figure out a generalized approach to handle this and the above might be the one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did a more sensible change (by implementing log for f32, fmod for (f32, f32) and log1p for f32). So the overloads make sense now.

The other way is to do it for every imported symbol. For example, from math import log, fabs should be stored as math_log__external_, math_fabs__external_.

This is not possible actually because call names won't resolve correctly. This is a problem on our compiler's end and I fixed it by implementing, log and fmod correctly.

if( v->m_symbolic_value ) {
this->visit_expr(*v->m_symbolic_value);
decl += indent + "#define " + std::string(v_ext->m_name) + " " + src + "\n";
src.clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we have to be careful here.

What happens if v_ext->m_name is equal to "x" and then we have another variable like "expr, will the x` in it be substituted? I think it will be, so we do not want to do that. Also we do not want to have any such macro defines in header files. We currently do not generate them, but eventually we will.

I would not use C macros.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I would not use C macros.

So constant C variables should work for this case?

Copy link
Contributor

Choose a reason for hiding this comment

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

Either that, or we can initialize the global variables when the program starts. I think both seems better than macros.

@certik certik marked this pull request as draft December 23, 2022 23:45
@czgdp1807 czgdp1807 marked this pull request as ready for review January 10, 2023 19:12
@certik certik marked this pull request as draft June 4, 2023 01:06
@certik
Copy link
Contributor

certik commented Jun 4, 2023

This needs some work. There might be good ideas here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C: Segmentation fault on generated test_math.c
3 participants