Are compilers deterministic?

(blog.onepatchdown.net)

37 points | by fragmede 3 hours ago ago

49 comments

  • nu11ptr 2 hours ago

    > The computer science answer: a compiler is deterministic as a function of its full input state. Engineering answer: most real builds do not control the full input state, so outputs drift.

    To me that implies the input isn't deterministic, not the compiler itself

    • saghm 2 hours ago

      You're not wrong but I think the point is to differentiate between the computer science "academic" answer and the engineering "pragmatic" answer. The former is concerned about correctly describing all possible behavior of the compiler, whereas the latter is concerned about what the actual experience is when using the compiler in practice.

      You might argue that this is redefining the question in a way that changes the answer, but I'd argue that's also an academic objection; pragmatically, the important thing isn't the exact language but the intent behind the question, and for an engineer being asked this question, it's a lot more likely that the person asking has context for asking that cares about more than just the literal phrasing of "are compilers deterministic?"

      • roenxi 33 minutes ago

        > ... the important thing isn't the exact language but the intent behind the question ...

        If we're not going to assume the input state is known then we definitely can't say what the intent behind the question is - for many engineering applications the compiler is deterministic. Debian has the whole reproducible builds thing going which has been a triumph of pragmatic engineering on a remarkable scale. And suggests that, pragmatically, compilers may be deterministic.

      • jacquesm an hour ago

        It matters a lot. For instance, many compilers will put time stamps in their output streams. This can mess up the downstream if your goal is a bit-by-bit identical piece of output across multiple environments.

        And that's just one really low hanging fruit type of example, there are many more for instance selecting a different optimization path when memory pressure is high and so on.

    • a-dub 2 hours ago

      > To me that implies the input isn't deterministic, not the compiler itself

      or the system upon which the compiler is built (as well as the compiler itself) has made some practical trade offs.

      the source file contents are usually deterministic. the order in which they're read and combined and build-time metadata injections often are not (and can be quite difficult to make so).

  • DougMerritt 2 hours ago

    It's not uncommon to have a regression test for compilers that are written in their own language (e.g. some C compilers): compile each new version with itself, then use that to compile itself again, then use the result on unit tests or whatever, which should yield the same results as before.

    The point being that determinism of a particular form is expected and required in the instances where they do that.

    (I'm not arguing for or against that, I'm simply saying I've seen it in real life projects over the years.)

  • eru an hour ago

    > This comes up now as “is vibecoding sane if LLMs are nondeterministic?” Again: do you want the CS answer, or the engineering answer?

    Determinism would help you. With a bit of engineering, you could make LLMs deterministic: basically, fix the random seed for the PRNG and make sure none of the other sources of entropy mentioned earlier in the article contribute.

    But that barely impact any of the issues people bring up with LLMs.

    • bandrami an hour ago

      If you've got a fixed GPU that doesn't degrade at all during the process, I think? If you switch GPUs (even another one of the same model) or run it long enough the feed-forward of rounding will produce different results, right?

    • wat10000 an hour ago

      And determinism isn’t particularly helpful with compilers. We expect adherence to some sort of spec. A compiler that emits radically different code depending on how much whitespace you put between tokens could still be completely deterministic, but it’s not the kind of tool we want to be using.

      Determinism is a red herring. What matters is how rigorous the relationship is between the input and the output. Compilers can be used in automated pipelines because that relationship is rigorous.

      • sarchertech an hour ago

        And the reason that relationship can be regiorous is because compilers by definition translate one formal language to another. You can’t have a compiler that translates English to machine code in a rigorous, repeatable manner because English is ambiguous.

  • sarchertech an hour ago

    If we’re talking about “can we ignore the code the way we mostly ignore assembly and treat prompts as the new high level language”, determinism isn’t the hard problem.

    The real issue is prompt instability (chaos). A one word change to a prompt/spec will produce a drastically different program. Until that is solved there’s no world where we just check in the prompt and almost no one ever has to worry about the code.

    • gurjeet an hour ago

      It’s much worse than that. LLMs today don’t produce the same output for the same prompt.

      • seanmcdirmid an hour ago

        If you think that’s bad you should see how non-deterministic the alternative is (human programmers). Thankfully LLMs can iterate on the code they write, anyone who is using them to generate the same code from scratch each time a change is made needs some extra education. They are not code generators, they are junior programmers.

    • virgil_disgr4ce an hour ago

      > A one word change to a prompt/spec will produce a drastically different program

      So in other words, determinism (or lack thereof) is the hard problem!

      • ChadNauseam an hour ago

        A deterministic program must have the same output for the same input, but determinism does not restrict the outputs for different inputs

      • ddtaylor an hour ago

        Obviously they are cryptographic hashing functions as they exhibit an avalanche effect!

  • AlotOfReading an hour ago

    A related property is whether particular kinds of changes to the inputs have proportionally sized changes to the output. Adding a print statement shouldn't change the behavior of the function it's in (sans I/O), for example. Using calling the same function from two different callsites shouldn't change the behavior either. A new compiler version shouldn't change the observable behavior. Etc.

    I think this is the more important property and I'm not sure if it has a well-known name. The article obliquely calls it reliability, but regardless it's the key difference from LLMs. Compilers mostly achieve it, ignoring an endless list of exceptions you learn with experience.

    LLMs usually don't, even with 0 temperature and floating point determinism.

  • IanCal 2 hours ago

    I’ve felt like a good response to the vibe coding thing is that customers, product managers, etc ask for features and don’t read the code. You don’t need to read the code of something to build a level of trust about what it does and whether that matches your expectations. It is not that wild that you can have a setup where you get an application and without reading the code decide if it solves your problem to your satisfaction.

    • acedTrex 2 hours ago

      There will absolutely be systems of the future that are entirely LLM written. Honestly they will probably be better quality than the standard offshore team output.

      But lets all hope these are not vital systems we end up depending on.

      • fragmede 3 minutes ago

        The failure mode, as foretold by James Cameron, is in handing control of the nuclear launch systems over to an AI. Let's all really hope not!

    • sarchertech an hour ago

      And what happens when you change one word in the specification and the app completely changes?

      Sure everything you have unit tests for might stay the same, but unless your unit tests are testing all observable behavior (and if they are they’ll be 100x longer than the code) users will notice incredibly confusing differences in every build.

    • overgard an hour ago

      I'm using Claude every day and I am correcting the output all the time, by reading the code, because it does bad things and breaks things. I feel like the people saying that it doesn't matter either aren't building anything real or they're outsourcing their work onto reviewers or they have a time bomb on their hands.

    • system2 2 hours ago

      I bet there will be strict companies that will ask not to vibe code ever. It will become a bigger problem as vibe coding gets more popular.

  • bandrami an hour ago

    If they weren't, then reproducible builds wouldn't be possible. The trick is being able to control the input tuple exactly.

  • einrealist 2 hours ago

    If the output has problems, do you usually rerun the compilation with the same input (that you control)? I don't usually.

    What is included in the 'verify' step? Does it involve changing the generated code? If not, how do you ensure things like code quality, architectural constraints, efficiency and consistency? It's difficult, if not (economically) impossible, to write tests for these things. What if the LLM does not follow the guidelines outlined in your prompt? This is still happening. If this is not included, I would call it 'brute forcing'. How much do you pay for tokens?

    • bandrami an hour ago

      I thought to myself that I do this pretty frequently, but then I realized only if I'm going from make -j8 to make -j1. I guess parallelism does throw some indeterminancy into this

      • eichin an hour ago

        If parallelism adds indeterminacy, then you have a bug (probably in working out the dependency graph.) Not an unusual one - lots of open source in the 1990s had warnings about not building above -j1 because multi-core systems weren't that common and people weren't actually trying it themselves...

        • bandrami 18 minutes ago

          Whenever I traced them, those bugs were always in the logic of the makefile rather than in the compiler. A target in fact depends on another target (generally from much earlier in the file) but the makefile doesn't specify that.

      • fragmede an hour ago

        The time I was able to make -j 128 and it took 3 minutes to do what used to take an hour, I almost wet myself.

  • nickelpro an hour ago

    Dumb.

    Compilers aren't deterministic in small ways, timestamps, encoding paths into debug information, etc. These are trivial, annoyances to reproducible build people and little else.

    You cannot take these trivial reproducibility issues and extrapolate out to "determinism doesn't matter therefore LLMs are fine". You cannot throw a ball in the air, determine it is trivial to launch an object a few feet, and thus conclude a trip the moon is similarly easy.

    The magnitude matters, not merely the category. Handwaving magnitude is a massive red flag a speaker has no idea what they're talking about.

    • groundzeros2015 an hour ago

      And that result of that magnitude is the paradigm of operation is just completely different. Good programmers create inputs, check outputs, and build up a mental model of the system. When the input -> output is not well defined you can't use those same skills.

  • Smalltalker-80 2 hours ago

    Not if they're made by Anthropic...

  • pertymcpert an hour ago

    GCC and LLVM consider it a bug if the compiler is non-deterministic. If re-running the compiler generates different output because of things like address differences for example then it's something that needs to be fixed. So yes they are deterministic.

  • pizlonator an hour ago

    Yes, yes they are.

    Lots of engineering effort goes into making this be true.

    TFA argues that you can't control the inputs perfectly, and so the behavior may differ if you fail to control the inputs. Yeah sure.

    But the answer to the clickbaity question in the title is simply "Yes".

    • eichin an hour ago

      Even the specific example in the article, the non-determinism was treated as a bug and was fixed (since by 2004 that was definitely a regression - we put a lot of work in, in the mid to late 1990s, to get bit level reproducibility - and even before that, those little details like timestamps were still deterministic variations, we had binary diff tools that could filter them out.)

  • overgard an hour ago

    I feel like this is kind of missing the point of the argument around this. People love to say "Well you don't check your compiler output do you?" (never mind that some of us actually do for various reasons). When's the last time a compiler introduced a bug into your code? When's the last time an LLM introduced a bug into your code? There you go.

    • gaigalas an hour ago

      > When's the last time a compiler introduced a bug into your code?

      A compiler, making my job harder by being unpredictable? All the time.

      So did other programmers, users with creative input, random parallel processes running at the same time.

      LLMs are actually kind of tame in comparison.

    • salawat 32 minutes ago

      Excuse me. I curse at compiler writer's on a regular goddamn basis. There are these things called "optimizations" that I assume they spend a bunch of time hmmming and hawwwing over, but will regularly take a shit on how the structure of how the resulting assembly comes out. It is downright infuriating.

      Maybe you don't build or tinker with things enough to have warranted making a dartboard out of the gcc contributor graph, but damnit, some of us do. That compiler is not magic, continually floats around, and when you're just trying to get something from the stage of "doesn't exist at all" to "exists", does absolutely throw curve balls your way. I start wit -O0 -g and then crank up the optimization level once everything works. Otherwise come debug time, shit's missing, stuff happens at weird times, etc. If you don't treat the compiler as spooky, you haven't paid enough attention to it.

      • overgard 3 minutes ago

        While I agree undefined behavior is annoying and a huge UX issue, end of the day the bug is in your code not in the compiler.

        Also having an -O0 debug build is standard practice.

  • measurablefunc 2 hours ago

    Compilers preserve semantics. That is part of their contract. Whether the output has instructions in one order or another does not matter as long as the output is observationally/functionally equivalent. Article does not do a good job of actually explaining this & instead meanders around sources of irrelevant "stochasticity" like timestamps & build-time UUIDs & concludes by claiming that LLMs have solved the halting problem.

    • Findecanor an hour ago

      C and C++ compilers are limited to preserving semantics for data-race free code only, though. They are allowed to turn a single load into multiple loads, or even a store into multiple stores: things that won't affect anything if you have only one thread accessing memory but for multithreaded programs, changing compiler or just making seemingly unrelated changes and recompiling can make existing data-race bugs have effects or not.

      Attempting to get consistent results from floating-point code is another rabbit hole. GCC and clang have various flags for "fast math" which can enable different optimisations that reduce precision.

      Before SSE, fp on x86 was done by the "x87" FPU which always had 80-bit precision, even if the type in the source code was 32 or 64 bits — and it used to be accepted to sometimes get more precision than asked for. Java got its "strictfp" mode mainly because of x87.

      • measurablefunc an hour ago

        Data races are undefined behavior¹ so in that case the compiler is still technically preserving semantics but if you use the proper primitives to remove undefined behavior (atomic operations, locks) for any shared state then the compiler will not generate code w/ undefined behavior & all modifications/mutations will be serialized in some order. You can then further refine the code if you want the operations to happen in a certain order. At the end of the day you must assume that the compiler will preserve your intended semantics otherwise we'd still be writing assembly b/c no high level specification would ever mean what it was intended to mean for the low-level executable machine model/target of the compiler.

        ¹https://cppreference.com/w/cpp/language/multithread.html

    • markatto 2 hours ago

      It matters for things like verifiable builds.

      • adonovan 2 hours ago

        Full determinism is also highly prized by compiler writers because it massively simplifies the task of reproducing and debugging problematic executions.

      • measurablefunc 2 hours ago

        You have to specify what exactly you're verifying.

    • fragmede 2 hours ago

      Thank you for reading!

      I wrote "We have not remotely solved the halting problem in the formal sense", which does not read like a claim that LLMs have solved the halting problem to me, but I'm open to rewording it. How would you put it?

      I added in a bit about compiler contract, wdyt?

      • measurablefunc 2 hours ago

        We haven't solve it in the formal sense b/c it is formally unsolvable so hedging adds no semantic content. It's not a formal article so you can phrase things however you want but an uncritical reading will leave the reader confused about what exactly you were trying to explain.

  • drivingmenuts 2 hours ago

    > . I’m AI-pilled enough to daily-drive comma.ai, and I still want deterministic verification gates around generated code. My girlfriend prefers when I let it drive because it’s smoother and less erratic than I am, which is a useful reminder that “probabilistic system” and “operationally better result” can coexist.

    When did the girlfriend enter the discussion? Did I miss something?

  • cyanydeez 2 hours ago

    tl;dr: In the universe of chaos, the definition of deterministic is different than thehuman universe. Since the human can't control/measure every variable, it's not deterministic.