10 ms·
Hello. I'm a compiler
- Swizec 14y agoFascinating, while I was reading that, it got 5 upvotes! Mindblowing. This is a really awesome description too. The only thing I know about compilers is that I implemented one for class (without fancy optimisations) and I am surprised any software works, ever. Compiler are just ... mindbogglingly complex things. Almost as much voodoo dark magic as engineering.
- perlgeek 14y agoOne of the reason software still works is that compilers are (mostly ()) very easy to test. Just just need an input source code, and expected outcome (either compiler error, or the result when running the code). No intermediate state to consider, no concurrency, no test database to set up etc. Oh, and many serious compilers can compile themselves, so bootstrapping is a pretty good test too. Another reason is that compilers simply must* work for software development to continue, so people have spend the necessary amount of energy to get implementation and tests "right". (*) There are always exceptions, like when a compiler auto-parallelizes code and introduces a race condition that is rarely triggered. But those cases are blessedly rare.
- Swizec 14y agoTesting was the hardest thing I came across. You have on your hands a compiler that you can no longer think of a program for, that would produce an incorrect result. You're happy. Then your friend comes along and the first thing they try behaves incorrectly. The problem isn't even that the compiler would crash and burn, it lovingly compiles the input program into something that does the wrong thing!
- michaelt 14y agoThere are tools available that can generate random (valid) C programs, comparing the results generated by code from different compilers [1]. CSmith say bugs have been found in every tool tested [2] - from GCC to costly commercial compilers. [1] http://gcc.gnu.org/wiki/summit2010?action=AttachFile&do=get&target=regehr_gcc_summit_2010.pdf http://gcc.gnu.org/wiki/summit2010?action=AttachFile&do=... [2] http://embed.cs.utah.edu/csmith/ http://embed.cs.utah.edu/csmith/
- eru 14y agoWhat did you write your compiler in? Writing compilers in, say, raw C is indeed a complex endeavour. But using, say, OCaml or Haskell (which is secretly a DSL for compiler writing) should make it much easier to not fail silently.
- dragontamer 14y agoHey, my name is ICC, and I'm one of the most respected compilers in the industry. I also sabotage your code so that it works poorly on AMD CPUs, while making sure that Intel CPUs run my code at full speed. After all, Intel likes to establish market dominance. http://www.agner.org/optimize/blog/read.php?i=49#49 http://www.agner.org/optimize/blog/read.php?i=49#49 Blind trust in the compiler is bad people. Good luck discussing this issue without any Assembly Programers who can fully understand what is going on here.
- friendly_chap 14y agoYou can see the upvote counter constantly being updated ATM :)
- mjs 14y agoUgh, it's being edited constantly too! Someone just removed the "Hello" from the first line, which was part of what made it great...
- ygra 14y agoFor some reason that change was attributed to me when all I did was adding a comma after the »Hello« o.ô EDIT: Ouch. I think those are SO's automatic attempts at making answers less chatty by removing salutations and thanks and the like. I'll rollback or try fixing it by doing evil Unicode things, then.
- MojoJolo 14y agoBoo. I read it with "Hello" and at that moment, I knew it will be a great read. Why someone remove it?
- friendly_chap 14y agoStackoverflow is plagued by overzealous editing IMHO. I understand it's hard to balance it, but a question of mine recently got 3 downvotes and a close, to be reopened later! And now it even has 2 upvotes. A guy answered it immediately, while some other peeps started whining about how it is not a proper question! I only wanted a quick fix, and I got it too, while the others nitpicked. Hilarious.
- qompiler 14y agoHello. I'm a programmer. I noticed you couldn't optimize my code to use SIMD so I went ahead and used inline assembly. It will probably take another 30 years before you can actually think like a human and perform optimizations like this.
- Scaevolus 14y ago30 years? ICC already does automatic vectorization pretty well. LLVM and GCC have implementations that need more tuning. I bet they'll be solid within 5 years. You still beat them with inline assembly, but you probably won't accelerate the code vectorwidth times anymore.
- raverbashing 14y agoExactly ICC is the best one, but GCC from 4.0 could do it "automatically" (being very loose about the term) And no one does inline assembly for that, they use intrinsics
- cwzwarich 14y ago> And no one does inline assembly for that, they use intrinsics Depends on your architecture. With ARM, compilers will often not set the alignment bits in vector loads and stores, and this can be a big performance hit depending on the microarchitecture. In general, compilers sometimes deal poorly with instructions that have particularly strange register constraints, or loads/stores with address writeback.
- raverbashing 14y agoGood point
- pjmlp 14y agoCommercial compiler vendors tend to invest more in code generation quality as open source developers. After all they need to provide reasons why you would buy them.
- yen223 14y agoIt isn't closed or locked? Huh.
- ygra 14y agoOnly questions are closed or locked, I think. And only if they are not very good questions that will just lead to debate or opinions but should stay around out of historical interest. This here is a question that can be answered reasonably but has a single whimsical answer. So no need of closing or locking here. And even though the tone of that answer is humourous it still is a good answer, explaining why we don't all write Assembly instead of HLLs.
- andremedeiros 14y agoYeah, I thought it was a great read as well. That answer pops to my mind every now and again.
- BitMastro 14y agoAaaand it's closed. I hate that SO can close a question as "non constructive" while I find it constructive instead.
- sonergonul 14y agoNot closed or locked. In SO, only the questions can closed, answers and questions can locked. But this question is open for everybody, for now..
- Achshar 14y agoIt was just closed as not constructive.
- Xcelerate 14y agoThe poor compilers do the best with what they have. And by "what they have", I mean the things they can't make assumptions about. Which turns out to be a crapload. Until that happens, very highly-tuned assembly will continue to outperform the best compilers. Of course, a more typical scenario is hand-tuning a few loops where 99% of the clock cycles occur and letting the compiler take care of the rest. (Also, I was attempting to send a Morse code message by flashing the vote counter between up and down, but I don't think anyone got it :( )
- timmipetit 14y agoI saw the flashing vote counter, and was just thinking that SO was getting funky.
- andremedeiros 14y agoI will meet you at said coordinates and bring the pineapple you've requested.
- andrewaylett 14y agoI used to work on a compiler for an embedded system. One fun trick we used while writing the run-time libraries was to write them in C, then optimise the generated assembly (possibly by rewriting it from scratch), then fix the compiler so it generated that assembly :). Sometimes that did require using (and occasionally adding) new intrinsics.
- eru 14y agoHow stable where those fixes? I.e. if you made slight changes to the C code, did the optimizations still kick in?
- smcl 14y agoI worked in the same company (Hey Andrew!). It would definitely depend on the specific instructions/optimisations involved but I believe they were generally pretty robust to changes. Some would require the user to supply pragmas (i.e. to specify data alignment, etc) but obviously the compiler intrinsics would always generate the expected instructions.
- orangethirty 14y agoAs someone who is working on a project where the CTO demands more LOC, this makes me warm inside.
- raverbashing 14y agoYes, from that comment I think I can infer what CTO means in this context You can easily go creative on this
- eru 14y agoThat should be hard to believe, save for my honed cynicism. Could you please elaborate?
- orangethirty 14y agoI'm working on a project where the CTO of this huge company is criticizing the lack of code I'm writing. Doesn't matter the fact that my code meets every criteria. No functionality is being left out. But, he wants more code. It has been very difficult for me to deal with this, because its the first time I have ever faced such idiocy.
- ncallaway 14y agoThat sounds terribly frustrating. Have you tried including war and peace in a comment (http://www.gutenberg.org/cache/epub/2600/pg2600.txt http://www.gutenberg.org/cache/epub/2600/pg2600.txt)?
- orangethirty 14y agoIt is quite frustrating. More so when the project demands the system I'm building be very lean and fast. I'm talking 100K/requests per second here.
- jordanwallwork 14y agoAm I the only one just enjoying watching the upvotes rocketing up on SO?
- deleted 14y ago[deleted]
- quorn3000 14y agoNO!
- shared4you 14y agoI upvoted when the counter was 520. Now it is 1500! (or more, by the time you read this :) EDIT: Looks like it's locked at 1788!
- johncoltrane 14y agoThis is the first time I see the vote count grow by 10 in real time. Impressive stuff.
- ck2 14y agoAre they pushing those realtime vote counts with websockets? Pretty slick. Beats polling by far. Looks like IE is holding it back as usual: https://developer.mozilla.org/en-US/docs/WebSockets#Browser_compatibility https://developer.mozilla.org/en-US/docs/WebSockets#Browser_...
- ygra 14y agoUnless my eyes are strange that page says it's supported on IE.
- ck2 14y agoThis chart is easier to read: http://caniuse.com/#feat=websockets http://caniuse.com/#feat=websockets Only IE10 and above. And IE Mobile would be dubious. Even older safari 5.1 has partial support but not IE. Surprised to see android mobile does not have support - is that chart correct?
- serge2k 14y agoIE10 came out around the same time as Firefox 16 didn't it?
- adieulot 14y agoYes that’s websockets it seems. Must be quite a overhead, they use it on all answers.
- Trufa 14y agoBut it's awfully cool when you see it going up or down :)
- naiquevin 14y agoYes, it's websockets. You can check it in Chrome by keeping the console open and refreshing the page. Would be interesting to test in IE. BTW, as much as I am amazed to see websockets in action, the real time updating of vote count feels creepy and distracting at the same time :-)
- gilgoomesh 14y agoCute. Although I think my compiler has different things to say. Hello, I'm a C compiler that still can't handle C99. I hope you're wearing waterproof clothing because I'm gonna throw up on you. Also, I've been drinking heavily so your C++ code is going to take a while to compile and when it's done, it's gonna smell funny.
- valdiorn 14y agoVisual C++ ? :)
- pjmlp 14y agoC++ compiler != C compiler
- brokenparser 14y agoI'll take the blame for that mishap.
- spatulon 14y agoVisual C++ is a C compiler - it compiles C90. The issue is that they still haven't added support for C99. You can compile your C code as C++, but then you're restricted to the common subset of C and C++.
- pjmlp 14y agoNo it is not. It is two compilers bundled together and called by the same driver application. Microsoft will not update the C compiler and will focus only on the C++ one. As explained by their main architect: http://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99/ http://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and... Bundling C and C++ compilers together was a way for C++ vendors to ease the migration into C++ land, specially in the early days. There is no law that C++ compiler vendors are required to offer a C compiler as well. Given that on Build 2012 it was mentioned that the Windows team is making their code compilable under C++ and the actual position on C++ vs C at Microsoft, the C compiler might even be dropped from Visual Studio.
- deleted 14y ago[deleted]
- sonergonul 14y ago:D iyi deneme.
- deleted 14y ago[deleted]
- patlaksincap 14y agowhat about me a.q.!? what is my deficient! nick:dictummeumpactum
- ck2 14y agoIsn't Steve Gibson ( http://www.grc.com/stevegibson.htm http://www.grc.com/stevegibson.htm ) still coding large, complex programs in pure assembly? His work on spinrite is legendary, for those born before IDE hard drives were invented.
- B-Con 14y agoFrom his podcasts I hear that it is pure assembly. He's borderline machine, though.
- dschiptsov 14y agoIt is funny how people want to believe in tools. In fact, the optimizations compiler does are incomparable with those programmer could do by choosing an appropriate data-structure with corresponding algorithms and by being aware of strengths and weakness of a particular CPU architecture. JVM, which is nothing but a stack-based byte-code interpreter is the most famous case.) People seems to believe it can do wonders, especially in memory management and data throughput. It is so strange to see how people are trying to create a whole world inside a JVM. What it is called when people are building models of ships inside a bottle?) btw, now, it seems, they are trying to build a whole world inside a V8 bottle.)
- lmm 14y agoMy experience in the industry is just the opposite; people will do anything to avoid admitting that the tools can do better than they than. I've had senior programmers sneeringly declare that anyone who uses a debugger must be an inferior programmer, and then watched them spend a full day figuring out a bug that could be found in half an hour with the appropriate tool. You know why it's worth recreating the world inside a JVM? Because its behaviour is so much more thoroughly specified and predictable. Prior to Java, C++ didn't even have a memory model; every new release of GCC breaks a whole raft of C programs that didn't realise they were invoking undefined behaviour by having integer overflow. By the C standard, even a single instance of undefined behaviour invalidates your whole program, making it virtually impossible to predict the behaviour of any nontrivial-sized codebase.
- dschiptsov 14y agoIn my opinion it is a computer architecture that has a memory model, not Java or C++. For some strange reasons nginx - a mere C program - works for millions. So does postgresql or redis or whatever.
- tonyarkles 14y agoI half agree. C++ directly exposes the underlying computer architecture's memory model. Java abstracts it reasonably well and essentially results in a different model with different advantages and disadvantages (e.g. garbage collection and garbage collection :))
- jawerty 14y agoIt's even better when you read what the question was.
- detay 14y agoYet another spark of genius from ssg.
- sonergonul 14y agoYeah..
- majmun 14y agoCompiler got AI human language capabilities?
- Nikolas0 14y agoNow I can't wait to get a message from the kernel :D
- Arjuna 14y agoAnytime I read about the topic of assembly language, I can't help but think of Michael Abrash. For example, check out Chapter 22 [1] from his Graphics Programming Black Book entitled Zenning and the Flexible Mind for a pleasant stroll down Optimization Lane. You might also enjoy his book entitled The Zen of Assembly Language which features the Zen Timer (a timing tool for performance measuring). [1] http://downloads.gamedev.net/pdf/gpbb/gpbb22.pdf http://downloads.gamedev.net/pdf/gpbb/gpbb22.pdf
- mmphosis 14y agoHello, I am a programmer. I have little idea what modern day compilers are doing, or what the CPU, or the operating system is doing for that matter. Often, way too often, compilers fail, hardware fails, operating systems fail, lots of things fail. I am not going to read the millions of lines of code written by other programmers (in f-ing emacs no less) in the any number of differing complex beasts, the compilers. It seems crazy-making to me, that other programmers would create compilers that would use millions of possibilities of optimizing a single line of mine using hundreds of different optimization techniques based on a vast amount of academic research that I won't be spending years getting at. I do feel embarrassed, yes very icky, that I have little to no idea what a three-line loop will be compiled as, but bloat would be my guess. There is risk in going to great lengths of optimization or doing the dirtiest tricks. And if I don't want the compiler to do this, I have no idea how to stop this behavior, nor do I want to invest in the specific knowledge of the nuances any particular compiler. The compiler does not allow me easy access because the compiler itself is an overly complex piece of software written by other programmers. I could care less about how a compiler would make my code would look in assembly, on different processor architectures and different operating systems and in different assembly conventions. Transformation comes with how we as programmers write code, not in compiler-fu. P.S. Oh, and by the way if I really wasn't using half of the code I wrote, I would throw the source code away.
- simias 14y agoYou seem to be saying that you're at the same time completely clueless about how programs get built and executed and yet you know better that the compiler what needs to be done. I've found that in the general case Compilers Know Better. It might not be true for very simple and limiter architecture such as small microcontrolers, but modern CPUs are so much complex and "quirky" that most of the time the compiler will beat you. These days I only use assembly for very low level stuff where I need complete control of the execution flow (dealing with cache invalidation, MMU etc...) or some very specific and aggressive optimisation (like implementing <string.h> in ASM). But hey, if you want to ditch C and write everything in ASM, be my guest (as long as we don't work together).
- 14y ago
- kaffeinecoma 14y agoI never noticed that the Stackoverflow js pulls updates for vote tallies in real-time. Browsing this answer while HN is sending lots of traffic there is almost like watching a car odometer.
- mrleinad 14y agoReally? HN is now including witty posts in Stackoverflow as news?
- drhowarddrfine 14y agoI have never seen so many stupid, misinformed comments by people who have never touched assembly in my life. But this is the internet.
- opminion 14y agoAh, the irony of Knuth being the proponent of Literate Programming, and at the same time the last man standing in general purpose assembly.
- Breakthrough 14y agoAnd on the Fourth Day, God proclaimed "Thou shalt have the ability to use inline assembly in thy C/C++ code for performance-critical tasks". I can think of absolutely zero reason to write an entire program in x86 assembly, let alone any other kind of assembly (GCC spits out some pretty optimized code for my little Atmel MCU)... It's a lot nicer to write everything in a high-level, and then write any performance specifics in inline assembly. The really cool thing to see is how other newer languages have adopted this scheme (e.g. PyASM for Python, or the ability to edit the instructions for interpreted languages that run in their own VM). And as always, great power comes with great responsibility ;)
- ohwp 14y ago"I can think of absolutely zero reason to write an entire program in x86 assembly," I can think of 2 reasons: study and fun.
- tedajax 14y agoFor x86 I'd say study but I don't think there's any fun to be had there. Something like MIPS32 would be pretty fun because that instruction set actually makes sense to a human.
- LockeWatts 14y agoI'm in a class using MIPS, and I don't find it fun.
- ryanmolden 14y agoOh, come on, the ModR/M byte, opcode extensions, variable length instructions, opcode prefixes, what's not to love </sarcasm>
- bitwize 14y agoFunny. x86 was designed to be understandable to humans. MIPS32 was designed to be more suitable for compilers.
- dragontamer 14y agoHello. I'm an assembly programmer. I used a compiler to generate the majority of code, and can hand-craft any assembly that comes out of it. I understand how compilers auto-generate SIMD instructions can be more easily compiler-generated if I make a "struct of arrays" instead of "an array of structs". TLDR: Real performance programmers need to understand the assembly a compiler generates if they hope to tune the compiler to generate optimal assembly. Also, GCC -O3 is prone to removing too much code and reordering it, causing memory barrier issues and the like. All multi-threaded programmers need to understand how the compiler generates assembly (ie: by reordering your code), and how it can generate new bugs if you don't use the right compiler flags.
- colanderman 14y agoAlso, GCC -O3 is prone to removing too much code and reordering it, causing memory barrier issues and the like. Whoah, that's what __sync_synchronize() and volatile are for. If you're trying to write order-dependent multithreaded code without those, the bug's in your code, not in compiler flag juju.
- oliland 14y agoThis answer purports a myth that compilers are magical black boxes, the sum of millions of hours of intense academic research that "you will never understand". Replace "compiler" with "computer". Doesn't that make you angry? Answers like these do nothing but prevent people from learning about them. If you are interested in compilers, here's Edinburgh University's notes from the course "Compiling Techniques", probably a good place to start. Don't let internet tough-guys stop you from learning. http://www.inf.ed.ac.uk/teaching/courses/ct/ http://www.inf.ed.ac.uk/teaching/courses/ct/
- kami8845 14y ago>computers are magical black boxes, the sum of millions of hours of intense academic research Isn't that awesome? I can use the thing without ever understanding what it does :) I'm so glad my car works without me ever having to know anything about how it works. I know it requires some form of money (gas) to work. That's it. Money in -> Transport out. Perfect. The linked post never proclaimed no one "will ever understand" compilers. That's just what you're reading into it. You try to get upset at it, so you do. It merely proclaims that compilers are incredibly complex, which is the nice thing about them. If compilers were stupid they'd be a lot less useful.
- endlessvoid94 14y agoHe's just saying the answer makes compilers sound intimidating.
- Marazan 14y agoCompilers are intimidating. I mean, the basics of parsing and lexing are easy enough to understand & do and forming an AST is straightforward but all the "clever stuff" adfer that, all the hundreds and thousands of optimisation tests that are done - they are mind boggling. Each one by itself is pretty straightfoward but start adding them up and layering them on top of each other and it gets crazy pretty quick.
- Confusion 14y ago
- davidroberts 14y agoThe best comment: "Thank you compiler, but perhaps if you weren't commenting on StackOverflow, you could get me a drink and play some nice music while you're working?"
- cobrausn 14y agoThis was linked in the comments (by the author): https://www.facebook.com/sedatk/posts/10151240841812644 https://www.facebook.com/sedatk/posts/10151240841812644 Sedat Kapanoglu · 2,372 followers 3 hours ago near Maslak, Istanbul Today I was at the Istanbul courthouse third time this year. I attended a trial defending myself to a judge. Then I bore my testimony to a prosecutor about a different case. Both cases were about the free speech platform I own in Turkey. Meanwhile in the world, one of my older posts in Stackoverflow became Hackernews #1 & reddit/programming #1. I wish it was Turkey which made me feel better about myself, not the rest of the world.
- _kst_ 14y agohttp://imagemacros.files.wordpress.com/2009/07/hianteater.jpg http://imagemacros.files.wordpress.com/2009/07/hianteater.jp...