Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ssa/eh.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,10 @@ func (p Function) endDefer(b Builder) {
b.Jump(rethNext)
}
}
// With no defer statements, the loop does not position the builder.
if n == 0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The n == 0 branch encodes a subtle, load-bearing invariant with no comment: the n >= 1 loop above implicitly leaves the builder positioned on rethsNext[1] (which equals procBlk when n == 1, since the final i == 0 iteration skips its Jump), so the trailing SetThreadDefer / IndirectJump land in procBlk. When n == 0 the loop never runs and nothing sets the insert point, so this branch is required for those instructions to be emitted into procBlk.

Consider a one-line comment, e.g.:

// No defer stmts: the loop above never runs, so position the builder
// on procBlk directly before emitting the tail (SetThreadDefer/IndirectJump).
if n == 0 {
    b.SetBlockEx(procBlk, AtEnd, true)
}

This matches the inline-comment style already used nearby (e.g. eh.go:601) and prevents a future maintainer from mistaking the branch for dead/redundant code.

b.SetBlockEx(procBlk, AtEnd, true)
}
link := b.getField(b.Load(self.data), deferLink)
b.Call(b.Pkg.rtFunc("SetThreadDefer"), link)
b.IndirectJump(b.Load(rundPtr), nexts)
Expand Down
12 changes: 9 additions & 3 deletions ssa/eh_defer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/goplus/llgo/ssa"
"github.com/goplus/llgo/ssa/ssatest"
"github.com/xgo-dev/llvm"
)

func TestExplicitDeferStackIR(t *testing.T) {
Expand Down Expand Up @@ -67,16 +68,18 @@ func TestExplicitDeferStackFallbackAndNilBuiltin(t *testing.T) {
}
}

func TestExplicitDeferStackDrainWithoutLoopCases(t *testing.T) {
func TestExplicitDeferStackWithoutDeferredActions(t *testing.T) {
prog := ssatest.NewProgram(t, nil)
pkg := prog.NewPackage("foo", "foo")

fn := pkg.NewFunc("main", ssa.NoArgsNoRet, ssa.InGo)
b := fn.MakeBody(1)
fn.SetRecover(fn.MakeBlock())
recoverBlock := fn.MakeBlock()
fn.SetRecover(recoverBlock)
b.SetBlock(recoverBlock).Return()
b.SetBlock(fn.Block(0))

_ = b.BuiltinCall("ssa:deferstack")
b.DeferStackDrain()
b.RunDefers()
b.Return()
b.EndBuild()
Expand All @@ -88,6 +91,9 @@ func TestExplicitDeferStackDrainWithoutLoopCases(t *testing.T) {
if !strings.Contains(ir, "sigsetjmp") && !strings.Contains(ir, "setjmp") {
t.Fatalf("expected defer stack setup with recover, got:\n%s", ir)
}
if err := llvm.VerifyModule(pkg.Module(), llvm.ReturnStatusAction); err != nil {
t.Fatalf("explicit defer stack without loop cases produced invalid IR: %v\n%s", err, ir)
}
}

func TestExplicitDeferStackDrainWithoutRecoverNoop(t *testing.T) {
Expand Down
Loading