From aba73d667370081f8ea30b380ac25ebffb164471 Mon Sep 17 00:00:00 2001 From: Evgeny Mankov Date: Thu, 11 Aug 2016 22:29:55 +0300 Subject: [PATCH] clang-hipify: Add support for nested macro expansion and translation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes bug “HIPIFY: nested macro is not hipified” https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/issues/33 Example: #include "cuda_runtime.h" #define MY_MACRO(func, flags) (func, flags) ... cudaEvent_t *event = NULL; MY_MACRO(cudaEventCreateWithFlags(event, cudaEventDisableTiming), NULL); where cudaEventDisableTiming is a defined numeric literal and thus a nested MACRO: #define cudaEventDisableTiming 0x02 /**< Event will not record timing data */ After hipifying now: MY_MACRO(hipEventCreateWithFlags(event, cudaEventDisableTiming), NULL); Should be: MY_MACRO(hipEventCreateWithFlags(event, hipEventDisableTiming), NULL); --- hipamd/clang-hipify/src/Cuda2Hip.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/hipamd/clang-hipify/src/Cuda2Hip.cpp b/hipamd/clang-hipify/src/Cuda2Hip.cpp index c7b666b3f4..863dd731b9 100644 --- a/hipamd/clang-hipify/src/Cuda2Hip.cpp +++ b/hipamd/clang-hipify/src/Cuda2Hip.cpp @@ -1151,11 +1151,29 @@ struct HipifyPPCallbacks : public PPCallbacks, public SourceFileCallbacks { Replacement Rep(*_sm, sl, name.size(), repName); Replace->insert(Rep); } - } - if (tok.is(tok::string_literal)) { - StringRef s(tok.getLiteralData(), tok.getLength()); - processString(unquoteStr(s), N, Replace, *_sm, tok.getLocation(), - countReps); + } else if (tok.isLiteral()) { + SourceLocation sl = tok.getLocation(); + if (_sm->isMacroBodyExpansion(sl)) { + LangOptions DefaultLangOptions; + SourceLocation sl_macro = _sm->getExpansionLoc(sl); + SourceLocation sl_end = Lexer::getLocForEndOfToken(sl_macro, 0, *_sm, DefaultLangOptions); + size_t length = _sm->getCharacterData(sl_end) - _sm->getCharacterData(sl_macro); + StringRef name = StringRef(_sm->getCharacterData(sl_macro), length); + const auto found = N.cuda2hipRename.find(name); + if (found != N.cuda2hipRename.end()) { + sl = sl_macro; + countReps[found->second.countType]++; + StringRef repName = found->second.hipName; + Replacement Rep(*_sm, sl, length, repName); + Replace->insert(Rep); + } + } else { + if (tok.is(tok::string_literal)) { + StringRef s(tok.getLiteralData(), tok.getLength()); + processString(unquoteStr(s), N, Replace, *_sm, tok.getLocation(), + countReps); + } + } } } }