Click here to Skip to main content
15,913,106 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
AnswerRe: My kid just crashed a Steam game he's playing Pin
Shao Voon Wong27-Mar-17 4:01
mvaShao Voon Wong27-Mar-17 4:01 
GeneralDart is so much easier to learn now. PinPopular
Jörgen Andersson26-Mar-17 10:53
professionalJörgen Andersson26-Mar-17 10:53 
GeneralRe: Dart is so much easier to learn now. Pin
OriginalGriff26-Mar-17 11:16
mveOriginalGriff26-Mar-17 11:16 
GeneralRe: Dart is so much easier to learn now. Pin
CDP180226-Mar-17 12:52
CDP180226-Mar-17 12:52 
GeneralRe: Dart is so much easier to learn now. Pin
Jörgen Andersson26-Mar-17 18:48
professionalJörgen Andersson26-Mar-17 18:48 
GeneralRe: Dart is so much easier to learn now. Pin
OriginalGriff26-Mar-17 21:39
mveOriginalGriff26-Mar-17 21:39 
GeneralRe: Dart is so much easier to learn now. Pin
Marc Clifton26-Mar-17 14:41
mvaMarc Clifton26-Mar-17 14:41 
GeneralWhy compilers aren't magic PinPopular
harold aptroot26-Mar-17 5:55
harold aptroot26-Mar-17 5:55 
Magic doesn't exist by definition, but there are also real reasons why compilers do not rise to meet the naive expectations that are nevertheless often repeated as a kind of software engineering meme. Some people think memes are true and maybe some wishful thinking plays a roll (I get it, it would be great if the compiler was magic), and will just think I'm talking sh*t when I give the short version, so here's a longer one.
There is way too much to cover, so for now I'll just concentrate on one thing, why are compilers not godly at code generation. They are pretty good nowadays, but their mythical status is undeserved.

A fairly fundamental problem (for both compilers and novice programmers, but programmers can learn) is that the cost model is wrong, so when it's tiling its internal representation with pieces of machine code, it's not modeling reality accurately enough.
As far as I know, every reasonable code generation technique, even advanced ones want the cost to be a scalar.
But it isn't a scalar, not in an accurate model of reality anyway, not since the end of what I'll call "simple architectures" (circularly defined as those architectures where the cost of an instruction is the number of cycles it takes and no other considerations exist).

What does reality look like? Let's look at the code below. It doesn't really matter what it actually does, I'm just going analyze the cost (for Haswell) to show a bit of what's involved.
ASM
.L3:
    mov rcx, rdi    ; free   0
    imul rcx, rax   ; p1     3
    imul rdx, rsi   ; p1     3
    add rcx, rdx    ; p0156  1
    mul rsi         ; p1 p6  3/4 (4 for the upper half)
    add rdx, rcx    ; p0156  1
    shrd rax, rdx, 2; p1     3
    sar rdx, 2      ; p06    1
    add rsi, 1      ; p0156  1
    mov rcx, rsi    ; free   0
    adc rdi, 0      ; 2p0156 2
    xor rcx, 10000000; p01256 1
    or  rcx, rdi    ; \ fused,
    jnz .L3         ; / p6   1
This is a fairly interesting loop because it has a non-trivial loop carried dependency, which I have drawn here (two iterations shown, arrows are in the direction of the dependency, data flow is from bottom to top "against" the arrows). Just adding the latencies on the critical path (imul3, add3, add4, shrd2) or (mul2, add4, shrd2), either way gives 8, but it actually costs 9 cycles per iteration: imul3 and mul2 cannot be executed in the same cycle (both need p1), one of them has to wait a cycle and either way that holds everything up by a cycle.
There is a bunch of other code in this loop, but it "fits in the gaps". In general, you do have to care about the other code, especially in typical throughput-limited loops.

This is tricky enough by hand, now imagine implementing that in a compiler. What does the model even look like? Certainly not like a "just combine everything into one scalar"-cost that you can simply add, that's not even close. A vector "pressure per port" seems like an obvious model for loops with only a trivial loop-carried dependency, but even that is really tricky: many instructions can dynamically go to a port with a low pressure (eg p0156 means it can go to ports 0, 1, 5 or 6), modeling that as 1/4 pressure to each port only works if there are no instructions that must go to a certain port, but there usually are. You could distribute those instructions across the ports like a CPU would, but only if you know the context, so now you have a cost that does not just depend on the tile that you're looking at but also all other tiles (which you may not even have chosen yet!).

