Skip to content

Commit 4eec52f

Browse files
committed
compiler: refactor handing of functions
Previously, all functions were processed first by the ir package which scanned them all for reachability. Only the reachable functions were compiled to LLVM IR. This change removes this dead code elimination pass, and in fact refactors most of that infrastructure. This cleans up the compiler and should open the door for caching the compilation and optimization of packages.
1 parent bbc3046 commit 4eec52f

File tree

9 files changed

+395
-539
lines changed

9 files changed

+395
-539
lines changed

compiler/asserts.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// slice. This is required by the Go language spec: an index out of bounds must
1414
// cause a panic.
1515
func (c *Compiler) emitLookupBoundsCheck(frame *Frame, arrayLen, index llvm.Value, indexType types.Type) {
16-
if frame.fn.IsNoBounds() {
16+
if frame.info.nobounds {
1717
// The //go:nobounds pragma was added to the function to avoid bounds
1818
// checking.
1919
return
@@ -32,8 +32,8 @@ func (c *Compiler) emitLookupBoundsCheck(frame *Frame, arrayLen, index llvm.Valu
3232
arrayLen = c.builder.CreateZExt(arrayLen, index.Type(), "")
3333
}
3434

35-
faultBlock := c.ctx.AddBasicBlock(frame.fn.LLVMFn, "lookup.outofbounds")
36-
nextBlock := c.ctx.AddBasicBlock(frame.fn.LLVMFn, "lookup.next")
35+
faultBlock := c.ctx.AddBasicBlock(frame.llvmFn, "lookup.outofbounds")
36+
nextBlock := c.ctx.AddBasicBlock(frame.llvmFn, "lookup.next")
3737
frame.blockExits[frame.currentBlock] = nextBlock // adjust outgoing block for phi nodes
3838

3939
// Now do the bounds check: index >= arrayLen
@@ -57,7 +57,7 @@ func (c *Compiler) emitLookupBoundsCheck(frame *Frame, arrayLen, index llvm.Valu
5757
// biggest possible slice capacity, 'low' means len and 'high' means cap. The
5858
// logic is the same in both cases.
5959
func (c *Compiler) emitSliceBoundsCheck(frame *Frame, capacity, low, high, max llvm.Value, lowType, highType, maxType *types.Basic) {
60-
if frame.fn.IsNoBounds() {
60+
if frame.info.nobounds {
6161
// The //go:nobounds pragma was added to the function to avoid bounds
6262
// checking.
6363
return
@@ -101,8 +101,8 @@ func (c *Compiler) emitSliceBoundsCheck(frame *Frame, capacity, low, high, max l
101101
}
102102
}
103103

104-
faultBlock := c.ctx.AddBasicBlock(frame.fn.LLVMFn, "slice.outofbounds")
105-
nextBlock := c.ctx.AddBasicBlock(frame.fn.LLVMFn, "slice.next")
104+
faultBlock := c.ctx.AddBasicBlock(frame.llvmFn, "slice.outofbounds")
105+
nextBlock := c.ctx.AddBasicBlock(frame.llvmFn, "slice.next")
106106
frame.blockExits[frame.currentBlock] = nextBlock // adjust outgoing block for phi nodes
107107

108108
// Now do the bounds check: low > high || high > capacity
@@ -132,8 +132,8 @@ func (c *Compiler) emitNilCheck(frame *Frame, ptr llvm.Value, blockPrefix string
132132
}
133133

134134
// Check whether this is a nil pointer.
135-
faultBlock := c.ctx.AddBasicBlock(frame.fn.LLVMFn, blockPrefix+".nil")
136-
nextBlock := c.ctx.AddBasicBlock(frame.fn.LLVMFn, blockPrefix+".next")
135+
faultBlock := c.ctx.AddBasicBlock(frame.llvmFn, blockPrefix+".nil")
136+
nextBlock := c.ctx.AddBasicBlock(frame.llvmFn, blockPrefix+".next")
137137
frame.blockExits[frame.currentBlock] = nextBlock // adjust outgoing block for phi nodes
138138

139139
// Compare against nil.

compiler/calls.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ func (c *Compiler) createRuntimeCall(fnName string, args []llvm.Value, name stri
1919
if member == nil {
2020
panic("trying to call runtime." + fnName)
2121
}
22-
fn := c.ir.GetFunction(member.(*ssa.Function))
23-
if !fn.IsExported() {
22+
fn := member.(*ssa.Function)
23+
if !c.getFunctionInfo(fn).exported {
2424
args = append(args, llvm.Undef(c.i8ptrType)) // unused context parameter
2525
args = append(args, llvm.ConstPointerNull(c.i8ptrType)) // coroutine handle
2626
}
27-
return c.createCall(fn.LLVMFn, args, name)
27+
return c.createCall(c.getFunction(fn), args, name)
2828
}
2929

3030
// Create a call to the given function with the arguments possibly expanded.

0 commit comments

Comments
 (0)