Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions llvm/include/llvm/Transforms/Instrumentation/Instrumentor.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,15 @@ struct InstrumentationConfig {
DemangleFunctionNames = BaseConfigurationOpportunity::getBoolOption(
*this, "demangle_function_names",
"Demangle functions names passed to the runtime.", true);
TargetRegex = BaseConfigurationOpportunity::getStringOption(
*this, "target_regex",
"Regular expression to be matched against the module target. "
"Only targets that match this regex will be instrumented",
"");
HostEnabled = BaseConfigurationOpportunity::getBoolOption(
*this, "host_enabled", "Instrument non-GPU targets", true);
GPUEnabled = BaseConfigurationOpportunity::getBoolOption(
*this, "gpu_enabled", "Instrument GPU targets", true);
}

bool ReadConfig = true;
Expand All @@ -425,6 +434,9 @@ struct InstrumentationConfig {
BaseConfigurationOpportunity *RuntimePrefix;
BaseConfigurationOpportunity *RuntimeStubsFile;
BaseConfigurationOpportunity *DemangleFunctionNames;
BaseConfigurationOpportunity *TargetRegex;
BaseConfigurationOpportunity *HostEnabled;
BaseConfigurationOpportunity *GPUEnabled;

EnumeratedArray<StringMap<InstrumentationOpportunity *>,
InstrumentationLocation::KindTy>
Expand Down
26 changes: 26 additions & 0 deletions llvm/lib/Transforms/Instrumentation/Instrumentor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "llvm/Support/JSON.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
Expand Down Expand Up @@ -367,6 +368,7 @@ class InstrumentorImpl final {
bool instrument();

private:
bool shouldInstrumentTarget();
bool shouldInstrumentFunction(Function &Fn);
bool shouldInstrumentGlobalVariable(GlobalVariable &GV);

Expand Down Expand Up @@ -434,6 +436,28 @@ class InstrumentorImpl final {

} // end anonymous namespace

bool InstrumentorImpl::shouldInstrumentTarget() {
const auto &TripleStr = M.getTargetTriple();
Triple T(TripleStr);
const bool IsGPU = T.isAMDGPU() || T.isNVPTX();

bool RegexMatches = true;
const auto TargetRegexStr = IConf.TargetRegex->getString();
if (!TargetRegexStr.empty()) {
llvm::Regex TargetRegex(TargetRegexStr);
std::string ErrMsg;
if (!TargetRegex.isValid(ErrMsg)) {
errs() << "WARNING: failed to parse target regex: " << ErrMsg << "\n";
return false;
}
RegexMatches = TargetRegex.match(TripleStr);
}

return ((IsGPU && IConf.GPUEnabled->getBool()) ||
(!IsGPU && IConf.HostEnabled->getBool())) &&
RegexMatches;
}

bool InstrumentorImpl::shouldInstrumentFunction(Function &Fn) {
if (Fn.isDeclaration())
return false;
Expand Down Expand Up @@ -567,6 +591,8 @@ bool InstrumentorImpl::instrumentModule() {

bool InstrumentorImpl::instrument() {
bool Changed = false;
if (!shouldInstrumentTarget())
return Changed;

for (auto &ChoiceIt :
IConf.IChoices[InstrumentationLocation::INSTRUCTION_PRE])
Expand Down
8 changes: 7 additions & 1 deletion llvm/test/Instrumentation/Instrumentor/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
"runtime_stubs_file": "test.c",
"runtime_stubs_file.description": "The file into which runtime stubs should be written.",
"demangle_function_names": true,
"demangle_function_names.description": "Demangle functions names passed to the runtime."
"demangle_function_names.description": "Demangle functions names passed to the runtime.",
"target_regex": "",
"target_regex.description": "Regular expression to be matched against the module target. Only targets that match this regex will be instrumented",
"host_enabled": true,
"host_enabled.description": "Instrument non-GPU targets",
"gpu_enabled": true,
"gpu_enabled.description": "Instrument GPU targets"
},
"module_pre": {
"module": {
Expand Down