[llvm] [IR] Composable and Extensible Memory Cache Control Hints (PR #181612)

Fei Peng via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 23 20:20:44 PDT 2026


================
@@ -5576,6 +5577,72 @@ void Verifier::visitAllocTokenMetadata(Instruction &I, MDNode *MD) {
         "expected integer constant", MD);
 }
 
+void Verifier::visitMemCacheHintMetadata(Instruction &I, MDNode *MD) {
+  Check(I.mayReadOrWriteMemory(),
+        "!mem.cache_hint is only valid on memory operations", &I);
+
+  Check(MD->getNumOperands() % 2 == 0,
+        "!mem.cache_hint must have even number of operands "
+        "(operand_no, hint_node pairs)",
+        MD);
+
+  auto IsMemoryObjectOperand = [](const Value *V) {
+    return V->getType()->isPtrOrPtrVectorTy();
+  };
+
+  unsigned NumMemoryObjectOperands = 0;
+  if (const auto *CB = dyn_cast<CallBase>(&I))
+    NumMemoryObjectOperands = count_if(CB->args(), [&](const Use &Arg) {
+      return IsMemoryObjectOperand(Arg.get());
+    });
+  else
+    NumMemoryObjectOperands = count_if(I.operands(), [&](const Use &Op) {
+      return IsMemoryObjectOperand(Op.get());
+    });
+
+  SmallVector<unsigned, 4> SeenOperandNos;
+
+  // Top-level metadata alternates: i32 operand_no, MDNode hint_node.
+  for (unsigned i = 0; i + 1 < MD->getNumOperands(); i += 2) {
+    auto *OpNoCI = mdconst::dyn_extract<ConstantInt>(MD->getOperand(i));
+    Check(OpNoCI,
+          "!mem.cache_hint operand_no must be an integer constant in pair", MD);
+
+    Check(OpNoCI->getValue().isNonNegative(),
+          "!mem.cache_hint operand_no must be non-negative", MD);
+
+    uint64_t OperandNo = OpNoCI->getZExtValue();
+    Check(OperandNo < NumMemoryObjectOperands,
+          "!mem.cache_hint operand_no must refer to a valid memory object "
+          "operand",
+          &I);
+
+    Check(!is_contained(SeenOperandNos, OperandNo),
+          "!mem.cache_hint contains duplicate operand_no", MD);
+    SeenOperandNos.push_back(OperandNo);
+
+    const auto *Node = dyn_cast<MDNode>(MD->getOperand(i + 1));
+    Check(Node, "!mem.cache_hint hint node must be a metadata node", MD);
+
+    Check(Node->getNumOperands() % 2 == 0,
+          "!mem.cache_hint hint node must have even number of operands "
+          "(key-value pairs)",
+          Node);
+
+    SmallVector<StringRef, 8> SeenKeys;
----------------
fiigii wrote:

Fixed.

https://github.com/llvm/llvm-project/pull/181612


More information about the llvm-commits mailing list