[llvm] [BOLT] Support non-null MCInst operands in annotation handling (PR #192188)

Brian Cain via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 16 06:42:06 PDT 2026


https://github.com/androm3da updated https://github.com/llvm/llvm-project/pull/192188

>From f521b9465a5f853a2b12656021dee83c7754d445 Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Tue, 7 Apr 2026 20:21:15 -0700
Subject: [PATCH] [BOLT] Support non-null MCInst operands in annotation
 handling

The annotation sentinel in BOLT is a null MCInst operand appended
after all prime operands. However, some architectures (e.g. Hexagon)
use non-null MCInst operands as legitimate prime operands for duplex
sub-instructions. The existing code treated any MCInst operand as
the annotation sentinel, causing duplex sub-instructions to be
misidentified.

In getNumPrimeOperands(), only treat a null MCInst operand as the
sentinel. In getAnnotationInstOp(), skip non-null MCInst operands
when searching for the annotation sentinel.
---
 bolt/include/bolt/Core/MCPlus.h        | 11 +++++++++--
 bolt/include/bolt/Core/MCPlusBuilder.h |  4 +---
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/bolt/include/bolt/Core/MCPlus.h b/bolt/include/bolt/Core/MCPlus.h
index ead6ba1470da6..e97c31049f48d 100644
--- a/bolt/include/bolt/Core/MCPlus.h
+++ b/bolt/include/bolt/Core/MCPlus.h
@@ -113,13 +113,20 @@ template <typename ValueType> class MCSimpleAnnotation : public MCAnnotation {
   ValueType Value;
 };
 
+/// Return true if \p Op is the annotation sentinel: a null MCInst operand
+/// that marks the start of BOLT annotations. Non-null MCInst operands
+/// (e.g. Hexagon duplex sub-instructions) are legitimate prime operands.
+inline bool isAnnotationSentinel(const MCOperand &Op) {
+  return Op.isInst() && Op.getInst() == nullptr;
+}
+
 /// Return a number of operands in \Inst excluding operands representing
 /// annotations.
 inline unsigned getNumPrimeOperands(const MCInst &Inst) {
   for (signed I = Inst.getNumOperands() - 1; I >= 0; --I) {
-    if (Inst.getOperand(I).isInst())
+    if (isAnnotationSentinel(Inst.getOperand(I)))
       return I;
-    if (!Inst.getOperand(I).isImm())
+    if (!Inst.getOperand(I).isInst() && !Inst.getOperand(I).isImm())
       return Inst.getNumOperands();
   }
   return Inst.getNumOperands();
diff --git a/bolt/include/bolt/Core/MCPlusBuilder.h b/bolt/include/bolt/Core/MCPlusBuilder.h
index 0f6076688b65d..f9acaa4ee55a3 100644
--- a/bolt/include/bolt/Core/MCPlusBuilder.h
+++ b/bolt/include/bolt/Core/MCPlusBuilder.h
@@ -138,10 +138,8 @@ class MCPlusBuilder {
 
   MCInst::iterator getAnnotationInstOp(MCInst &Inst) const {
     for (MCInst::iterator Iter = Inst.begin(); Iter != Inst.end(); ++Iter) {
-      if (Iter->isInst()) {
-        assert(Iter->getInst() == nullptr && "Empty instruction expected.");
+      if (MCPlus::isAnnotationSentinel(*Iter))
         return Iter;
-      }
     }
     return Inst.end();
   }



More information about the llvm-commits mailing list