This is an archive of the discontinued LLVM Phabricator instance.

[amdgpu-arch] Guard hsa.h with __has_include
ClosedPublic

Authored by pdhaliwal on May 7 2021, 5:13 AM.

Details

Summary

This patch is suppose to fix the issue of hsa.h not found.
Issue was reported in D99949

Diff Detail

Event Timeline

pdhaliwal created this revision.May 7 2021, 5:13 AM
pdhaliwal requested review of this revision.May 7 2021, 5:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 7 2021, 5:13 AM
JonChesterfield added a comment.EditedMay 7 2021, 6:12 AM

Logic doesn't look quite right to me. If the compiler supports has_include, but neither of those headers exist, we fall through to main() which won't compile.

How about:

#if defined(__has_include)
  #if __has_include("hsa.h")
    #define HSA_FOUND 1 // name tbd
    #include "hsa.h"
  #elif __has_include("hsa/hsa.h")
    #define HSA_FOUND 1
    #include "hsa/hsa.h"
  #else
    #define HSA_FOUND 0
  #endif
#else
  #define HSA_FOUND 0
#endif


#if !HSA_FOUND
int main() { return 1; }
#else
// all the current stuff
#endif

That takes the approach that missing __has_include is a bad sign and immediately gives up. As far as I can tell it was introduced ~ 10 years ago, and was more recently added to c++17 (https://en.cppreference.com/w/cpp/preprocessor/include, which uses a similar pattern to the above in terms of booleans). Seems to be present in gcc, clang, msvc and we have a safe fallback if it isn't.

edit: We could also start down the dlopen path, but let's not do that if we can avoid it

pdhaliwal updated this revision to Diff 343668.May 7 2021, 6:48 AM

Added fallback in case __has_include is not defined or header is not found anywhere.

JonChesterfield accepted this revision.May 7 2021, 7:17 AM

LGTM, thanks. I think this leaves us totally sure the clang build will succeed with a best effort chance of having a working amdgpu ident tool afterwards.

This revision is now accepted and ready to land.May 7 2021, 7:17 AM

A thought - we need to keep return 0 for success, but could return a different integer for each failure mode. That would be useful when guessing why it failed. Orthogonal to this patch.

This revision was automatically updated to reflect the committed changes.