Merge pull request #154 from xushiwei/q

compileBlock bugfix: pyMod
This commit is contained in:
xushiwei
2024-05-13 00:52:57 +08:00
committed by GitHub
4 changed files with 28 additions and 22 deletions

View File

@@ -5,7 +5,6 @@ source_filename = "math"
@"math.init$guard" = global ptr null
@__llgo_py.math = linkonce global ptr null
@0 = private unnamed_addr constant [5 x i8] c"math\00", align 1
@1 = private unnamed_addr constant [5 x i8] c"sqrt\00", align 1
define void @math.init() {
_llgo_0:
@@ -15,20 +14,16 @@ _llgo_0:
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"math.init$guard", align 1
%1 = load ptr, ptr @__llgo_py.math, align 8
call void (ptr, ...) @llgoLoadPyModSyms(ptr %1, ptr @1, ptr @__llgo_py.math.sqrt, ptr null)
%2 = load ptr, ptr @__llgo_py.math, align 8
%3 = icmp ne ptr %2, null
br i1 %3, label %_llgo_2, label %_llgo_3
%2 = icmp ne ptr %1, null
br i1 %2, label %_llgo_2, label %_llgo_3
_llgo_2: ; preds = %_llgo_3, %_llgo_1, %_llgo_0
ret void
_llgo_3: ; preds = %_llgo_1
%4 = call ptr @PyImport_ImportModule(ptr @0)
store ptr %4, ptr @__llgo_py.math, align 8
%3 = call ptr @PyImport_ImportModule(ptr @0)
store ptr %3, ptr @__llgo_py.math, align 8
br label %_llgo_2
}
declare ptr @PyImport_ImportModule(ptr)
declare void @llgoLoadPyModSyms(ptr, ...)

View File

@@ -347,20 +347,31 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do
if pyModInit = p.pyMod != ""; pyModInit {
last = len(instrs) - 1
instrs = instrs[:last]
} else {
// TODO(xsw): confirm pyMod don't need to call LoadPyModSyms
p.inits = append(p.inits, func() {
if objs := pkg.PyObjs(); len(objs) > 0 {
mods := make(map[string][]llssa.PyObjRef)
for name, obj := range objs {
modName := modOf(name)
mods[modName] = append(mods[modName], obj)
}
// sort by module name
modNames := make([]string, 0, len(mods))
for modName := range mods {
modNames = append(modNames, modName)
}
sort.Strings(modNames)
b.SetBlockEx(ret, llssa.AfterInit)
for _, modName := range modNames {
objs := mods[modName]
b.LoadPyModSyms(modName, objs...)
}
}
})
}
p.inits = append(p.inits, func() {
if objs := pkg.PyObjs(); len(objs) > 0 {
mods := make(map[string][]llssa.PyObjRef)
for name, obj := range objs {
modName := modOf(name)
mods[modName] = append(mods[modName], obj)
}
b.SetBlockEx(ret, llssa.AfterInit)
for modName, objs := range mods {
b.LoadPyModSyms(modName, objs...)
}
}
})
} else if doMainInit {
fn := p.fn
argc := pkg.NewVar("__llgo_argc", types.NewPointer(types.Typ[types.Int32]), llssa.InC)

Binary file not shown.

Binary file not shown.