compiler: move ReplacePanicsWithTrap pass to transforms
This moves the transformation pass to the right location, and adds tests to see that it actually works correctly.
Этот коммит содержится в:
родитель
f49e69b02a
коммит
172efc26a7
5 изменённых файлов: 91 добавлений и 20 удалений
|
@ -20,7 +20,7 @@ func (c *Compiler) Optimize(optLevel, sizeLevel int, inlinerThreshold uint) erro
|
|||
builder.AddCoroutinePassesToExtensionPoints()
|
||||
|
||||
if c.PanicStrategy() == "trap" {
|
||||
c.replacePanicsWithTrap() // -panic=trap
|
||||
transform.ReplacePanicsWithTrap(c.mod) // -panic=trap
|
||||
}
|
||||
|
||||
// run a check of all of our code
|
||||
|
@ -147,22 +147,3 @@ func (c *Compiler) Optimize(optLevel, sizeLevel int, inlinerThreshold uint) erro
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Replace panic calls with calls to llvm.trap, to reduce code size. This is the
|
||||
// -panic=trap intrinsic.
|
||||
func (c *Compiler) replacePanicsWithTrap() {
|
||||
trap := c.mod.NamedFunction("llvm.trap")
|
||||
for _, name := range []string{"runtime._panic", "runtime.runtimePanic"} {
|
||||
fn := c.mod.NamedFunction(name)
|
||||
if fn.IsNil() {
|
||||
continue
|
||||
}
|
||||
for _, use := range getUses(fn) {
|
||||
if use.IsACallInst().IsNil() || use.CalledValue() != fn {
|
||||
panic("expected use of a panic function to be a call")
|
||||
}
|
||||
c.builder.SetInsertPointBefore(use)
|
||||
c.builder.CreateCall(trap, nil, "")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
33
transform/panic.go
Обычный файл
33
transform/panic.go
Обычный файл
|
@ -0,0 +1,33 @@
|
|||
package transform
|
||||
|
||||
import (
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
|
||||
// ReplacePanicsWithTrap replaces each call to panic (or similar functions) with
|
||||
// calls to llvm.trap, to reduce code size. This is the -panic=trap command-line
|
||||
// option.
|
||||
func ReplacePanicsWithTrap(mod llvm.Module) {
|
||||
ctx := mod.Context()
|
||||
builder := ctx.NewBuilder()
|
||||
defer builder.Dispose()
|
||||
|
||||
trap := mod.NamedFunction("llvm.trap")
|
||||
if trap.IsNil() {
|
||||
trapType := llvm.FunctionType(ctx.VoidType(), nil, false)
|
||||
trap = llvm.AddFunction(mod, "llvm.trap", trapType)
|
||||
}
|
||||
for _, name := range []string{"runtime._panic", "runtime.runtimePanic"} {
|
||||
fn := mod.NamedFunction(name)
|
||||
if fn.IsNil() {
|
||||
continue
|
||||
}
|
||||
for _, use := range getUses(fn) {
|
||||
if use.IsACallInst().IsNil() || use.CalledValue() != fn {
|
||||
panic("expected use of a panic function to be a call")
|
||||
}
|
||||
builder.SetInsertPointBefore(use)
|
||||
builder.CreateCall(trap, nil, "")
|
||||
}
|
||||
}
|
||||
}
|
10
transform/panic_test.go
Обычный файл
10
transform/panic_test.go
Обычный файл
|
@ -0,0 +1,10 @@
|
|||
package transform
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReplacePanicsWithTrap(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTransform(t, "testdata/panic", ReplacePanicsWithTrap)
|
||||
}
|
22
transform/testdata/panic.ll
предоставленный
Обычный файл
22
transform/testdata/panic.ll
предоставленный
Обычный файл
|
@ -0,0 +1,22 @@
|
|||
target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
|
||||
target triple = "armv7m-none-eabi"
|
||||
|
||||
@"runtime.lookupPanic$string" = constant [18 x i8] c"index out of range"
|
||||
|
||||
declare void @runtime.runtimePanic(i8*, i32)
|
||||
|
||||
declare void @runtime._panic(i32, i8*)
|
||||
|
||||
define void @runtime.lookupPanic() {
|
||||
call void @runtime.runtimePanic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @"runtime.lookupPanic$string", i64 0, i64 0), i32 18)
|
||||
ret void
|
||||
}
|
||||
|
||||
; This is equivalent to the following code:
|
||||
; func someFunc(x interface{}) {
|
||||
; panic(x)
|
||||
; }
|
||||
define void @someFunc(i32 %typecode, i8* %value) {
|
||||
call void @runtime._panic(i32 %typecode, i8* %value)
|
||||
unreachable
|
||||
}
|
25
transform/testdata/panic.out.ll
предоставленный
Обычный файл
25
transform/testdata/panic.out.ll
предоставленный
Обычный файл
|
@ -0,0 +1,25 @@
|
|||
target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
|
||||
target triple = "armv7m-none-eabi"
|
||||
|
||||
@"runtime.lookupPanic$string" = constant [18 x i8] c"index out of range"
|
||||
|
||||
declare void @runtime.runtimePanic(i8*, i32)
|
||||
|
||||
declare void @runtime._panic(i32, i8*)
|
||||
|
||||
define void @runtime.lookupPanic() {
|
||||
call void @llvm.trap()
|
||||
call void @runtime.runtimePanic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @"runtime.lookupPanic$string", i64 0, i64 0), i32 18)
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @someFunc(i32 %typecode, i8* %value) {
|
||||
call void @llvm.trap()
|
||||
call void @runtime._panic(i32 %typecode, i8* %value)
|
||||
unreachable
|
||||
}
|
||||
|
||||
; Function Attrs: cold noreturn nounwind
|
||||
declare void @llvm.trap() #0
|
||||
|
||||
attributes #0 = { cold noreturn nounwind }
|
Загрузка…
Создание таблицы
Сослаться в новой задаче