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.41k stars 197 forks source link

Question: NativeAOT-LLVM : cannot get empty string for unbox stub #2027

Closed yowl closed 1 year ago

yowl commented 1 year ago

With optimisation on and inlining, we get the attached dump for <Boxed>S_P_CoreLib_System_Numerics_Vector_1<Int32>__<unbox>S_P_CoreLib_System_Numerics_Vector_1__Equals

The NativeAOT-LLVM RyuJIT backend throws an IndexOutOfRangeException at https://github.com/dotnet/runtimelab/blob/555ac7dd06e4423983d859a2ef9dcf723422f11b/src/coreclr/tools/Common/TypeSystem/IL/Stubs/ILEmitter.cs#L627 apparently looking for the emptyStringLiteral for BB21 which looks like an inlined Assert. Token is 0x7000000 so we get -1 for the index. I have no idea what is special about the this empty string literal, the unbox, the inline... Any idea? Thanks sl.txt

stack:

ILCompiler.TypeSystem.dll!Internal.IL.Stubs.ILStubMethodIL.GetObject(int token, Internal.TypeSystem.NotFoundBehavior notFoundBehavior) Line 627
    at E:\GitHub\runtimelab\src\coreclr\tools\Common\TypeSystem\IL\Stubs\ILEmitter.cs(627)
ILCompiler.TypeSystem.dll!Internal.IL.InstantiatedMethodIL.GetObject(int token, Internal.TypeSystem.NotFoundBehavior notFoundBehavior) Line 94
    at E:\GitHub\runtimelab\src\coreclr\tools\Common\TypeSystem\IL\InstantiatedMethodIL.cs(94)
ILCompiler.RyuJit.dll!Internal.JitInterface.CorInfoImpl.constructStringLiteral(Internal.JitInterface.CORINFO_MODULE_STRUCT_* module, Internal.JitInterface.mdToken metaTok, ref void* ppValue) Line 595
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\ILCompiler.RyuJit\JitInterface\CorInfoImpl.RyuJit.cs(595)
ILCompiler.RyuJit.dll!Internal.JitInterface.CorInfoImpl.emptyStringLiteral(ref void* ppValue) Line 3217
    at E:\GitHub\runtimelab\src\coreclr\tools\Common\JitInterface\CorInfoImpl.cs(3217)
ILCompiler.RyuJit.dll!Internal.JitInterface.CorInfoImpl._emptyStringLiteral(System.IntPtr thisHandle, System.IntPtr* ppException, void** ppValue) Line 2194
    at E:\GitHub\runtimelab\src\coreclr\tools\Common\JitInterface\CorInfoBase.cs(2194)
[Native to Managed Transition]
jitinterface_x64.dll!JitInterfaceWrapper::emptyStringLiteral(void * * ppValue) Line 1614
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\jitinterface\jitinterface.h(1614)
clrjit_browser_wasm32_x64.dll!Compiler::impImportBlockCode(BasicBlock * block) Line 15110
    at E:\GitHub\runtimelab\src\coreclr\jit\importer.cpp(15110)
clrjit_browser_wasm32_x64.dll!`Compiler::impImportBlock'::`13'::__Body::Run(Compiler::impImportBlock::__l2::FilterVerificationExceptionsParam * pParam) Line 17895
    at E:\GitHub\runtimelab\src\coreclr\jit\importer.cpp(17895)
clrjit_browser_wasm32_x64.dll!Compiler::impImportBlock(BasicBlock * block) Line 17900
    at E:\GitHub\runtimelab\src\coreclr\jit\importer.cpp(17900)
clrjit_browser_wasm32_x64.dll!Compiler::impImport() Line 18975
    at E:\GitHub\runtimelab\src\coreclr\jit\importer.cpp(18975)
clrjit_browser_wasm32_x64.dll!Compiler::fgImport() Line 631
    at E:\GitHub\runtimelab\src\coreclr\jit\flowgraph.cpp(631)
clrjit_browser_wasm32_x64.dll!CompilerPhaseWithStatus::DoPhase() Line 125
    at E:\GitHub\runtimelab\src\coreclr\jit\phase.h(125)
clrjit_browser_wasm32_x64.dll!Phase::Run() Line 61
    at E:\GitHub\runtimelab\src\coreclr\jit\phase.cpp(61)
