dotnet / runtimelab

This repo is for experimentation and exploring new ideas that may or may not make it into the main dotnet/runtime repo.
MIT License
1.4k stars 196 forks source link

ILC.exe issues #2412

Open david-tf opened 11 months ago

david-tf commented 11 months ago

Hello, I wanted to experiment using ILC onwindows, I followed the instructions in docs/workflow/building/coreclt/nativeaot.md and built llvm, then used: build.cmd -s clr.tools -arch x64 -os windows when running ilc.exe on a c# dll, I go get the following (if using a string in main):

F:\repos\runtimelab\artifacts\bin\coreclr\windows.x64.Debug\ilc>ilc.exe ConsoleApp2.dll --out out.o --reference .  -g --ildump out.bc --targetarch llvm --targetos browser --verbose
ILC: Compiling 1 methods...
ILC: Compiling 1 methods...
ILC: Compiling 1 methods...
Unhandled exception: System.InvalidOperationException: Expected type 'System.Array`1' not found in module 'System.Private.CoreLib'
   at Internal.IL.HelperExtensions.GetKnownType(ModuleDesc module, String namespace, String name) in F:\repos\runtimelab\src\coreclr\tools\Common\TypeSystem\IL\HelperExtensions.cs:line 117
   at ILCompiler.DependencyAnalysis.NodeFactory.get_ArrayOfTClass() in F:\repos\runtimelab\src\coreclr\tools\aot\ILCompiler.Compiler\Compiler\DependencyAnalysis\NodeFactory.cs:line 1071
   at ILCompiler.DependencyAnalysis.EETypeNode.OnMarked(NodeFactory context) in F:\repos\runtimelab\src\coreclr\tools\aot\ILCompiler.Compiler\Compiler\DependencyAnalysis\EETypeNode.cs:line 1338
   at ILCompiler.DependencyAnalysisFramework.DependencyNodeCore`1.CallOnMarked(DependencyContextType context) in F:\repos\runtimelab\src\coreclr\tools\aot\ILCompiler.DependencyAnalysisFramework\DependencyNodeCore.cs:line 134
   at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.AddToMarkStack(DependencyNodeCore`1 node, String reason, DependencyNodeCore`1 reason1, DependencyNodeCore`1 reason2) in F:\repos\runtimelab\src\coreclr\tools\aot\ILCompiler.DependencyAnalysisFramework\DependencyAnalyzer.cs:line 363
   at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.GetStaticDependenciesImpl(DependencyNodeCore`1 node) in F:\repos\runtimelab\src\coreclr\tools\aot\ILCompiler.DependencyAnalysisFramework\DependencyAnalyzer.cs:line 187
   at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.GetStaticDependencies(DependencyNodeCore`1 node) in F:\repos\runtimelab\src\coreclr\tools\aot\ILCompiler.DependencyAnalysisFramework\DependencyAnalyzer.cs:line 222
   at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ProcessMarkStack() in F:\repos\runtimelab\src\coreclr\tools\aot\ILCompiler.DependencyAnalysisFramework\DependencyAnalyzer.cs:line 257
   at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() in F:\repos\runtimelab\src\coreclr\tools\aot\ILCompiler.DependencyAnalysisFramework\DependencyAnalyzer.cs:line 308
   at ILCompiler.LLVMCodegenCompilation.CompileInternal(String outputFile, ObjectDumper dumper) in F:\repos\runtimelab\src\coreclr\tools\aot\ILCompiler.LLVM\Compiler\LLVMCodegenCompilation.cs:line 59
   at ILCompiler.Compilation.ILCompiler.ICompilation.Compile(String outputFile, ObjectDumper dumper) in F:\repos\runtimelab\src\coreclr\tools\aot\ILCompiler.Compiler\Compiler\Compilation.cs:line 523
   at ILCompiler.Program.Run() in F:\repos\runtimelab\src\coreclr\tools\aot\ILCompiler\Program.cs:line 583
   at ILCompiler.ILCompilerRootCommand.<>c__DisplayClass227_0.<.ctor>b__0(ParseResult result) in F:\repos\runtimelab\src\coreclr\tools\aot\ILCompiler\ILCompilerRootCommand.cs:line 283
   at System.CommandLine.Invocation.InvocationPipeline.Invoke(ParseResult parseResult)

if only using floats and ints, the above error doesnt occur, but I get the following in the output:

(....)
Alias and aliasee types should match!
i8* @g_compilerEmbeddedSettingsBlob
Alias and aliasee types should match!
i8* @__module_initializers
LLVM ERROR: Broken module found, compilation aborted!

though, the emitted ildump seems correct but incomplete. any hints are greatly appreciated.

SingleAccretion commented 11 months ago

Expected type 'System.Array1' not found in module 'System.Private.CoreLib`

This error means that the build is using a CoreCLR CoreLib, which is not compatible with the NativeAOT one. The build scripts use heuristics to decide which one to build, one of them is whether the subsets supplied are "NativeAOT-related".

So, long story short, it is recommended to build the whole thing: ./build clr.wasmjit+clr.aot+libs.

Note as well that if you want to target WASM, you'll need to build the WASM-targeting libraries (certain parts of them are architecture-specific) with ./build clr.aot+libs -c [Debug|Release] -a wasm -os [browser|wasi].

Edit: also, to get rid of the CoreCLR CoreLib, a git clean is recommended.

Another edit: also, the error messages are somewhat suspicious due to the inclusion of pointer types - did you build LLVM 15 and not some earlier version?

david-tf commented 11 months ago

Thank you, I had built llvm-15.0.6.src. I made a fresh clone and ran ./build clr.wasmjit+clr.aot+libs, but i am getting the same errors. Is the flag -r . correct for ilc.exe?

SingleAccretion commented 11 months ago

It requires some custom configuration for it to be correct. The usual way to get a good baseline command line is to take one of the runtime smoke tests, compile it and then use the resulting response file (I believe the building document has some information about this too).

david-tf commented 11 months ago

It seems it is necessary to specify multiple references, I got a little further this way, but still get this error:

ILC: Compiling 1 methods...
ILC: Compiling 56 methods...
ILC: Compiling 18 methods...
ILC: Compiling 39 methods...
ILC: Compiling 41 methods...
ILC: Compiling 94 methods...
ILC: Compiling 107 methods...
ILC: Compiling 153 methods...
ILC: Compiling 213 methods...
ILC: Compiling 328 methods...
ILC: Compiling 485 methods...
Assertion failed: (Args.size() == FTy->getNumParams() || (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && "Calling a function with bad signature!", file F:\llvm-project\llvm-15.0.6.src\lib\IR\Instructions.cpp, line 525
abort() has been called
SingleAccretion commented 11 months ago

What are the reproduction steps for the above? It could be a compiler bug.