Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ CVALUE(Base, bool, PrintRuntimeSignatures, true)

CVALUE(Base, std::string, StubRuntimePath, "rt.c")

/// Regex string that is matched against the current target.
/// This target option has the highest precedence. If the target
/// string doesn't match the regex, it won't be instrumented.
CVALUE(Base, std::string, EnabledTargetRegex, ".*")

/// Instrument non-GPU targets
CVALUE(Base, bool, EnabledTargetHost, true)

/// Instrument GPU targets (AMDGPU and NVPTX)
CVALUE(Base, bool, EnabledTargetGPU, true)

SECTION_END(Base)
///}

Expand Down
23 changes: 23 additions & 0 deletions llvm/lib/Transforms/Instrumentation/Instrumentor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"

Expand Down Expand Up @@ -456,6 +457,8 @@ class InstrumentorImpl final {
bool instrumentMainFunction(Function &MainFn, InstrumentorConfig::Position P);
bool instrumentModule(InstrumentorConfig::Position P);

bool canInstrumentTarget();

DenseMap<Value *, CallInst *> BasePtrMap;
bool instrumentBasePointer(Value &ArgOrInst);
bool removeUnusedBasePointers();
Expand Down Expand Up @@ -1705,10 +1708,30 @@ void InstrumentorImpl::addCtorOrDtor(bool Ctor) {
}
}

bool InstrumentorImpl::canInstrumentTarget() {
const auto TripleStr = M.getTargetTriple();
const auto &T = Triple(TripleStr);
const bool IsGPU = T.isAMDGPU() || T.isNVPTX();
llvm::Regex TargetRegex(IC.Base.EnabledTargetRegex);
std::string ErrMsg;

if (!TargetRegex.isValid(ErrMsg)) {
errs() << "WARNING: failed to parse TargetRegex: " << ErrMsg << "\n";
return false;
}

return ((IsGPU && IC.Base.EnabledTargetGPU) ||
(!IsGPU && IC.Base.EnabledTargetHost)) &&
TargetRegex.match(TripleStr);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the regex was invalid, the match is guaranteed to work and return false?

}

bool InstrumentorImpl::instrument() {
bool Changed = false;
printRuntimeSignatures();

if (!canInstrumentTarget())
return Changed;

Function *MainFn = nullptr;

if (IC.module.isEnabled(InstrumentorConfig::PRE) ||
Expand Down
5 changes: 4 additions & 1 deletion llvm/test/Instrumentation/Instrumentor/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"Base": {
"RuntimeName": "__instrumentor_",
"PrintRuntimeSignatures": true,
"StubRuntimePath": "rt.c"
"StubRuntimePath": "rt.c",
"EnabledTargetRegex": ".*",
"EnabledTargetHost": true,
"EnabledTargetGPU": true
},
"alloca": {
"EnabledPost": true,
Expand Down