[llvm] [NVPTX] Composable and Extensible Memory Cache Control Hints (PR #175901)

Fei Peng via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 15 10:20:36 PST 2026


================
@@ -377,4 +381,187 @@ bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM) {
          !isKernelFunction(*F);
 }
 
+namespace NVPTX {
+
+// Helper to parse L1 eviction policy from metadata string
+static std::optional<L1Eviction> parseL1Eviction(StringRef Str) {
+  return StringSwitch<std::optional<L1Eviction>>(Str)
+      .Case("normal", L1Eviction::Normal)
+      .Case("unchanged", L1Eviction::Unchanged)
+      .Case("first", L1Eviction::First)
+      .Case("last", L1Eviction::Last)
+      .Case("no_allocate", L1Eviction::NoAllocate)
+      .Default(std::nullopt);
+}
+
+// Helper to parse L2 eviction policy from metadata string
+static std::optional<L2Eviction> parseL2Eviction(StringRef Str) {
+  return StringSwitch<std::optional<L2Eviction>>(Str)
+      .Case("normal", L2Eviction::Normal)
+      .Case("first", L2Eviction::First)
+      .Case("last", L2Eviction::Last)
+      .Default(std::nullopt);
+}
+
+// Helper to parse L2 prefetch size from metadata string
+static std::optional<L2Prefetch> parseL2Prefetch(StringRef Str) {
+  return StringSwitch<std::optional<L2Prefetch>>(Str)
+      .Case("64B", L2Prefetch::Bytes64)
+      .Case("128B", L2Prefetch::Bytes128)
+      .Case("256B", L2Prefetch::Bytes256)
+      .Default(std::nullopt);
+}
+
+// Helper to find the metadata node matching a specific operand number.
+// The metadata structure is:
+// !mem.cache_hint = !{!node1, !node2, ...}
+// Each node contains key-value pairs (operand_no can be anywhere):
+// !node = !{!"operand_no", i32 N, !"nvvm.key1", value1, ...}
+// Returns the matching MDNode or nullptr if not found.
+static const MDNode *findCacheHintNode(const MDNode *MD, unsigned OperandNo) {
+  if (!MD)
+    return nullptr;
+
+  for (unsigned i = 0, e = MD->getNumOperands(); i < e; ++i) {
+    const MDNode *Node = dyn_cast<MDNode>(MD->getOperand(i));
+    if (!Node || Node->getNumOperands() < 2) {
+      LLVM_DEBUG(if (Node) dbgs()
+                 << "NVPTX: Skipping malformed cache hint node with "
+                 << Node->getNumOperands() << " operands\n");
+      continue;
+    }
+
+    // Search for operand_no in the node (can be at any position)
+    std::optional<unsigned> NodeOperandNo;
+    for (unsigned j = 0; j + 1 < Node->getNumOperands(); j += 2) {
+      const MDString *Key = dyn_cast<MDString>(Node->getOperand(j));
+      if (Key && Key->getString() == "operand_no") {
+        if (auto *OpNoMD =
+                dyn_cast<ConstantAsMetadata>(Node->getOperand(j + 1))) {
+          if (auto *OpNoCI = dyn_cast<ConstantInt>(OpNoMD->getValue()))
+            NodeOperandNo = OpNoCI->getZExtValue();
+          else
+            LLVM_DEBUG(dbgs()
+                       << "NVPTX: operand_no value is not ConstantInt\n");
----------------
fiigii wrote:

Since dropping the metadata does not impact correctness, I prefer to ignore incorrect metadata payloads silently.

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


More information about the llvm-commits mailing list