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
12 changes: 3 additions & 9 deletions src/ArgParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,8 @@ cl::opt<bool> HipDnnSupport("hipdnn",
cl::init(false),
cl::cat(ToolTemplateCategory));

cl::opt<bool> OptLocalHeaders("local-headers",
cl::desc("Enable hipification of quoted local headers (non-recursive)"),
cl::init(false),
cl::cat(ToolTemplateCategory));

cl::opt<bool> OptLocalHeadersRecursive("local-headers-recursive",
cl::desc("Enable hipification of quoted local headers recursively"),
cl::opt<bool> SkipLocalHeaders("skip-local-headers",
cl::desc("Skip implicit hipification of local (quoted) headers included in the main source file"),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the "local" headers are included with <>?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is out of scope. Angled-bracket includes files are resolved using -I search paths. They represent system headers or external library dependencies.

But I do think there is edge case like, if a project uses #include <mylib/utils.h> via -I project/src flag, that would require resolving each angled-bracket against every -I path. That is a meaningful work and requires a separate PR of its own.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2505: Angle-bracket local header hipification (#include <>)

cl::init(false),
cl::cat(ToolTemplateCategory));

Expand Down Expand Up @@ -264,8 +259,7 @@ const std::vector<std::string> hipifyOptions {
std::string(NoWarningsUndocumented.ArgStr),
std::string(HipifyAMAP.ArgStr),
std::string(HipDnnSupport.ArgStr),
std::string(OptLocalHeaders.ArgStr),
std::string(OptLocalHeadersRecursive.ArgStr),
std::string(SkipLocalHeaders.ArgStr),
};

const std::vector<std::string> hipifyOptionsWithTwoArgs {
Expand Down
3 changes: 1 addition & 2 deletions src/ArgParse.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,4 @@ extern cl::opt<bool> NoUndocumented;
extern cl::opt<bool> NoWarningsUndocumented;
extern cl::opt<bool> HipifyAMAP;
extern cl::opt<bool> HipDnnSupport;
extern cl::opt<bool> OptLocalHeaders;
extern cl::opt<bool> OptLocalHeadersRecursive;
extern cl::opt<bool> SkipLocalHeaders;
178 changes: 161 additions & 17 deletions src/LocalHeader.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
/*
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 "LocalHeader.h"
#include "LLVMCompat.h"

#include <sstream>
#include <regex>
Expand All @@ -11,8 +34,6 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"

#include "LLVMCompat.h"

using namespace clang;
using namespace clang::tooling;
using namespace llvm;
Expand Down Expand Up @@ -43,8 +64,11 @@ static bool pathExists(const std::string &p) {
}

namespace {
static const std::regex LocalIncludeRe(
R"(^\s*#\s*include\s*\"([^\"\n]+)\"\s*(?://.*)?$)", std::regex::ECMAScript);
static const std::regex LocalIncludeRe
(R"re(^\s*#\s*include\s*"([^"\n]+)")re", std::regex::ECMAScript);

static const std::regex SystemIncludeRe(
R"(^\s*#\s*include\s*<([^>\n]+)>)", std::regex::ECMAScript);

bool readFile(const std::string &path, std::string &out) {
auto MBOrErr = llvm::MemoryBuffer::getFile(path);
Expand All @@ -67,7 +91,77 @@ namespace {
}
return false;
}
}

static bool collectIncludesBefore(const std::string &filePath,
const std::string &stopAtAbsPath,
bool collectLocal,
std::set<std::string> &seen,
std::vector<std::string> &outIncludes) {
std::string content;
if (!readFile(filePath, content))
return false;

std::istringstream iss(content);
std::string line;
std::smatch m;

while (std::getline(iss, line)) {
if (std::regex_search(line, m, LocalIncludeRe)) {
std::string abspath;
if (resolveLocalIncludeInternal(filePath, m[1].str(), abspath)) {
if (abspath == stopAtAbsPath)
break;
if (collectLocal && seen.insert(abspath).second)
outIncludes.push_back(abspath);
}
}
if (std::regex_search(line, m, SystemIncludeRe)) {
std::string sysInclude = m[1].str();
if (seen.insert(sysInclude).second)
outIncludes.push_back(sysInclude);
}
}
return true;
}

bool collectPrecedingIncludes(const std::string &mainSourceAbspath,
const std::string &targetHeaderAbspath,
std::vector<std::string> &outIncludes) {

std::set<std::string> seen;

if (!collectIncludesBefore(mainSourceAbspath, targetHeaderAbspath, true, seen, outIncludes)) {
errs() << sHipify << sError << "Cannot read source files: "
<< mainSourceAbspath << "\n";
return false;
}
return true;
}

void collectAncestorSystemIncludes(
const std::vector<std::string> &ancestorChain,
std::vector<std::string> &outIncludes) {

std::set<std::string> seen(outIncludes.begin(), outIncludes.end());

for (size_t i = 1; i < ancestorChain.size(); ++i) {
collectIncludesBefore(ancestorChain[i], ancestorChain[i - 1], false, seen, outIncludes);
}
}
}

static std::string
resolveCompileContext(const std::string &parentPath,
const std::string &mainSourceAbsPath,
const clang::tooling::CompilationDatabase *compDB) {
if (!compDB)
return mainSourceAbsPath;

if (!compDB->getCompileCommands(parentPath).empty())
return parentPath;

return mainSourceAbsPath;
}

bool resolveLocalInclude(const std::string &mainSourceAbsPath,
const std::string &includeToken,
Expand All @@ -88,7 +182,7 @@ bool collectLocalQuotedIncludes(const std::string &mainSourceAbsPath,
std::istringstream iss(content);
std::string line;
while (std::getline(iss, line)) {
if (std::regex_match(line, m, LocalIncludeRe)) {
if (std::regex_search(line, m, LocalIncludeRe)) {
std::string rel = m[1].str();
std::string abs;
if (resolveLocalIncludeInternal(mainSourceAbsPath, rel, abs)){
Expand Down Expand Up @@ -116,52 +210,102 @@ bool hipifyLocalHeaders(const std::string &mainSourceAbsPath,
}

if (initial.empty()) {
outs() << sHipify << "No local headers detected in " << mainSourceAbsPath << "\n";
outs() << "\n" << sHipify << "No local headers detected in "
<< sys::path::filename(mainSourceAbsPath) << "\n";
return true;
}

std::vector<std::string> work(initial.begin(), initial.end());
outs() << "\n" << sHipify << "Local headers found: " << initial.size()
<< " in " << sys::path::filename(mainSourceAbsPath) << "\n";
for (size_t i = 0; i < initial.size(); ++i) {
outs() << (i + 1) << "/" << initial.size()
<< ": " << sys::path::filename(initial[i]) << "\n";
}

std::vector<std::pair<std::string, std::vector<std::string>>> work;
for (const auto &h : initial) {
work.push_back({h, std::vector<std::string>{mainSourceAbsPath}});
}
std::set<std::string> processed;
std::set<std::string> queued;
size_t total = initial.size();
size_t current = 0;

for (const auto &h: initial) {
queued.insert(h);
}

while (!work.empty()) {
std::string hdr = work.back();
std::string hdr = work.back().first;
std::vector<std::string> ancestorChain = work.back().second;
work.pop_back();
std::string parentPath = ancestorChain[0];
if (processed.count(hdr)) {
errs() << sHipify << sWarning << "Duplicate local header reference ignored: " << hdr << "\n";
outs() << sHipify << sWarning
<< "Duplicate local header reference ignored: "
<< sys::path::filename(hdr) << "\n";
continue;
}
processed.insert(hdr);
++current;

std::string original;
if (!readFile(hdr, original)) {
errs() << sHipify << sError << "Cannot read header: " << hdr << "\n";
errs() << "\n" << sHipify << sError
<< "Cannot read header: " << sys::path::filename(hdr) << "\n";
continue;
}

std::string hipOut = hdr + ".hip";
bool ok = hipifySingleSource(hdr, hipOut, compDB, OptionsParserPtr,
hipify_exe, mainSourceAbsPath, false);
std::vector<std::string> precedingIncludes;
collectPrecedingIncludes(parentPath, hdr, precedingIncludes);
collectAncestorSystemIncludes(ancestorChain, precedingIncludes);
outs() << "\n" << sHipify << "Hipifying local header [" << current
<< "/" << total << "]: " << sys::path::filename(hdr) << "\n";

bool ok = hipifySingleSource(
hdr, hipOut, compDB, OptionsParserPtr, hipify_exe,
resolveCompileContext(parentPath, mainSourceAbsPath, compDB), false,
precedingIncludes);

if (!ok) {
errs() << sHipify << sError << "Hipify failed for header: " << hdr << "\n";
errs() << "\n" << sHipify << sError
<< "Hipify failed for header [" << current << "/" << total
<< "]: " << sys::path::filename(hdr) << "\n";
return false;
}
outs() << sHipify << "Successfully hipified header file" << "\n";

if (recursive) {
std::smatch m;
std::istringstream iss(original);
std::string line;
std::vector<std::string> newHeaders;
while (std::getline(iss, line)) {
if (std::regex_match(line, m, LocalIncludeRe)) {
if (std::regex_search(line, m, LocalIncludeRe)) {
std::string rel = m[1].str();
std::string abs;
if (resolveLocalIncludeInternal(hdr, rel, abs) &&
!processed.count(abs))
work.push_back(abs);
!processed.count(abs) && !queued.count(abs)) {
queued.insert(abs);
std::vector<std::string> childChain;
childChain.push_back(hdr);
childChain.insert(childChain.end(), ancestorChain.begin(), ancestorChain.end());
work.push_back({abs, childChain});
newHeaders.push_back(abs);
}
}
}
if (!newHeaders.empty()) {
total += newHeaders.size();
outs() << sHipify << " Recursive: found " << newHeaders.size()
<< " additional local header(s) in "
<< sys::path::filename(hdr) << "\n";
}
}
}

outs() << "\n" << sHipify << "Local header hipification complete: "
<< processed.size() << " header(s) processed.\n";
return true;
}
25 changes: 24 additions & 1 deletion src/LocalHeader.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/*
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 <string>
Expand All @@ -12,7 +34,8 @@ extern 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 = {});

bool hipifyLocalHeaders(const std::string &srcPath,
const ct::CompilationDatabase *compDB,
Expand Down
24 changes: 18 additions & 6 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,15 @@ bool hipifySingleSource(const std::string &srcPath,
return false;
}

for (auto it = additionalIncludes.rbegin(); it != additionalIncludes.rend(); ++it) {
Tool.appendArgumentsAdjuster(
ct::getInsertArgumentAdjuster(it->c_str(),
ct::ArgumentInsertPosition::BEGIN));
Tool.appendArgumentsAdjuster(
ct::getInsertArgumentAdjuster("-include",
ct::ArgumentInsertPosition::BEGIN));
}

// Hipify _all_ the things!
if (Tool.runAndSave(&actionFactory)) {
llvm::errs() << "\n" << sHipify << sError
Expand Down Expand Up @@ -516,15 +527,16 @@ 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 (!SkipLocalHeaders) {
if (!hipifyLocalHeaders(sSourceAbsPath,
compilationDatabase.get(),
&OptionsParser,
argv[0],
OptLocalHeadersRecursive)) {
true)) {
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;
}
}
Expand All @@ -535,7 +547,7 @@ int main(int argc, const char **argv) {
&OptionsParser,
argv[0],
sSourceAbsPath,
false)) {
false, {})) {
Statistics::current().hasErrors = true;
Result = 1;
LLVM_DEBUG(llvm::dbgs() << "Hipification failed for: " << src << "\n");
Expand Down
2 changes: 2 additions & 0 deletions tests/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ 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')

delimiter = "===============================================================";
print(delimiter)
Expand Down
Loading