Skip to content

Conversation

@tambry
Copy link
Contributor

@tambry tambry commented Sep 30, 2024

The profiling code requires GNU extensions as it uses functions such as getpagesize(), fdopen(), etc.

The problem manifests when the compiler is built to not default to the extensions mode, e.g. custom config with -std=c2x. CMake didn't support this scenario very well, but it's been fixed by CMP0128. Set the policy to NEW as we now conform to it.

The profiling code requires GNU extensions as it uses functions such as getpagesize(), fdopen(), etc.

The problem manifests when the compiler is built to not default to the extensions mode, e.g. custom config with -std=c2x.
CMake didn't support this scenario very well, but it's been fixed by CMP0128. Set the policy to NEW as we now conform to it.
@tambry tambry added cmake Build system in general and CMake in particular compiler-rt labels Sep 30, 2024
@tambry tambry requested a review from petrhosek September 30, 2024 19:17
@tambry tambry self-assigned this Sep 30, 2024
@llvmbot llvmbot added the PGO Profile Guided Optimizations label Sep 30, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 30, 2024

@llvm/pr-subscribers-pgo

Author: Raul Tambre (tambry)

Changes

The profiling code requires GNU extensions as it uses functions such as getpagesize(), fdopen(), etc.

The problem manifests when the compiler is built to not default to the extensions mode, e.g. custom config with -std=c2x. CMake didn't support this scenario very well, but it's been fixed by CMP0128. Set the policy to NEW as we now conform to it.


Full diff: https://github.com/llvm/llvm-project/pull/110555.diff

3 Files Affected:

  • (modified) compiler-rt/CMakeLists.txt (+5)
  • (modified) compiler-rt/cmake/Modules/AddCompilerRT.cmake (+7-2)
  • (modified) compiler-rt/lib/profile/CMakeLists.txt (+4-2)
diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index deb6994f481854..1c24b520da985d 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -12,6 +12,11 @@ endif()
 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
   NO_POLICY_SCOPE)
 
+# TODO(CMake 3.22): remove
+if(POLICY CMP0128)
+  cmake_policy(SET CMP0128 NEW)
+endif()
+
 # Check if compiler-rt is built as a standalone project.
 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR COMPILER_RT_STANDALONE_BUILD)
   project(CompilerRT C CXX ASM)
diff --git a/compiler-rt/cmake/Modules/AddCompilerRT.cmake b/compiler-rt/cmake/Modules/AddCompilerRT.cmake
index 6962b733733a6a..e3d81d241b1054 100644
--- a/compiler-rt/cmake/Modules/AddCompilerRT.cmake
+++ b/compiler-rt/cmake/Modules/AddCompilerRT.cmake
@@ -161,7 +161,8 @@ endmacro()
 #                         LINK_LIBS <linked libraries> (only for shared library)
 #                         OBJECT_LIBS <object libraries to use as sources>
 #                         PARENT_TARGET <convenience parent target>
-#                         ADDITIONAL_HEADERS <header files>)
+#                         ADDITIONAL_HEADERS <header files>
+#                         EXTENSIONS <boolean>)
 function(add_compiler_rt_runtime name type)
   if(NOT type MATCHES "^(OBJECT|STATIC|SHARED|MODULE)$")
     message(FATAL_ERROR
@@ -171,7 +172,7 @@ function(add_compiler_rt_runtime name type)
   cmake_parse_arguments(LIB
     ""
     "PARENT_TARGET"
-    "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;DEPS;LINK_LIBS;OBJECT_LIBS;ADDITIONAL_HEADERS"
+    "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;DEPS;LINK_LIBS;OBJECT_LIBS;ADDITIONAL_HEADERS;EXTENSIONS"
     ${ARGN})
   set(libnames)
   # Until we support this some other way, build compiler-rt runtime without LTO
@@ -435,6 +436,10 @@ function(add_compiler_rt_runtime name type)
     if(type STREQUAL "SHARED")
       rt_externalize_debuginfo(${libname})
     endif()
+
+    if(DEFINED LIB_EXTENSIONS)
+      set_target_properties(${libname} PROPERTIES C_EXTENSIONS ${LIB_EXTENSIONS})
+    endif()
   endforeach()
   if(LIB_PARENT_TARGET)
     add_dependencies(${LIB_PARENT_TARGET} ${libnames})
diff --git a/compiler-rt/lib/profile/CMakeLists.txt b/compiler-rt/lib/profile/CMakeLists.txt
index ef23492514898b..26178412967201 100644
--- a/compiler-rt/lib/profile/CMakeLists.txt
+++ b/compiler-rt/lib/profile/CMakeLists.txt
@@ -138,7 +138,8 @@ if(APPLE)
     CFLAGS ${EXTRA_FLAGS}
     SOURCES ${PROFILE_SOURCES}
     ADDITIONAL_HEADERS ${PROFILE_HEADERS}
-    PARENT_TARGET profile)
+    PARENT_TARGET profile
+    EXTENSIONS ON)
 else()
   add_compiler_rt_runtime(clang_rt.profile
     STATIC
@@ -146,5 +147,6 @@ else()
     CFLAGS ${EXTRA_FLAGS}
     SOURCES ${PROFILE_SOURCES}
     ADDITIONAL_HEADERS ${PROFILE_HEADERS}
-    PARENT_TARGET profile)
+    PARENT_TARGET profile
+    EXTENSIONS ON)
 endif()

