llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

GlobalOpt deletes a GV that's kept alive only by an !implicit.ref metadata.

Open w2yehia opened this issue 8 months ago • 0 comments

The !implicit.ref metadata seeks to create a reference from global A to global B, so that a linker does not garbage collect B if its unreferenced but A is alive. If both A and B are dead then the linker is free to garbage collect any of them. I think we should teach GlobalOpt to respect the relationship create by !implicit.ref . An alternative is to require users to specify B in the llvm.used or llvm.compiler.used, but that will disallow garbage collecting B in the compiler even when A is garbage collected, which is not desirable.

> cat t.ll

@str = internal unnamed_addr constant [4 x i8] c"foo\00", align 1
;; @llvm.used = appending global [1 x ptr] [ptr @str], section "llvm.metadata"

define i32 @main() !implicit.ref !0 {
entry:
  ret i32 0
}

!0 = !{ptr @str}
> opt -passes=globalopt t.ll -o /dev/null
ref metadata must be ValueAsMetadata
ptr @main
!0 = distinct !{null}
LLVM ERROR: Broken module found, compilation aborted!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0.      Program arguments: /home/wyehia/Source/build.opt/bin/opt -passes=globalopt t.ll -o /dev/null
1.      Running pass "verify" on module "t.ll"
./t.sh: line 15: 43778520 IOT/Abort trap

w2yehia avatar Mar 25 '26 17:03 w2yehia