←back to thread

Why SSA?

(mcyoung.xyz)
213 points transpute | 1 comments | | HN request time: 0.344s | source
Show context
mbauman ◴[] No.45675610[source]
Forget compilers, SSA is an immensely valuable readability improvement for humans, too.
replies(1): >>45678241 #
MeetingsBrowser ◴[] No.45678241[source]
Why have

    while (c < 10) { c *= 3; }
when you could have

  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %3, align 4
  br label %4, !dbg !18

  4:
  %5 = load i32, ptr %3, align 4, !dbg !19
  %6 = icmp slt i32 %5, 10, !dbg !20
  br i1 %6, label %7, label %10, !dbg !18

  7:
  %8 = load i32, ptr %3, align 4, !dbg !21
  %9 = mul nsw i32 %8, 3, !dbg !21
  store i32 %9, ptr %3, align 4, !dbg !21
  br label %4, !dbg !18
replies(2): >>45680346 #>>45680725 #
James_K ◴[] No.45680725[source]
The second code snippet doesn't use SSA. It just translates the first loop into IR and mangles the variable names. Here is an SSA version of that in the Scheme language.

  (let loop ((c c)) (if (< c 10) (loop (* c 3)) c))
Notice that this is stateless and also returns the final value of “c” from the loop. People who use the below style have tended to find that it is much easier to reason about for more complicated looping structures.
replies(1): >>45682374 #
1. MeetingsBrowser ◴[] No.45682374[source]
I intended it to be mostly a joke. Many people equate LLVM IR with SSA.

I might even argue that easy to read and easy to reason about are opposites.

For the most part, languages like Python and Ruby can be very easy to read but difficult to understand precisely what actually happens at runtime.

Things like LLVM IR are much more explicit, making it easier to reason about but extremely difficult to read.

Maybe somewhere between is "pure" side effect free functional programs.