https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107661

--- Comment #3 from Sergei Trofimovich <slyfox at gcc dot gnu.org> ---
Created attachment 53889
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53889&action=edit
a_simpler.cc

a_simpler.cc is a bit more trimmed down version of a.cc: does not need pragmas
or argument forwarding. Uses only -O1 -fipa-cp -fipa-cp-clone:

$ ./gcc-13-HEAD/bin/gcc -Wall -O1 -fipa-cp -fipa-cp-clone -DDISABLE_HACK
a_simpler.cc -o a && ./a
$ ./gcc-13-HEAD/bin/gcc -Wall -O1 -fipa-cp -fipa-cp-clone a_simpler.cc -o a &&
./a
Illegal instruction (core dumped)

/// 'function_ref' is taken from llvm-12 as is without any modifications.
/// The rest if severely maimed AMDGCN hazard verifier code.

// How to break:
// $ ./gcc-13-snap/bin/gcc -O1 -fipa-cp -fipa-cp-clone               
a_simpler.cc -o a && ./a
// Illegal instruction (core dumped)
// $ ./gcc-13-snap/bin/gcc -O1 -fipa-cp -fipa-cp-clone -DDISABLE_HACK
a_simpler.cc -o a && ./a
// <ok>

// #define DISABLE_HACK 1

#include <utility>

class function_ref {
  void (*callback)(void * callable) = nullptr;
  void * callable;

  template<typename Callable>
  static void callback_fn(void * callable) {
    (*reinterpret_cast<Callable*>(callable))();
  }

public:
  function_ref() = delete;

  template <typename Callable>
  function_ref(
      Callable &&callable,
      // This is not the copy-constructor.
      std::enable_if_t<
          !std::is_same<std::remove_cv_t<std::remove_reference_t<Callable>>,
                        function_ref>::value> * = nullptr)
      : callback(callback_fn<typename std::remove_reference<Callable>::type>),
        callable(reinterpret_cast<void*>(&callable)) {}

  void operator()(void) const {
    callback(callable);
  }
};

static int get_mbb_b(int MBB) { return MBB; }
static int get_mbb_e(int MBB) {
    static int n = 0;
    switch (n++) {
      case 0: return MBB + 1;
      default: return MBB;
    }
}

static void getWaitStatesSince6(int MBB, int I, int WaitStates, function_ref
Expired) {
  Expired();

  if (I) {
    WaitStates += 1;
  }

  auto pri = get_mbb_b(MBB);
  auto pre = get_mbb_e(MBB);
  if (pri != pre) {
    getWaitStatesSince6(pri, I, WaitStates, Expired);
  }
}

static void getWaitStatesSince3(int MI, function_ref Expired) {
  getWaitStatesSince6(0, MI, 42, Expired);
}

static void always_void(void) { }

int main() {
  getWaitStatesSince3(0, always_void);
}

#if defined(DISABLE_HACK)
#else
void seemingly_unused_foo(int MI) {
  struct L {
    void operator()(void) const {
      __builtin_trap();
    }
  };
  L IsExpiredFn;

  getWaitStatesSince3(MI, IsExpiredFn);
}
#endif

Reply via email to