Skip to content

Commit 521941a

Browse files
authored
Merge pull request #2727 from Vipul-Cariappa/jit-execfn
combining duplicated function into a single templated function
2 parents 7ecac3e + 7c8850c commit 521941a

File tree

5 files changed

+76
-146
lines changed

5 files changed

+76
-146
lines changed

src/bin/lpython.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ int compile_python_using_llvm(
11061106

11071107
e.add_module(std::move(m));
11081108
if (call_stmts) {
1109-
e.voidfn("__module___main_____main__global_stmts");
1109+
e.execfn<void>("__module___main_____main__global_stmts");
11101110
}
11111111

11121112
if (compiler_options.enable_cpython) {

src/libasr/codegen/evaluator.cpp

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -275,72 +275,6 @@ intptr_t LLVMEvaluator::get_symbol_address(const std::string &name) {
275275
return (intptr_t)cantFail(std::move(addr0));
276276
}
277277

278-
char *LLVMEvaluator::strfn(const std::string &name) {
279-
intptr_t addr = get_symbol_address(name);
280-
char *(*f)() = (char *(*)())addr;
281-
return f();
282-
}
283-
284-
int8_t LLVMEvaluator::int8fn(const std::string &name) {
285-
intptr_t addr = get_symbol_address(name);
286-
int8_t (*f)() = (int8_t (*)())addr;
287-
return f();
288-
}
289-
290-
int16_t LLVMEvaluator::int16fn(const std::string &name) {
291-
intptr_t addr = get_symbol_address(name);
292-
int16_t (*f)() = (int16_t (*)())addr;
293-
return f();
294-
}
295-
296-
int32_t LLVMEvaluator::int32fn(const std::string &name) {
297-
intptr_t addr = get_symbol_address(name);
298-
int32_t (*f)() = (int32_t (*)())addr;
299-
return f();
300-
}
301-
302-
int64_t LLVMEvaluator::int64fn(const std::string &name) {
303-
intptr_t addr = get_symbol_address(name);
304-
int64_t (*f)() = (int64_t (*)())addr;
305-
return f();
306-
}
307-
308-
bool LLVMEvaluator::boolfn(const std::string &name) {
309-
intptr_t addr = get_symbol_address(name);
310-
bool (*f)() = (bool (*)())addr;
311-
return f();
312-
}
313-
314-
float LLVMEvaluator::floatfn(const std::string &name) {
315-
intptr_t addr = get_symbol_address(name);
316-
float (*f)() = (float (*)())addr;
317-
return f();
318-
}
319-
320-
double LLVMEvaluator::doublefn(const std::string &name) {
321-
intptr_t addr = get_symbol_address(name);
322-
double (*f)() = (double (*)())addr;
323-
return f();
324-
}
325-
326-
std::complex<float> LLVMEvaluator::complex4fn(const std::string &name) {
327-
intptr_t addr = get_symbol_address(name);
328-
std::complex<float> (*f)() = (std::complex<float> (*)())addr;
329-
return f();
330-
}
331-
332-
std::complex<double> LLVMEvaluator::complex8fn(const std::string &name) {
333-
intptr_t addr = get_symbol_address(name);
334-
std::complex<double> (*f)() = (std::complex<double> (*)())addr;
335-
return f();
336-
}
337-
338-
void LLVMEvaluator::voidfn(const std::string &name) {
339-
intptr_t addr = get_symbol_address(name);
340-
void (*f)() = (void (*)())addr;
341-
f();
342-
}
343-
344278
void write_file(const std::string &filename, const std::string &contents)
345279
{
346280
std::ofstream out;

src/libasr/codegen/evaluator.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,6 @@ class LLVMEvaluator
5252
void add_module(std::unique_ptr<llvm::Module> mod);
5353
void add_module(std::unique_ptr<LLVMModule> m);
5454
intptr_t get_symbol_address(const std::string &name);
55-
char *strfn(const std::string &name);
56-
int8_t int8fn(const std::string &name);
57-
int16_t int16fn(const std::string &name);
58-
int32_t int32fn(const std::string &name);
59-
int64_t int64fn(const std::string &name);
60-
bool boolfn(const std::string &name);
61-
float floatfn(const std::string &name);
62-
double doublefn(const std::string &name);
63-
std::complex<float> complex4fn(const std::string &name);
64-
std::complex<double> complex8fn(const std::string &name);
65-
void voidfn(const std::string &name);
6655
std::string get_asm(llvm::Module &m);
6756
void save_asm_file(llvm::Module &m, const std::string &filename);
6857
void save_object_file(llvm::Module &m, const std::string &filename);
@@ -73,6 +62,13 @@ class LLVMEvaluator
7362
llvm::LLVMContext &get_context();
7463
static void print_targets();
7564
static std::string get_default_target_triple();
65+
66+
template<class T>
67+
T execfn(const std::string &name) {
68+
intptr_t addr = get_symbol_address(name);
69+
T (*f)() = (T (*)())addr;
70+
return f();
71+
}
7672
};
7773

7874

src/lpython/python_evaluator.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
132132
->m_symtab->get_symbol(run_fn);
133133
LCOMPILERS_ASSERT(fn)
134134
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::Character) {
135-
char *r = e->strfn(run_fn);
135+
char *r = e->execfn<char*>(run_fn);
136136
result.type = EvalResult::string;
137137
result.str = r;
138138
} else {
@@ -143,11 +143,11 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
143143
->m_symtab->get_symbol(run_fn);
144144
LCOMPILERS_ASSERT(fn)
145145
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::UnsignedInteger) {
146-
uint8_t r = e->int8fn(run_fn);
146+
uint8_t r = e->execfn<uint8_t>(run_fn);
147147
result.type = EvalResult::unsignedInteger1;
148148
result.u32 = r;
149149
} else {
150-
int8_t r = e->int8fn(run_fn);
150+
int8_t r = e->execfn<int8_t>(run_fn);
151151
result.type = EvalResult::integer1;
152152
result.i32 = r;
153153
}
@@ -156,11 +156,11 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
156156
->m_symtab->get_symbol(run_fn);
157157
LCOMPILERS_ASSERT(fn)
158158
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::UnsignedInteger) {
159-
uint16_t r = e->int16fn(run_fn);
159+
uint16_t r = e->execfn<uint16_t>(run_fn);
160160
result.type = EvalResult::unsignedInteger2;
161161
result.u32 = r;
162162
} else {
163-
int16_t r = e->int16fn(run_fn);
163+
int16_t r = e->execfn<int16_t>(run_fn);
164164
result.type = EvalResult::integer2;
165165
result.i32 = r;
166166
}
@@ -169,11 +169,11 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
169169
->m_symtab->get_symbol(run_fn);
170170
LCOMPILERS_ASSERT(fn)
171171
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::UnsignedInteger) {
172-
uint32_t r = e->int32fn(run_fn);
172+
uint32_t r = e->execfn<uint32_t>(run_fn);
173173
result.type = EvalResult::unsignedInteger4;
174174
result.u32 = r;
175175
} else {
176-
int32_t r = e->int32fn(run_fn);
176+
int32_t r = e->execfn<int32_t>(run_fn);
177177
result.type = EvalResult::integer4;
178178
result.i32 = r;
179179
}
@@ -182,34 +182,34 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
182182
->m_symtab->get_symbol(run_fn);
183183
LCOMPILERS_ASSERT(fn)
184184
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::UnsignedInteger) {
185-
uint64_t r = e->int64fn(run_fn);
185+
uint64_t r = e->execfn<uint64_t>(run_fn);
186186
result.type = EvalResult::unsignedInteger8;
187187
result.u64 = r;
188188
} else {
189-
int64_t r = e->int64fn(run_fn);
189+
int64_t r = e->execfn<int64_t>(run_fn);
190190
result.type = EvalResult::integer8;
191191
result.i64 = r;
192192
}
193193
} else if (return_type == "real4") {
194-
float r = e->floatfn(run_fn);
194+
float r = e->execfn<float>(run_fn);
195195
result.type = EvalResult::real4;
196196
result.f32 = r;
197197
} else if (return_type == "real8") {
198-
double r = e->doublefn(run_fn);
198+
double r = e->execfn<double>(run_fn);
199199
result.type = EvalResult::real8;
200200
result.f64 = r;
201201
} else if (return_type == "complex4") {
202-
std::complex<float> r = e->complex4fn(run_fn);
202+
std::complex<float> r = e->execfn<std::complex<float>>(run_fn);
203203
result.type = EvalResult::complex4;
204204
result.c32.re = r.real();
205205
result.c32.im = r.imag();
206206
} else if (return_type == "complex8") {
207-
std::complex<double> r = e->complex8fn(run_fn);
207+
std::complex<double> r = e->execfn<std::complex<double>>(run_fn);
208208
result.type = EvalResult::complex8;
209209
result.c64.re = r.real();
210210
result.c64.im = r.imag();
211211
} else if (return_type == "void") {
212-
e->voidfn(run_fn);
212+
e->execfn<void>(run_fn);
213213
result.type = EvalResult::statement;
214214
} else if (return_type == "none") {
215215
result.type = EvalResult::none;

0 commit comments

Comments
 (0)