[Mlir-commits] [mlir] [mlir][llvm] Preserve function entry count metadata (PR #204707)

Tobias Gysi llvmlistbot at llvm.org
Sun Jun 21 11:35:09 PDT 2026


================
@@ -112,27 +125,64 @@ static LogicalResult setProfilingAttr(OpBuilder &builder, llvm::MDNode *node,
   auto *name = dyn_cast<llvm::MDString>(node->getOperand(0));
   if (!name)
     return failure();
+  StringRef profName = name->getString();
 
   // Handle function entry count metadata.
-  if (name->getString() == llvm::MDProfLabels::FunctionEntryCount) {
+  if (profName == llvm::MDProfLabels::FunctionEntryCount ||
+      profName == llvm::MDProfLabels::SyntheticFunctionEntryCount) {
+    if (node->getNumOperands() < 2)
+      return failure();
+
+    bool isSynthetic =
+        profName == llvm::MDProfLabels::SyntheticFunctionEntryCount;
 
-    // TODO support function entry count metadata with GUID fields.
-    if (node->getNumOperands() != 2)
+    // LLVM's semantic import-GUID API only reads trailing GUID operands from
+    // "function_entry_count" metadata. Do not model trailing operands on
+    // "synthetic_function_entry_count" as import GUIDs in MLIR.
+    if (isSynthetic && node->getNumOperands() > 2)
       return failure();
 
-    llvm::ConstantInt *entryCount =
-        llvm::mdconst::dyn_extract<llvm::ConstantInt>(node->getOperand(1));
-    if (!entryCount)
+    std::optional<uint64_t> entryCountValue =
+        getUInt64Metadata(node->getOperand(1));
+    if (!entryCountValue)
       return failure();
+
+    SmallVector<uint64_t> importGUIDValues;
+    importGUIDValues.reserve(node->getNumOperands() - 2);
+    for (unsigned idx = 2, e = node->getNumOperands(); idx < e; ++idx) {
+      std::optional<uint64_t> guidValue =
+          getUInt64Metadata(node->getOperand(idx));
+      if (!guidValue)
+        return failure();
+      importGUIDValues.push_back(*guidValue);
+    }
+
+    // Import GUIDs are semantically a set in LLVM. Canonicalize them as
+    // unsigned sorted-unique values before storing the bit patterns in MLIR.
+    llvm::sort(importGUIDValues);
+    importGUIDValues.erase(
+        std::unique(importGUIDValues.begin(), importGUIDValues.end()),
+        importGUIDValues.end());
----------------
gysit wrote:

Is the sorting needed? I would assume LLVM guarantees that the imported IR contains unique IDs and the export will compute a set anyways?

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


More information about the Mlir-commits mailing list