Thanks to visit codestin.com
Credit goes to github.com

Skip to content

REPL str support #2724

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

Merged
merged 2 commits into from
Jun 6, 2024
Merged
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
6 changes: 6 additions & 0 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,12 @@ int interactive_python_repl(
std::cout << std::setprecision(17) << "(" << r.c64.re << ", " << r.c64.im << ")" << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::string) : {
if (verbose) std::cout << "Return type: str" << std::endl;
if (verbose) section("Result:");
std::cout << (r.str == nullptr ? "" : r.str) << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::statement) : {
if (verbose) {
std::cout << "Return type: none" << std::endl;
Expand Down
8 changes: 8 additions & 0 deletions src/libasr/codegen/evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ std::string LLVMModule::get_return_type(const std::string &fn_name)
return "integer4";
} else if (type->isIntegerTy(64)) {
return "integer8";
} else if (type->isPointerTy() && type->getPointerElementType()->isIntegerTy(8)) {
return "integer1ptr";
} else if (type->isStructTy()) {
llvm::StructType *st = llvm::cast<llvm::StructType>(type);
if (st->hasName()) {
Expand Down Expand Up @@ -273,6 +275,12 @@ intptr_t LLVMEvaluator::get_symbol_address(const std::string &name) {
return (intptr_t)cantFail(std::move(addr0));
}

char *LLVMEvaluator::strfn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
char *(*f)() = (char *(*)())addr;
return f();
}

int8_t LLVMEvaluator::int8fn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
int8_t (*f)() = (int8_t (*)())addr;
Expand Down
1 change: 1 addition & 0 deletions src/libasr/codegen/evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class LLVMEvaluator
void add_module(std::unique_ptr<llvm::Module> mod);
void add_module(std::unique_ptr<LLVMModule> m);
intptr_t get_symbol_address(const std::string &name);
char *strfn(const std::string &name);
int8_t int8fn(const std::string &name);
int16_t int16fn(const std::string &name);
int32_t int32fn(const std::string &name);
Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/global_stmts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ void pass_wrap_global_stmts(Allocator &al,
fn_scope->add_symbol(std::string(var_name), down_cast<ASR::symbol_t>(return_var));
target = return_var_ref;
idx++;
} else if (ASRUtils::expr_type(value)->type == ASR::ttypeType::Complex) {
} else if ((ASRUtils::expr_type(value)->type == ASR::ttypeType::Complex) ||
(ASRUtils::expr_type(value)->type == ASR::ttypeType::Character)) {
s.from_str(al, fn_name_s + std::to_string(idx));
var_name = s.c_str(al);
type = ASRUtils::expr_type(value);
Expand Down
4 changes: 4 additions & 0 deletions src/libasr/runtime/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,10 @@ LFORTRAN_API void _lfortran_strcpy(char** x, char *y, int8_t free_target)
// *x = (char*) malloc((strlen(y) + 1) * sizeof(char));
// _lfortran_string_init(strlen(y) + 1, *x);
}
if (y == NULL) {
*x = NULL;
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need this? Could you share an example where this fails currently as of the main branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without the above change, the following fails (SEGFAULT):

❯ lp                                                           
>>> s: str
>>> s
The stack address was not found in any shared library or the main program, the stack is probably corrupted. Aborting.

Tested by Test Case PythonCompiler string 2.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it is fine. I am curious about the following:

  • why does _lfortran_strcpy get called in this case?
  • are you able to reproduce this same bug in normal/regular lpython invocation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why does _lfortran_strcpy get called in this case?

The global_stmt function that is constructed with the top-level expressions, copies the string into the return variable. You can run the example code above with lp -v flag to look at the ASR and LLVM IR generated.

The ASR and IR for the second line:

(TranslationUnit
    (SymbolTable
        1
        {
            __main__:
                (Module
                    (SymbolTable
                        2
                        {
                            __main__global_stmts_2__:
                                (Function
                                    (SymbolTable
                                        3
                                        {
                                            __main__global_stmts_2__1:
                                                (Variable
                                                    3
                                                    __main__global_stmts_2__1
                                                    []
                                                    ReturnVar
                                                    ()
                                                    ()
                                                    Default
                                                    (Character 1 -2 ())
                                                    ()
                                                    BindC
                                                    Public
                                                    Required
                                                    .false.
                                                )
                                        })
                                    __main__global_stmts_2__
                                    (FunctionType
                                        []
                                        (Character 1 -2 ())
                                        BindC
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    []
                                    []
                                    [(Assignment
                                        (Var 3 __main__global_stmts_2__1)
                                        (Var 2 s)
                                        ()
                                    )]
                                    (Var 3 __main__global_stmts_2__1)
                                    Public
                                    .false.
                                    .false.
                                    ()
                                ),
                            s:
                                (Variable
                                    2
                                    s
                                    []
                                    Local
                                    ()
                                    ()
                                    Default
                                    (Character 1 -2 ())
                                    ()
                                    Interactive
                                    Public
                                    Required
                                    .false.
                                )
                        })
                    __main__
                    []
                    .false.
                    .false.
                )
        })
    []
)
; ModuleID = 'LFortran'
source_filename = "LFortran"

@s = external global i8*

define i8* @__main__global_stmts_2__() {
.entry:
  %__main__global_stmts_2__1 = alloca i8*, align 8
  store i8* null, i8** %__main__global_stmts_2__1, align 8
  %0 = load i8*, i8** @s, align 8
  call void @_lfortran_strcpy(i8** %__main__global_stmts_2__1, i8* %0, i8 0)
  br label %return

return:                                           ; preds = %.entry
  %1 = load i8*, i8** %__main__global_stmts_2__1, align 8
  ret i8* %1
}

declare void @_lfortran_strcpy(i8**, i8*, i8)

are you able to reproduce this same bug in normal/regular lpython invocation?

No, this does not cause any problem with normal lpython execution.


Maybe we can optimize it to not make a copy. I will look into it.

// if( *x == NULL ) {
*x = (char*) malloc((strlen(y) + 1) * sizeof(char));
_lfortran_string_init(strlen(y) + 1, *x);
Expand Down
15 changes: 13 additions & 2 deletions src/lpython/python_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,18 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(

e->add_module(std::move(m));
if (call_run_fn) {
if (return_type == "integer1") {
if (return_type == "integer1ptr") {
ASR::symbol_t *fn = ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol(module_name))
->m_symtab->get_symbol(run_fn);
LCOMPILERS_ASSERT(fn)
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::Character) {
char *r = e->strfn(run_fn);
result.type = EvalResult::string;
result.str = r;
} else {
throw LCompilersException("PythonCompiler::evaluate(): Return type not supported");
}
} else if (return_type == "integer1") {
ASR::symbol_t *fn = ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol(module_name))
->m_symtab->get_symbol(run_fn);
LCOMPILERS_ASSERT(fn)
Expand Down Expand Up @@ -203,7 +214,7 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
} else if (return_type == "none") {
result.type = EvalResult::none;
} else {
throw LCompilersException("FortranEvaluator::evaluate(): Return type not supported");
throw LCompilersException("PythonCompiler::evaluate(): Return type not supported");
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/lpython/python_evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class PythonCompiler
real8,
complex4,
complex8,
string,
statement,
none
} type;
Expand All @@ -59,6 +60,7 @@ class PythonCompiler
uint64_t u64;
float f32;
double f64;
char *str;
struct {float re, im;} c32;
struct {double re, im;} c64;
};
Expand Down
88 changes: 88 additions & 0 deletions src/lpython/tests/test_llvm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <tests/doctest.h>

#include <cmath>
#include <cstring>

#include <lpython/python_evaluator.h>
#include <libasr/codegen/evaluator.h>
Expand Down Expand Up @@ -1270,3 +1271,90 @@ TEST_CASE("PythonCompiler u16 declaration") {
CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger2);
CHECK(r.result.u32 == 45);
}

TEST_CASE("PythonCompiler string 1") {
CompilerOptions cu;
cu.po.disable_main = true;
cu.emit_debug_line_column = false;
cu.generate_object_code = false;
cu.interactive = true;
cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir();
PythonCompiler e(cu);
LCompilers::Result<PythonCompiler::EvalResult>

r = e.evaluate2("\"My String\"");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::string);
CHECK(std::strcmp(r.result.str, "My String") == 0);

r = e.evaluate2("\"s1\" + \" \" + \"s2\"");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::string);
CHECK(std::strcmp(r.result.str, "s1 s2") == 0);
}

