Index: cmd/gllgo/gllgo.go =================================================================== --- cmd/gllgo/gllgo.go +++ cmd/gllgo/gllgo.go @@ -64,6 +64,7 @@ DebugPrefixMaps: opts.debugPrefixMaps, DumpSSA: opts.dumpSSA, GccgoPath: opts.gccgoPath, + GccgoABI: opts.gccgoPath != "", ImportPaths: importPaths, SanitizerAttribute: opts.sanitizer.getAttribute(), } Index: irgen/compiler.go =================================================================== --- irgen/compiler.go +++ irgen/compiler.go @@ -75,6 +75,9 @@ // path in ImportPaths. GccgoPath string + // Whether to use the gccgo ABI. + GccgoABI bool + // ImportPaths is the list of additional import paths ImportPaths []string @@ -316,8 +319,6 @@ } func (c *compiler) createInitMainFunction(mainPkg *ssa.Package) { - initdata := c.buildPackageInitData(mainPkg) - ftyp := llvm.FunctionType(llvm.VoidType(), nil, false) initMain := llvm.AddFunction(c.module.Module, "__go_init_main", ftyp) c.addCommonFunctionAttrs(initMain) @@ -327,6 +328,17 @@ defer builder.Dispose() builder.SetInsertPointAtEnd(entry) + if !c.GccgoABI { + initfn := c.module.Module.NamedFunction("main..import") + if !initfn.IsNil() { + builder.CreateCall(initfn, nil, "") + } + builder.CreateRetVoid() + return + } + + initdata := c.buildPackageInitData(mainPkg) + for _, init := range initdata.Inits { initfn := c.module.Module.NamedFunction(init.InitFunc) if initfn.IsNil() { @@ -342,8 +354,13 @@ exportData := importer.ExportData(mainPkg.Object) b := bytes.NewBuffer(exportData) + b.WriteString("v1;\n") + if !c.GccgoABI { + return b.Bytes() + } + initdata := c.buildPackageInitData(mainPkg) - b.WriteString("v1;\npriority ") + b.WriteString("priority ") b.WriteString(strconv.Itoa(initdata.Priority)) b.WriteString(";\n") Index: irgen/ssa.go =================================================================== --- irgen/ssa.go +++ irgen/ssa.go @@ -409,11 +409,6 @@ } } - // If this is the "init" function, enable init-specific optimizations. - if !isMethod && f.Name() == "init" { - fr.isInit = true - } - // If the function contains any defers, we must first create // an unwind block. We can short-circuit the check for defers with // f.Recover != nil. @@ -422,8 +417,47 @@ fr.frameptr = fr.builder.CreateAlloca(llvm.Int8Type(), "") } - term := fr.builder.CreateBr(fr.blocks[0]) - fr.allocaBuilder.SetInsertPointBefore(term) + // Keep track of the block into which we need to insert the call + // to __go_register_gc_roots. This needs to be inserted after the + // init guard check under the llgo ABI. + registerGcBlock := prologueBlock + + // If this is the "init" function, emit the init guard check and + // enable init-specific optimizations. + if !isMethod && f.Name() == "init" { + if !fr.GccgoABI { + initGuard := llvm.AddGlobal(fr.module.Module, llvm.Int1Type(), "init$guard") + initGuard.SetLinkage(llvm.InternalLinkage) + initGuard.SetInitializer(llvm.ConstNull(llvm.Int1Type())) + + returnBlock := llvm.AddBasicBlock(fr.function, "") + initBlock := llvm.AddBasicBlock(fr.function, "") + registerGcBlock = initBlock + + initGuardVal := fr.builder.CreateLoad(initGuard, "") + fr.builder.CreateCondBr(initGuardVal, returnBlock, initBlock) + + fr.builder.SetInsertPointAtEnd(returnBlock) + fr.builder.CreateRetVoid() + + fr.builder.SetInsertPointAtEnd(initBlock) + fr.builder.CreateStore(llvm.ConstInt(llvm.Int1Type(), 1, false), initGuard) + ftyp := llvm.FunctionType(llvm.VoidType(), nil, false) + for _, pkg := range u.pkg.Object.Imports() { + initname := ManglePackagePath(pkg.Path()) + "..import" + initfn := u.module.Module.NamedFunction(initname) + if initfn.IsNil() { + initfn = llvm.AddFunction(u.module.Module, initname, ftyp) + } + fr.builder.CreateCall(initfn, nil, "") + } + } + + fr.isInit = true + } + + fr.builder.CreateBr(fr.blocks[0]) + fr.allocaBuilder.SetInsertPointBefore(prologueBlock.FirstInstruction()) for _, block := range f.DomPreorder() { fr.translateBlock(block, fr.blocks[block.Index]) @@ -439,7 +473,7 @@ // after generating code for it because allocations may have caused // additional GC roots to be created. if fr.isInit { - fr.builder.SetInsertPointBefore(prologueBlock.FirstInstruction()) + fr.builder.SetInsertPointBefore(registerGcBlock.FirstInstruction()) fr.registerGcRoots() } } Index: test/irgen/imports.go =================================================================== --- /dev/null +++ test/irgen/imports.go @@ -0,0 +1,20 @@ +// RUN: llgo -S -emit-llvm -o - %s | FileCheck %s + +package foo + +import _ "fmt" + +var X interface{} + +// CHECK: @"init$guard" = internal global i1 false + +// CHECK: define void @foo..import() +// CHECK-NEXT: : +// CHECK-NEXT: %[[N:.*]] = load i1* @"init$guard" +// CHECK-NEXT: br i1 %[[N]], label %{{.*}}, label %[[L:.*]] + +// CHECK: ;