Skip to content

[LTO] Fix used before intialised warning #143705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025

Conversation

DavidSpickett
Copy link
Collaborator

For whatever reason I can't reproduce this locally but I can on Compiler Explorer (https://godbolt.org/z/nfv4b83q6) and on our flang gcc bot (https://lab.llvm.org/buildbot/#/builders/130/builds/13683/steps/5/logs/stdio).

In file included from ../llvm-project/llvm/include/llvm/LTO/LTO.h:33,
from ../llvm-project/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:29:
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h: In constructor ‘llvm::FunctionImporter::ImportListsTy::ImportListsTy()’: ../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h:275:33: warning: member ‘llvm::FunctionImporter::ImportListsTy::ImportIDs’ is used uninitialized [-Wuninitialized]
275 | ImportListsTy() : EmptyList(ImportIDs) {}
| ^~~~~~~~~
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h: In constructor ‘llvm::FunctionImporter::ImportListsTy::ImportListsTy(size_t)’:
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h:276:44: warning: member ‘llvm::FunctionImporter::ImportListsTy::ImportIDs’ is used uninitialized [-Wuninitialized]
276 | ImportListsTy(size_t Size) : EmptyList(ImportIDs), ListsImpl(Size) {}
| ^~~~~~~~~

ImportIDs was being used during construction of EmptyList, before ImportIDs itself had been constructed.

