Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Kimock__Benjamin
Beginner
77 Views

Missed optimization- using undefined symbol to do compile-time check

This is sort of a trick to get an optimizing compiler to prove an interesting property of some code; we insert a call to an nonexistent extern function along the code path we want to have proven is impossible. If the optimizer fails to prove that the code path is never taken, the code fails to link. I tried to use this in a Vec3/Point implementation and was surprised to see that icc doesn't manage to prove the extern function is never called (clang and gcc do manage this). The runtime performance of this code isn't really the point, what I'd like is the optimizer's proof.

https://godbolt.org/z/SqPBZ_

0 Kudos
6 Replies
Viet_H_Intel
Moderator
77 Views

If the optimizer fails to prove that the code path is never taken, the code fails to link. 

Can you provide us a test case to show this happens?

Kimock__Benjamin
Beginner
77 Views

I provided a godbolt link in my original post, which is a case I've run into. Is that insufficient? I'm happy to whip up something else if it doesn't fulfill your requirements.

Viet_H_Intel
Moderator
77 Views

It would be helpful if you could provide us code that fails to link.

Kimock__Benjamin
Beginner
77 Views

#include <vector>

extern double optimization_failed();

struct Vec3 {
  double x, y, z;

  double operator[](int index) const {
    switch (index) {
    case 0:
      return x;
    case 1:
      return y;
    case 2:
      return z;
    default:
      return optimization_failed();
    }
  }
};

std::vector<Vec3> r;
std::vector<double> w;

bool point_is_in_cell(Vec3 const point, int _i_cell) {
  for (int i = 0; i < 3; ++i)
    if (point < r[_i_cell] || point >= r[_i_cell] + w[_i_cell])
      return false;
  return true;
}

int main() {
  w = std::vector<double>(1, 1.);
  r = std::vector<Vec3>(1, Vec3{1., 2., 3.});

  return point_is_in_cell(Vec3{0., 0., 0.}, 0);
}

 

Viet_H_Intel
Moderator
77 Views

Thank for the test case. I'll take a look and get back to you.

Viet_H_Intel
Moderator
77 Views

I've reported this case to the compiler team. An internal report number is: CMPLRIL0-31953

Reply