skatrak created this revision.
skatrak added reviewers: dpalermo, jsjodin, domada, agozillon.
Herald added subscribers: sunshaoce, guansong, hiraditya, yaxunl.
Herald added a project: All.
skatrak requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: llvm-commits, cfe-commits, jplehr, sstefan1.
Herald added projects: clang, LLVM.
This patch updates the `OpenMPIRBuilderConfig` structure to hold all
available 'requires' clauses, and it moves part of the code
generation for the 'requires' registration function from clang to the
`OMPIRBuilder`, to be shared with flang.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D147217
Files:
clang/lib/CodeGen/CGOpenMPRuntime.cpp
clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===================================================================
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -5731,7 +5731,8 @@
TEST_F(OpenMPIRBuilderTest, OffloadEntriesInfoManager) {
OpenMPIRBuilder OMPBuilder(*M);
- OMPBuilder.setConfig(OpenMPIRBuilderConfig(true, false, false, false));
+ OMPBuilder.setConfig(
+ OpenMPIRBuilderConfig(true, false, false, false, false, false, false));
OffloadEntriesInfoManager &InfoManager = OMPBuilder.OffloadInfoManager;
TargetRegionEntryInfo EntryInfo("parent", 1, 2, 4, 0);
InfoManager.initializeTargetRegionEntryInfo(EntryInfo, 0);
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===================================================================
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -21,10 +21,12 @@
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/MDBuilder.h"
@@ -328,6 +330,104 @@
return splitBB(Builder, CreateBranch, Old->getName() + Suffix);
}
+//===----------------------------------------------------------------------===//
+// OpenMPIRBuilderConfig
+//===----------------------------------------------------------------------===//
+
+namespace {
+LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
+/// Values for bit flags for marking which requires clauses have been used.
+enum OpenMPOffloadingRequiresDirFlags {
+ /// flag undefined.
+ OMP_REQ_UNDEFINED = 0x000,
+ /// no requires directive present.
+ OMP_REQ_NONE = 0x001,
+ /// reverse_offload clause.
+ OMP_REQ_REVERSE_OFFLOAD = 0x002,
+ /// unified_address clause.
+ OMP_REQ_UNIFIED_ADDRESS = 0x004,
+ /// unified_shared_memory clause.
+ OMP_REQ_UNIFIED_SHARED_MEMORY = 0x008,
+ /// dynamic_allocators clause.
+ OMP_REQ_DYNAMIC_ALLOCATORS = 0x010,
+ LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/OMP_REQ_DYNAMIC_ALLOCATORS)
+};
+
+} // anonymous namespace
+
+OpenMPIRBuilderConfig::OpenMPIRBuilderConfig()
+ : RequiresFlags(OMP_REQ_UNDEFINED) {}
+
+OpenMPIRBuilderConfig::OpenMPIRBuilderConfig(
+ bool IsEmbedded, bool IsTargetCodegen, bool OpenMPOffloadMandatory,
+ bool HasRequiresReverseOffload, bool HasRequiresUnifiedAddress,
+ bool HasRequiresUnifiedSharedMemory, bool HasRequiresDynamicAllocators)
+ : RequiresFlags(OMP_REQ_UNDEFINED), IsEmbedded(IsEmbedded),
+ IsTargetCodegen(IsTargetCodegen),
+ OpenMPOffloadMandatory(OpenMPOffloadMandatory) {
+ if (HasRequiresReverseOffload)
+ RequiresFlags |= OMP_REQ_REVERSE_OFFLOAD;
+ if (HasRequiresUnifiedAddress)
+ RequiresFlags |= OMP_REQ_UNIFIED_ADDRESS;
+ if (HasRequiresUnifiedSharedMemory)
+ RequiresFlags |= OMP_REQ_UNIFIED_SHARED_MEMORY;
+ if (HasRequiresDynamicAllocators)
+ RequiresFlags |= OMP_REQ_DYNAMIC_ALLOCATORS;
+}
+
+bool OpenMPIRBuilderConfig::hasRequiresReverseOffload() const {
+ return RequiresFlags & OMP_REQ_REVERSE_OFFLOAD;
+}
+
+bool OpenMPIRBuilderConfig::hasRequiresUnifiedAddress() const {
+ return RequiresFlags & OMP_REQ_UNIFIED_ADDRESS;
+}
+
+bool OpenMPIRBuilderConfig::hasRequiresUnifiedSharedMemory() const {
+ return RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY;
+}
+
+bool OpenMPIRBuilderConfig::hasRequiresDynamicAllocators() const {
+ return RequiresFlags & OMP_REQ_DYNAMIC_ALLOCATORS;
+}
+
+int64_t OpenMPIRBuilderConfig::getRequiresFlags() const {
+ return hasRequiresFlags() ? RequiresFlags
+ : static_cast<int64_t>(OMP_REQ_NONE);
+}
+
+void OpenMPIRBuilderConfig::setHasRequiresReverseOffload(bool Value) {
+ if (Value)
+ RequiresFlags |= OMP_REQ_REVERSE_OFFLOAD;
+ else
+ RequiresFlags &= ~OMP_REQ_REVERSE_OFFLOAD;
+}
+
+void OpenMPIRBuilderConfig::setHasRequiresUnifiedAddress(bool Value) {
+ if (Value)
+ RequiresFlags |= OMP_REQ_UNIFIED_ADDRESS;
+ else
+ RequiresFlags &= ~OMP_REQ_UNIFIED_ADDRESS;
+}
+
+void OpenMPIRBuilderConfig::setHasRequiresUnifiedSharedMemory(bool Value) {
+ if (Value)
+ RequiresFlags |= OMP_REQ_UNIFIED_SHARED_MEMORY;
+ else
+ RequiresFlags &= ~OMP_REQ_UNIFIED_SHARED_MEMORY;
+}
+
+void OpenMPIRBuilderConfig::setHasRequiresDynamicAllocators(bool Value) {
+ if (Value)
+ RequiresFlags |= OMP_REQ_DYNAMIC_ALLOCATORS;
+ else
+ RequiresFlags &= ~OMP_REQ_DYNAMIC_ALLOCATORS;
+}
+
+//===----------------------------------------------------------------------===//
+// OpenMPIRBuilder
+//===----------------------------------------------------------------------===//
+
void OpenMPIRBuilder::addAttributes(omp::RuntimeFunction FnID, Function &Fn) {
LLVMContext &Ctx = Fn.getContext();
Triple T(M.getTargetTriple());
@@ -5104,6 +5204,35 @@
}
}
+Function *OpenMPIRBuilder::createRegisterRequires(StringRef Name) {
+ Builder.ClearInsertionPoint();
+
+ // Create registration function prototype
+ auto *RegFnTy = FunctionType::get(Builder.getVoidTy(), {});
+ auto *RegFn = Function::Create(
+ RegFnTy, GlobalVariable::LinkageTypes::InternalLinkage, Name, M);
+ RegFn->setSection(".text.startup");
+ RegFn->addFnAttr(Attribute::NoInline);
+ RegFn->addFnAttr(Attribute::NoUnwind);
+
+ // Create registration function body
+ auto *BB = BasicBlock::Create(M.getContext(), "entry", RegFn);
+ ConstantInt *FlagsVal =
+ ConstantInt::getSigned(Builder.getInt64Ty(), Config.getRequiresFlags());
+ Function *RTLRegFn = getOrCreateRuntimeFunctionPtr(
+ omp::RuntimeFunction::OMPRTL___tgt_register_requires);
+
+ Builder.SetInsertPoint(BB);
+ Builder.CreateCall(RTLRegFn, {FlagsVal});
+ Builder.CreateRetVoid();
+
+ return RegFn;
+}
+
+//===----------------------------------------------------------------------===//
+// OffloadEntriesInfoManager
+//===----------------------------------------------------------------------===//
+
bool OffloadEntriesInfoManager::empty() const {
return OffloadEntriesTargetRegion.empty() &&
OffloadEntriesDeviceGlobalVar.empty();
@@ -5237,6 +5366,10 @@
Action(E.getKey(), E.getValue());
}
+//===----------------------------------------------------------------------===//
+// CanonicalLoopInfo
+//===----------------------------------------------------------------------===//
+
void CanonicalLoopInfo::collectControlBlocks(
SmallVectorImpl<BasicBlock *> &BBs) {
// We only count those BBs as control block for which we do not need to
Index: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
===================================================================
--- llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -81,6 +81,10 @@
/// is not necessary at all, because because the only functions that are called
/// are ones that are not dependent on the configuration.
class OpenMPIRBuilderConfig {
+private:
+ /// Flags for specifying which requires directive clauses are present.
+ int64_t RequiresFlags;
+
public:
/// Flag for specifying if the compilation is done for embedded device code
/// or host code.
@@ -90,10 +94,6 @@
/// like GPU.
std::optional<bool> IsTargetCodegen;
- /// Flag for specifying weather a requires unified_shared_memory
- /// directive is present or not.
- std::optional<bool> HasRequiresUnifiedSharedMemory;
-
// Flag for specifying if offloading is mandatory.
std::optional<bool> OpenMPOffloadMandatory;
@@ -102,13 +102,13 @@
/// Separator used between all of the rest consecutive parts of s name
std::optional<StringRef> Separator;
- OpenMPIRBuilderConfig() {}
+ OpenMPIRBuilderConfig();
OpenMPIRBuilderConfig(bool IsEmbedded, bool IsTargetCodegen,
+ bool OpenMPOffloadMandatory,
+ bool HasRequiresReverseOffload,
+ bool HasRequiresUnifiedAddress,
bool HasRequiresUnifiedSharedMemory,
- bool OpenMPOffloadMandatory)
- : IsEmbedded(IsEmbedded), IsTargetCodegen(IsTargetCodegen),
- HasRequiresUnifiedSharedMemory(HasRequiresUnifiedSharedMemory),
- OpenMPOffloadMandatory(OpenMPOffloadMandatory) {}
+ bool HasRequiresDynamicAllocators);
// Getters functions that assert if the required values are not present.
bool isEmbedded() const {
@@ -121,17 +121,22 @@
return *IsTargetCodegen;
}
- bool hasRequiresUnifiedSharedMemory() const {
- assert(HasRequiresUnifiedSharedMemory.has_value() &&
- "HasUnifiedSharedMemory is not set");
- return *HasRequiresUnifiedSharedMemory;
- }
-
bool openMPOffloadMandatory() const {
assert(OpenMPOffloadMandatory.has_value() &&
"OpenMPOffloadMandatory is not set");
return *OpenMPOffloadMandatory;
}
+
+ bool hasRequiresFlags() const { return RequiresFlags; }
+ bool hasRequiresReverseOffload() const;
+ bool hasRequiresUnifiedAddress() const;
+ bool hasRequiresUnifiedSharedMemory() const;
+ bool hasRequiresDynamicAllocators() const;
+
+ /// Returns requires directive clauses as flags compatible with those expected
+ /// by libomptarget.
+ int64_t getRequiresFlags() const;
+
// Returns the FirstSeparator if set, otherwise use the default
// separator depending on isTargetCodegen
StringRef firstSeparator() const {
@@ -154,11 +159,13 @@
void setIsEmbedded(bool Value) { IsEmbedded = Value; }
void setIsTargetCodegen(bool Value) { IsTargetCodegen = Value; }
- void setHasRequiresUnifiedSharedMemory(bool Value) {
- HasRequiresUnifiedSharedMemory = Value;
- }
void setFirstSeparator(StringRef FS) { FirstSeparator = FS; }
void setSeparator(StringRef S) { Separator = S; }
+
+ void setHasRequiresReverseOffload(bool Value);
+ void setHasRequiresUnifiedAddress(bool Value);
+ void setHasRequiresUnifiedSharedMemory(bool Value);
+ void setHasRequiresDynamicAllocators(bool Value);
};
/// Data structure to contain the information needed to uniquely identify
@@ -2175,6 +2182,16 @@
/// \param Name Name of the variable.
GlobalVariable *getOrCreateInternalVariable(Type *Ty, const StringRef &Name,
unsigned AddressSpace = 0);
+
+ /// Create a global function to register OpenMP requires flags into the
+ /// runtime, according to the `Config`.
+ ///
+ /// This function should be added to the list of constructors of the
+ /// compilation unit in order to be called before other OpenMP runtime
+ /// functions.
+ ///
+ /// \param Name Name of the created function.
+ Function *createRegisterRequires(StringRef Name);
};
/// Class to represented the control flow structure of an OpenMP canonical loop.
Index: clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
===================================================================
--- clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
+++ clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
@@ -859,9 +859,11 @@
CGOpenMPRuntimeGPU::CGOpenMPRuntimeGPU(CodeGenModule &CGM)
: CGOpenMPRuntime(CGM) {
- llvm::OpenMPIRBuilderConfig Config(CGM.getLangOpts().OpenMPIsDevice, true,
- hasRequiresUnifiedSharedMemory(),
- CGM.getLangOpts().OpenMPOffloadMandatory);
+ llvm::OpenMPIRBuilderConfig Config(
+ CGM.getLangOpts().OpenMPIsDevice,
+ /*IsTargetCodegen*/ true, CGM.getLangOpts().OpenMPOffloadMandatory,
+ /*HasRequiresReverseOffload*/ false, /*HasRequiresUnifiedAddress*/ false,
+ hasRequiresUnifiedSharedMemory(), /*HasRequiresDynamicAllocators*/ false);
OMPBuilder.setConfig(Config);
if (!CGM.getLangOpts().OpenMPIsDevice)
Index: clang/lib/CodeGen/CGOpenMPRuntime.cpp
===================================================================
--- clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -479,27 +479,6 @@
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/OMP_IDENT_WORK_DISTRIBUTE)
};
-namespace {
-LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
-/// Values for bit flags for marking which requires clauses have been used.
-enum OpenMPOffloadingRequiresDirFlags : int64_t {
- /// flag undefined.
- OMP_REQ_UNDEFINED = 0x000,
- /// no requires clause present.
- OMP_REQ_NONE = 0x001,
- /// reverse_offload clause.
- OMP_REQ_REVERSE_OFFLOAD = 0x002,
- /// unified_address clause.
- OMP_REQ_UNIFIED_ADDRESS = 0x004,
- /// unified_shared_memory clause.
- OMP_REQ_UNIFIED_SHARED_MEMORY = 0x008,
- /// dynamic_allocators clause.
- OMP_REQ_DYNAMIC_ALLOCATORS = 0x010,
- LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/OMP_REQ_DYNAMIC_ALLOCATORS)
-};
-
-} // anonymous namespace
-
/// Describes ident structure that describes a source location.
/// All descriptions are taken from
/// https://github.com/llvm/llvm-project/blob/main/openmp/runtime/src/kmp.h
@@ -1056,9 +1035,11 @@
CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
: CGM(CGM), OMPBuilder(CGM.getModule()) {
KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8);
- llvm::OpenMPIRBuilderConfig Config(CGM.getLangOpts().OpenMPIsDevice, false,
- hasRequiresUnifiedSharedMemory(),
- CGM.getLangOpts().OpenMPOffloadMandatory);
+ llvm::OpenMPIRBuilderConfig Config(
+ CGM.getLangOpts().OpenMPIsDevice,
+ /*IsTargetCodegen*/ false, CGM.getLangOpts().OpenMPOffloadMandatory,
+ /*HasRequiresReverseOffload*/ false, /*HasRequiresUnifiedAddress*/ false,
+ hasRequiresUnifiedSharedMemory(), /*HasRequiresDynamicAllocators*/ false);
// Initialize Types used in OpenMPIRBuilder from OMPKinds.def
OMPBuilder.initialize();
OMPBuilder.setConfig(Config);
@@ -10576,7 +10557,6 @@
std::string ReqName = getName({"omp_offloading", "requires_reg"});
RequiresRegFn = CGM.CreateGlobalInitOrCleanUpFunction(FTy, ReqName, FI);
CGF.StartFunction(GlobalDecl(), C.VoidTy, RequiresRegFn, FI, {});
- OpenMPOffloadingRequiresDirFlags Flags = OMP_REQ_NONE;
// TODO: check for other requires clauses.
// The requires directive takes effect only when a target region is
// present in the compilation unit. Otherwise it is ignored and not
@@ -10586,11 +10566,10 @@
assert((HasEmittedTargetRegion || HasEmittedDeclareTargetRegion ||
!OMPBuilder.OffloadInfoManager.empty()) &&
"Target or declare target region expected.");
- if (HasRequiresUnifiedSharedMemory)
- Flags = OMP_REQ_UNIFIED_SHARED_MEMORY;
CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
CGM.getModule(), OMPRTL___tgt_register_requires),
- llvm::ConstantInt::get(CGM.Int64Ty, Flags));
+ llvm::ConstantInt::get(
+ CGM.Int64Ty, OMPBuilder.Config.getRequiresFlags()));
CGF.FinishFunction();
}
return RequiresRegFn;
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits