-
Notifications
You must be signed in to change notification settings - Fork 105
[HIPIFY][feature] Add header injection fallback for local headers hipification #2275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: amd-develop
Are you sure you want to change the base?
[HIPIFY][feature] Add header injection fallback for local headers hipification #2275
Conversation
|
Cases like CudaMath.h are a different story, as those header files are not self-contained and are not intended to be compiled as separate compilation units. The compilation unit here designed to be compiled is dxtc.cu, which is being correctly compiled by clang. FMPOV, the problem with
And not all of them necessarily need to be included during our hipification process. Such
So the question is: how should we determine whether to include this or that CUDA header file from the most likely incomplete list above? |
yes, you are correct. nvcc pre-includes the CUDA headers and headers like CudaMath.h is not designed to be a standalone compilation. But when users are trying to port using I believe we should support users even when their headers have implicit header dependencies (not only properly designed headers).
This approach avoids this problem because
whatever includes the main .cu/cpp file had before including this header - those are what the header needs here we include only header_1.h file for "sample.h". We don't need to know why it is needed just it comes before in main source file. This feature is a FALLBACK mechanism, not the primary/direct path. Here
I believe this approach works for any project (cuda-samples, etc.,) as we are only injecting original determined headers for that specific project. |
Nope. The proposed approach avoids the problems in CUDA samples, where additional header files such as The wider problem is that the necessary header file might not be provided at all. And this is the common practice in CUDA, as I tried to explain above.
Can't agree here; the approach won't work for projects that don't provide header files at all. |
You are right. I appreciate you clarifying the distinction. This approach does not solve the projects that rely entirely on nvcc's implicit pre-inclusion with no explicit system includes. I agree this is a limitation. I would like to take this feature as incremental improvement, with understanding that
I'm open to take a different approach that you'd suggest. |
|
On Windows: `error C2589: '(': illegal token on right side of '::'`:while compiling A possible reason is a Conflict: |
|
|
| @@ -0,0 +1,163 @@ | |||
| /* | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explain the reason(s) for having this Std Err Capture.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The StderrCapture class provides a clean, professional user experience in the hybrid hipification approach. The local header hipification uses below strategy:
Phase 1: Direct hipification (works for self-contained headers)
Phase 2: Injection fallback (if direct fails due to missing dependencies)
Without StderrCapture, when Phase 1 fails which is expected for non-self-contained headers, users might see confusing Clang compilation errors. These errors appear even though the injection fallback succeeds moments later. This creates confusion for users to think something is broken when the tool is working correctly.
When both approaches fail, we are showing users meaningful diagnostics/errors. This gives a well structured error(s) showing what went wrong in each phase, rather than an interleaved mess of error messages.
I want us to think a bit broader here and come to a solution, partially based on your PR. What if we implicitly include the "short list" of already-known "hidden" CUDA headers during the hipification of each source CUDA file, as the first step?
Will we get some issues in that scenario, performance penalties, etc.? |
Fixed in the latest commit. |
Thank you for detailed review. The current temp-file approach chosen for simplicity in initial implementation. The injection fallback is only triggered when direct hipification fails. That said, VFS and Clang's PPCallbacks approach would be cleaner and robust. Would you prefer I refractor the code in this PR or accept this in a follow-up PR improvement? |
Valid point. The current implementation is not thread safe. If parallelization is planned, I have added a static mutex to serialize the stderr captures. This mutex approach is thread safe by ensuring only one thread can capture stderr at a time. |
This inclusion of already-known CUDA headers is interesting approach. It would address the nvcc's implicit pre-inclusion problem. I see some potential concerns
One approach I think of is we can make it configurable with CLI flag option for users to enable/disable the short list injection. Would you prefer having CLI flag approach for short list of already knows headers? |
Nope, as those CUDA header files (or all the included header files, like, for instance, defined in the #if !defined(...) #define ... #endif
Nope, for the same reason.
This we can overcome, as hipify-clang now detects the CUDA version at runtime.
I'd prefer to do it implicitly (w/o flag), as there is no actual point for the end user to see compilation errors due to the headers implicitly mentioned by CUDA's nvcc. |
We need to use VFS by default everywhere in the |
If we reach a common decision, perhaps all of this |
Agreed. If we implement implicit CUDA header inclusion at the Clang preprocessor level which reduces the need for fallback phases. This potentially removes the need for |
Motivation
https://github.com/NVIDIA/cuda-samples/blob/master/Samples/5_Domain_Specific/dxtc/dxtc.cu
The
dxtc.cucuda-samples includes multiple local headers (CudaMath.h, dds.h, permutations.h) that depend onhelper_math.hfor float3 operator definitions. Without header injection, these headers fail with standalone hipify-clang because they lack the required operator overloads.Purpose
Enhances the existing
--local-headersand--local-headers-recursiveoptions with header injection fallback mechanism. When direct hipification of a local header fails due to missing type definitions or operator overloads, the header injection method automatically injects preceding system includes from the main source file.Problem Statement
The initial local header hipification works well for self-contained headers that include all their dependencies. However, many cuda-samples have headers that depend on types and operators defined in preceding includes from the main source file. For example, CudaMath.h in cuda-samples/Samples/5_Domain_Specific/dxtc/ uses float3 operators (+=, *, -) that are defined in helper_math.h, which is included in the dxtc.cu. Few more below
bodysystemcpu_impl.h
MonteCarlo_reduction.cuh
fastWalshTransform_kernel.cuh
bicubicTexture_kernel.cuh
Solution
Implements a hybrid approach:
Existing: First attempts direct hipification, Works for self-contained headers.
Enhanced version: Header Injection, when direct method fails, it collects preceding system header files (#include <...>) from the main source and includes them.
Usage
No change to CLI flags
Non-recursive:
hipify-clang --local-headers main.cu -I include/Recursive:
hipify-clang --local-headers-recursive main.cu -I include/