clrjit_browser_wasm32_x64.dll!DoPhase(Compiler * _compiler, Phases _phase, PhaseStatus(Compiler::*)() _action) Line 137
    at E:\GitHub\runtimelab\src\coreclr\jit\phase.h(137)
clrjit_browser_wasm32_x64.dll!Compiler::compCompile(void * * methodCodePtr, unsigned int * methodCodeSize, JitFlags * compileFlags) Line 4621
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(4621)
clrjit_browser_wasm32_x64.dll!Compiler::compCompileHelper(CORINFO_MODULE_STRUCT_ * classPtr, ICorJitInfo * compHnd, CORINFO_METHOD_INFO * methodInfo, void * * methodCodePtr, unsigned int * methodCodeSize, JitFlags * compileFlags) Line 6561
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(6561)
clrjit_browser_wasm32_x64.dll!`Compiler::compCompile'::`73'::__Body::Run(Compiler::compCompile::__l2::__JITParam * __JITpParam) Line 5830
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(5830)
clrjit_browser_wasm32_x64.dll!Compiler::compCompile(CORINFO_MODULE_STRUCT_ * classPtr, void * * methodCodePtr, unsigned int * methodCodeSize, JitFlags * compileFlags) Line 5834
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(5834)
clrjit_browser_wasm32_x64.dll!``jitNativeCode'::`8'::__Body::Run'::`6'::__Body::Run(jitNativeCode::__l8::__Body::Run::__l5::__JITParam * __JITpParam) Line 7204
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(7204)
clrjit_browser_wasm32_x64.dll!`jitNativeCode'::`8'::__Body::Run(jitNativeCode::__l2::__JITParam * __JITpParam) Line 7207
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(7207)
clrjit_browser_wasm32_x64.dll!jitNativeCode(CORINFO_METHOD_STRUCT_ * methodHnd, CORINFO_MODULE_STRUCT_ * classPtr, ICorJitInfo * compHnd, CORINFO_METHOD_INFO * methodInfo, void * * methodCodePtr, unsigned int * methodCodeSize, JitFlags * compileFlags, void * inlineInfoPtr) Line 7231
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(7231)
clrjit_browser_wasm32_x64.dll!Compiler::fgInvokeInlineeCompiler::__l2::<lambda>(Compiler::fgInvokeInlineeCompiler::__l2::Param * pParam) Line 980
    at E:\GitHub\runtimelab\src\coreclr\jit\fginline.cpp(980)
clrjit_browser_wasm32_x64.dll!void <lambda>(Compiler::fgInvokeInlineeCompiler::__l2::Param *)::<lambda_invoker_cdecl>(Compiler::fgInvokeInlineeCompiler::__l2::Param * pParam) Line 997
    at E:\GitHub\runtimelab\src\coreclr\jit\fginline.cpp(997)
jitinterface_x64.dll!JitInterfaceWrapper::runWithErrorTrap(void(*)(void *) function, void * parameter) Line 26
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\jitinterface\jitinterface.cpp(26)
clrjit_browser_wasm32_x64.dll!Compiler::eeRunWithErrorTrapImp(void(*)(void *) function, void * param) Line 1245
    at E:\GitHub\runtimelab\src\coreclr\jit\ee_il_dll.cpp(1245)
clrjit_browser_wasm32_x64.dll!Compiler::eeRunWithErrorTrap<`Compiler::fgInvokeInlineeCompiler'::`2'::Param>(void(*)(Compiler::fgInvokeInlineeCompiler::__l2::Param *) function, Compiler::fgInvokeInlineeCompiler::__l2::Param * param) Line 8113
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.h(8113)
clrjit_browser_wasm32_x64.dll!Compiler::fgInvokeInlineeCompiler(GenTreeCall * call, InlineResult * inlineResult) Line 928
    at E:\GitHub\runtimelab\src\coreclr\jit\fginline.cpp(928)
clrjit_browser_wasm32_x64.dll!Compiler::fgMorphCallInlineHelper(GenTreeCall * call, InlineResult * result) Line 6777
    at E:\GitHub\runtimelab\src\coreclr\jit\morph.cpp(6777)
clrjit_browser_wasm32_x64.dll!Compiler::fgMorphCallInline(GenTreeCall * call, InlineResult * inlineResult) Line 6660
    at E:\GitHub\runtimelab\src\coreclr\jit\morph.cpp(6660)
