From 302b28f94a8115f7dda3ca95851c0f0f0d9585e4 Mon Sep 17 00:00:00 2001 From: ranapratap55 Date: Sun, 1 Feb 2026 22:48:13 +0530 Subject: [PATCH 1/2] [HIPIFY][feature] Add implicit CUDA header inclusion to mimic nvcc behavior --- src/ImplicitCudaHeaders.cpp | 48 +++++++++++++++++++++++++++++++++++++ src/ImplicitCudaHeaders.h | 46 +++++++++++++++++++++++++++++++++++ src/main.cpp | 4 ++++ 3 files changed, 98 insertions(+) create mode 100644 src/ImplicitCudaHeaders.cpp create mode 100644 src/ImplicitCudaHeaders.h diff --git a/src/ImplicitCudaHeaders.cpp b/src/ImplicitCudaHeaders.cpp new file mode 100644 index 00000000..5a094a9a --- /dev/null +++ b/src/ImplicitCudaHeaders.cpp @@ -0,0 +1,48 @@ +/* +Copyright (c) 2015 - 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 { + +std::vector getImplicitCudaHeaders() { + return { + "cuda_runtime.h" + }; +} + +void addImplicitCudaHeaders(ct::RefactoringTool &Tool) { + + std::vector headers = getImplicitCudaHeaders(); + + for (const auto &header : headers) { + Tool.appendArgumentsAdjuster( + ct::getInsertArgumentAdjuster(header.c_str(), + 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..fbcd4bf0 --- /dev/null +++ b/src/ImplicitCudaHeaders.h @@ -0,0 +1,46 @@ +/* +Copyright (c) 2015 - 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); + +std::vector getImplicitCudaHeaders(); + +} // 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)); From 805b7d363e4813be1674e5ca8a34ced378daf630 Mon Sep 17 00:00:00 2001 From: ranapratap55 Date: Mon, 9 Feb 2026 15:04:48 +0530 Subject: [PATCH 2/2] Removed getImplicitCudaHeaders() and modified the Copyright --- src/ImplicitCudaHeaders.cpp | 23 ++++++----------------- src/ImplicitCudaHeaders.h | 4 +--- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/src/ImplicitCudaHeaders.cpp b/src/ImplicitCudaHeaders.cpp index 5a094a9a..11ca4149 100644 --- a/src/ImplicitCudaHeaders.cpp +++ b/src/ImplicitCudaHeaders.cpp @@ -1,5 +1,5 @@ /* -Copyright (c) 2015 - present Advanced Micro Devices, Inc. All rights reserved. +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 @@ -25,24 +25,13 @@ THE SOFTWARE. namespace hipify { -std::vector getImplicitCudaHeaders() { - return { - "cuda_runtime.h" - }; -} +static const char *const ImplicitCudaHeader = "cuda_runtime.h"; void addImplicitCudaHeaders(ct::RefactoringTool &Tool) { - - std::vector headers = getImplicitCudaHeaders(); - - for (const auto &header : headers) { - Tool.appendArgumentsAdjuster( - ct::getInsertArgumentAdjuster(header.c_str(), - ct::ArgumentInsertPosition::BEGIN)); - Tool.appendArgumentsAdjuster( - ct::getInsertArgumentAdjuster("-include", - ct::ArgumentInsertPosition::BEGIN)); - } + 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 index fbcd4bf0..3c3a1e9c 100644 --- a/src/ImplicitCudaHeaders.h +++ b/src/ImplicitCudaHeaders.h @@ -1,5 +1,5 @@ /* -Copyright (c) 2015 - present Advanced Micro Devices, Inc. All rights reserved. +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 @@ -41,6 +41,4 @@ namespace hipify { void addImplicitCudaHeaders(ct::RefactoringTool &Tool); -std::vector getImplicitCudaHeaders(); - } // namespace hipify