For whatever reason I can't reproduce this locally but I can
on Compiler Explorer (https://godbolt.org/z/nfv4b83q6) and
on our flang gcc bot (https://lab.llvm.org/buildbot/#/builders/130/builds/13683/steps/5/logs/stdio).

In file included from ../llvm-project/llvm/include/llvm/LTO/LTO.h:33,
                 from ../llvm-project/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:29:
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h: In constructor ‘llvm::FunctionImporter::ImportListsTy::ImportListsTy()’:
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h:275:33: warning: member ‘llvm::FunctionImporter::ImportListsTy::ImportIDs’ is used uninitialized [-Wuninitialized]
  275 |     ImportListsTy() : EmptyList(ImportIDs) {}
      |                                 ^~~~~~~~~
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h: In constructor ‘llvm::FunctionImporter::ImportListsTy::ImportListsTy(size_t)’:
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h:276:44: warning: member ‘llvm::FunctionImporter::ImportListsTy::ImportIDs’ is used uninitialized [-Wuninitialized]
  276 |     ImportListsTy(size_t Size) : EmptyList(ImportIDs), ListsImpl(Size) {}
      |                                            ^~~~~~~~~

ImportIDs was being used during construction of EmptyList,
before ImportIDs itself had been constructed.
@llvmbot
Copy link
Member

llvmbot commented Jun 11, 2025

@llvm/pr-subscribers-llvm-transforms

Author: David Spickett (DavidSpickett)

Changes

For whatever reason I can't reproduce this locally but I can on Compiler Explorer (https://godbolt.org/z/nfv4b83q6) and on our flang gcc bot (https://lab.llvm.org/buildbot/#/builders/130/builds/13683/steps/5/logs/stdio).

In file included from ../llvm-project/llvm/include/llvm/LTO/LTO.h:33,
from ../llvm-project/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:29:
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h: In constructor ‘llvm::FunctionImporter::ImportListsTy::ImportListsTy()’: ../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h:275:33: warning: member ‘llvm::FunctionImporter::ImportListsTy::ImportIDs’ is used uninitialized [-Wuninitialized]
275 | ImportListsTy() : EmptyList(ImportIDs) {}
| ^~~~~~~~~
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h: In constructor ‘llvm::FunctionImporter::ImportListsTy::ImportListsTy(size_t)’:
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h:276:44: warning: member ‘llvm::FunctionImporter::ImportListsTy::ImportIDs’ is used uninitialized [-Wuninitialized]
276 | ImportListsTy(size_t Size) : EmptyList(ImportIDs), ListsImpl(Size) {}
| ^~~~~~~~~

ImportIDs was being used during construction of EmptyList, before ImportIDs itself had been constructed.


Full diff: https://github.com/llvm/llvm-project/pull/143705.diff

1 Files Affected:

  • (modified) llvm/include/llvm/Transforms/IPO/FunctionImport.h (+4-3)
diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h
index 65228bb65ba8b..e6ae9ee831d50 100644
--- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h
+++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h
@@ -272,8 +272,9 @@ class FunctionImporter {
   // A map from destination modules to lists of imports.
   class ImportListsTy {
   public:
-    ImportListsTy() : EmptyList(ImportIDs) {}
-    ImportListsTy(size_t Size) : EmptyList(ImportIDs), ListsImpl(Size) {}
+    ImportListsTy() : ImportIDs(), EmptyList(ImportIDs) {}
+    ImportListsTy(size_t Size)
+        : ImportIDs(), EmptyList(ImportIDs), ListsImpl(Size) {}
 
     ImportMapTy &operator[](StringRef DestMod) {
       return ListsImpl.try_emplace(DestMod, ImportIDs).first->second;
@@ -293,9 +294,9 @@ class FunctionImporter {
     const_iterator end() const { return ListsImpl.end(); }
 
   private:
+    ImportIDTable ImportIDs;
     ImportMapTy EmptyList;
     DenseMap<StringRef, ImportMapTy> ListsImpl;
-    ImportIDTable ImportIDs;
   };
 
   /// The set contains an entry for every global value that the module exports.

Copy link
Member

@mstorsjo mstorsjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Copy link
Contributor

@kazutakahirata kazutakahirata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@DavidSpickett DavidSpickett merged commit bc9f4ed into llvm:main Jun 11, 2025
7 of 9 checks passed
@DavidSpickett DavidSpickett deleted the llvm-funcimp branch June 11, 2025 15:44
@mstorsjo
Copy link
Member

FWIW, building LLVM with GCC (with modern versions) products loads of warnings. It has never been entirely free of warnings, but it used to be half a dozen or so. I haven’t tried to get rid of the ones that have popped up lately, so there’s quite many these days. If you have time, there may be many more that might be similarly low hanging fruit to silence.

@DavidSpickett
Copy link
Collaborator Author

Most recent I have to hand is gcc 13, and I built just llvm here:

$ cat /tmp/warnings | sort | uniq -c | sort -r
     99 /home/david.spickett/llvm-project/llvm/lib/Target/AMDGPU/SIInstrInfo.h:1214: warning: possibly dangling reference to a temporary [-Wdangling-reference]
     63 /home/david.spickett/llvm-project/llvm/lib/Target/PowerPC/Disassembler/PPCDisassembler.cpp:88:44: warning: array subscript ‘const llvm::MCPhysReg [32] {aka const short unsigned int [32]}[0]’ is partly outside array bounds of ‘const llvm::MCPhysReg [8]’ {aka ‘const short unsigned int [8]’} [-Warray-bounds=]
     20 /home/david.spickett/llvm-project/llvm/lib/Target/BPF/BPF.h:25:20: warning: ‘llvm::BPF_TRAP’ defined but not used [-Wunused-variable]
     11 /home/david.spickett/llvm-project/llvm/lib/DWARFLinker/Parallel/OutputSections.h:157:67: warning: member ‘llvm::dwarf_linker::parallel::SectionDescriptor::Contents’ is used uninitialized [-Wuninitialized]
     10 /home/david.spickett/llvm-project/llvm/include/llvm/IR/Use.h:55:31: warning: array subscript -1 is outside array bounds of ‘const llvm::Instruction [115292150460684697]’ [-Warray-bounds=]
      5 /home/david.spickett/llvm-project/llvm/include/llvm/CodeGen/SelectionDAG.h:324:27: warning: storing the address of local variable ‘Listener’ in ‘*this.llvm::SelectionDAG::UpdateListeners’ [-Wdangling-pointer=]
      4 /home/david.spickett/llvm-project/llvm/tools/llvm-readobj/ELFDumper.cpp:6950:22: warning: possibly dangling reference to a temporary [-Wdangling-reference]
      3 /home/david.spickett/llvm-project/llvm/include/llvm/IR/Use.h:54:37: warning: array subscript -3 is outside array bounds of ‘const llvm::Instruction [115292150460684697]’ [-Warray-bounds=]
      2 /usr/include/c++/13/bits/stl_algobase.h:437:30: warning: ‘void* __builtin_memmove(void*, const void*, long unsigned int)’ offset [16, 24] is out of the bounds [0, 16] of object ‘NewSyms’ with type ‘llvm::SmallVector<const llvm::MCSymbol*, 0>’ [-Warray-bounds=]
      2 /usr/include/c++/13/bits/stl_algobase.h:437:30: warning: ‘void* __builtin_memmove(void*, const void*, long unsigned int)’ forming offset 40 is out of the bounds [0, 40] of object ‘<anonymous>’ with type ‘llvm::sampleprof::SampleContextFrameVector’ {aka ‘llvm::SmallVector<llvm::sampleprof::SampleContextFrame, 1>’} [-Warray-bounds=]
      2 /home/david.spickett/llvm-project/llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp:1645:8: warning: variable ‘PrevIterCreatedNode’ set but not used [-Wunused-but-set-variable]
      2 /home/david.spickett/llvm-project/llvm/include/llvm/Support/Compiler.h:120:43: warning: attribute ignored [-Wattributes]
      1 /usr/include/c++/13/bits/atomic_base.h:505:31: warning: ‘long unsigned int __atomic_load_8(const volatile void*, int)’ writing 8 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]

Dangling reference to temporary I can't tell whether this is real or not, or whether there's a pattern we could use to make it know that it's not dangling.

Same deal for the array bounds, they look ok to me.

Fixed the dwarf linker one.

Maybe gcc 15 has improved the detection for these. Some of them are from inlined versions of functions which makes it hard to unpick.

@mstorsjo
Copy link
Member

Thanks for looking into it, and great to have it counted by category like that!

If there's some GCC warning category which seems noisy and mostly has false positives, we perhaps should consider to silence that category entirely (like the array bounds)?

@DavidSpickett
Copy link
Collaborator Author

Yeah could do. I'd like to understand why gcc thinks this, because it would be nicer to write it in a way that it didn't. Especially the dangling reference ones.

 20 /home/david.spickett/llvm-project/llvm/lib/Target/BPF/BPF.h:25:20: warning: ‘llvm::BPF_TRAP’ defined but not used [-Wunused-variable]

Should mention that this one is a false positive.

tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
For whatever reason I can't reproduce this locally but I can on Compiler
Explorer (https://godbolt.org/z/nfv4b83q6) and on our flang gcc bot
(https://lab.llvm.org/buildbot/#/builders/130/builds/13683/steps/5/logs/stdio).

In file included from ../llvm-project/llvm/include/llvm/LTO/LTO.h:33,
from
../llvm-project/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:29:
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h: In
constructor ‘llvm::FunctionImporter::ImportListsTy::ImportListsTy()’:
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h:275:33:
warning: member ‘llvm::FunctionImporter::ImportListsTy::ImportIDs’ is
used uninitialized [-Wuninitialized]
  275 |     ImportListsTy() : EmptyList(ImportIDs) {}
      |                                 ^~~~~~~~~
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h: In
constructor
‘llvm::FunctionImporter::ImportListsTy::ImportListsTy(size_t)’:

../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h:276:44:
warning: member ‘llvm::FunctionImporter::ImportListsTy::ImportIDs’ is
used uninitialized [-Wuninitialized]
276 | ImportListsTy(size_t Size) : EmptyList(ImportIDs), ListsImpl(Size)
{}
      |                                            ^~~~~~~~~

ImportIDs was being used during construction of EmptyList, before
ImportIDs itself had been constructed.
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
For whatever reason I can't reproduce this locally but I can on Compiler
Explorer (https://godbolt.org/z/nfv4b83q6) and on our flang gcc bot
(https://lab.llvm.org/buildbot/#/builders/130/builds/13683/steps/5/logs/stdio).

In file included from ../llvm-project/llvm/include/llvm/LTO/LTO.h:33,
from
../llvm-project/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:29:
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h: In
constructor ‘llvm::FunctionImporter::ImportListsTy::ImportListsTy()’:
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h:275:33:
warning: member ‘llvm::FunctionImporter::ImportListsTy::ImportIDs’ is
used uninitialized [-Wuninitialized]
  275 |     ImportListsTy() : EmptyList(ImportIDs) {}
      |                                 ^~~~~~~~~
../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h: In
constructor
‘llvm::FunctionImporter::ImportListsTy::ImportListsTy(size_t)’:

../llvm-project/llvm/include/llvm/Transforms/IPO/FunctionImport.h:276:44:
warning: member ‘llvm::FunctionImporter::ImportListsTy::ImportIDs’ is
used uninitialized [-Wuninitialized]
276 | ImportListsTy(size_t Size) : EmptyList(ImportIDs), ListsImpl(Size)
{}
      |                                            ^~~~~~~~~

ImportIDs was being used during construction of EmptyList, before
ImportIDs itself had been constructed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants