From 7e7cfa10cc0be22ed34110d48b70057515ef6212 Mon Sep 17 00:00:00 2001 From: Evgeny Mankov Date: Tue, 26 Dec 2017 20:54:54 +0300 Subject: [PATCH 1/2] [HIPIFY][FIX][#306] Eliminate second cuda main include directive // hipified to #include #include // 1st cuda main include (Driver API) // to eliminate #include // 2nd cuda main include (Runtime API) HIP has one header hip_runtime.h for both CUDA APIs, thus second cuda main include directive is eliminated entirely. --- hipify-clang/src/HipifyAction.cpp | 22 ++++++++++++++-------- tests/hipify-clang/headers_test_01.cu | 6 ++++++ tests/hipify-clang/headers_test_02.cu | 6 ++++++ 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 tests/hipify-clang/headers_test_01.cu create mode 100644 tests/hipify-clang/headers_test_02.cu diff --git a/hipify-clang/src/HipifyAction.cpp b/hipify-clang/src/HipifyAction.cpp index ee23387e1f..87329680b3 100644 --- a/hipify-clang/src/HipifyAction.cpp +++ b/hipify-clang/src/HipifyAction.cpp @@ -157,11 +157,11 @@ void HipifyAction::InclusionDirective(clang::SourceLocation hash_loc, } // Special-casing to avoid duplication of the hip_runtime include. + bool secondMainInclude = false; if (found->second.hipName == "hip/hip_runtime.h") { if (insertedRuntimeHeader) { - return; + secondMainInclude = true; } - insertedRuntimeHeader = true; } @@ -169,22 +169,28 @@ void HipifyAction::InclusionDirective(clang::SourceLocation hash_loc, clang::SourceLocation sl = filename_range.getBegin(); if (found->second.unsupported) { - // An unsupported CUDA header? Oh dear. Print a warning. clang::DiagnosticsEngine& DE = getCompilerInstance().getDiagnostics(); DE.Report(sl, DE.getCustomDiagID(clang::DiagnosticsEngine::Warning, "Unsupported CUDA header")); return; } - const char *B = SM.getCharacterData(sl); + char *B = nullptr; const char *E = SM.getCharacterData(filename_range.getEnd()); - clang::SmallString<128> includeBuffer; clang::StringRef newInclude; // Keep the same include type that the user gave. - if (is_angled) { - newInclude = llvm::Twine("<" + found->second.hipName + ">").toStringRef(includeBuffer); + if (!secondMainInclude) { + B = const_cast(SM.getCharacterData(sl)); + clang::SmallString<128> includeBuffer; + if (is_angled) { + newInclude = llvm::Twine("<" + found->second.hipName + ">").toStringRef(includeBuffer); + } else { + newInclude = llvm::Twine("\"" + found->second.hipName + "\"").toStringRef(includeBuffer); + } } else { - newInclude = llvm::Twine("\"" + found->second.hipName + "\"").toStringRef(includeBuffer); + // hashLoc is location of the '#', thus replacing the whole include directive by empty newInclude starting with '#'. + B = const_cast(SM.getCharacterData(hash_loc)); + sl = hash_loc; } ct::Replacement Rep(SM, sl, E - B, newInclude); diff --git a/tests/hipify-clang/headers_test_01.cu b/tests/hipify-clang/headers_test_01.cu new file mode 100644 index 0000000000..c39ef80d8f --- /dev/null +++ b/tests/hipify-clang/headers_test_01.cu @@ -0,0 +1,6 @@ +// RUN: %run_test hipify "%s" "%t" %cuda_args + +// CHECK: #include +#include +// CHECK-NOT: #include +#include diff --git a/tests/hipify-clang/headers_test_02.cu b/tests/hipify-clang/headers_test_02.cu new file mode 100644 index 0000000000..90d412f797 --- /dev/null +++ b/tests/hipify-clang/headers_test_02.cu @@ -0,0 +1,6 @@ +// RUN: %run_test hipify "%s" "%t" %cuda_args + +// CHECK: #include +#include +// CHECK-NOT: #include +#include From 08662fba7307fb4b00fb3b83e7d3021b1841c8ff Mon Sep 17 00:00:00 2001 From: Evgeny Mankov Date: Wed, 10 Jan 2018 21:26:05 +0300 Subject: [PATCH 2/2] [HIPIFY][fix][#306] Code improve --- hipify-clang/src/HipifyAction.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/hipify-clang/src/HipifyAction.cpp b/hipify-clang/src/HipifyAction.cpp index 87329680b3..8fb318776d 100644 --- a/hipify-clang/src/HipifyAction.cpp +++ b/hipify-clang/src/HipifyAction.cpp @@ -174,13 +174,10 @@ void HipifyAction::InclusionDirective(clang::SourceLocation hash_loc, return; } - char *B = nullptr; - const char *E = SM.getCharacterData(filename_range.getEnd()); clang::StringRef newInclude; // Keep the same include type that the user gave. if (!secondMainInclude) { - B = const_cast(SM.getCharacterData(sl)); clang::SmallString<128> includeBuffer; if (is_angled) { newInclude = llvm::Twine("<" + found->second.hipName + ">").toStringRef(includeBuffer); @@ -189,10 +186,10 @@ void HipifyAction::InclusionDirective(clang::SourceLocation hash_loc, } } else { // hashLoc is location of the '#', thus replacing the whole include directive by empty newInclude starting with '#'. - B = const_cast(SM.getCharacterData(hash_loc)); sl = hash_loc; } - + const char *B = SM.getCharacterData(sl); + const char *E = SM.getCharacterData(filename_range.getEnd()); ct::Replacement Rep(SM, sl, E - B, newInclude); insertReplacement(Rep, clang::FullSourceLoc{sl, SM}); }