mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
wasm: remove i64 workaround, use BigInt instead
Browsers previously didn't support the WebAssembly i64 type, so we had to work around that limitation by converting the LLVM i64 type to something else. Some people used a pair of i32 values, but we used a pointer to a stack allocated i64. Now however, all major browsers and Node.js do support WebAssembly BigInt integration so that i64 values can be passed back and forth between WebAssembly and JavaScript easily. Therefore, I think the time has come to drop support for this workaround. For more information: https://v8.dev/features/wasm-bigint (note that TinyGo has used a slightly different way of passing i64 values between JS and Wasm). For information on browser support: https://webassembly.org/roadmap/
This commit is contained in:
committed by
Ron Evans
parent
6d5f4c4be2
commit
4d2a6d2bbe
Vendored
-28
@@ -1,28 +0,0 @@
|
||||
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
|
||||
target triple = "wasm32-unknown-unknown-wasm"
|
||||
|
||||
declare i64 @externalCall(ptr, i32, i64)
|
||||
|
||||
define internal i64 @testCall(ptr %ptr, i32 %len, i64 %foo) {
|
||||
%val = call i64 @externalCall(ptr %ptr, i32 %len, i64 %foo)
|
||||
ret i64 %val
|
||||
}
|
||||
|
||||
define internal i64 @testCallNonEntry(ptr %ptr, i32 %len) {
|
||||
entry:
|
||||
br label %bb1
|
||||
|
||||
bb1:
|
||||
%val = call i64 @externalCall(ptr %ptr, i32 %len, i64 3)
|
||||
ret i64 %val
|
||||
}
|
||||
|
||||
define void @exportedFunction(i64 %foo) {
|
||||
%unused = shl i64 %foo, 1
|
||||
ret void
|
||||
}
|
||||
|
||||
define internal void @callExportedFunction(i64 %foo) {
|
||||
call void @exportedFunction(i64 %foo)
|
||||
ret void
|
||||
}
|
||||
Vendored
-45
@@ -1,45 +0,0 @@
|
||||
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
|
||||
target triple = "wasm32-unknown-unknown-wasm"
|
||||
|
||||
declare i64 @"externalCall$i64wrap"(ptr, i32, i64)
|
||||
|
||||
define internal i64 @testCall(ptr %ptr, i32 %len, i64 %foo) {
|
||||
%i64asptr = alloca i64, align 8
|
||||
%i64asptr1 = alloca i64, align 8
|
||||
store i64 %foo, ptr %i64asptr1, align 8
|
||||
call void @externalCall(ptr %i64asptr, ptr %ptr, i32 %len, ptr %i64asptr1)
|
||||
%retval = load i64, ptr %i64asptr, align 8
|
||||
ret i64 %retval
|
||||
}
|
||||
|
||||
define internal i64 @testCallNonEntry(ptr %ptr, i32 %len) {
|
||||
entry:
|
||||
%i64asptr = alloca i64, align 8
|
||||
%i64asptr1 = alloca i64, align 8
|
||||
br label %bb1
|
||||
|
||||
bb1: ; preds = %entry
|
||||
store i64 3, ptr %i64asptr1, align 8
|
||||
call void @externalCall(ptr %i64asptr, ptr %ptr, i32 %len, ptr %i64asptr1)
|
||||
%retval = load i64, ptr %i64asptr, align 8
|
||||
ret i64 %retval
|
||||
}
|
||||
|
||||
define internal void @"exportedFunction$i64wrap"(i64 %foo) unnamed_addr {
|
||||
%unused = shl i64 %foo, 1
|
||||
ret void
|
||||
}
|
||||
|
||||
define internal void @callExportedFunction(i64 %foo) {
|
||||
call void @"exportedFunction$i64wrap"(i64 %foo)
|
||||
ret void
|
||||
}
|
||||
|
||||
declare void @externalCall(ptr, ptr, i32, ptr)
|
||||
|
||||
define void @exportedFunction(ptr %0) {
|
||||
entry:
|
||||
%i64 = load i64, ptr %0, align 8
|
||||
call void @"exportedFunction$i64wrap"(i64 %i64)
|
||||
ret void
|
||||
}
|
||||
@@ -1,167 +0,0 @@
|
||||
package transform
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/tinygo-org/tinygo/compileopts"
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
|
||||
// ExternalInt64AsPtr converts i64 parameters in externally-visible functions to
|
||||
// values passed by reference (*i64), to work around the lack of 64-bit integers
|
||||
// in JavaScript (commonly used together with WebAssembly). Once that's
|
||||
// resolved, this pass may be avoided. For more details:
|
||||
// https://github.com/WebAssembly/design/issues/1172
|
||||
//
|
||||
// This pass is enabled via the wasm-abi JSON target key.
|
||||
func ExternalInt64AsPtr(mod llvm.Module, config *compileopts.Config) error {
|
||||
ctx := mod.Context()
|
||||
builder := ctx.NewBuilder()
|
||||
defer builder.Dispose()
|
||||
int64Type := ctx.Int64Type()
|
||||
int64PtrType := llvm.PointerType(int64Type, 0)
|
||||
|
||||
// This builder is only used for creating new allocas in the entry block of
|
||||
// a function, avoiding many SetInsertPoint* calls.
|
||||
entryBlockBuilder := ctx.NewBuilder()
|
||||
defer entryBlockBuilder.Dispose()
|
||||
|
||||
for fn := mod.FirstFunction(); !fn.IsNil(); fn = llvm.NextFunction(fn) {
|
||||
if fn.Linkage() != llvm.ExternalLinkage {
|
||||
// Only change externally visible functions (exports and imports).
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(fn.Name(), "llvm.") || strings.HasPrefix(fn.Name(), "runtime.") {
|
||||
// Do not try to modify the signature of internal LLVM functions and
|
||||
// assume that runtime functions are only temporarily exported for
|
||||
// transforms.
|
||||
continue
|
||||
}
|
||||
if !fn.GetStringAttributeAtIndex(-1, "tinygo-methods").IsNil() {
|
||||
// These are internal functions (interface method call, interface
|
||||
// type assert) that will be lowered by the interface lowering pass.
|
||||
// Don't transform them.
|
||||
continue
|
||||
}
|
||||
|
||||
hasInt64 := false
|
||||
paramTypes := []llvm.Type{}
|
||||
|
||||
// Check return type for 64-bit integer.
|
||||
fnType := fn.GlobalValueType()
|
||||
returnType := fnType.ReturnType()
|
||||
if returnType == int64Type {
|
||||
hasInt64 = true
|
||||
paramTypes = append(paramTypes, int64PtrType)
|
||||
returnType = ctx.VoidType()
|
||||
}
|
||||
|
||||
// Check param types for 64-bit integers.
|
||||
for param := fn.FirstParam(); !param.IsNil(); param = llvm.NextParam(param) {
|
||||
if param.Type() == int64Type {
|
||||
hasInt64 = true
|
||||
paramTypes = append(paramTypes, int64PtrType)
|
||||
} else {
|
||||
paramTypes = append(paramTypes, param.Type())
|
||||
}
|
||||
}
|
||||
|
||||
if !hasInt64 {
|
||||
// No i64 in the paramter list.
|
||||
continue
|
||||
}
|
||||
|
||||
// Add $i64wrapper to the real function name as it is only used
|
||||
// internally.
|
||||
// Add a new function with the correct signature that is exported.
|
||||
name := fn.Name()
|
||||
fn.SetName(name + "$i64wrap")
|
||||
externalFnType := llvm.FunctionType(returnType, paramTypes, fnType.IsFunctionVarArg())
|
||||
externalFn := llvm.AddFunction(mod, name, externalFnType)
|
||||
AddStandardAttributes(fn, config)
|
||||
|
||||
if fn.IsDeclaration() {
|
||||
// Just a declaration: the definition doesn't exist on the Go side
|
||||
// so it cannot be called from external code.
|
||||
// Update all users to call the external function.
|
||||
// The old $i64wrapper function could be removed, but it may as well
|
||||
// be left in place.
|
||||
for _, call := range getUses(fn) {
|
||||
entryBlockBuilder.SetInsertPointBefore(call.InstructionParent().Parent().EntryBasicBlock().FirstInstruction())
|
||||
builder.SetInsertPointBefore(call)
|
||||
callParams := []llvm.Value{}
|
||||
var retvalAlloca llvm.Value
|
||||
if fnType.ReturnType() == int64Type {
|
||||
retvalAlloca = entryBlockBuilder.CreateAlloca(int64Type, "i64asptr")
|
||||
callParams = append(callParams, retvalAlloca)
|
||||
}
|
||||
for i := 0; i < call.OperandsCount()-1; i++ {
|
||||
operand := call.Operand(i)
|
||||
if operand.Type() == int64Type {
|
||||
// Pass a stack-allocated pointer instead of the value
|
||||
// itself.
|
||||
alloca := entryBlockBuilder.CreateAlloca(int64Type, "i64asptr")
|
||||
builder.CreateStore(operand, alloca)
|
||||
callParams = append(callParams, alloca)
|
||||
} else {
|
||||
// Unchanged parameter.
|
||||
callParams = append(callParams, operand)
|
||||
}
|
||||
}
|
||||
var callName string
|
||||
if returnType.TypeKind() != llvm.VoidTypeKind {
|
||||
// Only use the name of the old call instruction if the new
|
||||
// call is not a void call.
|
||||
// A call instruction with an i64 return type may have had a
|
||||
// name, but it cannot have a name after this transform
|
||||
// because the return type will now be void.
|
||||
callName = call.Name()
|
||||
}
|
||||
if fnType.ReturnType() == int64Type {
|
||||
// Pass a stack-allocated pointer as the first parameter
|
||||
// where the return value should be stored, instead of using
|
||||
// the regular return value.
|
||||
builder.CreateCall(externalFnType, externalFn, callParams, callName)
|
||||
returnValue := builder.CreateLoad(int64Type, retvalAlloca, "retval")
|
||||
call.ReplaceAllUsesWith(returnValue)
|
||||
call.EraseFromParentAsInstruction()
|
||||
} else {
|
||||
newCall := builder.CreateCall(externalFnType, externalFn, callParams, callName)
|
||||
call.ReplaceAllUsesWith(newCall)
|
||||
call.EraseFromParentAsInstruction()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// The function has a definition in Go. This means that it may still
|
||||
// be called both Go and from external code.
|
||||
// Keep existing calls with the existing convention in place (for
|
||||
// better performance), but export a new wrapper function with the
|
||||
// correct calling convention.
|
||||
fn.SetLinkage(llvm.InternalLinkage)
|
||||
fn.SetUnnamedAddr(true)
|
||||
entryBlock := ctx.AddBasicBlock(externalFn, "entry")
|
||||
builder.SetInsertPointAtEnd(entryBlock)
|
||||
var callParams []llvm.Value
|
||||
if fnType.ReturnType() == int64Type {
|
||||
return errors.New("not yet implemented: exported function returns i64 with the JS wasm-abi; " +
|
||||
"see https://tinygo.org/compiler-internals/calling-convention/")
|
||||
}
|
||||
for i, origParam := range fn.Params() {
|
||||
paramValue := externalFn.Param(i)
|
||||
if origParam.Type() == int64Type {
|
||||
paramValue = builder.CreateLoad(int64Type, paramValue, "i64")
|
||||
}
|
||||
callParams = append(callParams, paramValue)
|
||||
}
|
||||
retval := builder.CreateCall(fn.GlobalValueType(), fn, callParams, "")
|
||||
if retval.Type().TypeKind() == llvm.VoidTypeKind {
|
||||
builder.CreateRetVoid()
|
||||
} else {
|
||||
builder.CreateRet(retval)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package transform_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/tinygo-org/tinygo/transform"
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
|
||||
func TestWasmABI(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTransform(t, "testdata/wasm-abi", func(mod llvm.Module) {
|
||||
// Run ABI change pass.
|
||||
err := transform.ExternalInt64AsPtr(mod, defaultTestConfig)
|
||||
if err != nil {
|
||||
t.Errorf("failed to change wasm ABI: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user