[llvm] [StructuralHash] Global Variable (PR #118412)

Kyungwoo Lee via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 2 22:39:01 PST 2024


================
@@ -65,19 +65,44 @@ class StructuralHashImpl {
     }
   }
 
-  stable_hash hashAPInt(const APInt &I) {
+  static stable_hash hashAPInt(const APInt &I) {
     SmallVector<stable_hash> Hashes;
     Hashes.emplace_back(I.getBitWidth());
     auto RawVals = ArrayRef<uint64_t>(I.getRawData(), I.getNumWords());
     Hashes.append(RawVals.begin(), RawVals.end());
     return stable_hash_combine(Hashes);
   }
 
-  stable_hash hashAPFloat(const APFloat &F) {
+  static stable_hash hashAPFloat(const APFloat &F) {
     return hashAPInt(F.bitcastToAPInt());
   }
 
-  stable_hash hashGlobalValue(const GlobalValue *GV) {
+  static stable_hash hashGlobalVariable(const GlobalVariable &GVar) {
+    if (!GVar.hasInitializer())
+      return hashGlobalValue(&GVar);
+
+    // Hash the contents of a string.
+    if (GVar.getName().starts_with(".str"))
+      return hashConstant(GVar.getInitializer());
+
+    // Hash structural contents of Objective-C metadata in specific sections.
+    // This can be extended to other metadata if needed.
+    static constexpr const char *SectionNames[] = {
+        "__cfstring",      "__cstring",      "__objc_classrefs",
+        "__objc_methname", "__objc_selrefs",
+    };
+    if (GVar.hasSection()) {
+      StringRef SectionName = GVar.getSection();
+      for (const char *Name : SectionNames) {
+        if (SectionName.contains(Name))
----------------
kyulee-com wrote:

Unfortunately, we can't as the section name can be in the middle. You see an example, `cgdata-merge-gvar-objc.ll`, like `section "__DATA,__objc_selrefs,literal_pointers,no_dead_strip"`.

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


More information about the llvm-commits mailing list