2021-12-22 02:21:41 +08:00
|
|
|
//===- bolt/Passes/Instrumentation.cpp ------------------------------------===//
|
2019-06-20 11:10:49 +08:00
|
|
|
//
|
2021-03-16 09:04:18 +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
|
2019-06-20 11:10:49 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2021-12-22 02:21:41 +08:00
|
|
|
// This file implements the Instrumentation class.
|
|
|
|
//
|
2019-06-20 11:10:49 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-10-09 02:47:10 +08:00
|
|
|
#include "bolt/Passes/Instrumentation.h"
|
|
|
|
#include "bolt/Core/ParallelUtilities.h"
|
|
|
|
#include "bolt/RuntimeLibs/InstrumentationRuntimeLibrary.h"
|
|
|
|
#include "bolt/Utils/Utils.h"
|
2020-12-02 08:29:39 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include <stack>
|
2019-06-20 11:10:49 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "bolt-instrumentation"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace opts {
|
2020-10-31 05:45:30 +08:00
|
|
|
extern cl::OptionCategory BoltInstrCategory;
|
2019-06-20 11:10:49 +08:00
|
|
|
|
|
|
|
cl::opt<std::string> InstrumentationFilename(
|
|
|
|
"instrumentation-file",
|
2020-10-31 05:45:30 +08:00
|
|
|
cl::desc("file name where instrumented profile will be saved (default: "
|
|
|
|
"/tmp/prof.fdata)"),
|
|
|
|
cl::init("/tmp/prof.fdata"), cl::Optional, cl::cat(BoltInstrCategory));
|
2019-07-03 07:56:41 +08:00
|
|
|
|
2021-07-30 23:07:53 +08:00
|
|
|
cl::opt<std::string> InstrumentationBinpath(
|
|
|
|
"instrumentation-binpath",
|
|
|
|
cl::desc("path to instumented binary in case if /proc/self/map_files "
|
|
|
|
"is not accessible due to access restriction issues"),
|
|
|
|
cl::Optional, cl::cat(BoltInstrCategory));
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
cl::opt<bool> InstrumentationFileAppendPID(
|
|
|
|
"instrumentation-file-append-pid",
|
|
|
|
cl::desc("append PID to saved profile file name (default: false)"),
|
2021-12-15 08:52:51 +08:00
|
|
|
cl::init(false), cl::Optional, cl::cat(BoltInstrCategory));
|
2019-12-14 09:27:03 +08:00
|
|
|
|
|
|
|
cl::opt<bool> ConservativeInstrumentation(
|
|
|
|
"conservative-instrumentation",
|
2021-08-08 04:50:06 +08:00
|
|
|
cl::desc("disable instrumentation optimizations that sacrifice profile "
|
|
|
|
"accuracy (for debugging, default: false)"),
|
2020-10-31 05:45:30 +08:00
|
|
|
cl::init(false), cl::Optional, cl::cat(BoltInstrCategory));
|
2019-12-14 09:27:03 +08:00
|
|
|
|
2020-10-31 05:45:30 +08:00
|
|
|
cl::opt<uint32_t> InstrumentationSleepTime(
|
|
|
|
"instrumentation-sleep-time",
|
|
|
|
cl::desc("interval between profile writes (default: 0 = write only at "
|
|
|
|
"program end). This is useful for service workloads when you "
|
|
|
|
"want to dump profile every X minutes or if you are killing the "
|
|
|
|
"program and the profile is not being dumped at the end."),
|
|
|
|
cl::init(0), cl::Optional, cl::cat(BoltInstrCategory));
|
2019-12-14 09:27:03 +08:00
|
|
|
|
2021-03-10 08:18:11 +08:00
|
|
|
cl::opt<bool> InstrumentationNoCountersClear(
|
|
|
|
"instrumentation-no-counters-clear",
|
|
|
|
cl::desc("Don't clear counters across dumps "
|
|
|
|
"(use with instrumentation-sleep-time option)"),
|
|
|
|
cl::init(false), cl::Optional, cl::cat(BoltInstrCategory));
|
|
|
|
|
|
|
|
cl::opt<bool> InstrumentationWaitForks(
|
|
|
|
"instrumentation-wait-forks",
|
|
|
|
cl::desc("Wait until all forks of instrumented process will finish "
|
|
|
|
"(use with instrumentation-sleep-time option)"),
|
|
|
|
cl::init(false), cl::Optional, cl::cat(BoltInstrCategory));
|
|
|
|
|
2020-10-31 05:45:30 +08:00
|
|
|
cl::opt<bool>
|
|
|
|
InstrumentHotOnly("instrument-hot-only",
|
|
|
|
cl::desc("only insert instrumentation on hot functions "
|
|
|
|
"(needs profile, default: false)"),
|
|
|
|
cl::init(false), cl::Optional,
|
|
|
|
cl::cat(BoltInstrCategory));
|
2019-08-08 07:09:50 +08:00
|
|
|
|
2020-10-31 05:45:30 +08:00
|
|
|
cl::opt<bool> InstrumentCalls("instrument-calls",
|
|
|
|
cl::desc("record profile for inter-function "
|
|
|
|
"control flow activity (default: true)"),
|
|
|
|
cl::init(true), cl::Optional,
|
|
|
|
cl::cat(BoltInstrCategory));
|
2021-12-15 08:52:51 +08:00
|
|
|
} // namespace opts
|
2019-06-20 11:10:49 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace bolt {
|
|
|
|
|
|
|
|
uint32_t Instrumentation::getFunctionNameIndex(const BinaryFunction &Function) {
|
|
|
|
auto Iter = FuncToStringIdx.find(&Function);
|
|
|
|
if (Iter != FuncToStringIdx.end())
|
|
|
|
return Iter->second;
|
2021-04-08 15:19:26 +08:00
|
|
|
size_t Idx = Summary->StringTable.size();
|
2019-06-20 11:10:49 +08:00
|
|
|
FuncToStringIdx.emplace(std::make_pair(&Function, Idx));
|
2021-06-30 00:54:08 +08:00
|
|
|
Summary->StringTable.append(getEscapedName(Function.getOneName()));
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
Summary->StringTable.append(1, '\0');
|
2019-06-20 11:10:49 +08:00
|
|
|
return Idx;
|
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
bool Instrumentation::createCallDescription(FunctionDescription &FuncDesc,
|
|
|
|
const BinaryFunction &FromFunction,
|
|
|
|
uint32_t From, uint32_t FromNodeID,
|
|
|
|
const BinaryFunction &ToFunction,
|
|
|
|
uint32_t To, bool IsInvoke) {
|
2019-08-08 07:09:50 +08:00
|
|
|
CallDescription CD;
|
2019-12-14 09:27:03 +08:00
|
|
|
// Ordinarily, we don't augment direct calls with an explicit counter, except
|
|
|
|
// when forced to do so or when we know this callee could be throwing
|
|
|
|
// exceptions, in which case there is no other way to accurately record its
|
|
|
|
// frequency.
|
|
|
|
bool ForceInstrumentation = opts::ConservativeInstrumentation || IsInvoke;
|
2019-08-08 07:09:50 +08:00
|
|
|
CD.FromLoc.FuncString = getFunctionNameIndex(FromFunction);
|
|
|
|
CD.FromLoc.Offset = From;
|
2019-12-14 09:27:03 +08:00
|
|
|
CD.FromNode = FromNodeID;
|
|
|
|
CD.Target = &ToFunction;
|
2019-08-08 07:09:50 +08:00
|
|
|
CD.ToLoc.FuncString = getFunctionNameIndex(ToFunction);
|
|
|
|
CD.ToLoc.Offset = To;
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
CD.Counter = ForceInstrumentation ? Summary->Counters.size() : 0xffffffff;
|
2019-12-14 09:27:03 +08:00
|
|
|
if (ForceInstrumentation)
|
|
|
|
++DirectCallCounters;
|
|
|
|
FuncDesc.Calls.emplace_back(CD);
|
|
|
|
return ForceInstrumentation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instrumentation::createIndCallDescription(
|
|
|
|
const BinaryFunction &FromFunction, uint32_t From) {
|
|
|
|
IndCallDescription ICD;
|
|
|
|
ICD.FromLoc.FuncString = getFunctionNameIndex(FromFunction);
|
|
|
|
ICD.FromLoc.Offset = From;
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
Summary->IndCallDescriptions.emplace_back(ICD);
|
2019-12-14 09:27:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instrumentation::createIndCallTargetDescription(
|
|
|
|
const BinaryFunction &ToFunction, uint32_t To) {
|
|
|
|
IndCallTargetDescription ICD;
|
|
|
|
ICD.ToLoc.FuncString = getFunctionNameIndex(ToFunction);
|
|
|
|
ICD.ToLoc.Offset = To;
|
|
|
|
ICD.Target = &ToFunction;
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
Summary->IndCallTargetDescriptions.emplace_back(ICD);
|
2019-06-20 11:10:49 +08:00
|
|
|
}
|
|
|
|
|
2021-12-15 08:52:51 +08:00
|
|
|
bool Instrumentation::createEdgeDescription(FunctionDescription &FuncDesc,
|
|
|
|
const BinaryFunction &FromFunction,
|
|
|
|
uint32_t From, uint32_t FromNodeID,
|
|
|
|
const BinaryFunction &ToFunction,
|
|
|
|
uint32_t To, uint32_t ToNodeID,
|
|
|
|
bool Instrumented) {
|
2019-08-08 07:09:50 +08:00
|
|
|
EdgeDescription ED;
|
|
|
|
auto Result = FuncDesc.EdgesSet.insert(std::make_pair(FromNodeID, ToNodeID));
|
|
|
|
// Avoid creating duplicated edge descriptions. This happens in CFGs where a
|
|
|
|
// block jumps to its fall-through.
|
|
|
|
if (Result.second == false)
|
|
|
|
return false;
|
|
|
|
ED.FromLoc.FuncString = getFunctionNameIndex(FromFunction);
|
|
|
|
ED.FromLoc.Offset = From;
|
|
|
|
ED.FromNode = FromNodeID;
|
|
|
|
ED.ToLoc.FuncString = getFunctionNameIndex(ToFunction);
|
|
|
|
ED.ToLoc.Offset = To;
|
|
|
|
ED.ToNode = ToNodeID;
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
ED.Counter = Instrumented ? Summary->Counters.size() : 0xffffffff;
|
2019-12-14 09:27:03 +08:00
|
|
|
if (Instrumented)
|
|
|
|
++BranchCounters;
|
2019-08-08 07:09:50 +08:00
|
|
|
FuncDesc.Edges.emplace_back(ED);
|
2019-12-14 09:27:03 +08:00
|
|
|
return Instrumented;
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
2019-06-20 11:10:49 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
void Instrumentation::createLeafNodeDescription(FunctionDescription &FuncDesc,
|
2019-08-08 07:09:50 +08:00
|
|
|
uint32_t Node) {
|
|
|
|
InstrumentedNode IN;
|
|
|
|
IN.Node = Node;
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
IN.Counter = Summary->Counters.size();
|
2019-12-14 09:27:03 +08:00
|
|
|
++LeafNodeCounters;
|
|
|
|
FuncDesc.LeafNodes.emplace_back(IN);
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
2021-12-10 03:53:12 +08:00
|
|
|
InstructionListType
|
2019-08-08 07:09:50 +08:00
|
|
|
Instrumentation::createInstrumentationSnippet(BinaryContext &BC, bool IsLeaf) {
|
|
|
|
auto L = BC.scopeLock();
|
|
|
|
MCSymbol *Label;
|
2020-12-02 08:29:39 +08:00
|
|
|
Label = BC.Ctx->createNamedTempSymbol("InstrEntry");
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
Summary->Counters.emplace_back(Label);
|
2021-12-10 03:53:12 +08:00
|
|
|
InstructionListType CounterInstrs;
|
2021-10-12 00:10:06 +08:00
|
|
|
BC.MIB->createInstrIncMemory(CounterInstrs, Label, &*BC.Ctx, IsLeaf);
|
2019-06-20 11:10:49 +08:00
|
|
|
return CounterInstrs;
|
|
|
|
}
|
|
|
|
|
2020-07-18 14:16:52 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Helper instruction sequence insertion function
|
2021-12-10 03:53:12 +08:00
|
|
|
BinaryBasicBlock::iterator insertInstructions(InstructionListType &Instrs,
|
|
|
|
BinaryBasicBlock &BB,
|
|
|
|
BinaryBasicBlock::iterator Iter) {
|
2021-04-08 15:19:26 +08:00
|
|
|
for (MCInst &NewInst : Instrs) {
|
2020-07-18 14:16:52 +08:00
|
|
|
Iter = BB.insertInstruction(Iter, NewInst);
|
|
|
|
++Iter;
|
|
|
|
}
|
|
|
|
return Iter;
|
|
|
|
}
|
2021-12-15 08:52:51 +08:00
|
|
|
} // namespace
|
2020-07-18 14:16:52 +08:00
|
|
|
|
2021-10-26 15:06:34 +08:00
|
|
|
void Instrumentation::instrumentLeafNode(BinaryBasicBlock &BB,
|
2019-08-08 07:09:50 +08:00
|
|
|
BinaryBasicBlock::iterator Iter,
|
|
|
|
bool IsLeaf,
|
|
|
|
FunctionDescription &FuncDesc,
|
|
|
|
uint32_t Node) {
|
2019-12-14 09:27:03 +08:00
|
|
|
createLeafNodeDescription(FuncDesc, Node);
|
2021-12-10 03:53:12 +08:00
|
|
|
InstructionListType CounterInstrs = createInstrumentationSnippet(
|
2021-10-26 15:06:34 +08:00
|
|
|
BB.getFunction()->getBinaryContext(), IsLeaf);
|
2020-07-18 14:16:52 +08:00
|
|
|
insertInstructions(CounterInstrs, BB, Iter);
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
void Instrumentation::instrumentIndirectTarget(BinaryBasicBlock &BB,
|
|
|
|
BinaryBasicBlock::iterator &Iter,
|
|
|
|
BinaryFunction &FromFunction,
|
|
|
|
uint32_t From) {
|
|
|
|
auto L = FromFunction.getBinaryContext().scopeLock();
|
2021-04-08 15:19:26 +08:00
|
|
|
const size_t IndCallSiteID = Summary->IndCallDescriptions.size();
|
2019-12-14 09:27:03 +08:00
|
|
|
createIndCallDescription(FromFunction, From);
|
|
|
|
|
|
|
|
BinaryContext &BC = FromFunction.getBinaryContext();
|
|
|
|
bool IsTailCall = BC.MIB->isTailCall(*Iter);
|
2021-12-10 03:53:12 +08:00
|
|
|
InstructionListType CounterInstrs = BC.MIB->createInstrumentedIndirectCall(
|
2019-12-14 09:27:03 +08:00
|
|
|
*Iter, IsTailCall,
|
2021-06-24 02:24:09 +08:00
|
|
|
IsTailCall ? IndTailCallHandlerExitBBFunction->getSymbol()
|
|
|
|
: IndCallHandlerExitBBFunction->getSymbol(),
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
IndCallSiteID, &*BC.Ctx);
|
2019-12-14 09:27:03 +08:00
|
|
|
|
|
|
|
Iter = BB.eraseInstruction(Iter);
|
2020-07-18 14:16:52 +08:00
|
|
|
Iter = insertInstructions(CounterInstrs, BB, Iter);
|
2019-12-14 09:27:03 +08:00
|
|
|
--Iter;
|
|
|
|
}
|
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
bool Instrumentation::instrumentOneTarget(
|
|
|
|
SplitWorklistTy &SplitWorklist, SplitInstrsTy &SplitInstrs,
|
|
|
|
BinaryBasicBlock::iterator &Iter, BinaryFunction &FromFunction,
|
|
|
|
BinaryBasicBlock &FromBB, uint32_t From, BinaryFunction &ToFunc,
|
2019-12-14 09:27:03 +08:00
|
|
|
BinaryBasicBlock *TargetBB, uint32_t ToOffset, bool IsLeaf, bool IsInvoke,
|
2019-08-08 07:09:50 +08:00
|
|
|
FunctionDescription *FuncDesc, uint32_t FromNodeID, uint32_t ToNodeID) {
|
|
|
|
{
|
|
|
|
auto L = FromFunction.getBinaryContext().scopeLock();
|
2021-05-14 01:50:47 +08:00
|
|
|
bool Created = true;
|
2019-08-08 07:09:50 +08:00
|
|
|
if (!TargetBB)
|
2019-12-14 09:27:03 +08:00
|
|
|
Created = createCallDescription(*FuncDesc, FromFunction, From, FromNodeID,
|
|
|
|
ToFunc, ToOffset, IsInvoke);
|
2019-08-08 07:09:50 +08:00
|
|
|
else
|
|
|
|
Created = createEdgeDescription(*FuncDesc, FromFunction, From, FromNodeID,
|
|
|
|
ToFunc, ToOffset, ToNodeID,
|
|
|
|
/*Instrumented=*/true);
|
|
|
|
if (!Created)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-10 03:53:12 +08:00
|
|
|
InstructionListType CounterInstrs =
|
|
|
|
createInstrumentationSnippet(FromFunction.getBinaryContext(), IsLeaf);
|
2019-06-20 11:10:49 +08:00
|
|
|
|
|
|
|
BinaryContext &BC = FromFunction.getBinaryContext();
|
|
|
|
const MCInst &Inst = *Iter;
|
2020-07-18 14:16:56 +08:00
|
|
|
if (BC.MIB->isCall(Inst)) {
|
|
|
|
// This code handles both
|
|
|
|
// - (regular) inter-function calls (cross-function control transfer),
|
|
|
|
// - (rare) intra-function calls (function-local control transfer)
|
2020-07-18 14:16:52 +08:00
|
|
|
Iter = insertInstructions(CounterInstrs, FromBB, Iter);
|
2019-06-20 11:10:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
if (!TargetBB || !FuncDesc)
|
2019-06-20 11:10:49 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Indirect branch, conditional branches or fall-throughs
|
|
|
|
// Regular cond branch, put counter at start of target block
|
2020-07-18 14:16:56 +08:00
|
|
|
//
|
|
|
|
// N.B.: (FromBB != TargetBBs) checks below handle conditional jumps where
|
|
|
|
// we can't put the instrumentation counter in this block because not all
|
|
|
|
// paths that reach it at this point will be taken and going to the target.
|
2019-06-20 11:10:49 +08:00
|
|
|
if (TargetBB->pred_size() == 1 && &FromBB != TargetBB &&
|
|
|
|
!TargetBB->isEntryPoint()) {
|
2020-07-18 14:16:52 +08:00
|
|
|
insertInstructions(CounterInstrs, *TargetBB, TargetBB->begin());
|
2019-06-20 11:10:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (FromBB.succ_size() == 1 && &FromBB != TargetBB) {
|
2020-07-18 14:16:52 +08:00
|
|
|
Iter = insertInstructions(CounterInstrs, FromBB, Iter);
|
2019-06-20 11:10:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Critical edge, create BB and put counter there
|
2021-05-08 09:43:25 +08:00
|
|
|
SplitWorklist.emplace_back(&FromBB, TargetBB);
|
2019-06-20 11:10:49 +08:00
|
|
|
SplitInstrs.emplace_back(std::move(CounterInstrs));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:06:34 +08:00
|
|
|
void Instrumentation::instrumentFunction(BinaryFunction &Function,
|
2019-08-08 07:09:50 +08:00
|
|
|
MCPlusBuilder::AllocatorIdTy AllocId) {
|
2020-04-07 08:38:11 +08:00
|
|
|
if (Function.hasUnknownControlFlow())
|
|
|
|
return;
|
|
|
|
|
2021-10-26 15:06:34 +08:00
|
|
|
BinaryContext &BC = Function.getBinaryContext();
|
2020-11-20 10:18:28 +08:00
|
|
|
if (BC.isMachO() && Function.hasName("___GLOBAL_init_65535/1"))
|
|
|
|
return;
|
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
SplitWorklistTy SplitWorklist;
|
|
|
|
SplitInstrsTy SplitInstrs;
|
|
|
|
|
|
|
|
FunctionDescription *FuncDesc = nullptr;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::shared_timed_mutex> L(FDMutex);
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
Summary->FunctionDescriptions.emplace_back();
|
|
|
|
FuncDesc = &Summary->FunctionDescriptions.back();
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
FuncDesc->Function = &Function;
|
2019-08-08 07:09:50 +08:00
|
|
|
Function.disambiguateJumpTables(AllocId);
|
2019-12-14 09:27:03 +08:00
|
|
|
Function.deleteConservativeEdges();
|
2019-08-08 07:09:50 +08:00
|
|
|
|
|
|
|
std::unordered_map<const BinaryBasicBlock *, uint32_t> BBToID;
|
|
|
|
uint32_t Id = 0;
|
|
|
|
for (auto BBI = Function.begin(); BBI != Function.end(); ++BBI) {
|
|
|
|
BBToID[&*BBI] = Id++;
|
|
|
|
}
|
|
|
|
std::unordered_set<const BinaryBasicBlock *> VisitedSet;
|
|
|
|
// DFS to establish edges we will use for a spanning tree. Edges in the
|
|
|
|
// spanning tree can be instrumentation-free since their count can be
|
|
|
|
// inferred by solving flow equations on a bottom-up traversal of the tree.
|
|
|
|
// Exit basic blocks are always instrumented so we start the traversal with
|
|
|
|
// a minimum number of defined variables to make the equation solvable.
|
|
|
|
std::stack<std::pair<const BinaryBasicBlock *, BinaryBasicBlock *>> Stack;
|
|
|
|
std::unordered_map<const BinaryBasicBlock *,
|
|
|
|
std::set<const BinaryBasicBlock *>>
|
|
|
|
STOutSet;
|
|
|
|
for (auto BBI = Function.layout_rbegin(); BBI != Function.layout_rend();
|
|
|
|
++BBI) {
|
2019-12-14 09:27:03 +08:00
|
|
|
if ((*BBI)->isEntryPoint() || (*BBI)->isLandingPad()) {
|
2019-08-08 07:09:50 +08:00
|
|
|
Stack.push(std::make_pair(nullptr, *BBI));
|
2019-12-14 09:27:03 +08:00
|
|
|
if (opts::InstrumentCalls && (*BBI)->isEntryPoint()) {
|
|
|
|
EntryNode E;
|
|
|
|
E.Node = BBToID[&**BBI];
|
|
|
|
E.Address = (*BBI)->getInputOffset();
|
|
|
|
FuncDesc->EntryNodes.emplace_back(E);
|
|
|
|
createIndCallTargetDescription(Function, (*BBI)->getInputOffset());
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Modified version of BinaryFunction::dfs() to build a spanning tree
|
2019-12-14 09:27:03 +08:00
|
|
|
if (!opts::ConservativeInstrumentation) {
|
|
|
|
while (!Stack.empty()) {
|
|
|
|
BinaryBasicBlock *BB;
|
|
|
|
const BinaryBasicBlock *Pred;
|
|
|
|
std::tie(Pred, BB) = Stack.top();
|
|
|
|
Stack.pop();
|
|
|
|
if (VisitedSet.find(BB) != VisitedSet.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
VisitedSet.insert(BB);
|
|
|
|
if (Pred)
|
|
|
|
STOutSet[Pred].insert(BB);
|
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
for (BinaryBasicBlock *SuccBB : BB->successors())
|
2019-12-14 09:27:03 +08:00
|
|
|
Stack.push(std::make_pair(BB, SuccBB));
|
|
|
|
}
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine whether this is a leaf function, which needs special
|
|
|
|
// instructions to protect the red zone
|
2021-05-14 01:50:47 +08:00
|
|
|
bool IsLeafFunction = true;
|
2019-12-14 09:27:03 +08:00
|
|
|
DenseSet<const BinaryBasicBlock *> InvokeBlocks;
|
2019-08-08 07:09:50 +08:00
|
|
|
for (auto BBI = Function.begin(), BBE = Function.end(); BBI != BBE; ++BBI) {
|
|
|
|
for (auto I = BBI->begin(), E = BBI->end(); I != E; ++I) {
|
|
|
|
if (BC.MIB->isCall(*I)) {
|
2021-12-29 08:36:17 +08:00
|
|
|
if (BC.MIB->isInvoke(*I))
|
2019-12-14 09:27:03 +08:00
|
|
|
InvokeBlocks.insert(&*BBI);
|
2019-08-08 07:09:50 +08:00
|
|
|
IsLeafFunction = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto BBI = Function.begin(), BBE = Function.end(); BBI != BBE; ++BBI) {
|
2021-05-14 01:50:47 +08:00
|
|
|
BinaryBasicBlock &BB = *BBI;
|
|
|
|
bool HasUnconditionalBranch = false;
|
|
|
|
bool HasJumpTable = false;
|
2019-12-14 09:27:03 +08:00
|
|
|
bool IsInvokeBlock = InvokeBlocks.count(&BB) > 0;
|
2019-08-08 07:09:50 +08:00
|
|
|
|
|
|
|
for (auto I = BB.begin(); I != BB.end(); ++I) {
|
2021-04-08 15:19:26 +08:00
|
|
|
const MCInst &Inst = *I;
|
2021-08-04 08:53:32 +08:00
|
|
|
if (!BC.MIB->getOffset(Inst))
|
2019-08-08 07:09:50 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const bool IsJumpTable = Function.getJumpTable(Inst);
|
|
|
|
if (IsJumpTable)
|
|
|
|
HasJumpTable = true;
|
|
|
|
else if (BC.MIB->isUnconditionalBranch(Inst))
|
|
|
|
HasUnconditionalBranch = true;
|
|
|
|
else if ((!BC.MIB->isCall(Inst) && !BC.MIB->isConditionalBranch(Inst)) ||
|
|
|
|
BC.MIB->isUnsupportedBranch(Inst.getOpcode()))
|
|
|
|
continue;
|
|
|
|
|
2021-08-04 08:53:32 +08:00
|
|
|
const uint32_t FromOffset = *BC.MIB->getOffset(Inst);
|
2019-08-08 07:09:50 +08:00
|
|
|
const MCSymbol *Target = BC.MIB->getTargetSymbol(Inst);
|
|
|
|
BinaryBasicBlock *TargetBB = Function.getBasicBlockForLabel(Target);
|
|
|
|
uint32_t ToOffset = TargetBB ? TargetBB->getInputOffset() : 0;
|
|
|
|
BinaryFunction *TargetFunc =
|
|
|
|
TargetBB ? &Function : BC.getFunctionForSymbol(Target);
|
2020-07-18 14:16:56 +08:00
|
|
|
if (TargetFunc && BC.MIB->isCall(Inst)) {
|
2019-12-14 09:27:03 +08:00
|
|
|
if (opts::InstrumentCalls) {
|
2021-04-08 15:19:26 +08:00
|
|
|
const BinaryBasicBlock *ForeignBB =
|
|
|
|
TargetFunc->getBasicBlockForLabel(Target);
|
2019-12-14 09:27:03 +08:00
|
|
|
if (ForeignBB)
|
|
|
|
ToOffset = ForeignBB->getInputOffset();
|
2019-08-08 07:09:50 +08:00
|
|
|
instrumentOneTarget(SplitWorklist, SplitInstrs, I, Function, BB,
|
|
|
|
FromOffset, *TargetFunc, TargetBB, ToOffset,
|
2019-12-14 09:27:03 +08:00
|
|
|
IsLeafFunction, IsInvokeBlock, FuncDesc,
|
|
|
|
BBToID[&BB]);
|
|
|
|
}
|
2019-08-08 07:09:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (TargetFunc) {
|
|
|
|
// Do not instrument edges in the spanning tree
|
|
|
|
if (STOutSet[&BB].find(TargetBB) != STOutSet[&BB].end()) {
|
|
|
|
auto L = BC.scopeLock();
|
|
|
|
createEdgeDescription(*FuncDesc, Function, FromOffset, BBToID[&BB],
|
|
|
|
Function, ToOffset, BBToID[TargetBB],
|
|
|
|
/*Instrumented=*/false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
instrumentOneTarget(SplitWorklist, SplitInstrs, I, Function, BB,
|
|
|
|
FromOffset, *TargetFunc, TargetBB, ToOffset,
|
2019-12-14 09:27:03 +08:00
|
|
|
IsLeafFunction, IsInvokeBlock, FuncDesc,
|
|
|
|
BBToID[&BB], BBToID[TargetBB]);
|
2019-08-08 07:09:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsJumpTable) {
|
2021-04-08 15:19:26 +08:00
|
|
|
for (BinaryBasicBlock *&Succ : BB.successors()) {
|
2019-08-08 07:09:50 +08:00
|
|
|
// Do not instrument edges in the spanning tree
|
|
|
|
if (STOutSet[&BB].find(&*Succ) != STOutSet[&BB].end()) {
|
|
|
|
auto L = BC.scopeLock();
|
|
|
|
createEdgeDescription(*FuncDesc, Function, FromOffset, BBToID[&BB],
|
|
|
|
Function, Succ->getInputOffset(),
|
|
|
|
BBToID[&*Succ], /*Instrumented=*/false);
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-14 09:27:03 +08:00
|
|
|
instrumentOneTarget(
|
|
|
|
SplitWorklist, SplitInstrs, I, Function, BB, FromOffset, Function,
|
|
|
|
&*Succ, Succ->getInputOffset(), IsLeafFunction, IsInvokeBlock,
|
|
|
|
FuncDesc, BBToID[&BB], BBToID[&*Succ]);
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-14 09:27:03 +08:00
|
|
|
|
|
|
|
// Handle indirect calls -- could be direct calls with unknown targets
|
|
|
|
// or secondary entry points of known functions, so check it is indirect
|
|
|
|
// to be sure.
|
|
|
|
if (opts::InstrumentCalls && BC.MIB->isIndirectCall(*I))
|
|
|
|
instrumentIndirectTarget(BB, I, Function, FromOffset);
|
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
} // End of instructions loop
|
|
|
|
|
|
|
|
// Instrument fallthroughs (when the direct jump instruction is missing)
|
|
|
|
if (!HasUnconditionalBranch && !HasJumpTable && BB.succ_size() > 0 &&
|
|
|
|
BB.size() > 0) {
|
2021-04-08 15:19:26 +08:00
|
|
|
BinaryBasicBlock *FTBB = BB.getFallthrough();
|
2019-08-08 07:09:50 +08:00
|
|
|
assert(FTBB && "expected valid fall-through basic block");
|
|
|
|
auto I = BB.begin();
|
|
|
|
auto LastInstr = BB.end();
|
|
|
|
--LastInstr;
|
|
|
|
while (LastInstr != I && BC.MIB->isPseudo(*LastInstr))
|
|
|
|
--LastInstr;
|
|
|
|
uint32_t FromOffset = 0;
|
|
|
|
// The last instruction in the BB should have an annotation, except
|
|
|
|
// if it was branching to the end of the function as a result of
|
|
|
|
// __builtin_unreachable(), in which case it was deleted by fixBranches.
|
|
|
|
// Ignore this case. FIXME: force fixBranches() to preserve the offset.
|
2021-08-04 08:53:32 +08:00
|
|
|
if (!BC.MIB->getOffset(*LastInstr))
|
2019-08-08 07:09:50 +08:00
|
|
|
continue;
|
2021-08-04 08:53:32 +08:00
|
|
|
FromOffset = *BC.MIB->getOffset(*LastInstr);
|
2019-08-08 07:09:50 +08:00
|
|
|
|
|
|
|
// Do not instrument edges in the spanning tree
|
|
|
|
if (STOutSet[&BB].find(FTBB) != STOutSet[&BB].end()) {
|
|
|
|
auto L = BC.scopeLock();
|
|
|
|
createEdgeDescription(*FuncDesc, Function, FromOffset, BBToID[&BB],
|
|
|
|
Function, FTBB->getInputOffset(), BBToID[FTBB],
|
|
|
|
/*Instrumented=*/false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
instrumentOneTarget(SplitWorklist, SplitInstrs, I, Function, BB,
|
|
|
|
FromOffset, Function, FTBB, FTBB->getInputOffset(),
|
2019-12-14 09:27:03 +08:00
|
|
|
IsLeafFunction, IsInvokeBlock, FuncDesc, BBToID[&BB],
|
|
|
|
BBToID[FTBB]);
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
} // End of BBs loop
|
|
|
|
|
|
|
|
// Instrument spanning tree leaves
|
2019-12-14 09:27:03 +08:00
|
|
|
if (!opts::ConservativeInstrumentation) {
|
|
|
|
for (auto BBI = Function.begin(), BBE = Function.end(); BBI != BBE; ++BBI) {
|
2021-05-14 01:50:47 +08:00
|
|
|
BinaryBasicBlock &BB = *BBI;
|
2019-12-14 09:27:03 +08:00
|
|
|
if (STOutSet[&BB].size() == 0)
|
2021-10-26 15:06:34 +08:00
|
|
|
instrumentLeafNode(BB, BB.begin(), IsLeafFunction, *FuncDesc,
|
2019-12-14 09:27:03 +08:00
|
|
|
BBToID[&BB]);
|
|
|
|
}
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Consume list of critical edges: split them and add instrumentation to the
|
|
|
|
// newly created BBs
|
|
|
|
auto Iter = SplitInstrs.begin();
|
2021-04-08 15:19:26 +08:00
|
|
|
for (std::pair<BinaryBasicBlock *, BinaryBasicBlock *> &BBPair :
|
|
|
|
SplitWorklist) {
|
|
|
|
BinaryBasicBlock *NewBB = Function.splitEdge(BBPair.first, BBPair.second);
|
2019-08-08 07:09:50 +08:00
|
|
|
NewBB->addInstructions(Iter->begin(), Iter->end());
|
|
|
|
++Iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unused now
|
|
|
|
FuncDesc->EdgesSet.clear();
|
|
|
|
}
|
|
|
|
|
2019-06-20 11:10:49 +08:00
|
|
|
void Instrumentation::runOnFunctions(BinaryContext &BC) {
|
|
|
|
if (!BC.isX86())
|
|
|
|
return;
|
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
const unsigned Flags = BinarySection::getFlags(/*IsReadOnly=*/false,
|
|
|
|
/*IsText=*/false,
|
|
|
|
/*IsAllocatable=*/true);
|
2019-08-03 02:20:13 +08:00
|
|
|
BC.registerOrUpdateSection(".bolt.instr.counters", ELF::SHT_PROGBITS, Flags,
|
2020-02-19 01:20:17 +08:00
|
|
|
nullptr, 0, 1);
|
2019-06-20 11:10:49 +08:00
|
|
|
|
2021-12-15 08:52:51 +08:00
|
|
|
BC.registerOrUpdateNoteSection(".bolt.instr.tables", nullptr, 0,
|
|
|
|
/*Alignment=*/1,
|
|
|
|
/*IsReadOnly=*/true, ELF::SHT_NOTE);
|
2019-08-03 02:20:13 +08:00
|
|
|
|
2021-06-24 02:24:09 +08:00
|
|
|
Summary->IndCallCounterFuncPtr =
|
|
|
|
BC.Ctx->getOrCreateSymbol("__bolt_ind_call_counter_func_pointer");
|
|
|
|
Summary->IndTailCallCounterFuncPtr =
|
|
|
|
BC.Ctx->getOrCreateSymbol("__bolt_ind_tailcall_counter_func_pointer");
|
|
|
|
|
|
|
|
createAuxiliaryFunctions(BC);
|
2019-12-14 09:27:03 +08:00
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
ParallelUtilities::PredicateTy SkipPredicate = [&](const BinaryFunction &BF) {
|
2020-05-04 04:54:45 +08:00
|
|
|
return (!BF.isSimple() || BF.isIgnored() ||
|
2019-08-08 07:09:50 +08:00
|
|
|
(opts::InstrumentHotOnly && !BF.getKnownExecutionCount()));
|
|
|
|
};
|
2019-06-20 11:10:49 +08:00
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
ParallelUtilities::WorkFuncWithAllocTy WorkFun =
|
|
|
|
[&](BinaryFunction &BF, MCPlusBuilder::AllocatorIdTy AllocatorId) {
|
2021-10-26 15:06:34 +08:00
|
|
|
instrumentFunction(BF, AllocatorId);
|
2019-08-08 07:09:50 +08:00
|
|
|
};
|
2019-06-20 11:10:49 +08:00
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
ParallelUtilities::runOnEachFunctionWithUniqueAllocId(
|
|
|
|
BC, ParallelUtilities::SchedulingPolicy::SP_INST_QUADRATIC, WorkFun,
|
|
|
|
SkipPredicate, "instrumentation", /* ForceSequential=*/true);
|
2019-12-14 09:27:03 +08:00
|
|
|
|
2020-11-20 10:18:28 +08:00
|
|
|
if (BC.isMachO()) {
|
|
|
|
if (BC.StartFunctionAddress) {
|
|
|
|
BinaryFunction *Main =
|
|
|
|
BC.getBinaryFunctionAtAddress(*BC.StartFunctionAddress);
|
|
|
|
assert(Main && "Entry point function not found");
|
|
|
|
BinaryBasicBlock &BB = Main->front();
|
|
|
|
|
|
|
|
ErrorOr<BinarySection &> SetupSection =
|
|
|
|
BC.getUniqueSectionByName("I__setup");
|
|
|
|
if (!SetupSection) {
|
|
|
|
llvm::errs() << "Cannot find I__setup section\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
MCSymbol *Target = BC.registerNameAtAddress(
|
|
|
|
"__bolt_instr_setup", SetupSection->getAddress(), 0, 0);
|
|
|
|
MCInst NewInst;
|
|
|
|
BC.MIB->createCall(NewInst, Target, BC.Ctx.get());
|
|
|
|
BB.insertInstruction(BB.begin(), std::move(NewInst));
|
|
|
|
} else {
|
|
|
|
llvm::errs() << "BOLT-WARNING: Entry point not found\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BinaryData *BD = BC.getBinaryDataByName("___GLOBAL_init_65535/1")) {
|
|
|
|
BinaryFunction *Ctor = BC.getBinaryFunctionAtAddress(BD->getAddress());
|
|
|
|
assert(Ctor && "___GLOBAL_init_65535 function not found");
|
|
|
|
BinaryBasicBlock &BB = Ctor->front();
|
|
|
|
ErrorOr<BinarySection &> FiniSection =
|
|
|
|
BC.getUniqueSectionByName("I__fini");
|
|
|
|
if (!FiniSection) {
|
|
|
|
llvm::errs() << "Cannot find I__fini section\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
MCSymbol *Target = BC.registerNameAtAddress(
|
|
|
|
"__bolt_instr_fini", FiniSection->getAddress(), 0, 0);
|
|
|
|
auto IsLEA = [&BC](const MCInst &Inst) { return BC.MIB->isLEA64r(Inst); };
|
2021-12-15 08:52:51 +08:00
|
|
|
const auto LEA =
|
|
|
|
std::find_if(std::next(std::find_if(BB.rbegin(), BB.rend(), IsLEA)),
|
|
|
|
BB.rend(), IsLEA);
|
2020-11-20 10:18:28 +08:00
|
|
|
LEA->getOperand(4).setExpr(
|
|
|
|
MCSymbolRefExpr::create(Target, MCSymbolRefExpr::VK_None, *BC.Ctx));
|
|
|
|
} else {
|
|
|
|
llvm::errs() << "BOLT-WARNING: ___GLOBAL_init_65535 not found\n";
|
2020-10-15 16:39:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
setupRuntimeLibrary(BC);
|
2019-12-14 09:27:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instrumentation::createAuxiliaryFunctions(BinaryContext &BC) {
|
|
|
|
auto createSimpleFunction =
|
2021-12-10 03:53:12 +08:00
|
|
|
[&](StringRef Title, InstructionListType Instrs) -> BinaryFunction * {
|
2020-12-02 08:29:39 +08:00
|
|
|
BinaryFunction *Func = BC.createInjectedBinaryFunction(std::string(Title));
|
2019-12-14 09:27:03 +08:00
|
|
|
|
|
|
|
std::vector<std::unique_ptr<BinaryBasicBlock>> BBs;
|
|
|
|
BBs.emplace_back(
|
|
|
|
Func->createBasicBlock(BinaryBasicBlock::INVALID_OFFSET, nullptr));
|
|
|
|
BBs.back()->addInstructions(Instrs.begin(), Instrs.end());
|
|
|
|
BBs.back()->setCFIState(0);
|
|
|
|
Func->insertBasicBlocks(nullptr, std::move(BBs),
|
|
|
|
/*UpdateLayout=*/true,
|
|
|
|
/*UpdateCFIState=*/false);
|
|
|
|
Func->updateState(BinaryFunction::State::CFG_Finalized);
|
|
|
|
return Func;
|
|
|
|
};
|
|
|
|
|
2021-06-24 02:24:09 +08:00
|
|
|
// Here we are creating a set of functions to handle BB entry/exit.
|
|
|
|
// IndCallHandlerExitBB contains instructions to finish handling traffic to an
|
|
|
|
// indirect call. We pass it to createInstrumentedIndCallHandlerEntryBB(),
|
|
|
|
// which will check if a pointer to runtime library traffic accounting
|
|
|
|
// function was initialized (it is done during initialization of runtime
|
|
|
|
// library). If it is so - calls it. Then this routine returns to normal
|
|
|
|
// execution by jumping to exit BB.
|
|
|
|
BinaryFunction *IndCallHandlerExitBB =
|
|
|
|
createSimpleFunction("__bolt_instr_ind_call_handler",
|
|
|
|
BC.MIB->createInstrumentedIndCallHandlerExitBB());
|
|
|
|
|
|
|
|
IndCallHandlerExitBBFunction =
|
|
|
|
createSimpleFunction("__bolt_instr_ind_call_handler_func",
|
|
|
|
BC.MIB->createInstrumentedIndCallHandlerEntryBB(
|
|
|
|
Summary->IndCallCounterFuncPtr,
|
|
|
|
IndCallHandlerExitBB->getSymbol(), &*BC.Ctx));
|
|
|
|
|
|
|
|
BinaryFunction *IndTailCallHandlerExitBB = createSimpleFunction(
|
|
|
|
"__bolt_instr_ind_tail_call_handler",
|
|
|
|
BC.MIB->createInstrumentedIndTailCallHandlerExitBB());
|
|
|
|
|
|
|
|
IndTailCallHandlerExitBBFunction = createSimpleFunction(
|
|
|
|
"__bolt_instr_ind_tailcall_handler_func",
|
|
|
|
BC.MIB->createInstrumentedIndCallHandlerEntryBB(
|
|
|
|
Summary->IndTailCallCounterFuncPtr,
|
|
|
|
IndTailCallHandlerExitBB->getSymbol(), &*BC.Ctx));
|
2021-01-29 04:32:03 +08:00
|
|
|
|
|
|
|
createSimpleFunction("__bolt_num_counters_getter",
|
|
|
|
BC.MIB->createNumCountersGetter(BC.Ctx.get()));
|
|
|
|
createSimpleFunction("__bolt_instr_locations_getter",
|
|
|
|
BC.MIB->createInstrLocationsGetter(BC.Ctx.get()));
|
2021-01-29 04:44:14 +08:00
|
|
|
createSimpleFunction("__bolt_instr_tables_getter",
|
|
|
|
BC.MIB->createInstrTablesGetter(BC.Ctx.get()));
|
|
|
|
createSimpleFunction("__bolt_instr_num_funcs_getter",
|
|
|
|
BC.MIB->createInstrNumFuncsGetter(BC.Ctx.get()));
|
2021-06-19 04:08:35 +08:00
|
|
|
|
|
|
|
if (BC.isELF()) {
|
|
|
|
if (BC.StartFunctionAddress) {
|
|
|
|
BinaryFunction *Start =
|
|
|
|
BC.getBinaryFunctionAtAddress(*BC.StartFunctionAddress);
|
|
|
|
assert(Start && "Entry point function not found");
|
|
|
|
const MCSymbol *StartSym = Start->getSymbol();
|
|
|
|
createSimpleFunction(
|
|
|
|
"__bolt_start_trampoline",
|
|
|
|
BC.MIB->createSymbolTrampoline(StartSym, BC.Ctx.get()));
|
|
|
|
}
|
|
|
|
if (BC.FiniFunctionAddress) {
|
|
|
|
BinaryFunction *Fini =
|
|
|
|
BC.getBinaryFunctionAtAddress(*BC.FiniFunctionAddress);
|
|
|
|
assert(Fini && "Finalization function not found");
|
|
|
|
const MCSymbol *FiniSym = Fini->getSymbol();
|
|
|
|
createSimpleFunction(
|
|
|
|
"__bolt_fini_trampoline",
|
|
|
|
BC.MIB->createSymbolTrampoline(FiniSym, BC.Ctx.get()));
|
2021-06-21 01:59:38 +08:00
|
|
|
} else {
|
|
|
|
// Create dummy return function for trampoline to avoid issues
|
|
|
|
// with unknown symbol in runtime library. E.g. for static PIE
|
|
|
|
// executable
|
|
|
|
createSimpleFunction("__bolt_fini_trampoline",
|
|
|
|
BC.MIB->createDummyReturnFunction(BC.Ctx.get()));
|
2021-06-19 04:08:35 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
2019-06-20 11:10:49 +08:00
|
|
|
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
void Instrumentation::setupRuntimeLibrary(BinaryContext &BC) {
|
2021-04-08 15:19:26 +08:00
|
|
|
uint32_t FuncDescSize = Summary->getFDSize();
|
2019-12-14 09:27:03 +08:00
|
|
|
|
|
|
|
outs() << "BOLT-INSTRUMENTER: Number of indirect call site descriptors: "
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
<< Summary->IndCallDescriptions.size() << "\n";
|
2019-12-14 09:27:03 +08:00
|
|
|
outs() << "BOLT-INSTRUMENTER: Number of indirect call target descriptors: "
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
<< Summary->IndCallTargetDescriptions.size() << "\n";
|
2019-08-08 07:09:50 +08:00
|
|
|
outs() << "BOLT-INSTRUMENTER: Number of function descriptors: "
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
<< Summary->FunctionDescriptions.size() << "\n";
|
2019-12-14 09:27:03 +08:00
|
|
|
outs() << "BOLT-INSTRUMENTER: Number of branch counters: " << BranchCounters
|
|
|
|
<< "\n";
|
|
|
|
outs() << "BOLT-INSTRUMENTER: Number of ST leaf node counters: "
|
|
|
|
<< LeafNodeCounters << "\n";
|
|
|
|
outs() << "BOLT-INSTRUMENTER: Number of direct call counters: "
|
|
|
|
<< DirectCallCounters << "\n";
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
outs() << "BOLT-INSTRUMENTER: Total number of counters: "
|
|
|
|
<< Summary->Counters.size() << "\n";
|
2019-06-20 11:10:49 +08:00
|
|
|
outs() << "BOLT-INSTRUMENTER: Total size of counters: "
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
<< (Summary->Counters.size() * 8) << " bytes (static alloc memory)\n";
|
2019-08-03 02:20:13 +08:00
|
|
|
outs() << "BOLT-INSTRUMENTER: Total size of string table emitted: "
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
<< Summary->StringTable.size() << " bytes in file\n";
|
2019-06-20 11:10:49 +08:00
|
|
|
outs() << "BOLT-INSTRUMENTER: Total size of descriptors: "
|
2019-12-14 09:27:03 +08:00
|
|
|
<< (FuncDescSize +
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
Summary->IndCallDescriptions.size() * sizeof(IndCallDescription) +
|
|
|
|
Summary->IndCallTargetDescriptions.size() *
|
2019-12-14 09:27:03 +08:00
|
|
|
sizeof(IndCallTargetDescription))
|
2019-08-08 07:09:50 +08:00
|
|
|
<< " bytes in file\n";
|
2019-06-20 11:10:49 +08:00
|
|
|
outs() << "BOLT-INSTRUMENTER: Profile will be saved to file "
|
|
|
|
<< opts::InstrumentationFilename << "\n";
|
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
InstrumentationRuntimeLibrary *RtLibrary =
|
2021-03-16 07:34:25 +08:00
|
|
|
static_cast<InstrumentationRuntimeLibrary *>(BC.getRuntimeLibrary());
|
|
|
|
assert(RtLibrary && "instrumentation runtime library object must be set");
|
|
|
|
RtLibrary->setSummary(std::move(Summary));
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-22 05:28:47 +08:00
|
|
|
}
|
2021-12-15 08:52:51 +08:00
|
|
|
} // namespace bolt
|
|
|
|
} // namespace llvm
|