r/programming • u/LardPi • Dec 23 '20
C Is Not a Low-level Language
https://queue.acm.org/detail.cfm?id=321247914
u/valadian Dec 23 '20
Its all relative.
Compared to python... yeah, C is extremely low-level. And in every context I have ever discussed in my 10 years as a professional software engineer, it is the lowest level that realistically has to be considered.
Below that, and you get in the domain of FPGA or hyper optimized assembly, which realistically isn't a consideration in 99% of programming done today.
3
u/LardPi Dec 24 '20
You missed the point. The thing is C is based on an iutdated abstract machine and making modern CPU perform well with those assumptions imply a lot of difficult work both on hardware side and on compiler side, while an hypothetical CPU that would not try to provide a pdp like interface would open the door to knew languages with less abstractions between the cpu and the reference liw level language that would fill the gap letpft by C. Better performance from simpler compiler would then be possible.
5
u/valadian Dec 24 '20 edited Dec 24 '20
I didn't miss the point. I presented an alternative viewpoint. From a software engineer's perspective (I am one, and this is my perspective), C is the lowest I could possibly care about. I don't have the time or funding to implement a completely new abstract machine to base a completely new language on it. That isn't even remotely considered. To me, and 95% of software engineers... C IS low level.
Sure, to some computer architecture guy, or someone working for a giant corporation that wants to dump >$10million into developing a new language (And then spend the next 10 years growing the community/library support to the point that us front line software engineers would even consider it [rust/go have just gotten to that point for me, and they are 10/11 years old])... They have a different definition of low level.
Further Optimizations is always possible. The question is whether the requirements of a certain problem require, or merit the effort for that next iteration of optimization.
2
u/LardPi Dec 27 '20
When I say you miss the point what I mean is that what you should take from this article is not a discussion on high or low level. What you should take is that the assumptions behind the abstract machine C is based on are outdated and become a source of complexity. Higher level languages works on different abstract machines. Python does not need to consider a flat memory for example because the concept of memory is mostly irrelevant when you write python code. The problem here is that people using C (and assembly) assume the CPU actually work like the C abstract machine while it does not. In the other end, python codes know how there current tool is not a representation of the computer working and hence do not make assumptions on what should be fast or not.
0
Dec 25 '20
[deleted]
1
u/valadian Dec 25 '20
I define it how every software engineer in the industry defines it. The author is allowed to refer to the abstraction from the physical registers and instruction set if they want to, but it isn't how the term is used in software engineering.
→ More replies (2)3
u/drew8311 Dec 24 '20
Yep, hardware is low level but as a programmer its not even what I'm thinking of when this discussion comes up.
15
u/LardPi Dec 23 '20
I stumbled upon this in another subreddit and I thought it was very interesting. I already had the intuition that C is very outdated in term of abstraction of current hardware but this article explain very clearly how and why.
8
Dec 23 '20 edited Dec 23 '20
"Outdated" or not, when compared to everything else, it's still fast and efficient with memory, and produces small binaries. What does this say about basically all of computing?
11
u/ragnese Dec 23 '20
Fortran may be faster. I think it's mostly cultural that C gets this place in our zeitgeist as being the benchmark. (Language X is within a factor of 3 of C! Oh boy!)
1
u/bythenumbers10 Dec 23 '20
Kinda. Fortran is faster for certain (number-crunching) tasks, because a LOT of work has been put into making Fortran fast at number crunching, but C is more general in application. Doing things like text processing in Fortran is a nightmare, and less performant than the equivalent in C, for example.
12
u/atilaneves Dec 23 '20
Doing text processing in C is also a nightmare. Until you mentioned Fortran, I couldn't think of a worse language than C to do it in.
5
2
u/ragnese Dec 23 '20
I was also thinking of things like pointer aliasing not being a thing in Fortran. But I wasn't aware that text processing is less performant.
→ More replies (3)9
u/yiliu Dec 23 '20
The point of the article is that C is fast because chips are designed to make it fast, and other languages can't be faster for that reason. In spite of massive architectural changes that have occurred in the past 50 years, the x86 model still pretends that the underlying system looks like a PDP-11 (with flat memory, sequential execution, etc). There is no way to circumvent that using another language: you still have to generate x86 assembly in the end. Put another way: no matter what the language may look like at a high level (say, immutable, message-passing, massively parallel, whatever) it has to be translated into (basically) C before it can run.
5
u/jdefr Dec 23 '20
Only its not outdated.. It provides every other abstraction you know and utilize. C code is still the most prevalent language to this day. Don't fall into the belief C isn't updated - the language is very alive, it just evolves a bit slower do to the primary purpose of remaining so universal. Creating a new architecture or platform? First thing you will probably do is provide C compiler to get things off the ground.
1
u/SarHavelock Dec 23 '20
Yeah, the language is still growing and adopting modern features. Languages adapt and C is no exception. The most popular language in the world isn't going anywhere.
2
u/Iksf Dec 24 '20
Arguing that Assembly is the only low level language just makes the low-high level language spectrum useless as a definition. Maybe its "technically" correct but its just not useful when defined like that. High level = JS, low level = C; because that's the spectrum of languages that gets commonly used outside of niche areas and represent the upper and lower ends of that spectrum.
I almost more think Assembly belongs in its own category seperate from programming languages, more alongside IR's like LLVM-IR or GIMPLE or Rust's MIR. Like Assembly there's nothing to stop you writing code in these IR's but its unlikely you'll ever need to approach a problem that way. Perhaps that'd also clear up a lot of confusion with Intel vs AT&T syntax for Assembly. This idea of the one true low level language keeps causing loads of problems it seems.
1
u/LardPi Dec 24 '20
This is not what this article is about. The point is that what we consider to be the lower level, C and Assembly, are in fact higher level that what we think in the sense that it does abstract away some important aspect of the CPU behavior, namely cache and pipelining. The point is that to make those languages fast while keeping the PDP like abstract machine spec, both the CPU vendors and the compiler authors have to go into great complexity while an hypothetical CPU breaking whith such assumptions and an associated language (yet to invent) would make simpler writing effecient code.
170
u/GYN-k4H-Q3z-75B Dec 23 '20
So, that was an interesting take on the topic. You can apply the same arguments to any programming language on currently prevalent architectures, including assembly on x86. While assembly offers a bunch of instructions that are closer to metal, isn't the reality that x86 has under the hood been a completely different architecture since about Pentium II?
Assembly itself is at this point not much different from LLVM IR (or CIL or Java byte code, though those are much simpler). A representation that can be converted to various chips' real under the hood language, though that process is proprietary and covered under many layers of industrial secrets.
You couldn't develop in a low-level language on x86 even if you wanted because x86 isn't metal. It's a specification of behaviors and capabilities, just like the C and C++ standards.
61
u/tasminima Dec 23 '20 edited Dec 23 '20
The microarch of modern high perf ARM processor is broadly similar to the microarch of modern high perf x86.
The microarch of a 8086 is vastly different from a 486, which is vastly different from a PPro.
The microarch of old IBM mainframes in the same line are vastly different despite them keeping backward compat.
It makes no sense to pretend that a programming language is not low level because it can target ISAs which can have very complex hardware implementation (or more simple). If the author only wants to do open programming in microcode / more explicit handling of chip resources, good for them but it has been tried over and over and it is not very practical for people not ready to put extreme effort in it (N64 custom graphics microcode, Cell SPUs, etc.). Intermediate layers are required, either software or hardware, to make the application programming efforts reasonable.
And always doing the intermediate work statically (that would be by definition required with a language in an architecture scheme that permit it being lower level than C currently is from this point of view) is extremely unreasonable in an age with deep cache hierarchies and more generally wide speed/size disparities and asymmetrical or even heterogeneous computing. Do you want something lower level than C for general purpose programming that will run on a wide variety of systems or even in the same system on big/little cores? Doubtful. The example of N64 graphics and Cell SPUs were only possible because the hardware always the same, and the result obviously not portable.
46
u/lock-free Dec 23 '20
I think you're missing the point - of course if you zoom in far enough there's something below you in the stack and whatever you look at is "high level" - a NAND gate is "high level" from the perspective of the gate on a MOSFET.
But I think it's more apt to say, "C isn't a low level language anymore." It reflects how computers worked 50 years ago, not how they operate today (although how they operate today is influenced by how C was designed).
Do you want something lower level than C for general purpose programming that will run on a wide variety of systems or even in the same system on big/little cores? Doubtful.
Sometimes you have to. Efficient cooperative multitasking is a good example of something that is necessary on high performance systems (from embedded to distributed) that cannot be expressed in C, even inefficiently is hazardous because setjmp/longjmp can lead to truly awful crashes when mixing languages while ucontext.h is deprecated on Apple targets, isn't nearly as efficient as using native fibers in Windows, and the implementation in glibc does a few bonkers things because of legacy (like saving a bunch of state that isn't necessary and performing extra syscalls, which tank performance on context switches).
One of the reasons that it's hard is because C has an obsolete model of the universe. It simply isn't a low enough level language to express nontrivial control flow - not everything is a function call. Ironically, high level languages require things that cannot be done efficiently in portable C, like call/cc.
I could go on, that's just a single example. Another is the batch compilation and linker/loader models. The requirement of static analysis tools and extensive manual code review to catch unsoundness in production. Struct layout optimization as a manual task. Having to write serializers and deserializers despite the fact the ABI is more or less stable on any target you care about.
There's so much bullshit because C has a broken worldview, and that's the takeaway people should have.
→ More replies (25)4
u/tasminima Dec 23 '20
Efficient cooperative multitasking has really nothing to do with the impedance mismatch between C (or for that matter ASM, because what discusses the article applies to ASM) and the microarch of modern high perf processors. It is a mix of a software problem and/or high level programming language problem.
And I agree with you that C is a bad intermediate language. LLVM (or similar) is way better for that purpose.
3
u/Thaufas Dec 23 '20
And I agree with you that C is a bad intermediate language. LLVM (or similar) is way better for that purpose.
I don't understand your logic with this statement. LLVM was originally implemented in C, and later, in C++. Why do you say that "that C is a bad intermediate language" and that LLVM is better when LLVM is another abstraction on top of C/C++?
12
u/tasminima Dec 23 '20
Sorry, it was an abuse of language, I was referring to LLVM IR.
2
u/Thaufas Dec 23 '20
Sorry, it was an abuse of language, I was referring to LLVM IR.
After reading more of the comments here, I understand your meaning. Thank you.
13
u/meteorMatador Dec 23 '20
LLVM is another abstraction on top of C/C++
Say what? LLVM was designed from day 1 to be lower-level than C, such that C becomes an abstraction on top of LLVM. That was literally the whole point of the project, and why it has that name.
Note that the compiler's implementation language doesn't especially matter when talking about the high- or low-level-ness of a compiler's input language. You can write a C compiler in Python, for example.
9
u/lorlen47 Dec 23 '20
I think they mean LLVM IR, the intermediate bytecode used by LLVM project.
3
u/Thaufas Dec 23 '20
I think they mean LLVM IR, the intermediate bytecode used by LLVM project.
That makes sense. Thank you.
26
u/yiliu Dec 23 '20
You could apply the same argument to any programming language, but only C is blessed as the 'official' target, so to speak. The argument in the article is that the requirement for modern processors to function as a PDP-11 emulator to make C fast is costing us opportunities to significantly improve performance with non-C languages--and also making vulnerabilities like Meltdown and Spectre possible.
5
u/Ameisen Dec 23 '20 edited Dec 23 '20
CIL or Java byte code
CIL can be quite a bit more complex since it supports many more features than the JVM does (including pointers, direct addressing of memory, value-types, non-type-erased generics, and so forth).
isn't the reality that x86 has under the hood been a completely different architecture since about Pentium II?
Are you referring to microcode?
Microcode has been around for a very long time (like, '70s/'80s long time, if not earlier). However, in terms of the CPU, the only thing that matters to the programmer is the forward-facing ISA, which is still x86. Microcode varies from chip-to-chip and the user cannot directly interact with it. Some instructions are still directly wired for performance, though.
I mean, you're basically arguing that Mac 128K assembly wasn't low-level because the MC68000 had microcode.
0
u/serviscope_minor Dec 23 '20
You can apply the same arguments to any programming language on currently prevalent architectures,
Some but not all. There are many current chips out there where many of the arguments don't apply, at least 10x by number of cores reckon. They apply strongly to high performance chips like common x8 variants, Arm Cortex A7x and up and so on.
Even a pretty fast Cortex A5x wouldn't apply as well since it's an in order processor (but superscalar). Once you drop below the Cortex M something (4??) you lose even superscalar execution. The M0 is a scalar in order processor with a short pipeline, 3 stages or something. Dial it down even further to AVR or PIC and you've got a single cycle architecture.
3
u/dnew Dec 23 '20
Are you familiar with the Mill? (millcomputing.com) Very interesting and innovative architecture. (Stuff like thread switches being as cheap as function calls, having two instruction pointers running in opposite directions, branch prediction working completely differently, no registers at all, etc.) Yet they still managed to put in enough weird instructions and data types to support a 50-year-old language and OS.
1
u/serviscope_minor Dec 23 '20
Are you familiar with the Mill?
Not as familiar as I'd like to be :) It does sound interesting,
Yet they still managed to put in enough weird instructions and data types to support a 50-year-old language and OS.
I wonder what speed they get relative to conventional CPUs with unmodified C. It does seem to put more work on the compiler (Itanium waves from the corner), on the other hand compilers are a LOT better than when Itanium was a going concern.
1
u/dnew Dec 23 '20
They have a a crapload of cool hardware optimizations that make even things like simple loops parallelizable. It is as if pretty much any loop can be run in parallel, including things where each cycle relies on the result of the previous cycle. (Just as an example.) I think their goal is to get hardware that would work well in a data center, with good efficiency on normal business code (i.e., that you can't GPU) while keeping the energy and size expenses down. Basically, all the funky stuff something like x86 tries to parallelize in hardware they do by having the instruction set be really clever.
49
u/Bahatur Dec 23 '20
Well heckin’ ouch, that was disillusioning. This further gives me doubts about the other candidates for systems-level programming, because everything I have read about them just compares them to C and nothing talked about modern bare metal.
51
u/krista Dec 23 '20
problem is, even using threads instead of instruction level parallelism isn't going to yield much because most problem sets are not parallel.
the real problem here is dram latency. keeping the processor fed is incredibly difficult now, although it was a lot easier in the sparc days as there was a lot less discrepancy between processor speed and dram latency.
besides, memory isn't very parallel, so stuff with a lot of threads accessing ram gets slaughtered.
the author needs to address this, and didn't.
18
u/qqwy Dec 23 '20
because most problem sets are not parallel.
I do not think this is true. I think most programs except toy examples and possibly some scientific ones would like to perform different tasks at the same time.
And as for the scientific programs that want to calculate a single result based on e.g. a large amount of data: There probably still is some data parallelism you can harness, and/or the program could be written as a "pipes and filters" version.
9
u/krista Dec 23 '20
hence threads (if zero cost) and instruction level parallelism can be interchangeable, but the problem becomes one if data access.
as i mentioned, data parallelism is difficult because memory access isn't very parallel.
see the intel itanium.
2
u/Kered13 Dec 24 '20
the real problem here is dram latency. keeping the processor fed is incredibly difficult now,
I saw a fascinating talk that used C++ coroutines to do this by prefetching memory addresses and switches threads, in much the same way you would write asynchronous disk IO code. However it was by necessity designed around a fairly specific use case: In order for the coroutine switching to be fast enough, it had to be done without heap allocations, so all coroutine frames need to be the same size. So it's not generally applicable, but it was still a very interesting look into how modern techniques can help us solve these problems.
1
u/Hipponomics Jan 08 '21
most problem sets are not parallel.
Things that are very parallel: supercomputers and all their workloads, web serving, bitcoin mining, chess engines, neural networks, and most other real life tasks.
There are some things that have been proven to be hard to parallelize like the game of life, horn-satisfiability, LZW compression, and some even more obscure problems.
I suspect you might be talking about video games as they are said to be hard to parallelize. That is largely caused by the popularity of object oriented programming. People are now realizing that it tends to encourage hard to parallelize logic as opposed to ECS or more functional approaches. Not to mention that 90% of the computations in a AAA game are done by extremely parallelizable shaders in the GPU.
19
u/PM_ME_UR_OBSIDIAN Dec 23 '20 edited Dec 23 '20
See, the problem is not the language, the problem is the x86 execution model. And until the next industry-wide paradigm shift we're locked into this one. Last time we made any progress in practical execution models for general-purpose computing was when ARM emerged as victor in the mobile space, and all it took the appearance of the mobile space. ARM isn't even that different from x86. When will the next opportunity appear?
17
Dec 23 '20
It won't for generic processors. Processor vendors will always want to have that abstraction layer just because it is way easier to sell stuff that runs your existing code faster than to sell stuff you'd need to recompile everything just to run something.
Sure we might get arch that makes it easier for compilers to generate assembly that is translated via microcode to more optimal CPU utilization, but the abstraction isn't going away.
2
u/PM_ME_UR_OBSIDIAN Dec 23 '20
Yes, I'm saying we should be iterating on that abstraction.
15
Dec 23 '20
The problem is that iterating requires to either
leave old shit in - that's how you get into mess like x86
recompile everything - massive problem with anything that's not open source, complex for open source too as you now need to keep more binary versions.
So unless some truly massive improvement (like architecture/ISA allowing for some massive improvement in performance with same or lower transistor count) comes, we're left with just adding new stuff to big blob of ISA with occasional extra "do this thing fast" instructions added
→ More replies (7)→ More replies (1)9
u/tasminima Dec 23 '20
The x86 execution model is not really that special. Of course the parallel memory model is too strong, the variable length instruction set is garbage, etc. But it is at least not too bad. Not IA-64 level bad. Not IAPX-432 bad. etc.
That model for general purpose won because the other attempted models were worse, and lots have been tried. Its scaling is not over, so there is no burning problem with it. It is nowadays use in combination with massively parallel GPUs, and this combination works extremely well for an insane variety of applications.
3
u/PM_ME_UR_OBSIDIAN Dec 23 '20
What's so bad about IA-64?
5
u/smcameron Dec 23 '20
I'm no expert, and I'm probably botching it a fair bit, but from what I recall, the instruction stream was really like 3 parallel instruction streams kind of interleaved together, and it was left up to the compiler guys to produce machine code that used all three streams. This turned out to be much harder than anticipated, and made the generated code way bigger. (I worked at HP during the time the ia64 machines came out, doing linux storage drivers... but I never really got down into ia64 machine code much, just C code.)
→ More replies (1)3
u/tasminima Dec 23 '20 edited Dec 23 '20
Basically it wanted to avoid OOO (in a kind of approach similar to what previously lead to RISC: try to simplify the hardware) by betting on the compiler, but this approach does not work well at all because OOO (+in some case HT) is dynamically adaptive, while most of the time the performance profile of EPIC binaries would have been way more tied to specific implementations (hard to design new chips that broadly run the old binaries faster, a problem similar to what happened on some early-RISC btw), workloads and workload parameters, and very hard to produce efficient code from linear scalar code in the first place.
And general purpose linear scalar code is not going anywhere anytime soon, or maybe even ever.
→ More replies (2)15
u/qqwy Dec 23 '20
While Rust is by no means perfect, it definitely tries to improve targeting bare metal by for instance:
having a well-defined memory model (therefore not needing to deal with the provenance issues explained in the article);
having (except when explicitly asked for) no guaranteed memory layout (therefore not requiring complex padding rules);
preferring immutable structures over mutable ones (therefore not requiring as complex of a cache coherency protocol);
preferring higher-level functional looping constructs over low-level hand-written loops (allowing for easier loop vectorization as well as easier data parallelization, c.f.
Rayon).This hypothetically would allow Rust to run faster than C on even x86 (however, currently there are still many bugs inside
clangrelated to the relaxed memory model guarantees that stop certain optimizations from being available.) and especially on hardware with a different execution model that does not have 'C compatibility' as its primary focus.When vendors will go there, however, is a big question. WebAssembly might be interesting to look at because it also tries to be more agnostic, though runtime speed is not its main focus.
Another interesting example is the J1 Forth, where an FPGA was programmed with a very simple Forth as 'assembly' language, allowing for better efficiency in terms of 'compiled binary size + running time' than the same code written in C.
10
u/DreadY2K Dec 23 '20
Can I have a source on your claim that Rust has a well-defined memory model? The Rust Reference calls it, "an under-defined place in the language".
10
u/qqwy Dec 23 '20
Definitely! I looked up the proper nomenclature what I was describing to make sure I get it right. 'memory model' was not entirely the correct term here; apologies. I was alluding to the strong non-aliasing guarantee that
&mut(mutable references) have in Rust. Actually the same is true for any other (mutable) type (except a few unsafe types which you do not encounter in normal programming usage). More information can be found on the Aliasing page in the Nomicon.10
u/DreadY2K Dec 24 '20
Oh yeah, the memory aliasing rules are a good step in the right direction for making efficient code (once llvm fixes the bugs that prevent rustc from using it). I thought you were referring to an entire formal memory model, which Rust doesn't have yet.
9
u/crackez Dec 23 '20
LOL x86 is not a fast PDP-11? I bet they have more in common than you might think...
The thing is the x86 ISA is a lot like the PDP-11 ISA.
In reality the underlying hardware is nothing like the x86 ISA.
27
u/helloworder Dec 23 '20
C is not a low level language by definition
A low-level programming language is a programming language that provides little or no abstraction from a computer's instruction set architecture—commands or functions in the language map closely to processor instructions.
(from wiki). This distinction is pretty clear and straightforward.
The only low level languages are machine code & various types of assemblies (masm, nasm etc).
Ofc C is "lower" than Javascript, Python or Java but it is not a "low" language.
10
u/ryeguy Dec 23 '20
I don't think that definition matches the canonical usage of the phrase (also, that definition from wikipedia isn't even sourced). People refer to C as a low level language all the time. Are they wrong, or do we just not have an agreed upon definition of it?
I mean, look at the quote in the linked article:
A programming language is low level when its programs require attention to the irrelevant
This is another subjective view of it. One could argue that C exposes you to things that are irrelevant like manual memory management and that garbage collection is the high level abstraction. But then someone else could argue that malloc/free are abstractions too, so they're high level.
2
u/helloworder Dec 23 '20
I don't think that definition matches the canonical usage of the phrase
This definition matches exactly what I have been taught in my university on one of the first classes on programming languages.
People refer to C as a low level language all the time
and they are wrong. One more example: people often refer to C as a strongly-typed language, whilst in reality it is a quite weakly typed one.
I mean, look at the quote in the linked article:
It is a quote of someone who tried to be fancy I guess. Anyways it is not the strict definition, but the one I provided is.
Think of it this way: asm code must target the exact model of a processor (or a family at least), C code lets the programmer be abstracted away from the processor registers and how exactly things are implemented inside.
Ofc sometimes (in the embedded world for instance) you still cannot avoid it, but the mere fact that you can write a portable code which would get compiled on any machine is the key.
C is a high level language, but not the highest, sure.
8
u/ryeguy Dec 23 '20 edited Dec 24 '20
This definition matches exactly what I have been taught in my university on one of the first classes on programming languages.
Here's a google scholar search and a google book search for the phrase "c is a low level language". Do I win because there are more links here than programming language courses you took? I kid. Of course not -- my point here is it is clearly being used by people in a way different than you describe.
and they are wrong. One more example: people often refer to C as a strongly-typed language, whilst in reality it is a quite weakly typed one.
It's interesting you bring this up. This isn't set in stone either. Strong/weak has no agreed upon absolute definition; all we can do is agree on what strong and weak features are, and then compare languages on a spectrum to each other. For what it's worth, I agree with you and would call C weakly typed. But I could see an argument where C is considered strongly typed. The internet is filled with well-reasoned arguments about this, too.
I'm not necessarily arguing that defining C as low level is correct, just that this is a long debated topic with a lot of nuance and no right answer. I personally find it more useful conversationally to refer to C as a low level language, and that is often how I see people talking about it as well. There's a point where canonical usage has an effect what the accepted definition is. Should we argue about what "literally" means next?
1
u/helloworder Dec 23 '20
Strong/weak has no agreed upon absolute definition;
you're correct, I admit. It was the only example of a "common misbelief" that popped up in my head tho
my point here is it is clearly being used by people in a way different than you describe.
I just don't believe this can justify anything. No matter how many people misuse certain terminology, it does not change its meaning. You can argue that in natural languages words tend to evolve exactly this way, but we are talking about technical terminology, not "real life" words. (sorry my clumsy english does not allow me to express the thought more gracefully)
Why more and more people misuse this term? My guess: today people mostly (like 95%) code in very high abstracted languages and do not deal with memory allocations or pointer juggling, they are not CS scholars and they simply... are a bit ignorant (not that it is necessarily bad, they just don't need this information). So they simply treat "this weird pointer syntax" as something alien/magical and "very low-level".
3
u/aivdov Dec 23 '20
To them it is very low level. And they are not wrong. You sound like some pseudointellectual elitist who is hung up on his university teachings as gospel. Obviously even people who studied CS call c low level relative to popular/common languages that are used in the industry.
3
u/helloworder Dec 24 '20
You missed my point. I didn’t say they were wrong that it is low to them. I said that there is a strict definition of what is considered to be low language in general.
What is your definition of low level language then?
→ More replies (1)2
2
u/The_One_X Dec 24 '20
Yeah, I like the hierarchy of machine -> assembly -> systems -> managed -> scripting, with machine and assembly grouped as low level, and systems, managed, and scripting grouped as high level.
2
u/mode_2 Dec 23 '20
Yeah, I'm really surprised that this is contentious at all. I was always taught that the low/high level distinction was exactly what you said.
0
Dec 23 '20
[deleted]
4
6
u/pjmlp Dec 23 '20
Not at all, that is a urban myth, there were plenty of languages for that spot, C just won over them thanks UNIX.
1
u/SarHavelock Dec 23 '20
C is the language of choice for general low-level programming and the most used programming language in the world.
4
u/pjmlp Dec 24 '20
Just like JavaScript is the language of choice for Web development, due to historical accidents.
→ More replies (8)
6
u/ischickenafruit Dec 23 '20
As a couple of observed, the author seems more interested in the academic “what to do next” question rather than how to fix today. That’s because he’s part of this project https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/ which is looking to build new safer CPUs and languages to program them.
18
u/nx7497 Dec 23 '20
We have a number of examples of designs that have not focused on traditional C
code to provide some inspiration. For example, highly multithreaded chips, such
as Sun/Oracle's UltraSPARC Tx series, don't require as much cache to keep their
execution units full. Research processors2 have extended this concept to very
large numbers of hardware-scheduled threads. The key idea behind these designs
is that with enough high-level parallelism, you can suspend the threads that
are waiting for data from memory and fill your execution units with
instructions from others. The problem with such designs is that C programs tend
to have few busy threads.Instead of making your program parallel-enough to do stuff while stalled on memory accesses, why wouldn't you just focus on improving your memory access patterns? It seems like the holy grail here is "parallelism", but I could just as easily say the holy grail is "data locality" or something.
There is a common myth in software development that parallel programming is
hard. This would come as a surprise to Alan Kay, who was able to teach an
actor-model language to young children, with which they wrote working programs
with more than 200 threads. It comes as a surprise to Erlang programmers, who
commonly write programs with thousands of parallel components. It's more
accurate to say that parallel programming in a language with a C-like abstract
machine is difficult, and given the prevalence of parallel hardware, from
multicore CPUs to many-core GPUs, that's just another way of saying that C
doesn't map to modern hardware very well.Idk, sorry, I'm just not convinced about parallelism or functional programming.
20
u/bythenumbers10 Dec 23 '20
The problem with "just" making the memory faster is basically physics. Speeding up memory involves hitting memory registers faster on skinny little copper traces, who now have high-frequency signals on them, and now your discrete logic is also a tiny antenna, so now you've gotta redesign your memory chip to handle self-induced currents (or you risk your memory accesses overwriting themselves basically at random) because yay, electromagnetism!
I'm happy to babble on more, I love sharing my field with others (pun fully intended).
5
u/fartsAndEggs Dec 23 '20
Please babble on more, I dont know a specific question but I'm curious where this leads
11
u/bythenumbers10 Dec 23 '20
Okay, so for unrelated reasons, hardware chip design isn't my forte, but the little traces are still tiny conductors, and the ostensibly DC logic signals, run fast enough, due to non-ideal features in the traces (think crystal structure in the copper traces), start to "smooth out" the crisp DC signal into something that looks more like AC. And AC through a conductor makes a transmitting antenna. Maybe not a "good" one, but it doesn't take a good one to generate interference. And ALL conductors you plug into are antennas, including the other traces. So now you have "crosstalk" between traces. The nice part is, the signals are low-power and probably don't get out of the housing, but all those traces are clumped together w/o any kind of shielding between them, so you have to route them carefully to minimise the crosstalk, often at right angles.
See also humbucker pickups in guitars.
9
Dec 23 '20
So, crosstalk and interference are a problem at high speeds, but I wouldn't say they are the fundamental problem as far as I am aware. There are microwave systems that can operate at much higher speeds than microprocessors. The problem is power density (W/um^2). To flip a bit, you must charge and discharge a gate, and this dynamic power usage scales ~f^2. That's why you won't see chips with frequencies much above 3 to 4 GHz. Before we could makes things more complex while keeping W/um^2 constant using Dennard Scaling. This decrease in delay required an increase in clock speed to be useful (no longer possible), and it also requires scaling down supply voltage (VDD) in order to gain with power. VDD scaling has also slowed down I think because of noise margin but I am not 100% sure. Finally the W/um^2 has also increased with scaled technologies because of static power, which is a result of quantum tunneling through the gates as they get thinner. All of this has led to the end of Dennard Scaling around 2003ish (some say later around 2006). This was one of the major reasons that single thread performance has stalled, leading to the end of Moore's law as it was originally proposed, and as you said has led to the rise of parallelism.
3
u/bythenumbers10 Dec 23 '20
This guy fucks. And presumably knows a LOT more about hardware & chip design than I do.
6
Dec 23 '20
Haha thanks, I actually do do CPU design for a large tech company (I am pretty junior though)
1
u/nx7497 Dec 23 '20
I'm not suggesting making hardware faster, I'm referring to software.
2
u/bythenumbers10 Dec 23 '20
"Data locality" IS a hardware problem, and we're coming up against the physical limitations there. Perhaps better memory access protocols would help, but paging still limits throughput, and we're back to hardware.
5
u/dnew Dec 23 '20
Part of the problem with improving memory access in a C-like language is that you can't say "in 12 cycles, I'll need the value of this memory, whatever it happens to be when I need it." You wind up blocking on memory, or having to do something odd to account for the possibility that someone writes to that memory somewhere in the next 12 cycles.
I'm not sure how you'd "improve your memory access patterns" in general in C? Maybe you can give an example?
Check out millcomputing.com for some really interesting architectural choices. Their videos tab has a bunch of fascinating lectures.
3
u/nx7497 Dec 23 '20
Improving memory access patterns is extremely commonly discussed when people are trying to optimize programs. The classic example is iterating over a matrix: it's faster to iterate over the rows than the columns because (in C) rows are contiguous in memory. If you're not familiar with this, watch any CppCon talk by Chandler Carruth, or watch the one on data oriented design by Mike Acton.
4
u/dnew Dec 23 '20
Sure. But that's going to be true of any language with arrays, and even design patterns like ECS. It's not specific to C, and indeed Fortran (for example) lets you say how you want to store the arrays for just that reason.
And I've worked with languages that look at how you're accessing the data in a table and will store it differently based on your access patterns. Like, if you iterate over columns in the code that accesses it, it'll store it column-major.
I was more thinking "what sort of data access does C allow you to do faster that other languages don't" than "what's an example of a data access pattern that's faster". :-)
2
u/nx7497 Dec 23 '20
Ah, ok. Sorry. Idk, I think some people are misinterpreting my original comment, but whatever.
1
u/DimwittFlathead Feb 03 '21
Correct, c is NOT a low level language, however if the compiler is implemented correctly, you CAN write assembly language within c and that is considered low level language. Nobody is going to have a bank of toggle switches with a load button in order to properly code in machine language.
1
u/LardPi Feb 03 '21
I think the point of this article is to show how even assembly no longer trivially maps to CPU working. Of course no one would ever use a switch panel, but what if we had a different microcode that no longer hide cache and pipeline from the assembly ? We could create better compilers that would produce simpler code with simpler algorithms because they wouldn’t have to guess what will really be happening.
18
u/jdefr Dec 23 '20
Useless nit-picking. It pretty much gave the definition of a low-level language, said C fits that definition, then proceeded to say C isn't a low-level language despite fitting the agreed upon definition. C puts you closer to the metal than virtually everything but Assembly. C also provides support (albeit optional) for embedded assembly to reach into any corners that might provide a tad bit too much abstraction for the language itself to adequately address.
16
u/SarHavelock Dec 23 '20
C is the lowest high-level language and Assembly is the highest low-level language.
3
2
u/pjmlp Dec 23 '20
C is not the only systems programming language in existence, in fact the first one is 10 years older than C and still being used by Unisys.
1
u/SarHavelock Dec 23 '20 edited Dec 23 '20
But none of them are as widespread or popular as C. C is ubiquitous.
LISP has also been used as a systems programming language and was created 14 years before C and introduced revolutionary features like garbage collection.
1
u/pjmlp Dec 23 '20
Lisp did not had the luck of being bundled with an OS that was available for free with source tapes.
Just like browsers made JavaScript a thing, UNIX made the JavaScript of systems programming languages a thing.
Had Bell Labs been allowed to take commercial advantage of UNIX, and C would have been a footnote like BLISS and many others of the same era.
5
u/SarHavelock Dec 23 '20
This is all beside the point. The point is that C is the most well-known lowest high-level programming language.
→ More replies (12)
6
u/glonq Dec 23 '20
...anymore.
C did not move away from being close to the metal. Rather, CPU's moved towards hiding fancier metal.
3
u/mode_2 Dec 23 '20
...anymore.
When was it? It was very expressive and high level when it was introduced. Did it become more low level then revert?
3
u/Radmonger Dec 24 '20
Rather than debating semantics as to what does or doesn't count as a low level language, it is more useful to specify the properties that a modern low level language would need in order for it to have the same relation to the hardware that C has to a PDP11.
For a first pass:
- semi-explicit register allocation, as opposed to hints that get ignored because the compiler is assumed to know better
- load/store from arbitrary memory into a register by dedicated syntax that can specify the full properties (uniqueness, caching, mutability, endianess, ...) of the pointer loaded from
- consequently, arthmetic operations may only be on register variables
- explict prefetch and discard operations on addresses
- dedicated syntax for strings, complex numbers and short vectors
- biased booleans ('99.9% likely to be true') to feed into branch prediction.
This is the opposite of the approach suggested in the article of proposing a new compiler architecture, but it probably an easier task to implement and deploy. Maybe Rust could add a 'fast' block type, to go with 'unsafe', in which code had to be written that way. Or the equivalent as a C extension.
8
Dec 23 '20
Nowadays there are just so many layers of abstraction
that stuff like C
or even malloc and free
seem low level
Not just to thee
But also to me.
That being said there probably was a time when C was regarded as the cool new hip programming language to code in, much like JS frameworks nowadays.
2
1
18
u/bigmell Dec 23 '20 edited Dec 23 '20
Its hard to imagine a reason to go lower level than C these days. There is absolutely nothing more universal than C. Nothing more widely known, used, tested, and optimized.
The performance increase from using one of the many assembler type languages would be completely negligible these days. Assuming someone could even get a large assembler type project debugged and out the door. That skillset has almost completely disappeared, replaced well by C.
The last time I heard someone seriously using assembler was when John Carmack wrote bits of the quake engine in it because performance was a huge issue. But those days seem a thing of the past.
C is old, and young guys think everything old is stupid and everything new is better. They will have many hard lessons to learn. But if you have a problem that you think you need a lower level language than C, you should probably go back to the drawing board. You likely are mistaken about a great many things.
22
u/Catfish_Man Dec 23 '20
x264 has an enormous amount of assembly in it. Hot inner loops in most major operating systems (e.g. optimized math libraries shipping with them) also have quite a bit.
→ More replies (1)32
Dec 23 '20
The problem with the article is that all of these problems apply to x86 assembly as well. It might as well be titled "x86 is not low level."
→ More replies (1)15
u/MorrisonLevi Dec 23 '20
There are articles about x86 being a high-level language. This isn't the one I'm thinking of, but it's a start: https://blog.erratasec.com/2015/03/x86-is-high-level-language.html#.X-NYZulKgcg.
19
Dec 23 '20
On the one hand I concede the general point, that the CPU is doing a lot of things the programmer doesn't have access to.
However, if x86 is a high level language, then there are no low level languages. And if there are no low level languages then your definition isn't useful. Or, the author doesn't actually believe that. Since the author probably isn't being sincere, the title is click-bait, which is why that take annoys me.
If the author is being sincere in claiming that there are no low level languages (since all languages on my computer run on x86 or are compiled to x86, that would be the case), then I would want to see a positive description of what a hypothetical, but not yet existing low level language (and hardware if the ISA itself is high level) would look like. This is something the linked article and the article you linked to don't do. So, I am not even clear at that point, what the author means by high and low level.
-1
u/gcross Dec 23 '20 edited Dec 23 '20
It is beyond unreasonable for you to be attacking the author for being insincere for not describing what hypothetical alternatives could look like when there is a section titled "Imagining a Non-C Processor" right there in plain sight in the article, and it is shameful that you are being so highly upvoted for doing this.
Edit: Downvote this comment all you want. It is one thing to disagree with someone, it is another to question their sincerity, and if you are doing the latter you had better have good reason to do so and at the very least there should not be something in plain sight that directly contradicts the reason you have given.
15
u/serviscope_minor Dec 23 '20
Its hard to imagine a reason to go lower level than C these days.
Bit banging on a microcontroller is sometimes best done in assembly because you can tightly control the timing down all branches to make sure it's the same. You can count instructions then insert nops, to even out the cycle counts. Writing in C or C++ means the compiler will probably optimise your code too well making some branches faster than you want.
The other option is you write in C or C++, examine the output the insert some asm nops judiciously here and there. Of course they can change if you mess with the code at all since optimizers are unpredictable at times, so it might be more work than "just" writing asm.
If you've never done it, I recommend you grab an arduino and give it a crack. It's immensely fun to do, since it's unlike any other kind of programming one does. You get to/have to pour hours into a tiny amount of code bringing just that little bit to some kind of perfection.
6
Dec 23 '20
Bit banging on a microcontroller is sometimes best done in assembly because you can tightly control the timing down all branches to make sure it's the same. You can count instructions then insert nops, to even out the cycle counts
Not anymore. Many of even cheap micros have DMA controllers (on top of various other peripherals), so you can do stuff like bit-bang multiple serial outputs by just having DMA + code feeding it. Here is one guy doing it.
Unless you're targetting sub-$1 (which is of course valid use case for the big mass production stuff) microcontrollers you usually have plenty to work with, even the "small" 32 bit M3 core usually have plenty of peripherals to go around.
4
u/serviscope_minor Dec 23 '20
Not anymore. Many of even cheap micros have DMA controllers (on top of various other peripherals), so you can do stuff like bit-bang multiple serial outputs by just having DMA + code feeding it.
Ooh one for the to-watch list! I didn't know of this hack. Thanks!
Unless you're targetting sub-$1 (which is of course valid use case for the big mass production stuff) microcontrollers you usually have plenty to work with, even the "small" 32 bit M3 core usually have plenty of peripherals to go around.
I was thinking of PIC or AVR really super low end stuff.
2
Dec 23 '20
AVRs are kinda expensive for what they do. And you can get a lot for $1, even few 32 bit chips
3
u/serviscope_minor Dec 23 '20
AVRs are kinda expensive for what they do. And you can get a lot for $1, even few 32 bit chips
Low power though. I think PICs have the edge there, but those little ATTiny's aren't bad. Since we're nerding out....
One of my favourite feature is one hidden away on some of the low end PICs like the 12F675. The HALT instruction halts AFTER executing the following instruction. Sounds odd, right? The reason is really cool. You can use the following instruction to start a conversion on the ADC (if it's set up to be self clocked). So the chip powers down, then the ADC runs with the main clock off, giving you much less noise. Then it generates an interrupt which wakes up the chip (if wake on interrupt is enabled), and it continues on it's merry way.
And that's how you can get really a really amazing ADC noise floor on a cheap microcontroller on a cheap 2 layer board without quality grounding. Also, the ADC is slow, so with the main clock off you can save a ton of power if your "on" time is dominated by the ADC.
→ More replies (1)3
u/happyscrappy Dec 23 '20
You see that kind of think only at the lowest levels now. Faster processors aren't really predictable enough anymore.
This kind of bare metal control codeI associate with brushless motor controllers.
Microchip wrote some reference code for such control over a decade ago and gave it away:
http://ww1.microchip.com/downloads/en/appnotes/00857b.pdf
And sold a lot of microcontrollers as brushless motors became popular.
But for things which aren't really sensitive down to the cycle, that era seems to be over. There are as many as a dozen timers and sophisticated cross-triggering as well as DMA in modern microcontrollers. Go to Adafruit's examples as they migrate from AVR hardware (Arduino) to mostly ARM-based (Feather) and you'll see a lot of the hand-rolled assembly loops are gone.
2
u/serviscope_minor Dec 23 '20
You see that kind of think only at the lowest levels now. Faster processors aren't really predictable enough anymore.
Yeah, I mean I'm not suggesting it's common (and as a sibling post pointed out you can use DMA too). I think predictability decreases as you go up the chain. I think an M4 is probably predictably, it's scalar, in order without a cache hierarchy, so not so bad I guess. It'll get worse the higher you go.
But for things which aren't really sensitive down to the cycle, that era seems to be over.
Yeah it's shrinking a lot. You can also often do a fair bit by abusing a UART, especially the ones which will send a continuous bitstream.
In fairness to me the OP couldn't imagine, and I provided the only example I could think of.
Oh actually I've thought of another one!
If you want to write an efficient medium long integer library you probably need bits of ASM since you need to access the carry flag. Maybe if you write code in C to deduce the carry status the compiler can figure out what you mean. I don't know TBH.
1
u/bigmell Dec 23 '20
I actually taught a class programming remote control cars using arduino. All gui building blocks for kids though, no heavy programming. These days I think more about how to keep a motor yacht running than hand tuning assembler code... And I suggest you do the same!!! :))))
13
u/th3typh00n Dec 23 '20
The performance increase from using one of the many assembler type languages would be completely negligible these days. Assuming someone could even get a large assembler type project debugged and out the door. That skillset has almost completely disappeared, replaced well by C.
You can often gain an order of magnitude performance increase by using assembly over C, which is why it's done all the time in low-level libraries where performance actually matters. Such code bases aren't purely written in assembly nowadays (that'd be a huge waste of time), but the most important pieces are.
The last time I heard someone seriously using assembler was when John Carmack wrote bits of the quake engine in it because performance was a huge issue. But those days seem a thing of the past.
You haven't been looking very hard then.
5
u/MorrisonLevi Dec 23 '20
You don't have to look very hard, either. For instance, write a routine to do equality comparison for a struct that is composed of two 64 bit unsigned integers(
struct Pos { uint64_t x, y; }). The straightforward way to write this is:bool Pos_eq(struct Pos a, struct Pos b) { return a.x == b.x && a.y == b.y; }GCC doesn't generate a branchless version of this. One could argue that in certain cases, namely when a.x and b.x are not equal, and that this is called in a loop where branch prediction would matter, the branching version is faster/better. If it's not in a loop, or if a.x and b.x are equal, then it's going to be slower. Contrast this with the branchless version, which is barely any more expensive at all if I did the math right, and since it avoids the branch it isn't susceptible to mis-predicts.
I think most people would agree the branchless code is better, and that's actually what clang does. Now, I'm not sure how much this specific example matters--it might if it's used in a critical path somehow--but it erodes any confidence I might have had in statements like this:
The performance increase from using one of the many assembler type languages would be completely negligible these days.
Don't get me wrong; I think compilers are doing a great job; rather I think there is room to go from great to excellent if you need to.
→ More replies (2)4
u/happyscrappy Dec 23 '20 edited Dec 24 '20
On a modern processor I think gcc's implementation should be good. The microarchitecture has a return stack so it doesn't fail to prefetch from the right spot. It does have to reset the pipe if it goes the wrong way on the branch though.
C offers shortcut evaluation and gcc is using this. It saves two memory accesses when the xes are not equal. And for some machines that means the branching version would be faster. For example, flip the compiler over to the AVR version and you can see you can skip dozens of instructions with the conditional version. That will make the branching code faster overall on AVR (given a reasonable amount of data where the first two differ). I can see how a compiler team would have to spend a lot of time deciding where to draw these lines. If on AVR branching is definitely better and on an ARM64 machine it definitely is worse then where do you draw the lines for machines in between? And when the datatypes change size where do you put the lines? If you change your types to be 32-bit then now gcc will go branchless on x86 and both ARMs. But AVR still branches.
So the gcc team just has to get in and redraw some lines for 64-bit types.
No, I don't really mean that, I don't mean to trivialize it.
Meanwhile, I tried some likelys and unlikelys to see if it would help things and it doesn't. I did run into this though.
That code on ARMv7-A is a travesty. Not only do we know you could do this with only 3 registers so you don't need to spill to the stack but even after you do spill that code at the top is inappropriate for ARMv7-A. It would be right for ARMv6-M, but on ARMv7-A you don't need to remake a pointer to the end of the stack space and stmdb, you can just stm from the bottom up. You don't need to set ip at all.
Kind of underscores your point even more.
1
u/bigmell Dec 23 '20
An order of magnitude performance increase? No no no. Order of magnitude performance increases usually mean the old thing was coded incorrectly. 10-20 percent gains, maybe 30 in some extreme cases that C is known to do poorly, but nothing like an order of magnitude. Thats like you did something in seconds everyone else did in hours. Assembler aint that fast.
You haven't been looking very hard then.
its not exactly common knowledge from what I understand. What applications are not "fast enough" in C that require hand written assembly? My understanding is maybe its the difference between 9 and 10 hours execution time. Still runs overnight. Still runs over a weekend. Not worth the effort usually.
1
u/th3typh00n Dec 23 '20
Someone else in this thread mentioned x264, and that's a good example. Go compile it and run it with and without assembly optimizations enabled and compare the numbers.
0
Dec 24 '20
[deleted]
3
u/th3typh00n Dec 24 '20 edited Dec 24 '20
Sure, but intrinsics are pretty much 1:1 mappings of assembly instructions and as such it's basically inline assembly with a different syntax.
I don't really categorize intrinsics or inline assembly as "C code", even though both of them can be written inside a .c file.
Oh, and intrinsics aren't even a part of the C standard - they're non-standard non-portable platform-specific vendor extensions.
8
u/jsburke Dec 23 '20
Its hard to imagine a reason to go lower level than C these days. There is absolutely nothing more universal than C. Nothing more widely known, used, tested, and optimized.
While I'm "that guy" I overall agree. I'm in assembly not infrequently for non-hobby projects.
I've needed to go lower than C recently because of custom processor extensions. My only real option in C would have been something horrendous like
__asm__(".word 0x32001200")and I didn't want myself or anyone else trying to support that house of cards. Some bootrom related stuff as well, assembly has been begrudgingly the better fitAside from this kind of stuff, I'd imagine multi-threading libraries on ISAs that feature weak memory models might be better handled in assembly sometimes than C, but I think that might be my preference to avoid inlining assembly coming to the surface.
Overall, I agree, production quality code should be done in at least C. It's easier to support than assembly and you probably cannot optimize better than compilers like clang or gcc all that often, especially with highly optimized processors that have deep pipelines
1
u/bigmell Dec 23 '20
and I didn't want myself or anyone else trying to support that house of cards
I agree that assembler can be better in certain cases when you know exactly what you are doing. However I doubt that assembler would be easier to maintain then the corresponding C code. The guy who could clean up your assembler project behind you is almost certainly a needle in a haystack.
Its good to see a couple assembler guys still around. It always made me sad to think that all these guys wrote all these wonderful things in assembler, and my generation came along and said "use C assembler is too hard!" And now the new generation is saying "use Ruby lol." as I shake my head in disbelief.
I realize gcc is often good enough, but I wonder what happened between two generations where they are hand writing nes and atari games in assembler, and this generation that can barely get a flash game working properly.
Almost the only answer is we humans are getting stupider. Maybe some "we didnt need to learn assembler all that stuff was already done" type arguments. But where did that brain power and ability go? Why does it seem like computing is getting dumber and not smarter? I was amazed at the magic people could do in lisp, but it seems todays generation can barely write a function or a for-loop at all.
7
u/fartsAndEggs Dec 23 '20
This article isnt about getting more performance from lower level languages. Its about how the C abstract machine doesnt map onto modern hardware, and also how we can redesign modern hardware to not be designed with C in mind. Doing so is good, for reasons. It has nothing to do with writing assembly
→ More replies (2)9
u/DadAndClimber Dec 23 '20
Around the same time, Rollercoaster Tycoon was written almost entirely in x86.
1
u/bigmell Dec 23 '20
now that is strange because from what I remember Rollercoaster Tycoon wasnt a super complicated game, kind of like a sim earth. Do you think it was necessary to write a game like this in assembler? It wasnt that detailed or real time or any of that kinda stuff right?
1
u/DadAndClimber Dec 23 '20
I think it's just what the developer knew. I think it was just one guy who built the game.
0
Dec 23 '20
[deleted]
4
Dec 23 '20
Using a CPU core is high level language, only low level language are transistors and solder
2
2
u/yiliu Dec 23 '20
You're missing the point of the article, though. It's not a question of assembly vs. C. Modern assembly is tied to a computer architecture that's been obsolete for decades, because of C and the piles of legacy code written for that (abstract) architecture. Modern CPUs have all sorts of complexity that's hidden by the assembly language in order to provide the illusion of sequential execution, flat memory, etc.
You're right that there's not much point moving to assembly to write code for modern processors. But the point of the article is that you could hypothetically come up with a new low-level paradigm (i.e. assembly language) that factored in different layers of memory, pipeline or parallel execution, shared and immutable memory, and so on, and come up with something that could be targeted by languages very different from C (like Erlang) to produce something much faster, simpler, and safer.
1
u/bigmell Dec 23 '20 edited Dec 23 '20
yea, it seemed like a convoluted way of saying we should write a new language, with hookers, and blackjack. It will never get the legs C has. C has been around for 50 years, only an idiot would think we could rewrite something faster in a decade.
Some guy and his ego who will get lost in the complexity and disappear probably before a first version. Or actually produce a first version no one ever uses, because they can just use C. Is he also gonna replace all the school textbooks, curriculums, and sample code? Ridiculous. Sounds like guy on the couch wants to make his own NBA, but with blackjack and hookers.
I worked at Nationwide's corporate development office with a bunch of guys like this. They wanted to replace their 30 year old risk assessment system in Cobol with something the new guys threw together in Ruby. You just cant rewrite something that big and sprawling in a couple years. There were so many hand coded corner cases that the rewrite failed for anything other than the simplest case.
A guy does boat coverage along the coastline and had some special work done on the system for boat cases and hurricanes like a decade ago. NONE of that stuff survived the new version. It was a horrible mess. This type of large rewrite is completely impractical and seemed to be the point of the article to me.
All device drivers are written in C, and the underlying hardware hasnt changed more than the new hardware does the same as the old but faster. All that multicore parallel processing stuff is just marketing anyway. You cant do parallel processing on one processor its just kind of a software simulation that puts the operations back in serial. There are very few jobs that can be executed in parallel, especially without explicit instruction. i.e. The compiler will never be able to parallelize it by itself just from looking at the code. And the memory model has hardly changed as far as caching and the like as far as I know.
1
u/the_gnarts Dec 23 '20
The last time I heard someone seriously using assembler was when John Carmack wrote bits of the quake engine in it because performance was a huge issue. But those days seem a thing of the past.
Crypto primitives tend to be implemented in ASM, to give another example.
1
u/bigmell Dec 23 '20
oh cool, I also heard cryptographers are the main users of the quantum computing stuff too.
0
Dec 23 '20
[deleted]
5
u/happyscrappy Dec 23 '20
What does extremely precisely controlled stack layouts mean and why do you need it? If you take an interrupt then you do receive data in a certain stack format and you have to match that. But when you call out to the next function below you can use any standard you want. So you can write all that in C.
With the work I do we have to do things like both of your first examples so every engineer has to be able to read and write assembly. But they rarely do because most of the work in an OS is in the algorithms and APIs and all that is in C. Once in a while we have to add a function which loads a 128-bit value using a specific instruction (you cannot force the compiler to do such a thing) but that's rare. By far most of our work is in C.
The fact is, no compiler out there can match a human with a copy of the processor reference docs and Agner Fog's manuals.
I guess that's an x86 thing? Because on ARM most of the time the engineer's first cut at an assembly language function will be less well optimized than compiled C. Yes, the engineer can refine their function and often win, but the compiler is so good at what it does that my instructions are to first write the function in C and compile it and disassemble it. Turn that into assembly source and then look at that and see where you can optimize. This way the compiler does the standard stuff like the calling conventions and can even find some optimizations you might not find. But then you spend the time making it even better.
1
u/AVTOCRAT Dec 24 '20
To your second point -- isn't that the process /u/luvemfloppy is describing? As you say, it takes a lot of work, but you do end up with more efficient code (though whether it's worth the effort is another question).
1
u/happyscrappy Dec 24 '20
It isn't. He said pull out the processor manual and a PDF and have at it and beat the compiler.
I'm saying use the compiler and just improve where you can is usually better than looking at a table of cycle timings and instructions. Because the compiler people have those tables too, and while they may not catch every trick they usually will do better than you will.
0
2
u/bigmell Dec 23 '20
Too cool man its good to see people still around with that skillset. I did some old motorola 6800 stuff but nothing production level. I knew hand tuning loops in assembler will get a performance bump, just wondering if it wouldnt be better to say "hey, this is just gonna take a couple extra minutes to run."
Hand tuning assembler is some serious work. Especially when you already have something running a little slower in C. It takes 9 hours instead of 10 is not a really big deal these days... or is it? That was the theory I read around when .net was kinda new.
2
u/stirling_archer Dec 23 '20
They inspect adjacent operations and issue independent ones in parallel.... In contrast, GPUs achieve very high performance without any of this logic, at the expense of requiring explicitly parallel programs.
Not critical to the article's main point, but this is not true at all. In addition to massive thread parallelism, [NVIDIA GPUs have allowed for instruction-level parallelism since at least 2011] (https://www.anandtech.com/show/3809/nvidias-geforce-gtx-460-the-200-king/2), and it's critical to also expose ILP to get peak performance out of GPU code. The ideal case is dual-issuing, where instructions in a single thread are executed fully in parallel if they fall on different execution units.
2
u/RandomDamage Dec 24 '20
Too many of the things he blames on C are things that C programmers have been fighting with for decades because it's necessary to optimize for the hardware, and now somehow C is to blame?
Cache is hidden from programmers because of programmers that think they can optimize cache access better than the microcode developers can.
The same sort of developers who are the reason cooperative multitasking was a dead-end.
4
u/SupremeBaconist Dec 24 '20
C SUCKS... in the modern age of computing.
It didn't used to suck. It was a very high level versatile language for its time that helped create and popularize Unix, one of the most important and inspirational OSes to exist. But... then C stuck around because it was THE Unix language. This wasn't a problem for a while but then things started improving and for C legacy reasons as this article explains we were held back in terms of improving our hardware. C also has some draconian features even its creator regretted making like its preprocessor which C++ has been trying to make obsolete and for good reason. C is a tool that served its purpose yet still sticks around after its prime and causes problems like a hungry ghost.
3
u/B8F1F488 Dec 23 '20
The issue is that these arguments can be applied to all sorts of contemporary programming languages. C has survived the test of time, many languages have raised against it and died over the past decades. It is very unlikely that we will see C replaced without significant hardware architecture change. The reality is that the abstract machine of the language was carefully modeled after existing hardware at the time and the subsequent hardware changes were modeled to fit somehow that abstract machine. The argument that the hardware is so much different that it doesn't fit the C model no more is insane.
For example C might not understand the cache hierarchies, but a good C programmer does understand it, and the cache hierarchies were built in a way that a good programmers can exploit them. There is no magical alternative to C for which that hardware was designed for. You got it backwards.
4
4
u/photonymous Dec 23 '20
This was a great read, but I'm not quite sure what to do now... Is there a language a performance addict like me should learn? (other than assembly) or is there a different processor architecture I should consider?
21
Dec 23 '20
Well, given that we're in a catch-22 between the body of existing software, which relies heavily on C and C-based languages, and the processors whose designs are already bent backwards to accommodate that, I don't think there's much you can do. The paper seems more aimed at academics brainstorming about processor design than individuals writing code.
Assembly is indeed not really going to help here, and writing it would only increase the lock-in to current CPU design by tying yourself directly to their instruction set. If anything, I'd encourage people to write in high-level functional languages more, that allow for easy parallelisation, and worry less about how many individual cycles something takes on current processors (unless in a hot path, of course).
6
u/realestLink Dec 23 '20
I mean. Assembly doesn't let you deal with any of it either really. Itanium had a way of manually controlling the pipeline using assembly, but Itanium basically died out
0
Dec 23 '20
[deleted]
1
u/realestLink Dec 23 '20
Oh yeah. I totally agree. I was just pointing out that most modern CPU's are complex and heavily abstracted away and how exposing some of the details directly has been tried before.
3
u/carrottread Dec 23 '20
You may find ISPC (https://ispc.github.io/) interesting. See also its story: https://pharr.org/matt/blog/2018/04/30/ispc-all.html
1
3
u/ffscc Dec 23 '20
Maybe you would be interested in ispc. It's a c-like language that is designed to leverage SIMD.
2
2
u/red75prim Dec 23 '20
Verilog
1
u/photonymous Dec 23 '20
Are there any fpgas at gpu prices with gpu-competitive performance? ...and with affordable development tools?
2
u/bythenumbers10 Dec 23 '20
Julia seems poised to take advantage of any speed available, and is gaining ground in HPC applications. Or you could pick up VHDL and start working out your own hardware.
1
Dec 23 '20
What type of performance for what use case? Databases, HPC, SDR or gaming require different sorts of trade offs in hardware and software configuation.
1
u/krista Dec 23 '20
check out harvard architecture and dsp programming, as well as an hdl for an fpga.
0
Dec 23 '20
[deleted]
2
Dec 23 '20
I think you misunderstand the article. The article is about needing lower level access to how the CPU works, so using something other than a CPU doesn't solve that problem. Secondly, on current GPUs programmers don't even have access to a ISA, we can only access through a driver, which is another layer of software between you and the hardware that C doesn't have. So in that sense every GPU is "higher level" than C.
1
u/tasminima Dec 23 '20
Why there is this need is not clear at all from the article, and previous attempts never lasted compared to the current CPU / GPU models; and the modern CPU approach has simply been refinement from what was done in the PPro (and, in some aspects, its competitors) era. High level from the application programmer point of view is often extremely good because it can allow dynamic optimizations and backward compat. The intermediate layer can be implemented in hardware or in software.
I'm curious where "C" propagates down to the processors and constrains the micro arch that much. Maybe you want very weak memory models? Already possible, even to unreasonable level see the Alpha; and well it was not a good idea to go that weak. The M1 seems to do extremely fine and it is not really because it departs from "C". Get ride of byte/word aliasing? I don't know, use a DSP, but you can program it in "C"... Explicit handling of the memory hierarchy? Use a Cell, maybe? But I doubt game developers want that kind of thing back. Replace the need of a gigantic OOO engine by smarter compilers? hum yeah IA-64 was soooo successful.
1
u/jdefr Dec 23 '20
I think the reason C is so successful and universal is because it provides just enough abstraction to be useful but enough detail to adequately be processed by modern computing architectures. C was one referred to as portable assembly language. In some respects that is true. I can nit-pick on why maybe it shouldn't be considered as such but I am not sure that's worth any ones time.
0
Dec 23 '20
[deleted]
1
Dec 23 '20
The introductory paragraph mentions meltdown and spectre so the article is not just about performance.
1
u/agumonkey Dec 23 '20
May I ask why the ordering from high to low ?
Every language have a semantic space where some things are more straightforward, but people made asm generator in prolog and tree matching in Pascal.
1
u/SarHavelock Dec 23 '20
Like why do we draw distinctions between C and Python and call one high-level and the other low-level?
2
u/agumonkey Dec 23 '20
I kinda know why but I think at some scale, the idea becomes moot. You can have some code in python (or php, or perl or ada) that may tap into a tiny bit of asm, or a native code generator or else, which globally will beat the average C program due to how you approached and solved the problem.. something that is not guaranteed by the abstraction level of a language (you could be a cpp master and make a suboptimal solution that fails while a smart python team will approach it in another way)
4
u/SarHavelock Dec 23 '20
The difference is that low-level operations like memory management, pointer arithmetic and inline assembly are staples of C programming. Moreover, C is an abstraction layer over assembly whereas other languages are, ultimately, built on top of C.
While higher level languages can reach C speeds for certain operations, there is a reason operating systems and embedded systems favor C: C has a smaller runtime but still gives you everything you need. If Python or Lisp can do it, so can C. Higher level languages just make complex tasks easier than C does.
1
u/dnew Dec 23 '20
If you want to see one of the new architectures he's talking about, check out millcomputing.com. They have some really wild architecture ideas that (for example) solve his vectorization problem in a very novel way.
1
u/merlinsbeers Dec 23 '20
The day microcode was invented μP opcodes became a HLL.
2
u/pjmlp Dec 23 '20
Which was basically 10 years before C was even an idea.
1
u/wikipedia_text_bot Dec 23 '20
The Burroughs Large Systems Group produced a family of large 48-bit mainframes using stack machine instruction sets with dense syllables. The first machine in the family was the B5000 in 1961. It was optimized for compiling ALGOL 60 programs extremely well, using single-pass compilers. It evolved into the B5500.
About Me - Opt out - OP can reply !delete to delete - Article of the day
This bot will soon be transitioning to an opt-in system. Click here to learn more and opt in.
1
u/slaymaker1907 Dec 23 '20
Interesting take, though the biggest culprit IMO is virtual memory. Virtualized memory is a huge abstraction and a lot of CPU space is dedicated to making it fast.
1
1
u/AbleZion Dec 25 '20
Kind of reminds me of that this talk, The Future of Programming, for some reason.
Maybe it's the part where he talks about researchers trying to find the next level of abstraction. The Actor Model was supposed to replace threading so that people could program for the newer multi-core/multi-cpu architectures at a higher level. They knew back in the 70s that the current model of computing wouldn't scale.
Maybe the problems we face are similar to those that people researching the Actor Model figured would exist.
1
u/rustjelqing Dec 23 '20
C is a simple language relative to many other languages. Forth moreso. The problem with other languages is the complexity they introduce as dependencies. A good or bad non-conforming C++ compiler is a beast to recreate. A similar C compiler is something most peasants can manage.
Just look at Rust which for a long time had a hard dependency on LLVM and therefore a way to build LLVM and thus C++ and down the rabbit hole.
1
u/realestLink Dec 23 '20
This is a very famous talk that I first saw a few years ago. It's pretty good
1
u/hector_villalobos Dec 23 '20
I think this depends on perspective, for example, I'm always surprise that for llvm compiler, the languages are frontends, so, it's weird to read that Rust is a frontend for llvm. For me C is a low level language because I program using Ruby and Javascript.
1
0
u/lazyant Dec 24 '20
If you can write a whole operating system in it like Linux (C) or Windows (C++) it’s low enough, so it depends on the definition.
3
u/LardPi Dec 24 '20
So many people read the title and fo straight to commenting. The article is not about whether C is low level or not. This is about realizing the difference between the C abstract machine and modern hardware and acknowledging the implications of such a difference.
-2
u/lelanthran Dec 23 '20
"C Is Not A Low-Level Language"
Oh? What's lower than C?
crickets ....
3
u/SarHavelock Dec 23 '20
Oh? What's lower than C?
Lots actually, but you're not wrong. C is as low-level as it gets for high-level languages, but there are lower level languages like Assembly.
2
u/amal0neintheDark Dec 23 '20
I agree with your sentiment here. The article says
"Computer science pioneer Alan Perlis defined low-level languages this way:
"A programming language is low level when its programs require attention to the irrelevant."5
While, yes, this definition applies to C, it does not capture what people desire in a low-level language."
So it is and it isn't.
Sorry to disturb the crickets.
2
u/onequbit Dec 24 '20
"C Is Not A Low-Level Language"
Oh? What's lower than C?
...
Assembly
1
u/lelanthran Dec 24 '20
If assembly is the only thing lower than C, how can you say that C is not a low level language?
Let's make it clear - are there more languages that are higher level than C than there are languages that are lower level than C?
If we ranked all programming languages in a list where the highest were at the top and the lowest were at the tom, where about in that list would you expect to find C?
-5
Dec 23 '20 edited Dec 24 '20
I love C. I started with it back in the early 80s. I love the simplicity, the lack of "clever" CS concepts and so on. It is great on my little IoT systems
This article very cleanly explains why using C on anything more complex is just bonkers. Great post!
Edut: clearly some people are humour impaired. Just because I love C for its simplicity doesn't mean I don't love other languages for their completeness. The world isn't binary, you can like multiple things.
0
u/rakidi Dec 23 '20
Clever CS concepts such as?
→ More replies (13)1
u/SarHavelock Dec 23 '20
Builtin objects? If you can do it in X, you can do it in C, with more or less effort.
→ More replies (1)
0
u/tonefart Dec 23 '20
Considering that most people only know javascript or python nowadays or some other managed language, C is pretty much low level to them.
35
u/mohragk Dec 23 '20
Wasn’t C created as a high level language? No fussing with instructions in assembly but just create expressive code and let the compiler handle the low level stuff.