TEST_CASE("PythonCompiler string 2") {
CompilerOptions cu;
cu.po.disable_main = true;
cu.emit_debug_line_column = false;
cu.generate_object_code = false;
cu.interactive = true;
cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir();
PythonCompiler e(cu);
LCompilers::Result<PythonCompiler::EvalResult>

r = e.evaluate2("s: str");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::none);

r = e.evaluate2("s");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::string);
CHECK(r.result.str == nullptr);

r = e.evaluate2(R"(
s = ""
i: i32 = 0
for i in range(10):
s += str(i)
)");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::statement);

r = e.evaluate2("s");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::string);
CHECK(std::strcmp(r.result.str, "0123456789") == 0);
}

TEST_CASE("PythonCompiler string 3") {
CompilerOptions cu;
cu.po.disable_main = true;
cu.emit_debug_line_column = false;
cu.generate_object_code = false;
cu.interactive = true;
cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir();
PythonCompiler e(cu);
LCompilers::Result<PythonCompiler::EvalResult>

r = e.evaluate2(R"(
def my_concat(x: str, y: str) -> str:
return x + " " + y
)");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::none);

r = e.evaluate2("s: str = \"0123456789\"");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::none);

r = e.evaluate2("my_concat(s, \"NUM\")");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::string);
CHECK(std::strcmp(r.result.str, "0123456789 NUM") == 0);

r = e.evaluate2("my_concat(\"Python\", \"REPL\")");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::string);
CHECK(std::strcmp(r.result.str, "Python REPL") == 0);
}
Loading