Skip to content

Commit e351551

Browse files
changes made according to code review
1 parent b2a8610 commit e351551

5 files changed

Lines changed: 60 additions & 63 deletions

File tree

src/bin/lpython.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,14 +1007,14 @@ int interactive_python_repl(
10071007
}
10081008
break;
10091009
}
1010-
case (LCompilers::PythonCompiler::EvalResult::structt) : {
1010+
case (LCompilers::PythonCompiler::EvalResult::struct_type) : {
10111011
if (verbose) {
10121012
std::cout << "Return type: "
10131013
<< LCompilers::ASRUtils::get_type_code(r.structure.ttype)
10141014
<< std::endl;
10151015
}
10161016
if (verbose) section("Result:");
1017-
std::cout << fe.string_aggregate_type(r) << std::endl;
1017+
std::cout << fe.aggregate_type_to_string(r) << std::endl;
10181018
break;
10191019
}
10201020
case (LCompilers::PythonCompiler::EvalResult::none) : {

src/libasr/pass/global_stmts.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,6 @@ void pass_wrap_global_stmts(Allocator &al,
102102
unit.m_symtab->add_symbol(global_underscore_name, down_cast<ASR::symbol_t>(global_underscore));
103103
ASR::stmt_t* asr_stmt = ASRUtils::STMT(ASR::make_Assignment_t(al, loc, global_underscore_ref, return_var_ref, nullptr));
104104
body.push_back(al, asr_stmt);
105-
106-
// if ((type->type == ASR::ttypeType::Tuple) ||
107-
// (type->type == ASR::ttypeType::List)) {
108-
// s.from_str(al, fn_name_s + std::to_string(idx));
109-
// var_name = s.c_str(al);
110-
111-
// ASR::ttype_t *type_pointer = ASRUtils::TYPE(ASR::make_Pointer_t(al, loc, type));
112-
// ASR::expr_t *value = EXPR(ASR::make_GetPointer_t(al, loc, global_underscore_ref, type_pointer, nullptr));
113-
114-
// return_var = ASR::make_Variable_t(al, loc,
115-
// fn_scope, var_name, nullptr, 0, ASRUtils::intent_local, nullptr, nullptr,
116-
// ASR::storage_typeType::Default, type_pointer,
117-
// nullptr, ASR::abiType::BindC,
118-
// ASR::Public, ASR::presenceType::Required, false);
119-
// return_var_ref = EXPR(ASR::make_Var_t(al, loc, down_cast<ASR::symbol_t>(return_var)));
120-
// ASR::stmt_t* asr_stmt = ASRUtils::STMT(ASR::make_Assignment_t(al, loc, return_var_ref, value, nullptr));
121-
// body.push_back(al, asr_stmt);
122-
// idx++;
123-
// return_var_ref = nullptr;
124-
// return_var = nullptr;
125-
// }
126105
}
127106

128107
ASR::asr_t *fn = ASRUtils::make_Function_t_util(

src/lpython/python_evaluator.cpp

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -128,35 +128,19 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
128128
call_run_fn = true;
129129
}
130130

131-
llvm::Type *type = nullptr;
132-
ASR::symbol_t *f = symbol_table->get_symbol("_" + run_fn);
133-
if ((return_type == "struct") && (f)) {
134-
llvm::Function *fn = m->get_function(run_fn);
135-
type = fn->getReturnType();
136-
LCOMPILERS_ASSERT(type->isStructTy())
131+
ASR::symbol_t *global_underscore_sym = symbol_table->get_symbol("_" + run_fn);
132+
if ((return_type == "struct") && (global_underscore_sym)) {
133+
// we compute the offsets of the struct's attribute here
134+
// we will be using it later in aggregate_type_to_string to print the struct
137135

138-
const llvm::DataLayout &dl = e->get_jit_data_layout();
139-
size_t elements_count = type->getStructNumElements();
140-
LCompilers::Vec<size_t> offsets;
141-
offsets.reserve(al, elements_count);
142-
for (size_t i = 0; i < elements_count; i++) {
143-
size_t offset = dl.getStructLayout((llvm::StructType*)type)->getElementOffset(i);
144-
offsets.push_back(al, offset);
145-
}
146-
result.structure.offsets = offsets.p;
136+
// we compute the offsets here instead of computing it in aggregate_type_to_string
137+
// because once we call `e->add_module`, internally LLVM may deallocate all the
138+
// type info after compiling the IR into machine code
147139

148-
result.structure.ttype = ASR::down_cast<ASR::Variable_t>(f)->m_type;
149-
if (result.structure.ttype->type == ASR::ttypeType::List) {
150-
type = type->getStructElementType(2);
151-
LCOMPILERS_ASSERT(type->isPointerTy())
152-
result.structure.element_size = e->get_jit_data_layout().getTypeAllocSize(
153-
#if LLVM_VERSION_MAJOR >= 14
154-
type->getNonOpaquePointerElementType()
155-
#else
156-
type->getPointerElementType()
157-
#endif
158-
);
159-
}
140+
llvm::Function *fn = m->get_function(run_fn);
141+
llvm::Type *llvm_type = fn->getReturnType();
142+
LCOMPILERS_ASSERT(llvm_type->isStructTy())
143+
compute_offsets(llvm_type, global_underscore_sym, result);
160144
}
161145

162146
e->add_module(std::move(m));
@@ -248,11 +232,11 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
248232
result.b = r;
249233
} else if (return_type == "struct") {
250234
e->execfn<void>(run_fn);
251-
if (f) {
235+
if (global_underscore_sym) {
252236
void *r = (void*)e->get_symbol_address("_" + run_fn);
253237
LCOMPILERS_ASSERT(r)
254238
result.structure.structure = r;
255-
result.type = EvalResult::structt;
239+
result.type = EvalResult::struct_type;
256240
}
257241
} else if (return_type == "void") {
258242
e->execfn<void>(run_fn);
@@ -492,7 +476,7 @@ Result<std::string> PythonCompiler::get_asm(
492476

493477
void print_type(ASR::ttype_t *t, void *data, std::string &result);
494478

495-
std::string PythonCompiler::string_aggregate_type(const struct EvalResult &r) {
479+
std::string PythonCompiler::aggregate_type_to_string(const struct EvalResult &r) {
496480
ASR::ttype_t *asr_type = r.structure.ttype;
497481
void *data = r.structure.structure;
498482
size_t *offsets = r.structure.offsets;
@@ -522,6 +506,33 @@ std::string PythonCompiler::string_aggregate_type(const struct EvalResult &r) {
522506
return result;
523507
}
524508

509+
void PythonCompiler::compute_offsets(llvm::Type *type, ASR::symbol_t *asr_type, EvalResult &result) {
510+
LCOMPILERS_ASSERT(type->isStructTy())
511+
512+
const llvm::DataLayout &dl = e->get_jit_data_layout();
513+
size_t elements_count = type->getStructNumElements();
514+
LCompilers::Vec<size_t> offsets;
515+
offsets.reserve(al, elements_count);
516+
for (size_t i = 0; i < elements_count; i++) {
517+
size_t offset = dl.getStructLayout((llvm::StructType*)type)->getElementOffset(i);
518+
offsets.push_back(al, offset);
519+
}
520+
result.structure.offsets = offsets.p;
521+
522+
result.structure.ttype = ASR::down_cast<ASR::Variable_t>(asr_type)->m_type;
523+
if (result.structure.ttype->type == ASR::ttypeType::List) {
524+
type = type->getStructElementType(2);
525+
LCOMPILERS_ASSERT(type->isPointerTy())
526+
result.structure.element_size = e->get_jit_data_layout().getTypeAllocSize(
527+
#if LLVM_VERSION_MAJOR >= 14
528+
type->getNonOpaquePointerElementType()
529+
#else
530+
type->getPointerElementType()
531+
#endif
532+
);
533+
}
534+
}
535+
525536
void print_type(ASR::ttype_t *t, void *data, std::string &result) {
526537
switch (t->type) {
527538
case ASR::ttypeType::Logical:

src/lpython/python_evaluator.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include <libasr/diagnostics.h>
1414
#include <libasr/pass/pass_manager.h>
1515

16+
namespace llvm {
17+
class Type;
18+
}
19+
1620
namespace LCompilers {
1721

1822
class LLVMModule;
@@ -53,7 +57,7 @@ class PythonCompiler
5357
complex8,
5458
boolean,
5559
string,
56-
structt,
60+
struct_type,
5761
statement,
5862
none
5963
} type;
@@ -120,7 +124,10 @@ class PythonCompiler
120124
LCompilers::PassManager& pass_manager,
121125
diag::Diagnostics &diagnostics);
122126

123-
std::string string_aggregate_type(const struct EvalResult &r);
127+
std::string aggregate_type_to_string(const struct EvalResult &r);
128+
129+
private:
130+
void compute_offsets(llvm::Type *type, ASR::symbol_t *asr_type, EvalResult &result);
124131

125132
private:
126133
Allocator al;

src/lpython/tests/test_llvm.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,31 +1479,31 @@ TEST_CASE("PythonCompiler lists") {
14791479

14801480
r = e.evaluate2("[1, 2, 3]");
14811481
CHECK(r.ok);
1482-
CHECK(r.result.type == PythonCompiler::EvalResult::structt);
1482+
CHECK(r.result.type == PythonCompiler::EvalResult::struct_type);
14831483
CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[i32]");
1484-
CHECK(e.string_aggregate_type(r.result) == "[1, 2, 3]");
1484+
CHECK(e.aggregate_type_to_string(r.result) == "[1, 2, 3]");
14851485

14861486
r = e.evaluate2("[u8(1), u8(2), u8(3)] + [u8(1), u8(2), u8(3)]");
14871487
CHECK(r.ok);
1488-
CHECK(r.result.type == PythonCompiler::EvalResult::structt);
1488+
CHECK(r.result.type == PythonCompiler::EvalResult::struct_type);
14891489
CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[u8]");
1490-
CHECK(e.string_aggregate_type(r.result) == "[1, 2, 3, 1, 2, 3]");
1490+
CHECK(e.aggregate_type_to_string(r.result) == "[1, 2, 3, 1, 2, 3]");
14911491

14921492
r = e.evaluate2("x: list[f64] = [1.5, 2.5, 3.5]");
14931493
CHECK(r.ok);
14941494
CHECK(r.result.type == PythonCompiler::EvalResult::statement);
14951495

14961496
r = e.evaluate2("x + [4.5]");
14971497
CHECK(r.ok);
1498-
CHECK(r.result.type == PythonCompiler::EvalResult::structt);
1498+
CHECK(r.result.type == PythonCompiler::EvalResult::struct_type);
14991499
CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[r64]");
1500-
CHECK(e.string_aggregate_type(r.result) == "[1.500000, 2.500000, 3.500000, 4.500000]");
1500+
CHECK(e.aggregate_type_to_string(r.result) == "[1.500000, 2.500000, 3.500000, 4.500000]");
15011501

15021502
r = e.evaluate2("[\"lfortran\", \"lpython\", \"lc\"]");
15031503
CHECK(r.ok);
1504-
CHECK(r.result.type == PythonCompiler::EvalResult::structt);
1504+
CHECK(r.result.type == PythonCompiler::EvalResult::struct_type);
15051505
CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[str]");
1506-
CHECK(e.string_aggregate_type(r.result) == "[\"lfortran\", \"lpython\", \"lc\"]");
1506+
CHECK(e.aggregate_type_to_string(r.result) == "[\"lfortran\", \"lpython\", \"lc\"]");
15071507
}
15081508

15091509
TEST_CASE("PythonCompiler asr verify 1") {

0 commit comments

Comments
 (0)