[clang] 9058762 - [OpenMP][Flang][MLIR] Lowering of requires directive from MLIR to LLVM IR
Sergio Afonso via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 14 02:36:40 PDT 2023
Author: Sergio Afonso
Date: 2023-09-14T10:35:44+01:00
New Revision: 9058762789c0a83560c2b567a347b993e70b05ae
URL: https://github.com/llvm/llvm-project/commit/9058762789c0a83560c2b567a347b993e70b05ae
DIFF: https://github.com/llvm/llvm-project/commit/9058762789c0a83560c2b567a347b993e70b05ae.diff
LOG: [OpenMP][Flang][MLIR] Lowering of requires directive from MLIR to LLVM IR
Default atomic ordering information is processed in the OpenMP dialect
to LLVM IR lowering stage at every spot where an operation can be
affected by it. The rest of clauses are stored globally in the
OpenMPIRBuilderConfig object before starting that lowering stage, so
that the OMPIRBuilder can conditionally modify code generation
depending on these. At the end of the process, the omp.requires
attribute is itself lowered into a global constructor that passes these
clauses as flags to the OpenMP runtime.
Depends on D147217, D147218 and D158278.
Differential Revision: https://reviews.llvm.org/D147219
Added:
Modified:
clang/lib/CodeGen/CGOpenMPRuntime.cpp
llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
mlir/test/Target/LLVMIR/openmp-llvm.mlir
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 3b69305d050d769..92b7c8d4aa546f0 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -1039,9 +1039,10 @@ CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
CGM.getLangOpts().OpenMPOffloadMandatory,
/*HasRequiresReverseOffload*/ false, /*HasRequiresUnifiedAddress*/ false,
hasRequiresUnifiedSharedMemory(), /*HasRequiresDynamicAllocators*/ false);
- OMPBuilder.initialize(CGM.getLangOpts().OpenMPIsTargetDevice
- ? CGM.getLangOpts().OMPHostIRFile
- : StringRef{});
+ OMPBuilder.initialize();
+ OMPBuilder.loadOffloadInfoMetadata(CGM.getLangOpts().OpenMPIsTargetDevice
+ ? CGM.getLangOpts().OMPHostIRFile
+ : StringRef{});
OMPBuilder.setConfig(Config);
}
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
index a5977356da3e530..523a0718e1ffb53 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -154,6 +154,7 @@ class OpenMPIRBuilderConfig {
void setIsTargetDevice(bool Value) { IsTargetDevice = Value; }
void setIsGPU(bool Value) { IsGPU = Value; }
+ void setOpenMPOffloadMandatory(bool Value) { OpenMPOffloadMandatory = Value; }
void setFirstSeparator(StringRef FS) { FirstSeparator = FS; }
void setSeparator(StringRef S) { Separator = S; }
@@ -449,14 +450,9 @@ class OpenMPIRBuilder {
/// Initialize the internal state, this will put structures types and
/// potentially other helpers into the underlying module. Must be called
- /// before any other method and only once! This internal state includes
- /// Types used in the OpenMPIRBuilder generated from OMPKinds.def as well
- /// as loading offload metadata for device from the OpenMP host IR file
- /// passed in as the HostFilePath argument.
- /// \param HostFilePath The path to the host IR file, used to load in
- /// offload metadata for the device, allowing host and device to
- /// maintain the same metadata mapping.
- void initialize(StringRef HostFilePath = {});
+ /// before any other method and only once! This internal state includes types
+ /// used in the OpenMPIRBuilder generated from OMPKinds.def.
+ void initialize();
void setConfig(OpenMPIRBuilderConfig C) { Config = C; }
@@ -2519,6 +2515,15 @@ class OpenMPIRBuilder {
/// loaded from bitcode file, i.e,
diff erent from OpenMPIRBuilder::M module.
void loadOffloadInfoMetadata(Module &M);
+ /// Loads all the offload entries information from the host IR
+ /// metadata read from the file passed in as the HostFilePath argument. This
+ /// function is only meant to be used with device code generation.
+ ///
+ /// \param HostFilePath The path to the host IR file,
+ /// used to load in offload metadata for the device, allowing host and device
+ /// to maintain the same metadata mapping.
+ void loadOffloadInfoMetadata(StringRef HostFilePath);
+
/// Gets (if variable with the given name already exist) or creates
/// internal global variable with the specified Name. The created variable has
/// linkage CommonLinkage by default and is initialized by null value.
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index cc6054cb19d7806..9341c529c603e5d 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -582,31 +582,7 @@ Function *OpenMPIRBuilder::getOrCreateRuntimeFunctionPtr(RuntimeFunction FnID) {
return Fn;
}
-void OpenMPIRBuilder::initialize(StringRef HostFilePath) {
- initializeTypes(M);
-
- if (HostFilePath.empty())
- return;
-
- auto Buf = MemoryBuffer::getFile(HostFilePath);
- if (std::error_code Err = Buf.getError()) {
- report_fatal_error(("error opening host file from host file path inside of "
- "OpenMPIRBuilder: " +
- Err.message())
- .c_str());
- }
-
- LLVMContext Ctx;
- auto M = expectedToErrorOrAndEmitErrors(
- Ctx, parseBitcodeFile(Buf.get()->getMemBufferRef(), Ctx));
- if (std::error_code Err = M.getError()) {
- report_fatal_error(
- ("error parsing host file inside of OpenMPIRBuilder: " + Err.message())
- .c_str());
- }
-
- loadOffloadInfoMetadata(*M.get());
-}
+void OpenMPIRBuilder::initialize() { initializeTypes(M); }
void OpenMPIRBuilder::finalize(Function *Fn) {
SmallPtrSet<BasicBlock *, 32> ParallelRegionBlockSet;
@@ -6206,6 +6182,30 @@ void OpenMPIRBuilder::loadOffloadInfoMetadata(Module &M) {
}
}
+void OpenMPIRBuilder::loadOffloadInfoMetadata(StringRef HostFilePath) {
+ if (HostFilePath.empty())
+ return;
+
+ auto Buf = MemoryBuffer::getFile(HostFilePath);
+ if (std::error_code Err = Buf.getError()) {
+ report_fatal_error(("error opening host file from host file path inside of "
+ "OpenMPIRBuilder: " +
+ Err.message())
+ .c_str());
+ }
+
+ LLVMContext Ctx;
+ auto M = expectedToErrorOrAndEmitErrors(
+ Ctx, parseBitcodeFile(Buf.get()->getMemBufferRef(), Ctx));
+ if (std::error_code Err = M.getError()) {
+ report_fatal_error(
+ ("error parsing host file inside of OpenMPIRBuilder: " + Err.message())
+ .c_str());
+ }
+
+ loadOffloadInfoMetadata(*M.get());
+}
+
Function *OpenMPIRBuilder::createRegisterRequires(StringRef Name) {
// Skip the creation of the registration function if this is device codegen
if (Config.isTargetDevice())
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index 3181b5710c60708..dcc5e3823d663a8 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -17,6 +17,7 @@
#include "mlir/IR/IRMapping.h"
#include "mlir/IR/Operation.h"
#include "mlir/Support/LLVM.h"
+#include "mlir/Support/LogicalResult.h"
#include "mlir/Target/LLVMIR/Dialect/OpenMPCommon.h"
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
#include "mlir/Transforms/RegionUtils.h"
@@ -28,6 +29,8 @@
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/FileSystem.h"
+#include "llvm/Transforms/Utils/ModuleUtils.h"
+#include <optional>
using namespace mlir;
@@ -1165,7 +1168,7 @@ convertOmpSimdLoop(Operation &opInst, llvm::IRBuilderBase &builder,
}
/// Convert an Atomic Ordering attribute to llvm::AtomicOrdering.
-llvm::AtomicOrdering
+static llvm::AtomicOrdering
convertAtomicOrdering(std::optional<omp::ClauseMemoryOrderKind> ao) {
if (!ao)
return llvm::AtomicOrdering::Monotonic; // Default Memory Ordering
@@ -1932,6 +1935,27 @@ convertDeclareTargetAttr(Operation *op,
return success();
}
+/// Converts the module-level set of OpenMP requires clauses into LLVM IR using
+/// OpenMPIRBuilder.
+static LogicalResult
+convertRequiresAttr(Operation &op, omp::ClauseRequiresAttr requiresAttr,
+ LLVM::ModuleTranslation &moduleTranslation) {
+ auto *ompBuilder = moduleTranslation.getOpenMPBuilder();
+
+ // No need to read requiresAttr here, because it has already been done in
+ // translateModuleToLLVMIR(). There, flags are stored in the
+ // OpenMPIRBuilderConfig object, available to the OpenMPIRBuilder.
+ auto *regFn =
+ ompBuilder->createRegisterRequires(ompBuilder->createPlatformSpecificName(
+ {"omp_offloading", "requires_reg"}));
+
+ // Add registration function as global constructor
+ if (regFn)
+ llvm::appendToGlobalCtors(ompBuilder->M, regFn, /* Priority = */ 0);
+
+ return success();
+}
+
namespace {
/// Implementation of the dialect interface that converts operations belonging
@@ -1947,6 +1971,8 @@ class OpenMPDialectLLVMIRTranslationInterface
convertOperation(Operation *op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) const final;
+ /// Given an OpenMP MLIR attribute, create the corresponding LLVM-IR, runtime
+ /// calls, or operation amendments
LogicalResult
amendOperation(Operation *op, NamedAttribute attribute,
LLVM::ModuleTranslation &moduleTranslation) const final;
@@ -1954,31 +1980,90 @@ class OpenMPDialectLLVMIRTranslationInterface
} // namespace
-/// Given an OpenMP MLIR attribute, create the corresponding LLVM-IR, runtime
-/// calls, or operation amendments
LogicalResult OpenMPDialectLLVMIRTranslationInterface::amendOperation(
Operation *op, NamedAttribute attribute,
LLVM::ModuleTranslation &moduleTranslation) const {
- return llvm::TypeSwitch<Attribute, LogicalResult>(attribute.getValue())
- .Case([&](mlir::omp::FlagsAttr rtlAttr) {
- return convertFlagsAttr(op, rtlAttr, moduleTranslation);
- })
- .Case([&](mlir::omp::VersionAttr versionAttr) {
- llvm::OpenMPIRBuilder *ompBuilder =
- moduleTranslation.getOpenMPBuilder();
- ompBuilder->M.addModuleFlag(llvm::Module::Max, "openmp",
- versionAttr.getVersion());
- return success();
- })
- .Case([&](mlir::omp::DeclareTargetAttr declareTargetAttr) {
- return convertDeclareTargetAttr(op, declareTargetAttr,
- moduleTranslation);
- })
- .Default([&](Attribute attr) {
- // fall through for omp attributes that do not require lowering and/or
- // have no concrete definition and thus no type to define a case on
+ return llvm::StringSwitch<llvm::function_ref<LogicalResult(Attribute)>>(
+ attribute.getName())
+ .Case("omp.is_target_device",
+ [&](Attribute attr) {
+ if (auto deviceAttr = attr.dyn_cast<BoolAttr>()) {
+ llvm::OpenMPIRBuilderConfig &config =
+ moduleTranslation.getOpenMPBuilder()->Config;
+ config.setIsTargetDevice(deviceAttr.getValue());
+ return success();
+ }
+ return failure();
+ })
+ .Case("omp.is_gpu",
+ [&](Attribute attr) {
+ if (auto gpuAttr = attr.dyn_cast<BoolAttr>()) {
+ llvm::OpenMPIRBuilderConfig &config =
+ moduleTranslation.getOpenMPBuilder()->Config;
+ config.setIsGPU(gpuAttr.getValue());
+ return success();
+ }
+ return failure();
+ })
+ .Case("omp.host_ir_filepath",
+ [&](Attribute attr) {
+ if (auto filepathAttr = attr.dyn_cast<StringAttr>()) {
+ llvm::OpenMPIRBuilder *ompBuilder =
+ moduleTranslation.getOpenMPBuilder();
+ ompBuilder->loadOffloadInfoMetadata(filepathAttr.getValue());
+ return success();
+ }
+ return failure();
+ })
+ .Case("omp.flags",
+ [&](Attribute attr) {
+ if (auto rtlAttr = attr.dyn_cast<omp::FlagsAttr>())
+ return convertFlagsAttr(op, rtlAttr, moduleTranslation);
+ return failure();
+ })
+ .Case("omp.version",
+ [&](Attribute attr) {
+ if (auto versionAttr = attr.dyn_cast<omp::VersionAttr>()) {
+ llvm::OpenMPIRBuilder *ompBuilder =
+ moduleTranslation.getOpenMPBuilder();
+ ompBuilder->M.addModuleFlag(llvm::Module::Max, "openmp",
+ versionAttr.getVersion());
+ return success();
+ }
+ return failure();
+ })
+ .Case("omp.declare_target",
+ [&](Attribute attr) {
+ if (auto declareTargetAttr =
+ attr.dyn_cast<omp::DeclareTargetAttr>())
+ return convertDeclareTargetAttr(op, declareTargetAttr,
+ moduleTranslation);
+ return failure();
+ })
+ .Case(
+ "omp.requires",
+ [&](Attribute attr) {
+ if (auto requiresAttr = attr.dyn_cast<omp::ClauseRequiresAttr>()) {
+ using Requires = omp::ClauseRequires;
+ Requires flags = requiresAttr.getValue();
+ llvm::OpenMPIRBuilderConfig &config =
+ moduleTranslation.getOpenMPBuilder()->Config;
+ config.setHasRequiresReverseOffload(
+ bitEnumContainsAll(flags, Requires::reverse_offload));
+ config.setHasRequiresUnifiedAddress(
+ bitEnumContainsAll(flags, Requires::unified_address));
+ config.setHasRequiresUnifiedSharedMemory(
+ bitEnumContainsAll(flags, Requires::unified_shared_memory));
+ config.setHasRequiresDynamicAllocators(
+ bitEnumContainsAll(flags, Requires::dynamic_allocators));
+ return convertRequiresAttr(*op, requiresAttr, moduleTranslation);
+ }
+ return failure();
+ })
+ .Default([](Attribute) {
+ // Fall through for omp attributes that do not require lowering.
return success();
- });
+ })(attribute.getValue());
return failure();
}
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index e3ece38b6b8ca9a..0b0a0d1559144a6 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1278,32 +1278,18 @@ SmallVector<llvm::Value *> ModuleTranslation::lookupValues(ValueRange values) {
llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
if (!ompBuilder) {
ompBuilder = std::make_unique<llvm::OpenMPIRBuilder>(*llvmModule);
+ ompBuilder->initialize();
- bool isTargetDevice = false, isGPU = false;
- llvm::StringRef hostIRFilePath = "";
-
- if (auto deviceAttr =
- mlirModule->getAttrOfType<mlir::BoolAttr>("omp.is_target_device"))
- isTargetDevice = deviceAttr.getValue();
-
- if (auto gpuAttr = mlirModule->getAttrOfType<mlir::BoolAttr>("omp.is_gpu"))
- isGPU = gpuAttr.getValue();
-
- if (auto filepathAttr =
- mlirModule->getAttrOfType<mlir::StringAttr>("omp.host_ir_filepath"))
- hostIRFilePath = filepathAttr.getValue();
-
- ompBuilder->initialize(hostIRFilePath);
-
- // TODO: set the flags when available
- llvm::OpenMPIRBuilderConfig config(
- isTargetDevice, isGPU,
+ // Flags represented as top-level OpenMP dialect attributes are set in
+ // `OpenMPDialectLLVMIRTranslationInterface::amendOperation()`. Here we set
+ // the default configuration.
+ ompBuilder->setConfig(llvm::OpenMPIRBuilderConfig(
+ /* IsTargetDevice = */ false, /* IsGPU = */ false,
/* OpenMPOffloadMandatory = */ false,
/* HasRequiresReverseOffload = */ false,
/* HasRequiresUnifiedAddress = */ false,
/* HasRequiresUnifiedSharedMemory = */ false,
- /* HasRequiresDynamicAllocators = */ false);
- ompBuilder->setConfig(config);
+ /* HasRequiresDynamicAllocators = */ false));
}
return ompBuilder.get();
}
diff --git a/mlir/test/Target/LLVMIR/openmp-llvm.mlir b/mlir/test/Target/LLVMIR/openmp-llvm.mlir
index 73b6b007e5a56b5..1dff33cdc22ce12 100644
--- a/mlir/test/Target/LLVMIR/openmp-llvm.mlir
+++ b/mlir/test/Target/LLVMIR/openmp-llvm.mlir
@@ -2661,3 +2661,12 @@ llvm.func @omp_task_if(%boolexpr: i1) {
// CHECK: call void @foo_after()
// CHECK: ret void
+// -----
+
+// Check that OpenMP requires flags are registered by a global constructor.
+// CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }]
+// CHECK-SAME: [{ i32, ptr, ptr } { i32 0, ptr @[[REG_FN:.*]], ptr null }]
+// CHECK: define {{.*}} @[[REG_FN]]({{.*}})
+// CHECK-NOT: }
+// CHECK: call void @__tgt_register_requires(i64 10)
+module attributes {omp.requires = #omp<clause_requires reverse_offload|unified_shared_memory>} {}
More information about the cfe-commits
mailing list