-
Notifications
You must be signed in to change notification settings - Fork 170
Implement real and imag for complex #256
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
Conversation
} | ||
if (ASR::is_a<ASR::Variable_t>(*t)) { | ||
ASR::Variable_t *var = ASR::down_cast<ASR::Variable_t>(t); | ||
LFORTRAN_ASSERT(var->m_value); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 👍
af322ae
to
f0dce71
Compare
I think the CI fails because of recent changes in |
Now the CI works. Is there anything in libasr that broke |
Now it works fine. Thanks. |
There was a problem hiding this 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.
Looks like we need to rebase with the main branch. |
Co-Authored-By: Ondřej Čertík <[email protected]>
Part of #232
Fixes #229