Is there a guide about how to use HIPE in elixir?

erlang
beam
Tags: #<Tag:0x00007f8ec012a790> #<Tag:0x00007f8ec0129ae8>

#1

I have some questions about this:

  • Is everything compiled and ran by mix already using hipe, after running iex I see that hipe shows on the message

iex
Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.3.2) - press Ctrl+C to exit (type h() ENTER for help)

  • How can I compile only an specific module in erlang I can add the attribute module -compile()

  • Is there a guide about when we should use HIPE and how to use it on elixir?


#2

You use hipe exactly the same way you do it in erlang - with a module attribute. The syntax in elixir is @compile .... The hipe bit in the VM manifest is merely stating that this VM has support for hipe-compiled programs.

Hipe doesn’t work too good if you have a code that switches often between hipe and non-hipe code - the switches are expensive.


#3

I never used HiPE per module attribute in erlang, but using {erl_opts, [{native, o3}]} in my rebar.config.

So could you please provide a full example of how to activate HiPE for a single module, and for the complete project as well?


#4

Check this chapter http://learnyousomeerlang.com/modules#compiling-the-code


#5

There is an ElixirSips video showing how to use HiPE https://www.dailydrip.com/topics/elixirsips/drips/native-compilation-with-hipe


#6

For the record, you can set the env-var ERLC_COMPILER_OPTIONS with options to be passed to erlc when compiling Erlang and Elixir code (because Elixir code gets translated to Erlang).

See https://github.com/elixir-lang/elixir/issues/2665

e…g. to compile only your deps native:

ERL_COMPILER_OPTIONS="native" mix deps.compile

You know it’s working if compiling takes much longer!


#7

Not accurate here…

The correct is:

Both Elixir and Erlang are compiled to same byte code.


#8

No, actually Elixir does output to Erlang code. Erlang Core it might do later, but for now it is still outputting to Erlang itself for a variety of extra ‘free’ compiler checks it gets.


#9

Thanks for the clarification @OvermindDL1 and sorry @ellispritchard for my wrong correction.

This was caused by reading somewhere, more than once, that Elixir was compiled to the same byte code of Erlang, not translated to Erlang code.

Trying to find that sources led me to this article https://medium.com/@fxn/how-does-elixir-compile-execute-code-c1b36c9ec8cf and for what I can understand Elixir is not translated to true Erlang code as some Erlang developer would write, but instead is translated to an Erlang Abstract Format to take advantage of the mentioned BEAM optimisations.

I was not able to find the sources of my misunderstood but at least I know more about what is the compiler process now :slight_smile:


#10

From my understanding it does output to Erlang itself, not Erlang Core, I know that translating to a lower level was eventually planned (and the new debug segments added to the BEAM facilitate making that easier), but I don’t think it’s been done yet? Would love to find out though. :slight_smile:


#11

Well from the blog post I linked it does look like so…

Maybe I am still misunderstanding something?


#12

Hmm, it’s possible it does use the EAF, I’ve always dumped out Erlang Core when I want to see what it is making so that is above that…


#13

The primary reason to use Erlang Abstract Format instead of Core Erlang is tooling. Using that we get, basically for free dialyzer, cover, debugger and erl_eval. Compiling to Core is also a bit awkward since it’s a moving target - it’s treated as an internal detail of the Erlang compiler and changing rather frequently between releases.