-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reapply "[compiler-rt][ctx_instr] Add
ctx_profile
component" (#89625)
This reverts commit 8b2ba6a. The uild errors (see below) were likely due to the same issue PR #88074 fixed. Addressed by following that PR. https://lab.llvm.org/buildbot/#/builders/165/builds/52789 https://lab.llvm.org/buildbot/#/builders/91/builds/25273
- Loading branch information
Showing
10 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
add_compiler_rt_component(ctx_profile) | ||
|
||
set(CTX_PROFILE_SOURCES | ||
CtxInstrProfiling.cpp | ||
) | ||
|
||
set(CTX_PROFILE_HEADERS | ||
CtxInstrProfiling.h | ||
) | ||
|
||
include_directories(..) | ||
include_directories(../../include) | ||
|
||
# We don't use the C++ Standard Library here, so avoid including it by mistake. | ||
append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ EXTRA_FLAGS) | ||
|
||
add_compiler_rt_runtime(clang_rt.ctx_profile | ||
STATIC | ||
ARCHS ${CTX_PROFILE_SUPPORTED_ARCH} | ||
OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc | ||
CFLAGS ${EXTRA_FLAGS} | ||
SOURCES ${CTX_PROFILE_SOURCES} | ||
ADDITIONAL_HEADERS ${CTX_PROFILE_HEADERS} | ||
PARENT_TARGET ctx_profile) | ||
|
||
if(COMPILER_RT_INCLUDE_TESTS) | ||
add_subdirectory(tests) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//===- CtxInstrProfiling.cpp - contextual instrumented PGO ----------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "CtxInstrProfiling.h" | ||
#include "sanitizer_common/sanitizer_allocator_internal.h" | ||
#include "sanitizer_common/sanitizer_common.h" | ||
#include "sanitizer_common/sanitizer_dense_map.h" | ||
#include "sanitizer_common/sanitizer_mutex.h" | ||
#include "sanitizer_common/sanitizer_placement_new.h" | ||
#include "sanitizer_common/sanitizer_thread_safety.h" | ||
|
||
#include <assert.h> | ||
|
||
using namespace __ctx_profile; | ||
|
||
// FIXME(mtrofin): use malloc / mmap instead of sanitizer common APIs to reduce | ||
// the dependency on the latter. | ||
Arena *Arena::allocateNewArena(size_t Size, Arena *Prev) { | ||
assert(!Prev || Prev->Next == nullptr); | ||
Arena *NewArena = | ||
new (__sanitizer::InternalAlloc(Size + sizeof(Arena))) Arena(Size); | ||
if (Prev) | ||
Prev->Next = NewArena; | ||
return NewArena; | ||
} | ||
|
||
void Arena::freeArenaList(Arena *&A) { | ||
assert(A); | ||
for (auto *I = A; I != nullptr;) { | ||
auto *Current = I; | ||
I = I->Next; | ||
__sanitizer::InternalFree(Current); | ||
} | ||
A = nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/*===- CtxInstrProfiling.h- Contextual instrumentation-based PGO ---------===*\ | ||
|* | ||
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
|* See https://llvm.org/LICENSE.txt for license information. | ||
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|* | ||
\*===----------------------------------------------------------------------===*/ | ||
|
||
#ifndef CTX_PROFILE_CTXINSTRPROFILING_H_ | ||
#define CTX_PROFILE_CTXINSTRPROFILING_H_ | ||
|
||
#include <sanitizer/common_interface_defs.h> | ||
|
||
namespace __ctx_profile { | ||
|
||
/// Arena (bump allocator) forming a linked list. Intentionally not thread safe. | ||
/// Allocation and de-allocation happen using sanitizer APIs. We make that | ||
/// explicit. | ||
class Arena final { | ||
public: | ||
// When allocating a new Arena, optionally specify an existing one to append | ||
// to, assumed to be the last in the Arena list. We only need to support | ||
// appending to the arena list. | ||
static Arena *allocateNewArena(size_t Size, Arena *Prev = nullptr); | ||
static void freeArenaList(Arena *&A); | ||
|
||
uint64_t size() const { return Size; } | ||
|
||
// Allocate S bytes or return nullptr if we don't have that many available. | ||
char *tryBumpAllocate(size_t S) { | ||
if (Pos + S > Size) | ||
return nullptr; | ||
Pos += S; | ||
return start() + (Pos - S); | ||
} | ||
|
||
Arena *next() const { return Next; } | ||
|
||
// the beginning of allocatable memory. | ||
const char *start() const { return const_cast<Arena *>(this)->start(); } | ||
const char *pos() const { return start() + Pos; } | ||
|
||
private: | ||
explicit Arena(uint32_t Size) : Size(Size) {} | ||
~Arena() = delete; | ||
|
||
char *start() { return reinterpret_cast<char *>(&this[1]); } | ||
|
||
Arena *Next = nullptr; | ||
uint64_t Pos = 0; | ||
const uint64_t Size; | ||
}; | ||
|
||
} // namespace __ctx_profile | ||
#endif // CTX_PROFILE_CTXINSTRPROFILING_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
include(CheckCXXCompilerFlag) | ||
include(CompilerRTCompile) | ||
include(CompilerRTLink) | ||
|
||
set(CTX_PROFILE_UNITTEST_CFLAGS | ||
${COMPILER_RT_UNITTEST_CFLAGS} | ||
${COMPILER_RT_GTEST_CFLAGS} | ||
${COMPILER_RT_GMOCK_CFLAGS} | ||
${SANITIZER_TEST_CXX_CFLAGS} | ||
-I${COMPILER_RT_SOURCE_DIR}/lib/ | ||
-DSANITIZER_COMMON_NO_REDEFINE_BUILTINS | ||
-O2 | ||
-g | ||
-fno-rtti | ||
-Wno-pedantic | ||
-fno-omit-frame-pointer) | ||
|
||
# Suppress warnings for gmock variadic macros for clang and gcc respectively. | ||
append_list_if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG -Wno-gnu-zero-variadic-macro-arguments CTX_PROFILE_UNITTEST_CFLAGS) | ||
append_list_if(COMPILER_RT_HAS_WVARIADIC_MACROS_FLAG -Wno-variadic-macros CTX_PROFILE_UNITTEST_CFLAGS) | ||
|
||
file(GLOB CTX_PROFILE_HEADERS ../*.h) | ||
|
||
set(CTX_PROFILE_SOURCES | ||
../CtxInstrProfiling.cpp) | ||
|
||
set(CTX_PROFILE_UNITTESTS | ||
CtxInstrProfilingTest.cpp | ||
driver.cpp) | ||
|
||
include_directories(../../../include) | ||
|
||
set(CTX_PROFILE_UNITTEST_HEADERS | ||
${CTX_PROFILE_HEADERS}) | ||
|
||
set(CTX_PROFILE_UNITTEST_LINK_FLAGS | ||
${COMPILER_RT_UNITTEST_LINK_FLAGS}) | ||
|
||
list(APPEND CTX_PROFILE_UNITTEST_LINK_FLAGS -pthread) | ||
|
||
set(CTX_PROFILE_UNITTEST_DEPS) | ||
if (TARGET cxx-headers OR HAVE_LIBCXX) | ||
list(APPEND CTX_PROFILE_UNITTEST_DEPS cxx-headers) | ||
endif() | ||
|
||
set(CTX_PROFILE_UNITTEST_LINK_LIBRARIES | ||
${COMPILER_RT_UNWINDER_LINK_LIBS} | ||
${SANITIZER_TEST_CXX_LIBRARIES}) | ||
append_list_if(COMPILER_RT_HAS_LIBDL -ldl CTX_PROFILE_UNITTEST_LINK_LIBRARIES) | ||
|
||
macro (add_ctx_profile_tests_for_arch arch) | ||
set(CTX_PROFILE_TEST_RUNTIME_OBJECTS | ||
$<TARGET_OBJECTS:RTSanitizerCommon.${arch}> | ||
$<TARGET_OBJECTS:RTSanitizerCommonCoverage.${arch}> | ||
$<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}> | ||
$<TARGET_OBJECTS:RTSanitizerCommonSymbolizer.${arch}> | ||
$<TARGET_OBJECTS:RTSanitizerCommonSymbolizerInternal.${arch}> | ||
) | ||
set(CTX_PROFILE_TEST_RUNTIME RTCtxProfileTest.${arch}) | ||
add_library(${CTX_PROFILE_TEST_RUNTIME} STATIC ${CTX_PROFILE_TEST_RUNTIME_OBJECTS}) | ||
set_target_properties(${CTX_PROFILE_TEST_RUNTIME} PROPERTIES | ||
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} | ||
FOLDER "Compiler-RT Runtime tests") | ||
set(CTX_PROFILE_TEST_OBJECTS) | ||
generate_compiler_rt_tests(CTX_PROFILE_TEST_OBJECTS | ||
CtxProfileUnitTests "CtxProfile-${arch}-UnitTest" ${arch} | ||
RUNTIME ${CTX_PROFILE_TEST_RUNTIME} | ||
DEPS ${CTX_PROFILE_UNITTEST_DEPS} | ||
SOURCES ${CTX_PROFILE_UNITTESTS} ${CTX_PROFILE_SOURCES} ${COMPILER_RT_GTEST_SOURCE} | ||
COMPILE_DEPS ${CTX_PROFILE_UNITTEST_HEADERS} | ||
CFLAGS ${CTX_PROFILE_UNITTEST_CFLAGS} | ||
LINK_FLAGS ${CTX_PROFILE_UNITTEST_LINK_FLAGS} ${CTX_PROFILE_UNITTEST_LINK_LIBRARIES}) | ||
endmacro() | ||
|
||
add_custom_target(CtxProfileUnitTests) | ||
set_target_properties(CtxProfileUnitTests PROPERTIES FOLDER "Compiler-RT Tests") | ||
if(COMPILER_RT_CAN_EXECUTE_TESTS AND COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST CTX_PROFILE_SUPPORTED_ARCH) | ||
# CtxProfile unit tests are only run on the host machine. | ||
foreach(arch ${COMPILER_RT_DEFAULT_TARGET_ARCH}) | ||
add_ctx_profile_tests_for_arch(${arch}) | ||
endforeach() | ||
endif() |
22 changes: 22 additions & 0 deletions
22
compiler-rt/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "../CtxInstrProfiling.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace __ctx_profile; | ||
|
||
TEST(ArenaTest, Basic) { | ||
Arena *A = Arena::allocateNewArena(1024); | ||
EXPECT_EQ(A->size(), 1024U); | ||
EXPECT_EQ(A->next(), nullptr); | ||
|
||
auto *M1 = A->tryBumpAllocate(1020); | ||
EXPECT_NE(M1, nullptr); | ||
auto *M2 = A->tryBumpAllocate(4); | ||
EXPECT_NE(M2, nullptr); | ||
EXPECT_EQ(M1 + 1020, M2); | ||
EXPECT_EQ(A->tryBumpAllocate(1), nullptr); | ||
Arena *A2 = Arena::allocateNewArena(2024, A); | ||
EXPECT_EQ(A->next(), A2); | ||
EXPECT_EQ(A2->next(), nullptr); | ||
Arena::freeArenaList(A); | ||
EXPECT_EQ(A, nullptr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//===-- driver.cpp ----------------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "gtest/gtest.h" | ||
|
||
int main(int argc, char **argv) { | ||
testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
a3e7a12
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems to break cmake with error
Objects of target "RTSanitizerCommon.x86_64" referenced but no such target exists
.for example with cmake variables
a3e7a12
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
talked to mtrofin, this is working as intended and for the chromium build the cmake flag should be turned off