clrjit_browser_wasm32_x64.dll!Compiler::fgInline() Line 156
    at E:\GitHub\runtimelab\src\coreclr\jit\fginline.cpp(156)
clrjit_browser_wasm32_x64.dll!CompilerPhaseWithStatus::DoPhase() Line 125
    at E:\GitHub\runtimelab\src\coreclr\jit\phase.h(125)
clrjit_browser_wasm32_x64.dll!Phase::Run() Line 61
    at E:\GitHub\runtimelab\src\coreclr\jit\phase.cpp(61)
clrjit_browser_wasm32_x64.dll!DoPhase(Compiler * _compiler, Phases _phase, PhaseStatus(Compiler::*)() _action) Line 137
    at E:\GitHub\runtimelab\src\coreclr\jit\phase.h(137)
clrjit_browser_wasm32_x64.dll!Compiler::compCompile(void * * methodCodePtr, unsigned int * methodCodeSize, JitFlags * compileFlags) Line 4795
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(4795)
clrjit_browser_wasm32_x64.dll!Compiler::compCompileHelper(CORINFO_MODULE_STRUCT_ * classPtr, ICorJitInfo * compHnd, CORINFO_METHOD_INFO * methodInfo, void * * methodCodePtr, unsigned int * methodCodeSize, JitFlags * compileFlags) Line 6561
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(6561)
clrjit_browser_wasm32_x64.dll!`Compiler::compCompile'::`73'::__Body::Run(Compiler::compCompile::__l2::__JITParam * __JITpParam) Line 5830
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(5830)
clrjit_browser_wasm32_x64.dll!Compiler::compCompile(CORINFO_MODULE_STRUCT_ * classPtr, void * * methodCodePtr, unsigned int * methodCodeSize, JitFlags * compileFlags) Line 5834
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(5834)
clrjit_browser_wasm32_x64.dll!``jitNativeCode'::`8'::__Body::Run'::`6'::__Body::Run(jitNativeCode::__l8::__Body::Run::__l5::__JITParam * __JITpParam) Line 7204
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(7204)
clrjit_browser_wasm32_x64.dll!`jitNativeCode'::`8'::__Body::Run(jitNativeCode::__l2::__JITParam * __JITpParam) Line 7207
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(7207)
clrjit_browser_wasm32_x64.dll!jitNativeCode(CORINFO_METHOD_STRUCT_ * methodHnd, CORINFO_MODULE_STRUCT_ * classPtr, ICorJitInfo * compHnd, CORINFO_METHOD_INFO * methodInfo, void * * methodCodePtr, unsigned int * methodCodeSize, JitFlags * compileFlags, void * inlineInfoPtr) Line 7231
    at E:\GitHub\runtimelab\src\coreclr\jit\compiler.cpp(7231)
clrjit_browser_wasm32_x64.dll!CILJit::compileMethod(ICorJitInfo * compHnd, CORINFO_METHOD_INFO * methodInfo, unsigned int flags, unsigned char * * entryAddress, unsigned int * nativeSizeOfCode) Line 279
    at E:\GitHub\runtimelab\src\coreclr\jit\ee_il_dll.cpp(279)
jitinterface_x64.dll!JitCompileMethod(CorInfoExceptionClass * * ppException, ICorJitCompiler * pJit, void * thisHandle, void * * callbacks, CORINFO_METHOD_INFO * methodInfo, unsigned int flags, unsigned char * * entryAddress, unsigned int * nativeSizeOfCode) Line 36
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\jitinterface\jitwrapper.cpp(36)
clrjit_browser_wasm32_x64.dll!00007ffb8dfa83a8()
[Managed to Native Transition]
ILCompiler.RyuJit.dll!Internal.JitInterface.CorInfoImpl.CompileMethodInternal(ILCompiler.DependencyAnalysis.IMethodNode methodCodeNodeNeedingCode, Internal.IL.MethodIL methodIL) Line 305
    at E:\GitHub\runtimelab\src\coreclr\tools\Common\JitInterface\CorInfoImpl.cs(305)
