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

Implement real and imag for complex #256

Merged
merged 10 commits into from
Mar 24, 2022
Merged

Conversation

Smit-create
Copy link
Collaborator

@Smit-create Smit-create commented Mar 19, 2022

Part of #232
Fixes #229

}
if (ASR::is_a<ASR::Variable_t>(*t)) {
ASR::Variable_t *var = ASR::down_cast<ASR::Variable_t>(t);
LFORTRAN_ASSERT(var->m_value);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Presently, the following check fails on this line:

def test():
    x: c64
    x = 2 + 3j
    a: f64
    a = x.imag

ASR:

file "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 1801
    LFORTRAN_ASSERT(var->m_value);
AssertFailed: var->m_value

This might be because we are keeping value as nullptr while creating a variable which I expect to be changed after the assignment. @certik What are your views/suggestions on this?

Copy link
Contributor

Choose a reason for hiding this comment

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

The m_value member is only present if there is a compile time value. Otherwise it is nullptr. What you implemented below is a compile time "optimization", which is always good to have, but the main thing to implement is a runtime evaluation, so you have to create a node. For real, just cast the complex to real. For imag, you'll have to call a function. You can see LFortran for %re and %im, which is the same thing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, real seems to be working fine, but still finding some issues with imag.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

diff --git a/src/runtime/lpython_builtin.py b/src/runtime/lpython_builtin.py
index e439f9d2c..0e338f501 100644
--- a/src/runtime/lpython_builtin.py
+++ b/src/runtime/lpython_builtin.py
@@ -289,10 +289,13 @@ def lbound(x: i32[:], dim: i32) -> i32:
 def ubound(x: i32[:], dim: i32) -> i32:
     pass
 
+@ccall
+def _lfortran_zaimag(x: c64) -> f64:
+    pass
 
 @overload
 def imag(x: c64) -> f64:
-    pass
+    return _lfortran_zaimag(x)

This fails and produces this error:

code generation error: Intrinsic '_lfortran_zaimag' not implemented yet and compile time value is not available.


Note: if any of the above error or warning messages are not clear or are lacking
context please report it to us (we consider that a bug that needs to be fixed).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is this something related to #239 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Add --show-stacktrace to see where it is coming from. Not sure right now.

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 have the following stack trace:

Traceback

Traceback (most recent call last):
  File "/Users/thebigbool/repos/lpython/src/bin/lpython.cpp", line 745
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir, compiler_options);
  File "/Users/thebigbool/repos/lpython/src/bin/lpython.cpp", line 262
    res = fe.get_llvm3(*asr, diagnostics);
  File "/Users/thebigbool/repos/lpython/src/lpython/fortran_evaluator.cpp", line 58
    if (res.ok) {
  File "/Users/thebigbool/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 4170
    v.visit_asr((ASR::asr_t&)asr);
  File "../libasr/asr.h", line 2396
  File "../libasr/asr.h", line 2374
  File "../libasr/asr.h", line 2397
  File "../libasr/asr.h", line 2232
  File "/Users/thebigbool/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 904
    visit_symbol(*mod);
  File "../libasr/asr.h", line 2399
  File "../libasr/asr.h", line 2241
  File "/Users/thebigbool/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 1237
    visit_procedures(x);
  File "/Users/thebigbool/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 2415
    visit_Function(*s);
  File "/Users/thebigbool/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 1968
    generate_function(x);
  File "/Users/thebigbool/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 2320
    this->visit_stmt(*x.m_body[i]);
  File "../libasr/asr.h", line 2411
  File "../libasr/asr.h", line 2260
  File "/Users/thebigbool/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 2480
    this->visit_expr_wrapper(x.m_value, true);
  File "/Users/thebigbool/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 2505
    this->visit_expr(*x);
  File "../libasr/asr.h", line 2444
  File "../libasr/asr.h", line 2305
  File "/Users/thebigbool/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 4047
    throw CodeGenError("Intrinsic '" + func_name + "' not implemented yet and compile time value is not available.");
  File "/Users/thebigbool/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 93
    { }
  File "/Users/thebigbool/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 92
    : d{diag::Diagnostic(msg, diag::Level::Error, diag::Stage::CodeGen)}
code generation error: Intrinsic '_lfortran_zaimag' not implemented yet and compile time value is not available.

I believe it is because we don't have an implementation in pure python for this at present and ccall is not supported for this?

Copy link
Contributor

@gptsarthak gptsarthak Apr 14, 2023

Choose a reason for hiding this comment

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

Hey @Smit-create , I was working with intrinsic functions and got the same error.
All I did was call set_intrinsic() for all intrinsic functions starting whose name starts with _lfortran_.

I believe it is because we don't have an implementation in pure python for this at present and ccall is not supported for this?

This is true for all intrinsic functions in lfortran_intrinsics.c. How did you solve this issue?
Doubt cleared 👍

@Smit-create Smit-create force-pushed the pr232_2 branch 2 times, most recently from af322ae to f0dce71 Compare March 21, 2022 13:38
@Smit-create
Copy link
Collaborator Author

I think the CI fails because of recent changes in libasr. Before rebasing, the tests we working fine locally.

@Smit-create Smit-create marked this pull request as ready for review March 21, 2022 13:39
@certik
Copy link
Contributor

certik commented Mar 21, 2022

Now the CI works. Is there anything in libasr that broke main? If so, please let me know.

@Smit-create
Copy link
Collaborator Author

Now it works fine. Thanks.

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 this is good to go in. There are still some bugs, but we can fix them in subsequent PRs.

@certik certik enabled auto-merge March 23, 2022 16:13
@certik certik mentioned this pull request Mar 23, 2022
9 tasks
@namannimmo10
Copy link
Collaborator

Looks like we need to rebase with the main branch.

@Smit-create Smit-create merged commit 808ff02 into lcompilers:main Mar 24, 2022
@Smit-create Smit-create deleted the pr232_2 branch March 24, 2022 07:00
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.

Implement .real and .imag attributes on complex numbers
4 participants