Repository navigation
Lazy PInvoke resolution - #1240
Conversation
| ILLocalVariable vNativeFunctionPointer = _emitter.NewLocal(_targetMethod.Context.GetWellKnownType(WellKnownType.IntPtr)); | ||
|
|
||
| MetadataType lazyHelperType = _targetMethod.Context.GetHelperType("LazyPInvokeHelpers"); | ||
| if (_importMetadata.Module == "[MRT]" || _importMetadata.Module == "*" || _targetMethod.OwningType == lazyHelperType) |
There was a problem hiding this comment.
The [MRT] and * conditions are the result of me bashing on it until it worked. I'll sit down and find the right pattern tomorrow.
There was a problem hiding this comment.
*condition is fine.[MRT]would be ideally replaced with*as well. But we can live it for now. Replacing it with*would require modifying the .NET Native for UWP (adding MRT export library to the link step)._targetMethod.OwningType == lazyHelperTypeshould be changed to:
if (targeting Windows)
eager = _importMetadata.Module.StartsWith("api-ms-win-");
else
eager = _importMetadata.Module.StartsWith("System.Private.");
Eventually, we can have command line options or attributes to allow overriding this default policy.
|
cc @yizhang82 |
| // Eager call | ||
| MethodDesc nativeMethod = | ||
| new PInvokeTargetNativeMethod(_targetMethod.OwningType, nativeSig, _importMetadata); | ||
| fnptrLoadStream.Emit(ILOpcode.ldftn, _emitter.NewToken(nativeMethod)); |
There was a problem hiding this comment.
Can this just use call as before? No need to stress JIT optimizations... .
|
Incidentally I've happened to asked @tijoytom to look at supporting LoadLibrary in MCG today :) |
| } | ||
|
|
||
| if (!Internal.IL.Stubs.PInvokeMarshallingILEmitter.RequiresMarshalling(method)) | ||
| // Only treat the synthetic target methods as real PInvoke. |
There was a problem hiding this comment.
The pre-generated interop should still go directly here, without going through PInvokeMarshallingILEmitter. We should encapsulate it in helper method - something like:
static bool IsRawPInvoke(MethodDesc method)
{
return method.IsPInvoke && ((method is Internal.IL.Stubs.PInvokeTargetNativeMethod) || Internal.IL.McgInteropSupport.IsPregeneratedInterop(method));
}
There was a problem hiding this comment.
(I will have several uses for this helper in the GC transitions change that I am working on.)
96157f7 to
21045a2
Compare
| if (importModule == "[MRT]" || importModule == "*") | ||
| useLazyResolution = false; | ||
| else if (_targetMethod.Context.Target.IsWindows) | ||
| useLazyResolution = !importModule.StartsWith("api-ms-win-core-libraryloader-l1-2-0"); |
There was a problem hiding this comment.
Note to self: fix this before merging. There's not much to test if this is just api-ms-win-
21045a2 to
e7b9f54
Compare
|
|
||
| extern "C" void* CoreLibNative_LoadLibrary(const char* filename) | ||
| { | ||
| return dlopen(filename, RTLD_LAZY); |
There was a problem hiding this comment.
Might need to prepend lib + append .so depending on the platform conventions here, but I don't have a Linux/OSX machine to test with so...
|
@jkotas Removing the WIP tag. I'll file an up-for-grabs issue about making this work on Unix. |
|
LGTM |
112f9a7 to
38f2453
Compare
Work to resolve PInvoke entrypoints lazily. This enables us to support programs that have platform-specific light up behaviors (call platform APIs depending on the platform they are native-compiled for). It also removes the need to supply import libraries when linking the OBJ files (which is super convenient because managed developers are not used to it).