[llvm] [PGO] Instrument modules with at least a single function definition (PR #93421)

Pavel Samolysov via llvm-commits llvm-commits at lists.llvm.org
Tue May 28 19:14:10 PDT 2024


================
@@ -405,9 +405,14 @@ static GlobalVariable *createIRLevelProfileFlagVar(Module &M, bool IsCS) {
     ProfileVersion |= VARIANT_MASK_BYTE_COVERAGE;
   if (PGOTemporalInstrumentation)
     ProfileVersion |= VARIANT_MASK_TEMPORAL_PROF;
-  auto IRLevelVersionVariable = new GlobalVariable(
+  assert(!M.global_empty() &&
+         "If a module was instrumented, there must be defined global variables "
+         "at least for the counters.");
+  auto *InsertionPoint = &*M.global_begin();
+  auto *IRLevelVersionVariable = new GlobalVariable(
       M, IntTy64, true, GlobalValue::WeakAnyLinkage,
-      Constant::getIntegerValue(IntTy64, APInt(64, ProfileVersion)), VarName);
+      Constant::getIntegerValue(IntTy64, APInt(64, ProfileVersion)), VarName,
+      InsertionPoint);
----------------
samolisov wrote:

I see the COMDATs are generated in an LLVM IR module at the beginning while globals are generated one after another. Because now the `__llvm_profile_raw_version` variable is generated **after** the counters, it is decoupled from the corresponding `comdat` directive by default (so, without the insertion point):

```llvm
$__llvm_profile_raw_version = comdat any
@__profn_test_br_1 = private constant [9 x i8] c"test_br_1"
@__llvm_profile_raw_version = hidden constant i64 {00000}, comdat
```

what looks ugly and may make the debugging process less smooth. This is why I added the explicit insertion point.

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


More information about the llvm-commits mailing list