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

Interplay between compiler inlining and -nifs #9227

Open
josevalim opened this issue Dec 20, 2024 · 0 comments
Open

Interplay between compiler inlining and -nifs #9227

josevalim opened this issue Dec 20, 2024 · 0 comments
Labels
bug Issue is reported as a bug

Comments

@josevalim
Copy link
Contributor

Describe the bug

Today it is possible to inline functions declared as NIFs but the behaviour may not be intuitive:

-module(foo).
-compile(export_all).
-compile({inline, bar/0}).
-nifs([bar/0]).

bar() ->
  erlang:nif_error(oops).

baz() ->
  bar(),
  ok.

In the following code, even if a NIF is loaded for bar(), baz() will inline erlang:nif_error(oops). One could argue the behaviour is obvious. Others could argue it is surprising. Should we warn about it?

A similar problem exists with -compile(inline):

-module(foo).
-compile(export_all).
-compile(inline).
-nifs([bar/0]).

foo() ->
  erlang:load_nif("foo", "bar").

bar() ->
  erlang:nif_error(omg).

baz() ->
  bar(),
  ok.

In this case, although a inlining happens, a warning is also emitted:

foo.erl:7:3: Warning: inlining is enabled - local calls to NIFs may call their Erlang implementation instead
%    7|   erlang:load_nif("foo", "bar").
%     |   ^

Given the -nifs attribute exists, this could probably be improved to inline all functions, except the ones listed on NIFs.

I am not sure if any of these behaviours are desired, so feel free to close this. Happy holidays! 🎄 🍾

Affected versions

As far as I know all of them, but especially after OTP 25, since that's when -nifs was added.

@josevalim josevalim added the bug Issue is reported as a bug label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue is reported as a bug
Projects
None yet
Development

No branches or pull requests

1 participant