diff --git a/cl/_testpy/math/out.ll b/cl/_testpy/math/out.ll index 378284c6..f5feead3 100644 --- a/cl/_testpy/math/out.ll +++ b/cl/_testpy/math/out.ll @@ -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, ...) diff --git a/cl/compile.go b/cl/compile.go index a786981b..78dbb47b 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -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) diff --git a/py/math/llgo_autogen.lla b/py/math/llgo_autogen.lla index 8eaa159b..44b7ad79 100644 Binary files a/py/math/llgo_autogen.lla and b/py/math/llgo_autogen.lla differ diff --git a/py/os/llgo_autogen.lla b/py/os/llgo_autogen.lla index d9ad409c..e88a4ffa 100644 Binary files a/py/os/llgo_autogen.lla and b/py/os/llgo_autogen.lla differ