google / highway

Performance-portable, length-agnostic SIMD with runtime dispatch
Apache License 2.0
3.95k stars 305 forks source link

MSVC 2019 reports errors like "identifier Native is undefined" in error list, but still builds #2267

Closed Dwedit closed 2 days ago

Dwedit commented 4 days ago

In detect_compiler_arch.h, line 22:

// Add to #if conditions to prevent IDE from graying out code.
#if (defined __CDT_PARSER__) || (defined __INTELLISENSE__) || \
    (defined Q_CREATOR_RUN) || (defined __CLANGD__) ||        \
    (defined GROK_ELLIPSIS_BUILD)
#define HWY_IDE 1
#else
#define HWY_IDE 0
#endif

HWY_IDE is defined when MSVC's intellisense is running. But MSVC's intellisense is not just for gathering the names of identifiers, it also populates an Error List that shows code which contains errors, even though these errors do not apply when actually building the project.

One such error is "identifier 'Native' is undefined".

"Native" is defined in base.h, line 1075:

#if HWY_HAVE_SCALAR_F16_TYPE
#if HWY_RVV_HAVE_F16_VEC || HWY_SSE2_HAVE_F16_TYPE
  using Native = _Float16;
#elif HWY_NEON_HAVE_F16C
  using Native = __fp16;
#else
#error "Logic error: condition should be 'all but NEON_HAVE_F16C'"
#endif
#endif  // HWY_HAVE_SCALAR_F16_TYPE

It is only defined when HWY_HAVE_SCALAR_F16_TYPE is set, and either HWY_RVV_HAVE_F16_VEC or HWY_SSE2_HAVE_F16_TYPE are set. Otherwise it is undefined.

But then "Native" is used without being defined when HWY_IDE is set.
One such place is in base.h, line 1129:

#if HWY_HAVE_SCALAR_F16_OPERATORS || HWY_IDE
  template <typename T, hwy::EnableIf<!IsSame<RemoveCvRef<T>, float16_t>() &&
                                      IsConvertible<T, Native>()>* = nullptr>

This puts an error into the Error List. But it still will compile correctly because HWY_IDE is not defined when building the project for real.

Having phantom errors appearing in the Error List is confusing for a programmer, especially since those errors do not affect whether a project builds successfully. There are several possible ways to resolve the errors out of the error list:

jan-wassenberg commented 2 days ago

Hi @Dwedit , thanks for pointing this out. Errors indeed sound undesirable. I like your third idea, providing a Native = uint16_t if f16 is unavailable and HWY_IDE. Would you like to send a pull request implementing that?