Index: lib/CodeGen/CodeGenModule.cpp =================================================================== --- lib/CodeGen/CodeGenModule.cpp +++ lib/CodeGen/CodeGenModule.cpp @@ -1563,8 +1563,14 @@ if (!CodeGenOpts.SanitizeCfiCrossDso) CreateFunctionTypeMetadataForIcall(FD, F); - if (getLangOpts().OpenMP && FD->hasAttr()) - getOpenMPRuntime().emitDeclareSimdFunction(FD, F); + if (getLangOpts().OpenMP) { + if (FD->hasAttr()) + getOpenMPRuntime().emitDeclareSimdFunction(FD, F); + + if (!getLangOpts().OpenMPIsDevice && FD->hasAttr()) { + F->addFnAttr("omp-declare-target"); + } + } } void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) { Index: lib/Sema/CMakeLists.txt =================================================================== --- lib/Sema/CMakeLists.txt +++ lib/Sema/CMakeLists.txt @@ -1,4 +1,5 @@ set(LLVM_LINK_COMPONENTS + Core Support ) Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp +++ lib/Sema/SemaDecl.cpp @@ -44,6 +44,8 @@ #include "clang/Sema/Template.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/Triple.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/Module.h" #include #include #include @@ -5530,7 +5532,19 @@ CurContext->addHiddenDecl(New); } - if (isInOpenMPDeclareTargetContext()) + if (getLangOpts().OpenMPIsDevice) { + if (auto *OpenMPHostIR = getASTContext().getOpenMPHostIR()) { + if (auto *FD = dyn_cast(New)) { + // FIXME: This should be mangled. + StringRef Name = FD->getNameAsString(); + auto *HostFunc = OpenMPHostIR->getFunction(Name); + if (HostFunc && HostFunc->hasFnAttribute("omp-declare-target")) { + checkDeclIsAllowedInOpenMPTarget(nullptr, New); + } + } + // FIXME: Handle other declarators. + } + } else if (isInOpenMPDeclareTargetContext()) checkDeclIsAllowedInOpenMPTarget(nullptr, New); return New;