Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
356 changes: 235 additions & 121 deletions src/LocalHeader.cpp

Large diffs are not rendered by default.

54 changes: 46 additions & 8 deletions src/LocalHeader.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,66 @@
/*
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/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"
#include <string>
#include <vector>
#include "clang/Tooling/CommonOptionsParser.h"

namespace ct = clang::tooling;

// A single `#include` directive, recorded in preprocessing order.
struct IncludeEntry {
std::string fileName;
std::string resolvedPath;
std::string includerPath;
bool isAngled = false;
bool isSystem = false;
bool isFromMainFile = false;
};

extern bool appendArgumentsAdjusters(ct::RefactoringTool &Tool,
const std::string &sSourceAbsPath,
const char *hipify_exe);

extern bool hipifySingleSource(const std::string &srcPath,
const std::string &dstPath,
const ct::CompilationDatabase *compDB,
ct::CommonOptionsParser *OptionsParserPtr,
const char *hipify_exe_path,
const std::string &mainContextPath,
bool preserveTemp);
bool preserveTemp,
const std::vector<std::string> &additionalIncludes = {});

bool hipifyLocalHeaders(const std::string &srcPath,
const ct::CompilationDatabase *compDB,
ct::CommonOptionsParser *OptionsParserPtr,
const char *hipify_exe,
bool recursive = false);

bool resolveLocalInclude(const std::string &mainSourceAbsPath,
const std::string &includeToken,
std::string &outAbsPath);

bool collectLocalQuotedIncludes(const std::string &mainSourceAbsPath,
std::vector<std::string> &outHeaders);
// Preprocesses srcPath and records its whole include tree in outEntries.
bool collectIncludeTree(const std::string &srcPath,
const ct::CompilationDatabase *compDB,
ct::CommonOptionsParser *OptionsParserPtr,
const char *hipify_exe,
std::vector<IncludeEntry> &outEntries);
40 changes: 31 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ THE SOFTWARE.
*/

