all: move -panic=trap support to the compiler/runtime

Support for `-panic=trap` was previously a pass in the optimization
pipeline. This change moves it to the compiler and runtime, which in my
opinion is a much better place.

As a side effect, it also fixes
https://github.com/tinygo-org/tinygo/issues/4161 by trapping inside
runtime.runtimePanicAt and not just runtime.runtimePanic.

This change also adds a test for the list of imported functions. This is
a more generic test where it's easy to add more tests for WebAssembly
file properties, such as exported functions.
This commit is contained in:
Ayke van Laethem
2024-03-16 16:31:36 +01:00
committed by Ron Evans
parent 6384ecace0
commit ad4d722f54
10 changed files with 86 additions and 96 deletions
-4
View File
@@ -40,10 +40,6 @@ func Optimize(mod llvm.Module, config *compileopts.Config) []error {
fn.SetLinkage(llvm.ExternalLinkage)
}
if config.PanicStrategy() == "trap" {
ReplacePanicsWithTrap(mod) // -panic=trap
}
// run a check of all of our code
if config.VerifyIR() {
errs := ircheck.Module(mod)
-33
View File
@@ -1,33 +0,0 @@
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.GlobalValueType(), trap, nil, "")
}
}
}
-12
View File
@@ -1,12 +0,0 @@
package transform_test
import (
"testing"
"github.com/tinygo-org/tinygo/transform"
)
func TestReplacePanicsWithTrap(t *testing.T) {
t.Parallel()
testTransform(t, "testdata/panic", transform.ReplacePanicsWithTrap)
}
-22
View File
@@ -1,22 +0,0 @@
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(ptr, i32)
declare void @runtime._panic(i32, ptr)
define void @runtime.lookupPanic() {
call void @runtime.runtimePanic(ptr @"runtime.lookupPanic$string", i32 18)
ret void
}
; This is equivalent to the following code:
; func someFunc(x interface{}) {
; panic(x)
; }
define void @someFunc(i32 %typecode, ptr %value) {
call void @runtime._panic(i32 %typecode, ptr %value)
unreachable
}
-25
View File
@@ -1,25 +0,0 @@
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(ptr, i32)
declare void @runtime._panic(i32, ptr)
define void @runtime.lookupPanic() {
call void @llvm.trap()
call void @runtime.runtimePanic(ptr @"runtime.lookupPanic$string", i32 18)
ret void
}
define void @someFunc(i32 %typecode, ptr %value) {
call void @llvm.trap()
call void @runtime._panic(i32 %typecode, ptr %value)
unreachable
}
; Function Attrs: cold noreturn nounwind
declare void @llvm.trap() #0
attributes #0 = { cold noreturn nounwind }
+1
View File
@@ -137,6 +137,7 @@ func compileGoFileForTesting(t *testing.T, filename string) llvm.Module {
Scheduler: config.Scheduler(),
AutomaticStackSize: config.AutomaticStackSize(),
Debug: true,
PanicStrategy: config.PanicStrategy(),
}
machine, err := compiler.NewTargetMachine(compilerConfig)
if err != nil {