Reality is a mess, and compilers just don't model it (though they could). Not out of laziness, implementing a realistic model means you can't use the old DP tiling algorithm (which for DAGs isn't optimal anyway, but with some tweaks you can get close) because you don't have optimal substructure: the cost of a sub-tiling depends on the context in which it appears, the best tiling may not consist of locally-optimal sub-tilings.

To give a fairly abstract example of that, suppose you have parts A and B, part A can be tiled either such that it has 2 µops going to port 1, or such that it has 1 µops to port 0 and 3 to port 5. Which is better? It depends: if part B needs to send 2 µops to port 1 then combining it with the "locally better" first option gives port 1 a pressure of 4, which (if there is no other context and we're talking about throughput) is worse than combining it with the second option where the worst port pressure would be 3. With more context, the decision can flip again.

Often heard: "Compilers know better what is fast is what isn't than you do."
Well you can fix that, start here.

An other big problem is that a lot is set in stone before code generation. Whether a certain optimization should be applied depends on how it actually works out during code generation, but compilers are too linear for that - they optimize their IR, then do code generation. If they make a choice that works out badly, too bad.
Ideally (from a quality perspective) any choice should have its consequences computed by running all possible versions all the way through code generation. Choosing based on anything else is essentially a guess, though there are "obvious cases". But it would be way too slow, since many of the decisions stack to an exponential number of versions that would be tried. Not all decisions affect each other of course, but it's bad enough.
It is really the opposite of the workflow of a human, if I may be so bold as to speak for an entire species, we're all about trying different approaches and seeing what works out.

The higher level problems are even worse, maybe more on that some other time..
GeneralRe: Why compilers aren't magic Pin
OriginalGriff26-Mar-17 6:05
mveOriginalGriff26-Mar-17 6:05 
GeneralRe: Why compilers aren't magic PinPopular
Sander Rossel26-Mar-17 11:10
professionalSander Rossel26-Mar-17 11:10 
GeneralRe: Why compilers aren't magic Pin
CDP180226-Mar-17 11:35
CDP180226-Mar-17 11:35 
GeneralRe: Why compilers aren't magic Pin
Nelek26-Mar-17 19:03
protectorNelek26-Mar-17 19:03 
GeneralRe: Why compilers aren't magic Pin
Gerardo Orozco27-Mar-17 9:08
Gerardo Orozco27-Mar-17 9:08 
GeneralRe: Why compilers aren't magic Pin
Sander Rossel27-Mar-17 10:00
professionalSander Rossel27-Mar-17 10:00 
GeneralRe: Why compilers aren't magic Pin
BillWoodruff27-Mar-17 1:46
professionalBillWoodruff27-Mar-17 1:46 
GeneralRe: Why compilers aren't magic Pin
Kirk 1038982127-Mar-17 3:39
Kirk 1038982127-Mar-17 3:39 
GeneralRe: Why compilers aren't magic Pin
BrainiacV27-Mar-17 5:54
BrainiacV27-Mar-17 5:54 
GeneralInternet Explorer, please forgive me... Pin
charlieg26-Mar-17 2:45
charlieg26-Mar-17 2:45 
GeneralRe: Internet Explorer, please forgive me... Pin
Ron Anders26-Mar-17 2:51
Ron Anders26-Mar-17 2:51 
GeneralRe: Internet Explorer, please forgive me... Pin
Sander Rossel26-Mar-17 11:11
professionalSander Rossel26-Mar-17 11:11 
GeneralRe: Internet Explorer, please forgive me... Pin
Mycroft Holmes26-Mar-17 14:21
professionalMycroft Holmes26-Mar-17 14:21 
GeneralRe: Internet Explorer, please forgive me... Pin
Mark_Wallace27-Mar-17 0:00
Mark_Wallace27-Mar-17 0:00 
GeneralRe: Internet Explorer, please forgive me... Pin
User 991608027-Mar-17 4:31
professionalUser 991608027-Mar-17 4:31 
GeneralRe: Internet Explorer, please forgive me... Pin
  Forogar  26-Mar-17 3:15
professional  Forogar  26-Mar-17 3:15 
GeneralRe: Internet Explorer, please forgive me... Pin
Kornfeld Eliyahu Peter26-Mar-17 3:19
professionalKornfeld Eliyahu Peter26-Mar-17 3:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.