[PATCH] D154019: [IR] Ignore globals with the `llvm.` prefix when calculating module hash

Paul Kirth via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 28 17:01:37 PDT 2023


paulkirth created this revision.
paulkirth added reviewers: aeubanks, nikic.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
paulkirth requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This came up in This came up in
https://reviews.llvm.org/D146776#inline-1489091 and is slightly related
to https://reviews.llvm.org/D153855. In both patches, there is the
observation that some modifications of the module should not invalidate
analysis, such as when adding a declaration or some metadata the won't
be used when compiling the current module.

This patch implements the suggestion that we should ignore globals that have
the `llvm.` prefix when calculating the module hash.

Fixes https://github.com/llvm/llvm-project/issues/63590


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154019

Files:
  llvm/lib/IR/StructuralHash.cpp
  llvm/unittests/IR/PassManagerTest.cpp


Index: llvm/unittests/IR/PassManagerTest.cpp
===================================================================
--- llvm/unittests/IR/PassManagerTest.cpp
+++ llvm/unittests/IR/PassManagerTest.cpp
@@ -10,6 +10,7 @@
 #include "llvm/Analysis/AssumptionCache.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/Constants.h"
 #include "llvm/IR/Dominators.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/LLVMContext.h"
@@ -18,6 +19,7 @@
 #include "llvm/Passes/StandardInstrumentations.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
+#include "llvm/Transforms/Utils/ModuleUtils.h"
 #include "gtest/gtest.h"
 
 using namespace llvm;
@@ -1070,5 +1072,44 @@
       "Module changed by WrongModulePass2 without invalidating analyses");
 }
 
+struct InsertMetadataPass : PassInfoMixin<InsertMetadataPass> {
+  PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) {
+    StringRef Data = "foo";
+    MemoryBufferRef Buf(Data, "ModuleData");
+    Constant *ModuleConstant = ConstantDataArray::get(
+        M.getContext(), ArrayRef(Buf.getBufferStart(), Buf.getBufferSize()));
+    GlobalVariable *GV = new GlobalVariable(
+        M, ModuleConstant->getType(), true, GlobalValue::PrivateLinkage,
+        ModuleConstant, "llvm.foo");
+  appendToCompilerUsed(M, GV);
+    return PreservedAnalyses::all();
+  }
+  static StringRef name() { return "InsertMetadataPass"; }
+};
+
+TEST_F(PassManagerTest, ModulePassNoAnalysisInvalidation) {
+  LLVMContext Context;
+  auto M = parseIR(Context, "define void @foo() {\n"
+                            "  %a = add i32 0, 0\n"
+                            "  ret void\n"
+                            "}\n");
+
+  FunctionAnalysisManager FAM;
+  ModuleAnalysisManager MAM;
+  PassInstrumentationCallbacks PIC;
+  StandardInstrumentations SI(M->getContext(), /*DebugLogging*/ false);
+  SI.registerCallbacks(PIC, &MAM);
+  MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
+  MAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
+  FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
+  FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
+
+  ModulePassManager MPM;
+  MPM.addPass(InsertMetadataPass());
+  EXPECT_TRUE(MPM.run(*M, MAM).areAllPreserved());
+
+}
+
+
 #endif
 }
Index: llvm/lib/IR/StructuralHash.cpp
===================================================================
--- llvm/lib/IR/StructuralHash.cpp
+++ llvm/lib/IR/StructuralHash.cpp
@@ -60,8 +60,7 @@
   void update(const GlobalVariable &GV) {
     // used/compiler.used don't affect analyses.
     // Same for llvm.embedded.object, which is always a metadata section.
-    if (GV.getName() == "llvm.compiler.used" || GV.getName() == "llvm.used" ||
-        GV.getName() == "llvm.embedded.object")
+    if (GV.getName().starts_with("llvm."))
       return;
     hash(23456); // Global header
     hash(GV.getValueType()->getTypeID());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154019.535566.patch
Type: text/x-patch
Size: 3020 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230629/861e6ef6/attachment.bin>


More information about the llvm-commits mailing list