b3b00 / csly

a C# embeddable lexer and parser generator (.Net core)
MIT License
361 stars 31 forks source link

AOT/Trimming #466

Closed lilith closed 1 month ago

lilith commented 1 month ago

Does this support trimming and AOT?

b3b00 commented 1 month ago

hello @lilith , the short answer is I don't know . I 've juste done a quick try with a real simple expression parser as a console app (code attached, simply run dotnet run and it should display "parse OK 4") It works OK without AOT + trim. It seems not to work with AOT + Trimming, giving AOT warning on publish. And then running it produces a stack :

❯ dotnet publish .\SimpleExpressionParser.csproj
Version MSBuild 17.8.5+b5265ef37 pour .NET
  Determining projects to restore...
  Restauration effectuée de C:\Users\olduh\dev\generated\SimpleExpressionParser.csproj (en 271 ms).
  SimpleExpressionParser -> C:\Users\olduh\dev\generated\bin\Release\net8.0\win-x64\SimpleExpressionParser.dll
  Generating native code
C:\Users\olduh\.nuget\packages\sly\3.2.7\lib\net7.0\sly.dll : warning IL2104: Assembly 'sly' produced trim warnings. Fo
r more information see https://aka.ms/dotnet-illink/libraries [C:\Users\olduh\dev\generated\SimpleExpressionParser.cspr
oj]
C:\Users\olduh\.nuget\packages\sly\3.2.7\lib\net7.0\sly.dll : warning IL3053: Assembly 'sly' produced AOT analysis warn
ings. [C:\Users\olduh\dev\generated\SimpleExpressionParser.csproj]
  SimpleExpressionParser -> C:\Users\olduh\dev\generated\bin\Release\net8.0\win-x64\publish\
      generated                                                                                      09:03:54
❯ .\bin\Release\net8.0\win-x64\publish\SimpleExpressionParser.exe
Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key 'root' was not present in the dictionary.
   at System.ThrowHelper.ThrowKeyNotFoundException[T](T) + 0x14
   at System.Collections.Generic.Dictionary`2.get_Item(TKey) + 0x28
   at sly.parser.llparser.RecursiveDescentSyntaxParser`2.SafeParse(IList`1, SyntaxParsingContext`1, String) + 0x5d
   at sly.parser.Parser`2.ParseWithContext(IList`1, Object, String) + 0x60
   at ns.Program.Main(String[] args) + 0x8c
   at SimpleExpressionParser!<BaseAddress>+0x218d60

I don't know much about AOT and Trimming. Maybe you could look at it and share ?

generated.zip

b3b00 commented 1 month ago

My guess is that trimming will trim out the visitor methods as they are not called directly but through reflection , the trimmer thinks it could safely remove them.

Maybe some mechanism exists to avoid this ?

I've not try AOT alone.

b3b00 commented 1 month ago

hello @lilith ,

After playing with aot and trimming (see repo : https://github.com/b3b00/csly-aot) I've found :

I've tried to cheat the trimmer calling directly the visitor's methods but it did not work. I don't know exactly what's going wrong but I suspect that the use of attributes to build the lexer and parser is related to it. I need some reading about .net AOT compilation to better understand what is possible.

Do you have any idea that could help ? documentation ?

thanks for replying . I 'd really like to make CSLY AOT-compliant but I need help for this

lilith commented 1 month ago

Run-time reflection is the problem; have you tried source generation? It can access attributes too.

On Sun, Jul 21, 2024, 1:04 AM Olivier Duhart @.***> wrote:

hello @lilith https://github.com/lilith ,

After playing with aot and trimming (see repo : https://github.com/b3b00/csly-aot) I've found :

  • trimming alone seems OK
  • AOT alone is KO
  • AOT and trimming : kO as well

I've tried to cheat the trimmer calling directly the visitor's methods but it did not work. I don't know exactly what's going wrong but I suspect that the use of attributes to build the lexer and parser is related to it. I need some reading about .net AOT compilation to better understand what is possible.

Do you have any idea that could help ? documentation ?

thanks for replying . I 'd really like to make CSLY AOT-compliant but I need help for this

— Reply to this email directly, view it on GitHub https://github.com/b3b00/csly/issues/466#issuecomment-2241503202, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA2LH63LXQ4CG62NEXCNOLZNNMPXAVCNFSM6AAAAABLEI4TNSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBRGUYDGMRQGI . You are receiving this because you were mentioned.Message ID: @.***>

b3b00 commented 1 month ago

Thanks for your feedback @lilith. I've never played with source generators. I'll take a look at then and evaluate how WE Can use them for CSLY.