@tambry tambry requested a review from mstorsjo October 14, 2024 09:53
Copy link
Member

@mstorsjo mstorsjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens here, if running on a version of CMake that doesn't support the new policy and then probably doesn't support the <LANG>_EXTENSIONS target property?

@tambry
Copy link
Contributor Author

tambry commented Oct 14, 2024

  1. C_EXTENSIONS has been supported since CMake 3.1. LLVM requires 3.20.
  2. If CMP0128 isn't supported then the behaviour remains the same. Compilers with extensions off by default won't have them enabled and won't be able to compile the code. No other relevant changes for LLVM.

CMP0128 is basically a bugfix (I implemented it).
But since there's code in the wild depending on the bugs it's a policy. For example some projects came to rely on <LANG>_EXTENSIONS=OFF actually not disabling extensions in some cases.

@mstorsjo
Copy link
Member

Ah, I see - thanks for the additional explanations!

Copy link
Member

@mstorsjo mstorsjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these changes look good.

(Do you have commit access, or do you need someone to merge it for you?)

@tambry tambry merged commit 9cc6d6e into llvm:main Oct 14, 2024
@tambry tambry deleted the compiler-rt_profile_gnu_ext branch October 14, 2024 12:07
@tambry
Copy link
Contributor Author

tambry commented Oct 14, 2024

Thanks for the quick review!
Not contributing too often anymore, but enough to maintain commit access. 🙂

DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
The profiling code requires GNU extensions as it uses functions such as getpagesize(), fdopen(), etc.

The problem manifests when the compiler is built to not default to the extensions mode, e.g. custom config with -std=c2x. CMake didn't support this scenario very well, but it's been fixed by CMP0128. Set the policy to NEW as we now conform to it.
tambry added a commit to tambry/llvm-project that referenced this pull request Oct 14, 2025
…sions

GNU extensions are a bit of a hammer approach to enabling access to POSIX extensions.
Instead we can define _DEFAULT_SOURCE ourselves, which is what the extensions mechanism does.

See: llvm#110555
tambry added a commit to tambry/llvm-project that referenced this pull request Oct 21, 2025
…sions

GNU extensions are a bit of a hammer approach to enabling access to POSIX extensions.
Instead we can define _DEFAULT_SOURCE ourselves where necessary, which is what the extensions
mechanism does.

See: llvm#110555
tambry added a commit to tambry/llvm-project that referenced this pull request Oct 21, 2025
…sions

GNU extensions are a bit of a hammer approach to enabling access to POSIX extensions.
Instead we can define _DEFAULT_SOURCE ourselves where necessary, which is what the extensions
mechanism does.

See: llvm#110555
tambry added a commit that referenced this pull request Oct 21, 2025
…sions (#163377)

GNU extensions are a bit of a hammer approach to enabling access to POSIX extensions.
Instead we can define _DEFAULT_SOURCE ourselves where necessary, which is what the extensions
mechanism does.

See: #110555
tambry added a commit to tambry/llvm-project that referenced this pull request Oct 21, 2025
…sions

GNU extensions are a bit of a hammer approach to enabling access to POSIX extensions.
Instead we can define _DEFAULT_SOURCE ourselves, which is what the extensions mechanism does.

See: llvm#110555
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
…sions (llvm#163377)

GNU extensions are a bit of a hammer approach to enabling access to POSIX extensions.
Instead we can define _DEFAULT_SOURCE ourselves where necessary, which is what the extensions
mechanism does.

See: llvm#110555
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
…sions (llvm#163377)

GNU extensions are a bit of a hammer approach to enabling access to POSIX extensions.
Instead we can define _DEFAULT_SOURCE ourselves where necessary, which is what the extensions
mechanism does.

See: llvm#110555
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cmake Build system in general and CMake in particular compiler-rt PGO Profile Guided Optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants