Page MenuHomeFreeBSD

clang: Build with -fno-strict-aliasing when using GCC
ClosedPublic

Authored by jrtc27 on Aug 13 2021, 5:57 PM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Jun 24, 5:57 PM
Unknown Object (File)
Feb 19 2024, 11:32 PM
Unknown Object (File)
Feb 13 2024, 8:40 PM
Unknown Object (File)
Dec 22 2023, 10:27 PM
Unknown Object (File)
Dec 16 2023, 4:52 AM
Unknown Object (File)
Dec 12 2023, 8:17 AM
Unknown Object (File)
Nov 29 2023, 11:37 PM
Unknown Object (File)
Oct 4 2023, 12:25 PM
Subscribers

Details

Summary

Somewhat ironically, there are strict aliasing violations in Clang,
which can result in the following assertion failure:

Assertion `*(NamedDecl **)&Data == ND && "PointerUnion mangles the NamedDecl pointer!"' failed.

Upstream's clang/CMakeLists.txt specifically (not LLVM as a whole)
passes -fno-strict-aliasing if the compiler is not Clang, and this fixes
the above issue.

This was seen when cross-building from Linux using a bootstrap
compiler, but likely also affects worlds built with a new enough
external GCC toolchain.

MFC after: 1 week

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Are the strict aliasing violations in clang itself, or in llvm in general? If so, this flag addition should be added to llvm.build.mk instead. But it would maybe pessimize quite of lot of code...

In D31533#710854, @dim wrote:

Are the strict aliasing violations in clang itself, or in llvm in general? If so, this flag addition should be added to llvm.build.mk instead. But it would maybe pessimize quite of lot of code...

This one is in Clang's StoredDeclsList; full assertion is:

c++: /local/scratch/jrtc4/freebsd/src/contrib/llvm-project/clang/include/clang/AST/DeclContextInternals.h:102: void clang::StoredDeclsList::setOnlyValue(clang::NamedDecl*): Assertion `*(NamedDecl **)&Data == ND && "PointerUnion mangles the NamedDecl pointer!"' failed.

but got trimmed so the commit wasn't a stupid line width. Upstream only does -fno-strict-aliasing for clang itself (https://github.com/llvm/llvm-project/blob/a0c42ca56c2ea14c53f7bebcd9b53c4dfe4152e2/clang/CMakeLists.txt#L375), hence why I put it here not llvm.build.mk.

Sure, that's OK then. Doing some spelunking shows that -fno-strict-aliasing got conditionalized for gcc here:

https://github.com/llvm/llvm-project/commit/c5635a6af7c64 (2015-08-18, "We shouldn't need to pass -fno-strict-aliasing when building clang with clang")

and the original commit that added the flag is much older even:

https://github.com/llvm/llvm-project/commit/40fee6313df68 (2010-06-08, "Globally disable -fno-strict-aliasing, for reasons given in the comment")

where that comment is:

# Disable -fstrict-aliasing. Darwin disables it by default (and LLVM doesn't
# work with it enabled with GCC), Clang/llvm-gc don't support it yet, and newer
# GCC's have false positive warnings with it on Linux (which prove a pain to
# fix). For example:
#   http://gcc.gnu.org/PR41874
#   http://gcc.gnu.org/PR41838
#
# We can revisit this when LLVM/Clang support it.
This revision is now accepted and ready to land.Aug 13 2021, 6:28 PM