[PATCH] D88433: [IRMover] Fix up "CG Profile" MDNode after RAUW

Yuanfang Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 28 10:35:26 PDT 2020


ychen created this revision.
ychen added reviewers: tejohnson, pcc, mehdi_amini.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
ychen requested review of this revision.

`Verifier::visitModuleFlagCGProfileEntry` sometimes would fail due to
"expected a Function or null". It turns out during RAUW in IRMover,

  `Function` ValueAsMetadata in "CG Profile" could become bitcast. This
  patch add a fix up function for this after the MapValue loop in
  IRLinker::run.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D88433

Files:
  llvm/lib/Linker/IRMover.cpp
  llvm/test/Transforms/FunctionImport/Inputs/cg_profile.ll
  llvm/test/Transforms/FunctionImport/cg_profile.ll


Index: llvm/test/Transforms/FunctionImport/cg_profile.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/FunctionImport/cg_profile.ll
@@ -0,0 +1,25 @@
+; Check that RAUW performed by IRMover during function importing does not
+; generate bitcast in "CG Profile" related metadat nodes.
+; RUN: opt -cg-profile -module-summary %s -o %t.bc
+; RUN: opt -module-summary %p/Inputs/cg_profile.ll -o %t2.bc
+; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
+; RUN: opt -function-import -print-imports -summary-file %t3.thinlto.bc %t.bc -o %t.o
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; %class.A is defined differently in %p/Inputs/cg_profile.ll. This is to trigger
+; bitcast.
+%class.A = type { i8 }
+
+define void @foo() !prof !2 {
+  call void @bar(%class.A* null)
+  ret void
+}
+
+declare void @bar(%class.A*)
+
+!llvm.module.flags = !{!1}
+
+!1 = !{i32 1, !"EnableSplitLTOUnit", i32 0}
+!2 = !{!"function_entry_count", i64 2753}
Index: llvm/test/Transforms/FunctionImport/Inputs/cg_profile.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/FunctionImport/Inputs/cg_profile.ll
@@ -0,0 +1,12 @@
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+%class.A = type { [16 x i8] }
+
+define void @bar(%class.A*) {
+  ret void
+}
+
+!llvm.module.flags = !{!1}
+
+!1 = !{i32 1, !"EnableSplitLTOUnit", i32 0}
Index: llvm/lib/Linker/IRMover.cpp
===================================================================
--- llvm/lib/Linker/IRMover.cpp
+++ llvm/lib/Linker/IRMover.cpp
@@ -1405,6 +1405,25 @@
   return InlineAsm;
 }
 
+static void fixupCGProfileUser(Module &DstM) {
+  MDNode *CFGProfile = cast_or_null<MDNode>(DstM.getModuleFlag("CG Profile"));
+  if (!CFGProfile)
+    return;
+
+  auto StripCast = [](MDNode *E, unsigned Idx) {
+    const MDOperand &MDO = E->getOperand(Idx);
+    auto V = cast<ValueAsMetadata>(MDO);
+    if (Value *StrippedV = V->getValue()->stripPointerCasts())
+      E->replaceOperandWith(Idx, ValueAsMetadata::get(StrippedV));
+  };
+
+  for (const auto &Edge : CFGProfile->operands()) {
+    MDNode *E = cast<MDNode>(Edge);
+    StripCast(E, 0);
+    StripCast(E, 1);
+  }
+}
+
 Error IRLinker::run() {
   // Ensure metadata materialized before value mapping.
   if (SrcM->getMaterializer())
@@ -1471,6 +1490,8 @@
     flushRAUWWorklist();
   }
 
+  fixupCGProfileUser(DstM);
+
   // Note that we are done linking global value bodies. This prevents
   // metadata linking from creating new references.
   DoneLinkingBodies = true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D88433.294753.patch
Type: text/x-patch
Size: 2761 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200928/e364bc1e/attachment.bin>


More information about the llvm-commits mailing list