From 5fc69f2fef31b772d77f7c1e999096575c904e40 Mon Sep 17 00:00:00 2001 From: eleviant Date: Mon, 5 Oct 2026 19:22:43 +0200 Subject: [PATCH] [MemDep] Stop traversal if invariant.group load operand is a constant (#223456) When traversing a load with attached invariant.group metadata, MemDep analysis can start looking outside the function that the GVN pass is being run on. This happens if the load operand is a constant expression referencing a global object (e.g. a vtable pointer). The DominatorTree then crashes with an assertion when analyzing two instructions from different functions. This issue causes a crash in clang when the `-fstrict-vtable-pointers` option is used, see https://godbolt.org/z/sbKMa5K1d (cherry picked from commit 11f533111575e9ccefea35a7090592375e24543b) [Backport to julia-release/20.x: the cherry-pick applied with no conflicts.] Co-Authored-By: Claude --- .../lib/Analysis/MemoryDependenceAnalysis.cpp | 4 +- llvm/test/Transforms/GVN/strict-vtable.ll | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 llvm/test/Transforms/GVN/strict-vtable.ll diff --git a/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp b/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp index dec5ee5361ca33..faf1f6789c4772 100644 --- a/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp +++ b/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp @@ -284,11 +284,11 @@ MemoryDependenceResults::getInvariantGroupPointerDependency(LoadInst *LI, // cast graph down only. Value *LoadOperand = LI->getPointerOperand()->stripPointerCasts(); - // It's is not safe to walk the use list of global value, because function + // It's is not safe to walk the use list of constant, because function // passes aren't allowed to look outside their functions. // FIXME: this could be fixed by filtering instructions from outside // of current function. - if (isa(LoadOperand)) + if (isa(LoadOperand)) return MemDepResult::getUnknown(); Instruction *ClosestDependency = nullptr; diff --git a/llvm/test/Transforms/GVN/strict-vtable.ll b/llvm/test/Transforms/GVN/strict-vtable.ll new file mode 100644 index 00000000000000..dde1fba97f7eb7 --- /dev/null +++ b/llvm/test/Transforms/GVN/strict-vtable.ll @@ -0,0 +1,37 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6 +; RUN: opt -passes=gvn -S %s -o - | FileCheck %s + +@p = global ptr null, align 8 + +@_ZZ1gvE1a = internal global { ptr, ptr } zeroinitializer + +define void @_Z1hv() { +; CHECK-LABEL: define void @_Z1hv() { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8 +; CHECK-NEXT: [[VTABLE:%.*]] = load ptr, ptr getelementptr inbounds nuw (i8, ptr @_ZZ1gvE1a, i64 8), align 8, !invariant.group [[META0:![0-9]+]] +; CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr [[VTABLE]], align 8 +; CHECK-NEXT: tail call void [[TMP1]](ptr getelementptr inbounds nuw (i8, ptr @_ZZ1gvE1a, i64 8), ptr [[TMP0]]) +; CHECK-NEXT: ret void +; +entry: + %0 = load ptr, ptr @p, align 8 + %vtable = load ptr, ptr getelementptr inbounds nuw (i8, ptr @_ZZ1gvE1a, i64 8), align 8, !invariant.group !0 + %1 = load ptr, ptr %vtable, align 8 + tail call void %1(ptr getelementptr inbounds nuw (i8, ptr @_ZZ1gvE1a, i64 8), ptr %0) + ret void +} + +define dso_local ptr @_Z1gv() { +; CHECK-LABEL: define dso_local ptr @_Z1gv() { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: ret ptr getelementptr inbounds nuw (i8, ptr @_ZZ1gvE1a, i64 8) +; +entry: + ret ptr getelementptr inbounds nuw (i8, ptr @_ZZ1gvE1a, i64 8) +} + +!0 = !{} +;. +; CHECK: [[META0]] = !{} +;.