Skip to content
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

[CLANG_X] use of infinity is undefined behavior due to the currently enabled floating-point options #45181

Closed
iarspider opened this issue Jun 10, 2024 · 8 comments · Fixed by #45488

Comments

@iarspider
Copy link
Contributor

Splitting off from cms-sw/cmsdist#9200 for visibility

In CLANG_X IBs, a lot of Wnan-infinity-disabled warnings are generated, e.g.

  src/DataFormats/EgammaReco/interface/ElectronSeed.h:115:46: warning: use of infinity is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]
   115 |     void setNegAttributes(const float dRZ2 = std::numeric_limits<float>::infinity(),
      |                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  src/DataFormats/EgammaReco/interface/ElectronSeed.h:116:47: warning: use of infinity is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]
   116 |                           const float dPhi2 = std::numeric_limits<float>::infinity(),
      |                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  src/DataFormats/EgammaReco/interface/ElectronSeed.h:117:46: warning: use of infinity is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]
   117 |                           const float dRZ1 = std::numeric_limits<float>::infinity(),
      |                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  src/DataFormats/EgammaReco/interface/ElectronSeed.h:118:47: warning: use of infinity is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]
   118 |                           const float dPhi1 = std::numeric_limits<float>::infinity());
      |                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  src/DataFormats/EgammaReco/interface/ElectronSeed.h:119:46: warning: use of infinity is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]
   119 |     void setPosAttributes(const float dRZ2 = std::numeric_limits<float>::infinity(),
      |                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  src/DataFormats/EgammaReco/interface/ElectronSeed.h:120:47: warning: use of infinity is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]
   120 |                           const float dPhi2 = std::numeric_limits<float>::infinity(),
      |                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  src/DataFormats/EgammaReco/interface/ElectronSeed.h:121:46: warning: use of infinity is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]
   121 |                           const float dRZ1 = std::numeric_limits<float>::infinity(),
      |                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  src/DataFormats/EgammaReco/interface/ElectronSeed.h:122:47: warning: use of infinity is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]
   122 |                           const float dPhi1 = std::numeric_limits<float>::infinity());
      |                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  src/DataFormats/EgammaReco/interface/ElectronSeed.h:143:63: warning: use of infinity is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]
   143 |       return hitNr < hitInfo_.size() ? hitInfo_[hitNr].*val : std::numeric_limits<T>::infinity();
      |                                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/DataFormats/EgammaReco/interface/ElectronSeed.h:102:48: note: in instantiation of function template specialization 'reco::ElectronSeed::getVal<float>' requested here
  102 |     float dPhiNeg(size_t hitNr) const { return getVal(hitNr, &PMVars::dPhiNeg); }
      |                                                ^
  src/DataFormats/EgammaReco/interface/ElectronSeed.h:143:63: warning: use of infinity is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]
   143 |       return hitNr < hitInfo_.size() ? hitInfo_[hitNr].*val : std::numeric_limits<T>::infinity();
      |                                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/DataFormats/EgammaReco/interface/ElectronSeed.h:110:52: note: in instantiation of function template specialization 'reco::ElectronSeed::getVal<int>' requested here
  110 |     int layerOrDiskNr(size_t hitNr) const { return getVal(hitNr, &PMVars::layerOrDiskNr); }
      |                                                    ^

We are building cmssw with -ffast-math, which sets (among other flags) -ffinite-math-only, allowing compiler to assume all floating-point numbers will be finite (no NaNs or infinities), but some functions explicitly return std::numeric_limits<float>::infinity and std::numeric_limits<float>::quiet_NaN.

If I'm reading @makortel's comment right, we check for NaN/infinity ourselves (instead of relying on compiler to do it), so this warning can be disabled.

@iarspider
Copy link
Contributor Author

assign core

@cmsbuild
Copy link
Contributor

New categories assigned: core

@Dr15Jones,@makortel,@smuzaffar you have been requested to review this Pull request/Issue and eventually sign? Thanks

@cmsbuild
Copy link
Contributor

cmsbuild commented Jun 10, 2024

cms-bot internal usage

@cmsbuild
Copy link
Contributor

A new Issue was created by @iarspider.

@smuzaffar, @rappoccio, @sextonkennedy, @Dr15Jones, @antoniovilela, @makortel can you please review it and eventually sign/assign? Thanks.

cms-bot commands are listed here

@makortel
Copy link
Contributor

If I'm reading @makortel's comment right, we check for NaN/infinity ourselves (instead of relying on compiler to do it), so this warning can be disabled.

I meant we need to use edm::isFinite etc to check for infinity/nan in presence of -ffast-math (although one could also ask if such code relying on IEEE infinity behavior should really be compiled with -ffast-math...).

This warning (and the similar static analyzer check) are very useful to catch the problems.

@VinInn
Copy link
Contributor

VinInn commented Jun 21, 2024

Just use std::numeric_limits<float>::max()

@makortel
Copy link
Contributor

+core

@cmsbuild
Copy link
Contributor

This issue is fully signed and ready to be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants