diff --git a/src/ImplicitCudaHeaders.cpp b/src/ImplicitCudaHeaders.cpp new file mode 100644 index 00000000..11ca4149 --- /dev/null +++ b/src/ImplicitCudaHeaders.cpp @@ -0,0 +1,37 @@ +/* +Copyright (c) 2026 - present Advanced Micro Devices, Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include "ImplicitCudaHeaders.h" +#include "clang/Tooling/ArgumentsAdjusters.h" + +namespace hipify { + +static const char *const ImplicitCudaHeader = "cuda_runtime.h"; + +void addImplicitCudaHeaders(ct::RefactoringTool &Tool) { + Tool.appendArgumentsAdjuster(ct::getInsertArgumentAdjuster( + ImplicitCudaHeader, ct::ArgumentInsertPosition::BEGIN)); + Tool.appendArgumentsAdjuster(ct::getInsertArgumentAdjuster( + "-include", ct::ArgumentInsertPosition::BEGIN)); +} + +} // namespace hipify diff --git a/src/ImplicitCudaHeaders.h b/src/ImplicitCudaHeaders.h new file mode 100644 index 00000000..3c3a1e9c --- /dev/null +++ b/src/ImplicitCudaHeaders.h @@ -0,0 +1,44 @@ +/* +Copyright (c) 2026 - present Advanced Micro Devices, Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#pragma once + +#include "clang/Tooling/Refactoring.h" + +namespace ct = clang::tooling; + +namespace hipify { + +/// nvcc automatically pre-includes certain CUDA headers at the beginning of +/// any compilation unit. +/// +/// The primary header is cuda_runtime.h, which transitively includes: +/// - host_config.h (System configuration) +/// - builtin_types.h (Basic CUDA types) +/// - device_types.h (Device enums and structs) +/// - host_defines.h (Macro definitions) +/// - driver_types.h (Driver types) +/// - vector_types.h (float3, int2, etc.) + +void addImplicitCudaHeaders(ct::RefactoringTool &Tool); + +} // namespace hipify diff --git a/src/main.cpp b/src/main.cpp index b7c866ce..0675bdfc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,6 +44,7 @@ THE SOFTWARE. #endif #include "LocalHeader.h" +#include "ImplicitCudaHeaders.h" #if LLVM_VERSION_MAJOR < 8 #include "llvm/Support/Path.h" @@ -221,6 +222,9 @@ bool appendArgumentsAdjusters(ct::RefactoringTool &Tool, const std::string &sSou std::string sCudaPath = "--cuda-path=" + CudaPath; Tool.appendArgumentsAdjuster(ct::getInsertArgumentAdjuster(sCudaPath.c_str(), ct::ArgumentInsertPosition::BEGIN)); } + + // Implicit CUDA headers to mimic nvcc behavior + hipify::addImplicitCudaHeaders(Tool); llcompat::addTargetIfNeeded(Tool); Tool.appendArgumentsAdjuster(ct::getInsertArgumentAdjuster("cuda", ct::ArgumentInsertPosition::BEGIN)); Tool.appendArgumentsAdjuster(ct::getInsertArgumentAdjuster("-x", ct::ArgumentInsertPosition::BEGIN));