Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scala Native should allow nativeLTO option #1951

Closed
ekrich opened this issue Mar 21, 2023 · 6 comments · Fixed by #1964
Closed

Scala Native should allow nativeLTO option #1951

ekrich opened this issue Mar 21, 2023 · 6 comments · Fixed by #1964
Assignees
Labels
enhancement New feature or request
Projects

Comments

@ekrich
Copy link

ekrich commented Mar 21, 2023

Is your feature request related to a problem? Please describe.
I am trying to create a reproducer for XCode LLVM tools that only happens when using LTO "thin" option. Scala-cli seems to be the only reasonable way outside of the Scala world to share Scala Native code.

Describe the solution you'd like
In Scala Native you can have the Link Time Optimization (LTO) set to "thin", "full", or "none" (default)
A clear and concise description of what you want to happen.

Describe alternatives you've considered
I would have them install sbt instead and have a special repo on github for the reproducer.m Perhaps use a template and then add sbt options.

Additional context
Selecting a garbage collector would be nice as well although default "immix" is good but single threaded.

@ekrich ekrich added the enhancement New feature or request label Mar 21, 2023
@tgodzik tgodzik added this to To do in Issue Board via automation Mar 23, 2023
@ekrich
Copy link
Author

ekrich commented Mar 23, 2023

I tried to do the following as suggested by Wojciech as a workaround.
//> using nativeLinking "-flto=thin"

This did nothing and I noticed that when compiling the linking output line is missing.
scala-cli

[info] Compiling to native code (3530 ms)
[info] Total (6249 ms)

sbt plugin

[info] Compiling to native code (1940 ms)
[info] Linking native code (immix gc, none lto) (69 ms)
[info] Total (3884 ms)

When I added to the compile options as well - can't remember if it needs to be in both compiling and linking but perhaps this is closer. //> using nativeCompile "-flto=thin"

[info] Linking (725 ms)
[info] Discovered 578 classes and 3395 methods
[info] Optimizing (release-fast mode) (1105 ms)
[info] Generating intermediate code (591 ms)
[info] Produced 1 files
[info] Compiling to native code (2603 ms)
[error] ThinLTO: /Users/eric/workspace/scala-native/.scala-build/project_dee1b519dc-ad868d95e9/native/native-code-clib_native0.4_2.13-0.4.9-4/scala-native/errno.c.o86: error: Opaque pointers are only supported in -opaque-pointers mode (Producer: 'LLVM15.0.7' Reader: 'LLVM 15.0.7')
[error] LLVM ERROR: Can't load module, abort.
[error] clang-15: error: unable to execute command: Abort trap: 6
[error] clang-15: error: linker command failed due to signal (use -v to see invocation)
Exception in thread "main" scala.scalanative.build.BuildException: Failed to link /Users/eric/workspace/scala-native/.scala-build/project_dee1b519dc-ad868d95e9/native/main
	at scala.scalanative.build.LLVM$.link(LLVM.scala:126)
	at scala.scalanative.build.Build$.$anonfun$build$1(Build.scala:103)
	at scala.scalanative.build.Logger.time(Logger.scala:35)
	at scala.scalanative.build.Logger.time$(Logger.scala:32)
	at scala.scalanative.cli.utils.FilteredLogger.time(FilteredLogger.scala:5)
	at scala.scalanative.build.Build$.build(Build.scala:61)
	at scala.scalanative.cli.ScalaNativeLd$.$anonfun$runLd$3(ScalaNativeLd.scala:73)
	at scala.scalanative.util.Scope$.apply(Scope.scala:32)
	at scala.scalanative.cli.ScalaNativeLd$.runLd(ScalaNativeLd.scala:72)
	at scala.scalanative.cli.ScalaNativeLd$.main(ScalaNativeLd.scala:42)
	at scala.scalanative.cli.ScalaNativeLd.main(ScalaNativeLd.scala)
Error: scala.build.errors.ScalaNativeBuildError: Error compiling with Scala Native
For more details, please see '/Users/eric/workspace/scala-native/.scala-build/stacktraces/1679585628-3700571289528134558.log'

@ekrich
Copy link
Author

ekrich commented Mar 23, 2023

I also wanted to mention that perhaps we could share the actual Scala Native code in the future via this WIP project.
scala-native/scala-native#3233

@Gedochao
Copy link
Contributor

cc @WojciechMazur

@WojciechMazur
Copy link
Contributor

@ekrich There seems to not be such a problems on Ubuntu, in fact combination of

//> using nativeCompile "-flto=thin"
//> using nativeLinking "-flto=thin"

would enable usage of LTO. It can be confirmed with usage of scala-cli --native compile <path> -v:

  • files are compiled with this additional flag
[debug] Running
/usr/bin/clang
        -c
        /home/wmazur/projects/scalacenter/scala-native/scala-native/sandbox/src/main/scala/.scala-build/project_4c5dc41f25-0480e1dff6/native/1.ll
        -o
        /home/wmazur/projects/scalacenter/scala-native/scala-native/sandbox/src/main/scala/.scala-build/project_4c5dc41f25-0480e1dff6/native/1.ll.o
        -Wno-override-module
        -fexceptions
        -funwind-tables
        -fvisibility=hidden
        -O0
        -flto=thin
        -I/usr/local/include
        -I/usr/lib/llvm-14/include
        -Qunused-arguments

and the lto is also present in linking config (.scala-build/<project>/native/llvmLinkInfo)

The issue with the compilation might be present in combination with MacOS + LLVM 15, and should be fixed since version 0.4.11 when we added opaque pointers support. My version of scala-cli was using 0.4.9 by default, so it's possible that might be also the case for your build. Probably forcing latest version with scala-cli run --native --native-version=0.4.12 should fix it.

@ekrich
Copy link
Author

ekrich commented Mar 27, 2023

@WojciechMazur I forgot to follow up. I indeed found your workaround just after I posted that last post of mine above.

@lwronski
Copy link
Contributor

I closed the issue, in Scala CLI was added the --native-lto option and using directives:

 //> using nativeLto "thin"

@lwronski lwronski moved this from To do to Done in Issue Board Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging a pull request may close this issue.

4 participants