How do I make an infinite empty loop that won't be optimized away?

前端 未结 13 1422
后悔当初
后悔当初 2020-12-23 13:16

The C11 standard appears to imply that iteration statements with constant controlling expressions should not be optimized out. I\'m taking my advice from this answer, which

13条回答
  •  攒了一身酷
    2020-12-23 13:22

    Other answers already covered ways to make Clang emit the infinite loop, with inline assembly language or other side effects. I just want to confirm that this is indeed a compiler bug. Specifically, it's a long-standing LLVM bug - it applies the C++ concept of "all loops without side-effects must terminate" to languages where it shouldn't, such as C.

    For example, the Rust programming language also allows infinite loops and uses LLVM as a backend, and it has this same issue.

    In the short term, it appears that LLVM will continue to assume that "all loops without side-effects must terminate". For any language that allows infinite loops, LLVM expects the front-end to insert llvm.sideeffect opcodes into such loops. This is what Rust is planning to do, so Clang (when compiling C code) will probably have to do that too.

提交回复
热议问题