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

Tobias Gysi llvmlistbot at llvm.org
Tue Jun 23 06:04:37 PDT 2026


================
@@ -112,27 +121,48 @@ 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) {
-
-    // TODO support function entry count metadata with GUID fields.
-    if (node->getNumOperands() != 2)
+  if (profName == llvm::MDProfLabels::FunctionEntryCount ||
+      profName == llvm::MDProfLabels::SyntheticFunctionEntryCount) {
+    if (node->getNumOperands() < 2)
       return failure();
 
-    llvm::ConstantInt *entryCount =
-        llvm::mdconst::dyn_extract<llvm::ConstantInt>(node->getOperand(1));
-    if (!entryCount)
+    bool isSynthetic =
+        profName == llvm::MDProfLabels::SyntheticFunctionEntryCount;
+    llvm::Function::ProfileCountType profileCountType =
+        isSynthetic ? llvm::Function::PCT_Synthetic : llvm::Function::PCT_Real;
+
+    std::optional<uint64_t> entryCountValue =
+        getUInt64Metadata(node->getOperand(1));
+    if (!entryCountValue)
       return failure();
+    if (profileCountType == llvm::Function::PCT_Real &&
+        *entryCountValue == uint64_t(-1))
+      return success();
----------------
gysit wrote:

I now realize that this breaks the roundtrip when going LLVM IR -> LLVM dialect -> LLVM IR. Let's maybe circle back and just keep the -1 value as execution count. Sorry for the confusion that is my bad.

What we could consider is making the entryCount field of the struct optional and only set it if the value is not -1. The export would then convert the value back to -1. This would be a bit a nicer modeling. However, just importing -1 is fine as well.

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


More information about the Mlir-commits mailing list