Skip to content

Commit

Permalink
[IR] Ignore globals with the llvm. prefix when calculating module hash
Browse files Browse the repository at this point in the history
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 #63590

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D154019
  • Loading branch information
ilovepi committed Jul 12, 2023
1 parent 33e6048 commit e8e499f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 3 additions & 4 deletions llvm/lib/IR/StructuralHash.cpp
Expand Up @@ -59,10 +59,9 @@ class StructuralHashImpl {

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());
Expand Down
19 changes: 19 additions & 0 deletions llvm/unittests/IR/StructuralHashTest.cpp
Expand Up @@ -12,6 +12,8 @@
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"

#include <memory>

using namespace llvm;

namespace {
Expand Down Expand Up @@ -121,4 +123,21 @@ TEST(StructuralHashTest, InstructionType) {
EXPECT_EQ(StructuralHash(*M1), StructuralHash(*M2));
}

TEST(StructuralHashTest, IgnoredMetadata) {
LLVMContext Ctx;
std::unique_ptr<Module> M1 = parseIR(Ctx, "@a = global i32 1\n");
// clang-format off
std::unique_ptr<Module> M2 = parseIR(
Ctx, R"(
@a = global i32 1
@llvm.embedded.object = private constant [4 x i8] c"BC\C0\00", section ".llvm.lto", align 1, !exclude !0
@llvm.compiler.used = appending global [1 x ptr] [ptr @llvm.embedded.object], section "llvm.metadata"
!llvm.embedded.objects = !{!1}
!0 = !{}
!1 = !{ptr @llvm.embedded.object, !".llvm.lto"}
)");
EXPECT_EQ(StructuralHash(*M1), StructuralHash(*M2));
}
} // end anonymous namespace

0 comments on commit e8e499f

Please sign in to comment.