dd🌺's profile picture

Published by


Category: Web, HTML, Tech

"Why do people still write low level code when the compiler does it better?"

(I originally got this question on another platform and decided to repost it here.)

By low-level, I'll assume someone means working with a system programming language like C and C++, and not the term used for low-level hardware access used by freestanding operating system kernels and drivers.

Some unfortunate people think they know better (that's the tl;dr), but some may have found a limiting factor within a compiler (intention), or need a feature unavailable in the current environment (feature).

Because at the end of the day, the compiler knows the context provided, only you know your true intentions.

If you tell a car to turn left, a compiler giving directions to such a car might optimize its trajectory, considering the road, the shape and weight of the car, but even if you give the direction to hit a brick wall, it will do just that.

While compilers are complex, amazing pieces of software, it cannot decide to replace your algorithms (what people might call a "high-level" principle), but it will, for example, re-order loops to make them fit in cache.

But every compiler can do so much, not all of them will recognize your patterns and intentions, because everyone writes in such different ways (there are so many ways to write an English sentence, for example).

If you do a crazy byte-swapping pattern that LLVM and GCC might catch, it does not guarantee others will as well (looking at you, DMD). Even worse, what if you had to support an older version that doesn't have intrinsic (example for intention) for a 64-bit byteswap?

Another reason is features. Like implementing a "high-level" API for vector programming, multithreaded programming, or other processor facilities. There might be parts of that program that will facilitate using an assembler (detached or inlined) to access such features.

For example, I wrote ddcpuid with quite a bit of inlined assembler, simply because there are no high-level APIs to generate the instruction, and with this, I can perform additional optimizations for each major compiler (again, everyone writes differently, and the same goes for generating instructions) that the compiler won't know about, or notice.

At least that's my take on this kind of question. There's always "It depends"...

3 Kudos


Displaying 0 of 0 comments ( View all | Add Comment )