[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
Tue Jul 11 10:45:18 PDT 2023


paulkirth updated this revision to Diff 539189.
paulkirth added a comment.

Rebase. I will update the test code after D154308 <https://reviews.llvm.org/D154308> lands.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154019/new/

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
@@ -59,10 +59,9 @@
 
   void update(const GlobalVariable &GV) {
     // Declarations and used/compiler.used don't affect analyses.
-    // Same for llvm.embedded.object, which is always a metadata section.
-    if (GV.isDeclaration() ||
-        GV.getName() == "llvm.compiler.used" || GV.getName() == "llvm.used" ||
-        GV.getName() == "llvm.embedded.object")
+    // Since there are several `llvm.*` metadata, like `llvm.embedded.object`,
+    // we ignore anything with the `.llvm` prefix
+    if (GV.isDeclaration() || GV.getName().starts_with("llvm."))
       return;
     hash(23456); // Global header
     hash(GV.getValueType()->getTypeID());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154019.539189.patch
Type: text/x-patch
Size: 3224 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230711/79547b68/attachment.bin>


More information about the llvm-commits mailing list