diff --git a/clang/lib/DPCT/ASTTraversal.cpp b/clang/lib/DPCT/ASTTraversal.cpp index e45367eb5393..9dd1d6d2c0ff 100644 --- a/clang/lib/DPCT/ASTTraversal.cpp +++ b/clang/lib/DPCT/ASTTraversal.cpp @@ -169,6 +169,16 @@ void IncludesCallbacks::insertCudaArchRepl( return; } +std::shared_ptr +generateReplacement(SourceLocation SL, MacroMigrationRule Rule) { + requestFeature(Rule.HelperFeature); + for (auto ItHeader = Rule.Includes.begin(); ItHeader != Rule.Includes.end(); + ItHeader++) { + DpctGlobalInfo::getInstance().insertHeader(SL, *ItHeader); + } + return std::make_shared(SL, std::move(Rule.Out)); +} + bool IncludesCallbacks::ReplaceCuMacro(const Token &MacroNameTok, MacroInfo *MI) { bool IsInAnalysisScope = isInAnalysisScope(MacroNameTok.getLocation()); @@ -181,12 +191,9 @@ bool IncludesCallbacks::ReplaceCuMacro(const Token &MacroNameTok, std::string MacroName = MacroNameTok.getIdentifierInfo()->getName().str(); auto Iter = MapNames::MacroRuleMap.find(MacroName); if (Iter != MapNames::MacroRuleMap.end()) { - std::string ReplacedMacroName = Iter->second.Out; - auto Repl = std::make_shared(MacroNameTok.getLocation(), - std::move(ReplacedMacroName)); + auto Repl = generateReplacement(MacroNameTok.getLocation(), Iter->second); if (MacroName == "__CUDA_ARCH__") { if (DpctGlobalInfo::getInstance().getContext().getLangOpts().CUDA) { - requestFeature(HelperFeatureEnum::device_ext); insertCudaArchRepl(Repl->getReplacement(DpctGlobalInfo::getContext())); return true; } @@ -198,9 +205,9 @@ bool IncludesCallbacks::ReplaceCuMacro(const Token &MacroNameTok, } else { return false; } - } - if (MacroName == "CUDART_VERSION" || MacroName == "__CUDART_API_VERSION" || - MacroName == "CUDA_VERSION") { + } else if (MacroName == "CUDART_VERSION" || + MacroName == "__CUDART_API_VERSION" || + MacroName == "CUDA_VERSION") { // These two macros are defined by CUDA header file auto LocInfo = DpctGlobalInfo::getLocInfo(MacroNameTok.getLocation()); auto Ver = clang::getCudaVersionPair(DpctGlobalInfo::getSDKVersion()); @@ -208,8 +215,7 @@ bool IncludesCallbacks::ReplaceCuMacro(const Token &MacroNameTok, .insertFile(LocInfo.first) ->setRTVersionValue( std::to_string(Ver.first * 1000 + Ver.second * 10)); - } - if (MacroName == "NCCL_VERSION_CODE" && MI) { + } else if (MacroName == "NCCL_VERSION_CODE" && MI) { auto LocInfo = DpctGlobalInfo::getLocInfo(MacroNameTok.getLocation()); DpctGlobalInfo::getInstance() .insertFile(LocInfo.first) @@ -240,12 +246,6 @@ bool IncludesCallbacks::ReplaceCuMacro(const Token &MacroNameTok, } } TransformSet.emplace_back(Repl); - requestFeature(Iter->second.HelperFeature); - for (auto ItHeader = Iter->second.Includes.begin(); - ItHeader != Iter->second.Includes.end(); ItHeader++) { - DpctGlobalInfo::getInstance().insertHeader(MacroNameTok.getLocation(), - *ItHeader); - } return true; } return false; @@ -280,15 +280,8 @@ void IncludesCallbacks::MacroDefined(const Token &MacroNameTok, auto ItRule = MapNames::MacroRuleMap.find(II->getName().str()); if (ItRule != MapNames::MacroRuleMap.end()) { - std::string ReplacedMacroName = ItRule->second.Out; TransformSet.emplace_back( - new ReplaceToken(Iter->getLocation(), std::move(ReplacedMacroName))); - requestFeature(ItRule->second.HelperFeature); - for (auto ItHeader = ItRule->second.Includes.begin(); - ItHeader != ItRule->second.Includes.end(); ItHeader++) { - DpctGlobalInfo::getInstance().insertHeader(Iter->getLocation(), - *ItHeader); - } + generateReplacement(Iter->getLocation(), ItRule->second)); } if (II->hasMacroDefinition() && (II->getName().str() == "__host__" || @@ -754,7 +747,6 @@ void IncludesCallbacks::ReplaceCuMacro(SourceRange ConditionRange, IfType IT, for (auto &MacroRule : MapNames::MacroRuleMap) { size_t Pos = 0; std::string MacroName = MacroRule.first; - std::string ReplacedMacroName = MacroRule.second.Out; std::size_t Found = E.find(MacroName, Pos); if (Found != std::string::npos && MacroName == "__CUDA_ARCH__") { @@ -826,8 +818,7 @@ void IncludesCallbacks::ReplaceCuMacro(SourceRange ConditionRange, IfType IT, SourceLocation IE = IB.getLocWithOffset(MacroName.length()); CharSourceRange InsertRange(SourceRange(IB, IE), false); - auto Repl = - std::make_shared(InsertRange, ReplacedMacroName); + auto Repl = generateReplacement(IB, MacroRule.second); if (MacroName == "__CUDA_ARCH__" && DpctGlobalInfo::getInstance().getContext().getLangOpts().CUDA) { insertCudaArchRepl(Repl->getReplacement(DpctGlobalInfo::getContext())); @@ -836,12 +827,6 @@ void IncludesCallbacks::ReplaceCuMacro(SourceRange ConditionRange, IfType IT, MacroName != "__CUDA_ARCH__") { TransformSet.emplace_back(Repl); } - requestFeature(MacroRule.second.HelperFeature); - for (auto ItHeader = MacroRule.second.Includes.begin(); - ItHeader != MacroRule.second.Includes.end(); ItHeader++) { - DpctGlobalInfo::getInstance().insertHeader(InsertRange.getBegin(), - *ItHeader); - } // check next Pos = Found + MacroName.length(); if ((Pos + MacroName.length()) > Size) { diff --git a/clang/lib/DPCT/MapNames.cpp b/clang/lib/DPCT/MapNames.cpp index 60866fe695a7..04d4e08e0795 100644 --- a/clang/lib/DPCT/MapNames.cpp +++ b/clang/lib/DPCT/MapNames.cpp @@ -3903,84 +3903,89 @@ std::unordered_map MapNames::MacroRuleMap{ "__noinline__", "__dpct_noinline__", HelperFeatureEnum::device_ext)}, {"cudaMemAttachGlobal", - MacroMigrationRule("flag_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "cudaMemAttachGlobal", "0")}, {"cudaStreamDefault", - MacroMigrationRule("cudaStreamDefault_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "cudaStreamDefault", "0")}, {"CU_LAUNCH_PARAM_BUFFER_SIZE", - MacroMigrationRule("kernel_param_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "CU_LAUNCH_PARAM_BUFFER_SIZE", "((void *) 2)", HelperFeatureEnum::device_ext)}, {"CU_LAUNCH_PARAM_BUFFER_POINTER", - MacroMigrationRule("kernel_param_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "CU_LAUNCH_PARAM_BUFFER_POINTER", "((void *) 1)", HelperFeatureEnum::device_ext)}, {"CU_LAUNCH_PARAM_END", - MacroMigrationRule("kernel_param_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "CU_LAUNCH_PARAM_END", "((void *) 0)", HelperFeatureEnum::device_ext)}, - {"CUDART_PI_F", MacroMigrationRule("CUDART_PI_F", RulePriority::Fallback, - "CUDART_PI_F", "3.141592654F")}, - {"CUB_MAX", MacroMigrationRule("cub_macro_rule", RulePriority::Fallback, - "CUB_MAX", "std::max")}, - {"CUB_MIN", MacroMigrationRule("cub_macro_rule", RulePriority::Fallback, - "CUB_MIN", "std::min")}, + {"CUDART_PI_F", + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, + "CUDART_PI_F", "3.141592654F")}, + {"CUB_MAX", + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, + "CUB_MAX", "std::max")}, + {"CUB_MIN", + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, + "CUB_MIN", "std::min")}, {"CUB_RUNTIME_FUNCTION", - MacroMigrationRule("cub_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "CUB_RUNTIME_FUNCTION", "")}, {"cudaStreamAttrValue", - MacroMigrationRule("cudaStreamAttrValue_macro_rule", - RulePriority::Fallback, "cudaStreamAttrValue", "int")}, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, + "cudaStreamAttrValue", "int")}, {"NCCL_VERSION_CODE", - MacroMigrationRule("nccl_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "NCCL_VERSION_CODE", "DPCT_COMPAT_CCL_VERSION")}, {"__CUDA_ARCH__", - MacroMigrationRule("cuda_arch_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "__CUDA_ARCH__", "DPCT_COMPATIBILITY_TEMP", clang::dpct::HelperFeatureEnum::device_ext)}, - {"__NVCC__", MacroMigrationRule("nvcc_macro_rule", RulePriority::Fallback, - "__NVCC__", "SYCL_LANGUAGE_VERSION")}, + {"__NVCC__", + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, + "__NVCC__", "SYCL_LANGUAGE_VERSION")}, {"__CUDACC__", - MacroMigrationRule("cudacc_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "__CUDACC__", "SYCL_LANGUAGE_VERSION")}, {"__DRIVER_TYPES_H__", - MacroMigrationRule("driver_types_h_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "__DRIVER_TYPES_H__", "__DPCT_HPP__")}, {"__CUDA_RUNTIME_H__", - MacroMigrationRule("cuda_runtime_h_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "__CUDA_RUNTIME_H__", "__DPCT_HPP__")}, {"CUDART_VERSION", - MacroMigrationRule("cudart_version_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "CUDART_VERSION", "DPCT_COMPAT_RT_VERSION")}, {"__CUDART_API_VERSION", - MacroMigrationRule("cudart_api_version_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "__CUDART_API_VERSION", "DPCT_COMPAT_RT_VERSION")}, {"CUDA_VERSION", - MacroMigrationRule("cuda_version_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "CUDA_VERSION", "DPCT_COMPAT_RT_VERSION")}, {"__CUDACC_VER_MAJOR__", - MacroMigrationRule("cudacc_ver_major_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "__CUDACC_VER_MAJOR__", "DPCT_COMPAT_RT_MAJOR_VERSION")}, {"__CUDACC_VER_MINOR__", - MacroMigrationRule("cudacc_ver_minor_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "__CUDACC_VER_MINOR__", "DPCT_COMPAT_RT_MINOR_VERSION")}, {"CUBLAS_V2_H_", - MacroMigrationRule("cublas_v2_h_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "CUBLAS_V2_H_", "MKL_SYCL_HPP")}, - {"__CUDA__", MacroMigrationRule("cuda_macro_rule", RulePriority::Fallback, - "__CUDA__", "SYCL_LANGUAGE_VERSION")}, + {"__CUDA__", + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, + "__CUDA__", "SYCL_LANGUAGE_VERSION")}, {"CUFFT_FORWARD", - MacroMigrationRule("cufft_forward_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "CUFFT_FORWARD", "-1")}, {"CUFFT_INVERSE", - MacroMigrationRule("cufft_forward_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "CUFFT_INVERSE", "1")}, {"cudaEventDefault", - MacroMigrationRule("cufft_forward_macro_rule", RulePriority::Fallback, + MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback, "cudaEventDefault", "0")}, //... };