[llvm-branch-commits] [llvm] [Instrumentor] Add a global function regexp to limit the instrumentation (PR #196234)

2026-05-14 Thread Johannes Doerfert via llvm-branch-commits

https://github.com/jdoerfert updated 
https://github.com/llvm/llvm-project/pull/196234

>From 18edc05fa09fe3103a691056f826cea860185736 Mon Sep 17 00:00:00 2001
From: Johannes Doerfert 
Date: Mon, 4 May 2026 15:19:00 -0700
Subject: [PATCH] [Instrumentor] Add a global function regexp to limit the
 instrumentation

Only functions that match the "function_regex" will be instrumented,
or if they have the instrumentation attribute.
---
 .../llvm/Transforms/IPO/Instrumentor.h|  8 ++-
 llvm/lib/Transforms/IPO/Instrumentor.cpp  | 26 +++--
 .../Instrumentor/bad_function_regex.json  | 26 +
 .../Instrumentor/bad_function_regexp.ll   | 13 +
 .../Instrumentor/default_config.json  |  4 +-
 .../Instrumentor/function_regex.json  | 26 +
 .../Instrumentor/function_regex.ll| 57 +++
 7 files changed, 154 insertions(+), 6 deletions(-)
 create mode 100644 
llvm/test/Instrumentation/Instrumentor/bad_function_regex.json
 create mode 100644 
llvm/test/Instrumentation/Instrumentor/bad_function_regexp.ll
 create mode 100644 llvm/test/Instrumentation/Instrumentor/function_regex.json
 create mode 100644 llvm/test/Instrumentation/Instrumentor/function_regex.ll

diff --git a/llvm/include/llvm/Transforms/IPO/Instrumentor.h 
b/llvm/include/llvm/Transforms/IPO/Instrumentor.h
index 747c3d110b650..ba3ad2498c873 100644
--- a/llvm/include/llvm/Transforms/IPO/Instrumentor.h
+++ b/llvm/include/llvm/Transforms/IPO/Instrumentor.h
@@ -357,7 +357,12 @@ struct InstrumentationConfig {
 TargetRegex = BaseConfigurationOption::createStringOption(
 *this, "target_regex",
 "Regular expression to be matched against the module target. "
-"Only targets that match this regex will be instrumented",
+"Only targets that match this regex will be instrumented.",
+"");
+FunctionRegex = BaseConfigurationOption::createStringOption(
+*this, "function_regex",
+"Regular expression to be matched against a function name. "
+"Only functions that match this regex will be instrumented.",
 "");
 DemangleFunctionNames = BaseConfigurationOption::createBoolOption(
 *this, "demangle_function_names",
@@ -424,6 +429,7 @@ struct InstrumentationConfig {
   std::unique_ptr RuntimeStubsFile;
   std::unique_ptr DemangleFunctionNames;
   std::unique_ptr TargetRegex;
+  std::unique_ptr FunctionRegex;
   std::unique_ptr HostEnabled;
   std::unique_ptr GPUEnabled;
 
diff --git a/llvm/lib/Transforms/IPO/Instrumentor.cpp 
b/llvm/lib/Transforms/IPO/Instrumentor.cpp
index bffc84f1d35e4..3862bfbbee9fa 100644
--- a/llvm/lib/Transforms/IPO/Instrumentor.cpp
+++ b/llvm/lib/Transforms/IPO/Instrumentor.cpp
@@ -161,6 +161,9 @@ class InstrumentorImpl final {
   /// The instrumentor configuration.
   InstrumentationConfig &IConf;
 
+  /// The function regex filter, if any.
+  Regex ParsedFunctionRegex;
+
   /// The underlying module.
   Module &M;
 
@@ -176,13 +179,13 @@ bool InstrumentorImpl::shouldInstrumentTarget() {
   const bool IsGPU = T.isAMDGPU() || T.isNVPTX();
 
   bool RegexMatches = true;
-  const auto TargetRegexStr = IConf.TargetRegex->getString();
+  StringRef TargetRegexStr = IConf.TargetRegex->getString();
   if (!TargetRegexStr.empty()) {
-llvm::Regex TargetRegex(TargetRegexStr);
+Regex TargetRegex(TargetRegexStr);
 std::string ErrMsg;
 if (!TargetRegex.isValid(ErrMsg)) {
   IIRB.Ctx.diagnose(DiagnosticInfoInstrumentation(
-  Twine("failed to parse target regex: ") + ErrMsg, DS_Warning));
+  Twine("failed to parse target regex: ") + ErrMsg, DS_Error));
   return false;
 }
 RegexMatches = TargetRegex.match(T.str());
@@ -197,7 +200,11 @@ bool InstrumentorImpl::shouldInstrumentTarget() {
 bool InstrumentorImpl::shouldInstrumentFunction(Function &Fn) {
   if (Fn.isDeclaration())
 return false;
-  return !Fn.getName().starts_with(IConf.getRTName()) ||
+  bool RegexMatches = true;
+  StringRef FunctionRegexStr = IConf.FunctionRegex->getString();
+  if (!FunctionRegexStr.empty())
+RegexMatches = ParsedFunctionRegex.match(Fn.getName());
+  return (RegexMatches && !Fn.getName().starts_with(IConf.getRTName())) ||
  Fn.hasFnAttribute("instrument");
 }
 
@@ -282,6 +289,17 @@ bool InstrumentorImpl::instrument() {
   if (!shouldInstrumentTarget())
 return Changed;
 
+  StringRef FunctionRegexStr = IConf.FunctionRegex->getString();
+  if (!FunctionRegexStr.empty()) {
+ParsedFunctionRegex = Regex(FunctionRegexStr);
+std::string ErrMsg;
+if (!ParsedFunctionRegex.isValid(ErrMsg)) {
+  IIRB.Ctx.diagnose(DiagnosticInfoInstrumentation(
+  Twine("failed to parse target regex: ") + ErrMsg, DS_Error));
+  return false;
+}
+  }
+
   for (auto &[Name, IO] :
IConf.IChoices[InstrumentationLocation::INSTRUCTION_PRE])
 if (IO->Enabled)
diff --git a/llvm/test/Instrumentation/Instrumentor/bad_function_regex.json 
b/llvm/test/Instrum

[llvm-branch-commits] [llvm] [Instrumentor] Add a global function regexp to limit the instrumentation (PR #196234)

2026-05-08 Thread Johannes Doerfert via llvm-branch-commits

https://github.com/jdoerfert updated 
https://github.com/llvm/llvm-project/pull/196234

>From 42f99d61f246b65f5253e10b2b118c62406b174b Mon Sep 17 00:00:00 2001
From: Johannes Doerfert 
Date: Mon, 4 May 2026 15:19:00 -0700
Subject: [PATCH] [Instrumentor] Add a global function regexp to limit the
 instrumentation

Only functions that match the "function_regex" will be instrumented,
or if they have the instrumentation attribute.
---
 .../llvm/Transforms/IPO/Instrumentor.h|  8 ++-
 llvm/lib/Transforms/IPO/Instrumentor.cpp  | 22 ++-
 .../Instrumentor/default_config.json  |  4 +-
 .../Instrumentor/function_regex.json  | 26 +
 .../Instrumentor/function_regex.ll| 57 +++
 5 files changed, 113 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/Instrumentation/Instrumentor/function_regex.json
 create mode 100644 llvm/test/Instrumentation/Instrumentor/function_regex.ll

diff --git a/llvm/include/llvm/Transforms/IPO/Instrumentor.h 
b/llvm/include/llvm/Transforms/IPO/Instrumentor.h
index 796104944f7a1..63a37931e98dc 100644
--- a/llvm/include/llvm/Transforms/IPO/Instrumentor.h
+++ b/llvm/include/llvm/Transforms/IPO/Instrumentor.h
@@ -356,7 +356,12 @@ struct InstrumentationConfig {
 TargetRegex = BaseConfigurationOption::createStringOption(
 *this, "target_regex",
 "Regular expression to be matched against the module target. "
-"Only targets that match this regex will be instrumented",
+"Only targets that match this regex will be instrumented.",
+"");
+FunctionRegex = BaseConfigurationOption::createStringOption(
+*this, "function_regex",
+"Regular expression to be matched against a function name. "
+"Only functions that match this regex will be instrumented.",
 "");
 DemangleFunctionNames = BaseConfigurationOption::createBoolOption(
 *this, "demangle_function_names",
@@ -423,6 +428,7 @@ struct InstrumentationConfig {
   std::unique_ptr RuntimeStubsFile;
   std::unique_ptr DemangleFunctionNames;
   std::unique_ptr TargetRegex;
+  std::unique_ptr FunctionRegex;
   std::unique_ptr HostEnabled;
   std::unique_ptr GPUEnabled;
 
diff --git a/llvm/lib/Transforms/IPO/Instrumentor.cpp 
b/llvm/lib/Transforms/IPO/Instrumentor.cpp
index e42befada5ab2..23fbeb9e7ec33 100644
--- a/llvm/lib/Transforms/IPO/Instrumentor.cpp
+++ b/llvm/lib/Transforms/IPO/Instrumentor.cpp
@@ -158,6 +158,9 @@ class InstrumentorImpl final {
   /// The instrumentor configuration.
   InstrumentationConfig &IConf;
 
+  /// The function regex filter, if any.
+  Regex ParsedFunctionRegex;
+
   /// The underlying module.
   Module &M;
 
@@ -175,7 +178,7 @@ bool InstrumentorImpl::shouldInstrumentTarget() {
   bool RegexMatches = true;
   const auto TargetRegexStr = IConf.TargetRegex->getString();
   if (!TargetRegexStr.empty()) {
-llvm::Regex TargetRegex(TargetRegexStr);
+Regex TargetRegex(TargetRegexStr);
 std::string ErrMsg;
 if (!TargetRegex.isValid(ErrMsg)) {
   IIRB.Ctx.diagnose(DiagnosticInfoInstrumentation(
@@ -194,7 +197,11 @@ bool InstrumentorImpl::shouldInstrumentTarget() {
 bool InstrumentorImpl::shouldInstrumentFunction(Function &Fn) {
   if (Fn.isDeclaration())
 return false;
-  return !Fn.getName().starts_with(IConf.getRTName()) ||
+  bool RegexMatches = true;
+  const auto FunctionRegexStr = IConf.FunctionRegex->getString();
+  if (!FunctionRegexStr.empty())
+RegexMatches = ParsedFunctionRegex.match(Fn.getName());
+  return (RegexMatches && !Fn.getName().starts_with(IConf.getRTName())) ||
  Fn.hasFnAttribute("instrument");
 }
 
@@ -279,6 +286,17 @@ bool InstrumentorImpl::instrument() {
   if (!shouldInstrumentTarget())
 return Changed;
 
+  const auto FunctionRegexStr = IConf.FunctionRegex->getString();
+  if (!FunctionRegexStr.empty()) {
+ParsedFunctionRegex = Regex(FunctionRegexStr);
+std::string ErrMsg;
+if (!ParsedFunctionRegex.isValid(ErrMsg)) {
+  IIRB.Ctx.diagnose(DiagnosticInfoInstrumentation(
+  Twine("failed to parse target regex: ") + ErrMsg, DS_Warning));
+  return false;
+}
+  }
+
   for (auto &[Name, IO] :
IConf.IChoices[InstrumentationLocation::INSTRUCTION_PRE])
 if (IO->Enabled)
diff --git a/llvm/test/Instrumentation/Instrumentor/default_config.json 
b/llvm/test/Instrumentation/Instrumentor/default_config.json
index 01dd8f8a8f720..c97701d4d84c5 100644
--- a/llvm/test/Instrumentation/Instrumentor/default_config.json
+++ b/llvm/test/Instrumentation/Instrumentor/default_config.json
@@ -5,7 +5,9 @@
 "runtime_stubs_file": "",
 "runtime_stubs_file.description": "The file into which runtime stubs 
should be written.",
 "target_regex": "",
-"target_regex.description": "Regular expression to be matched against the 
module target. Only targets that match this regex will be instrumented",
+"target_regex.description": "Regular expression to be matched against the 
module target. 

[llvm-branch-commits] [llvm] [Instrumentor] Add a global function regexp to limit the instrumentation (PR #196234)

2026-05-07 Thread Matt Arsenault via llvm-branch-commits


@@ -274,6 +281,17 @@ bool InstrumentorImpl::instrument() {
   if (!shouldInstrumentTarget())
 return Changed;
 
+  const auto FunctionRegexStr = IConf.FunctionRegex->getString();
+  if (!FunctionRegexStr.empty()) {
+ParsedFunctionRegex = Regex(FunctionRegexStr);
+std::string ErrMsg;
+if (!ParsedFunctionRegex.isValid(ErrMsg)) {
+  IIRB.Ctx.diagnose(DiagnosticInfoInstrumentation(

arsenm wrote:

missing error test 

https://github.com/llvm/llvm-project/pull/196234
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [Instrumentor] Add a global function regexp to limit the instrumentation (PR #196234)

2026-05-07 Thread Matt Arsenault via llvm-branch-commits


@@ -190,7 +193,11 @@ bool InstrumentorImpl::shouldInstrumentTarget() {
 bool InstrumentorImpl::shouldInstrumentFunction(Function &Fn) {
   if (Fn.isDeclaration())
 return false;
-  return !Fn.getName().starts_with(IConf.getRTName()) ||
+  bool RegexMatches = true;
+  const auto FunctionRegexStr = IConf.FunctionRegex->getString();

arsenm wrote:

No auto, I have no idea which string type this is 

https://github.com/llvm/llvm-project/pull/196234
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [Instrumentor] Add a global function regexp to limit the instrumentation (PR #196234)

2026-05-06 Thread via llvm-branch-commits

llvmorg-github-actions[bot] wrote:




@llvm/pr-subscribers-llvm-transforms

Author: Johannes Doerfert (jdoerfert)


Changes

Only functions that match the "function_regex" will be instrumented, or if they 
have the instrumentation attribute.

---
Full diff: https://github.com/llvm/llvm-project/pull/196234.diff


5 Files Affected:

- (modified) llvm/include/llvm/Transforms/IPO/Instrumentor.h (+7-1) 
- (modified) llvm/lib/Transforms/IPO/Instrumentor.cpp (+20-2) 
- (modified) llvm/test/Instrumentation/Instrumentor/default_config.json (+3-1) 
- (added) llvm/test/Instrumentation/Instrumentor/function_regex.json (+26) 
- (added) llvm/test/Instrumentation/Instrumentor/function_regex.ll (+57) 


``diff
diff --git a/llvm/include/llvm/Transforms/IPO/Instrumentor.h 
b/llvm/include/llvm/Transforms/IPO/Instrumentor.h
index 9bd809230d14d..a5859fc20505c 100644
--- a/llvm/include/llvm/Transforms/IPO/Instrumentor.h
+++ b/llvm/include/llvm/Transforms/IPO/Instrumentor.h
@@ -355,7 +355,12 @@ struct InstrumentationConfig {
 TargetRegex = BaseConfigurationOption::getStringOption(
 *this, "target_regex",
 "Regular expression to be matched against the module target. "
-"Only targets that match this regex will be instrumented",
+"Only targets that match this regex will be instrumented.",
+"");
+FunctionRegex = BaseConfigurationOption::getStringOption(
+*this, "function_regex",
+"Regular expression to be matched against a function name. "
+"Only functions that match this regex will be instrumented.",
 "");
 HostEnabled = BaseConfigurationOption::getBoolOption(
 *this, "host_enabled", "Instrument non-GPU targets", true);
@@ -419,6 +424,7 @@ struct InstrumentationConfig {
   BaseConfigurationOption *RuntimeStubsFile;
   BaseConfigurationOption *DemangleFunctionNames;
   BaseConfigurationOption *TargetRegex;
+  BaseConfigurationOption *FunctionRegex;
   BaseConfigurationOption *HostEnabled;
   BaseConfigurationOption *GPUEnabled;
 
diff --git a/llvm/lib/Transforms/IPO/Instrumentor.cpp 
b/llvm/lib/Transforms/IPO/Instrumentor.cpp
index 0265447832357..9fd04ab911135 100644
--- a/llvm/lib/Transforms/IPO/Instrumentor.cpp
+++ b/llvm/lib/Transforms/IPO/Instrumentor.cpp
@@ -154,6 +154,9 @@ class InstrumentorImpl final {
   /// The instrumentor configuration.
   InstrumentationConfig &IConf;
 
+  /// The function regex filter, if any.
+  Regex ParsedFunctionRegex;
+
   /// The underlying module.
   Module &M;
 
@@ -171,7 +174,7 @@ bool InstrumentorImpl::shouldInstrumentTarget() {
   bool RegexMatches = true;
   const auto TargetRegexStr = IConf.TargetRegex->getString();
   if (!TargetRegexStr.empty()) {
-llvm::Regex TargetRegex(TargetRegexStr);
+Regex TargetRegex(TargetRegexStr);
 std::string ErrMsg;
 if (!TargetRegex.isValid(ErrMsg)) {
   IIRB.Ctx.diagnose(DiagnosticInfoInstrumentation(
@@ -190,7 +193,11 @@ bool InstrumentorImpl::shouldInstrumentTarget() {
 bool InstrumentorImpl::shouldInstrumentFunction(Function &Fn) {
   if (Fn.isDeclaration())
 return false;
-  return !Fn.getName().starts_with(IConf.getRTName()) ||
+  bool RegexMatches = true;
+  const auto FunctionRegexStr = IConf.FunctionRegex->getString();
+  if (!FunctionRegexStr.empty())
+RegexMatches = ParsedFunctionRegex.match(Fn.getName());
+  return (RegexMatches && !Fn.getName().starts_with(IConf.getRTName())) ||
  Fn.hasFnAttribute("instrument");
 }
 
@@ -274,6 +281,17 @@ bool InstrumentorImpl::instrument() {
   if (!shouldInstrumentTarget())
 return Changed;
 
+  const auto FunctionRegexStr = IConf.FunctionRegex->getString();
+  if (!FunctionRegexStr.empty()) {
+ParsedFunctionRegex = Regex(FunctionRegexStr);
+std::string ErrMsg;
+if (!ParsedFunctionRegex.isValid(ErrMsg)) {
+  IIRB.Ctx.diagnose(DiagnosticInfoInstrumentation(
+  Twine("failed to parse target regex: ") + ErrMsg, DS_Warning));
+  return false;
+}
+  }
+
   for (auto &It : IConf.IChoices[InstrumentationLocation::INSTRUCTION_PRE])
 if (It.second->Enabled)
   InstChoicesPRE[It.second->getOpcode()] = It.second;
diff --git a/llvm/test/Instrumentation/Instrumentor/default_config.json 
b/llvm/test/Instrumentation/Instrumentor/default_config.json
index 295adfe0bd96e..86b81a9568de6 100644
--- a/llvm/test/Instrumentation/Instrumentor/default_config.json
+++ b/llvm/test/Instrumentation/Instrumentor/default_config.json
@@ -7,7 +7,9 @@
 "demangle_function_names": true,
 "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",
+"target_regex.description": "Regular expression to be matched against the 
module target. Only targets that match this regex will be instrumented.",
+"function_regex": "",
+"function_regex.descriptio