Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NativeAOT-LLVM: Change name of Wasm Import function name to include the module prefix #2410

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public override ISymbolNode GetExternalMethodAccessor(MethodDesc method, ReadOnl
{
Debug.Assert(!sig.IsEmpty);
string name = PInvokeILProvider.GetDirectCallExternName(method);
ConfigurableWasmImportPolicy.TryGetWasmModule(name, out string wasmModuleName);

return NodeFactory.ExternSymbolWithAccessor(name, method, sig);
return NodeFactory.ExternSymbolWithAccessor(name, method, sig, wasmModuleName);
}

public override CorInfoLlvmEHModel GetLlvmExceptionHandlingModel() => Options.ExceptionHandlingModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ private void GetCodeForUnboxThunkMethod(UnboxingStubNode node)
private void GetCodeForExternMethodAccessor(ExternMethodAccessorNode node)
{
// TODO-LLVM: use the Utf8 string directly here.
string externFuncName = node.ExternMethodName.ToString();
string externFuncName = node.ExternFuncName.ToString();
LLVMTypeRef externFuncType;

if (node.Signature != null)
Expand Down Expand Up @@ -811,10 +811,10 @@ private void GetCodeForExternMethodAccessor(ExternMethodAccessorNode node)
LLVMValueRef externFunc = externFuncModule.AddFunction(externFuncName, externFuncType);

// Add import attributes if specified.
if (_compilation.ConfigurableWasmImportPolicy.TryGetWasmModule(externFuncName, out string wasmModuleName))
if (node.WasmModuleName != null)
{
externFunc.AddFunctionAttribute("wasm-import-name", externFuncName);
externFunc.AddFunctionAttribute("wasm-import-module", wasmModuleName);
externFunc.AddFunctionAttribute("wasm-import-name", node.WasmImportFuncName);
externFunc.AddFunctionAttribute("wasm-import-module", node.WasmModuleName);
}

// Define the accessor function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ namespace ILCompiler.DependencyAnalysis
{
internal sealed class ExternMethodAccessorNode : AssemblyStubNode
{
private readonly Utf8String _externMethodName;
private readonly Utf8String _externFuncName;
private readonly string _wasmModuleName;
private readonly string _wasmImportFuncName;
private TargetAbiType[] _signature;
private object _methods;

public ExternMethodAccessorNode(string externMethodName)
public ExternMethodAccessorNode(string wasmImportFuncName, string wasmModuleName)
{
_externMethodName = externMethodName;
_wasmImportFuncName = wasmImportFuncName;
_wasmModuleName = wasmModuleName;

// If the function has a Wasm Import, construct a name that differentiates this function from a possible export
// of the same name.
_externFuncName = wasmModuleName == null ? wasmImportFuncName : wasmModuleName + "_" + wasmImportFuncName;
}

public ref readonly Utf8String ExternMethodName => ref _externMethodName;
// Name of the LLVM extern function
public ref readonly Utf8String ExternFuncName => ref _externFuncName;

public string WasmModuleName => _wasmModuleName;
public string WasmImportFuncName => _wasmImportFuncName;

public ref TargetAbiType[] Signature => ref _signature;

public void AddMethod(MethodDesc method)
Expand Down Expand Up @@ -69,14 +81,14 @@ public IEnumerable<MethodDesc> EnumerateMethods()
public override void AppendMangledName(NameMangler nameMangler, Utf8StringBuilder sb)
{
sb.Append("get.");
sb.Append(ExternMethodName);
sb.Append(WasmImportFuncName);
}

public override int ClassCode => 935251149;

public override int CompareToImpl(ISortableNode other, CompilerComparer comparer)
{
return ExternMethodName.CompareTo(((ExternMethodAccessorNode)other).ExternMethodName);
return WasmImportFuncName.CompareTo(((ExternMethodAccessorNode)other).WasmImportFuncName);
}

protected override void EmitCode(NodeFactory factory, ref X64Emitter instructionEncoder, bool relocsOnly) => throw new NotImplementedException();
Expand All @@ -86,6 +98,6 @@ public override int CompareToImpl(ISortableNode other, CompilerComparer comparer
protected override void EmitCode(NodeFactory factory, ref LoongArch64Emitter instructionEncoder, bool relocsOnly) => throw new NotImplementedException();
protected override void EmitCode(NodeFactory factory, ref WasmEmitter instructionEncoder, bool relocsOnly) { }

protected override string GetName(NodeFactory context) => $"ExternMethodAccessor {ExternMethodName}";
protected override string GetName(NodeFactory context) => $"ExternMethodAccessor {WasmImportFuncName}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override IMethodNode RuntimeExportManagedEntrypoint(string name)
return _runtimeExports.TryGetValue(name, out EcmaMethod export) ? MethodEntrypoint(export) : null;
}

internal ExternMethodAccessorNode ExternSymbolWithAccessor(string name, MethodDesc method, ReadOnlySpan<TargetAbiType> sig)
internal ExternMethodAccessorNode ExternSymbolWithAccessor(string name, MethodDesc method, ReadOnlySpan<TargetAbiType> sig, string wasmModuleName)
{
Dictionary<string, ExternMethodAccessorNode> map = _externSymbolsWithAccessors;

Expand All @@ -59,7 +59,7 @@ internal ExternMethodAccessorNode ExternSymbolWithAccessor(string name, MethodDe

if (!exists)
{
node = new ExternMethodAccessorNode(name);
node = new ExternMethodAccessorNode(name, wasmModuleName);
node.Signature = sig.ToArray();
}
else if (!node.Signature.AsSpan().SequenceEqual(sig))
Expand Down