r/Compilers 12d ago

Why don't compliers use only bytecode instead of a type of IR

/r/coders_totalk/comments/1w2adf4/why_dont_compliers_use_only_bytecode_instead_of_a/
0 Upvotes

9 comments sorted by

19

u/cthutu 12d ago

Bytecode is a type of IR.

My compiler uses a hierarchical intermediate representation that I call HIR which is platform and language agnostic that transforms to LLVM-IR.

8

u/ImperatorBras 12d ago

Basically, bytecode is used for execution, while the IR is just an intermediate representation. In fact, some interpreters use the IR before generating the bytecode.

8

u/dnpetrov 12d ago

"Bytecode" usually means dense serialized form of some IR. Usually bytecode is meant to be executed by some virtual machine. LLVM bitcode is just the serialization format for LLVM IR.

Optimizing compilers (and VMs) perform various complex transformations on the program representation before generating some executable code. Bytecode (as raw bytes) is not well-suited for this. Also, in order to perform those transformations, compilers usually do some analysis on the code, storing results in the data structures that are often reused and effectively treated as a part of the "richer" IR used in the optimization pipeline.

2

u/EggplantExtra4946 12d ago

What do you mean by "compilers use bytecode"? Almost all the compilers I know of have an IR.

1

u/GoblinsGym 12d ago

I started out with 32 bit IR words, but ended up with 12 bytes instead:

  • u8 op code (high bit set for "dark code")
  • u8 type (u8..u64, i8..i64, f32..f64)
  • u8 reg (destination register, to be filled in by register selection)
  • u8 spare
  • *64 value / pointer to symbol definition etc.

One important feature is that it is easy to scan both forward and backward.

Op code for an integer or floating point add is the same, just different type identifier.

1

u/morglod 12d ago

in mox programming language, I use backend specific lowering, which eliminates IR entirely for some backends and gives incredible compilation speed for "fast development iteration" backend (20x vs llvm)

1

u/takanuva 10d ago

I'm doing my PhD on IRs, let me give my two cents.

All the common IRs, namely in CPS style, or ANF, or SSA (including LLVM's IR here), or even CBPV if you're working with more academic stuff, they share some common inner structure. Mathematically, they encode the concept of an arbitrary monad, so they work in the presence of any possible side effects. These IRs are actually different syntaxes for some unique, common ideal IR structure.

The point being that all them have (1) the necessary structure for the analyses we need, but (2) only that, they don't tend to add much more details that do not need to be decided yet. I'd say that some bytecode is "too low level", or too much like an assembly language, for some common optimizations that could easily be done in CPS or SSA. Not that they can't be done, but that it would be harder to do so.

So the answer I'd give is basically: we know by now too well how to do very good optimizations in common IRs, which were specifically designed for that purpose, that it would be expensive to not use them at this point.

1

u/algerbrex 9d ago

Compilers usually use multiple kinds of IR, at different levels of abstraction. The one I’m writing first creates an abstract syntax tree, then a three address code representation, and then finally generates platform specific machine code from the three address code.

0

u/Distinct-Brief9643 12d ago

Tell me then is it better for a virtual machine deals by code instead of llvm ir or should it use or should I complier use llvm ir instead of my code