diff --git a/llvm/include/llvm/Transforms/Instrumentation/InstrumentorConfig.def b/llvm/include/llvm/Transforms/Instrumentation/InstrumentorConfig.def index 8e2964811aa4e..d99be04163738 100644 --- a/llvm/include/llvm/Transforms/Instrumentation/InstrumentorConfig.def +++ b/llvm/include/llvm/Transforms/Instrumentation/InstrumentorConfig.def @@ -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) ///} diff --git a/llvm/lib/Transforms/Instrumentation/Instrumentor.cpp b/llvm/lib/Transforms/Instrumentation/Instrumentor.cpp index 12f61607673fd..3c4ee3bda4e8a 100644 --- a/llvm/lib/Transforms/Instrumentation/Instrumentor.cpp +++ b/llvm/lib/Transforms/Instrumentation/Instrumentor.cpp @@ -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" @@ -456,6 +457,8 @@ class InstrumentorImpl final { bool instrumentMainFunction(Function &MainFn, InstrumentorConfig::Position P); bool instrumentModule(InstrumentorConfig::Position P); + bool canInstrumentTarget(); + DenseMap BasePtrMap; bool instrumentBasePointer(Value &ArgOrInst); bool removeUnusedBasePointers(); @@ -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); +} + bool InstrumentorImpl::instrument() { bool Changed = false; printRuntimeSignatures(); + if (!canInstrumentTarget()) + return Changed; + Function *MainFn = nullptr; if (IC.module.isEnabled(InstrumentorConfig::PRE) || diff --git a/llvm/test/Instrumentation/Instrumentor/default_config.json b/llvm/test/Instrumentation/Instrumentor/default_config.json index 7ec3212b450d8..ac38f16859de8 100644 --- a/llvm/test/Instrumentation/Instrumentor/default_config.json +++ b/llvm/test/Instrumentation/Instrumentor/default_config.json @@ -2,7 +2,10 @@ "Base": { "RuntimeName": "__instrumentor_", "PrintRuntimeSignatures": true, - "StubRuntimePath": "rt.c" + "StubRuntimePath": "rt.c", + "EnabledTargetRegex": ".*", + "EnabledTargetHost": true, + "EnabledTargetGPU": true }, "alloca": { "EnabledPost": true,