r/Compilers • • 19h ago

CFG vs DFG

/r/computerscience/comments/1y99sre/cfg_vs_dfg/
2 Upvotes

4 comments sorted by

4

u/cxzuk 10h ago

Hi Wash,

The first definition of a CFG (that I know of) was in 1972 by Fran Allen - though she does mention it was a concept used by others. This is a worthy paper to know about.

I mention this because I think its useful to understand that choices were made; Which mean trade-offs.

Two examples of trade-offs related to CFGs - CFG control edges point from predecessor to successor. The ordering in which we typically want to process CFGs in is Reverse Post Order. If the edges pointed the other way round, we could do Post Order. Which is also the same data edges typically travel.

Another is that there are alternatives to Basic Blocks - For example Extended Basic Blocks.

I'm not saying which is best, but pointing out options. We pick the representations/data structures for the tasks at hand.

IMHO, CFG/Basic Blocks main strength is that it closely matches the assembly layout. When you start to write sections, its straightforward to organise the BBs and linearise the instructions into bytes. The edges of a BB naturally encode the nature of jumps - we don't know the instruction(s) needed until BBs are laid out. Its a good fit.

But EBBs model fall through exactly. If Block A directly falls through to B (and A is Bs only predecessor). You could have a EBB instead. But these add complexity (Main case being Rusts attempt to use EBBs) and also fix the BB layouts together, you lose flexibility.

CFG vs ???

I am firmly in the camp that CFGs are a poor fit for todays optimisations. They impose an ordering on instructions, they are an early Instruction Scheduling that you need to keep correct when processing. More graph centric designs which remove the early scheduling constraints are IMHO more promising, such as Sea Of Nodes. Its very possible that DFG could be another option.

TLDR; Trade-offs. CFG is good for much later stages. Other structures need to be considered to ese if they help with the tasks you have at hand.

M ✌️

1

u/Calavar 7h ago

What do you make of V8 abandoning sea of nodes and moving to CFG?

1

u/caydenlund 4h ago

Not OP, but I think it was a great decision. The blog post was excellent and well-written, and I don't really have much to add.

I'm a HUGE sea-of-nodes fan. My current personal compiler project uses a sea-of-nodes graph. But it just doesn't seem like a good fit for a JavaScript engine.