[PATCH] D150524: [cmake] Disable GCC lifetime DSE

Xi Ruoyao via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun May 14 05:05:26 PDT 2023


xry111 created this revision.
Herald added subscribers: ekilmer, PiotrZSL, carlosgalvezp.
Herald added a project: All.
xry111 requested review of this revision.
Herald added projects: LLVM, clang-tools-extra.
Herald added subscribers: cfe-commits, llvm-commits.

LLVM data structures like llvm::User and llvm::MDNode rely on
the value of object storage persisting beyond the lifetime of the
object (#24952). This is not standard compliant and causes a runtime
crash if LLVM is built with GCC and LTO enabled (#57740). Until
these issues are fixed, we need to disable dead store eliminations
eliminations based on object lifetime.

The previous version (D150505 <https://reviews.llvm.org/D150505>) also exploited an issue in the
clang-tidy test suite.  The test `trivially-destructible.cpp` is filtered
into a temporary file, and when clang-tidy operates on the file, the
`compile_commands.json` file will be read and the compiler options
in it will be used.  If LLVM is not built with Clang, some of the options
may be unsupported, causing a test failure.  Fix it by adding "-p %s"
into the test command to force clang-tidy to search the compiler
option DB file in the source directory, where there is none.

Bug: https://github.com/llvm/llvm-project/issues/24952
Bug: https://github.com/llvm/llvm-project/issues/57740
Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106943


https://reviews.llvm.org/D150524

Files:
  clang-tools-extra/test/clang-tidy/checkers/performance/trivially-destructible.cpp
  llvm/cmake/modules/HandleLLVMOptions.cmake


Index: clang-tools-extra/test/clang-tidy/checkers/performance/trivially-destructible.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/performance/trivially-destructible.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/performance/trivially-destructible.cpp
@@ -1,7 +1,14 @@
 // RUN: %check_clang_tidy %s performance-trivially-destructible %t
 // RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
-// RUN: clang-tidy %t.cpp -checks='-*,performance-trivially-destructible' -fix
-// RUN: clang-tidy %t.cpp -checks='-*,performance-trivially-destructible' -warnings-as-errors='-*,performance-trivially-destructible'
+// RUN: clang-tidy -p %S %t.cpp -checks='-*,performance-trivially-destructible' -fix
+// RUN: clang-tidy -p %S %t.cpp -checks='-*,performance-trivially-destructible' -warnings-as-errors='-*,performance-trivially-destructible'
+
+// The "-p %S" in the clang-tidy command lines above is used for overriding
+// the detection of the `compile_commands.json` file.  As %t.cpp is in the
+// build directory, by default the `compile_commands.json` file created by
+// cmake for LLVM itself will be used.  But the file may contains options
+// not recognized by Clang if the LLVM project is built with another
+// compiler.
 
 struct TriviallyDestructible1 {
   int a;
Index: llvm/cmake/modules/HandleLLVMOptions.cmake
===================================================================
--- llvm/cmake/modules/HandleLLVMOptions.cmake
+++ llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -594,6 +594,16 @@
   add_flag_if_supported("-Werror=unguarded-availability-new" WERROR_UNGUARDED_AVAILABILITY_NEW)
 endif( LLVM_COMPILER_IS_GCC_COMPATIBLE OR CMAKE_CXX_COMPILER_ID MATCHES "XL" )
 
+if ( LLVM_COMPILER_IS_GCC_COMPATIBLE )
+  # LLVM data structures like llvm::User and llvm::MDNode rely on
+  # the value of object storage persisting beyond the lifetime of the
+  # object (#24952).  This is not standard compliant and causes a runtime
+  # crash if LLVM is built with GCC and LTO enabled (#57740).  Until
+  # these bugs are fixed, we need to disable dead store eliminations
+  # based on object lifetime.
+  add_flag_if_supported("-fno-lifetime-dse" CMAKE_CXX_FLAGS)
+endif ( LLVM_COMPILER_IS_GCC_COMPATIBLE )
+
 # Modules enablement for GCC-compatible compilers:
 if ( LLVM_COMPILER_IS_GCC_COMPATIBLE AND LLVM_ENABLE_MODULES )
   set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150524.521991.patch
Type: text/x-patch
Size: 2452 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230514/70084645/attachment.bin>


More information about the cfe-commits mailing list