llvm / llvm/llvm-project

The issue when using clang 21 with the `-O2` compilation flag

Open
#165,847 6 comments 0 reactions 0 assignees View on GitHub
needs-reduction
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Hello, I have a piece of code that, when compiled with clang 21 and the `-O2` flag, produces an executable that crashes with a coredump at runtime.

The problematic compilation command is:
```
clang++ -O2 main.cpp
```

However, if I omit the `-O2` flag, or use an older version of Clang or GCC to compile, the resulting executable runs without any issues. For example:
```
# Without `-O2`
clang++ main.cpp

# Using GCC
g++ main.cpp
g++ -O2 main.cpp

# Using an older version of Clang
/usr/lib/llvm20/bin/clang++ -O2 main.cpp
```

[main.cpp](https://github.com/user-attachments/files/23257372/main.cpp)
[cedarpp.h](https://github.com/DevO2012/cedar/blob/master/cedarpp.h)

Contributor guide

Open the contributing guide

Research direction

Start by compiling the attached main.cpp with clang++ -O2 and inspect the resulting coredump. Compare the behavior with clang++ without -O2, /usr/lib/llvm20/bin/clang++ -O2, and g++ -O2, using cedarpp.h as referenced. Done means confirming the regression and isolating its cause sufficiently for the compiler team to act.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.