Possible Undefined Behavior when `FunctionImporter` imports from different modules.
Originally from https://github.com/rust-lang/rust/issues/104377. I have observed that, when Rust performs its "Thin LTO" pass, FunctionImporter sometimes imports incompatible definitions/declarations. I don't believe this issue is Rust-specific.
~~Given the number of moving parts involved, I don't currently have a simple repro, but~~ consider the following case:
edit: now with a repro in the LLVM tests: https://github.com/llvm/llvm-project/compare/main...TimNN:llvm-project:def-deps-test (or https://github.com/TimNN/llvm-project/commit/a6a07261984cee019db23d95866f4de2e23d40da for the specific commit)
Assume we start with some module X (in pseudo-LL):
define void @outer(ptr noundef %0) {
call void @inner(ptr noundef %0)
}
define void @inner(ptr noundef %0) { ... }
Dead argument elimination comes along and does:
define void @outer(ptr noundef %0) {
call void @inner(ptr poison) ; `noundef` is gone.
}
define void @inner(ptr %0) { ... } ; `noundef` is gone.
So far so good. But now Thin LTO comes along and runs on a module Y that doesn't contain or reference @outer at all. Maybe it already contains a
declare void @inner(ptr noundef %0)
or maybe that gets (transitively) imported from some module Z. Now suppose that @outer also gets (transitively) imported. Suddenly we have in Y:
define available_externally void @outer(ptr noundef %0) {
call void @inner(ptr poison) ; Passing `poison` to `noundef` parameter!
}
declare void @inner(ptr noundef %0)
I'm not really familiar with how things are supposed to work, but I'm currently thinking that when importing a define x from a module A into a module B, then all globals referenced by x that are defined in A must not reference any declarations about them already present in B.
Background: https://reviews.llvm.org/D133036 (not yet submitted) eliminates function calls where undef (or poison) is passed to a parameter when that parameter is marked noundef. In the example above, it would eliminate the call to @inner. I've observed this causing mis-compiles in the Rust compiler.
@teresajohnson
I've created a repro in the LLVM tests: https://github.com/llvm/llvm-project/compare/main...TimNN:llvm-project:def-deps-test (or https://github.com/TimNN/llvm-project/commit/a6a07261984cee019db23d95866f4de2e23d40da for the specific commit).
I proposed a potential fix in https://reviews.llvm.org/D139209.
Sorry I didn't respond on this earlier. I think the general idea behind the fix seems reasonable, but I need to think through it some more and have a couple questions/suggestions. Will respond on the patch.
Teresa
On Fri, Dec 2, 2022 at 10:10 AM Tim Neumann @.***> wrote:
I proposed a potential fix in https://reviews.llvm.org/D139209.
— Reply to this email directly, view it on GitHub https://github.com/llvm/llvm-project/issues/58976#issuecomment-1335631410, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE37ZQ6SMKDCVRJCYOK4NF3WLI3SNANCNFSM6AAAAAAR7SJ534 . You are receiving this because you were mentioned.Message ID: @.***>
-- Teresa Johnson | Software Engineer | @.*** |
Patch was reverted in 86ced82099813fa000e4681882115636240d76eb. Reopening.
There are variants of this that don't involve LLVM passes which infer extra attributes or remove dead arguments -- is that the same issue or should it be considered a different issue?
Like, imagine:
// a.ll
define i1 @foo_a() {
bb:
%ptr = alloca ...
%a = call ptr @f(ptr %ptr)
ret i1 0
}
declare ptr nonnull @f(ptr nonnull)
and
// b.ll
define i1 @foo_b() {
bb:
%a = call ptr @f(ptr null)
%b = icmp ne ptr %a, null
ret i1 %b
}
declare ptr @f(ptr)
Both of these can be completely fine in isolation, e.g. if f is a function that just exactly returns its arguments. All calls in a.ll honor the nonnull attributes so everything is good. But if during LTO, LLVM merges this into a single file, and that single file uses declare ptr nonnull @f(ptr nonnull), then we have a problem because now foo_b can get miscompiled!
One can imagine even more extreme versions of this where the two functions differ not only in their attributes, but in the return type or the number of arguments.
@dtcxzyw reminded me that we can use freeze ptr poison here.