Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

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

Kimock__Benjamin
Beginner
555 Views

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
555 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?

0 Kudos
Kimock__Benjamin
Beginner
555 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.

0 Kudos
Viet_H_Intel
Moderator
555 Views

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

0 Kudos
Kimock__Benjamin
Beginner
555 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);
}

 

0 Kudos
Viet_H_Intel
Moderator
555 Views

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

0 Kudos
Viet_H_Intel
Moderator
555 Views

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

0 Kudos
Reply