Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Scan all input assemblies for NativeCallableAttribute (#1506) - #1510

Closed
wjk wants to merge 6 commits into
dotnet:masterfrom
wjk:corert-issue-1506
Closed

wjk wants to merge 6 commits into
dotnet:masterfrom
wjk:corert-issue-1506

Conversation

@wjk

@wjk wjk commented Jul 12, 2016 •

Copy link
Copy Markdown
Contributor

With this PR merged, ILC now scans referenced assemblies for methods marked with NativeCallableAttribute. Previously, it would ignore referenced assemblies (-r flag) except for System.Private.CoreLib.

Please note that the repo's unit tests currently do not pass with this change applied! (When used on my private-repo project, however, it works well.) I am only submitting this PR now for feedback and to hopefully gain an answer to why the tests are failing. I will not sign the CLA until these issues are resolved, or unless it is required for pre-merge code review. All unit tests now pass; see below.

@dnfclas

dnfclas commented Jul 12, 2016

Copy link
Copy Markdown

Hi @wjk, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution!

This seems like a small (but important) contribution, so no Contribution License Agreement is required at this point. Real humans will now evaluate your PR.

TTYL, DNFBOT;

@MichalStrehovsky

Copy link
Copy Markdown
Member
Unhandled Exception: System.IO.FileNotFoundException: Module with same simple name already exists D:\j\workspace\debug_windows---5721b50c\packages\runtime.win7-x64.Microsoft.NETCore.Runtime.CoreCLR\1.0.2-rc2-23819\runtimes\win7-x64\lib\dotnet\mscorlib.ni.dll
   at ILCompiler.CompilerTypeSystemContext.AddModule(String filePath, String expectedSimpleName)
   at ILCompiler.CompilerTypeSystemContext.GetModuleFromPath(String filePath)
   at ILCompiler.CompilationModuleGroup.AddCompilationRoots()
   at ILCompiler.SingleFileCompilationModuleGroup.AddCompilationRoots()
   at ILCompiler.Compilation.Compile()

Ah yes, I was trying to fix this in the past and I remember hitting this too, so I postponed my work on this because I didn't really need it.

The problem is that dotnet is dragging in a reference to mscorlib.ni.dll and passing it as one of the references (in the generated RSP file that is used to invoke the compiler). With this the compiler ends up seeing mscorlib.dll and mscorlib.ni.dll and while they have different file names, the assembly names are the same.

if (_typeSystemContext.InputFilePaths.ContainsKey(inputFile.Value)) continue;

var module = _typeSystemContext.GetModuleFromPath(inputFile.Value);
if (module.Equals(_typeSystemContext.SystemModule)) continue;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is only needed because there is

AddCompilationRootsForExports((EcmaModule)_typeSystemContext.SystemModule);

In SingleFileCompilationModuleGroup.cs. The right fix is to delete the line from there.

William Kent added 2 commits July 28, 2016 15:51
…thods

Previously, it would ignore referenced assemblies (-r flag) except
for System.Private.CoreLib. Note that the tests currently do not pass
with this change applied.
@wjk
wjk force-pushed the corert-issue-1506 branch from 9442a03 to d69882c Compare July 28, 2016 19:51
@wjk

wjk commented Jul 28, 2016

Copy link
Copy Markdown
Contributor Author

@MichalStrehovsky I've just rebased this branch on top of dotnet/corert:master. I will look into fixing the mscorlib.dll issue now. Is it OK if I simply add code to ignore any references to mscorlib, seeing as it shouldn't be used under CoreRT?

I simply checked to see if the assembly in question is
mscorlib when a double-reference error occurs, and if so
simply ignore the reference. (All other double references
still result in an exception.)
@wjk

wjk commented Jul 28, 2016

Copy link
Copy Markdown
Contributor Author

@MichalStrehovsky Assuming my fix to the double-reference error in commit 07be889 is OK, this PR is ready to merge now that the unit tests pass.

@wjk wjk changed the title [DO NOT MERGE] #1506 -- Work in Progress Scan all assemblies for NativeCallableAttribute (#1506) Jul 28, 2016

AddWellKnownTypes(rootProvider);
AddReflectionInitializationCode(rootProvider);
foreach (var inputFile in _typeSystemContext.ReferenceFilePaths)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block should be placed in SingleFileCompilationModuleGroup::AddCompilationRoots. We don't want to generate exports for non-input modules when we're doing a multifile compilation (because those exports will be generated when we're compiling the referenced module itself).


AddCompilationRootsForExports((EcmaModule)_typeSystemContext.SystemModule, rootProvider);
foreach (var inputFile in _typeSystemContext.ReferenceFilePaths)
{

@jkotas jkotas Jul 29, 2016 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more appropriate to iterate over InputFilePaths instead.

The idea behind InputFilePaths and ReferenceFilePaths is that InputFilePaths is the source of roots - what is guaranteed to get compiled. ReferenceFilePaths can contain a lot of junk, but that it should not matter because of it will be touched and compiled in only if it gets actually referenced.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Interesting. The main reason I brought up this change was because in my private-repo project I had a number of assemblies that needed to be scanned for NativeCallableAttribute. I only used ReferenceFilePaths because that was what worked for me at the time I first wrote it. If you want to go with InputFilePaths instead, it would easy enough for me to adjust my build scripts.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that we'll have to restore special-case handling for System.Private.CoreLib if we go this route.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas It feels like the distinction between what's an input file and what's a reference is rather blurry for single file compilation. If I e.g. use a NuGet package with a library that has some NativeCallable methods, should those be exposed? Or only when the NativeCallable methods are in my entrypoint assembly? What's the "entrypoint assembly" if I'm compiling a library?

Maybe we should add a method on ModuleDesc that lets us enumerate all the NativeCallable and RuntimeExport methods without loading all the types and methods (because scanning all the junk is indeed rather expensive) and go with this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I actually already experimented with the ModuleDesc extensions for this some time ago: MichalStrehovsky@52702cc. I ended up not using this because it wasn't saving anything because the NameMangler loads a bunch of junk types and methods from each assembly anyway.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The single file compilation is meant to mimic simplicity and efficiency of the classic compilers. You can do cl hello.cpp and it will quickly produce self-contained .exe. Underneath, the compiler has references to a ton of standard header files and the linker command line has references to number of .libs - so that you do not need to worry about adding just the right ones. You won't get any of it unless it is referenced from your .cpp file.

The simple things should be simple, the hard things should be possible. If you have multiple libraries with named NativeCallable exports, I think it is fine that you need to do a bit extra work for it. Also, I would not mind if we introduce more fine grained controls for it (e.g. scan named exports for given module, add full reflection for given module, etc.). But I do not think we should be adding roots for everything in the reference list by default.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that makes sense - I was not looking at it from the right angle.

William Kent added 2 commits July 29, 2016 16:03
Per suggestion by @jkotas. Also re-added a special-case check
for System.Private.CoreLib, without which ILC-generated object
files will not link.
@dnfclas

dnfclas commented Jul 29, 2016

Copy link
Copy Markdown

@wjk, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR.

Thanks, DNFBOT;

@wjk wjk changed the title Scan all assemblies for NativeCallableAttribute (#1506) Scan all input assemblies for NativeCallableAttribute (#1506) Jul 31, 2016
@MichalStrehovsky

Copy link
Copy Markdown
Member

@wjk Is there anything in this pull request you would like to merge, or can we close it?

  • The change to SingleFileCompilationModuleGroup.cs makes little sense (it now only works if the core library is named System.Private.CoreLib, which is not something we would want (see systemmodule command line switch in ILC).
  • The change to Program.cs is no longer necessary.

@wjk

wjk commented Aug 5, 2016

Copy link
Copy Markdown
Contributor Author

@MichalStrehovsky Yeah, this one can be closed. Following @jkotas’ comments above, the whole point of this PR is now already present in ILC.

@wjk
wjk deleted the corert-issue-1506 branch August 5, 2016 03:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants