Skip to content

Commit

Permalink
workarounds for gcc optimizer misbuilds
Browse files Browse the repository at this point in the history
See also #440, #464, #486
  • Loading branch information
biojppm committed Jan 16, 2025
1 parent d7cc5cf commit 4b88ef9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/c4/yml/parse_engine.def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ inline bool _is_doc_token(csubstr s) noexcept
// The current version does not suffer this problem, but it may
// appear again.
//
//
// UPDATE. The problem appeared again in gcc12 and gcc13 with -Os
// (but not any other optimization level, nor any other compiler
// or version), because the assignment to s is being hoisted out
// of the loop which calls this function. Then the length doesn't
// enter the s.len >= 3 when it should. Adding a
// C4_DONT_OPTIMIZE(var) makes the problem go away.
//
if(s.len >= 3)
{
switch(s.str[0])
Expand Down Expand Up @@ -1938,6 +1946,9 @@ typename ParseEngine<EventHandler>::ScannedScalar ParseEngine<EventHandler>::_sc
while( ! _finished_file())
{
const csubstr line = m_evt_handler->m_curr->line_contents.rem;
#if defined(__GNUC__) && __GNUC__ == 11
C4_DONT_OPTIMIZE(line); // prevent erroneous hoist of the assignment out of the loop
#endif
bool line_is_blank = true;
_c4dbgpf("scanning double quoted scalar @ line[{}]: line='{}'", m_evt_handler->m_curr->pos.line, line);
for(size_t i = 0; i < line.len; ++i)
Expand Down Expand Up @@ -2087,6 +2098,9 @@ void ParseEngine<EventHandler>::_scan_block(ScannedBlock *C4_RESTRICT sb, size_t
{
// peek next line, but do not advance immediately
lc.reset_with_next_line(m_buf, m_evt_handler->m_curr->pos.offset);
#if defined(__GNUC__) && (__GNUC__ == 12 || __GNUC__ == 13)
C4_DONT_OPTIMIZE(lc.rem);
#endif
_c4dbgpf("blck: peeking at [{}]~~~{}~~~", lc.stripped.len, lc.stripped);
// evaluate termination conditions
if(indentation != npos)
Expand Down
3 changes: 3 additions & 0 deletions src/c4/yml/parser_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ struct LineContents
++e;
RYML_ASSERT(e >= offset);
const substr stripped_ = buf.range(offset, e);
#if defined(__GNUC__) && __GNUC__ == 11
C4_DONT_OPTIMIZE(stripped_);
#endif
// advance pos to include the first line ending
if(e < buf.len && buf.str[e] == '\r')
++e;
Expand Down

0 comments on commit 4b88ef9

Please sign in to comment.