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] map runtimeimports to their native name for ldftn #1843

Merged
merged 1 commit into from
Mar 8, 2022
Merged
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
98 changes: 56 additions & 42 deletions src/coreclr/tools/aot/ILCompiler.LLVM/CodeGen/ILToLLVMImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3069,47 +3069,7 @@ private void ImportRawPInvoke(MethodDesc method)

private ExpressionEntry ImportRawPInvoke(MethodDesc method, StackEntry[] arguments, LLVMBuilderRef builder, TypeDesc forcedReturnType = null)
{
string realMethodName = method.Name;

if (method.IsPInvoke)
{
string entrypointName = method.GetPInvokeMethodMetadata().Name;
if (!String.IsNullOrEmpty(entrypointName))
{
realMethodName = entrypointName;
}
}
else if (!method.IsPInvoke && method is TypeSystem.Ecma.EcmaMethod)
{
realMethodName = ((TypeSystem.Ecma.EcmaMethod)method).GetRuntimeImportName() ?? method.Name;
}
MethodDesc existantDesc;
LLVMValueRef nativeFunc;
LLVMValueRef realNativeFunc = Module.GetNamedFunction(realMethodName);
if (_pinvokeMap.TryGetValue(realMethodName, out existantDesc))
{
if (existantDesc != method)
{
// Set up native parameter types
nativeFunc = MakeExternFunction(method, realMethodName, realNativeFunc);
}
else
{
nativeFunc = realNativeFunc;
}
}
else
{
_pinvokeMap.Add(realMethodName, method);
nativeFunc = realNativeFunc;
}

// Create an import if we haven't already
if (nativeFunc.Handle == IntPtr.Zero)
{
// Set up native parameter types
nativeFunc = MakeExternFunction(method, realMethodName);
}
LLVMValueRef nativeFunc = GetInternalNativeFunction(method);

LLVMValueRef[] llvmArguments = new LLVMValueRef[method.Signature.Length];
for (int i = 0; i < arguments.Length; i++)
Expand Down Expand Up @@ -3290,7 +3250,13 @@ private void ImportLdFtn(int token, ILOpcode opCode)
}
else
{
if (canonMethod.IsSharedByGenericInstantiations && (canonMethod.HasInstantiation || canonMethod.Signature.IsStatic))
if (IsInternalRuntimeImport(method) && method is EcmaMethod)
{
// TODO-LLVM: Suspect this is going to crash if its ever called as we've lost the information that it's an unmanaged call
// Can we live with this for the IL->LLVM compilation and address it in RyuJIT?
targetLLVMFunction = GetInternalNativeFunction(method);
}
else if (canonMethod.IsSharedByGenericInstantiations && (canonMethod.HasInstantiation || canonMethod.Signature.IsStatic))
{
var exactContextNeedsRuntimeLookup = method.HasInstantiation
? method.IsSharedByGenericInstantiations
Expand Down Expand Up @@ -3349,6 +3315,54 @@ private void ImportLdFtn(int token, ILOpcode opCode)
_stack.Push(entry);
}

LLVMValueRef GetInternalNativeFunction(MethodDesc method)
{
string realMethodName = method.Name;

if (method.IsPInvoke)
{
string entrypointName = method.GetPInvokeMethodMetadata().Name;
if (!String.IsNullOrEmpty(entrypointName))
{
realMethodName = entrypointName;
}
}
else if (!method.IsPInvoke && method is TypeSystem.Ecma.EcmaMethod)
{
realMethodName = ((TypeSystem.Ecma.EcmaMethod)method).GetRuntimeImportName() ?? method.Name;
}

MethodDesc existantDesc;
LLVMValueRef nativeFunc;
LLVMValueRef realNativeFunc = Module.GetNamedFunction(realMethodName);
if (_pinvokeMap.TryGetValue(realMethodName, out existantDesc))
{
if (existantDesc != method)
{
// Set up native parameter types
nativeFunc = MakeExternFunction(method, realMethodName, realNativeFunc);
}
else
{
nativeFunc = realNativeFunc;
}
}
else
{
_pinvokeMap.Add(realMethodName, method);
nativeFunc = realNativeFunc;
}

// Create an import if we haven't already
if (nativeFunc.Handle == IntPtr.Zero)
{
// Set up native parameter types
nativeFunc = MakeExternFunction(method, realMethodName);
}

return nativeFunc;
}

ISymbolNode GetAndAddFatFunctionPointer(MethodDesc method, bool isUnboxingStub = false)
{
ISymbolNode node = _compilation.NodeFactory.FatFunctionPointer(method, isUnboxingStub);
Expand Down