From 51f72d02f4e9fe65011bd53f15c398ed63c62887 Mon Sep 17 00:00:00 2001 From: Ethan Luis McDonough Date: Tue, 28 Jan 2025 12:28:51 -0600 Subject: [PATCH 1/2] [Instrumentor] Add target configuration options --- .../Instrumentation/InstrumentorConfig.def | 6 +++++ .../Instrumentation/Instrumentor.cpp | 23 +++++++++++++++++++ .../Instrumentor/default_config.json | 5 +++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/Transforms/Instrumentation/InstrumentorConfig.def b/llvm/include/llvm/Transforms/Instrumentation/InstrumentorConfig.def index 8e2964811aa4e..32b9fb6f5cc53 100644 --- a/llvm/include/llvm/Transforms/Instrumentation/InstrumentorConfig.def +++ b/llvm/include/llvm/Transforms/Instrumentation/InstrumentorConfig.def @@ -23,6 +23,12 @@ CVALUE(Base, bool, PrintRuntimeSignatures, true) CVALUE(Base, std::string, StubRuntimePath, "rt.c") +CVALUE(Base, std::string, TargetRegex, ".*") + +CVALUE(Base, bool, InstrumentHost, true) + +CVALUE(Base, bool, InstrumentGpu, true) + SECTION_END(Base) ///} diff --git a/llvm/lib/Transforms/Instrumentation/Instrumentor.cpp b/llvm/lib/Transforms/Instrumentation/Instrumentor.cpp index 12f61607673fd..2a4143afa8175 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(); @@ -939,6 +942,7 @@ bool InstrumentorImpl::instrumentCall(CallBase &I, break; case Intrinsic::not_intrinsic: Changed |= instrumentGenericCall(I, P); + break; default: break; } @@ -1705,10 +1709,29 @@ 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.TargetRegex); + std::string ErrMsg; + + if (!TargetRegex.isValid(ErrMsg)) { + errs() << "WARNING: failed to parse TargetRegex: " << ErrMsg << "\n"; + } + + return ((IsGPU && IC.Base.InstrumentGpu) || + (!IsGPU && IC.Base.InstrumentHost)) && + 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..c8fe311210b11 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", + "TargetRegex": ".*", + "InstrumentHost": true, + "InstrumentGpu": true }, "alloca": { "EnabledPost": true, From 686d0f585a8b32d5d47b29bf0360652afa9808d1 Mon Sep 17 00:00:00 2001 From: Ethan Luis McDonough Date: Tue, 4 Feb 2025 04:33:26 -0600 Subject: [PATCH 2/2] [Instrumentor] Rename target options --- .../Transforms/Instrumentation/InstrumentorConfig.def | 11 ++++++++--- llvm/lib/Transforms/Instrumentation/Instrumentor.cpp | 8 ++++---- .../Instrumentation/Instrumentor/default_config.json | 6 +++--- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/llvm/include/llvm/Transforms/Instrumentation/InstrumentorConfig.def b/llvm/include/llvm/Transforms/Instrumentation/InstrumentorConfig.def index 32b9fb6f5cc53..d99be04163738 100644 --- a/llvm/include/llvm/Transforms/Instrumentation/InstrumentorConfig.def +++ b/llvm/include/llvm/Transforms/Instrumentation/InstrumentorConfig.def @@ -23,11 +23,16 @@ CVALUE(Base, bool, PrintRuntimeSignatures, true) CVALUE(Base, std::string, StubRuntimePath, "rt.c") -CVALUE(Base, std::string, TargetRegex, ".*") +/// 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, ".*") -CVALUE(Base, bool, InstrumentHost, true) +/// Instrument non-GPU targets +CVALUE(Base, bool, EnabledTargetHost, true) -CVALUE(Base, bool, InstrumentGpu, 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 2a4143afa8175..3c4ee3bda4e8a 100644 --- a/llvm/lib/Transforms/Instrumentation/Instrumentor.cpp +++ b/llvm/lib/Transforms/Instrumentation/Instrumentor.cpp @@ -942,7 +942,6 @@ bool InstrumentorImpl::instrumentCall(CallBase &I, break; case Intrinsic::not_intrinsic: Changed |= instrumentGenericCall(I, P); - break; default: break; } @@ -1713,15 +1712,16 @@ 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.TargetRegex); + 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.InstrumentGpu) || - (!IsGPU && IC.Base.InstrumentHost)) && + return ((IsGPU && IC.Base.EnabledTargetGPU) || + (!IsGPU && IC.Base.EnabledTargetHost)) && TargetRegex.match(TripleStr); } diff --git a/llvm/test/Instrumentation/Instrumentor/default_config.json b/llvm/test/Instrumentation/Instrumentor/default_config.json index c8fe311210b11..ac38f16859de8 100644 --- a/llvm/test/Instrumentation/Instrumentor/default_config.json +++ b/llvm/test/Instrumentation/Instrumentor/default_config.json @@ -3,9 +3,9 @@ "RuntimeName": "__instrumentor_", "PrintRuntimeSignatures": true, "StubRuntimePath": "rt.c", - "TargetRegex": ".*", - "InstrumentHost": true, - "InstrumentGpu": true + "EnabledTargetRegex": ".*", + "EnabledTargetHost": true, + "EnabledTargetGPU": true }, "alloca": { "EnabledPost": true,