llvm / llvm/llvm-project

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

Open
#188,549 0 comments 0 reactions 1 assignee Claimed by @mandlebug View on GitHub
llvm:optimizations
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

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`

```llvm
@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
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.