12 ms·
Likewise no sane compiler would optimize memset. Unluckily we don't have sane compilers
by rurban 5y ago
Likewise no sane compiler would optimize memset. Unluckily we don't have sane compilers
- rowanG077 5y agoWhy not? This makes little sense to me. Why shouldn't a compiler optimize it?
- 1ris 5y agoBecause, when a programmer write a call to memset, she does, if you, as a compiler, take her seriously at all, expects the memory to be set, and not it just being a mere suggestion 1). should added a separate function called it memsetOrDontIfYouThinkINeverReadThisAgain. Does this make sense? 1) Yes, I know there are rules and all that are precise and allow that.
- rowanG077 5y agoIf you take this stance most optimizations are of the table. Optimization are rewriting code in some way to ensure it runs faster. In that sense yes the entirety of a program "is a mere suggesting on how to compute the output". If you actually want to tell the CPU what to do you have to use ASM and an ISA that specs what exactly every instruction does and how it does it.
- 1ris 5y agoLet me paraphrase this differently, on a more pragmatic level. From the perspecitve of a compiler user, rather than a compiler author: How likely do you think is it that a program that contains memset and has this optimisation applied how diverts from the indented behaviour and now has a serious flaw in it? The answer to this question is the answer to your question "Why not?". When people write C, they often do so for specific domains. Crypto, embedded, device drivers. They choose C for these domains because they think, and they are taught that C is some kind of macro assembler, because in these domains they care deeply about the "how". If they wouldn't, they would write java or any current top 20 programming languages. So almost all optimization off looks ok. Reasonable compared to this, anyhow.
- rowanG077 5y agoI would assume it doesn't divert because the user cannot tell from the output of the program if a memset is optimized away. If you want the memset itself to be observable you need to use volatile or memset_s. Else you are not describing your intent properly If people truly still think C is a macro assembler then that's the problem. Besides if you care deeply about how modern high performance architectures won't cut it anyway.
- Gibbon1 5y agoI think the thing is users of C compilers generally care about raw performance much much less than compiler maintainers who are universally using C++. Compiler writers are stuck in a trap. C++ compilation model is broken. Which makes compiling C++ programs very slow. Which means compiler writers care a lot about how fast compilers are. So they desperately add more optimizations to speed up the compiler program. But which adds more work to the process of compiling a program. Most of the other users, especially C programmers don't care about speed nearly as much. Notably because while the compilation model for C is also broken, it's not nearly so. So their programs compile in a few seconds, not minutes to hours.
- MauranKilom 5y agoI submit that this viewpoint is very mistaken. If it were true, "users of C compilers" would just compile with optimizations off and there would not be any problem. Evidently, "most of the other users" do care about performance. Unsurprisingly, I might add.
- Gibbon1 5y agoIf I am mistaken why do most programmers prefer slower to vastly slower languages such as JavaScript, Java, C#, Go, Python, PHP, or Lua? If they really thought that speed was the most important thing wouldn't that abandon those languages for C++. Explain yourself.
- 5y ago
- overgard 5y ago> the entirety of a program "is a mere suggesting on how to compute the output". Maybe people using Java or various interpretted languages are fine with that, but most C programmers want their code to be a very close mapping to what they specified.
- umanwizard 5y agoThen they can compile with -O0 and get basically that. What’s the problem?
- overgard 5y agoThe problem is, I want optimizations.. that don't break things. There's -O1 or -O3 but not -Ononewbugs. I don't expect perfection but changing the meaning of memset or collapsing atomic operations is like "settle down".
- innocenat 5y agoBut -O3 is suppose to do that. The problem is that there will almost always be bug. The flag that may allow things to break is -Ofast.
- gpderetta 5y agoThe set of all the optimizations that do not break any program is very likely empty. So you are happy to break other people programs as long as yours keep working?
- overgard 5y agoI'm pretty happy to leave the optimizations off. Writing the correct data structures matters a lot more than a clever compiler collapsing two commands into one.
- umanwizard 5y agoEvery optimization changes the observable behavior of a program in some way. How is the compiler supposed to know which changes you deem acceptable? Eliminating dead stores (including calls to memcpy whose result is never read) seems like one of the most basic and least objectionable optimizations I can imagine. So, if you’re against eliminating dead stores, what optimizations _are_ you okay with?
- aaronmdjones 5y ago> If you actually want to tell the CPU what to do you have to use ASM and an ISA that specs what exactly every instruction does and how it does it. Even that isn't enough, unless you go with a CPU that doesn't have any branch prediction, speculative execution, or out-of-order execution. I'm not aware of any such processors.
- rowanG077 5y agoThat's exactly what I meant with "how". If an ISA has branch prediction, speculative executions or out-of-order execution this definitely is included on how an ISA handles it's instructions.
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- aqrit 5y agohttps://media.ccc.de/v/35c3-9788-memsad https://media.ccc.de/v/35c3-9788-memsad
- halayli 5y agoCompilers are sane and written by very smart people. They do have bugs just like any other software. In my 20+ yrs of coding, I can count the number of bugs I've encountered on one hand. memset tend to be optimized out in the dead code elimination pass and highly relies on SSA. Compilers adhere to the abstract machine specs. If you want memset not to be optimized out then use memset_s because the spec explicitly doesn't allow that. if an optimization relies on a UB and surprised the programmer then the programmer was also relying on a UB in the first place.
- secondcoming 5y agoIsn't memset different in that it's an external function that compiler writers have chosen to intercept and perform themselves? Why is saying 'use memset_s' acceptable when they could have stopped treating memset as a special case?
- chrisseaton 5y ago> Isn't memset different in that it's an external function that compiler writers have chosen to intercept and perform themselves? The semantics of memset and memset_s are defined by a standard. They're not unknown functions that could do anything, and they're breaking a rule by treating them differently, like you're suggesting they are.
- Someone 5y agoIf you include a standard header (using <> brackets) and call a function that the standard says get declared when doing that, the compiler is allowed to assume you want the standard behavior, and inline that/replace it by a specialized version/etc. Optimizing memset, in particular, can reap huge benefits, for example for sizes of 4 or 8 bytes in tight loops or for zeroing cache line sized blocks.
- halayli 5y agomemset is not a special case. Compiler designers are very much against special casing, and rightfully so. Compilers do have an intrinsic equivalent but that has nothing to do with whether it gets optimized out or kept. Following SSA rules, if you are writing to a non-volatile memory and not reading from it for the remaining duration of its lifetime then it's dead code. use memset_s was a suggestion if your goal is to scrub memory because it guarantees the write will happen. The guarantee comes from the C standard: > Unlike memset, any call to the memset_s function shall be evaluated strictly according to the rules of the abstract machine as described in (5.1.2.3). That is, any call to the memset_s function shall assume that the memory indicated by s and n may be accessible in the future and thus must contain the values indicated by c Which essentially means treat the memory as-if it's volatile.