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

`FunctionImporter` links to wrong function with `private` linkage

Open TimNN opened this issue 4 years ago • 5 comments

Assume two modules contain two different functions @inner with private (or internal linkage). If a function @outer that references @inner gets imported across module boundaries the wrong definition of @inner may be used in places.

I haven't managed to repro this with clang so my IR may be bad, though the fact that changing a noinline attribute changes the result makes me relatively confident that this is an actual bug.

Repro (run as llvm-lit main.ll -a to see the output):

; other.ll

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define i32 @outer(i32 %0) {
  %r = call i32 @inner(i32 %0)
  ret i32 %r
}

define private i32 @inner(i32 %0) #0 {
  %r = mul i32 %0, 2
  ret i32 %r
}

; Comment this to change the result of `main`.
attributes #0 = { noinline }
; main.ll

; RUN: opt -module-summary %p/main.ll -o %t.main.bc
; RUN: opt -module-summary %p/other.ll -o %t.other.bc
; RUN: llvm-lto -thinlto -o %t.summary %t.main.bc %t.other.bc
; The `inline` pass isn't strictly necessary but makes the problem more obvious.
; RUN: opt -passes=function-import,inline -summary-file %t.summary.thinlto.bc %t.main.bc -S

define i32 @main()  {
  %a = call i32 @outer(i32 1)
  %b = call i32 @inner(i32 1)
  ; This _should_ be `2 * 3` but is either `2 * 2` or `3 * 3` depending on the
  ; `noinline` configuration in `other.ll`.
  %c = mul i32 %a, %b
  ret i32 %c
}

define private i32 @inner(i32 %0) {
  %r = mul i32 %0, 3
  ret i32 %r
}

declare i32 @outer(i32 noundef)

@inner in other.ll multiplies by 2. @inner in main.ll multiplies by 3. The multiplication in @main should be 2 * 3, but is either 3 * 3 (if @inner in other.ll is noinline) or 2 * 2 (if @inner is not noinline).

The correct result of 2 * 3 can be achieved by commenting the noinline and adding -passes=inline when running opt on other.ll.

cc @teresajohnson

TimNN avatar Dec 05 '22 21:12 TimNN

We promote locals to global scope and rename them if we export either a reference to them or export their definition due to function importing. The issue here is likely that you haven't run any post thin link modifications on other.bc, which would be responsible for doing the renaming of that definition (which won't get imported due to the "noinline" keyword). Although I would expect a linker error if the resulting files were compiled to native code and linked, because it should have been renamed similarly when the reference was imported into main.bc via the importing of outer.

The reason it works when you remove the "noinline" keyword is that we presumably end up also importing the definition of inner from other.bc which should get renamed on import and matched correctly with the imported reference from imported outer.

teresajohnson avatar Dec 06 '22 15:12 teresajohnson

I see the renaming happening (@inner in main.ll gets renamed to @inner.llvm.0). However not only the references to @inner originally from other.ll are renamed, but also the references to @inner originally from main.ll.

There is always only one definition of @inner (renamed or not) in main.ll. Here is the IR after running function-import on main.ll:

; ModuleID = '/home/logic/Projects/llvm/build/test/Transforms/FunctionImport/Output/main.ll.tmp.main.bc'
source_filename = "/home/logic/Projects/llvm/llvm/test/Transforms/FunctionImport/main.ll"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define i32 @main() {
  %a = call i32 @outer(i32 1)
  %b = call i32 @inner.llvm.0(i32 1)
  %c = mul i32 %a, %b
  ret i32 %c
}

; If `@inner` in `other.ll` is marked `noinline`, only this definition of `@inner.llvm.0` exists.
; It was originally part of `main.ll`.
; This _may_ result in the correct output if `@outer` is not inlined and ends up being linked
; to the definition from `other.ll`.
define hidden i32 @inner.llvm.0(i32 %0) {
  %r = mul i32 %0, 3
  ret i32 %r
}

define available_externally i32 @outer(i32 %0) {
  %r = call i32 @inner.llvm.0(i32 %0)
  ret i32 %r
}

; If `@inner` in `other.ll` is _not_ marked `noinline`, only this definition of `@inner.llvm.0` exists.
; It was originally part of `other.ll`.
; This will _never_ result in the correct output, because the definition of `@inner` originally from
; `main.ll` does not exist anymore anywhere.
define available_externally hidden i32 @inner.llvm.0(i32 %0) {
  %r = mul i32 %0, 2
  ret i32 %r
}

If the inline pass is run after function-import, the result is either

; ModuleID = '/home/logic/Projects/llvm/build/test/Transforms/FunctionImport/Output/main.ll.tmp.main.bc'
source_filename = "/home/logic/Projects/llvm/llvm/test/Transforms/FunctionImport/main.ll"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define i32 @main() {
  %c = mul i32 2, 2
  ret i32 %c
}

or

; ModuleID = '/home/logic/Projects/llvm/build/test/Transforms/FunctionImport/Output/main.ll.tmp.main.bc'
source_filename = "/home/logic/Projects/llvm/llvm/test/Transforms/FunctionImport/main.ll"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define i32 @main() {
  %c = mul i32 3, 3
  ret i32 %c
}

define hidden i32 @inner.llvm.0(i32 %0) {
  %r = mul i32 %0, 3
  ret i32 %r
}

depending on whether @inner in other.ll is noinline or not. Neither is correct.

TimNN avatar Dec 06 '22 16:12 TimNN

Alright, I found the problem. I don't know if this a problem with just the function-import (which, IIUC based on a commend I found, is only used when testing via opt) or a more general problem.

But when running with opt -passes=function-import, the ModuleHash in the ModulePathStringTable is 0 for both modules. So @inner from main.ll and other.ll both get renamed to the same @inner.llvm.0. This also means that running with -use-source-filename-for-promoted-locals (which used the module name instead of the hash when renaming) fixes this.

TimNN avatar Dec 06 '22 17:12 TimNN

Just returned here to say that (had to try it myself to remember). To get the module hashes you can either add -module-hash after -module-summary, in your initial opt invocations. Or replace -module-summary with -thinlto-bc which invokes a thinlto specific bitcode writer pass and will give you the hashes. I'm not sure why we're feeling the need to rename the inner definition in main, but in any case, it relies on distinct module hashes to create the renamed suffix.

teresajohnson avatar Dec 06 '22 17:12 teresajohnson

Ah, good to know about the flags. We can close this issue then.

TimNN avatar Dec 06 '22 17:12 TimNN