2011-04-16 09:20:23 +08:00
|
|
|
//===- GCOVProfiling.cpp - Insert edge counters for gcov profiling --------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2011-04-16 09:20:23 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass implements GCOV-style profiling. When this pass is run it emits
|
|
|
|
// "gcno" files next to the existing source, and instruments the code that runs
|
|
|
|
// to records the edges between blocks that run and emit a complementary "gcda"
|
|
|
|
// file on exit.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-09-13 15:07:31 +08:00
|
|
|
#include "CFGMST.h"
|
2011-04-16 09:20:23 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2013-11-20 12:15:05 +08:00
|
|
|
#include "llvm/ADT/Hashing.h"
|
2020-10-06 03:39:19 +08:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2011-04-16 09:20:23 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2018-05-03 06:24:39 +08:00
|
|
|
#include "llvm/ADT/Sequence.h"
|
2012-06-29 20:38:19 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2011-04-16 09:20:23 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2020-09-13 15:07:31 +08:00
|
|
|
#include "llvm/Analysis/BlockFrequencyInfo.h"
|
|
|
|
#include "llvm/Analysis/BranchProbabilityInfo.h"
|
2017-10-13 21:49:15 +08:00
|
|
|
#include "llvm/Analysis/EHPersonalities.h"
|
2018-07-11 00:05:47 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2018-09-12 02:38:34 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2014-03-06 08:46:21 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2014-03-05 18:30:38 +08:00
|
|
|
#include "llvm/IR/DebugLoc.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2014-03-04 18:30:26 +08:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2014-01-31 13:24:01 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2020-07-27 12:14:20 +08:00
|
|
|
#include "llvm/Support/CRC.h"
|
2013-03-14 13:13:26 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-06-29 20:38:19 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2013-03-27 06:47:50 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2013-06-12 06:21:28 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-11-12 17:01:43 +08:00
|
|
|
#include "llvm/Support/Regex.h"
|
2012-06-29 20:38:19 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-06-05 13:12:23 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2018-05-03 06:24:39 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
|
2012-06-29 20:38:19 +08:00
|
|
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
2013-06-18 14:38:21 +08:00
|
|
|
#include <algorithm>
|
2014-04-22 04:41:55 +08:00
|
|
|
#include <memory>
|
2011-04-16 09:20:23 +08:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2020-06-07 02:01:47 +08:00
|
|
|
|
2011-04-16 09:20:23 +08:00
|
|
|
using namespace llvm;
|
2020-06-07 02:01:47 +08:00
|
|
|
namespace endian = llvm::support::endian;
|
2011-04-16 09:20:23 +08:00
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "insert-gcov-profiling"
|
|
|
|
|
2020-06-07 02:01:47 +08:00
|
|
|
enum : uint32_t {
|
2020-09-13 15:07:31 +08:00
|
|
|
GCOV_ARC_ON_TREE = 1 << 0,
|
|
|
|
|
2020-06-07 02:01:47 +08:00
|
|
|
GCOV_TAG_FUNCTION = 0x01000000,
|
|
|
|
GCOV_TAG_BLOCKS = 0x01410000,
|
|
|
|
GCOV_TAG_ARCS = 0x01430000,
|
|
|
|
GCOV_TAG_LINES = 0x01450000,
|
|
|
|
};
|
|
|
|
|
2020-05-11 03:47:45 +08:00
|
|
|
static cl::opt<std::string> DefaultGCOVVersion("default-gcov-version",
|
2020-05-12 14:20:33 +08:00
|
|
|
cl::init("408*"), cl::Hidden,
|
2020-05-11 03:47:45 +08:00
|
|
|
cl::ValueRequired);
|
2013-03-14 13:13:26 +08:00
|
|
|
|
2020-08-29 07:32:35 +08:00
|
|
|
static cl::opt<bool> AtomicCounter("gcov-atomic-counter", cl::Hidden,
|
|
|
|
cl::desc("Make counter updates atomic"));
|
|
|
|
|
2020-06-08 02:25:40 +08:00
|
|
|
// Returns the number of words which will be used to represent this string.
|
|
|
|
static unsigned wordsOfString(StringRef s) {
|
|
|
|
// Length + NUL-terminated string + 0~3 padding NULs.
|
|
|
|
return (s.size() / 4) + 2;
|
|
|
|
}
|
|
|
|
|
2013-03-14 13:13:26 +08:00
|
|
|
GCOVOptions GCOVOptions::getDefault() {
|
|
|
|
GCOVOptions Options;
|
|
|
|
Options.EmitNotes = true;
|
|
|
|
Options.EmitData = true;
|
|
|
|
Options.NoRedZone = false;
|
2020-08-29 07:32:35 +08:00
|
|
|
Options.Atomic = AtomicCounter;
|
2013-03-14 13:13:26 +08:00
|
|
|
|
|
|
|
if (DefaultGCOVVersion.size() != 4) {
|
2021-10-06 19:04:30 +08:00
|
|
|
llvm::report_fatal_error(Twine("Invalid -default-gcov-version: ") +
|
2013-03-14 13:13:26 +08:00
|
|
|
DefaultGCOVVersion);
|
|
|
|
}
|
|
|
|
memcpy(Options.Version, DefaultGCOVVersion.c_str(), 4);
|
|
|
|
return Options;
|
|
|
|
}
|
|
|
|
|
2011-04-16 09:20:23 +08:00
|
|
|
namespace {
|
2016-06-05 11:40:03 +08:00
|
|
|
class GCOVFunction;
|
|
|
|
|
|
|
|
class GCOVProfiler {
|
|
|
|
public:
|
|
|
|
GCOVProfiler() : GCOVProfiler(GCOVOptions::getDefault()) {}
|
2020-06-07 02:01:47 +08:00
|
|
|
GCOVProfiler(const GCOVOptions &Opts) : Options(Opts) {}
|
2020-09-14 03:54:36 +08:00
|
|
|
bool
|
|
|
|
runOnModule(Module &M, function_ref<BlockFrequencyInfo *(Function &F)> GetBFI,
|
|
|
|
function_ref<BranchProbabilityInfo *(Function &F)> GetBPI,
|
|
|
|
std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
|
2016-06-05 11:40:03 +08:00
|
|
|
|
2020-06-08 02:25:40 +08:00
|
|
|
void write(uint32_t i) {
|
|
|
|
char Bytes[4];
|
|
|
|
endian::write32(Bytes, i, Endian);
|
|
|
|
os->write(Bytes, 4);
|
|
|
|
}
|
|
|
|
void writeString(StringRef s) {
|
|
|
|
write(wordsOfString(s) - 1);
|
|
|
|
os->write(s.data(), s.size());
|
|
|
|
os->write_zeros(4 - s.size() % 4);
|
|
|
|
}
|
|
|
|
void writeBytes(const char *Bytes, int Size) { os->write(Bytes, Size); }
|
|
|
|
|
2016-06-05 11:40:03 +08:00
|
|
|
private:
|
|
|
|
// Create the .gcno files for the Module based on DebugInfo.
|
2020-09-13 15:07:31 +08:00
|
|
|
bool
|
|
|
|
emitProfileNotes(NamedMDNode *CUNode, bool HasExecOrFork,
|
|
|
|
function_ref<BlockFrequencyInfo *(Function &F)> GetBFI,
|
|
|
|
function_ref<BranchProbabilityInfo *(Function &F)> GetBPI,
|
|
|
|
function_ref<const TargetLibraryInfo &(Function &F)> GetTLI);
|
2016-06-05 11:40:03 +08:00
|
|
|
|
2021-04-27 04:30:20 +08:00
|
|
|
Function *createInternalFunction(FunctionType *FTy, StringRef Name);
|
2020-09-13 04:51:53 +08:00
|
|
|
void emitGlobalConstructor(
|
|
|
|
SmallVectorImpl<std::pair<GlobalVariable *, MDNode *>> &CountersBySP);
|
2016-06-05 11:40:03 +08:00
|
|
|
|
2018-11-12 17:01:43 +08:00
|
|
|
bool isFunctionInstrumented(const Function &F);
|
|
|
|
std::vector<Regex> createRegexesFromString(StringRef RegexesStr);
|
|
|
|
static bool doesFilenameMatchARegex(StringRef Filename,
|
|
|
|
std::vector<Regex> &Regexes);
|
|
|
|
|
2016-06-05 11:40:03 +08:00
|
|
|
// Get pointers to the functions in the runtime library.
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
FunctionCallee getStartFileFunc(const TargetLibraryInfo *TLI);
|
|
|
|
FunctionCallee getEmitFunctionFunc(const TargetLibraryInfo *TLI);
|
|
|
|
FunctionCallee getEmitArcsFunc(const TargetLibraryInfo *TLI);
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
FunctionCallee getSummaryInfoFunc();
|
|
|
|
FunctionCallee getEndFileFunc();
|
2016-06-05 11:40:03 +08:00
|
|
|
|
|
|
|
// Add the function to write out all our counters to the global destructor
|
|
|
|
// list.
|
|
|
|
Function *
|
|
|
|
insertCounterWriteout(ArrayRef<std::pair<GlobalVariable *, MDNode *>>);
|
[profile] Don't crash when forking in several threads
Summary:
When forking in several threads, the counters were written out in using the same global static variables (see GCDAProfiling.c): that leads to crashes.
So when there is a fork, the counters are resetted in the child process and they will be dumped at exit using the interprocess file locking.
When there is an exec, the counters are written out and in case of failures they're resetted.
Reviewers: jfb, vsk, marco-c, serge-sans-paille
Reviewed By: marco-c, serge-sans-paille
Subscribers: llvm-commits, serge-sans-paille, dmajor, cfe-commits, hiraditya, dexonsmith, #sanitizers, marco-c, sylvestre.ledru
Tags: #sanitizers, #clang, #llvm
Differential Revision: https://reviews.llvm.org/D78477
2020-04-20 16:50:30 +08:00
|
|
|
Function *insertReset(ArrayRef<std::pair<GlobalVariable *, MDNode *>>);
|
2016-06-05 11:40:03 +08:00
|
|
|
|
2020-06-05 15:08:06 +08:00
|
|
|
bool AddFlushBeforeForkAndExec();
|
2018-11-07 21:49:17 +08:00
|
|
|
|
2016-09-01 07:04:32 +08:00
|
|
|
enum class GCovFileType { GCNO, GCDA };
|
|
|
|
std::string mangleName(const DICompileUnit *CU, GCovFileType FileType);
|
2016-06-05 11:40:03 +08:00
|
|
|
|
|
|
|
GCOVOptions Options;
|
2020-06-08 02:25:40 +08:00
|
|
|
support::endianness Endian;
|
|
|
|
raw_ostream *os;
|
2016-06-05 11:40:03 +08:00
|
|
|
|
|
|
|
// Checksum, produced by hash of EdgeDestinations
|
|
|
|
SmallVector<uint32_t, 4> FileChecksums;
|
|
|
|
|
2019-11-14 21:55:28 +08:00
|
|
|
Module *M = nullptr;
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
|
2019-11-14 21:55:28 +08:00
|
|
|
LLVMContext *Ctx = nullptr;
|
2016-06-05 11:40:03 +08:00
|
|
|
SmallVector<std::unique_ptr<GCOVFunction>, 16> Funcs;
|
2018-11-12 17:01:43 +08:00
|
|
|
std::vector<Regex> FilterRe;
|
|
|
|
std::vector<Regex> ExcludeRe;
|
2020-09-13 15:07:31 +08:00
|
|
|
DenseSet<const BasicBlock *> ExecBlocks;
|
2018-11-12 17:01:43 +08:00
|
|
|
StringMap<bool> InstrumentedFiles;
|
2016-06-05 11:40:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class GCOVProfilerLegacyPass : public ModulePass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
GCOVProfilerLegacyPass()
|
|
|
|
: GCOVProfilerLegacyPass(GCOVOptions::getDefault()) {}
|
|
|
|
GCOVProfilerLegacyPass(const GCOVOptions &Opts)
|
|
|
|
: ModulePass(ID), Profiler(Opts) {
|
|
|
|
initializeGCOVProfilerLegacyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override { return "GCOV Profiler"; }
|
2016-06-05 11:40:03 +08:00
|
|
|
|
2018-07-31 03:41:25 +08:00
|
|
|
bool runOnModule(Module &M) override {
|
2020-09-13 15:07:31 +08:00
|
|
|
auto GetBFI = [this](Function &F) {
|
|
|
|
return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI();
|
|
|
|
};
|
|
|
|
auto GetBPI = [this](Function &F) {
|
|
|
|
return &this->getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI();
|
|
|
|
};
|
|
|
|
auto GetTLI = [this](Function &F) -> const TargetLibraryInfo & {
|
|
|
|
return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
|
|
|
|
};
|
|
|
|
return Profiler.runOnModule(M, GetBFI, GetBPI, GetTLI);
|
2018-07-11 00:05:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2020-09-13 15:07:31 +08:00
|
|
|
AU.addRequired<BlockFrequencyInfoWrapperPass>();
|
2018-07-11 00:05:47 +08:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
|
|
|
}
|
2015-03-07 00:21:15 +08:00
|
|
|
|
2016-06-05 11:40:03 +08:00
|
|
|
private:
|
|
|
|
GCOVProfiler Profiler;
|
|
|
|
};
|
2020-09-13 15:07:31 +08:00
|
|
|
|
|
|
|
struct BBInfo {
|
|
|
|
BBInfo *Group;
|
|
|
|
uint32_t Index;
|
|
|
|
uint32_t Rank = 0;
|
|
|
|
|
|
|
|
BBInfo(unsigned Index) : Group(this), Index(Index) {}
|
2021-02-11 12:01:18 +08:00
|
|
|
std::string infoString() const {
|
2020-09-13 15:07:31 +08:00
|
|
|
return (Twine("Index=") + Twine(Index)).str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Edge {
|
|
|
|
// This class implements the CFG edges. Note the CFG can be a multi-graph.
|
|
|
|
// So there might be multiple edges with same SrcBB and DestBB.
|
|
|
|
const BasicBlock *SrcBB;
|
|
|
|
const BasicBlock *DestBB;
|
|
|
|
uint64_t Weight;
|
|
|
|
BasicBlock *Place = nullptr;
|
|
|
|
uint32_t SrcNumber, DstNumber;
|
|
|
|
bool InMST = false;
|
|
|
|
bool Removed = false;
|
|
|
|
bool IsCritical = false;
|
|
|
|
|
|
|
|
Edge(const BasicBlock *Src, const BasicBlock *Dest, uint64_t W = 1)
|
|
|
|
: SrcBB(Src), DestBB(Dest), Weight(W) {}
|
|
|
|
|
|
|
|
// Return the information string of an edge.
|
2021-02-11 12:01:18 +08:00
|
|
|
std::string infoString() const {
|
2020-09-13 15:07:31 +08:00
|
|
|
return (Twine(Removed ? "-" : " ") + (InMST ? " " : "*") +
|
|
|
|
(IsCritical ? "c" : " ") + " W=" + Twine(Weight))
|
|
|
|
.str();
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2011-04-16 09:20:23 +08:00
|
|
|
|
2016-06-05 11:40:03 +08:00
|
|
|
char GCOVProfilerLegacyPass::ID = 0;
|
2018-07-11 00:05:47 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(
|
|
|
|
GCOVProfilerLegacyPass, "insert-gcov-profiling",
|
|
|
|
"Insert instrumentation for GCOV profiling", false, false)
|
2020-09-13 15:07:31 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
|
2018-07-11 00:05:47 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(
|
|
|
|
GCOVProfilerLegacyPass, "insert-gcov-profiling",
|
|
|
|
"Insert instrumentation for GCOV profiling", false, false)
|
2011-04-16 09:20:23 +08:00
|
|
|
|
2013-03-14 13:13:26 +08:00
|
|
|
ModulePass *llvm::createGCOVProfilerPass(const GCOVOptions &Options) {
|
2016-06-05 11:40:03 +08:00
|
|
|
return new GCOVProfilerLegacyPass(Options);
|
2011-04-21 09:56:25 +08:00
|
|
|
}
|
2011-04-16 09:20:23 +08:00
|
|
|
|
2015-04-30 00:38:44 +08:00
|
|
|
static StringRef getFunctionName(const DISubprogram *SP) {
|
2015-04-14 11:40:37 +08:00
|
|
|
if (!SP->getLinkageName().empty())
|
|
|
|
return SP->getLinkageName();
|
|
|
|
return SP->getName();
|
2013-03-19 09:37:55 +08:00
|
|
|
}
|
|
|
|
|
2018-12-07 02:44:48 +08:00
|
|
|
/// Extract a filename for a DISubprogram.
|
|
|
|
///
|
|
|
|
/// Prefer relative paths in the coverage notes. Clang also may split
|
|
|
|
/// up absolute paths into a directory and filename component. When
|
|
|
|
/// the relative path doesn't exist, reconstruct the absolute path.
|
2019-01-13 02:36:22 +08:00
|
|
|
static SmallString<128> getFilename(const DISubprogram *SP) {
|
2018-12-07 02:44:48 +08:00
|
|
|
SmallString<128> Path;
|
|
|
|
StringRef RelPath = SP->getFilename();
|
|
|
|
if (sys::fs::exists(RelPath))
|
|
|
|
Path = RelPath;
|
|
|
|
else
|
|
|
|
sys::path::append(Path, SP->getDirectory(), SP->getFilename());
|
|
|
|
return Path;
|
|
|
|
}
|
|
|
|
|
2011-04-16 09:20:23 +08:00
|
|
|
namespace {
|
|
|
|
class GCOVRecord {
|
2020-06-07 02:01:47 +08:00
|
|
|
protected:
|
2020-06-08 02:25:40 +08:00
|
|
|
GCOVProfiler *P;
|
2011-04-16 09:20:23 +08:00
|
|
|
|
2020-06-08 02:25:40 +08:00
|
|
|
GCOVRecord(GCOVProfiler *P) : P(P) {}
|
2011-04-16 09:20:23 +08:00
|
|
|
|
2020-06-08 02:25:40 +08:00
|
|
|
void write(uint32_t i) { P->write(i); }
|
|
|
|
void writeString(StringRef s) { P->writeString(s); }
|
|
|
|
void writeBytes(const char *Bytes, int Size) { P->writeBytes(Bytes, Size); }
|
2011-04-16 09:20:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class GCOVFunction;
|
|
|
|
class GCOVBlock;
|
|
|
|
|
|
|
|
// Constructed only by requesting it from a GCOVBlock, this object stores a
|
2011-09-21 02:35:00 +08:00
|
|
|
// list of line numbers and a single filename, representing lines that belong
|
|
|
|
// to the block.
|
2011-04-16 09:20:23 +08:00
|
|
|
class GCOVLines : public GCOVRecord {
|
|
|
|
public:
|
2011-04-26 11:54:16 +08:00
|
|
|
void addLine(uint32_t Line) {
|
2014-06-03 12:25:36 +08:00
|
|
|
assert(Line != 0 && "Line zero is not a valid real line number.");
|
2011-04-26 11:54:16 +08:00
|
|
|
Lines.push_back(Line);
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
2013-07-17 11:54:53 +08:00
|
|
|
uint32_t length() const {
|
2020-06-08 02:25:40 +08:00
|
|
|
return 1 + wordsOfString(Filename) + Lines.size();
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
2011-09-21 02:35:00 +08:00
|
|
|
void writeOut() {
|
|
|
|
write(0);
|
2020-06-08 02:25:40 +08:00
|
|
|
writeString(Filename);
|
2011-09-21 02:35:00 +08:00
|
|
|
for (int i = 0, e = Lines.size(); i != e; ++i)
|
|
|
|
write(Lines[i]);
|
|
|
|
}
|
2011-04-16 09:20:23 +08:00
|
|
|
|
2020-06-08 02:25:40 +08:00
|
|
|
GCOVLines(GCOVProfiler *P, StringRef F)
|
|
|
|
: GCOVRecord(P), Filename(std::string(F)) {}
|
2011-04-16 09:20:23 +08:00
|
|
|
|
2020-06-08 02:25:40 +08:00
|
|
|
private:
|
2018-12-07 02:44:48 +08:00
|
|
|
std::string Filename;
|
2011-04-26 11:54:16 +08:00
|
|
|
SmallVector<uint32_t, 32> Lines;
|
2011-04-16 09:20:23 +08:00
|
|
|
};
|
|
|
|
|
2013-06-18 14:38:21 +08:00
|
|
|
|
2011-04-16 09:20:23 +08:00
|
|
|
// Represent a basic block in GCOV. Each block has a unique number in the
|
|
|
|
// function, number of lines belonging to each block, and a set of edges to
|
|
|
|
// other blocks.
|
|
|
|
class GCOVBlock : public GCOVRecord {
|
|
|
|
public:
|
2011-09-21 01:55:19 +08:00
|
|
|
GCOVLines &getFile(StringRef Filename) {
|
2020-06-08 02:25:40 +08:00
|
|
|
return LinesByFile.try_emplace(Filename, P, Filename).first->second;
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
2020-09-13 15:07:31 +08:00
|
|
|
void addEdge(GCOVBlock &Successor, uint32_t Flags) {
|
|
|
|
OutEdges.emplace_back(&Successor, Flags);
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
2011-04-26 11:54:16 +08:00
|
|
|
void writeOut() {
|
|
|
|
uint32_t Len = 3;
|
2016-07-21 20:06:31 +08:00
|
|
|
SmallVector<StringMapEntry<GCOVLines> *, 32> SortedLinesByFile;
|
2016-06-26 20:28:59 +08:00
|
|
|
for (auto &I : LinesByFile) {
|
2016-07-21 20:06:31 +08:00
|
|
|
Len += I.second.length();
|
2016-06-26 20:28:59 +08:00
|
|
|
SortedLinesByFile.push_back(&I);
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
2020-06-07 02:01:47 +08:00
|
|
|
write(GCOV_TAG_LINES);
|
2011-04-26 11:54:16 +08:00
|
|
|
write(Len);
|
|
|
|
write(Number);
|
2013-06-18 14:38:21 +08:00
|
|
|
|
2018-10-01 06:31:29 +08:00
|
|
|
llvm::sort(SortedLinesByFile, [](StringMapEntry<GCOVLines> *LHS,
|
|
|
|
StringMapEntry<GCOVLines> *RHS) {
|
|
|
|
return LHS->getKey() < RHS->getKey();
|
|
|
|
});
|
2016-06-26 20:28:59 +08:00
|
|
|
for (auto &I : SortedLinesByFile)
|
2016-07-21 20:06:31 +08:00
|
|
|
I->getValue().writeOut();
|
2011-04-26 11:54:16 +08:00
|
|
|
write(0);
|
|
|
|
write(0);
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
2014-12-23 07:12:42 +08:00
|
|
|
GCOVBlock(const GCOVBlock &RHS) : GCOVRecord(RHS), Number(RHS.Number) {
|
|
|
|
// Only allow copy before edges and lines have been added. After that,
|
|
|
|
// there are inter-block pointers (eg: edges) that won't take kindly to
|
|
|
|
// blocks being copied or moved around.
|
|
|
|
assert(LinesByFile.empty());
|
|
|
|
assert(OutEdges.empty());
|
|
|
|
}
|
|
|
|
|
2020-07-27 12:14:20 +08:00
|
|
|
uint32_t Number;
|
2020-09-13 15:07:31 +08:00
|
|
|
SmallVector<std::pair<GCOVBlock *, uint32_t>, 4> OutEdges;
|
2020-07-27 12:14:20 +08:00
|
|
|
|
2020-09-13 15:07:31 +08:00
|
|
|
private:
|
2011-04-16 09:20:23 +08:00
|
|
|
friend class GCOVFunction;
|
|
|
|
|
2020-06-08 02:25:40 +08:00
|
|
|
GCOVBlock(GCOVProfiler *P, uint32_t Number)
|
|
|
|
: GCOVRecord(P), Number(Number) {}
|
2011-04-16 09:20:23 +08:00
|
|
|
|
2016-07-21 20:06:31 +08:00
|
|
|
StringMap<GCOVLines> LinesByFile;
|
2011-04-16 09:20:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// A function has a unique identifier, a checksum (we leave as zero) and a
|
|
|
|
// set of blocks and a map of edges between blocks. This is the only GCOV
|
|
|
|
// object users can construct, the blocks and lines will be rooted here.
|
|
|
|
class GCOVFunction : public GCOVRecord {
|
2020-06-07 02:01:47 +08:00
|
|
|
public:
|
2020-09-13 03:34:43 +08:00
|
|
|
GCOVFunction(GCOVProfiler *P, Function *F, const DISubprogram *SP,
|
2020-06-08 02:25:40 +08:00
|
|
|
unsigned EndLine, uint32_t Ident, int Version)
|
2020-09-13 03:34:43 +08:00
|
|
|
: GCOVRecord(P), SP(SP), EndLine(EndLine), Ident(Ident),
|
2020-09-10 03:06:39 +08:00
|
|
|
Version(Version), EntryBlock(P, 0), ReturnBlock(P, 1) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Function: " << getFunctionName(SP) << "\n");
|
2020-06-08 02:25:40 +08:00
|
|
|
bool ExitBlockBeforeBody = Version >= 48;
|
2020-09-10 03:06:39 +08:00
|
|
|
uint32_t i = ExitBlockBeforeBody ? 2 : 1;
|
2020-09-13 03:34:43 +08:00
|
|
|
for (BasicBlock &BB : *F)
|
2020-06-08 02:25:40 +08:00
|
|
|
Blocks.insert(std::make_pair(&BB, GCOVBlock(P, i++)));
|
2015-03-17 07:52:03 +08:00
|
|
|
if (!ExitBlockBeforeBody)
|
|
|
|
ReturnBlock.Number = i;
|
2013-12-04 16:57:17 +08:00
|
|
|
|
2014-06-27 06:52:05 +08:00
|
|
|
std::string FunctionNameAndLine;
|
|
|
|
raw_string_ostream FNLOS(FunctionNameAndLine);
|
2015-04-14 11:40:37 +08:00
|
|
|
FNLOS << getFunctionName(SP) << SP->getLine();
|
2014-06-27 06:52:05 +08:00
|
|
|
FNLOS.flush();
|
|
|
|
FuncChecksum = hash_value(FunctionNameAndLine);
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
2020-09-13 15:07:31 +08:00
|
|
|
GCOVBlock &getBlock(const BasicBlock *BB) {
|
2020-10-06 03:39:19 +08:00
|
|
|
return Blocks.find(const_cast<BasicBlock *>(BB))->second;
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
2020-09-10 03:06:39 +08:00
|
|
|
GCOVBlock &getEntryBlock() { return EntryBlock; }
|
2011-04-26 11:54:16 +08:00
|
|
|
GCOVBlock &getReturnBlock() {
|
2014-12-23 07:12:42 +08:00
|
|
|
return ReturnBlock;
|
2011-04-21 11:18:00 +08:00
|
|
|
}
|
|
|
|
|
2019-11-14 21:55:28 +08:00
|
|
|
uint32_t getFuncChecksum() const {
|
2013-12-04 16:57:17 +08:00
|
|
|
return FuncChecksum;
|
|
|
|
}
|
|
|
|
|
2020-06-07 11:36:46 +08:00
|
|
|
void writeOut(uint32_t CfgChecksum) {
|
2020-06-07 02:01:47 +08:00
|
|
|
write(GCOV_TAG_FUNCTION);
|
2018-12-07 02:44:48 +08:00
|
|
|
SmallString<128> Filename = getFilename(SP);
|
2020-06-08 02:25:40 +08:00
|
|
|
uint32_t BlockLen =
|
|
|
|
2 + (Version >= 47) + wordsOfString(getFunctionName(SP));
|
|
|
|
if (Version < 80)
|
|
|
|
BlockLen += wordsOfString(Filename) + 1;
|
|
|
|
else
|
|
|
|
BlockLen += 1 + wordsOfString(Filename) + 3 + (Version >= 90);
|
|
|
|
|
2013-11-20 12:15:05 +08:00
|
|
|
write(BlockLen);
|
|
|
|
write(Ident);
|
2013-12-04 16:57:17 +08:00
|
|
|
write(FuncChecksum);
|
2020-06-08 02:25:40 +08:00
|
|
|
if (Version >= 47)
|
2013-11-20 12:15:05 +08:00
|
|
|
write(CfgChecksum);
|
2020-06-08 02:25:40 +08:00
|
|
|
writeString(getFunctionName(SP));
|
|
|
|
if (Version < 80) {
|
|
|
|
writeString(Filename);
|
|
|
|
write(SP->getLine());
|
|
|
|
} else {
|
|
|
|
write(SP->isArtificial()); // artificial
|
|
|
|
writeString(Filename);
|
|
|
|
write(SP->getLine()); // start_line
|
|
|
|
write(0); // start_column
|
|
|
|
// EndLine is the last line with !dbg. It is not the } line as in GCC,
|
|
|
|
// but good enough.
|
|
|
|
write(EndLine);
|
|
|
|
if (Version >= 90)
|
|
|
|
write(0); // end_column
|
|
|
|
}
|
2013-11-20 12:15:05 +08:00
|
|
|
|
2011-04-16 09:20:23 +08:00
|
|
|
// Emit count of blocks.
|
2020-06-07 02:01:47 +08:00
|
|
|
write(GCOV_TAG_BLOCKS);
|
2020-06-08 02:25:40 +08:00
|
|
|
if (Version < 80) {
|
2020-09-10 03:06:39 +08:00
|
|
|
write(Blocks.size() + 2);
|
|
|
|
for (int i = Blocks.size() + 2; i; --i)
|
2020-06-08 02:25:40 +08:00
|
|
|
write(0);
|
|
|
|
} else {
|
|
|
|
write(1);
|
2020-09-10 03:06:39 +08:00
|
|
|
write(Blocks.size() + 2);
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
2020-06-08 02:25:40 +08:00
|
|
|
LLVM_DEBUG(dbgs() << (Blocks.size() + 1) << " blocks\n");
|
2011-04-16 09:20:23 +08:00
|
|
|
|
|
|
|
// Emit edges between blocks.
|
2020-09-13 15:07:31 +08:00
|
|
|
const uint32_t Outgoing = EntryBlock.OutEdges.size();
|
|
|
|
if (Outgoing) {
|
|
|
|
write(GCOV_TAG_ARCS);
|
|
|
|
write(Outgoing * 2 + 1);
|
|
|
|
write(EntryBlock.Number);
|
|
|
|
for (const auto &E : EntryBlock.OutEdges) {
|
|
|
|
write(E.first->Number);
|
|
|
|
write(E.second);
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 03:39:19 +08:00
|
|
|
for (auto &It : Blocks) {
|
|
|
|
const GCOVBlock &Block = It.second;
|
2011-04-26 11:54:16 +08:00
|
|
|
if (Block.OutEdges.empty()) continue;
|
|
|
|
|
2020-06-07 02:01:47 +08:00
|
|
|
write(GCOV_TAG_ARCS);
|
2011-04-26 11:54:16 +08:00
|
|
|
write(Block.OutEdges.size() * 2 + 1);
|
|
|
|
write(Block.Number);
|
2020-09-13 15:07:31 +08:00
|
|
|
for (const auto &E : Block.OutEdges) {
|
|
|
|
write(E.first->Number);
|
|
|
|
write(E.second);
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit lines for each block.
|
2020-10-06 03:39:19 +08:00
|
|
|
for (auto &It : Blocks)
|
|
|
|
It.second.writeOut();
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
2020-09-13 15:07:31 +08:00
|
|
|
public:
|
2020-06-07 02:01:47 +08:00
|
|
|
const DISubprogram *SP;
|
2020-06-08 02:25:40 +08:00
|
|
|
unsigned EndLine;
|
2013-11-20 12:15:05 +08:00
|
|
|
uint32_t Ident;
|
2013-12-04 16:57:17 +08:00
|
|
|
uint32_t FuncChecksum;
|
2020-06-08 02:25:40 +08:00
|
|
|
int Version;
|
2020-10-06 03:39:19 +08:00
|
|
|
MapVector<BasicBlock *, GCOVBlock> Blocks;
|
2020-09-10 03:06:39 +08:00
|
|
|
GCOVBlock EntryBlock;
|
2014-12-23 07:12:42 +08:00
|
|
|
GCOVBlock ReturnBlock;
|
2011-04-16 09:20:23 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2011-04-16 09:20:23 +08:00
|
|
|
|
2018-11-12 17:01:43 +08:00
|
|
|
// RegexesStr is a string containing differents regex separated by a semi-colon.
|
|
|
|
// For example "foo\..*$;bar\..*$".
|
|
|
|
std::vector<Regex> GCOVProfiler::createRegexesFromString(StringRef RegexesStr) {
|
|
|
|
std::vector<Regex> Regexes;
|
|
|
|
while (!RegexesStr.empty()) {
|
|
|
|
std::pair<StringRef, StringRef> HeadTail = RegexesStr.split(';');
|
|
|
|
if (!HeadTail.first.empty()) {
|
|
|
|
Regex Re(HeadTail.first);
|
|
|
|
std::string Err;
|
|
|
|
if (!Re.isValid(Err)) {
|
|
|
|
Ctx->emitError(Twine("Regex ") + HeadTail.first +
|
|
|
|
" is not valid: " + Err);
|
|
|
|
}
|
|
|
|
Regexes.emplace_back(std::move(Re));
|
|
|
|
}
|
|
|
|
RegexesStr = HeadTail.second;
|
|
|
|
}
|
|
|
|
return Regexes;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GCOVProfiler::doesFilenameMatchARegex(StringRef Filename,
|
|
|
|
std::vector<Regex> &Regexes) {
|
2020-06-07 11:36:46 +08:00
|
|
|
for (Regex &Re : Regexes)
|
|
|
|
if (Re.match(Filename))
|
2018-11-12 17:01:43 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GCOVProfiler::isFunctionInstrumented(const Function &F) {
|
|
|
|
if (FilterRe.empty() && ExcludeRe.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-07 02:44:48 +08:00
|
|
|
SmallString<128> Filename = getFilename(F.getSubprogram());
|
2018-11-12 17:01:43 +08:00
|
|
|
auto It = InstrumentedFiles.find(Filename);
|
|
|
|
if (It != InstrumentedFiles.end()) {
|
|
|
|
return It->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallString<256> RealPath;
|
|
|
|
StringRef RealFilename;
|
|
|
|
|
|
|
|
// Path can be
|
|
|
|
// /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/*.h so for
|
|
|
|
// such a case we must get the real_path.
|
|
|
|
if (sys::fs::real_path(Filename, RealPath)) {
|
|
|
|
// real_path can fail with path like "foo.c".
|
|
|
|
RealFilename = Filename;
|
|
|
|
} else {
|
|
|
|
RealFilename = RealPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShouldInstrument;
|
|
|
|
if (FilterRe.empty()) {
|
|
|
|
ShouldInstrument = !doesFilenameMatchARegex(RealFilename, ExcludeRe);
|
|
|
|
} else if (ExcludeRe.empty()) {
|
|
|
|
ShouldInstrument = doesFilenameMatchARegex(RealFilename, FilterRe);
|
|
|
|
} else {
|
|
|
|
ShouldInstrument = doesFilenameMatchARegex(RealFilename, FilterRe) &&
|
|
|
|
!doesFilenameMatchARegex(RealFilename, ExcludeRe);
|
|
|
|
}
|
|
|
|
InstrumentedFiles[Filename] = ShouldInstrument;
|
|
|
|
return ShouldInstrument;
|
|
|
|
}
|
|
|
|
|
2015-04-30 00:38:44 +08:00
|
|
|
std::string GCOVProfiler::mangleName(const DICompileUnit *CU,
|
2016-09-01 07:04:32 +08:00
|
|
|
GCovFileType OutputType) {
|
|
|
|
bool Notes = OutputType == GCovFileType::GCNO;
|
|
|
|
|
2011-05-04 12:03:04 +08:00
|
|
|
if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
|
|
|
|
for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
|
2014-11-12 05:30:22 +08:00
|
|
|
MDNode *N = GCov->getOperand(i);
|
2016-09-01 07:04:32 +08:00
|
|
|
bool ThreeElement = N->getNumOperands() == 3;
|
|
|
|
if (!ThreeElement && N->getNumOperands() != 2)
|
|
|
|
continue;
|
2016-09-01 07:24:43 +08:00
|
|
|
if (dyn_cast<MDNode>(N->getOperand(ThreeElement ? 2 : 1)) != CU)
|
2016-09-01 07:04:32 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ThreeElement) {
|
|
|
|
// These nodes have no mangling to apply, it's stored mangled in the
|
|
|
|
// bitcode.
|
|
|
|
MDString *NotesFile = dyn_cast<MDString>(N->getOperand(0));
|
|
|
|
MDString *DataFile = dyn_cast<MDString>(N->getOperand(1));
|
|
|
|
if (!NotesFile || !DataFile)
|
|
|
|
continue;
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(Notes ? NotesFile->getString()
|
|
|
|
: DataFile->getString());
|
2011-05-05 08:03:30 +08:00
|
|
|
}
|
2016-09-01 07:04:32 +08:00
|
|
|
|
|
|
|
MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0));
|
|
|
|
if (!GCovFile)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SmallString<128> Filename = GCovFile->getString();
|
|
|
|
sys::path::replace_extension(Filename, Notes ? "gcno" : "gcda");
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(Filename.str());
|
2011-05-04 12:03:04 +08:00
|
|
|
}
|
|
|
|
}
|
2011-05-05 08:03:30 +08:00
|
|
|
|
2018-12-05 00:30:31 +08:00
|
|
|
SmallString<128> Filename = CU->getFilename();
|
2016-09-01 07:04:32 +08:00
|
|
|
sys::path::replace_extension(Filename, Notes ? "gcno" : "gcda");
|
2013-03-27 06:47:50 +08:00
|
|
|
StringRef FName = sys::path::filename(Filename);
|
|
|
|
SmallString<128> CurPath;
|
2020-01-29 03:23:46 +08:00
|
|
|
if (sys::fs::current_path(CurPath))
|
|
|
|
return std::string(FName);
|
2015-03-28 01:51:30 +08:00
|
|
|
sys::path::append(CurPath, FName);
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(CurPath.str());
|
2011-05-04 12:03:04 +08:00
|
|
|
}
|
|
|
|
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
bool GCOVProfiler::runOnModule(
|
2020-09-13 15:07:31 +08:00
|
|
|
Module &M, function_ref<BlockFrequencyInfo *(Function &F)> GetBFI,
|
|
|
|
function_ref<BranchProbabilityInfo *(Function &F)> GetBPI,
|
2020-09-14 03:54:36 +08:00
|
|
|
std::function<const TargetLibraryInfo &(Function &F)> GetTLI) {
|
2011-04-26 11:54:16 +08:00
|
|
|
this->M = &M;
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
this->GetTLI = std::move(GetTLI);
|
2011-04-16 10:05:18 +08:00
|
|
|
Ctx = &M.getContext();
|
|
|
|
|
2020-09-13 03:05:25 +08:00
|
|
|
NamedMDNode *CUNode = M.getNamedMetadata("llvm.dbg.cu");
|
2020-09-13 04:51:53 +08:00
|
|
|
if (!CUNode || (!Options.EmitNotes && !Options.EmitData))
|
2020-09-13 03:05:25 +08:00
|
|
|
return false;
|
|
|
|
|
2020-09-13 15:07:31 +08:00
|
|
|
bool HasExecOrFork = AddFlushBeforeForkAndExec();
|
2018-11-07 21:49:17 +08:00
|
|
|
|
2018-11-12 17:01:43 +08:00
|
|
|
FilterRe = createRegexesFromString(Options.Filter);
|
|
|
|
ExcludeRe = createRegexesFromString(Options.Exclude);
|
2020-09-14 03:54:36 +08:00
|
|
|
emitProfileNotes(CUNode, HasExecOrFork, GetBFI, GetBPI, this->GetTLI);
|
2020-09-13 15:07:31 +08:00
|
|
|
return true;
|
2011-04-16 10:05:18 +08:00
|
|
|
}
|
|
|
|
|
2016-06-05 13:12:23 +08:00
|
|
|
PreservedAnalyses GCOVProfilerPass::run(Module &M,
|
2016-08-09 08:28:38 +08:00
|
|
|
ModuleAnalysisManager &AM) {
|
2016-06-05 13:12:23 +08:00
|
|
|
|
|
|
|
GCOVProfiler Profiler(GCOVOpts);
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
FunctionAnalysisManager &FAM =
|
|
|
|
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
|
2016-06-05 13:12:23 +08:00
|
|
|
|
2020-09-13 15:07:31 +08:00
|
|
|
auto GetBFI = [&FAM](Function &F) {
|
|
|
|
return &FAM.getResult<BlockFrequencyAnalysis>(F);
|
|
|
|
};
|
|
|
|
auto GetBPI = [&FAM](Function &F) {
|
|
|
|
return &FAM.getResult<BranchProbabilityAnalysis>(F);
|
|
|
|
};
|
|
|
|
auto GetTLI = [&FAM](Function &F) -> const TargetLibraryInfo & {
|
|
|
|
return FAM.getResult<TargetLibraryAnalysis>(F);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!Profiler.runOnModule(M, GetBFI, GetBPI, GetTLI))
|
2016-06-05 13:12:23 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
|
|
|
return PreservedAnalyses::none();
|
|
|
|
}
|
|
|
|
|
2020-06-08 02:25:40 +08:00
|
|
|
static bool functionHasLines(const Function &F, unsigned &EndLine) {
|
2014-04-19 07:32:28 +08:00
|
|
|
// Check whether this function actually has any source lines. Not only
|
|
|
|
// do these waste space, they also can crash gcov.
|
2020-06-08 02:25:40 +08:00
|
|
|
EndLine = 0;
|
2016-04-15 23:57:41 +08:00
|
|
|
for (auto &BB : F) {
|
|
|
|
for (auto &I : BB) {
|
2014-06-05 05:47:19 +08:00
|
|
|
// Debug intrinsic locations correspond to the location of the
|
|
|
|
// declaration, not necessarily any statements or expressions.
|
2016-04-15 23:57:41 +08:00
|
|
|
if (isa<DbgInfoIntrinsic>(&I)) continue;
|
2014-06-05 12:31:43 +08:00
|
|
|
|
2016-04-15 23:57:41 +08:00
|
|
|
const DebugLoc &Loc = I.getDebugLoc();
|
2015-03-31 03:49:49 +08:00
|
|
|
if (!Loc)
|
|
|
|
continue;
|
2014-06-05 12:31:43 +08:00
|
|
|
|
|
|
|
// Artificial lines such as calls to the global constructors.
|
2015-03-17 07:52:03 +08:00
|
|
|
if (Loc.getLine() == 0) continue;
|
2020-06-08 02:25:40 +08:00
|
|
|
EndLine = std::max(EndLine, Loc.getLine());
|
2014-06-05 12:31:43 +08:00
|
|
|
|
2014-06-03 12:25:36 +08:00
|
|
|
return true;
|
2014-04-19 07:32:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
[WebAssembly] Add Wasm personality and isScopedEHPersonality()
Summary:
- Add wasm personality function
- Re-categorize the existing `isFuncletEHPersonality()` function into
two different functions: `isFuncletEHPersonality()` and
`isScopedEHPersonality(). This becomes necessary as wasm EH uses scoped
EH instructions (catchswitch, catchpad/ret, and cleanuppad/ret) but not
outlined funclets.
- Changed some callsites of `isFuncletEHPersonality()` to
`isScopedEHPersonality()` if they are related to scoped EH IR-level
stuff.
Reviewers: majnemer, dschuff, rnk
Subscribers: jfb, sbc100, jgravelle-google, eraman, JDevlieghere, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D45559
llvm-svn: 332667
2018-05-18 04:52:03 +08:00
|
|
|
static bool isUsingScopeBasedEH(Function &F) {
|
2017-10-13 21:49:15 +08:00
|
|
|
if (!F.hasPersonalityFn()) return false;
|
|
|
|
|
|
|
|
EHPersonality Personality = classifyEHPersonality(F.getPersonalityFn());
|
[WebAssembly] Add Wasm personality and isScopedEHPersonality()
Summary:
- Add wasm personality function
- Re-categorize the existing `isFuncletEHPersonality()` function into
two different functions: `isFuncletEHPersonality()` and
`isScopedEHPersonality(). This becomes necessary as wasm EH uses scoped
EH instructions (catchswitch, catchpad/ret, and cleanuppad/ret) but not
outlined funclets.
- Changed some callsites of `isFuncletEHPersonality()` to
`isScopedEHPersonality()` if they are related to scoped EH IR-level
stuff.
Reviewers: majnemer, dschuff, rnk
Subscribers: jfb, sbc100, jgravelle-google, eraman, JDevlieghere, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D45559
llvm-svn: 332667
2018-05-18 04:52:03 +08:00
|
|
|
return isScopedEHPersonality(Personality);
|
2017-10-13 21:49:15 +08:00
|
|
|
}
|
|
|
|
|
2020-06-05 15:08:06 +08:00
|
|
|
bool GCOVProfiler::AddFlushBeforeForkAndExec() {
|
[profile] Don't crash when forking in several threads
Summary:
When forking in several threads, the counters were written out in using the same global static variables (see GCDAProfiling.c): that leads to crashes.
So when there is a fork, the counters are resetted in the child process and they will be dumped at exit using the interprocess file locking.
When there is an exec, the counters are written out and in case of failures they're resetted.
Reviewers: jfb, vsk, marco-c, serge-sans-paille
Reviewed By: marco-c, serge-sans-paille
Subscribers: llvm-commits, serge-sans-paille, dmajor, cfe-commits, hiraditya, dexonsmith, #sanitizers, marco-c, sylvestre.ledru
Tags: #sanitizers, #clang, #llvm
Differential Revision: https://reviews.llvm.org/D78477
2020-04-20 16:50:30 +08:00
|
|
|
SmallVector<CallInst *, 2> Forks;
|
|
|
|
SmallVector<CallInst *, 2> Execs;
|
2018-11-07 21:49:17 +08:00
|
|
|
for (auto &F : M->functions()) {
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
auto *TLI = &GetTLI(F);
|
2018-11-07 21:49:17 +08:00
|
|
|
for (auto &I : instructions(F)) {
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(&I)) {
|
|
|
|
if (Function *Callee = CI->getCalledFunction()) {
|
|
|
|
LibFunc LF;
|
[profile] Don't crash when forking in several threads
Summary:
When forking in several threads, the counters were written out in using the same global static variables (see GCDAProfiling.c): that leads to crashes.
So when there is a fork, the counters are resetted in the child process and they will be dumped at exit using the interprocess file locking.
When there is an exec, the counters are written out and in case of failures they're resetted.
Reviewers: jfb, vsk, marco-c, serge-sans-paille
Reviewed By: marco-c, serge-sans-paille
Subscribers: llvm-commits, serge-sans-paille, dmajor, cfe-commits, hiraditya, dexonsmith, #sanitizers, marco-c, sylvestre.ledru
Tags: #sanitizers, #clang, #llvm
Differential Revision: https://reviews.llvm.org/D78477
2020-04-20 16:50:30 +08:00
|
|
|
if (TLI->getLibFunc(*Callee, LF)) {
|
|
|
|
if (LF == LibFunc_fork) {
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
Forks.push_back(CI);
|
|
|
|
#endif
|
|
|
|
} else if (LF == LibFunc_execl || LF == LibFunc_execle ||
|
|
|
|
LF == LibFunc_execlp || LF == LibFunc_execv ||
|
|
|
|
LF == LibFunc_execvp || LF == LibFunc_execve ||
|
|
|
|
LF == LibFunc_execvpe || LF == LibFunc_execvP) {
|
|
|
|
Execs.push_back(CI);
|
|
|
|
}
|
2018-11-07 21:49:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[profile] Don't crash when forking in several threads
Summary:
When forking in several threads, the counters were written out in using the same global static variables (see GCDAProfiling.c): that leads to crashes.
So when there is a fork, the counters are resetted in the child process and they will be dumped at exit using the interprocess file locking.
When there is an exec, the counters are written out and in case of failures they're resetted.
Reviewers: jfb, vsk, marco-c, serge-sans-paille
Reviewed By: marco-c, serge-sans-paille
Subscribers: llvm-commits, serge-sans-paille, dmajor, cfe-commits, hiraditya, dexonsmith, #sanitizers, marco-c, sylvestre.ledru
Tags: #sanitizers, #clang, #llvm
Differential Revision: https://reviews.llvm.org/D78477
2020-04-20 16:50:30 +08:00
|
|
|
for (auto F : Forks) {
|
|
|
|
IRBuilder<> Builder(F);
|
|
|
|
BasicBlock *Parent = F->getParent();
|
|
|
|
auto NextInst = ++F->getIterator();
|
|
|
|
|
|
|
|
// We've a fork so just reset the counters in the child process
|
|
|
|
FunctionType *FTy = FunctionType::get(Builder.getInt32Ty(), {}, false);
|
|
|
|
FunctionCallee GCOVFork = M->getOrInsertFunction("__gcov_fork", FTy);
|
|
|
|
F->setCalledFunction(GCOVFork);
|
|
|
|
|
|
|
|
// We split just after the fork to have a counter for the lines after
|
|
|
|
// Anyway there's a bug:
|
|
|
|
// void foo() { fork(); }
|
|
|
|
// void bar() { foo(); blah(); }
|
|
|
|
// then "blah();" will be called 2 times but showed as 1
|
|
|
|
// because "blah()" belongs to the same block as "foo();"
|
|
|
|
Parent->splitBasicBlock(NextInst);
|
|
|
|
|
|
|
|
// back() is a br instruction with a debug location
|
|
|
|
// equals to the one from NextAfterFork
|
|
|
|
// So to avoid to have two debug locs on two blocks just change it
|
|
|
|
DebugLoc Loc = F->getDebugLoc();
|
|
|
|
Parent->back().setDebugLoc(Loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto E : Execs) {
|
|
|
|
IRBuilder<> Builder(E);
|
|
|
|
BasicBlock *Parent = E->getParent();
|
|
|
|
auto NextInst = ++E->getIterator();
|
|
|
|
|
|
|
|
// Since the process is replaced by a new one we need to write out gcdas
|
|
|
|
// No need to reset the counters since they'll be lost after the exec**
|
2020-02-26 20:23:56 +08:00
|
|
|
FunctionType *FTy = FunctionType::get(Builder.getVoidTy(), {}, false);
|
[profile] Don't crash when forking in several threads
Summary:
When forking in several threads, the counters were written out in using the same global static variables (see GCDAProfiling.c): that leads to crashes.
So when there is a fork, the counters are resetted in the child process and they will be dumped at exit using the interprocess file locking.
When there is an exec, the counters are written out and in case of failures they're resetted.
Reviewers: jfb, vsk, marco-c, serge-sans-paille
Reviewed By: marco-c, serge-sans-paille
Subscribers: llvm-commits, serge-sans-paille, dmajor, cfe-commits, hiraditya, dexonsmith, #sanitizers, marco-c, sylvestre.ledru
Tags: #sanitizers, #clang, #llvm
Differential Revision: https://reviews.llvm.org/D78477
2020-04-20 16:50:30 +08:00
|
|
|
FunctionCallee WriteoutF =
|
|
|
|
M->getOrInsertFunction("llvm_writeout_files", FTy);
|
|
|
|
Builder.CreateCall(WriteoutF);
|
|
|
|
|
|
|
|
DebugLoc Loc = E->getDebugLoc();
|
|
|
|
Builder.SetInsertPoint(&*NextInst);
|
|
|
|
// If the exec** fails we must reset the counters since they've been
|
|
|
|
// dumped
|
|
|
|
FunctionCallee ResetF = M->getOrInsertFunction("llvm_reset_counters", FTy);
|
|
|
|
Builder.CreateCall(ResetF)->setDebugLoc(Loc);
|
2020-09-13 15:07:31 +08:00
|
|
|
ExecBlocks.insert(Parent);
|
[profile] Don't crash when forking in several threads
Summary:
When forking in several threads, the counters were written out in using the same global static variables (see GCDAProfiling.c): that leads to crashes.
So when there is a fork, the counters are resetted in the child process and they will be dumped at exit using the interprocess file locking.
When there is an exec, the counters are written out and in case of failures they're resetted.
Reviewers: jfb, vsk, marco-c, serge-sans-paille
Reviewed By: marco-c, serge-sans-paille
Subscribers: llvm-commits, serge-sans-paille, dmajor, cfe-commits, hiraditya, dexonsmith, #sanitizers, marco-c, sylvestre.ledru
Tags: #sanitizers, #clang, #llvm
Differential Revision: https://reviews.llvm.org/D78477
2020-04-20 16:50:30 +08:00
|
|
|
Parent->splitBasicBlock(NextInst);
|
|
|
|
Parent->back().setDebugLoc(Loc);
|
2018-11-07 21:49:17 +08:00
|
|
|
}
|
2020-06-05 15:08:06 +08:00
|
|
|
|
|
|
|
return !Forks.empty() || !Execs.empty();
|
2018-11-07 21:49:17 +08:00
|
|
|
}
|
|
|
|
|
2020-09-13 15:07:31 +08:00
|
|
|
static BasicBlock *getInstrBB(CFGMST<Edge, BBInfo> &MST, Edge &E,
|
|
|
|
const DenseSet<const BasicBlock *> &ExecBlocks) {
|
|
|
|
if (E.InMST || E.Removed)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
BasicBlock *SrcBB = const_cast<BasicBlock *>(E.SrcBB);
|
|
|
|
BasicBlock *DestBB = const_cast<BasicBlock *>(E.DestBB);
|
|
|
|
// For a fake edge, instrument the real BB.
|
|
|
|
if (SrcBB == nullptr)
|
|
|
|
return DestBB;
|
|
|
|
if (DestBB == nullptr)
|
|
|
|
return SrcBB;
|
|
|
|
|
|
|
|
auto CanInstrument = [](BasicBlock *BB) -> BasicBlock * {
|
|
|
|
// There are basic blocks (such as catchswitch) cannot be instrumented.
|
|
|
|
// If the returned first insertion point is the end of BB, skip this BB.
|
|
|
|
if (BB->getFirstInsertionPt() == BB->end())
|
|
|
|
return nullptr;
|
|
|
|
return BB;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Instrument the SrcBB if it has a single successor,
|
|
|
|
// otherwise, the DestBB if this is not a critical edge.
|
|
|
|
Instruction *TI = SrcBB->getTerminator();
|
|
|
|
if (TI->getNumSuccessors() <= 1 && !ExecBlocks.count(SrcBB))
|
|
|
|
return CanInstrument(SrcBB);
|
|
|
|
if (!E.IsCritical)
|
|
|
|
return CanInstrument(DestBB);
|
|
|
|
|
|
|
|
// Some IndirectBr critical edges cannot be split by the previous
|
|
|
|
// SplitIndirectBrCriticalEdges call. Bail out.
|
|
|
|
const unsigned SuccNum = GetSuccessorNumber(SrcBB, DestBB);
|
|
|
|
BasicBlock *InstrBB =
|
|
|
|
isa<IndirectBrInst>(TI) ? nullptr : SplitCriticalEdge(TI, SuccNum);
|
|
|
|
if (!InstrBB)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
MST.addEdge(SrcBB, InstrBB, 0);
|
|
|
|
MST.addEdge(InstrBB, DestBB, 0).InMST = true;
|
|
|
|
E.Removed = true;
|
|
|
|
|
|
|
|
return CanInstrument(InstrBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
static void dumpEdges(CFGMST<Edge, BBInfo> &MST, GCOVFunction &GF) {
|
|
|
|
size_t ID = 0;
|
|
|
|
for (auto &E : make_pointee_range(MST.AllEdges)) {
|
|
|
|
GCOVBlock &Src = E.SrcBB ? GF.getBlock(E.SrcBB) : GF.getEntryBlock();
|
|
|
|
GCOVBlock &Dst = E.DestBB ? GF.getBlock(E.DestBB) : GF.getReturnBlock();
|
|
|
|
dbgs() << " Edge " << ID++ << ": " << Src.Number << "->" << Dst.Number
|
|
|
|
<< E.infoString() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool GCOVProfiler::emitProfileNotes(
|
|
|
|
NamedMDNode *CUNode, bool HasExecOrFork,
|
|
|
|
function_ref<BlockFrequencyInfo *(Function &F)> GetBFI,
|
|
|
|
function_ref<BranchProbabilityInfo *(Function &F)> GetBPI,
|
|
|
|
function_ref<const TargetLibraryInfo &(Function &F)> GetTLI) {
|
2020-06-08 02:25:40 +08:00
|
|
|
int Version;
|
|
|
|
{
|
|
|
|
uint8_t c3 = Options.Version[0];
|
|
|
|
uint8_t c2 = Options.Version[1];
|
|
|
|
uint8_t c1 = Options.Version[2];
|
|
|
|
Version = c3 >= 'A' ? (c3 - 'A') * 100 + (c2 - '0') * 10 + c1 - '0'
|
|
|
|
: (c3 - '0') * 10 + c1 - '0';
|
|
|
|
}
|
|
|
|
|
2020-09-13 04:51:53 +08:00
|
|
|
bool EmitGCDA = Options.EmitData;
|
2020-09-13 03:05:25 +08:00
|
|
|
for (unsigned i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
|
2011-11-28 07:22:20 +08:00
|
|
|
// Each compile unit gets its own .gcno file. This means that whether we run
|
|
|
|
// this pass over the original .o's as they're produced, or run it after
|
|
|
|
// LTO, we'll generate the same .gcno files.
|
|
|
|
|
2020-09-13 03:05:25 +08:00
|
|
|
auto *CU = cast<DICompileUnit>(CUNode->getOperand(i));
|
2016-01-22 01:04:42 +08:00
|
|
|
|
|
|
|
// Skip module skeleton (and module) CUs.
|
|
|
|
if (CU->getDWOId())
|
|
|
|
continue;
|
|
|
|
|
2020-07-27 12:14:20 +08:00
|
|
|
std::vector<uint8_t> EdgeDestinations;
|
2020-09-13 04:51:53 +08:00
|
|
|
SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP;
|
2011-11-28 07:22:20 +08:00
|
|
|
|
2020-06-08 02:25:40 +08:00
|
|
|
Endian = M->getDataLayout().isLittleEndian() ? support::endianness::little
|
|
|
|
: support::endianness::big;
|
2014-11-06 14:55:02 +08:00
|
|
|
unsigned FunctionIdent = 0;
|
2016-04-15 23:57:41 +08:00
|
|
|
for (auto &F : M->functions()) {
|
|
|
|
DISubprogram *SP = F.getSubprogram();
|
2020-06-08 02:25:40 +08:00
|
|
|
unsigned EndLine;
|
2016-04-15 23:57:41 +08:00
|
|
|
if (!SP) continue;
|
2020-06-08 02:25:40 +08:00
|
|
|
if (!functionHasLines(F, EndLine) || !isFunctionInstrumented(F))
|
2018-11-12 17:01:43 +08:00
|
|
|
continue;
|
[WebAssembly] Add Wasm personality and isScopedEHPersonality()
Summary:
- Add wasm personality function
- Re-categorize the existing `isFuncletEHPersonality()` function into
two different functions: `isFuncletEHPersonality()` and
`isScopedEHPersonality(). This becomes necessary as wasm EH uses scoped
EH instructions (catchswitch, catchpad/ret, and cleanuppad/ret) but not
outlined funclets.
- Changed some callsites of `isFuncletEHPersonality()` to
`isScopedEHPersonality()` if they are related to scoped EH IR-level
stuff.
Reviewers: majnemer, dschuff, rnk
Subscribers: jfb, sbc100, jgravelle-google, eraman, JDevlieghere, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D45559
llvm-svn: 332667
2018-05-18 04:52:03 +08:00
|
|
|
// TODO: Functions using scope-based EH are currently not supported.
|
|
|
|
if (isUsingScopeBasedEH(F)) continue;
|
2021-06-19 04:58:34 +08:00
|
|
|
if (F.hasFnAttribute(llvm::Attribute::NoProfile))
|
|
|
|
continue;
|
2014-01-31 13:24:01 +08:00
|
|
|
|
2020-09-13 15:07:31 +08:00
|
|
|
// Add the function line number to the lines of the entry block
|
|
|
|
// to have a counter for the function definition.
|
|
|
|
uint32_t Line = SP->getLine();
|
|
|
|
auto Filename = getFilename(SP);
|
|
|
|
|
|
|
|
BranchProbabilityInfo *BPI = GetBPI(F);
|
|
|
|
BlockFrequencyInfo *BFI = GetBFI(F);
|
2013-11-23 07:07:45 +08:00
|
|
|
|
2020-09-13 15:07:31 +08:00
|
|
|
// Split indirectbr critical edges here before computing the MST rather
|
|
|
|
// than later in getInstrBB() to avoid invalidating it.
|
|
|
|
SplitIndirectBrCriticalEdges(F, BPI, BFI);
|
|
|
|
|
|
|
|
CFGMST<Edge, BBInfo> MST(F, /*InstrumentFuncEntry_=*/false, BPI, BFI);
|
|
|
|
|
|
|
|
// getInstrBB can split basic blocks and push elements to AllEdges.
|
|
|
|
for (size_t I : llvm::seq<size_t>(0, MST.AllEdges.size())) {
|
|
|
|
auto &E = *MST.AllEdges[I];
|
|
|
|
// For now, disable spanning tree optimization when fork or exec* is
|
|
|
|
// used.
|
|
|
|
if (HasExecOrFork)
|
|
|
|
E.InMST = false;
|
|
|
|
E.Place = getInstrBB(MST, E, ExecBlocks);
|
|
|
|
}
|
|
|
|
// Basic blocks in F are finalized at this point.
|
|
|
|
BasicBlock &EntryBlock = F.getEntryBlock();
|
2020-09-13 03:34:43 +08:00
|
|
|
Funcs.push_back(std::make_unique<GCOVFunction>(this, &F, SP, EndLine,
|
2020-06-08 02:25:40 +08:00
|
|
|
FunctionIdent++, Version));
|
2014-04-22 04:41:55 +08:00
|
|
|
GCOVFunction &Func = *Funcs.back();
|
2011-11-28 07:22:20 +08:00
|
|
|
|
2020-09-13 15:07:31 +08:00
|
|
|
// Some non-tree edges are IndirectBr which cannot be split. Ignore them
|
|
|
|
// as well.
|
|
|
|
llvm::erase_if(MST.AllEdges, [](std::unique_ptr<Edge> &E) {
|
|
|
|
return E->Removed || (!E->InMST && !E->Place);
|
|
|
|
});
|
|
|
|
const size_t Measured =
|
2020-10-06 03:52:32 +08:00
|
|
|
std::stable_partition(
|
|
|
|
MST.AllEdges.begin(), MST.AllEdges.end(),
|
|
|
|
[](std::unique_ptr<Edge> &E) { return E->Place; }) -
|
2020-09-13 15:07:31 +08:00
|
|
|
MST.AllEdges.begin();
|
|
|
|
for (size_t I : llvm::seq<size_t>(0, Measured)) {
|
|
|
|
Edge &E = *MST.AllEdges[I];
|
|
|
|
GCOVBlock &Src =
|
|
|
|
E.SrcBB ? Func.getBlock(E.SrcBB) : Func.getEntryBlock();
|
|
|
|
GCOVBlock &Dst =
|
|
|
|
E.DestBB ? Func.getBlock(E.DestBB) : Func.getReturnBlock();
|
|
|
|
E.SrcNumber = Src.Number;
|
|
|
|
E.DstNumber = Dst.Number;
|
|
|
|
}
|
|
|
|
std::stable_sort(
|
|
|
|
MST.AllEdges.begin(), MST.AllEdges.begin() + Measured,
|
|
|
|
[](const std::unique_ptr<Edge> &L, const std::unique_ptr<Edge> &R) {
|
|
|
|
return L->SrcNumber != R->SrcNumber ? L->SrcNumber < R->SrcNumber
|
|
|
|
: L->DstNumber < R->DstNumber;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const Edge &E : make_pointee_range(MST.AllEdges)) {
|
|
|
|
GCOVBlock &Src =
|
|
|
|
E.SrcBB ? Func.getBlock(E.SrcBB) : Func.getEntryBlock();
|
|
|
|
GCOVBlock &Dst =
|
|
|
|
E.DestBB ? Func.getBlock(E.DestBB) : Func.getReturnBlock();
|
|
|
|
Src.addEdge(Dst, E.Place ? 0 : uint32_t(GCOV_ARC_ON_TREE));
|
|
|
|
}
|
2019-11-16 03:23:05 +08:00
|
|
|
|
|
|
|
// Artificial functions such as global initializers
|
|
|
|
if (!SP->isArtificial())
|
|
|
|
Func.getBlock(&EntryBlock).getFile(Filename).addLine(Line);
|
2018-10-11 16:53:43 +08:00
|
|
|
|
2020-09-13 15:07:31 +08:00
|
|
|
LLVM_DEBUG(dumpEdges(MST, Func));
|
|
|
|
|
|
|
|
for (auto &GB : Func.Blocks) {
|
|
|
|
const BasicBlock &BB = *GB.first;
|
|
|
|
auto &Block = GB.second;
|
|
|
|
for (auto Succ : Block.OutEdges) {
|
|
|
|
uint32_t Idx = Succ.first->Number;
|
2020-07-27 12:14:20 +08:00
|
|
|
do EdgeDestinations.push_back(Idx & 255);
|
|
|
|
while ((Idx >>= 8) > 0);
|
|
|
|
}
|
2011-11-28 07:22:20 +08:00
|
|
|
|
2016-04-15 23:57:41 +08:00
|
|
|
for (auto &I : BB) {
|
2014-06-05 05:47:19 +08:00
|
|
|
// Debug intrinsic locations correspond to the location of the
|
|
|
|
// declaration, not necessarily any statements or expressions.
|
2016-04-15 23:57:41 +08:00
|
|
|
if (isa<DbgInfoIntrinsic>(&I)) continue;
|
2014-06-05 12:31:43 +08:00
|
|
|
|
2016-04-15 23:57:41 +08:00
|
|
|
const DebugLoc &Loc = I.getDebugLoc();
|
2015-03-31 03:49:49 +08:00
|
|
|
if (!Loc)
|
|
|
|
continue;
|
2014-06-05 12:31:43 +08:00
|
|
|
|
|
|
|
// Artificial lines such as calls to the global constructors.
|
[IR] Add a boolean field in DILocation to know if a line must covered or not
Summary:
Some lines have a hit counter where they should not have one.
For example, in C++, some cleanup is adding at the end of a scope represented by a '}'.
So such a line has a hit counter where a user expects to not have one.
The goal of the patch is to add this information in DILocation which is used to get the covered lines in GCOVProfiling.cpp.
A following patch in clang will add this information when generating IR (https://reviews.llvm.org/D49916).
Reviewers: marco-c, davidxl, vsk, javed.absar, rnk
Reviewed By: rnk
Subscribers: eraman, xur, danielcdh, aprantl, rnk, dblaikie, #debug-info, vsk, llvm-commits, sylvestre.ledru
Tags: #debug-info
Differential Revision: https://reviews.llvm.org/D49915
llvm-svn: 342631
2018-09-20 16:53:06 +08:00
|
|
|
if (Loc.getLine() == 0 || Loc.isImplicitCode())
|
|
|
|
continue;
|
2014-06-05 12:31:43 +08:00
|
|
|
|
2011-11-28 07:22:20 +08:00
|
|
|
if (Line == Loc.getLine()) continue;
|
|
|
|
Line = Loc.getLine();
|
2015-03-31 03:49:49 +08:00
|
|
|
if (SP != getDISubprogram(Loc.getScope()))
|
|
|
|
continue;
|
2011-11-28 07:22:20 +08:00
|
|
|
|
2018-12-07 02:44:48 +08:00
|
|
|
GCOVLines &Lines = Block.getFile(Filename);
|
2011-11-28 07:22:20 +08:00
|
|
|
Lines.addLine(Loc.getLine());
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
2018-10-31 02:41:31 +08:00
|
|
|
Line = 0;
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
2020-09-13 15:07:31 +08:00
|
|
|
if (EmitGCDA) {
|
|
|
|
DISubprogram *SP = F.getSubprogram();
|
|
|
|
ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(*Ctx), Measured);
|
|
|
|
GlobalVariable *Counters = new GlobalVariable(
|
|
|
|
*M, CounterTy, false, GlobalValue::InternalLinkage,
|
|
|
|
Constant::getNullValue(CounterTy), "__llvm_gcov_ctr");
|
|
|
|
CountersBySP.emplace_back(Counters, SP);
|
|
|
|
|
|
|
|
for (size_t I : llvm::seq<size_t>(0, Measured)) {
|
|
|
|
const Edge &E = *MST.AllEdges[I];
|
|
|
|
IRBuilder<> Builder(E.Place, E.Place->getFirstInsertionPt());
|
|
|
|
Value *V = Builder.CreateConstInBoundsGEP2_64(
|
|
|
|
Counters->getValueType(), Counters, 0, I);
|
|
|
|
if (Options.Atomic) {
|
|
|
|
Builder.CreateAtomicRMW(AtomicRMWInst::Add, V, Builder.getInt64(1),
|
2021-02-09 12:07:12 +08:00
|
|
|
MaybeAlign(), AtomicOrdering::Monotonic);
|
2020-09-13 15:07:31 +08:00
|
|
|
} else {
|
|
|
|
Value *Count =
|
|
|
|
Builder.CreateLoad(Builder.getInt64Ty(), V, "gcov_ctr");
|
|
|
|
Count = Builder.CreateAdd(Count, Builder.getInt64(1));
|
|
|
|
Builder.CreateStore(Count, V);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
2013-11-20 12:15:05 +08:00
|
|
|
|
2020-06-07 02:01:47 +08:00
|
|
|
char Tmp[4];
|
2020-07-27 12:14:20 +08:00
|
|
|
JamCRC JC;
|
|
|
|
JC.update(EdgeDestinations);
|
|
|
|
uint32_t Stamp = JC.getCRC();
|
2020-06-08 02:25:40 +08:00
|
|
|
FileChecksums.push_back(Stamp);
|
2020-09-13 04:51:53 +08:00
|
|
|
|
|
|
|
if (Options.EmitNotes) {
|
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream out(mangleName(CU, GCovFileType::GCNO), EC,
|
|
|
|
sys::fs::OF_None);
|
|
|
|
if (EC) {
|
|
|
|
Ctx->emitError(
|
|
|
|
Twine("failed to open coverage notes file for writing: ") +
|
|
|
|
EC.message());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
os = &out;
|
|
|
|
if (Endian == support::endianness::big) {
|
|
|
|
out.write("gcno", 4);
|
|
|
|
out.write(Options.Version, 4);
|
|
|
|
} else {
|
|
|
|
out.write("oncg", 4);
|
|
|
|
std::reverse_copy(Options.Version, Options.Version + 4, Tmp);
|
|
|
|
out.write(Tmp, 4);
|
|
|
|
}
|
|
|
|
write(Stamp);
|
|
|
|
if (Version >= 90)
|
|
|
|
writeString(""); // unuseful current_working_directory
|
|
|
|
if (Version >= 80)
|
|
|
|
write(0); // unuseful has_unexecuted_blocks
|
|
|
|
|
|
|
|
for (auto &Func : Funcs)
|
|
|
|
Func->writeOut(Stamp);
|
|
|
|
|
|
|
|
write(0);
|
|
|
|
write(0);
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EmitGCDA) {
|
|
|
|
emitGlobalConstructor(CountersBySP);
|
|
|
|
EmitGCDA = false;
|
2020-06-07 02:01:47 +08:00
|
|
|
}
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
2020-09-13 15:07:31 +08:00
|
|
|
return true;
|
2020-09-13 04:51:53 +08:00
|
|
|
}
|
2012-05-28 14:10:56 +08:00
|
|
|
|
2021-04-27 04:30:20 +08:00
|
|
|
Function *GCOVProfiler::createInternalFunction(FunctionType *FTy,
|
|
|
|
StringRef Name) {
|
|
|
|
Function *F = Function::createWithDefaultAttr(
|
|
|
|
FTy, GlobalValue::InternalLinkage, 0, Name, M);
|
|
|
|
F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
|
|
|
|
F->addFnAttr(Attribute::NoUnwind);
|
|
|
|
if (Options.NoRedZone)
|
|
|
|
F->addFnAttr(Attribute::NoRedZone);
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
2020-09-13 04:51:53 +08:00
|
|
|
void GCOVProfiler::emitGlobalConstructor(
|
|
|
|
SmallVectorImpl<std::pair<GlobalVariable *, MDNode *>> &CountersBySP) {
|
|
|
|
Function *WriteoutF = insertCounterWriteout(CountersBySP);
|
|
|
|
Function *ResetF = insertReset(CountersBySP);
|
|
|
|
|
|
|
|
// Create a small bit of code that registers the "__llvm_gcov_writeout" to
|
2021-04-27 04:30:20 +08:00
|
|
|
// be executed at exit and the "__llvm_gcov_reset" function to be executed
|
2020-09-13 04:51:53 +08:00
|
|
|
// when "__gcov_flush" is called.
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
2021-04-27 04:30:20 +08:00
|
|
|
Function *F = createInternalFunction(FTy, "__llvm_gcov_init");
|
2020-09-13 04:51:53 +08:00
|
|
|
F->addFnAttr(Attribute::NoInline);
|
|
|
|
|
|
|
|
BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
|
|
|
|
IRBuilder<> Builder(BB);
|
|
|
|
|
|
|
|
FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
|
|
|
auto *PFTy = PointerType::get(FTy, 0);
|
|
|
|
FTy = FunctionType::get(Builder.getVoidTy(), {PFTy, PFTy}, false);
|
|
|
|
|
|
|
|
// Initialize the environment and register the local writeout, flush and
|
|
|
|
// reset functions.
|
|
|
|
FunctionCallee GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy);
|
|
|
|
Builder.CreateCall(GCOVInit, {WriteoutF, ResetF});
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
|
|
|
|
appendToGlobalCtors(*M, F, 0);
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
FunctionCallee GCOVProfiler::getStartFileFunc(const TargetLibraryInfo *TLI) {
|
2013-03-07 16:28:49 +08:00
|
|
|
Type *Args[] = {
|
2020-06-07 02:01:47 +08:00
|
|
|
Type::getInt8PtrTy(*Ctx), // const char *orig_filename
|
|
|
|
Type::getInt32Ty(*Ctx), // uint32_t version
|
|
|
|
Type::getInt32Ty(*Ctx), // uint32_t checksum
|
2013-03-07 16:28:49 +08:00
|
|
|
};
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
AttributeList AL;
|
|
|
|
if (auto AK = TLI->getExtAttrForI32Param(false))
|
|
|
|
AL = AL.addParamAttribute(*Ctx, 2, AK);
|
|
|
|
FunctionCallee Res = M->getOrInsertFunction("llvm_gcda_start_file", FTy, AL);
|
2018-07-11 00:05:47 +08:00
|
|
|
return Res;
|
2011-04-26 11:54:16 +08:00
|
|
|
}
|
|
|
|
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
FunctionCallee GCOVProfiler::getEmitFunctionFunc(const TargetLibraryInfo *TLI) {
|
2013-11-20 12:15:05 +08:00
|
|
|
Type *Args[] = {
|
2011-05-05 10:46:38 +08:00
|
|
|
Type::getInt32Ty(*Ctx), // uint32_t ident
|
2013-12-04 16:57:17 +08:00
|
|
|
Type::getInt32Ty(*Ctx), // uint32_t func_checksum
|
2013-11-20 12:15:05 +08:00
|
|
|
Type::getInt32Ty(*Ctx), // uint32_t cfg_checksum
|
2011-05-05 10:46:38 +08:00
|
|
|
};
|
2012-05-26 07:55:00 +08:00
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
AttributeList AL;
|
|
|
|
if (auto AK = TLI->getExtAttrForI32Param(false)) {
|
|
|
|
AL = AL.addParamAttribute(*Ctx, 0, AK);
|
2020-05-11 03:47:45 +08:00
|
|
|
AL = AL.addParamAttribute(*Ctx, 1, AK);
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
AL = AL.addParamAttribute(*Ctx, 2, AK);
|
|
|
|
}
|
|
|
|
return M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
FunctionCallee GCOVProfiler::getEmitArcsFunc(const TargetLibraryInfo *TLI) {
|
2011-07-12 22:06:48 +08:00
|
|
|
Type *Args[] = {
|
2011-04-16 09:20:23 +08:00
|
|
|
Type::getInt32Ty(*Ctx), // uint32_t num_counters
|
|
|
|
Type::getInt64PtrTy(*Ctx), // uint64_t *counters
|
|
|
|
};
|
2013-03-07 16:28:49 +08:00
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
AttributeList AL;
|
|
|
|
if (auto AK = TLI->getExtAttrForI32Param(false))
|
|
|
|
AL = AL.addParamAttribute(*Ctx, 0, AK);
|
|
|
|
return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy, AL);
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
FunctionCallee GCOVProfiler::getSummaryInfoFunc() {
|
2013-11-12 12:59:08 +08:00
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
|
|
|
return M->getOrInsertFunction("llvm_gcda_summary_info", FTy);
|
|
|
|
}
|
|
|
|
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
FunctionCallee GCOVProfiler::getEndFileFunc() {
|
2011-07-18 12:54:35 +08:00
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
2011-04-26 11:54:16 +08:00
|
|
|
return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
2013-03-19 07:04:39 +08:00
|
|
|
Function *GCOVProfiler::insertCounterWriteout(
|
2012-08-30 02:45:41 +08:00
|
|
|
ArrayRef<std::pair<GlobalVariable *, MDNode *> > CountersBySP) {
|
2012-09-13 08:09:55 +08:00
|
|
|
FunctionType *WriteoutFTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
|
|
|
Function *WriteoutF = M->getFunction("__llvm_gcov_writeout");
|
|
|
|
if (!WriteoutF)
|
2021-04-27 04:30:20 +08:00
|
|
|
WriteoutF = createInternalFunction(WriteoutFTy, "__llvm_gcov_writeout");
|
2012-12-19 15:18:57 +08:00
|
|
|
WriteoutF->addFnAttr(Attribute::NoInline);
|
2012-09-13 08:09:55 +08:00
|
|
|
|
|
|
|
BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF);
|
2011-04-26 11:54:16 +08:00
|
|
|
IRBuilder<> Builder(BB);
|
2011-04-16 09:20:23 +08:00
|
|
|
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
auto *TLI = &GetTLI(*WriteoutF);
|
|
|
|
|
|
|
|
FunctionCallee StartFile = getStartFileFunc(TLI);
|
|
|
|
FunctionCallee EmitFunction = getEmitFunctionFunc(TLI);
|
|
|
|
FunctionCallee EmitArcs = getEmitArcsFunc(TLI);
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
FunctionCallee SummaryInfo = getSummaryInfoFunc();
|
|
|
|
FunctionCallee EndFile = getEndFileFunc();
|
2011-04-16 09:20:23 +08:00
|
|
|
|
2018-05-03 06:24:39 +08:00
|
|
|
NamedMDNode *CUNodes = M->getNamedMetadata("llvm.dbg.cu");
|
|
|
|
if (!CUNodes) {
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
return WriteoutF;
|
|
|
|
}
|
2016-01-22 01:04:42 +08:00
|
|
|
|
2018-05-03 06:24:39 +08:00
|
|
|
// Collect the relevant data into a large constant data structure that we can
|
|
|
|
// walk to write out everything.
|
|
|
|
StructType *StartFileCallArgsTy = StructType::create(
|
2020-09-13 13:42:37 +08:00
|
|
|
{Builder.getInt8PtrTy(), Builder.getInt32Ty(), Builder.getInt32Ty()},
|
|
|
|
"start_file_args_ty");
|
2020-05-11 03:47:45 +08:00
|
|
|
StructType *EmitFunctionCallArgsTy = StructType::create(
|
2020-09-13 13:42:37 +08:00
|
|
|
{Builder.getInt32Ty(), Builder.getInt32Ty(), Builder.getInt32Ty()},
|
|
|
|
"emit_function_args_ty");
|
2018-05-03 06:24:39 +08:00
|
|
|
StructType *EmitArcsCallArgsTy = StructType::create(
|
2020-09-13 13:42:37 +08:00
|
|
|
{Builder.getInt32Ty(), Builder.getInt64Ty()->getPointerTo()},
|
|
|
|
"emit_arcs_args_ty");
|
2018-05-03 06:24:39 +08:00
|
|
|
StructType *FileInfoTy =
|
|
|
|
StructType::create({StartFileCallArgsTy, Builder.getInt32Ty(),
|
|
|
|
EmitFunctionCallArgsTy->getPointerTo(),
|
2020-09-13 13:42:37 +08:00
|
|
|
EmitArcsCallArgsTy->getPointerTo()},
|
|
|
|
"file_info");
|
2018-05-03 06:24:39 +08:00
|
|
|
|
|
|
|
Constant *Zero32 = Builder.getInt32(0);
|
2018-05-03 08:11:03 +08:00
|
|
|
// Build an explicit array of two zeros for use in ConstantExpr GEP building.
|
|
|
|
Constant *TwoZero32s[] = {Zero32, Zero32};
|
2018-05-03 06:24:39 +08:00
|
|
|
|
|
|
|
SmallVector<Constant *, 8> FileInfos;
|
|
|
|
for (int i : llvm::seq<int>(0, CUNodes->getNumOperands())) {
|
|
|
|
auto *CU = cast<DICompileUnit>(CUNodes->getOperand(i));
|
2016-01-22 01:04:42 +08:00
|
|
|
|
2018-05-03 06:24:39 +08:00
|
|
|
// Skip module skeleton (and module) CUs.
|
|
|
|
if (CU->getDWOId())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string FilenameGcda = mangleName(CU, GCovFileType::GCDA);
|
|
|
|
uint32_t CfgChecksum = FileChecksums.empty() ? 0 : FileChecksums[i];
|
|
|
|
auto *StartFileCallArgs = ConstantStruct::get(
|
2020-06-07 02:01:47 +08:00
|
|
|
StartFileCallArgsTy,
|
|
|
|
{Builder.CreateGlobalStringPtr(FilenameGcda),
|
|
|
|
Builder.getInt32(endian::read32be(Options.Version)),
|
|
|
|
Builder.getInt32(CfgChecksum)});
|
2018-05-03 06:24:39 +08:00
|
|
|
|
|
|
|
SmallVector<Constant *, 8> EmitFunctionCallArgsArray;
|
|
|
|
SmallVector<Constant *, 8> EmitArcsCallArgsArray;
|
|
|
|
for (int j : llvm::seq<int>(0, CountersBySP.size())) {
|
|
|
|
uint32_t FuncChecksum = Funcs.empty() ? 0 : Funcs[j]->getFuncChecksum();
|
|
|
|
EmitFunctionCallArgsArray.push_back(ConstantStruct::get(
|
|
|
|
EmitFunctionCallArgsTy,
|
|
|
|
{Builder.getInt32(j),
|
|
|
|
Builder.getInt32(FuncChecksum),
|
|
|
|
Builder.getInt32(CfgChecksum)}));
|
|
|
|
|
|
|
|
GlobalVariable *GV = CountersBySP[j].first;
|
|
|
|
unsigned Arcs = cast<ArrayType>(GV->getValueType())->getNumElements();
|
|
|
|
EmitArcsCallArgsArray.push_back(ConstantStruct::get(
|
|
|
|
EmitArcsCallArgsTy,
|
2018-05-03 08:11:03 +08:00
|
|
|
{Builder.getInt32(Arcs), ConstantExpr::getInBoundsGetElementPtr(
|
|
|
|
GV->getValueType(), GV, TwoZero32s)}));
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
2018-05-03 06:24:39 +08:00
|
|
|
// Create global arrays for the two emit calls.
|
|
|
|
int CountersSize = CountersBySP.size();
|
|
|
|
assert(CountersSize == (int)EmitFunctionCallArgsArray.size() &&
|
|
|
|
"Mismatched array size!");
|
|
|
|
assert(CountersSize == (int)EmitArcsCallArgsArray.size() &&
|
|
|
|
"Mismatched array size!");
|
|
|
|
auto *EmitFunctionCallArgsArrayTy =
|
|
|
|
ArrayType::get(EmitFunctionCallArgsTy, CountersSize);
|
|
|
|
auto *EmitFunctionCallArgsArrayGV = new GlobalVariable(
|
|
|
|
*M, EmitFunctionCallArgsArrayTy, /*isConstant*/ true,
|
|
|
|
GlobalValue::InternalLinkage,
|
|
|
|
ConstantArray::get(EmitFunctionCallArgsArrayTy,
|
|
|
|
EmitFunctionCallArgsArray),
|
|
|
|
Twine("__llvm_internal_gcov_emit_function_args.") + Twine(i));
|
|
|
|
auto *EmitArcsCallArgsArrayTy =
|
|
|
|
ArrayType::get(EmitArcsCallArgsTy, CountersSize);
|
|
|
|
EmitFunctionCallArgsArrayGV->setUnnamedAddr(
|
|
|
|
GlobalValue::UnnamedAddr::Global);
|
|
|
|
auto *EmitArcsCallArgsArrayGV = new GlobalVariable(
|
|
|
|
*M, EmitArcsCallArgsArrayTy, /*isConstant*/ true,
|
|
|
|
GlobalValue::InternalLinkage,
|
|
|
|
ConstantArray::get(EmitArcsCallArgsArrayTy, EmitArcsCallArgsArray),
|
|
|
|
Twine("__llvm_internal_gcov_emit_arcs_args.") + Twine(i));
|
|
|
|
EmitArcsCallArgsArrayGV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
|
|
|
|
|
|
|
|
FileInfos.push_back(ConstantStruct::get(
|
|
|
|
FileInfoTy,
|
|
|
|
{StartFileCallArgs, Builder.getInt32(CountersSize),
|
2018-05-03 08:11:03 +08:00
|
|
|
ConstantExpr::getInBoundsGetElementPtr(EmitFunctionCallArgsArrayTy,
|
|
|
|
EmitFunctionCallArgsArrayGV,
|
|
|
|
TwoZero32s),
|
2018-05-03 06:24:39 +08:00
|
|
|
ConstantExpr::getInBoundsGetElementPtr(
|
2018-05-03 08:11:03 +08:00
|
|
|
EmitArcsCallArgsArrayTy, EmitArcsCallArgsArrayGV, TwoZero32s)}));
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
|
|
|
|
2018-05-03 06:24:39 +08:00
|
|
|
// If we didn't find anything to actually emit, bail on out.
|
|
|
|
if (FileInfos.empty()) {
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
return WriteoutF;
|
|
|
|
}
|
|
|
|
|
|
|
|
// To simplify code, we cap the number of file infos we write out to fit
|
|
|
|
// easily in a 32-bit signed integer. This gives consistent behavior between
|
|
|
|
// 32-bit and 64-bit systems without requiring (potentially very slow) 64-bit
|
|
|
|
// operations on 32-bit systems. It also seems unreasonable to try to handle
|
|
|
|
// more than 2 billion files.
|
|
|
|
if ((int64_t)FileInfos.size() > (int64_t)INT_MAX)
|
|
|
|
FileInfos.resize(INT_MAX);
|
|
|
|
|
|
|
|
// Create a global for the entire data structure so we can walk it more
|
|
|
|
// easily.
|
|
|
|
auto *FileInfoArrayTy = ArrayType::get(FileInfoTy, FileInfos.size());
|
|
|
|
auto *FileInfoArrayGV = new GlobalVariable(
|
|
|
|
*M, FileInfoArrayTy, /*isConstant*/ true, GlobalValue::InternalLinkage,
|
|
|
|
ConstantArray::get(FileInfoArrayTy, FileInfos),
|
|
|
|
"__llvm_internal_gcov_emit_file_info");
|
|
|
|
FileInfoArrayGV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
|
|
|
|
|
|
|
|
// Create the CFG for walking this data structure.
|
|
|
|
auto *FileLoopHeader =
|
|
|
|
BasicBlock::Create(*Ctx, "file.loop.header", WriteoutF);
|
|
|
|
auto *CounterLoopHeader =
|
|
|
|
BasicBlock::Create(*Ctx, "counter.loop.header", WriteoutF);
|
|
|
|
auto *FileLoopLatch = BasicBlock::Create(*Ctx, "file.loop.latch", WriteoutF);
|
|
|
|
auto *ExitBB = BasicBlock::Create(*Ctx, "exit", WriteoutF);
|
|
|
|
|
|
|
|
// We always have at least one file, so just branch to the header.
|
|
|
|
Builder.CreateBr(FileLoopHeader);
|
|
|
|
|
|
|
|
// The index into the files structure is our loop induction variable.
|
|
|
|
Builder.SetInsertPoint(FileLoopHeader);
|
2020-09-13 13:42:37 +08:00
|
|
|
PHINode *IV = Builder.CreatePHI(Builder.getInt32Ty(), /*NumReservedValues*/ 2,
|
|
|
|
"file_idx");
|
2018-05-03 06:24:39 +08:00
|
|
|
IV->addIncoming(Builder.getInt32(0), BB);
|
2019-02-02 04:44:47 +08:00
|
|
|
auto *FileInfoPtr = Builder.CreateInBoundsGEP(
|
|
|
|
FileInfoArrayTy, FileInfoArrayGV, {Builder.getInt32(0), IV});
|
|
|
|
auto *StartFileCallArgsPtr =
|
2020-09-13 13:42:37 +08:00
|
|
|
Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 0, "start_file_args");
|
2018-07-11 00:05:47 +08:00
|
|
|
auto *StartFileCall = Builder.CreateCall(
|
2018-05-03 06:24:39 +08:00
|
|
|
StartFile,
|
2019-02-02 04:44:24 +08:00
|
|
|
{Builder.CreateLoad(StartFileCallArgsTy->getElementType(0),
|
2019-02-02 04:44:47 +08:00
|
|
|
Builder.CreateStructGEP(StartFileCallArgsTy,
|
2020-09-13 13:42:37 +08:00
|
|
|
StartFileCallArgsPtr, 0),
|
|
|
|
"filename"),
|
2019-02-02 04:44:24 +08:00
|
|
|
Builder.CreateLoad(StartFileCallArgsTy->getElementType(1),
|
2019-02-02 04:44:47 +08:00
|
|
|
Builder.CreateStructGEP(StartFileCallArgsTy,
|
2020-09-13 13:42:37 +08:00
|
|
|
StartFileCallArgsPtr, 1),
|
|
|
|
"version"),
|
2019-02-02 04:44:24 +08:00
|
|
|
Builder.CreateLoad(StartFileCallArgsTy->getElementType(2),
|
2019-02-02 04:44:47 +08:00
|
|
|
Builder.CreateStructGEP(StartFileCallArgsTy,
|
2020-09-13 13:42:37 +08:00
|
|
|
StartFileCallArgsPtr, 2),
|
|
|
|
"stamp")});
|
2018-07-11 00:05:47 +08:00
|
|
|
if (auto AK = TLI->getExtAttrForI32Param(false))
|
|
|
|
StartFileCall->addParamAttr(2, AK);
|
2020-09-13 13:42:37 +08:00
|
|
|
auto *NumCounters = Builder.CreateLoad(
|
|
|
|
FileInfoTy->getElementType(1),
|
|
|
|
Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 1), "num_ctrs");
|
2019-02-02 04:44:47 +08:00
|
|
|
auto *EmitFunctionCallArgsArray =
|
|
|
|
Builder.CreateLoad(FileInfoTy->getElementType(2),
|
2020-09-13 13:42:37 +08:00
|
|
|
Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 2),
|
|
|
|
"emit_function_args");
|
|
|
|
auto *EmitArcsCallArgsArray = Builder.CreateLoad(
|
|
|
|
FileInfoTy->getElementType(3),
|
|
|
|
Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 3), "emit_arcs_args");
|
2018-05-03 06:24:39 +08:00
|
|
|
auto *EnterCounterLoopCond =
|
|
|
|
Builder.CreateICmpSLT(Builder.getInt32(0), NumCounters);
|
|
|
|
Builder.CreateCondBr(EnterCounterLoopCond, CounterLoopHeader, FileLoopLatch);
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(CounterLoopHeader);
|
2020-09-13 13:42:37 +08:00
|
|
|
auto *JV = Builder.CreatePHI(Builder.getInt32Ty(), /*NumReservedValues*/ 2,
|
|
|
|
"ctr_idx");
|
2018-05-03 06:24:39 +08:00
|
|
|
JV->addIncoming(Builder.getInt32(0), FileLoopHeader);
|
2019-02-02 04:44:47 +08:00
|
|
|
auto *EmitFunctionCallArgsPtr = Builder.CreateInBoundsGEP(
|
|
|
|
EmitFunctionCallArgsTy, EmitFunctionCallArgsArray, JV);
|
2018-07-11 00:05:47 +08:00
|
|
|
auto *EmitFunctionCall = Builder.CreateCall(
|
2018-05-03 06:24:39 +08:00
|
|
|
EmitFunction,
|
2019-02-02 04:44:24 +08:00
|
|
|
{Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(0),
|
2019-02-02 04:44:47 +08:00
|
|
|
Builder.CreateStructGEP(EmitFunctionCallArgsTy,
|
2020-09-13 13:42:37 +08:00
|
|
|
EmitFunctionCallArgsPtr, 0),
|
|
|
|
"ident"),
|
2019-02-02 04:44:24 +08:00
|
|
|
Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(1),
|
2019-02-02 04:44:47 +08:00
|
|
|
Builder.CreateStructGEP(EmitFunctionCallArgsTy,
|
2020-09-13 13:42:37 +08:00
|
|
|
EmitFunctionCallArgsPtr, 1),
|
|
|
|
"func_checkssum"),
|
2019-02-02 04:44:24 +08:00
|
|
|
Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(2),
|
2019-02-02 04:44:47 +08:00
|
|
|
Builder.CreateStructGEP(EmitFunctionCallArgsTy,
|
2020-09-13 13:42:37 +08:00
|
|
|
EmitFunctionCallArgsPtr, 2),
|
|
|
|
"cfg_checksum")});
|
2018-07-11 00:05:47 +08:00
|
|
|
if (auto AK = TLI->getExtAttrForI32Param(false)) {
|
|
|
|
EmitFunctionCall->addParamAttr(0, AK);
|
2020-05-11 01:50:20 +08:00
|
|
|
EmitFunctionCall->addParamAttr(1, AK);
|
2018-07-11 00:05:47 +08:00
|
|
|
EmitFunctionCall->addParamAttr(2, AK);
|
|
|
|
}
|
2018-05-03 06:24:39 +08:00
|
|
|
auto *EmitArcsCallArgsPtr =
|
2019-02-02 04:44:47 +08:00
|
|
|
Builder.CreateInBoundsGEP(EmitArcsCallArgsTy, EmitArcsCallArgsArray, JV);
|
2018-07-11 00:05:47 +08:00
|
|
|
auto *EmitArcsCall = Builder.CreateCall(
|
2018-05-03 06:24:39 +08:00
|
|
|
EmitArcs,
|
2019-02-02 04:44:47 +08:00
|
|
|
{Builder.CreateLoad(
|
|
|
|
EmitArcsCallArgsTy->getElementType(0),
|
2020-09-13 13:42:37 +08:00
|
|
|
Builder.CreateStructGEP(EmitArcsCallArgsTy, EmitArcsCallArgsPtr, 0),
|
|
|
|
"num_counters"),
|
|
|
|
Builder.CreateLoad(
|
|
|
|
EmitArcsCallArgsTy->getElementType(1),
|
|
|
|
Builder.CreateStructGEP(EmitArcsCallArgsTy, EmitArcsCallArgsPtr, 1),
|
|
|
|
"counters")});
|
2018-07-11 00:05:47 +08:00
|
|
|
if (auto AK = TLI->getExtAttrForI32Param(false))
|
|
|
|
EmitArcsCall->addParamAttr(0, AK);
|
2018-05-03 06:24:39 +08:00
|
|
|
auto *NextJV = Builder.CreateAdd(JV, Builder.getInt32(1));
|
|
|
|
auto *CounterLoopCond = Builder.CreateICmpSLT(NextJV, NumCounters);
|
|
|
|
Builder.CreateCondBr(CounterLoopCond, CounterLoopHeader, FileLoopLatch);
|
|
|
|
JV->addIncoming(NextJV, CounterLoopHeader);
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(FileLoopLatch);
|
|
|
|
Builder.CreateCall(SummaryInfo, {});
|
|
|
|
Builder.CreateCall(EndFile, {});
|
2020-09-13 13:42:37 +08:00
|
|
|
auto *NextIV = Builder.CreateAdd(IV, Builder.getInt32(1), "next_file_idx");
|
2018-05-03 06:24:39 +08:00
|
|
|
auto *FileLoopCond =
|
|
|
|
Builder.CreateICmpSLT(NextIV, Builder.getInt32(FileInfos.size()));
|
|
|
|
Builder.CreateCondBr(FileLoopCond, FileLoopHeader, ExitBB);
|
|
|
|
IV->addIncoming(NextIV, FileLoopLatch);
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(ExitBB);
|
2012-06-02 07:14:32 +08:00
|
|
|
Builder.CreateRetVoid();
|
2018-05-03 06:24:39 +08:00
|
|
|
|
2013-03-19 07:04:39 +08:00
|
|
|
return WriteoutF;
|
2011-04-16 09:20:23 +08:00
|
|
|
}
|
2012-05-28 14:10:56 +08:00
|
|
|
|
[profile] Don't crash when forking in several threads
Summary:
When forking in several threads, the counters were written out in using the same global static variables (see GCDAProfiling.c): that leads to crashes.
So when there is a fork, the counters are resetted in the child process and they will be dumped at exit using the interprocess file locking.
When there is an exec, the counters are written out and in case of failures they're resetted.
Reviewers: jfb, vsk, marco-c, serge-sans-paille
Reviewed By: marco-c, serge-sans-paille
Subscribers: llvm-commits, serge-sans-paille, dmajor, cfe-commits, hiraditya, dexonsmith, #sanitizers, marco-c, sylvestre.ledru
Tags: #sanitizers, #clang, #llvm
Differential Revision: https://reviews.llvm.org/D78477
2020-04-20 16:50:30 +08:00
|
|
|
Function *GCOVProfiler::insertReset(
|
|
|
|
ArrayRef<std::pair<GlobalVariable *, MDNode *>> CountersBySP) {
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
|
|
|
Function *ResetF = M->getFunction("__llvm_gcov_reset");
|
|
|
|
if (!ResetF)
|
2021-04-27 04:30:20 +08:00
|
|
|
ResetF = createInternalFunction(FTy, "__llvm_gcov_reset");
|
[profile] Don't crash when forking in several threads
Summary:
When forking in several threads, the counters were written out in using the same global static variables (see GCDAProfiling.c): that leads to crashes.
So when there is a fork, the counters are resetted in the child process and they will be dumped at exit using the interprocess file locking.
When there is an exec, the counters are written out and in case of failures they're resetted.
Reviewers: jfb, vsk, marco-c, serge-sans-paille
Reviewed By: marco-c, serge-sans-paille
Subscribers: llvm-commits, serge-sans-paille, dmajor, cfe-commits, hiraditya, dexonsmith, #sanitizers, marco-c, sylvestre.ledru
Tags: #sanitizers, #clang, #llvm
Differential Revision: https://reviews.llvm.org/D78477
2020-04-20 16:50:30 +08:00
|
|
|
ResetF->addFnAttr(Attribute::NoInline);
|
|
|
|
|
|
|
|
BasicBlock *Entry = BasicBlock::Create(*Ctx, "entry", ResetF);
|
|
|
|
IRBuilder<> Builder(Entry);
|
2021-08-06 12:18:53 +08:00
|
|
|
LLVMContext &C = Entry->getContext();
|
[profile] Don't crash when forking in several threads
Summary:
When forking in several threads, the counters were written out in using the same global static variables (see GCDAProfiling.c): that leads to crashes.
So when there is a fork, the counters are resetted in the child process and they will be dumped at exit using the interprocess file locking.
When there is an exec, the counters are written out and in case of failures they're resetted.
Reviewers: jfb, vsk, marco-c, serge-sans-paille
Reviewed By: marco-c, serge-sans-paille
Subscribers: llvm-commits, serge-sans-paille, dmajor, cfe-commits, hiraditya, dexonsmith, #sanitizers, marco-c, sylvestre.ledru
Tags: #sanitizers, #clang, #llvm
Differential Revision: https://reviews.llvm.org/D78477
2020-04-20 16:50:30 +08:00
|
|
|
|
|
|
|
// Zero out the counters.
|
|
|
|
for (const auto &I : CountersBySP) {
|
|
|
|
GlobalVariable *GV = I.first;
|
2021-08-06 12:18:53 +08:00
|
|
|
auto *GVTy = cast<ArrayType>(GV->getValueType());
|
|
|
|
Builder.CreateMemSet(GV, Constant::getNullValue(Type::getInt8Ty(C)),
|
|
|
|
GVTy->getNumElements() *
|
|
|
|
GVTy->getElementType()->getScalarSizeInBits() / 8,
|
|
|
|
GV->getAlign());
|
[profile] Don't crash when forking in several threads
Summary:
When forking in several threads, the counters were written out in using the same global static variables (see GCDAProfiling.c): that leads to crashes.
So when there is a fork, the counters are resetted in the child process and they will be dumped at exit using the interprocess file locking.
When there is an exec, the counters are written out and in case of failures they're resetted.
Reviewers: jfb, vsk, marco-c, serge-sans-paille
Reviewed By: marco-c, serge-sans-paille
Subscribers: llvm-commits, serge-sans-paille, dmajor, cfe-commits, hiraditya, dexonsmith, #sanitizers, marco-c, sylvestre.ledru
Tags: #sanitizers, #clang, #llvm
Differential Revision: https://reviews.llvm.org/D78477
2020-04-20 16:50:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Type *RetTy = ResetF->getReturnType();
|
|
|
|
if (RetTy->isVoidTy())
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
else if (RetTy->isIntegerTy())
|
|
|
|
// Used if __llvm_gcov_reset was implicitly declared.
|
|
|
|
Builder.CreateRet(ConstantInt::get(RetTy, 0));
|
|
|
|
else
|
|
|
|
report_fatal_error("invalid return type for __llvm_gcov_reset");
|
|
|
|
|
|
|
|
return ResetF;
|
|
|
|
}
|