ILCompiler.RyuJit.dll!Internal.JitInterface.CorInfoImpl.CompileMethod(ILCompiler.DependencyAnalysis.IMethodCodeNode methodCodeNodeNeedingCode, Internal.IL.MethodIL methodIL) Line 62
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\ILCompiler.RyuJit\JitInterface\CorInfoImpl.RyuJit.cs(62)
ILCompiler.LLVM.dll!ILCompiler.LLVMCodegenCompilation.CompileSingleMethod(Internal.JitInterface.CorInfoImpl corInfo, ILCompiler.DependencyAnalysis.LLVMMethodCodeNode methodCodeNodeNeedingCode) Line 165
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\ILCompiler.LLVM\Compiler\LLVMCodegenCompilation.cs(165)
ILCompiler.LLVM.dll!ILCompiler.LLVMCodegenCompilation.CompileSingleThreaded(System.Collections.Generic.List<ILCompiler.DependencyAnalysis.LLVMMethodCodeNode> methodsToCompile) Line 127
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\ILCompiler.LLVM\Compiler\LLVMCodegenCompilation.cs(127)
ILCompiler.LLVM.dll!ILCompiler.LLVMCodegenCompilation.ComputeDependencyNodeDependencies(System.Collections.Generic.List<ILCompiler.DependencyAnalysisFramework.DependencyNodeCore<ILCompiler.DependencyAnalysis.NodeFactory>> obj) Line 110
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\ILCompiler.LLVM\Compiler\LLVMCodegenCompilation.cs(110)
ILCompiler.DependencyAnalysisFramework.dll!ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer<ILCompiler.DependencyAnalysisFramework.NoLogStrategy<ILCompiler.DependencyAnalysis.NodeFactory>, ILCompiler.DependencyAnalysis.NodeFactory>.ComputeDependencies(System.Collections.Generic.List<ILCompiler.DependencyAnalysisFramework.DependencyNodeCore<ILCompiler.DependencyAnalysis.NodeFactory>> deferredStaticDependencies) Line 175
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\ILCompiler.DependencyAnalysisFramework\DependencyAnalyzer.cs(175)
ILCompiler.DependencyAnalysisFramework.dll!ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer<ILCompiler.DependencyAnalysisFramework.NoLogStrategy<ILCompiler.DependencyAnalysis.NodeFactory>, ILCompiler.DependencyAnalysis.NodeFactory>.ComputeMarkedNodes() Line 315
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\ILCompiler.DependencyAnalysisFramework\DependencyAnalyzer.cs(315)
ILCompiler.LLVM.dll!ILCompiler.LLVMCodegenCompilation.CompileInternal(string outputFile, ILCompiler.ObjectDumper dumper) Line 76
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\ILCompiler.LLVM\Compiler\LLVMCodegenCompilation.cs(76)
ILCompiler.Compiler.dll!ILCompiler.Compilation.ILCompiler.ICompilation.Compile(string outputFile, ILCompiler.ObjectDumper dumper) Line 504
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\ILCompiler.Compiler\Compiler\Compilation.cs(504)
ilc.dll!ILCompiler.Program.Run(string[] args) Line 806
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\ILCompiler\Program.cs(806)
ilc.dll!ILCompiler.Program.Main(string[] args) Line 977
    at E:\GitHub\runtimelab\src\coreclr\tools\aot\ILCompiler\Program.cs(977)
[Native to Managed Transition]
hostpolicy.dll!00007ffcbafa970b()
hostpolicy.dll!00007ffcbafa9a3c()
hostpolicy.dll!00007ffcbafaa3d7()
hostfxr.dll!00007ffcbb2db589()
hostfxr.dll!00007ffcbb2de576()
hostfxr.dll!00007ffcbb2e08a1()
hostfxr.dll!00007ffcbb2debd2()
hostfxr.dll!00007ffcbb2d82fb()
ilc.exe!00007ff63ffa1f88()
ilc.exe!00007ff63ffa22fb()
ilc.exe!00007ff63ffa37a8()
kernel32.dll!00007ffcd2e354e0()
ntdll.dll!00007ffcd436485b()
jkotas commented 1 year ago

Token is 0x7000000

Yes, this is token for the empty string.

src/coreclr/tools/Common/TypeSystem/IL/Stubs/ILEmitter.cs

This looks like that the token is getting resolved in a wrong context. Token from inlinee getting resolved in main method context?

jkotas commented 1 year ago

Actually, it looks like that constructStringLiteral is missing some changes in the LLVM branch.

https://github.com/dotnet/runtimelab/blob/feature/NativeAOT-LLVM/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs#L592-L599

vs.

https://github.com/dotnet/runtime/blob/main/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs#L809-L826

yowl commented 1 year ago

Thanks, that was it. I've merged that commit and it removes the problem