diff options
author | Pavel Samolysov <samolisov@gmail.com> | 2024-05-31 05:52:00 +0300 |
---|---|---|
committer | Pavel Samolysov <samolisov@gmail.com> | 2024-05-31 05:53:38 +0300 |
commit | 5d5ead1dbd9aac486aada3acf81cc09464ab7bae (patch) | |
tree | 07f6bae2c441619d2e0add9a66e22a5134102958 | |
parent | ac467402f0d688c8295bbca0f03161516b6982df (diff) | |
download | llvm-users/psamolysov/pgo-generate-version-when-instrumented.zip llvm-users/psamolysov/pgo-generate-version-when-instrumented.tar.gz llvm-users/psamolysov/pgo-generate-version-when-instrumented.tar.bz2 |
[PGO] Generate __llvm_profile_raw_version only when instrumentedusers/psamolysov/pgo-generate-version-when-instrumented
Currently, only modules that contain at least a single function
definition are instrumented. When a module contains no function
definitions at all, the module is not instrumented (yet?) and there is
no reason to introduce the '__llvm_profile_raw_version' variable into
the module.
5 files changed, 31 insertions, 19 deletions
diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp index fbf9688..ff94fc9 100644 --- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp +++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp @@ -405,9 +405,14 @@ static GlobalVariable *createIRLevelProfileFlagVar(Module &M, bool IsCS) { ProfileVersion |= VARIANT_MASK_BYTE_COVERAGE; if (PGOTemporalInstrumentation) ProfileVersion |= VARIANT_MASK_TEMPORAL_PROF; - auto IRLevelVersionVariable = new GlobalVariable( + assert(!M.global_empty() && + "If a module was instrumented, there must be defined global variables " + "at least for the counters."); + auto *InsertionPoint = &*M.global_begin(); + auto *IRLevelVersionVariable = new GlobalVariable( M, IntTy64, true, GlobalValue::WeakAnyLinkage, - Constant::getIntegerValue(IntTy64, APInt(64, ProfileVersion)), VarName); + Constant::getIntegerValue(IntTy64, APInt(64, ProfileVersion)), VarName, + InsertionPoint); IRLevelVersionVariable->setVisibility(GlobalValue::HiddenVisibility); Triple TT(M.getTargetTriple()); if (TT.supportsCOMDAT()) { @@ -1829,11 +1834,6 @@ static bool InstrumentAllFunctions( Module &M, function_ref<TargetLibraryInfo &(Function &)> LookupTLI, function_ref<BranchProbabilityInfo *(Function &)> LookupBPI, function_ref<BlockFrequencyInfo *(Function &)> LookupBFI, bool IsCS) { - // For the context-sensitve instrumentation, we should have a separated pass - // (before LTO/ThinLTO linking) to create these variables. - if (!IsCS && !PGOCtxProfLoweringPass::isContextualIRPGOEnabled()) - createIRLevelProfileFlagVar(M, /*IsCS=*/false); - Triple TT(M.getTargetTriple()); LLVMContext &Ctx = M.getContext(); if (!TT.isOSBinFormatELF() && EnableVTableValueProfiling) @@ -1855,9 +1855,15 @@ static bool InstrumentAllFunctions( instrumentOneFunc(F, &M, TLI, BPI, BFI, ComdatMembers, IsCS); AnythingInstrumented = true; } + if (!AnythingInstrumented) return false; + // For the context-sensitve instrumentation, we should have a separated pass + // (before LTO/ThinLTO linking) to create these variables. + if (!IsCS && !PGOCtxProfLoweringPass::isContextualIRPGOEnabled()) + createIRLevelProfileFlagVar(M, /*IsCS=*/false); + return true; } diff --git a/llvm/test/Transforms/PGOProfile/declarations_only.ll b/llvm/test/Transforms/PGOProfile/declarations_only.ll index e7208fc..7a975725 100644 --- a/llvm/test/Transforms/PGOProfile/declarations_only.ll +++ b/llvm/test/Transforms/PGOProfile/declarations_only.ll @@ -1,13 +1,8 @@ -; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN --check-prefix=GEN-COMDAT +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --implicit-check-not='__llvm_profile_raw_version' target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" -; GEN-COMDAT: $__llvm_profile_raw_version = comdat any -; GEN-COMDAT: @__llvm_profile_raw_version = hidden constant i64 {{[0-9]+}}, comdat -; GEN-NOT: @__profn_test_1 = private constant [6 x i8] c"test_1" -; GEN-NOT: @__profn_test_2 = private constant [6 x i8] c"test_2" - declare i32 @test_1(i32 %i) declare i32 @test_2(i32 %i) diff --git a/llvm/test/Transforms/PGOProfile/global_variables_only.ll b/llvm/test/Transforms/PGOProfile/global_variables_only.ll index 3bfa29a..46628bf 100644 --- a/llvm/test/Transforms/PGOProfile/global_variables_only.ll +++ b/llvm/test/Transforms/PGOProfile/global_variables_only.ll @@ -1,9 +1,6 @@ -; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN-COMDAT +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --implicit-check-not='__llvm_profile_raw_version' target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" -; GEN-COMDAT: $__llvm_profile_raw_version = comdat any -; GEN-COMDAT: @__llvm_profile_raw_version = hidden constant i64 {{[0-9]+}}, comdat - @var = internal unnamed_addr global [35 x ptr] zeroinitializer, align 16 diff --git a/llvm/test/Transforms/PGOProfile/no_global_variables.ll b/llvm/test/Transforms/PGOProfile/no_global_variables.ll new file mode 100644 index 0000000..0faa2f3 --- /dev/null +++ b/llvm/test/Transforms/PGOProfile/no_global_variables.ll @@ -0,0 +1,15 @@ +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN-COMDAT +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" +; GEN-COMDAT: $__llvm_profile_raw_version = comdat any +; GEN-COMDAT: @__llvm_profile_raw_version = hidden constant i64 {{[0-9]+}}, comdat + +define i32 @test_1(i32 %i) { +entry: + ret i32 0 +} + +define i32 @test_2(i32 %i) { +entry: + ret i32 1 +} diff --git a/llvm/unittests/Transforms/Instrumentation/PGOInstrumentationTest.cpp b/llvm/unittests/Transforms/Instrumentation/PGOInstrumentationTest.cpp index cbeaa50..01d17c5 100644 --- a/llvm/unittests/Transforms/Instrumentation/PGOInstrumentationTest.cpp +++ b/llvm/unittests/Transforms/Instrumentation/PGOInstrumentationTest.cpp @@ -217,8 +217,7 @@ TEST_P(PGOInstrumentationGenIgnoreTest, NotInstrumented) { const auto *IRInstrVar = M->getNamedGlobal(INSTR_PROF_QUOTE(INSTR_PROF_RAW_VERSION_VAR)); - EXPECT_THAT(IRInstrVar, NotNull()); - EXPECT_FALSE(IRInstrVar->isDeclaration()); + EXPECT_THAT(IRInstrVar, IsNull()); } } // end anonymous namespace |