| Issue |
241267
|
| Summary |
[ThinLTO] Import names an aliased variable by its aliasee; AA sees two distinct globals (miscompile at -O3)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
jeffchen006
|
`g4` is a GNU `alias` of `g4_real` in another TU. ThinLTO imports `setg` (which stores to `@g4_real` after pre-link GlobalOpt) into the module that uses `@g4`. The two names become two different globals in one module, and AA treats them as NoAlias.
**Reproducer** (`repro.c`, `repro_lib.c`)
`repro.c`:
```c
/* g is an alias of g_real (repro_lib.c). set(3) writes g through the alias, so the
program must print 3. Clang -O3 -flto=thin prints 5. */
int printf(const char *, ...);
extern int g;
void set(int a);
int main(void) {
g = 5;
set(3);
printf("%d\n", g);
return 0;
}
```
`repro_lib.c`:
```c
int g_real;
extern int g __attribute__((alias("g_real")));
void set(int a) { g = a; }
```
**Commands and output** (clang version 24.0.0git (https://github.com/llvm/llvm-project.git 8207e9143575), Target: x86_64-unknown-linux-gnu):
```console
$ clang -O0 -flto=thin -fuse-ld=lld repro.c repro_lib.c -o o0
$ ./o0
3
$ clang -O3 -flto=thin -fuse-ld=lld repro.c repro_lib.c -o o3
$ ./o3
5
```
Expected (as `-O0`): `3`. `-O3` prints `5`.
**Why the expected output is right**
Outside `min_lib.c` the object has one name `g4`; `alias` is a documented GNU/ELF extension used in the normal way; uint16 stores only, no address compare. UBSan/ASan/MSan at -O0 (ThinLTO) are clean.
**Potential Root cause**
In `min_lib.c`, pre-link GlobalOpt rewrites `setg`'s store to the aliasee `@g4_real` (correct in that module). The thin link imports `setg` and its ref `g4_real` (FunctionImport.cpp, `selectCallee` and `onImportingSummaryImpl`). The import module has `@g4 = external global i16` and `@g4_real = available_externally global i16 7` with `store ... @g4_real` in the imported body. BasicAA treats two non-alias GlobalVariables as different objects. After inlining, EarlyCSE forwards `store 5 -> @g4` past the store to `@g4_real`. Neither ModuleSummaryAnalysis nor FunctionImport checks for this.
**Possible Proposed fix**
Do not make such bodies importable (`llvm/lib/Analysis/ModuleSummaryAnalysis.cpp`, `buildModuleSummaryIndex`):
```diff
+ DenseSet<GlobalValue::GUID> AliasedVarGUIDs;
+ for (const GlobalAlias &GA : M.aliases())
+ if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GA.getAliaseeObject()))
+ AliasedVarGUIDs.insert(GVar->getGUID());
...
+ bool RefsAliasedVariable =
+ llvm::any_of(Summary->refs(), [&](const ValueInfo &VI) {
+ return AliasedVarGUIDs.count(VI.getGUID());
+ });
- if (!AllRefsCanBeExternallyReferenced) {
+ if (!AllRefsCanBeExternallyReferenced || RefsAliasedVariable) {
Summary->setNotEligibleToImport();
```
A larger fix in the importer: replace the destination's declaration of the alias with the aliasee.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs