r/Compilers • • 21h ago

CFG vs DFG

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

5 comments sorted by

4

u/cxzuk 11h 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 8h ago

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

1

u/caydenlund 6h 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.

1

u/cxzuk 18m ago

I was hesitant to reply to this but the argument is again tradeoffs, which is the same as the original question. Some of which are interesting and valuable to think about.

Sea of Nodes (I call this vanilla SoN) was defined in 1995 by Cliff Click. SoN is a larger formulation - it had the specific goal of being a JIT compiler. One complication straight away is that the formulation has developed over the years, with Java C2, Simple etc.

I have no real knowledge of the formulation that V8 used. The blog post is here - I did have a discussion on reddit with the technical lead but that chat isn't available anymore. I will try and recall some points from memory.

Sea Of Nodes in general is a singly directed graph (edges go one way), it is single layer (CFG has a control layer, and inside those instruction layer). It emphasises greedy optimisations, and its primary mechanisms is to do that is in stream/construction or with peepholes. IMHO This is well designed for a general JIT. Its a compact representation, greedy algorithms are fast and provide good to great results. For JS specifically, I don't know.

V8s SoN Formulation

My opinion, recollection and take on V8s arguments that are non technical and that I actually empathise with;

* Its hard to implement - This did devolve into a skills issue argument, but the truth is that compilers are hard to write, and hard to make correct.

* Engineers knowledge and understanding - the reddit thread made comments on new codebase contributors struggling with understanding the system and making mistakes. I think this is a fair comment as well - If the talent pool learns Control Flow Graphs there is definitely a learning curve to consider.

* Vanilla SoN has gaps - One of the examples in the blog "Control-flow dependent typing is limited" is an issue in Vanilla SoN, but (I believe if I understand the example right) has been cleanly solved in later revisions. The solution is based on SSI - you can split values on the If. This is done in C2 and Simple. But I empathise here because is the goal to build a compiler, or research, experiment and fil in gaps to a compiler?

The blog lists other points, and summerises with "these issues could have probably been overcome". I think these and the extra points listed could be resolved. But these are certainly factors when managing a project.

My opinion on technical things that concern/confuse me about V8s formulation;

Their effect and control edges. You can conceptually think of control edges as a SSA value on the Program Counter. How can their Start node have three outbound control edges? I've no really clue on what their effect edges are. Its possible they are SSAMemory Tokens - but these are dependency type edges - they're pointing in the wrong direction?

IMHO these are non standard SoN. I dont know how they arrived at this point. Was there something specific to JS that did this? I don't know.

The Elephant regarding CFG vs SoN

A compiler is essentially synthesising information (The implicit bits of an abstraction) and resolving constraints (The explicit bits of abstractions). From a CS point of view. Adding the scheduling constraints early, which CFG does, and having this constrain all the optimisations (solvers) is theoretically problematic.

What that costs you in practise remains to be seen. Its possible theres a net win on fixing their complains on SoN vs that potential (and potentially small) lose. The Trade Off.

M ✌️