#include <fstream>
#include <vector>
#include "CUDA2HIP.h"
#include "CUDA2HIP_Scripting.h"
#include "LLVMCompat.h"
Expand Down Expand Up @@ -241,7 +242,8 @@ bool hipifySingleSource(const std::string &srcPath,
ct::CommonOptionsParser *OptionsParserPtr,
const char *hipify_exe_path,
const std::string &mainContextPath,
bool preserveTemp) {
bool preserveTemp,
const std::vector<std::string> &additionalIncludes) {
std::error_code EC;
SmallString<128> tmpFile;
StringRef srcFileName = sys::path::filename(srcPath);
Expand Down Expand Up @@ -276,6 +278,27 @@ bool hipifySingleSource(const std::string &srcPath,
return false;
}

// The copy is preprocessed from a temporary directory, so includes quoted
// relative to the original one need an explicit search path.
StringRef srcDir = sys::path::parent_path(srcPath);
if (!srcDir.empty()) {
std::string sSrcDir = "-I" + srcDir.str();
Tool.appendArgumentsAdjuster(ct::getInsertArgumentAdjuster(
sSrcDir.c_str(), ct::ArgumentInsertPosition::BEGIN));
}

// Appended at the end to follow the implicit CUDA headers, inserted at the
// beginning: a header may use CUDA declarations without including any.
if (!additionalIncludes.empty()) {
ct::CommandLineArguments includeArgs;
for (const std::string &include : additionalIncludes) {
includeArgs.push_back("-include");
includeArgs.push_back(include);
}
Tool.appendArgumentsAdjuster(ct::getInsertArgumentAdjuster(
includeArgs, ct::ArgumentInsertPosition::END));
}

// Hipify _all_ the things!
if (Tool.runAndSave(&actionFactory)) {
llvm::errs() << "\n" << sHipify << sError
Expand Down Expand Up @@ -516,26 +539,25 @@ int main(int argc, const char **argv) {
}
// Initialise the statistics counters for this file.
Statistics::setActive(src);
// Checks the local headers if --local-headers/--local-header-recursive specified.
if (OptLocalHeaders || OptLocalHeadersRecursive) {
if (!hipifyLocalHeaders(sSourceAbsPath,
compilationDatabase.get(),
&OptionsParser,
argv[0],
if (!hipifyLocalHeaders(sSourceAbsPath, compilationDatabase.get(),
&OptionsParser, argv[0],
OptLocalHeadersRecursive)) {
llvm::errs() << "\n" << sHipify << sError
<< "Local header hipification failed for: "
<< sys::path::filename(sSourceAbsPath) << "\n";
Statistics::current().hasErrors = true;
LLVM_DEBUG(llvm::dbgs() << "Local header hipification failed for: " << sSourceAbsPath << "\n");
Result = 1;
}
}

std::string outputPath = NoOutput ? "" : dst;
if (!hipifySingleSource(src, outputPath,
compilationDatabase.get(),
&OptionsParser,
argv[0],
sSourceAbsPath,
false)) {
false, {})) {
Statistics::current().hasErrors = true;
Result = 1;
LLVM_DEBUG(llvm::dbgs() << "Hipification failed for: " << src << "\n");
Expand Down
7 changes: 7 additions & 0 deletions tests/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ config.excludes.append('inc.h')
config.excludes.append('inet.h')
config.excludes.append('types.h')
config.excludes.append('socket.h')
config.excludes.append('transitive_parent.h')
config.excludes.append('transitive_child.h')
config.excludes.append('diamond_left.h')
config.excludes.append('diamond_right.h')
config.excludes.append('diamond_shared.h')
# Not self-contained by design; only hipifiable through non_self_contained.cu.
config.excludes.append('nsc_dependent.h')

delimiter = "===============================================================";
print(delimiter)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/headers/local_headers/angle_bracket.cu
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args --local-headers %clang_args
// RUN: %run_test hipify "%s" "%t" %hipify_args 1 --local-headers %clang_args

// CHECK: #include <hip/hip_runtime.h>
// CHECK-NOT: #include <cuda_runtime.h>
Expand Down
15 changes: 15 additions & 0 deletions tests/unit_tests/headers/local_headers/block_comment_include.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args %clang_args

#ifndef BLOCK_COMMENT_INCLUDE_H
#define BLOCK_COMMENT_INCLUDE_H

// CHECK: #include <hip/hip_runtime.h>
// CHECK-NOT: #include <cuda_runtime.h>
#include <cuda_runtime.h>

inline void block_comment_sync() {
// CHECK: hipDeviceSynchronize();
cudaDeviceSynchronize();
}

#endif
2 changes: 1 addition & 1 deletion tests/unit_tests/headers/local_headers/common.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args --local-headers %clang_args
// RUN: %run_test hipify "%s" "%t" %hipify_args 1 --local-headers %clang_args

#ifndef COMMON_H
#define COMMON_H
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/headers/local_headers/cyclic_header.cu
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args --local-headers-recursive %clang_args
// RUN: %run_test hipify "%s" "%t" %hipify_args 1 --local-headers-recursive %clang_args

// CHECK: #include <hip/hip_runtime.h>
// CHECK-NOT: #include <cuda_runtime.h>
Expand Down
13 changes: 13 additions & 0 deletions tests/unit_tests/headers/local_headers/diamond_dep.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args 1 --local-headers-recursive %clang_args

// CHECK: #include <hip/hip_runtime.h>
// CHECK-NOT: #include <cuda_runtime.h>
// CHECK: #include "diamond_left.h"
// CHECK: #include "diamond_right.h"
#include <cuda_runtime.h>
#include "diamond_left.h"
#include "diamond_right.h"

int main() {
return 0;
}
11 changes: 11 additions & 0 deletions tests/unit_tests/headers/local_headers/diamond_left.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef DIAMOND_LEFT_H
#define DIAMOND_LEFT_H

#include "diamond_shared.h"

inline void left_sync() {
// CHECK: hipDeviceSynchronize();
cudaDeviceSynchronize();
}

#endif
11 changes: 11 additions & 0 deletions tests/unit_tests/headers/local_headers/diamond_right.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef DIAMOND_RIGHT_H
#define DIAMOND_RIGHT_H

#include "diamond_shared.h"

inline void right_free(void *p) {
// CHECK: hipFree(p);
cudaFree(p);
}

#endif
9 changes: 9 additions & 0 deletions tests/unit_tests/headers/local_headers/diamond_shared.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef DIAMOND_SHARED_H
#define DIAMOND_SHARED_H

inline void shared_alloc(void **p, size_t size) {
// CHECK: hipMalloc(p, size);
cudaMalloc(p, size);
}

#endif
2 changes: 1 addition & 1 deletion tests/unit_tests/headers/local_headers/duplicate.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args --local-headers %clang_args
// RUN: %run_test hipify "%s" "%t" %hipify_args %clang_args

#ifndef DUP_H
#define DUP_H
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args --local-headers %clang_args
// RUN: %run_test hipify "%s" "%t" %hipify_args 1 --local-headers %clang_args

// CHECK: #include <hip/hip_runtime.h>
// CHECK: #include "duplicate.h"
Expand Down
16 changes: 16 additions & 0 deletions tests/unit_tests/headers/local_headers/injection_has_cmath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args %clang_args

#ifndef INJECTION_HAS_CMATH_H
#define INJECTION_HAS_CMATH_H

// CHECK: #include <hip/hip_runtime.h>
// CHECK-NOT: #include <cuda_runtime.h>
// CHECK: #include <cmath>
#include <cuda_runtime.h>
#include <cmath>

inline float compute_value(float x) {
return sqrtf(x) + 1.0f;
}

#endif
22 changes: 22 additions & 0 deletions tests/unit_tests/headers/local_headers/injection_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args %clang_args

#ifndef INJECTION_HELPER_H
#define INJECTION_HELPER_H

// CHECK: #include <hip/hip_runtime.h>
// CHECK-NOT: #include <cuda_runtime.h>
#include <cuda_runtime.h>

inline __device__ float3 add_vectors(float3 a, float3 b) {
return make_float3(0.0f, 0.0f, 0.0f);
}

inline __device__ void accumulate(float3* sum, float3 val) {
return;
}

inline __device__ float3 scale_and_diff(float3 a, float3 b, float s) {
return make_float3(0.0f, 0.0f, 0.0f);
}

#endif
14 changes: 14 additions & 0 deletions tests/unit_tests/headers/local_headers/injection_inner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args %clang_args

#ifndef INJECTION_INNER_H
#define INJECTION_INNER_H

// CHECK: #include <hip/hip_runtime.h>
// CHECK-NOT: #include <cuda_runtime.h>
#include <cuda_runtime.h>

inline __device__ void inner_add(float3* data, int idx) {
return;
}

#endif
22 changes: 22 additions & 0 deletions tests/unit_tests/headers/local_headers/injection_multi.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args 1 --local-headers %clang_args

// CHECK: #include <hip/hip_runtime.h>
// CHECK-NOT: #include <cuda_runtime.h>
// CHECK: #include <cmath>
// CHECK: #include "vector_math.h"
// CHECK: #include "injection_helper.h"
// CHECK: #include "injection_uses_cmath.h"
#include <cuda_runtime.h>
#include <cmath>
#include "vector_math.h"
#include "injection_helper.h"
#include "injection_uses_cmath.h"

__global__ void multiKernel(float3* data, float* vals) {
int idx = threadIdx.x;
dummy_vector_op();
add_vectors(data[idx], data[idx + 1]);
vals[idx] = compute_sqrt(vals[idx]);
}

int main() { return 0; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args 1 --local-headers %clang_args

// CHECK: #include <hip/hip_runtime.h>
// CHECK-NOT: #include <cuda_runtime.h>
// CHECK: #include <cmath>
// CHECK: #include "injection_has_cmath.h"
#include <cuda_runtime.h>
#include <cmath>
#include "injection_has_cmath.h"

int main() {
float x = compute_value(4.0f);
return (int)x;
}
16 changes: 16 additions & 0 deletions tests/unit_tests/headers/local_headers/injection_outer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args %clang_args

#ifndef INJECTION_OUTER_H
#define INJECTION_OUTER_H

// CHECK: #include <hip/hip_runtime.h>
// CHECK-NOT: #include <cuda_runtime.h>
// CHECK: #include "injection_inner.h"
#include <cuda_runtime.h>
#include "injection_inner.h"

inline __device__ void outer_process(float3* data, int idx) {
inner_add(data, idx);
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %run_test hipify "%s" "%t" %hipify_args %clang_args

#pragma once

// CHECK: #include <hip/hip_runtime.h>
// CHECK-NOT: #include <cuda_runtime.h>
#include <cuda_runtime.h>

inline __device__ void pragma_add(float3* data, int idx) {
return;
}
Loading
Loading