nested opaque types confuse the linker
| Bugzilla Link | 38408 |
| Version | trunk |
| OS | All |
| CC | @ekatz |
Extended Description
Given the four input IR files below, the llvm-link should generate a single defintion of %struct.outer which contains a
pointer to a version of %struct.middle that in turn contains "%struct.inner = type { i32, i32, %struct.foo* }.
Instead, the linker mistakenly maps %struct.middle to %struct.middle.1 and produces a second version of %struct.outer
which must be bitcast back to the correct definition in the calls to use_outer() in the body of f1() and f3().
-=-=-=-=-=-=-=- File1.ll -=-=-=-=-=-=-=- %struct.outer = type { i32, i32, %struct.middle* } %struct.middle = type opaque
declare void @use_outer(%struct.outer* %o)
define void @f1(%struct.outer* %o) { call void @use_outer(%struct.outer* %o) ret void }
-=-=-=-=-=-=-=- File2.ll -=-=-=-=-=-=-=- %struct.middle.1 = type { %struct.inner.1 } %struct.inner.1 = type { i32, i32, %struct.bar } %struct.bar = type { i32, i32 }
define void @use_middle(%struct.middle.1* %m) { %pbar = getelementptr %struct.middle.1, %struct.middle.1* %m, i64 0, i32 0, i32 2 call void @use_bar(%struct.bar* %pbar) ret void }
define void @use_bar(%struct.bar* %b) { ret void }
-=-=-=-=-=-=-=- File3.ll -=-=-=-=-=-=-=- %struct.outer = type { i32, i32, %struct.middle* } %struct.middle = type { %struct.inner } %struct.inner = type { i32, i32, %struct.foo* } %struct.foo = type { i16, i16, i32 }
declare void @use_outer(%struct.outer* %o) { ret void }
-=-=-=-=-=-=-=--=-=-=-=-=-=-=--=-=-=-=-=- llvm-link -S File1.ll File2.ll File3.ll -=-=-=-=-=-=-=--=-=-=-=-=-=-=--=-=-=-=-=- %struct.outer = type { i32, i32, %struct.middle* } %struct.middle = type { %struct.inner.1 } %struct.inner.1 = type { i32, i32, %struct.bar } %struct.bar = type { i32, i32 } %struct.outer.0 = type { i32, i32, %struct.middle.1* } %struct.middle.1 = type { %struct.inner } %struct.inner = type { i32, i32, %struct.foo* } %struct.foo = type { i16, i16, i32 }
define void @f1(%struct.outer* %o) { call void bitcast (void (%struct.outer.0*)* @use_outer to void (%struct.outer*))(%struct.outer %o) ret void }
define void @use_middle(%struct.middle* %m) { %pbar = getelementptr %struct.middle, %struct.middle* %m, i64 0, i32 0, i32 2 call void @use_bar(%struct.bar* %pbar) ret void }
define void @use_bar(%struct.bar* %b) { ret void }
define void @use_outer(%struct.outer.0* %o) { ret void }
This also applies to LTO, of course.
That doesn't help the case that led me to report this. I came across this behavior investigating problems with linking modules that were produced by clang. That is, the .# suffix doesn't necessarily mean what IRMover thinks it means. In particular, multiple instantiations of a template will result in the same name with a .# suffix. For example: https://godbolt.org/z/u6Y2nG
In practice this probably isn't a major problem because you can only get into the confused state if a module only had a pointer to the incorrectly linked type and the linker fixes everything up with bitcasts. Unless the bitcasts block some optimization the problem is purely academic. On the other hand, I originally found the problem because a bitcast was blocking an optimization I was working on.
This is due to the use of the reserved ".#" suffix in File2.ll. This suffix is used by the IRMover (function getTypeNamePrefix()) to determine similar StructTypes. Replacing the suffix with ".x" for example, produces correct result.