https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102150
Bug ID: 102150 Summary: Speculative execution of inline assembly causes divide error Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: jeremy-gcc-bugzilla at sawicki dot us Target Milestone: --- Created attachment 51391 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51391&action=edit Reproducible test case The attached test case uses inline assembly to wrap the x86_64 DIV instruction. GCC speculatively executes the inline assembly on inputs that the source program does not, resulting in a divide error. The GCC documentation says that non-volatile inline assembly may be discarded or moved out of loops. It is not obvious whether speculative execution is also permitted. I asked on gcc-help and was asked to file a report. A related report points out that many projects currently wrap the DIV instruction without using volatile: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82677 Another related report considers the similar issue of whether pure/const functions must be non-trapping for inputs they don't actually receive: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93491 If it is determined that volatile is required, it would helpful to clarify in the documentation that speculative execution may occur without volatile: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Volatile gcc version 11.2.0 (GCC) Target: x86_64-pc-linux-gnu Configured with: /home/jeremys/gcc-11.2.0/configure --prefix=/home/jeremys/gcc-11.2.0-install --disable-multilib Command line: g++ -O3 -o divasm divasm.cpp No compiler errors/warnings are produced When executed, a divide error occurs