Skip to content

Commit bf25961

Browse files
author
drswinghead
committed
improve remod bind method.
1 parent e8420a4 commit bf25961

14 files changed

+220
-37
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ set(handby_lib_SRCS
7474
clvm.cpp
7575
clvm_letacy.cpp
7676
ivm/clvmjitlistener.cpp
77+
ivm/misc.cpp
7778
namelookup.cpp
7879
callargument.cpp
7980
invokestorage.cpp

compilerengine.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,27 @@ llvm::Module* CompilerEngine::conv_function2(clang::ASTUnit *unit, clang::Functi
11191119
return cu->mmod;
11201120
}
11211121

1122+
llvm::Module*
1123+
CompilerEngine::conv_dargs(clang::ASTUnit *unit, clang::FunctionDecl *fd, QVector<QVariant> &dargs)
1124+
{
1125+
auto cu = this->createCompilerUnit(unit, fd);
1126+
1127+
int cnter = -1;
1128+
for (auto &v: dargs) {
1129+
cnter ++;
1130+
// qDebug()<<v<<v.type()<<(int)v.type()<<v.userType();
1131+
int t = (int)v.type();
1132+
if (v.type() != QMetaType::User) continue;
1133+
if (v.userType() != EvalType::id) continue;
1134+
this->gen_darg(cu->mmod, v, cnter, fd);
1135+
EvalType r = v.value<EvalType>();
1136+
qDebug()<<v<<r.ve<<r.vv;
1137+
}
1138+
1139+
1140+
return cu->mmod;
1141+
}
1142+
11221143
bool CompilerEngine::gen_ctor(CompilerUnit *cu, clang::CXXConstructorDecl *yactor)
11231144
{
11241145
// 转换到需要的参数类型
@@ -1245,7 +1266,7 @@ bool CompilerEngine::gen_dtor(CompilerUnit *cu, clang::CXXDestructorDecl *yadtor
12451266
bool CompilerEngine::gen_darg(llvm::Module *mod, QVariant &darg, int idx, clang::FunctionDecl *fd)
12461267
{
12471268
CompilerUnit *cu = mcus.value(mod);
1248-
1269+
12491270
/*
12501271
for (auto &v: cu->mdargs) {
12511272
if (v.type() != EvalType::id) continue;
@@ -2045,6 +2066,9 @@ CompilerEngine::createCompilerUnit(clang::ASTUnit *unit, clang::NamedDecl *decl)
20452066
cu->mbdecl = this->get_decl_with_body(llvm::cast<clang::CXXMethodDecl>(decl));
20462067
qDebug()<<cu->mdecl<<cu->mbdecl<<(cu->mdecl == cu->mbdecl)<<cu->mcgm;
20472068

2069+
//
2070+
mcus.insert(cu->mmod, cu);
2071+
20482072
return cu;
20492073
}
20502074

compilerengine.h

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class CompilerEngine
6868
llvm::Module* conv_dtor(clang::ASTUnit *unit, clang::CXXDestructorDecl *dtor);
6969
llvm::Module* conv_method2(clang::ASTUnit *unit, clang::CXXMethodDecl *mth);
7070
llvm::Module* conv_function2(clang::ASTUnit *unit, clang::FunctionDecl *mth);
71+
llvm::Module* conv_dargs(clang::ASTUnit *unit, clang::FunctionDecl *fd, QVector<QVariant> &dargs);
7172

7273
bool gen_ctor(CompilerUnit *cu, clang::CXXConstructorDecl *yactor = NULL);
7374
bool gen_dtor(CompilerUnit *cu, clang::CXXDestructorDecl *yadtor = NULL);
@@ -121,6 +122,8 @@ class CompilerUnit
121122
QVector<QString> mUndefSymbols;
122123
QHash<QString, bool> mNoinlineSymbols;
123124
QVector<QVariant> mdargs;
125+
126+
llvm::Module *mremod = NULL;
124127
};
125128

126129
#endif /* COMPILERENGINE_H */

ctrlengine.cpp

+44-11
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ CtrlEngine::~CtrlEngine()
3333
{
3434
}
3535

36-
3736
void *CtrlEngine::vm_new(QString klass, QVector<QVariant> uargs)
3837
{
3938
// 处理流程,
@@ -68,7 +67,7 @@ void *CtrlEngine::vm_new(QString klass, QVector<QVariant> uargs)
6867
QString modname = QString("qtmod%1").arg(mce->mangle_symbol(mfe->getASTContext(), ctor_decl));
6968
llvm::Module *qtmod = NULL;
7069

71-
if (mman->contains(modname)) {
70+
if (mman->has(modname)) {
7271
qtmod = mman->get(modname);
7372
} else {
7473
// auto mod = mce->conv_ctor(mfe->getASTContext(), ctor_decl);
@@ -122,10 +121,14 @@ void *CtrlEngine::vm_new(QString klass, QVector<QVariant> uargs)
122121
// QString lamsym = oe.bind(mod, "_ZN7QStringC2Ev", kthis, uargs, dargs);
123122
QString lamsym = oe.bind(qtmod, symname, klass, uargs, dargs, mtdargs, false, kthis);
124123
// qDebug()<<lamsym;
124+
125+
llvm::Module *remod = this->process_dargs(dargs);
126+
DUMP_IR(remod);
125127

126-
llvm::Module *remod = oe.bind(qtmod, klass, uargs, dargs, mtdargs, false, kthis);
128+
oe.bind(qtmod, remod, klass, uargs, dargs, mtdargs, false, kthis);
127129
TEMP_DEBUG();
128-
DUMP_IR(remod);
130+
DUMP_IR(remod);
131+
129132
CodeUnit *cu = new CodeUnit(qtmod, remod);
130133
// QString lamsym = QString::fromStdString(remod->getName().str());
131134

@@ -146,6 +149,36 @@ void *CtrlEngine::vm_new(QString klass, QVector<QVariant> uargs)
146149
return kthis;
147150
}
148151

152+
llvm::Module* CtrlEngine::process_dargs(QVector<QVariant> &dargs)
153+
{
154+
// 默认参数编译成IR
155+
clang::FunctionDecl *jmt_decl = mfe->find_free_function("__jit_main_tmpl");
156+
qDebug()<<jmt_decl;
157+
// jmt_decl->dumpColor();
158+
DUMP_COLOR(jmt_decl);
159+
160+
if (true) {
161+
llvm::Module *remod = mce->conv_dargs(mfe->getASTUnit(), jmt_decl, dargs);
162+
// DUMP_IR(remod);
163+
return remod;
164+
165+
/*
166+
int cnter = -1;
167+
for (auto &v: dargs) {
168+
cnter ++;
169+
// qDebug()<<v<<v.type()<<(int)v.type()<<v.userType();
170+
int t = (int)v.type();
171+
if (v.type() != QMetaType::User) continue;
172+
if (v.userType() != EvalType::id) continue;
173+
mce->gen_darg(mod, v, cnter, jmt_decl);
174+
EvalType r = v.value<EvalType>();
175+
qDebug()<<v<<r.ve<<r.vv;
176+
}
177+
*/
178+
}
179+
return NULL;
180+
}
181+
149182
bool CtrlEngine::vm_delete(void *kthis, QString klass)
150183
{
151184
// 处理流程,
@@ -167,7 +200,7 @@ bool CtrlEngine::vm_delete(void *kthis, QString klass)
167200
QString modname = QString("qtmod%1").arg(mce->mangle_symbol(mfe->getASTContext(), dtor_decl));
168201
llvm::Module *qtmod = NULL;
169202

170-
if (mman->contains(modname)) {
203+
if (mman->has(modname)) {
171204
qtmod = mman->get(modname);
172205
} else {
173206
auto mod = mce->conv_dtor(mfe->getASTUnit(), dtor_decl);
@@ -358,7 +391,7 @@ QVariant CtrlEngine::vm_call(void *kthis, QString klass, QString method, QVector
358391
QString modname = QString("qtmod%1").arg(mce->mangle_symbol(mfe->getASTContext(), mth_decl));
359392
llvm::Module *qtmod = NULL;
360393

361-
if (mman->contains(modname)) {
394+
if (mman->has(modname)) {
362395
qtmod = mman->get(modname);
363396
} else {
364397
// auto mod = mce->conv_method(mfe->getASTContext(), mth_decl);
@@ -401,7 +434,7 @@ QVariant CtrlEngine::vm_call(void *kthis, QString klass, QString method, QVector
401434
QString lamsym = oe.bind(qtmod, symname, klass, uargs, dargs, mtdargs, mth_decl->isStatic(), kthis);
402435
qDebug()<<lamsym;
403436

404-
llvm::Module *remod = oe.bind(qtmod, klass, uargs, dargs, mtdargs, mth_decl->isStatic(), kthis);
437+
llvm::Module *remod = NULL;//oe.bind(qtmod, klass, uargs, dargs, mtdargs, mth_decl->isStatic(), kthis);
405438
DUMP_IR(remod);
406439
CodeUnit *cu = new CodeUnit(qtmod, remod);
407440

@@ -439,7 +472,7 @@ QVariant CtrlEngine::vm_static_call(QString klass, QString method, QVector<QVari
439472
QString modname = QString("qtmod%1").arg(mce->mangle_symbol(mfe->getASTContext(), mth_decl));
440473
llvm::Module *qtmod = NULL;
441474

442-
if (mman->contains(modname)) {
475+
if (mman->has(modname)) {
443476
qtmod = mman->get(modname);
444477
} else {
445478
// auto mod = mce->conv_method(mfe->getASTContext(), mth_decl);
@@ -456,7 +489,7 @@ QVariant CtrlEngine::vm_static_call(QString klass, QString method, QVector<QVari
456489
QString lamsym = oe.bind(qtmod, symname, klass, uargs, dargs, mtdargs, mth_decl->isStatic(), NULL);
457490
qDebug()<<lamsym;
458491

459-
llvm::Module *remod = oe.bind(qtmod, klass, uargs, dargs, mtdargs, mth_decl->isStatic(), NULL);
492+
llvm::Module *remod = NULL;// oe.bind(qtmod, klass, uargs, dargs, mtdargs, mth_decl->isStatic(), NULL);
460493
DUMP_IR(remod);
461494
CodeUnit *cu = new CodeUnit(qtmod, remod);
462495

@@ -527,7 +560,7 @@ QString CtrlEngine::vm_qdebug(void *kthis, QString klass)
527560
QString modname = QString("qtmod%1").arg(mce->mangle_symbol(mfe->getASTContext(), fun_decl));
528561
llvm::Module *qtmod = NULL;
529562

530-
if (mman->contains(modname)) {
563+
if (mman->has(modname)) {
531564
qtmod = mman->get(modname);
532565
} else {
533566
auto mod = mce->conv_function2(mfe->getASTUnit(), fun_decl);
@@ -542,7 +575,7 @@ QString CtrlEngine::vm_qdebug(void *kthis, QString klass)
542575
QString lamsym = oe.bind(qtmod, symname, klass, uargs, dargs, mtdargs, false, NULL);
543576
qDebug()<<lamsym;
544577

545-
llvm::Module *remod = oe.bind(qtmod, klass, uargs, dargs, mtdargs, false, NULL);
578+
llvm::Module *remod = NULL; // oe.bind(qtmod, klass, uargs, dargs, mtdargs, false, NULL);
546579
DUMP_IR(remod);
547580
CodeUnit *cu = new CodeUnit(qtmod, remod);
548581

ctrlengine.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <QtCore>
1212
#include <llvm/ExecutionEngine/GenericValue.h>
1313

14+
namespace llvm {
15+
class Module;
16+
};
17+
1418
namespace clang {
1519
class CompilerInstance;
1620
class CompilerInvocation;
@@ -42,9 +46,12 @@ class CtrlEngine
4246
QString vm_qdebug(void *kthis, QString klass);
4347

4448
// improve
45-
49+
4650
// hot fix
4751
QVariant vm_call_hotfix(void *kthis, QString klass, QString method, QVector<QVariant> uargs);
52+
53+
private:
54+
llvm::Module* process_dargs(QVector<QVariant> &dargs);
4855

4956
public:
5057
// 调度组件可能不需要这些组件,只管理调度问题。

docs/jit-ee-and-ir-reuse-improve.md

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
在实现了两个IR Module按功能拆分后,即可实现qtmod的重用了。
5151
而remod封装了不同的this对象指针和不同的参数,每次行完后,还是需要销毁的。
5252

53+
TODO remod的一个问题???
54+
是默认参数还没有编译到remod中。
5355

5456
### <h3>JIT引擎重用</h3>
5557

ivm/clvmengine.cpp

+35-7
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,26 @@ bool ClvmEngine::initExecutionEngine()
248248
return true;
249249
}
250250

251+
252+
llvm::ExecutionEngine *ClvmEngine::createEE(llvm::Module *mod)
253+
{
254+
// run module
255+
std::unique_ptr<llvm::Module> tmod(mod);
256+
llvm::EngineBuilder eb(std::move(tmod));
257+
258+
std::string errstr;
259+
eb.setErrorStr(&errstr);
260+
// eb.setUseMCJIT(true);
261+
262+
llvm::ExecutionEngine *EE = eb.create();
263+
264+
// ClvmJitListener *lsner = new ClvmJitListener();
265+
if (!mlsner) mlsner = ClvmJitListener::inst();
266+
EE->RegisterJITEventListener(mlsner);
267+
268+
return EE;
269+
}
270+
251271
// 执行IR代码中的一个入口函数
252272
llvm::GenericValue
253273
ClvmEngine::execute2(llvm::Module *mod, QString func_entry)
@@ -326,21 +346,28 @@ ClvmEngine::execute2(llvm::Module *mod, QString func_entry)
326346
}
327347

328348
// 使用类变量mee多次执行IR代码。
329-
llvm::GenericValue ClvmEngine::execute3(llvm::Module *mod, QString func_entry, CodeUnit *cu)
349+
llvm::GenericValue ClvmEngine::execute3(llvm::Module */*mod*/, QString func_entry, CodeUnit *cu)
330350
{
331-
llvm::ExecutionEngine *EE = mee;
351+
// llvm::ExecutionEngine *EE = mee;
332352
llvm::Module *qtmod = cu->qtmod;
333353
llvm::Module *remod = cu->remod;
354+
QString modname = QString::fromStdString(qtmod->getName().str());
334355

335-
mman->add(QString::fromStdString(mod->getName().str()), mod);
356+
llvm::ExecutionEngine *EE = mman->hasEE(modname) ? mman->getEE(modname) : this->createEE(qtmod);
357+
358+
mman->add(QString::fromStdString(qtmod->getName().str()), qtmod);
359+
mman->addEE(QString::fromStdString(qtmod->getName().str()), EE);
336360
// mman->add(QString::fromStdString(remod->getName().str()), remod);
361+
std::unique_ptr<llvm::Module> tremod(remod);
362+
EE->addModule(std::move(tremod));
363+
assert(tremod.get() == NULL);
337364

338365
EE->finalizeObject();
339366
EE->runStaticConstructorsDestructors(false);
340367

341368
llvm::Function *etyfn = NULL;
342369
QString mangle_name;
343-
llvm::ValueSymbolTable &vst = mod->getValueSymbolTable();
370+
llvm::ValueSymbolTable &vst = remod->getValueSymbolTable();
344371
for (auto sit = vst.begin(); sit != vst.end(); sit++) {
345372
auto k = sit->first();
346373
auto v = sit->second;
@@ -352,7 +379,7 @@ llvm::GenericValue ClvmEngine::execute3(llvm::Module *mod, QString func_entry, C
352379
}
353380
// if (QString(v->getName().data()).indexOf("jit_main") >= 0) {
354381
if (QString(v->getName().data()).indexOf(func_entry) >= 0) {
355-
etyfn = mod->getFunction(v->getName());
382+
etyfn = remod->getFunction(v->getName());
356383
mangle_name = QString(v->getName().data());
357384
// break;
358385
}
@@ -367,14 +394,15 @@ llvm::GenericValue ClvmEngine::execute3(llvm::Module *mod, QString func_entry, C
367394
qDebug()<<"our fun:"<<func_entry<<mangle_name2<<etyfn2;
368395

369396
std::vector<llvm::GenericValue> args;
370-
llvm::GenericValue rgv = EE->runFunction(etyfn, args);
397+
llvm::GenericValue rgv = EE->runFunction(etyfn2, args);
371398
// qDebug()<<"err:"<<errstr.c_str();
372399

373400
EE->runStaticConstructorsDestructors(true);
374401
// cleanups
375402
bool bret = false;
376403
// bret |= mman->remove(QString::fromStdString(remod->getName().str()));
377-
bret |= mman->remove(QString::fromStdString(mod->getName().str()));
404+
// bret |= mman->remove(QString::fromStdString(mod->getName().str()));
405+
EE->removeModule(remod);
378406
qDebug()<<bret;
379407

380408
qDebug()<<"run code done."<<llvm::GVTOP(rgv)<<rgv.IntVal.getZExtValue();

ivm/clvmengine.h

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public slots:
5555
bool init();
5656
bool initCompiler();
5757
bool initExecutionEngine();
58+
llvm::ExecutionEngine *createEE(llvm::Module *mod);
5859
llvm::GenericValue run_module_func(llvm::Module *mod, std::vector<llvm::GenericValue> & args,
5960
QString func_entry);
6061

ivm/misc.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "misc.h"
2+
3+
QString qStr(std::string ss)
4+
{
5+
return QString::fromStdString(ss);
6+
}
7+
8+
QString qStr(llvm::StringRef sr)
9+
{
10+
return QString::fromStdString(sr.str());
11+
}
12+
13+
const char *cStr(std::string ss)
14+
{
15+
return ss.c_str();
16+
}
17+
18+
const char *cStr(llvm::StringRef sr)
19+
{
20+
return sr.str().c_str();
21+
}
22+
23+
const char *cStr(QString qs)
24+
{
25+
return qs.toLatin1().data();
26+
}

ivm/misc.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef MISC_H
2+
#define MISC_H
3+
4+
// qt llvm std:: string helper
5+
#include <QString>
6+
#include <llvm/ADT/StringRef.h>
7+
8+
QString qStr(std::string ss);
9+
QString qStr(llvm::StringRef sr);
10+
11+
const char *cStr(std::string ss);
12+
const char *cStr(llvm::StringRef sr);
13+
const char *cStr(QString qs);
14+
15+
// QString to llvm:StringRef???
16+
17+
const static QString qtmod_prefix = "qtmod_";
18+
const static QString remod_prefix = "remod_";
19+
20+
21+
22+
#endif /* MISC_H */

0 commit comments

Comments
 (0)