Skip to content

Commit d8cc48b

Browse files
committed
compiler: remove ir package
This package was long making the design of the compiler more complicated than it needs to be. Previously this package implemented several optimization passes, but those passes have since moved to work directly with LLVM IR instead of Go SSA. The only remaining pass is the SimpleDCE pass. This commit removes the *ir.Function type that permeated the whole compiler and instead switches to use *ssa.Function directly. The SimpleDCE pass is kept but is far less tightly coupled to the rest of the compiler so that it can easily be removed once the switch to building and caching packages individually happens.
1 parent 9bd3659 commit d8cc48b

14 files changed

+567
-655
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ endif
8686
clean:
8787
@rm -rf build
8888

89-
FMT_PATHS = ./*.go builder cgo compiler interp ir loader src/device/arm src/examples src/machine src/os src/reflect src/runtime src/sync src/syscall src/internal/reflectlite transform
89+
FMT_PATHS = ./*.go builder cgo compiler interp loader src/device/arm src/examples src/machine src/os src/reflect src/runtime src/sync src/syscall src/internal/reflectlite transform
9090
fmt:
9191
@gofmt -l -w $(FMT_PATHS)
9292
fmt-check:

compiler/asserts.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// slice. This is required by the Go language spec: an index out of bounds must
1717
// cause a panic.
1818
func (b *builder) createLookupBoundsCheck(arrayLen, index llvm.Value, indexType types.Type) {
19-
if b.fn.IsNoBounds() {
19+
if b.info.nobounds {
2020
// The //go:nobounds pragma was added to the function to avoid bounds
2121
// checking.
2222
return
@@ -48,7 +48,7 @@ func (b *builder) createLookupBoundsCheck(arrayLen, index llvm.Value, indexType
4848
// biggest possible slice capacity, 'low' means len and 'high' means cap. The
4949
// logic is the same in both cases.
5050
func (b *builder) createSliceBoundsCheck(capacity, low, high, max llvm.Value, lowType, highType, maxType *types.Basic) {
51-
if b.fn.IsNoBounds() {
51+
if b.info.nobounds {
5252
// The //go:nobounds pragma was added to the function to avoid bounds
5353
// checking.
5454
return
@@ -104,7 +104,7 @@ func (b *builder) createSliceBoundsCheck(capacity, low, high, max llvm.Value, lo
104104
// createChanBoundsCheck creates a bounds check before creating a new channel to
105105
// check that the value is not too big for runtime.chanMake.
106106
func (b *builder) createChanBoundsCheck(elementSize uint64, bufSize llvm.Value, bufSizeType *types.Basic, pos token.Pos) {
107-
if b.fn.IsNoBounds() {
107+
if b.info.nobounds {
108108
// The //go:nobounds pragma was added to the function to avoid bounds
109109
// checking.
110110
return
@@ -189,7 +189,7 @@ func (b *builder) createNilCheck(inst ssa.Value, ptr llvm.Value, blockPrefix str
189189
// createNegativeShiftCheck creates an assertion that panics if the given shift value is negative.
190190
// This function assumes that the shift value is signed.
191191
func (b *builder) createNegativeShiftCheck(shift llvm.Value) {
192-
if b.fn.IsNoBounds() {
192+
if b.info.nobounds {
193193
// Function disabled bounds checking - skip shift check.
194194
return
195195
}
@@ -212,8 +212,8 @@ func (b *builder) createRuntimeAssert(assert llvm.Value, blockPrefix, assertFunc
212212
}
213213
}
214214

215-
faultBlock := b.ctx.AddBasicBlock(b.fn.LLVMFn, blockPrefix+".throw")
216-
nextBlock := b.ctx.AddBasicBlock(b.fn.LLVMFn, blockPrefix+".next")
215+
faultBlock := b.ctx.AddBasicBlock(b.llvmFn, blockPrefix+".throw")
216+
nextBlock := b.ctx.AddBasicBlock(b.llvmFn, blockPrefix+".next")
217217
b.blockExits[b.currentBlock] = nextBlock // adjust outgoing block for phi nodes
218218

219219
// Now branch to the out-of-bounds or the regular block.

compiler/calls.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"go/types"
55
"strconv"
66

7+
"golang.org/x/tools/go/ssa"
78
"tinygo.org/x/go-llvm"
89
)
910

@@ -34,14 +35,14 @@ const (
3435

3536
// createCall creates a new call to runtime.<fnName> with the given arguments.
3637
func (b *builder) createRuntimeCall(fnName string, args []llvm.Value, name string) llvm.Value {
37-
fullName := "runtime." + fnName
38-
fn := b.mod.NamedFunction(fullName)
39-
if fn.IsNil() {
40-
panic("trying to call non-existent function: " + fullName)
38+
fn := b.program.ImportedPackage("runtime").Members[fnName].(*ssa.Function)
39+
llvmFn := b.getFunction(fn)
40+
if llvmFn.IsNil() {
41+
panic("trying to call non-existent function: " + fn.RelString(nil))
4142
}
4243
args = append(args, llvm.Undef(b.i8ptrType)) // unused context parameter
4344
args = append(args, llvm.ConstPointerNull(b.i8ptrType)) // coroutine handle
44-
return b.createCall(fn, args, name)
45+
return b.createCall(llvmFn, args, name)
4546
}
4647

4748
// createCall creates a call to the given function with the arguments possibly

0 commit comments

Comments
 (0)