[llvm] [llvm-mca] Skip instrumentation pass when -disable-im is set (PR #211759)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 24 05:05:42 PDT 2026


https://github.com/skirtyman updated https://github.com/llvm/llvm-project/pull/211759

>From d5c187e67555783b271c339632186fa09acb8c6a Mon Sep 17 00:00:00 2001
From: Andreas Mullen <Andreas.Mullen at arm.com>
Date: Wed, 22 Jul 2026 14:26:39 +0000
Subject: [PATCH 1/2] [llvm-mca] Skip instrumentation pass when -disable-im is
 set

---
 .../tools/llvm-mca/AArch64/disable-im.test    | 12 ++++
 llvm/tools/llvm-mca/llvm-mca.cpp              | 62 ++++++++++++-------
 2 files changed, 50 insertions(+), 24 deletions(-)
 create mode 100644 llvm/test/tools/llvm-mca/AArch64/disable-im.test

diff --git a/llvm/test/tools/llvm-mca/AArch64/disable-im.test b/llvm/test/tools/llvm-mca/AArch64/disable-im.test
new file mode 100644
index 0000000000000..b2eff48dc8d78
--- /dev/null
+++ b/llvm/test/tools/llvm-mca/AArch64/disable-im.test
@@ -0,0 +1,12 @@
+# RUN: llvm-mca < %s -mtriple=aarch64 -mcpu=c1-nano -iterations=1 -debug 2>&1 | FileCheck %s --check-prefix=IMENABLED
+# RUN: llvm-mca < %s -mtriple=aarch64 -mcpu=c1-nano -iterations=1 -disable-im -debug 2>&1 | FileCheck %s --check-prefix=IMDISABLED
+
+# IMENABLED-COUNT-2: AsmMatcher: found
+# IMENABLED-NOT: AsmMatcher: found
+# IMENABLED: Iterations:{{ *}}1
+
+# IMDISABLED-COUNT-1: AsmMatcher: found
+# IMDISABLED-NOT: AsmMatcher: found
+# IMDISABLED: Iterations:{{ *}}1
+
+add x0, x0, x1
\ No newline at end of file
diff --git a/llvm/tools/llvm-mca/llvm-mca.cpp b/llvm/tools/llvm-mca/llvm-mca.cpp
index 87e2e0f091023..140c9025e040a 100644
--- a/llvm/tools/llvm-mca/llvm-mca.cpp
+++ b/llvm/tools/llvm-mca/llvm-mca.cpp
@@ -537,31 +537,45 @@ int main(int argc, char **argv) {
   }
 
   // Parse the input and create InstrumentRegion that llvm-mca
-  // can use to improve analysis.
-  MCContext ICtx(TheTriple, *MAI, *MRI, *STI, &SrcMgr);
-  std::unique_ptr<MCObjectFileInfo> IMOFI(
-      TheTarget->createMCObjectFileInfo(ICtx, /*PIC=*/false));
-  ICtx.setObjectFileInfo(IMOFI.get());
-  mca::AsmInstrumentRegionGenerator IRG(*TheTarget, SrcMgr, ICtx, *MAI, *STI,
-                                        *MCII, *IM);
-  Expected<const mca::InstrumentRegions &> InstrumentRegionsOrErr =
-      IRG.parseInstrumentRegions(std::move(IPtemp),
-                                 shouldSkip(SkipType::PARSE_FAILURE));
-  if (!InstrumentRegionsOrErr) {
-    if (auto Err = handleErrors(InstrumentRegionsOrErr.takeError(),
-                                [](const StringError &E) {
-                                  WithColor::error() << E.getMessage() << '\n';
-                                })) {
-      // Default case.
-      WithColor::error() << toString(std::move(Err)) << '\n';
+  // can use to improve analysis. Do not parse instrument regions if the
+  // -disable-im flag is set.
+  mca::InstrumentRegions EmptyInstrumentRegions(SrcMgr);
+  const mca::InstrumentRegions *InstrumentRegions = &EmptyInstrumentRegions;
+
+  std::unique_ptr<MCContext> ICtx;
+  std::unique_ptr<MCObjectFileInfo> IMOFI;
+  std::unique_ptr<mca::AsmInstrumentRegionGenerator> IRG;
+
+  if (!IM->shouldIgnoreInstruments()) {
+    ICtx = std::make_unique<MCContext>(TheTriple, *MAI, *MRI, *STI, &SrcMgr);
+    IMOFI.reset(TheTarget->createMCObjectFileInfo(*ICtx, /*PIC=*/false));
+    ICtx->setObjectFileInfo(IMOFI.get());
+    IRG = std::make_unique<mca::AsmInstrumentRegionGenerator>(
+        *TheTarget, SrcMgr, *ICtx, *MAI, *STI, *MCII, *IM);
+
+    Expected<const mca::InstrumentRegions &> InstrumentRegionsOrErr =
+        IRG->parseInstrumentRegions(std::move(IPtemp),
+                                   shouldSkip(SkipType::PARSE_FAILURE));
+
+    if (!InstrumentRegionsOrErr) {
+      if (auto Err = handleErrors(InstrumentRegionsOrErr.takeError(),
+                                  [](const StringError &E) {
+                                    WithColor::error() << E.getMessage() << '\n';
+                                  })) {
+        // Default case.
+        WithColor::error() << toString(std::move(Err)) << '\n';
+      }
+      return 1;
     }
-    return 1;
-  }
-  const mca::InstrumentRegions &InstrumentRegions = *InstrumentRegionsOrErr;
 
-  // Early exit if errors were found by the instrumentation parsing logic.
-  if (!InstrumentRegions.isValid())
-    return 1;
+    const mca::InstrumentRegions &ParsedRegions = *InstrumentRegionsOrErr;
+
+    // Early exit if errors were found by the instrumentation parsing logic.
+    if (!ParsedRegions.isValid())
+      return 1;
+
+    InstrumentRegions = &ParsedRegions;
+  }
 
   // Now initialize the output file.
   auto OF = getOutputStream();
@@ -647,7 +661,7 @@ int main(int argc, char **argv) {
     for (const MCInst &MCI : Insts) {
       SMLoc Loc = MCI.getLoc();
       const SmallVector<mca::Instrument *> Instruments =
-          InstrumentRegions.getActiveInstruments(Loc);
+          InstrumentRegions->getActiveInstruments(Loc);
 
       Expected<std::unique_ptr<mca::Instruction>> Inst =
           IB.createInstruction(MCI, Instruments);

>From 5fedd1984c9f3a33b20efd79e04c3bb3e2eed376 Mon Sep 17 00:00:00 2001
From: AJMullen <andreas.mullen at arm.com>
Date: Fri, 24 Jul 2026 13:05:30 +0100
Subject: [PATCH 2/2] Update llvm/test/tools/llvm-mca/AArch64/disable-im.test

Co-authored-by: Asher Dobrescu <dobrescuira at yahoo.com>
---
 llvm/test/tools/llvm-mca/AArch64/disable-im.test | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/test/tools/llvm-mca/AArch64/disable-im.test b/llvm/test/tools/llvm-mca/AArch64/disable-im.test
index b2eff48dc8d78..0b635eba0dcc5 100644
--- a/llvm/test/tools/llvm-mca/AArch64/disable-im.test
+++ b/llvm/test/tools/llvm-mca/AArch64/disable-im.test
@@ -1,5 +1,5 @@
-# RUN: llvm-mca < %s -mtriple=aarch64 -mcpu=c1-nano -iterations=1 -debug 2>&1 | FileCheck %s --check-prefix=IMENABLED
-# RUN: llvm-mca < %s -mtriple=aarch64 -mcpu=c1-nano -iterations=1 -disable-im -debug 2>&1 | FileCheck %s --check-prefix=IMDISABLED
+# RUN: llvm-mca < %s -mtriple=aarch64 -iterations=1 -debug 2>&1 | FileCheck %s --check-prefix=IMENABLED
+# RUN: llvm-mca < %s -mtriple=aarch64 -iterations=1 -disable-im -debug 2>&1 | FileCheck %s --check-prefix=IMDISABLED
 
 # IMENABLED-COUNT-2: AsmMatcher: found
 # IMENABLED-NOT: AsmMatcher: found



More information about the llvm-commits mailing list