LLVM 23.0.0git
Instrumentor.h
Go to the documentation of this file.
1//===-- Instrumentor.h - Highly configurable instrumentation pass ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// The Instrumentor, a highly configurable instrumentation pass.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TRANSFORMS_IPO_INSTRUMENTOR_H
14#define LLVM_TRANSFORMS_IPO_INSTRUMENTOR_H
15
16#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
21#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/Instruction.h"
26#include "llvm/IR/LLVMContext.h"
27#include "llvm/IR/Module.h"
28#include "llvm/IR/PassManager.h"
34
35#include <cstdint>
36#include <functional>
37#include <memory>
38#include <string>
39#include <tuple>
40
41namespace llvm {
42namespace instrumentor {
43
46
47/// Callback type for getting/setting a value for a instrumented opportunity.
48///{
49using GetterCallbackTy = std::function<Value *(
51using SetterCallbackTy = std::function<Value *(
53///}
54
55/// Helper to represent an argument to an instrumentation runtime function.
56struct IRTArg {
57 /// Flags describing the possible properties of an argument.
59 NONE = 0,
60 STRING = 1 << 0,
61 REPLACABLE = 1 << 1,
66 };
67
68 /// Construct an argument.
75
76 /// Whether the argument is enabled and should be passed to the function call.
77 bool Enabled;
78
79 /// The type of the argument.
81
82 /// A string with the name of the argument.
84
85 /// A string with the description of the argument.
87
88 /// The flags that describe the properties of the argument. Multiple flags may
89 /// be specified.
90 unsigned Flags;
91
92 /// The callback for getting the value of the argument.
94
95 /// The callback for consuming the output value of the argument.
97
98 /// Whether the argument value can be cached between the PRE and POST calls.
99 bool NoCache;
100};
101
102/// Helper to represent an instrumentation runtime function that is related to
103/// an instrumentation opportunity.
105 /// Construct an instrumentation function description linked to the \p IO
106 /// instrumentation opportunity and \p RetTy return type.
108
109 /// Create the type of the instrumentation function.
112 const DataLayout &DL,
113 bool ForceIndirection);
114
115 /// Create a call instruction that calls to the instrumentation function and
116 /// passes the corresponding arguments.
119 InstrumentationCaches &ICaches);
120
121 /// Create a string representation of the function declaration in C. Two
122 /// strings are returned: the function definition with direct arguments and
123 /// the function with any indirect argument.
124 std::pair<std::string, std::string>
125 createCSignature(const InstrumentationConfig &IConf) const;
126
127 /// Create a string representation of the function definition in C. The
128 /// function body implements a stub and only prints the passed arguments. Two
129 /// strings are returned: the function definition with direct arguments and
130 /// the function with any indirect argument.
131 std::pair<std::string, std::string> createCBodies() const;
132
133 /// Return whether the \p IRTA argument can be replaced.
134 bool isReplacable(IRTArg &IRTA) const {
136 }
137
138 /// Return whether the function may have any indirect argument.
139 bool isPotentiallyIndirect(IRTArg &IRTA) const {
140 return ((IRTA.Flags & IRTArg::POTENTIALLY_INDIRECT) ||
142 }
143
144 /// Whether the function requires indirection in some argument.
146
147 /// Whether any argument may require indirection.
149
150 /// The number of arguments that can be replaced.
151 unsigned NumReplaceableArgs = 0;
152
153 /// The instrumentation opportunity which it is linked to.
155
156 /// The return type of the instrumentation function.
157 Type *RetTy = nullptr;
158};
159
160/// Helper to represent an instrumentation location, which is composed of an
161/// instrumentation opportunity type and a position.
163 /// The supported location kinds, which are composed of a opportunity type and
164 /// position. The PRE position indicates the instrumentation function call is
165 /// inserted before the instrumented event occurs. The POST position indicates
166 /// the instrumentation call is inserted after the event occurs. Some
167 /// opportunity types may only support one position.
181
182 /// Construct an instrumentation location that is not instrumenting an
183 /// instruction.
184 InstrumentationLocation(KindTy Kind) : Kind(Kind) {
185 assert(Kind != INSTRUCTION_PRE && Kind != INSTRUCTION_POST &&
186 "Opcode required!");
187 }
188
189 /// Construct an instrumentation location belonging to the instrumentation of
190 /// an instruction.
191 InstrumentationLocation(unsigned Opcode, bool IsPRE)
192 : Kind(IsPRE ? INSTRUCTION_PRE : INSTRUCTION_POST), Opcode(Opcode) {}
193
194 /// Return the type and position.
195 KindTy getKind() const { return Kind; }
196
197 /// Return the string representation given a location kind. This is the string
198 /// used in the configuration file.
200 switch (Kind) {
201 case MODULE_PRE:
202 return "module_pre";
203 case MODULE_POST:
204 return "module_post";
205 case GLOBAL_PRE:
206 return "global_pre";
207 case GLOBAL_POST:
208 return "global_post";
209 case FUNCTION_PRE:
210 return "function_pre";
211 case FUNCTION_POST:
212 return "function_post";
213 case BASIC_BLOCK_PRE:
214 return "basic_block_pre";
215 case BASIC_BLOCK_POST:
216 return "basic_block_post";
217 case INSTRUCTION_PRE:
218 return "instruction_pre";
219 case INSTRUCTION_POST:
220 return "instruction_post";
221 }
222 llvm_unreachable("Invalid kind!");
223 }
224
225 /// Return the location kind described by a string.
227 return StringSwitch<KindTy>(S)
228 .Case("module_pre", MODULE_PRE)
229 .Case("module_post", MODULE_POST)
230 .Case("global_pre", GLOBAL_PRE)
231 .Case("global_post", GLOBAL_POST)
232 .Case("function_pre", FUNCTION_PRE)
233 .Case("function_post", FUNCTION_POST)
234 .Case("basic_block_pre", BASIC_BLOCK_PRE)
235 .Case("basic_block_post", BASIC_BLOCK_POST)
236 .Case("instruction_pre", INSTRUCTION_PRE)
237 .Case("instruction_post", INSTRUCTION_POST)
238 .Default(Last);
239 }
240
241 /// Return whether a location kind is positioned before the event occurs.
242 static bool isPRE(KindTy Kind) {
243 switch (Kind) {
244 case MODULE_PRE:
245 case GLOBAL_PRE:
246 case FUNCTION_PRE:
247 case BASIC_BLOCK_PRE:
248 case INSTRUCTION_PRE:
249 return true;
250 case MODULE_POST:
251 case GLOBAL_POST:
252 case FUNCTION_POST:
253 case BASIC_BLOCK_POST:
254 case INSTRUCTION_POST:
255 return false;
256 }
257 llvm_unreachable("Invalid kind!");
258 }
259
260 /// Return whether the instrumentation location is before the event occurs.
261 bool isPRE() const { return isPRE(Kind); }
262
263 /// Get the opcode of the instruction instrumentation location. This function
264 /// may not be called by a non-instruction instrumentation location.
265 unsigned getOpcode() const {
266 assert((Kind == INSTRUCTION_PRE || Kind == INSTRUCTION_POST) &&
267 "Expected instruction!");
268 return Opcode;
269 }
270
271private:
272 /// The kind (type and position) of the instrumentation location.
273 const KindTy Kind;
274
275 /// The opcode for instruction instrumentation locations.
276 const unsigned Opcode = -1;
277};
278
279/// An option for the base configuration.
281 /// The possible types of options.
286
287 /// Create a boolean option with \p Name name, \p Description description and
288 /// \p DefaultValue as boolean default value.
289 static std::unique_ptr<BaseConfigurationOption>
291 StringRef Description, bool DefaultValue);
292
293 /// Create a string option with \p Name name, \p Description description and
294 /// \p DefaultValue as string default value.
295 static std::unique_ptr<BaseConfigurationOption>
297 StringRef Description, StringRef DefaultValue);
298
299 /// Helper union that holds any possible option type.
300 union ValueTy {
301 bool Bool;
303 };
304
305 /// Set and get of the boolean value. Only valid if it is a boolean option.
306 ///{
307 void setBool(bool B) {
308 assert(Kind == BOOLEAN && "Not a boolean!");
309 Value.Bool = B;
310 }
311 bool getBool() const {
312 assert(Kind == BOOLEAN && "Not a boolean!");
313 return Value.Bool;
314 }
315 ///}
316
317 /// Set and get the string value. Only valid if it is a boolean option.
318 ///{
320 assert(Kind == STRING && "Not a string!");
321 Value.String = S;
322 }
324 assert(Kind == STRING && "Not a string!");
325 return Value.String;
326 }
327 ///}
328
329 /// The information of the option.
330 ///{
335 ///}
336
337 /// Construct a base configuration option.
340};
341
342/// The class that contains the configuration for the instrumentor. It holds the
343/// information for each instrumented opportunity, including the base
344/// configuration options. Another class may inherit from this one to modify the
345/// default behavior.
348
349 /// Construct an instrumentation configuration with the base options.
352 *this, "runtime_prefix", "The runtime API prefix.", "__instrumentor_");
354 *this, "runtime_stubs_file",
355 "The file into which runtime stubs should be written.", "");
357 *this, "target_regex",
358 "Regular expression to be matched against the module target. "
359 "Only targets that match this regex will be instrumented",
360 "");
362 *this, "host_enabled", "Instrument non-GPU targets", true);
364 *this, "gpu_enabled", "Instrument GPU targets", true);
365 }
366
367 /// Populate the instrumentation opportunities.
368 virtual void populate(InstrumentorIRBuilderTy &IIRB);
369
370 /// Get the runtime prefix for the instrumentation runtime functions.
371 StringRef getRTName() const { return RuntimePrefix->getString(); }
372
373 /// Get the instrumentation function name.
374 std::string getRTName(StringRef Prefix, StringRef Name,
375 StringRef Suffix1 = "", StringRef Suffix2 = "") const {
376 return (getRTName() + Prefix + Name + Suffix1 + Suffix2).str();
377 }
378
379 /// Add the base configuration option \p BCO into the list of base options.
381 BaseConfigurationOptions.push_back(BCO);
382 }
383
384 /// Register instrumentation opportunity \p IO.
386
387 /// Allocate an object of type \p Ty using a bump allocator and construct it
388 /// with the \p Args arguments. The object may not be freed manually.
389 template <typename Ty, typename... ArgsTy>
390 static Ty *allocate(ArgsTy &&...Args) {
392 Ty *Obj = Allocator.Allocate();
393 new (Obj) Ty(std::forward<ArgsTy>(Args)...);
394 return Obj;
395 }
396
397 /// The list of enabled base configuration options.
399
400 /// The base configuration options.
401 std::unique_ptr<BaseConfigurationOption> RuntimePrefix;
402 std::unique_ptr<BaseConfigurationOption> RuntimeStubsFile;
403 std::unique_ptr<BaseConfigurationOption> TargetRegex;
404 std::unique_ptr<BaseConfigurationOption> HostEnabled;
405 std::unique_ptr<BaseConfigurationOption> GPUEnabled;
406
407 /// The map registered instrumentation opportunities. The map is indexed by
408 /// the instrumentation location kind and then by the opportunity name. Notice
409 /// that an instrumentation location may have more than one instrumentation
410 /// opportunity registered.
414
415 /// Utilities for allocating and building strings.
416 ///{
419 ///}
420};
421
422/// Base class for instrumentation opportunities. All opportunities should
423/// inherit from this class and implement the virtual class members.
426
427 /// Construct an opportunity with location \p IP.
429
430 /// The instrumentation location of the opportunity.
432
433 /// The list of possible arguments for the instrumentation runtime function.
434 /// The order within the array determines the order of arguments. Arguments
435 /// may be disabled and will not be passed to the function call.
437
438 /// Whether the opportunity is enabled.
439 bool Enabled = true;
440
441 /// Helpers to cast values, pass them to the runtime, and replace them. To be
442 /// used as part of the getter/setter of a InstrumentationOpportunity.
443 ///{
444 static Value *forceCast(Value &V, Type &Ty, InstrumentorIRBuilderTy &IIRB);
447 return forceCast(V, Ty, IIRB);
448 }
449 static Value *replaceValue(Value &V, Value &NewV,
452 ///}
453
454 /// Instrument the value \p V using the configuration \p IConf, and
455 /// potentially, the caches \p ICaches.
458 InstrumentationCaches &ICaches) {
459 if (CB && !CB(*V))
460 return nullptr;
461
462 const DataLayout &DL = IIRB.IRB.GetInsertBlock()->getDataLayout();
463 IRTCallDescription IRTCallDesc(*this, getRetTy(V->getContext()));
464 auto *CI = IRTCallDesc.createLLVMCall(V, IConf, IIRB, DL, ICaches);
465 return CI;
466 }
467
468 /// Get the return type for the instrumentation runtime function.
469 virtual Type *getRetTy(LLVMContext &Ctx) const { return nullptr; }
470
471 /// Get the name of the instrumentation opportunity.
472 virtual StringRef getName() const = 0;
473
474 /// Get the opcode of the instruction instrumentation opportunity. Only valid
475 /// if it is instruction instrumentation.
476 unsigned getOpcode() const { return IP.getOpcode(); }
477
478 /// Get the location kind of the instrumentation opportunity.
480 return IP.getKind();
481 }
482
483 /// An optional callback that takes the value that is about to be
484 /// instrumented and can return false if it should be skipped.
485 ///{
486 using CallbackTy = std::function<bool(Value &)>;
487 CallbackTy CB = nullptr;
488 ///}
489
490 /// Add arguments available in all instrumentation opportunities.
492 bool PassId) {
493 const auto CB = IP.isPRE() ? getIdPre : getIdPost;
494 if (PassId) {
495 IRTArgs.push_back(
497 "A unique ID associated with the given instrumentor call",
498 IRTArg::NONE, CB, nullptr, true, true));
499 }
500 }
501
502 /// Get the opportunity identifier for the pre and post positions.
503 ///{
504 static Value *getIdPre(Value &V, Type &Ty, InstrumentationConfig &IConf,
506 static Value *getIdPost(Value &V, Type &Ty, InstrumentationConfig &IConf,
508 ///}
509
510 /// Compute the opportunity identifier for the current instrumentation epoch
511 /// \p CurrentEpoch. The identifiers are assigned consecutively as the epoch
512 /// advances. Epochs may have no identifier assigned (e.g., because no id was
513 /// requested). This function always returns the same identifier when called
514 /// multiple times with the same epoch.
515 static int32_t getIdFromEpoch(uint32_t CurrentEpoch) {
516 static DenseMap<uint32_t, int32_t> EpochIdMap;
517 static int32_t GlobalId = 0;
518 int32_t &EpochId = EpochIdMap[CurrentEpoch];
519 if (EpochId == 0)
520 EpochId = ++GlobalId;
521 return EpochId;
522 }
523};
524
525/// The base instrumentation opportunity class for instruction opportunities.
526/// Each instruction opportunity should inherit from this class and implement
527/// the virtual class members.
528template <unsigned Opcode>
530 virtual ~InstructionIO() {}
531
532 /// Construct an instruction opportunity.
535
536 /// Get the name of the instruction.
537 StringRef getName() const override {
538 return Instruction::getOpcodeName(Opcode);
539 }
540};
541
542/// The instrumentation opportunity for store instructions.
543struct StoreIO : public InstructionIO<Instruction::Store> {
544 virtual ~StoreIO() {};
545
546 /// Construct a store instruction opportunity.
547 StoreIO(bool IsPRE) : InstructionIO(IsPRE) {}
548
549 /// The selector of arguments for store opportunities.
550 ///{
565
568 ///}
569
570 /// Get the type of the stored value.
572 return IIRB.Int64Ty;
573 }
574
575 /// Initialize the store opportunity using the instrumentation config \p IConf
576 /// and the user config \p UserConfig.
578 ConfigTy *UserConfig = nullptr);
579
580 /// Getters and setters for the arguments of the instrumentation function for
581 /// the store opportunity.
582 ///{
583 static Value *getPointer(Value &V, Type &Ty, InstrumentationConfig &IConf,
585 static Value *setPointer(Value &V, Value &NewV, InstrumentationConfig &IConf,
587 static Value *getPointerAS(Value &V, Type &Ty, InstrumentationConfig &IConf,
589 static Value *getValue(Value &V, Type &Ty, InstrumentationConfig &IConf,
591 static Value *getValueSize(Value &V, Type &Ty, InstrumentationConfig &IConf,
593 static Value *getAlignment(Value &V, Type &Ty, InstrumentationConfig &IConf,
595 static Value *getValueTypeId(Value &V, Type &Ty, InstrumentationConfig &IConf,
597 static Value *getAtomicityOrdering(Value &V, Type &Ty,
600 static Value *getSyncScopeId(Value &V, Type &Ty, InstrumentationConfig &IConf,
602 static Value *isVolatile(Value &V, Type &Ty, InstrumentationConfig &IConf,
604 ///}
605
606 /// Create the store opportunities for pre and post positions. The
607 /// opportunities are also initialized with the arguments for their
608 /// instrumentation calls.
609 static void populate(InstrumentationConfig &IConf,
611 for (auto IsPRE : {true, false}) {
612 auto *AIC = IConf.allocate<StoreIO>(IsPRE);
613 AIC->init(IConf, IIRB);
614 }
615 }
616};
617
618/// The instrumentation opportunity for load instructions.
619struct LoadIO : public InstructionIO<Instruction::Load> {
620 virtual ~LoadIO() {};
621
622 /// Construct a load opportunity.
623 LoadIO(bool IsPRE) : InstructionIO(IsPRE) {}
624
625 /// The selector of arguments for load opportunities.
626 ///{
642
645 ///}
646
647 /// Get the type of the loaded value.
649 return IIRB.Int64Ty;
650 }
651
652 /// Initialize the load opportunity using the instrumentation config \p IConf
653 /// and the user config \p UserConfig.
655 ConfigTy *UserConfig = nullptr);
656
657 /// Getters and setters for the arguments of the instrumentation function for
658 /// the load opportunity.
659 ///{
660 static Value *getPointer(Value &V, Type &Ty, InstrumentationConfig &IConf,
662 static Value *setPointer(Value &V, Value &NewV, InstrumentationConfig &IConf,
664 static Value *getPointerAS(Value &V, Type &Ty, InstrumentationConfig &IConf,
666 static Value *getValue(Value &V, Type &Ty, InstrumentationConfig &IConf,
668 static Value *getValueSize(Value &V, Type &Ty, InstrumentationConfig &IConf,
670 static Value *getAlignment(Value &V, Type &Ty, InstrumentationConfig &IConf,
672 static Value *getValueTypeId(Value &V, Type &Ty, InstrumentationConfig &IConf,
674 static Value *getAtomicityOrdering(Value &V, Type &Ty,
677 static Value *getSyncScopeId(Value &V, Type &Ty, InstrumentationConfig &IConf,
679 static Value *isVolatile(Value &V, Type &Ty, InstrumentationConfig &IConf,
681 ///}
682
683 /// Create the store opportunities for PRE and POST positions.
684 static void populate(InstrumentationConfig &IConf,
686 for (auto IsPRE : {true, false}) {
687 auto *AIC = IConf.allocate<LoadIO>(IsPRE);
688 AIC->init(IConf, IIRB);
689 }
690 }
691};
692
693} // namespace instrumentor
694
695/// The Instrumentor pass.
696class InstrumentorPass : public RequiredPassInfoMixin<InstrumentorPass> {
697 using InstrumentationConfig = instrumentor::InstrumentationConfig;
698 using InstrumentorIRBuilderTy = instrumentor::InstrumentorIRBuilderTy;
699
700 /// The configuration and IR builder provided by the user.
701 InstrumentationConfig *UserIConf;
702 InstrumentorIRBuilderTy *UserIIRB;
703
704 PreservedAnalyses run(Module &M, InstrumentationConfig &IConf,
705 InstrumentorIRBuilderTy &IIRB, bool ReadConfig);
706
707public:
708 /// Construct an instrumentor pass that will use the instrumentation
709 /// configuration \p IC and the IR builder \p IIRB. If an IR builder is not
710 /// provided, a default builder is used. When the configuration is not
711 /// provided, it is read from the config file if available and otherwise a
712 /// default configuration is used.
713 InstrumentorPass(InstrumentationConfig *IC = nullptr,
714 InstrumentorIRBuilderTy *IIRB = nullptr)
715 : UserIConf(IC), UserIIRB(IIRB) {}
716
718};
719
720} // end namespace llvm
721
722#endif // LLVM_TRANSFORMS_IPO_INSTRUMENTOR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
This file defines an array type that can be indexed using scoped enum values.
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
ModuleAnalysisManager MAM
Basic Register Allocator
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
This class represents a function call, abstracting a target machine's calling convention.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Class to represent function types.
const char * getOpcodeName() const
InstrumentorPass(InstrumentationConfig *IC=nullptr, InstrumentorIRBuilderTy *IIRB=nullptr)
Construct an instrumentor pass that will use the instrumentation configuration IC and the IR builder ...
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A BumpPtrAllocator that allows only elements of a specific type to be allocated.
Definition Allocator.h:390
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition StringSaver.h:22
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:313
LLVM Value Representation.
Definition Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::function< Value *( Value &, Value &, InstrumentationConfig &, InstrumentorIRBuilderTy &)> SetterCallbackTy
std::function< Value *( Value &, Type &, InstrumentationConfig &, InstrumentorIRBuilderTy &)> GetterCallbackTy
Callback type for getting/setting a value for a instrumented opportunity.
This is an optimization pass for GlobalISel generic memory operations.
Op::Description Desc
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1916
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:874
A CRTP mix-in for passes that should not be skipped.
Boolean option bitset with a compile-time number of bits to store as many options as the enumeration ...
An option for the base configuration.
void setBool(bool B)
Set and get of the boolean value.
static std::unique_ptr< BaseConfigurationOption > createStringOption(InstrumentationConfig &IC, StringRef Name, StringRef Description, StringRef DefaultValue)
Create a string option with Name name, Description description and DefaultValue as string default val...
BaseConfigurationOption(StringRef Name, StringRef Desc, KindTy Kind)
}
KindTy
The possible types of options.
static std::unique_ptr< BaseConfigurationOption > createBoolOption(InstrumentationConfig &IC, StringRef Name, StringRef Description, bool DefaultValue)
Create a boolean option with Name name, Description description and DefaultValue as boolean default v...
GetterCallbackTy GetterCB
The callback for getting the value of the argument.
StringRef Description
A string with the description of the argument.
unsigned Flags
The flags that describe the properties of the argument.
Type * Ty
The type of the argument.
IRTArg(Type *Ty, StringRef Name, StringRef Description, unsigned Flags, GetterCallbackTy GetterCB, SetterCallbackTy SetterCB=nullptr, bool Enabled=true, bool NoCache=false)
Construct an argument.
bool Enabled
Whether the argument is enabled and should be passed to the function call.
SetterCallbackTy SetterCB
The callback for consuming the output value of the argument.
StringRef Name
A string with the name of the argument.
bool NoCache
Whether the argument value can be cached between the PRE and POST calls.
IRArgFlagTy
Flags describing the possible properties of an argument.
Helper to represent an instrumentation runtime function that is related to an instrumentation opportu...
bool isReplacable(IRTArg &IRTA) const
Return whether the IRTA argument can be replaced.
IRTCallDescription(InstrumentationOpportunity &IO, Type *RetTy=nullptr)
Construct an instrumentation function description linked to the IO instrumentation opportunity and Re...
bool MightRequireIndirection
Whether any argument may require indirection.
std::pair< std::string, std::string > createCBodies() const
Create a string representation of the function definition in C.
CallInst * createLLVMCall(Value *&V, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB, const DataLayout &DL, InstrumentationCaches &ICaches)
Create a call instruction that calls to the instrumentation function and passes the corresponding arg...
Type * RetTy
The return type of the instrumentation function.
InstrumentationOpportunity & IO
The instrumentation opportunity which it is linked to.
FunctionType * createLLVMSignature(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB, const DataLayout &DL, bool ForceIndirection)
Create the type of the instrumentation function.
std::pair< std::string, std::string > createCSignature(const InstrumentationConfig &IConf) const
Create a string representation of the function declaration in C.
unsigned NumReplaceableArgs
The number of arguments that can be replaced.
bool RequiresIndirection
Whether the function requires indirection in some argument.
bool isPotentiallyIndirect(IRTArg &IRTA) const
Return whether the function may have any indirect argument.
InstructionIO(bool IsPRE)
Construct an instruction opportunity.
StringRef getName() const override
Get the name of the instruction.
Helper that represent the caches for instrumentation call arguments.
The class that contains the configuration for the instrumentor.
virtual void populate(InstrumentorIRBuilderTy &IIRB)
Populate the instrumentation opportunities.
void addChoice(InstrumentationOpportunity &IO, LLVMContext &Ctx)
Register instrumentation opportunity IO.
std::unique_ptr< BaseConfigurationOption > HostEnabled
std::unique_ptr< BaseConfigurationOption > GPUEnabled
BumpPtrAllocator StringAllocator
Utilities for allocating and building strings.
std::string getRTName(StringRef Prefix, StringRef Name, StringRef Suffix1="", StringRef Suffix2="") const
Get the instrumentation function name.
std::unique_ptr< BaseConfigurationOption > RuntimeStubsFile
StringRef getRTName() const
Get the runtime prefix for the instrumentation runtime functions.
void addBaseChoice(BaseConfigurationOption *BCO)
Add the base configuration option BCO into the list of base options.
static Ty * allocate(ArgsTy &&...Args)
Allocate an object of type Ty using a bump allocator and construct it with the Args arguments.
SmallVector< BaseConfigurationOption * > BaseConfigurationOptions
The list of enabled base configuration options.
InstrumentationConfig()
Construct an instrumentation configuration with the base options.
std::unique_ptr< BaseConfigurationOption > TargetRegex
std::unique_ptr< BaseConfigurationOption > RuntimePrefix
The base configuration options.
EnumeratedArray< StringMap< InstrumentationOpportunity * >, InstrumentationLocation::KindTy > IChoices
The map registered instrumentation opportunities.
Helper to represent an instrumentation location, which is composed of an instrumentation opportunity ...
unsigned getOpcode() const
Get the opcode of the instruction instrumentation location.
KindTy getKind() const
Return the type and position.
InstrumentationLocation(KindTy Kind)
Construct an instrumentation location that is not instrumenting an instruction.
static KindTy getKindFromStr(StringRef S)
Return the location kind described by a string.
static StringRef getKindStr(KindTy Kind)
Return the string representation given a location kind.
KindTy
The supported location kinds, which are composed of a opportunity type and position.
static bool isPRE(KindTy Kind)
Return whether a location kind is positioned before the event occurs.
bool isPRE() const
Return whether the instrumentation location is before the event occurs.
InstrumentationLocation(unsigned Opcode, bool IsPRE)
Construct an instrumentation location belonging to the instrumentation of an instruction.
Base class for instrumentation opportunities.
InstrumentationLocation::KindTy getLocationKind() const
Get the location kind of the instrumentation opportunity.
bool Enabled
Whether the opportunity is enabled.
static Value * getIdPre(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
Get the opportunity identifier for the pre and post positions.
static Value * forceCast(Value &V, Type &Ty, InstrumentorIRBuilderTy &IIRB)
Helpers to cast values, pass them to the runtime, and replace them.
static int32_t getIdFromEpoch(uint32_t CurrentEpoch)
}
std::function< bool(Value &)> CallbackTy
An optional callback that takes the value that is about to be instrumented and can return false if it...
static Value * getIdPost(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
static Value * getValue(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
virtual Value * instrument(Value *&V, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB, InstrumentationCaches &ICaches)
}
static Value * replaceValue(Value &V, Value &NewV, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
unsigned getOpcode() const
Get the opcode of the instruction instrumentation opportunity.
virtual StringRef getName() const =0
Get the name of the instrumentation opportunity.
InstrumentationLocation IP
The instrumentation location of the opportunity.
InstrumentationOpportunity(const InstrumentationLocation IP)
Construct an opportunity with location IP.
SmallVector< IRTArg > IRTArgs
The list of possible arguments for the instrumentation runtime function.
void addCommonArgs(InstrumentationConfig &IConf, LLVMContext &Ctx, bool PassId)
}
virtual Type * getRetTy(LLVMContext &Ctx) const
Get the return type for the instrumentation runtime function.
An IR builder augmented with extra information for the instrumentor pass.
IRBuilder< ConstantFolder, IRBuilderCallbackInserter > IRB
The underlying IR builder with insertion callback.
LoadIO(bool IsPRE)
Construct a load opportunity.
static Value * getValueSize(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
static Value * getSyncScopeId(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
static Value * getAtomicityOrdering(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
virtual Type * getValueType(InstrumentorIRBuilderTy &IIRB) const
}
static Value * getValue(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
ConfigKind
The selector of arguments for load opportunities.
static Value * getAlignment(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
static Value * getPointer(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
Getters and setters for the arguments of the instrumentation function for the load opportunity.
static Value * isVolatile(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
static Value * setPointer(Value &V, Value &NewV, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
BaseConfigTy< ConfigKind > ConfigTy
static Value * getPointerAS(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
static void populate(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
}
static Value * getValueTypeId(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
void init(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB, ConfigTy *UserConfig=nullptr)
Initialize the load opportunity using the instrumentation config IConf and the user config UserConfig...
static Value * getPointer(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
Getters and setters for the arguments of the instrumentation function for the store opportunity.
static void populate(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
}
static Value * getValueTypeId(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
virtual Type * getValueType(InstrumentorIRBuilderTy &IIRB) const
}
static Value * getSyncScopeId(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
static Value * getPointerAS(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
ConfigKind
The selector of arguments for store opportunities.
static Value * getAlignment(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
static Value * getValue(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
static Value * setPointer(Value &V, Value &NewV, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
static Value * isVolatile(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
static Value * getValueSize(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
BaseConfigTy< ConfigKind > ConfigTy
void init(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB, ConfigTy *UserConfig=nullptr)
Initialize the store opportunity using the instrumentation config IConf and the user config UserConfi...
static Value * getAtomicityOrdering(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
StoreIO(bool IsPRE)
Construct a store instruction opportunity.
Helper union that holds any possible option type.