Skip to content

Commit dcff8b6

Browse files
authored
Namespaced Wasm Imports so they don't conflict across modules, or reserved LLVM IR (#1661)
wasm: namespaced all of the wasm import functions
1 parent 6862942 commit dcff8b6

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

compiler/symbol.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ import (
2121
// The linkName value contains a valid link name, even if //go:linkname is not
2222
// present.
2323
type functionInfo struct {
24-
module string // go:wasm-module
25-
linkName string // go:linkname, go:export
26-
exported bool // go:export, CGo
27-
nobounds bool // go:nobounds
28-
variadic bool // go:variadic (CGo only)
29-
inline inlineType // go:inline
24+
module string // go:wasm-module
25+
importName string // go:linkname, go:export - The name the developer assigns
26+
linkName string // go:linkname, go:export - The name that we map for the particular module -> importName
27+
exported bool // go:export, CGo
28+
nobounds bool // go:nobounds
29+
variadic bool // go:variadic (CGo only)
30+
inline inlineType // go:inline
3031
}
3132

3233
type inlineType int
@@ -151,8 +152,16 @@ func (c *compilerContext) getFunction(fn *ssa.Function) llvm.Value {
151152
if info.exported {
152153
// Set the wasm-import-module attribute if the function's module is set.
153154
if info.module != "" {
155+
156+
// We need to add the wasm-import-module and the wasm-import-name
154157
wasmImportModuleAttr := c.ctx.CreateStringAttribute("wasm-import-module", info.module)
155158
llvmFn.AddFunctionAttr(wasmImportModuleAttr)
159+
160+
// Add the Wasm Import Name, if we are a named wasm import
161+
if info.importName != "" {
162+
wasmImportNameAttr := c.ctx.CreateStringAttribute("wasm-import-name", info.importName)
163+
llvmFn.AddFunctionAttr(wasmImportNameAttr)
164+
}
156165
}
157166
nocaptureKind := llvm.AttributeKindID("nocapture")
158167
nocapture := c.ctx.CreateEnumAttribute(nocaptureKind, 0)
@@ -191,6 +200,10 @@ func (info *functionInfo) parsePragmas(f *ssa.Function) {
191200
return
192201
}
193202
if decl, ok := f.Syntax().(*ast.FuncDecl); ok && decl.Doc != nil {
203+
204+
// Our importName for a wasm module (if we are compiling to wasm), or llvm link name
205+
var importName string
206+
194207
for _, comment := range decl.Doc.List {
195208
text := comment.Text
196209
if strings.HasPrefix(text, "//export ") {
@@ -207,7 +220,8 @@ func (info *functionInfo) parsePragmas(f *ssa.Function) {
207220
if len(parts) != 2 {
208221
continue
209222
}
210-
info.linkName = parts[1]
223+
224+
importName = parts[1]
211225
info.exported = true
212226
case "//go:wasm-module":
213227
// Alternative comment for setting the import module.
@@ -250,6 +264,17 @@ func (info *functionInfo) parsePragmas(f *ssa.Function) {
250264
}
251265
}
252266
}
267+
268+
// Set the importName for our exported function if we have one
269+
if importName != "" {
270+
if info.module == "" {
271+
info.linkName = importName
272+
} else {
273+
// WebAssembly import
274+
info.importName = importName
275+
}
276+
}
277+
253278
}
254279
}
255280

0 commit comments

Comments
 (0)