[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-08-17 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 551088.
skatrak added a comment.
Herald added subscribers: gysit, Dinistro.

Update patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

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
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1306,8 +1306,11 @@
 // TODO: set the flags when available
 llvm::OpenMPIRBuilderConfig config(
 isTargetDevice, isGPU,
-/* HasRequiresUnifiedSharedMemory */ false,
-/* OpenMPOffloadMandatory */ false);
+/* OpenMPOffloadMandatory = */ false,
+/* HasRequiresReverseOffload = */ false,
+/* HasRequiresUnifiedAddress = */ false,
+/* HasRequiresUnifiedSharedMemory = */ false,
+/* HasRequiresDynamicAllocators = */ false);
 ompBuilder->setConfig(config);
   }
   return ompBuilder.get();
Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -5128,7 +5128,7 @@
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
   F->setName("func");
   IRBuilder<> Builder(BB);
@@ -5206,7 +5206,8 @@
 
 TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.setConfig(OpenMPIRBuilderConfig(true, false, false, false));
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(true, false, false, false, false, false, false));
   OMPBuilder.initialize();
 
   F->setName("func");
@@ -5896,7 +5897,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);
@@ -5919,7 +5921,7 @@
 TEST_F(OpenMPIRBuilderTest, registerTargetGlobalVariable) {
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
 
   std::vector TargetTriple;
@@ -5996,8 +5998,11 @@
   OMPBuilder.initialize();
   OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ true,
/* IsGPU = */ true,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ false,
+   /* HasRequiresUnifiedAddress = */ false,
/* HasRequiresUnifiedSharedMemory = */ false,
-   /* OpenMPOffloadMandatory = */ false);
+   /* HasRequiresDynamicAllocators = */ false);
   OMPBuilder.setConfig(Config);
 
   FunctionCallee FnTypeAndCallee =
@@ -6033,4 +6038,44 @@
   EXPECT_TRUE(Fn->hasFnAttribute(Attribute::MustProgress));
 }
 
+TEST_F(OpenMPIRBuilderTest, CreateRegisterRequires) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+
+  OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ false,
+   /* IsGPU = */ false,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ true,
+   /* HasRequiresUnifiedAddress = */ false,
+   /* HasRequiresUnifiedSharedMemory = */ true,
+   /* HasRequiresDynamicAllocators = */ false);
+  OMPBuilder.setConfig(Config);
+
+  auto FName =
+  OMPBuilder.createPlatformSpecificName({"omp_offloading", "requires_reg"});
+  EXPECT_EQ(FName, ".omp_offloading.requires_reg");
+
+  Function *Fn = OMPBuilder.createRegisterRequires(FName);
+  EXPECT_NE(Fn, nullptr);
+  EXPECT_EQ(FName, Fn->getName());
+
+  EXPECT_EQ(Fn->getSection(), ".text.startup");
+  EXPECT_TRUE(Fn->hasInternalLinkage());
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoInline));
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoUnwi

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-08-17 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 551113.
skatrak added a comment.

Change parent and update patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,11 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -2407,6 +2407,44 @@
  reductionDeclSymbols));
 }
 
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::AbstractConverter &converter,
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+llvm::SmallVectorImpl &symbolAndClause) {
+
+  // The default capture type
+  mlir::omp::DeclareTargetDeviceType deviceType =
+  mlir::omp::DeclareTargetDeviceType::any;
+  const auto &spec = std::get(
+  declareTargetConstruct.t);
+
+  if (const auto *objectList{
+  Fortran::parser::Unwrap(spec.u)}) {
+// Case: declare target(func, var1, var2)
+gatherFuncAndVarSyms(*objectList, mlir::omp::DeclareTargetCaptureClause::to,
+ symbolAndClause);
+  } else if (const auto *clauseList{
+ Fortran::parser::Unwrap(
+ spec.u)}) {
+if (clauseList->v.empty()) {
+  // Case: declare target, implicit capture of function
+  symbolAndClause.emplace_back(
+  mlir::omp::DeclareTargetCaptureClause::to,
+  eval.getOwningProcedure()->getSubprogramSymbol());
+}
+
+ClauseProcessor cp(converter, *clauseList);
+cp.processTo(symbolAndClause);
+cp.processLink(symbolAndClause);
+cp.processDeviceType(deviceType);
+  }
+
+  return deviceType;
+}
+
 //===--===//
 // genOMP() Code generation helper functions
 //===--===//
@@ -3306,32 +3344,8 @@
&declareTargetConstruct) {
   llvm::SmallVector symbolAndClause;
   mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
-
-  // The default capture type
-  mlir::omp::DeclareTargetDeviceType deviceType =
-  mlir::omp::DeclareTargetDeviceType::any;
-  const auto &spec = std::get(
-  declareTargetConstruct.t);
-  if (const auto *objectList{
-  Fortran::parser::Unwrap(spec.u)}) {
-// Case: declare target(func, var1, var2)
-gatherFuncAndVarSyms(*objectList, mlir::omp::DeclareTargetCaptureClause::to,
- symbolAndClause);
-  } else if (const auto *clauseList{
- Fortran::parser::Unwrap(
- spec.u)}) {
-if (clauseList->v.empty()) {
-  // Case: declare target, implicit capture of function
-  symbolAndClause.emplace_back(
-  mlir::omp::DeclareTargetCaptureClause::to,
-  eval.getOwningProcedure()->getSubprogramSymbol());
-}
-
-ClauseProcessor cp(converter, *clauseList);
-cp.processTo(symbolAndClause);
-cp.processLink(symbolAndClause);
-cp.processDeviceType(deviceType);
-  }
+  mlir::omp::DeclareTargetDeviceType deviceType = getDeclareTargetInfo(
+  converter, eval, declareTargetConstruct, symbolAndClause);
 
   for (const DeclareTargetCapturePair &symClause : symbolAndClause) {
 mlir::Operation *op = mod.lookupSymbol(
@@ -3435,6 +34

[PATCH] D147219: [OpenMP][Flang][MLIR] Lowering of requires directive from MLIR to LLVM IR

2023-08-17 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 551147.
skatrak added a comment.
Herald added subscribers: gysit, Dinistro.

Update patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147219/new/

https://reviews.llvm.org/D147219

Files:
  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

Index: mlir/test/Target/LLVMIR/openmp-llvm.mlir
===
--- mlir/test/Target/LLVMIR/openmp-llvm.mlir
+++ mlir/test/Target/LLVMIR/openmp-llvm.mlir
@@ -2661,3 +2661,12 @@
 // 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} {}
Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1286,32 +1286,18 @@
 llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
   if (!ompBuilder) {
 ompBuilder = std::make_unique(*llvmModule);
+ompBuilder->initialize();
 
-bool isTargetDevice = false, isGPU = false;
-llvm::StringRef hostIRFilePath = "";
-
-if (auto deviceAttr =
-mlirModule->getAttrOfType("omp.is_target_device"))
-  isTargetDevice = deviceAttr.getValue();
-
-if (auto gpuAttr = mlirModule->getAttrOfType("omp.is_gpu"))
-  isGPU = gpuAttr.getValue();
-
-if (auto filepathAttr =
-mlirModule->getAttrOfType("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();
 }
@@ -1399,8 +1385,14 @@
   if (failed(translator.createTBAAMetadata()))
 return nullptr;
 
-  // Convert other top-level operations if possible.
+  // Convert module itself before any functions and operations inside, so that
+  // the OpenMPIRBuilder is configured with the OpenMP dialect attributes
+  // attached to the module by `amendOperation()` calls before then.
   llvm::IRBuilder<> llvmBuilder(llvmContext);
+  if (failed(translator.convertOperation(*module, llvmBuilder)))
+return nullptr;
+
+  // Convert other top-level operations if possible.
   for (Operation &o : getModuleBody(module).getOperations()) {
 if (!isa(&o) &&
@@ -1416,10 +1408,6 @@
   if (failed(translator.convertFunctions()))
 return nullptr;
 
-  // Convert module itself.
-  if (failed(translator.convertOperation(*module, llvmBuilder)))
-return nullptr;
-
   if (llvm::verifyModule(*translator.llvmModule, &llvm::errs()))
 return nullptr;
 
Index: mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ 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 
 
 using namespace mlir;
 
@@ -1165,7 +1168,7 @@
 }
 
 /// Convert an Atomic Ordering attribute to llvm::AtomicOrdering.
-llvm::AtomicOrdering
+static llvm::AtomicOrdering
 convertAtomicOrdering(std::optional ao) {
   if (!ao)
 return llvm::AtomicOrderin

[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-08-17 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 551150.
skatrak added a comment.

Fix formatting.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

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
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1306,8 +1306,11 @@
 // TODO: set the flags when available
 llvm::OpenMPIRBuilderConfig config(
 isTargetDevice, isGPU,
-/* HasRequiresUnifiedSharedMemory */ false,
-/* OpenMPOffloadMandatory */ false);
+/* OpenMPOffloadMandatory = */ false,
+/* HasRequiresReverseOffload = */ false,
+/* HasRequiresUnifiedAddress = */ false,
+/* HasRequiresUnifiedSharedMemory = */ false,
+/* HasRequiresDynamicAllocators = */ false);
 ompBuilder->setConfig(config);
   }
   return ompBuilder.get();
Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -5128,7 +5128,7 @@
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
   F->setName("func");
   IRBuilder<> Builder(BB);
@@ -5206,7 +5206,8 @@
 
 TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.setConfig(OpenMPIRBuilderConfig(true, false, false, false));
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(true, false, false, false, false, false, false));
   OMPBuilder.initialize();
 
   F->setName("func");
@@ -5896,7 +5897,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);
@@ -5919,7 +5921,7 @@
 TEST_F(OpenMPIRBuilderTest, registerTargetGlobalVariable) {
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
 
   std::vector TargetTriple;
@@ -5996,8 +5998,11 @@
   OMPBuilder.initialize();
   OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ true,
/* IsGPU = */ true,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ false,
+   /* HasRequiresUnifiedAddress = */ false,
/* HasRequiresUnifiedSharedMemory = */ false,
-   /* OpenMPOffloadMandatory = */ false);
+   /* HasRequiresDynamicAllocators = */ false);
   OMPBuilder.setConfig(Config);
 
   FunctionCallee FnTypeAndCallee =
@@ -6033,4 +6038,44 @@
   EXPECT_TRUE(Fn->hasFnAttribute(Attribute::MustProgress));
 }
 
+TEST_F(OpenMPIRBuilderTest, CreateRegisterRequires) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+
+  OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ false,
+   /* IsGPU = */ false,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ true,
+   /* HasRequiresUnifiedAddress = */ false,
+   /* HasRequiresUnifiedSharedMemory = */ true,
+   /* HasRequiresDynamicAllocators = */ false);
+  OMPBuilder.setConfig(Config);
+
+  auto FName =
+  OMPBuilder.createPlatformSpecificName({"omp_offloading", "requires_reg"});
+  EXPECT_EQ(FName, ".omp_offloading.requires_reg");
+
+  Function *Fn = OMPBuilder.createRegisterRequires(FName);
+  EXPECT_NE(Fn, nullptr);
+  EXPECT_EQ(FName, Fn->getName());
+
+  EXPECT_EQ(Fn->getSection(), ".text.startup");
+  EXPECT_TRUE(Fn->hasInternalLinkage());
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoInline));
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoUnwind));
+  EXPECT_EQ(Fn->size(), 1u);
+
+  

[PATCH] D147219: [OpenMP][Flang][MLIR] Lowering of requires directive from MLIR to LLVM IR

2023-08-17 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak requested review of this revision.
skatrak marked 11 inline comments as done.
skatrak added a comment.

I think the change of approach to initialize the `OpenMPIRBuilderConfig` 
through the `amendOperation()` flow (rather than directly inside of 
`ModuleTranslation::getOpenMPBuilder()`) after @kiranchandramohan 's acceptance 
of the patch requires another review before landing. This patch still depends 
on a few pending patches so, in case significant changes have to be made to 
these, I think it's best to leave this review for last.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147219/new/

https://reviews.llvm.org/D147219

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-08-18 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 551454.
skatrak added a comment.

Rebase patch to fix unrelated build error.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

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
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1309,8 +1309,11 @@
 // TODO: set the flags when available
 llvm::OpenMPIRBuilderConfig config(
 isTargetDevice, isGPU,
-/* HasRequiresUnifiedSharedMemory */ false,
-/* OpenMPOffloadMandatory */ false);
+/* OpenMPOffloadMandatory = */ false,
+/* HasRequiresReverseOffload = */ false,
+/* HasRequiresUnifiedAddress = */ false,
+/* HasRequiresUnifiedSharedMemory = */ false,
+/* HasRequiresDynamicAllocators = */ false);
 ompBuilder->setConfig(config);
   }
   return ompBuilder.get();
Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -5128,7 +5128,7 @@
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
   F->setName("func");
   IRBuilder<> Builder(BB);
@@ -5206,7 +5206,8 @@
 
 TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.setConfig(OpenMPIRBuilderConfig(true, false, false, false));
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(true, false, false, false, false, false, false));
   OMPBuilder.initialize();
 
   F->setName("func");
@@ -5896,7 +5897,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);
@@ -5919,7 +5921,7 @@
 TEST_F(OpenMPIRBuilderTest, registerTargetGlobalVariable) {
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
 
   std::vector TargetTriple;
@@ -5996,8 +5998,11 @@
   OMPBuilder.initialize();
   OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ true,
/* IsGPU = */ true,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ false,
+   /* HasRequiresUnifiedAddress = */ false,
/* HasRequiresUnifiedSharedMemory = */ false,
-   /* OpenMPOffloadMandatory = */ false);
+   /* HasRequiresDynamicAllocators = */ false);
   OMPBuilder.setConfig(Config);
 
   FunctionCallee FnTypeAndCallee =
@@ -6033,4 +6038,44 @@
   EXPECT_TRUE(Fn->hasFnAttribute(Attribute::MustProgress));
 }
 
+TEST_F(OpenMPIRBuilderTest, CreateRegisterRequires) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+
+  OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ false,
+   /* IsGPU = */ false,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ true,
+   /* HasRequiresUnifiedAddress = */ false,
+   /* HasRequiresUnifiedSharedMemory = */ true,
+   /* HasRequiresDynamicAllocators = */ false);
+  OMPBuilder.setConfig(Config);
+
+  auto FName =
+  OMPBuilder.createPlatformSpecificName({"omp_offloading", "requires_reg"});
+  EXPECT_EQ(FName, ".omp_offloading.requires_reg");
+
+  Function *Fn = OMPBuilder.createRegisterRequires(FName);
+  EXPECT_NE(Fn, nullptr);
+  EXPECT_EQ(FName, Fn->getName());
+
+  EXPECT_EQ(Fn->getSection(), ".text.startup");
+  EXPECT_TRUE(Fn->hasInternalLinkage());
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoInline));
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoUnwind));
+  EXPEC

[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-08-18 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 551497.
skatrak added a comment.

Rebase.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

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
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1309,8 +1309,11 @@
 // TODO: set the flags when available
 llvm::OpenMPIRBuilderConfig config(
 isTargetDevice, isGPU,
-/* HasRequiresUnifiedSharedMemory */ false,
-/* OpenMPOffloadMandatory */ false);
+/* OpenMPOffloadMandatory = */ false,
+/* HasRequiresReverseOffload = */ false,
+/* HasRequiresUnifiedAddress = */ false,
+/* HasRequiresUnifiedSharedMemory = */ false,
+/* HasRequiresDynamicAllocators = */ false);
 ompBuilder->setConfig(config);
   }
   return ompBuilder.get();
Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -5128,7 +5128,7 @@
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
   F->setName("func");
   IRBuilder<> Builder(BB);
@@ -5206,7 +5206,8 @@
 
 TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.setConfig(OpenMPIRBuilderConfig(true, false, false, false));
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(true, false, false, false, false, false, false));
   OMPBuilder.initialize();
 
   F->setName("func");
@@ -5896,7 +5897,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);
@@ -5919,7 +5921,7 @@
 TEST_F(OpenMPIRBuilderTest, registerTargetGlobalVariable) {
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
 
   std::vector TargetTriple;
@@ -5996,8 +5998,11 @@
   OMPBuilder.initialize();
   OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ true,
/* IsGPU = */ true,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ false,
+   /* HasRequiresUnifiedAddress = */ false,
/* HasRequiresUnifiedSharedMemory = */ false,
-   /* OpenMPOffloadMandatory = */ false);
+   /* HasRequiresDynamicAllocators = */ false);
   OMPBuilder.setConfig(Config);
 
   FunctionCallee FnTypeAndCallee =
@@ -6033,4 +6038,44 @@
   EXPECT_TRUE(Fn->hasFnAttribute(Attribute::MustProgress));
 }
 
+TEST_F(OpenMPIRBuilderTest, CreateRegisterRequires) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+
+  OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ false,
+   /* IsGPU = */ false,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ true,
+   /* HasRequiresUnifiedAddress = */ false,
+   /* HasRequiresUnifiedSharedMemory = */ true,
+   /* HasRequiresDynamicAllocators = */ false);
+  OMPBuilder.setConfig(Config);
+
+  auto FName =
+  OMPBuilder.createPlatformSpecificName({"omp_offloading", "requires_reg"});
+  EXPECT_EQ(FName, ".omp_offloading.requires_reg");
+
+  Function *Fn = OMPBuilder.createRegisterRequires(FName);
+  EXPECT_NE(Fn, nullptr);
+  EXPECT_EQ(FName, Fn->getName());
+
+  EXPECT_EQ(Fn->getSection(), ".text.startup");
+  EXPECT_TRUE(Fn->hasInternalLinkage());
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoInline));
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoUnwind));
+  EXPECT_EQ(Fn->size(), 1u);
+
+  BasicBlo

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-08-18 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 551500.
skatrak added a comment.

Rebase and address conflicts.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,11 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -2407,6 +2407,44 @@
  reductionDeclSymbols));
 }
 
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::AbstractConverter &converter,
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+llvm::SmallVectorImpl &symbolAndClause) {
+
+  // The default capture type
+  mlir::omp::DeclareTargetDeviceType deviceType =
+  mlir::omp::DeclareTargetDeviceType::any;
+  const auto &spec = std::get(
+  declareTargetConstruct.t);
+
+  if (const auto *objectList{
+  Fortran::parser::Unwrap(spec.u)}) {
+// Case: declare target(func, var1, var2)
+gatherFuncAndVarSyms(*objectList, mlir::omp::DeclareTargetCaptureClause::to,
+ symbolAndClause);
+  } else if (const auto *clauseList{
+ Fortran::parser::Unwrap(
+ spec.u)}) {
+if (clauseList->v.empty()) {
+  // Case: declare target, implicit capture of function
+  symbolAndClause.emplace_back(
+  mlir::omp::DeclareTargetCaptureClause::to,
+  eval.getOwningProcedure()->getSubprogramSymbol());
+}
+
+ClauseProcessor cp(converter, *clauseList);
+cp.processTo(symbolAndClause);
+cp.processLink(symbolAndClause);
+cp.processDeviceType(deviceType);
+  }
+
+  return deviceType;
+}
+
 //===--===//
 // genOMP() Code generation helper functions
 //===--===//
@@ -3306,32 +3344,8 @@
&declareTargetConstruct) {
   llvm::SmallVector symbolAndClause;
   mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
-
-  // The default capture type
-  mlir::omp::DeclareTargetDeviceType deviceType =
-  mlir::omp::DeclareTargetDeviceType::any;
-  const auto &spec = std::get(
-  declareTargetConstruct.t);
-  if (const auto *objectList{
-  Fortran::parser::Unwrap(spec.u)}) {
-// Case: declare target(func, var1, var2)
-gatherFuncAndVarSyms(*objectList, mlir::omp::DeclareTargetCaptureClause::to,
- symbolAndClause);
-  } else if (const auto *clauseList{
- Fortran::parser::Unwrap(
- spec.u)}) {
-if (clauseList->v.empty()) {
-  // Case: declare target, implicit capture of function
-  symbolAndClause.emplace_back(
-  mlir::omp::DeclareTargetCaptureClause::to,
-  eval.getOwningProcedure()->getSubprogramSymbol());
-}
-
-ClauseProcessor cp(converter, *clauseList);
-cp.processTo(symbolAndClause);
-cp.processLink(symbolAndClause);
-cp.processDeviceType(deviceType);
-  }
+  mlir::omp::DeclareTargetDeviceType deviceType = getDeclareTargetInfo(
+  converter, eval, declareTargetConstruct, symbolAndClause);
 
   for (const DeclareTargetCapturePair &symClause : symbolAndClause) {
 mlir::Operation *op = mod.lookupSymbol(
@@ -3435,6 +3449

[PATCH] D147219: [OpenMP][Flang][MLIR] Lowering of requires directive from MLIR to LLVM IR

2023-08-18 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 551502.
skatrak marked an inline comment as done.
skatrak added a comment.

Update to split generic MLIR into its own patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147219/new/

https://reviews.llvm.org/D147219

Files:
  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

Index: mlir/test/Target/LLVMIR/openmp-llvm.mlir
===
--- mlir/test/Target/LLVMIR/openmp-llvm.mlir
+++ mlir/test/Target/LLVMIR/openmp-llvm.mlir
@@ -2661,3 +2661,12 @@
 // 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} {}
Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1289,32 +1289,18 @@
 llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
   if (!ompBuilder) {
 ompBuilder = std::make_unique(*llvmModule);
+ompBuilder->initialize();
 
-bool isTargetDevice = false, isGPU = false;
-llvm::StringRef hostIRFilePath = "";
-
-if (auto deviceAttr =
-mlirModule->getAttrOfType("omp.is_target_device"))
-  isTargetDevice = deviceAttr.getValue();
-
-if (auto gpuAttr = mlirModule->getAttrOfType("omp.is_gpu"))
-  isGPU = gpuAttr.getValue();
-
-if (auto filepathAttr =
-mlirModule->getAttrOfType("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();
 }
Index: mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ 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 
 
 using namespace mlir;
 
@@ -1165,7 +1168,7 @@
 }
 
 /// Convert an Atomic Ordering attribute to llvm::AtomicOrdering.
-llvm::AtomicOrdering
+static llvm::AtomicOrdering
 convertAtomicOrdering(std::optional ao) {
   if (!ao)
 return llvm::AtomicOrdering::Monotonic; // Default Memory Ordering
@@ -1932,6 +1935,27 @@
   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 =

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-08-18 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 551506.
skatrak added a comment.

Rebase.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,11 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -2407,6 +2407,44 @@
  reductionDeclSymbols));
 }
 
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::AbstractConverter &converter,
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+llvm::SmallVectorImpl &symbolAndClause) {
+
+  // The default capture type
+  mlir::omp::DeclareTargetDeviceType deviceType =
+  mlir::omp::DeclareTargetDeviceType::any;
+  const auto &spec = std::get(
+  declareTargetConstruct.t);
+
+  if (const auto *objectList{
+  Fortran::parser::Unwrap(spec.u)}) {
+// Case: declare target(func, var1, var2)
+gatherFuncAndVarSyms(*objectList, mlir::omp::DeclareTargetCaptureClause::to,
+ symbolAndClause);
+  } else if (const auto *clauseList{
+ Fortran::parser::Unwrap(
+ spec.u)}) {
+if (clauseList->v.empty()) {
+  // Case: declare target, implicit capture of function
+  symbolAndClause.emplace_back(
+  mlir::omp::DeclareTargetCaptureClause::to,
+  eval.getOwningProcedure()->getSubprogramSymbol());
+}
+
+ClauseProcessor cp(converter, *clauseList);
+cp.processTo(symbolAndClause);
+cp.processLink(symbolAndClause);
+cp.processDeviceType(deviceType);
+  }
+
+  return deviceType;
+}
+
 //===--===//
 // genOMP() Code generation helper functions
 //===--===//
@@ -3306,32 +3344,8 @@
&declareTargetConstruct) {
   llvm::SmallVector symbolAndClause;
   mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
-
-  // The default capture type
-  mlir::omp::DeclareTargetDeviceType deviceType =
-  mlir::omp::DeclareTargetDeviceType::any;
-  const auto &spec = std::get(
-  declareTargetConstruct.t);
-  if (const auto *objectList{
-  Fortran::parser::Unwrap(spec.u)}) {
-// Case: declare target(func, var1, var2)
-gatherFuncAndVarSyms(*objectList, mlir::omp::DeclareTargetCaptureClause::to,
- symbolAndClause);
-  } else if (const auto *clauseList{
- Fortran::parser::Unwrap(
- spec.u)}) {
-if (clauseList->v.empty()) {
-  // Case: declare target, implicit capture of function
-  symbolAndClause.emplace_back(
-  mlir::omp::DeclareTargetCaptureClause::to,
-  eval.getOwningProcedure()->getSubprogramSymbol());
-}
-
-ClauseProcessor cp(converter, *clauseList);
-cp.processTo(symbolAndClause);
-cp.processLink(symbolAndClause);
-cp.processDeviceType(deviceType);
-  }
+  mlir::omp::DeclareTargetDeviceType deviceType = getDeclareTargetInfo(
+  converter, eval, declareTargetConstruct, symbolAndClause);
 
   for (const DeclareTargetCapturePair &symClause : symbolAndClause) {
 mlir::Operation *op = mod.lookupSymbol(
@@ -3435,6 +3449,28 @@
   ompConst

[PATCH] D147219: [OpenMP][Flang][MLIR] Lowering of requires directive from MLIR to LLVM IR

2023-08-21 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 551968.
skatrak added a comment.

Update patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147219/new/

https://reviews.llvm.org/D147219

Files:
  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

Index: mlir/test/Target/LLVMIR/openmp-llvm.mlir
===
--- mlir/test/Target/LLVMIR/openmp-llvm.mlir
+++ mlir/test/Target/LLVMIR/openmp-llvm.mlir
@@ -2661,3 +2661,12 @@
 // 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} {}
Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1289,32 +1289,18 @@
 llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
   if (!ompBuilder) {
 ompBuilder = std::make_unique(*llvmModule);
+ompBuilder->initialize();
 
-bool isTargetDevice = false, isGPU = false;
-llvm::StringRef hostIRFilePath = "";
-
-if (auto deviceAttr =
-mlirModule->getAttrOfType("omp.is_target_device"))
-  isTargetDevice = deviceAttr.getValue();
-
-if (auto gpuAttr = mlirModule->getAttrOfType("omp.is_gpu"))
-  isGPU = gpuAttr.getValue();
-
-if (auto filepathAttr =
-mlirModule->getAttrOfType("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();
 }
Index: mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ 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 
 
 using namespace mlir;
 
@@ -1165,7 +1168,7 @@
 }
 
 /// Convert an Atomic Ordering attribute to llvm::AtomicOrdering.
-llvm::AtomicOrdering
+static llvm::AtomicOrdering
 convertAtomicOrdering(std::optional ao) {
   if (!ao)
 return llvm::AtomicOrdering::Monotonic; // Default Memory Ordering
@@ -1932,6 +1935,27 @@
   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

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-08-22 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 552411.
skatrak marked an inline comment as done.
skatrak added a comment.

Update patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,11 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -2467,6 +2467,47 @@
  reductionDeclSymbols));
 }
 
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::AbstractConverter &converter,
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+llvm::SmallVectorImpl &symbolAndClause) {
+
+  // The default capture type
+  mlir::omp::DeclareTargetDeviceType deviceType =
+  mlir::omp::DeclareTargetDeviceType::any;
+  const auto &spec = std::get(
+  declareTargetConstruct.t);
+
+  if (const auto *objectList{
+  Fortran::parser::Unwrap(spec.u)}) {
+// Case: declare target(func, var1, var2)
+gatherFuncAndVarSyms(*objectList, mlir::omp::DeclareTargetCaptureClause::to,
+ symbolAndClause);
+  } else if (const auto *clauseList{
+ Fortran::parser::Unwrap(
+ spec.u)}) {
+if (clauseList->v.empty()) {
+  // Case: declare target, implicit capture of function
+  symbolAndClause.emplace_back(
+  mlir::omp::DeclareTargetCaptureClause::to,
+  eval.getOwningProcedure()->getSubprogramSymbol());
+}
+
+ClauseProcessor cp(converter, *clauseList);
+cp.processTo(symbolAndClause);
+cp.processLink(symbolAndClause);
+cp.processDeviceType(deviceType);
+cp.processTODO(
+converter.getCurrentLocation(),
+llvm::omp::Directive::OMPD_declare_target);
+  }
+
+  return deviceType;
+}
+
 //===--===//
 // genOMP() Code generation helper functions
 //===--===//
@@ -3383,35 +3424,8 @@
&declareTargetConstruct) {
   llvm::SmallVector symbolAndClause;
   mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
-
-  // The default capture type
-  mlir::omp::DeclareTargetDeviceType deviceType =
-  mlir::omp::DeclareTargetDeviceType::any;
-  const auto &spec = std::get(
-  declareTargetConstruct.t);
-  if (const auto *objectList{
-  Fortran::parser::Unwrap(spec.u)}) {
-// Case: declare target(func, var1, var2)
-gatherFuncAndVarSyms(*objectList, mlir::omp::DeclareTargetCaptureClause::to,
- symbolAndClause);
-  } else if (const auto *clauseList{
- Fortran::parser::Unwrap(
- spec.u)}) {
-if (clauseList->v.empty()) {
-  // Case: declare target, implicit capture of function
-  symbolAndClause.emplace_back(
-  mlir::omp::DeclareTargetCaptureClause::to,
-  eval.getOwningProcedure()->getSubprogramSymbol());
-}
-
-ClauseProcessor cp(converter, *clauseList);
-cp.processTo(symbolAndClause);
-cp.processLink(symbolAndClause);
-cp.processDeviceType(deviceType);
-cp.processTODO(
-converter.getCurrentLocation(),
-llvm::omp::Directive::OMPD_declare_target);
-  }
+  mlir::omp::Dec

[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-08-22 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 552427.
skatrak added a comment.

Update patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

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
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1309,8 +1309,11 @@
 // TODO: set the flags when available
 llvm::OpenMPIRBuilderConfig config(
 isTargetDevice, isGPU,
-/* HasRequiresUnifiedSharedMemory */ false,
-/* OpenMPOffloadMandatory */ false);
+/* OpenMPOffloadMandatory = */ false,
+/* HasRequiresReverseOffload = */ false,
+/* HasRequiresUnifiedAddress = */ false,
+/* HasRequiresUnifiedSharedMemory = */ false,
+/* HasRequiresDynamicAllocators = */ false);
 ompBuilder->setConfig(config);
   }
   return ompBuilder.get();
Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -5128,7 +5128,7 @@
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
   F->setName("func");
   IRBuilder<> Builder(BB);
@@ -5206,7 +5206,8 @@
 
 TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.setConfig(OpenMPIRBuilderConfig(true, false, false, false));
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(true, false, false, false, false, false, false));
   OMPBuilder.initialize();
 
   F->setName("func");
@@ -5896,7 +5897,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);
@@ -5919,7 +5921,7 @@
 TEST_F(OpenMPIRBuilderTest, registerTargetGlobalVariable) {
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
 
   std::vector TargetTriple;
@@ -5996,8 +5998,11 @@
   OMPBuilder.initialize();
   OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ true,
/* IsGPU = */ true,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ false,
+   /* HasRequiresUnifiedAddress = */ false,
/* HasRequiresUnifiedSharedMemory = */ false,
-   /* OpenMPOffloadMandatory = */ false);
+   /* HasRequiresDynamicAllocators = */ false);
   OMPBuilder.setConfig(Config);
 
   FunctionCallee FnTypeAndCallee =
@@ -6033,4 +6038,44 @@
   EXPECT_TRUE(Fn->hasFnAttribute(Attribute::MustProgress));
 }
 
+TEST_F(OpenMPIRBuilderTest, CreateRegisterRequires) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+
+  OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ false,
+   /* IsGPU = */ false,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ true,
+   /* HasRequiresUnifiedAddress = */ false,
+   /* HasRequiresUnifiedSharedMemory = */ true,
+   /* HasRequiresDynamicAllocators = */ false);
+  OMPBuilder.setConfig(Config);
+
+  auto FName =
+  OMPBuilder.createPlatformSpecificName({"omp_offloading", "requires_reg"});
+  EXPECT_EQ(FName, ".omp_offloading.requires_reg");
+
+  Function *Fn = OMPBuilder.createRegisterRequires(FName);
+  EXPECT_NE(Fn, nullptr);
+  EXPECT_EQ(FName, Fn->getName());
+
+  EXPECT_EQ(Fn->getSection(), ".text.startup");
+  EXPECT_TRUE(Fn->hasInternalLinkage());
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoInline));
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoUnwind));
+  EXPECT_EQ(Fn->size(), 1u);
+
+  Ba

[PATCH] D157090: [Flang][Sema] Move directive sets to a shared location

2023-08-04 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak created this revision.
skatrak added reviewers: kiranchandramohan, jsjodin, domada, agozillon, 
TIFitis, raghavendhra.
Herald added a reviewer: sscalpone.
Herald added projects: Flang, All.
skatrak requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: cfe-commits, jplehr, sstefan1, jdoerfert.
Herald added a project: clang.

This patch moves directive sets defined internally in Semantics to a header 
accessible by other stages of the compiler to enable reuse. Some sets are 
renamed/rearranged and others are lifted from local definitions to provide a 
single source of truth.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157090

Files:
  clang/docs/tools/clang-formatted-files.txt
  flang/include/flang/Semantics/openmp-directive-sets.h
  flang/lib/Semantics/CMakeLists.txt
  flang/lib/Semantics/check-omp-structure.cpp
  flang/lib/Semantics/check-omp-structure.h
  flang/lib/Semantics/openmp-directive-sets.cpp
  flang/lib/Semantics/resolve-directives.cpp

Index: flang/lib/Semantics/resolve-directives.cpp
===
--- flang/lib/Semantics/resolve-directives.cpp
+++ flang/lib/Semantics/resolve-directives.cpp
@@ -1345,7 +1345,7 @@
 if (targetIt == dirContext_.rend()) {
   return;
 }
-if (llvm::omp::parallelSet.test(targetIt->directive) ||
+if (llvm::omp::allParallelSet.test(targetIt->directive) ||
 llvm::omp::taskGeneratingSet.test(targetIt->directive)) {
   break;
 }
@@ -1446,7 +1446,7 @@
 return;
   }
   Symbol::Flag ivDSA;
-  if (!llvm::omp::simdSet.test(GetContext().directive)) {
+  if (!llvm::omp::allSimdSet.test(GetContext().directive)) {
 ivDSA = Symbol::Flag::OmpPrivate;
   } else if (level == 1) {
 ivDSA = Symbol::Flag::OmpLinear;
Index: flang/lib/Semantics/openmp-directive-sets.cpp
===
--- /dev/null
+++ flang/lib/Semantics/openmp-directive-sets.cpp
@@ -0,0 +1,270 @@
+//===-- lib/Semantics/openmp-directive-sets.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "flang/Semantics/openmp-directive-sets.h"
+
+//===--===//
+// Single directives
+//===--===//
+
+OmpDirectiveSet llvm::omp::topParallelSet{
+Directive::OMPD_parallel,
+Directive::OMPD_parallel_do,
+Directive::OMPD_parallel_do_simd,
+Directive::OMPD_parallel_sections,
+Directive::OMPD_parallel_workshare,
+};
+
+OmpDirectiveSet llvm::omp::allParallelSet{
+Directive::OMPD_distribute_parallel_do,
+Directive::OMPD_distribute_parallel_do_simd,
+Directive::OMPD_parallel,
+Directive::OMPD_parallel_do,
+Directive::OMPD_parallel_do_simd,
+Directive::OMPD_parallel_sections,
+Directive::OMPD_parallel_workshare,
+Directive::OMPD_target_parallel,
+Directive::OMPD_target_parallel_do,
+Directive::OMPD_target_parallel_do_simd,
+Directive::OMPD_target_teams_distribute_parallel_do,
+Directive::OMPD_target_teams_distribute_parallel_do_simd,
+Directive::OMPD_teams_distribute_parallel_do,
+Directive::OMPD_teams_distribute_parallel_do_simd,
+};
+
+OmpDirectiveSet llvm::omp::topDoSet{
+Directive::OMPD_do,
+Directive::OMPD_do_simd,
+};
+
+OmpDirectiveSet llvm::omp::allDoSet{
+Directive::OMPD_distribute_parallel_do,
+Directive::OMPD_distribute_parallel_do_simd,
+Directive::OMPD_parallel_do,
+Directive::OMPD_parallel_do_simd,
+Directive::OMPD_do,
+Directive::OMPD_do_simd,
+Directive::OMPD_target_parallel_do,
+Directive::OMPD_target_parallel_do_simd,
+Directive::OMPD_target_teams_distribute_parallel_do,
+Directive::OMPD_target_teams_distribute_parallel_do_simd,
+Directive::OMPD_teams_distribute_parallel_do,
+Directive::OMPD_teams_distribute_parallel_do_simd,
+};
+
+OmpDirectiveSet llvm::omp::topTaskloopSet{
+Directive::OMPD_taskloop,
+Directive::OMPD_taskloop_simd,
+};
+
+OmpDirectiveSet llvm::omp::allTaskloopSet = llvm::omp::topTaskloopSet;
+
+OmpDirectiveSet llvm::omp::topTargetSet{
+Directive::OMPD_target,
+Directive::OMPD_target_parallel,
+Directive::OMPD_target_parallel_do,
+Directive::OMPD_target_parallel_do_simd,
+Directive::OMPD_target_simd,
+Directive::OMPD_target_teams,
+Directive::OMPD_target_teams_distribute,
+Directive::OMPD_target_teams_distribute_parallel_do,
+Directive::OMPD_target_teams_distribute_parallel_do_simd,
+Directive::OMPD_target_teams_distribute_simd,
+};
+
+OmpDirectiveSet llvm::omp::allTargetSet = llvm::omp::topTarg

[PATCH] D157090: [Flang][Sema] Move directive sets to a shared location

2023-08-04 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 547181.
skatrak added a comment.

Add missing newline at end of file.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157090/new/

https://reviews.llvm.org/D157090

Files:
  clang/docs/tools/clang-formatted-files.txt
  flang/include/flang/Semantics/openmp-directive-sets.h
  flang/lib/Semantics/CMakeLists.txt
  flang/lib/Semantics/check-omp-structure.cpp
  flang/lib/Semantics/check-omp-structure.h
  flang/lib/Semantics/openmp-directive-sets.cpp
  flang/lib/Semantics/resolve-directives.cpp

Index: flang/lib/Semantics/resolve-directives.cpp
===
--- flang/lib/Semantics/resolve-directives.cpp
+++ flang/lib/Semantics/resolve-directives.cpp
@@ -1345,7 +1345,7 @@
 if (targetIt == dirContext_.rend()) {
   return;
 }
-if (llvm::omp::parallelSet.test(targetIt->directive) ||
+if (llvm::omp::allParallelSet.test(targetIt->directive) ||
 llvm::omp::taskGeneratingSet.test(targetIt->directive)) {
   break;
 }
@@ -1446,7 +1446,7 @@
 return;
   }
   Symbol::Flag ivDSA;
-  if (!llvm::omp::simdSet.test(GetContext().directive)) {
+  if (!llvm::omp::allSimdSet.test(GetContext().directive)) {
 ivDSA = Symbol::Flag::OmpPrivate;
   } else if (level == 1) {
 ivDSA = Symbol::Flag::OmpLinear;
Index: flang/lib/Semantics/openmp-directive-sets.cpp
===
--- /dev/null
+++ flang/lib/Semantics/openmp-directive-sets.cpp
@@ -0,0 +1,270 @@
+//===-- lib/Semantics/openmp-directive-sets.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "flang/Semantics/openmp-directive-sets.h"
+
+//===--===//
+// Single directives
+//===--===//
+
+OmpDirectiveSet llvm::omp::topParallelSet{
+Directive::OMPD_parallel,
+Directive::OMPD_parallel_do,
+Directive::OMPD_parallel_do_simd,
+Directive::OMPD_parallel_sections,
+Directive::OMPD_parallel_workshare,
+};
+
+OmpDirectiveSet llvm::omp::allParallelSet{
+Directive::OMPD_distribute_parallel_do,
+Directive::OMPD_distribute_parallel_do_simd,
+Directive::OMPD_parallel,
+Directive::OMPD_parallel_do,
+Directive::OMPD_parallel_do_simd,
+Directive::OMPD_parallel_sections,
+Directive::OMPD_parallel_workshare,
+Directive::OMPD_target_parallel,
+Directive::OMPD_target_parallel_do,
+Directive::OMPD_target_parallel_do_simd,
+Directive::OMPD_target_teams_distribute_parallel_do,
+Directive::OMPD_target_teams_distribute_parallel_do_simd,
+Directive::OMPD_teams_distribute_parallel_do,
+Directive::OMPD_teams_distribute_parallel_do_simd,
+};
+
+OmpDirectiveSet llvm::omp::topDoSet{
+Directive::OMPD_do,
+Directive::OMPD_do_simd,
+};
+
+OmpDirectiveSet llvm::omp::allDoSet{
+Directive::OMPD_distribute_parallel_do,
+Directive::OMPD_distribute_parallel_do_simd,
+Directive::OMPD_parallel_do,
+Directive::OMPD_parallel_do_simd,
+Directive::OMPD_do,
+Directive::OMPD_do_simd,
+Directive::OMPD_target_parallel_do,
+Directive::OMPD_target_parallel_do_simd,
+Directive::OMPD_target_teams_distribute_parallel_do,
+Directive::OMPD_target_teams_distribute_parallel_do_simd,
+Directive::OMPD_teams_distribute_parallel_do,
+Directive::OMPD_teams_distribute_parallel_do_simd,
+};
+
+OmpDirectiveSet llvm::omp::topTaskloopSet{
+Directive::OMPD_taskloop,
+Directive::OMPD_taskloop_simd,
+};
+
+OmpDirectiveSet llvm::omp::allTaskloopSet = llvm::omp::topTaskloopSet;
+
+OmpDirectiveSet llvm::omp::topTargetSet{
+Directive::OMPD_target,
+Directive::OMPD_target_parallel,
+Directive::OMPD_target_parallel_do,
+Directive::OMPD_target_parallel_do_simd,
+Directive::OMPD_target_simd,
+Directive::OMPD_target_teams,
+Directive::OMPD_target_teams_distribute,
+Directive::OMPD_target_teams_distribute_parallel_do,
+Directive::OMPD_target_teams_distribute_parallel_do_simd,
+Directive::OMPD_target_teams_distribute_simd,
+};
+
+OmpDirectiveSet llvm::omp::allTargetSet = llvm::omp::topTargetSet;
+
+OmpDirectiveSet llvm::omp::topSimdSet{
+Directive::OMPD_simd,
+};
+
+OmpDirectiveSet llvm::omp::allSimdSet{
+Directive::OMPD_distribute_parallel_do_simd,
+Directive::OMPD_distribute_simd,
+Directive::OMPD_do_simd,
+Directive::OMPD_parallel_do_simd,
+Directive::OMPD_simd,
+Directive::OMPD_target_parallel_do_simd,
+Directive::OMPD_target_simd,
+Directive::OMPD_target_teams_distribute_parallel_do_simd,
+Di

[PATCH] D157090: [Flang][Sema] Move directive sets to a shared location

2023-08-07 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 547686.
skatrak added a comment.

Declare sets as `inline` inside of header file.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157090/new/

https://reviews.llvm.org/D157090

Files:
  clang/docs/tools/clang-formatted-files.txt
  flang/include/flang/Semantics/openmp-directive-sets.h
  flang/lib/Semantics/check-omp-structure.cpp
  flang/lib/Semantics/check-omp-structure.h
  flang/lib/Semantics/resolve-directives.cpp

Index: flang/lib/Semantics/resolve-directives.cpp
===
--- flang/lib/Semantics/resolve-directives.cpp
+++ flang/lib/Semantics/resolve-directives.cpp
@@ -1345,7 +1345,7 @@
 if (targetIt == dirContext_.rend()) {
   return;
 }
-if (llvm::omp::parallelSet.test(targetIt->directive) ||
+if (llvm::omp::allParallelSet.test(targetIt->directive) ||
 llvm::omp::taskGeneratingSet.test(targetIt->directive)) {
   break;
 }
@@ -1446,7 +1446,7 @@
 return;
   }
   Symbol::Flag ivDSA;
-  if (!llvm::omp::simdSet.test(GetContext().directive)) {
+  if (!llvm::omp::allSimdSet.test(GetContext().directive)) {
 ivDSA = Symbol::Flag::OmpPrivate;
   } else if (level == 1) {
 ivDSA = Symbol::Flag::OmpLinear;
Index: flang/lib/Semantics/check-omp-structure.h
===
--- flang/lib/Semantics/check-omp-structure.h
+++ flang/lib/Semantics/check-omp-structure.h
@@ -17,12 +17,10 @@
 #include "check-directive-structure.h"
 #include "flang/Common/enum-set.h"
 #include "flang/Parser/parse-tree.h"
+#include "flang/Semantics/openmp-directive-sets.h"
 #include "flang/Semantics/semantics.h"
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
 
-using OmpDirectiveSet = Fortran::common::EnumSet;
-
 using OmpClauseSet =
 Fortran::common::EnumSet;
 
@@ -31,74 +29,6 @@
 
 namespace llvm {
 namespace omp {
-static OmpDirectiveSet parallelSet{Directive::OMPD_distribute_parallel_do,
-Directive::OMPD_distribute_parallel_do_simd, Directive::OMPD_parallel,
-Directive::OMPD_parallel_do, Directive::OMPD_parallel_do_simd,
-Directive::OMPD_parallel_sections, Directive::OMPD_parallel_workshare,
-Directive::OMPD_target_parallel, Directive::OMPD_target_parallel_do,
-Directive::OMPD_target_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_parallel_do,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_parallel_do,
-Directive::OMPD_teams_distribute_parallel_do_simd};
-static OmpDirectiveSet doSet{Directive::OMPD_distribute_parallel_do,
-Directive::OMPD_distribute_parallel_do_simd, Directive::OMPD_parallel_do,
-Directive::OMPD_parallel_do_simd, Directive::OMPD_do,
-Directive::OMPD_do_simd, Directive::OMPD_target_parallel_do,
-Directive::OMPD_target_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_parallel_do,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_parallel_do,
-Directive::OMPD_teams_distribute_parallel_do_simd};
-static OmpDirectiveSet doSimdSet{Directive::OMPD_distribute_parallel_do_simd,
-Directive::OMPD_parallel_do_simd, Directive::OMPD_do_simd,
-Directive::OMPD_target_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_parallel_do_simd};
-static OmpDirectiveSet workShareSet{
-OmpDirectiveSet{Directive::OMPD_workshare,
-Directive::OMPD_parallel_workshare, Directive::OMPD_parallel_sections,
-Directive::OMPD_sections, Directive::OMPD_single} |
-doSet};
-static OmpDirectiveSet taskloopSet{
-Directive::OMPD_taskloop, Directive::OMPD_taskloop_simd};
-static OmpDirectiveSet targetSet{Directive::OMPD_target,
-Directive::OMPD_target_parallel, Directive::OMPD_target_parallel_do,
-Directive::OMPD_target_parallel_do_simd, Directive::OMPD_target_simd,
-Directive::OMPD_target_teams, Directive::OMPD_target_teams_distribute,
-Directive::OMPD_target_teams_distribute_parallel_do,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_simd};
-static OmpDirectiveSet simdSet{Directive::OMPD_distribute_parallel_do_simd,
-Directive::OMPD_distribute_simd, Directive::OMPD_do_simd,
-Directive::OMPD_parallel_do_simd, Directive::OMPD_simd,
-Directive::OMPD_target_parallel_do_simd, Directive::OMPD_target_simd,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_simd, Directive::OMPD_taskloop_simd,
-Directive::OMPD_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_simd};
-static OmpDirectiveSet teamSet{Directive::OMPD_teams,
-Directive::OMPD_teams_distribute,
-Directive::OMPD_teams_distribute_parallel_do,
-Directive::OMPD_teams_distribute_parallel_do_simd,
-Directive::OMPD_

[PATCH] D157090: [Flang][Sema] Move directive sets to a shared location

2023-08-07 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added a comment.

In D157090#4562088 , 
@kiranchandramohan wrote:

> This looks OK. The only concern is whether we will lose the ability to inline 
> the code for set membership. Can these sets be put in a header file with 
> `inline constexpr`?

Thank you for the suggestion. It's not possible to declare these as `constexpr` 
because `EnumSet::set`, called by the constructor, is not `constexpr` as well. 
But I have made them `const inline` and defined them in the header file. I hope 
that addresses your concerns.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157090/new/

https://reviews.llvm.org/D157090

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157090: [Flang][Sema] Move directive sets to a shared location

2023-08-07 Thread Sergio Afonso via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGec70627dd177: [Flang][Sema] Move directive sets to a shared 
location (authored by skatrak).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157090/new/

https://reviews.llvm.org/D157090

Files:
  clang/docs/tools/clang-formatted-files.txt
  flang/include/flang/Semantics/openmp-directive-sets.h
  flang/lib/Semantics/check-omp-structure.cpp
  flang/lib/Semantics/check-omp-structure.h
  flang/lib/Semantics/resolve-directives.cpp

Index: flang/lib/Semantics/resolve-directives.cpp
===
--- flang/lib/Semantics/resolve-directives.cpp
+++ flang/lib/Semantics/resolve-directives.cpp
@@ -1362,7 +1362,7 @@
 if (targetIt == dirContext_.rend()) {
   return;
 }
-if (llvm::omp::parallelSet.test(targetIt->directive) ||
+if (llvm::omp::allParallelSet.test(targetIt->directive) ||
 llvm::omp::taskGeneratingSet.test(targetIt->directive)) {
   break;
 }
@@ -1463,7 +1463,7 @@
 return;
   }
   Symbol::Flag ivDSA;
-  if (!llvm::omp::simdSet.test(GetContext().directive)) {
+  if (!llvm::omp::allSimdSet.test(GetContext().directive)) {
 ivDSA = Symbol::Flag::OmpPrivate;
   } else if (level == 1) {
 ivDSA = Symbol::Flag::OmpLinear;
Index: flang/lib/Semantics/check-omp-structure.h
===
--- flang/lib/Semantics/check-omp-structure.h
+++ flang/lib/Semantics/check-omp-structure.h
@@ -17,12 +17,10 @@
 #include "check-directive-structure.h"
 #include "flang/Common/enum-set.h"
 #include "flang/Parser/parse-tree.h"
+#include "flang/Semantics/openmp-directive-sets.h"
 #include "flang/Semantics/semantics.h"
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
 
-using OmpDirectiveSet = Fortran::common::EnumSet;
-
 using OmpClauseSet =
 Fortran::common::EnumSet;
 
@@ -31,74 +29,6 @@
 
 namespace llvm {
 namespace omp {
-static OmpDirectiveSet parallelSet{Directive::OMPD_distribute_parallel_do,
-Directive::OMPD_distribute_parallel_do_simd, Directive::OMPD_parallel,
-Directive::OMPD_parallel_do, Directive::OMPD_parallel_do_simd,
-Directive::OMPD_parallel_sections, Directive::OMPD_parallel_workshare,
-Directive::OMPD_target_parallel, Directive::OMPD_target_parallel_do,
-Directive::OMPD_target_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_parallel_do,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_parallel_do,
-Directive::OMPD_teams_distribute_parallel_do_simd};
-static OmpDirectiveSet doSet{Directive::OMPD_distribute_parallel_do,
-Directive::OMPD_distribute_parallel_do_simd, Directive::OMPD_parallel_do,
-Directive::OMPD_parallel_do_simd, Directive::OMPD_do,
-Directive::OMPD_do_simd, Directive::OMPD_target_parallel_do,
-Directive::OMPD_target_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_parallel_do,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_parallel_do,
-Directive::OMPD_teams_distribute_parallel_do_simd};
-static OmpDirectiveSet doSimdSet{Directive::OMPD_distribute_parallel_do_simd,
-Directive::OMPD_parallel_do_simd, Directive::OMPD_do_simd,
-Directive::OMPD_target_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_parallel_do_simd};
-static OmpDirectiveSet workShareSet{
-OmpDirectiveSet{Directive::OMPD_workshare,
-Directive::OMPD_parallel_workshare, Directive::OMPD_parallel_sections,
-Directive::OMPD_sections, Directive::OMPD_single} |
-doSet};
-static OmpDirectiveSet taskloopSet{
-Directive::OMPD_taskloop, Directive::OMPD_taskloop_simd};
-static OmpDirectiveSet targetSet{Directive::OMPD_target,
-Directive::OMPD_target_parallel, Directive::OMPD_target_parallel_do,
-Directive::OMPD_target_parallel_do_simd, Directive::OMPD_target_simd,
-Directive::OMPD_target_teams, Directive::OMPD_target_teams_distribute,
-Directive::OMPD_target_teams_distribute_parallel_do,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_simd};
-static OmpDirectiveSet simdSet{Directive::OMPD_distribute_parallel_do_simd,
-Directive::OMPD_distribute_simd, Directive::OMPD_do_simd,
-Directive::OMPD_parallel_do_simd, Directive::OMPD_simd,
-Directive::OMPD_target_parallel_do_simd, Directive::OMPD_target_simd,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_simd, Directive::OMPD_taskloop_simd,
-Directive::OMPD_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_simd};
-static OmpDirectiveSet teamSet{Directive::OMPD_teams,
-Directive::OMPD_teams_distribute,
-Di

[PATCH] D157493: Revert "Revert "[Flang][Sema] Move directive sets to a shared location""

2023-08-09 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak created this revision.
skatrak added a reviewer: kiranchandramohan.
Herald added a reviewer: sscalpone.
Herald added projects: Flang, All.
skatrak requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: cfe-commits, jplehr, sstefan1, jdoerfert.
Herald added a project: clang.

This reverts commit f48969f90769f37e042025dba6c544eeddd6d3e6 
.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157493

Files:
  clang/docs/tools/clang-formatted-files.txt
  flang/include/flang/Semantics/openmp-directive-sets.h
  flang/lib/Semantics/check-omp-structure.cpp
  flang/lib/Semantics/check-omp-structure.h
  flang/lib/Semantics/resolve-directives.cpp

Index: flang/lib/Semantics/resolve-directives.cpp
===
--- flang/lib/Semantics/resolve-directives.cpp
+++ flang/lib/Semantics/resolve-directives.cpp
@@ -1370,7 +1370,7 @@
 if (targetIt == dirContext_.rend()) {
   return;
 }
-if (llvm::omp::parallelSet.test(targetIt->directive) ||
+if (llvm::omp::allParallelSet.test(targetIt->directive) ||
 llvm::omp::taskGeneratingSet.test(targetIt->directive)) {
   break;
 }
@@ -1471,7 +1471,7 @@
 return;
   }
   Symbol::Flag ivDSA;
-  if (!llvm::omp::simdSet.test(GetContext().directive)) {
+  if (!llvm::omp::allSimdSet.test(GetContext().directive)) {
 ivDSA = Symbol::Flag::OmpPrivate;
   } else if (level == 1) {
 ivDSA = Symbol::Flag::OmpLinear;
Index: flang/lib/Semantics/check-omp-structure.h
===
--- flang/lib/Semantics/check-omp-structure.h
+++ flang/lib/Semantics/check-omp-structure.h
@@ -17,12 +17,10 @@
 #include "check-directive-structure.h"
 #include "flang/Common/enum-set.h"
 #include "flang/Parser/parse-tree.h"
+#include "flang/Semantics/openmp-directive-sets.h"
 #include "flang/Semantics/semantics.h"
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
 
-using OmpDirectiveSet = Fortran::common::EnumSet;
-
 using OmpClauseSet =
 Fortran::common::EnumSet;
 
@@ -31,74 +29,6 @@
 
 namespace llvm {
 namespace omp {
-static OmpDirectiveSet parallelSet{Directive::OMPD_distribute_parallel_do,
-Directive::OMPD_distribute_parallel_do_simd, Directive::OMPD_parallel,
-Directive::OMPD_parallel_do, Directive::OMPD_parallel_do_simd,
-Directive::OMPD_parallel_sections, Directive::OMPD_parallel_workshare,
-Directive::OMPD_target_parallel, Directive::OMPD_target_parallel_do,
-Directive::OMPD_target_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_parallel_do,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_parallel_do,
-Directive::OMPD_teams_distribute_parallel_do_simd};
-static OmpDirectiveSet doSet{Directive::OMPD_distribute_parallel_do,
-Directive::OMPD_distribute_parallel_do_simd, Directive::OMPD_parallel_do,
-Directive::OMPD_parallel_do_simd, Directive::OMPD_do,
-Directive::OMPD_do_simd, Directive::OMPD_target_parallel_do,
-Directive::OMPD_target_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_parallel_do,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_parallel_do,
-Directive::OMPD_teams_distribute_parallel_do_simd};
-static OmpDirectiveSet doSimdSet{Directive::OMPD_distribute_parallel_do_simd,
-Directive::OMPD_parallel_do_simd, Directive::OMPD_do_simd,
-Directive::OMPD_target_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_parallel_do_simd};
-static OmpDirectiveSet workShareSet{
-OmpDirectiveSet{Directive::OMPD_workshare,
-Directive::OMPD_parallel_workshare, Directive::OMPD_parallel_sections,
-Directive::OMPD_sections, Directive::OMPD_single} |
-doSet};
-static OmpDirectiveSet taskloopSet{
-Directive::OMPD_taskloop, Directive::OMPD_taskloop_simd};
-static OmpDirectiveSet targetSet{Directive::OMPD_target,
-Directive::OMPD_target_parallel, Directive::OMPD_target_parallel_do,
-Directive::OMPD_target_parallel_do_simd, Directive::OMPD_target_simd,
-Directive::OMPD_target_teams, Directive::OMPD_target_teams_distribute,
-Directive::OMPD_target_teams_distribute_parallel_do,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_simd};
-static OmpDirectiveSet simdSet{Directive::OMPD_distribute_parallel_do_simd,
-Directive::OMPD_distribute_simd, Directive::OMPD_do_simd,
-Directive::OMPD_parallel_do_simd, Directive::OMPD_simd,
-Directive::OMPD_target_parallel_do_simd, Directive::OMPD_target_simd,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_simd, Directive::OMPD_taskloop_simd,
-Directive::OMPD_teams_distribute_parallel_do_s

[PATCH] D157493: Revert "Revert "[Flang][Sema] Move directive sets to a shared location""

2023-08-10 Thread Sergio Afonso via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe5a524b8b5e9: Revert "Revert "[Flang][Sema] Move 
directive sets to a shared location"" (authored by skatrak).

Changed prior to commit:
  https://reviews.llvm.org/D157493?vs=548561&id=548962#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157493/new/

https://reviews.llvm.org/D157493

Files:
  clang/docs/tools/clang-formatted-files.txt
  flang/include/flang/Semantics/openmp-directive-sets.h
  flang/lib/Semantics/check-omp-structure.cpp
  flang/lib/Semantics/check-omp-structure.h
  flang/lib/Semantics/resolve-directives.cpp

Index: flang/lib/Semantics/resolve-directives.cpp
===
--- flang/lib/Semantics/resolve-directives.cpp
+++ flang/lib/Semantics/resolve-directives.cpp
@@ -1370,7 +1370,7 @@
 if (targetIt == dirContext_.rend()) {
   return;
 }
-if (llvm::omp::parallelSet.test(targetIt->directive) ||
+if (llvm::omp::allParallelSet.test(targetIt->directive) ||
 llvm::omp::taskGeneratingSet.test(targetIt->directive)) {
   break;
 }
@@ -1471,7 +1471,7 @@
 return;
   }
   Symbol::Flag ivDSA;
-  if (!llvm::omp::simdSet.test(GetContext().directive)) {
+  if (!llvm::omp::allSimdSet.test(GetContext().directive)) {
 ivDSA = Symbol::Flag::OmpPrivate;
   } else if (level == 1) {
 ivDSA = Symbol::Flag::OmpLinear;
@@ -1866,7 +1866,7 @@
   "clauses on a TARGET DATA construct"_err_en_US,
   symbol->name());
 }
-if (llvm::omp::distributeSet.test(GetContext().directive) &&
+if (llvm::omp::allDistributeSet.test(GetContext().directive) &&
 (((ompFlag == Symbol::Flag::OmpFirstPrivate) &&
  symbol->test(Symbol::Flag::OmpLastPrivate)) ||
 ((ompFlag == Symbol::Flag::OmpLastPrivate) &&
Index: flang/lib/Semantics/check-omp-structure.h
===
--- flang/lib/Semantics/check-omp-structure.h
+++ flang/lib/Semantics/check-omp-structure.h
@@ -17,12 +17,10 @@
 #include "check-directive-structure.h"
 #include "flang/Common/enum-set.h"
 #include "flang/Parser/parse-tree.h"
+#include "flang/Semantics/openmp-directive-sets.h"
 #include "flang/Semantics/semantics.h"
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
 
-using OmpDirectiveSet = Fortran::common::EnumSet;
-
 using OmpClauseSet =
 Fortran::common::EnumSet;
 
@@ -31,85 +29,6 @@
 
 namespace llvm {
 namespace omp {
-static OmpDirectiveSet parallelSet{Directive::OMPD_distribute_parallel_do,
-Directive::OMPD_distribute_parallel_do_simd, Directive::OMPD_parallel,
-Directive::OMPD_parallel_do, Directive::OMPD_parallel_do_simd,
-Directive::OMPD_parallel_sections, Directive::OMPD_parallel_workshare,
-Directive::OMPD_target_parallel, Directive::OMPD_target_parallel_do,
-Directive::OMPD_target_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_parallel_do,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_parallel_do,
-Directive::OMPD_teams_distribute_parallel_do_simd};
-static OmpDirectiveSet doSet{Directive::OMPD_distribute_parallel_do,
-Directive::OMPD_distribute_parallel_do_simd, Directive::OMPD_parallel_do,
-Directive::OMPD_parallel_do_simd, Directive::OMPD_do,
-Directive::OMPD_do_simd, Directive::OMPD_target_parallel_do,
-Directive::OMPD_target_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_parallel_do,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_parallel_do,
-Directive::OMPD_teams_distribute_parallel_do_simd};
-static OmpDirectiveSet doSimdSet{Directive::OMPD_distribute_parallel_do_simd,
-Directive::OMPD_parallel_do_simd, Directive::OMPD_do_simd,
-Directive::OMPD_target_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_teams_distribute_parallel_do_simd};
-static OmpDirectiveSet workShareSet{
-OmpDirectiveSet{Directive::OMPD_workshare,
-Directive::OMPD_parallel_workshare, Directive::OMPD_parallel_sections,
-Directive::OMPD_sections, Directive::OMPD_single} |
-doSet};
-static OmpDirectiveSet taskloopSet{
-Directive::OMPD_taskloop, Directive::OMPD_taskloop_simd};
-static OmpDirectiveSet targetSet{Directive::OMPD_target,
-Directive::OMPD_target_parallel, Directive::OMPD_target_parallel_do,
-Directive::OMPD_target_parallel_do_simd, Directive::OMPD_target_simd,
-Directive::OMPD_target_teams, Directive::OMPD_target_teams_distribute,
-Directive::OMPD_target_teams_distribute_parallel_do,
-Directive::OMPD_target_teams_distribute_parallel_do_simd,
-Directive::OMPD_target_teams_distribute_simd};
-static OmpDirectiveSet simdSet{Directive::OMPD_dist

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-09-11 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 556436.
skatrak added a comment.

Update patch. Ping for review and unblocking remaining accepted REQUIRES 
patches, thank you!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,11 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -2473,6 +2473,47 @@
  reductionDeclSymbols));
 }
 
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::AbstractConverter &converter,
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+llvm::SmallVectorImpl &symbolAndClause) {
+
+  // The default capture type
+  mlir::omp::DeclareTargetDeviceType deviceType =
+  mlir::omp::DeclareTargetDeviceType::any;
+  const auto &spec = std::get(
+  declareTargetConstruct.t);
+
+  if (const auto *objectList{
+  Fortran::parser::Unwrap(spec.u)}) {
+// Case: declare target(func, var1, var2)
+gatherFuncAndVarSyms(*objectList, mlir::omp::DeclareTargetCaptureClause::to,
+ symbolAndClause);
+  } else if (const auto *clauseList{
+ Fortran::parser::Unwrap(
+ spec.u)}) {
+if (clauseList->v.empty()) {
+  // Case: declare target, implicit capture of function
+  symbolAndClause.emplace_back(
+  mlir::omp::DeclareTargetCaptureClause::to,
+  eval.getOwningProcedure()->getSubprogramSymbol());
+}
+
+ClauseProcessor cp(converter, *clauseList);
+cp.processTo(symbolAndClause);
+cp.processLink(symbolAndClause);
+cp.processDeviceType(deviceType);
+cp.processTODO(
+converter.getCurrentLocation(),
+llvm::omp::Directive::OMPD_declare_target);
+  }
+
+  return deviceType;
+}
+
 //===--===//
 // genOMP() Code generation helper functions
 //===--===//
@@ -3453,35 +3494,8 @@
&declareTargetConstruct) {
   llvm::SmallVector symbolAndClause;
   mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
-
-  // The default capture type
-  mlir::omp::DeclareTargetDeviceType deviceType =
-  mlir::omp::DeclareTargetDeviceType::any;
-  const auto &spec = std::get(
-  declareTargetConstruct.t);
-  if (const auto *objectList{
-  Fortran::parser::Unwrap(spec.u)}) {
-// Case: declare target(func, var1, var2)
-gatherFuncAndVarSyms(*objectList, mlir::omp::DeclareTargetCaptureClause::to,
- symbolAndClause);
-  } else if (const auto *clauseList{
- Fortran::parser::Unwrap(
- spec.u)}) {
-if (clauseList->v.empty()) {
-  // Case: declare target, implicit capture of function
-  symbolAndClause.emplace_back(
-  mlir::omp::DeclareTargetCaptureClause::to,
-  eval.getOwningProcedure()->getSubprogramSymbol());
-}
-
-ClauseProcessor cp(converter, *clauseList);
-cp.processTo(symbolAndClause);
-cp.processLink(symbolAndClause);
-cp.processDeviceType(deviceType);
-cp.processTODO(
-converter.getCurrentLocation(),
-llvm::omp::Directive::OMPD_d

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-09-12 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 556542.
skatrak marked 3 inline comments as done.
skatrak added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/Todo/requires-unnamed-common.f90
  flang/test/Lower/OpenMP/requires-common.f90
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,14 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+  !$omp target
+  !$omp end target
+end program requires
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,14 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/test/Lower/OpenMP/requires-common.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-common.f90
@@ -0,0 +1,19 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+block data init
+  !$omp requires unified_shared_memory
+  integer :: x
+  common /block/ x
+  data x / 10 /
+end
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/Todo/requires-unnamed-common.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/Todo/requires-unnamed-common.f90
@@ -0,0 +1,25 @@
+! This test checks the lowering of REQUIRES inside of an unnamed BLOCK DATA.
+! The symbol of the `symTab` scope of the `BlockDataUnit` PFT node is null in
+! this case, resulting in the inability to store the REQUIRES flags gathered in
+! it.
+
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+! XFAIL: *
+
+! The string "EXPECTED" denotes the expected FIR
+
+!CHECK: module attributes {
+!CHECK-SAME:omp.requires = #omp
+block data
+  !$omp requires unified_shared_memory
+  integer :: x
+  common /block/ x
+  data x / 10 /
+end
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -78,9 +78,7 @@
 static void gatherFuncAndVarSyms(
 const Fortran::parser::OmpObjectList &objList,
 mlir::omp::DeclareTargetCaptureClause clause,
-llvm::SmallVectorImpl>
-&symbolAndClause) {
+llvm::SmallVectorImpl &symbolAndClause) {
   for (const Fortran::parser::OmpObject &ompObject : objList.v) {
 Fortran::common::visit(
 Fortran::common::visitors{
@@ -2474,6 +2472,71 @@
  reductionDeclSymbols));
 }
 
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::AbstractConverter &converter,
+Fortran::lower::pft::Evaluation &eval,
+

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-09-12 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added a comment.

Thank you Kiran for having a look at this!

In D147218#4643257 , 
@kiranchandramohan wrote:

> Could you add to the summary that the `atomic` related handling is done 
> elsewhere.

Done.

> Could you expand the tests to cover the various `if` conditions that are used 
> in the code?

I created a test to gather the flags from the symbol for a `FunctionLikeUnit` 
and for a `BlockDataUnit`, and checked that these flags are only added for the 
module when there's a device construct in the compilation unit, regardless of 
whether we're compiling for host or device and whether the device construct is 
a target region or a declare target subroutine. I think that covers all main 
aspects of this patch.

I have also detected that there is a situation where this approach doesn't 
currently work, so I created an expected-fail test for unnamed block data. The 
issue is that there is no symbol created for those, so there's nowhere to 
store/find these flags. I have seen this happen for main programs as well, but 
I've not been able to create a reproducer for which that happened that 
contained device constructs. In my opinion, this edge case should be addressed 
as its own patch.




Comment at: flang/include/flang/Lower/OpenMP.h:16
 
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include 

kiranchandramohan wrote:
> Is this include required here?
It was, but only because I had made public a function that wasn't supposed to 
be, thanks for noticing.



Comment at: flang/lib/Lower/Bridge.cpp:4779
+if (ompDeviceCodeFound)
+  Fortran::lower::genOpenMPRequires(getModuleOp().getOperation(),
+globalOmpRequiresSymbol);

kiranchandramohan wrote:
> If this is specific for device code, might be worth renaming it to something 
> specific to device.
This is for both host and device, it's just that it's only generated if there 
are device constructs in the compilation unit. Let me know if you still suggest 
renaming it to something else.

I modified the unit tests to run them for the target device as well, so that 
it's clear that the same behavior is expected.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-09-12 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 556543.
skatrak added a comment.

Remove leftover comment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/Todo/requires-unnamed-common.f90
  flang/test/Lower/OpenMP/requires-common.f90
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,14 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+  !$omp target
+  !$omp end target
+end program requires
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,14 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/test/Lower/OpenMP/requires-common.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-common.f90
@@ -0,0 +1,19 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+block data init
+  !$omp requires unified_shared_memory
+  integer :: x
+  common /block/ x
+  data x / 10 /
+end
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/Todo/requires-unnamed-common.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/Todo/requires-unnamed-common.f90
@@ -0,0 +1,23 @@
+! This test checks the lowering of REQUIRES inside of an unnamed BLOCK DATA.
+! The symbol of the `symTab` scope of the `BlockDataUnit` PFT node is null in
+! this case, resulting in the inability to store the REQUIRES flags gathered in
+! it.
+
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+! XFAIL: *
+
+!CHECK: module attributes {
+!CHECK-SAME:omp.requires = #omp
+block data
+  !$omp requires unified_shared_memory
+  integer :: x
+  common /block/ x
+  data x / 10 /
+end
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -78,9 +78,7 @@
 static void gatherFuncAndVarSyms(
 const Fortran::parser::OmpObjectList &objList,
 mlir::omp::DeclareTargetCaptureClause clause,
-llvm::SmallVectorImpl>
-&symbolAndClause) {
+llvm::SmallVectorImpl &symbolAndClause) {
   for (const Fortran::parser::OmpObject &ompObject : objList.v) {
 Fortran::common::visit(
 Fortran::common::visitors{
@@ -2474,6 +2472,71 @@
  reductionDeclSymbols));
 }
 
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::AbstractConverter &converter,
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+llvm::Sm

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-09-13 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added a comment.

Thank you for the review. After I address your last comment my plan is to land 
this and the other two accepted REQUIRES patches that depended on it.

Is there a preferred approach as to how to go about it? I could rebase them all 
and wait until the pre-merge builds finish without errors and then land them in 
quick succession or I could go one by one to make sure post-merge builds don't 
find any issues before landing the next, which will be over a couple of days 
most likely.




Comment at: flang/lib/Lower/Bridge.cpp:2366-2367
 mlir::OpBuilder::InsertPoint insertPt = builder->saveInsertionPoint();
+analyzeOpenMPDeclarativeConstruct(*this, getEval(), ompDecl,
+  ompDeviceCodeFound);
 genOpenMPDeclarativeConstruct(*this, getEval(), ompDecl);

kiranchandramohan wrote:
> Can this be rewritten this way.
> 
> And rename `analyzeOpenMPDeclarativeConstruct` to `isTargetDeclare` or 
> something like that?
My idea was to make it generic to allow the analysis of other declarative 
constructs in the future there as well, that's the reason for the function name 
and returning through an output argument. But I'll follow your suggestion, 
since we don't know for sure that we'll need this later on.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-09-13 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 556655.
skatrak added a comment.

Rebase and address review comment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/Todo/requires-unnamed-common.f90
  flang/test/Lower/OpenMP/requires-common.f90
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,14 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+  !$omp target
+  !$omp end target
+end program requires
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,14 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/test/Lower/OpenMP/requires-common.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-common.f90
@@ -0,0 +1,19 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+block data init
+  !$omp requires unified_shared_memory
+  integer :: x
+  common /block/ x
+  data x / 10 /
+end
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/Todo/requires-unnamed-common.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/Todo/requires-unnamed-common.f90
@@ -0,0 +1,23 @@
+! This test checks the lowering of REQUIRES inside of an unnamed BLOCK DATA.
+! The symbol of the `symTab` scope of the `BlockDataUnit` PFT node is null in
+! this case, resulting in the inability to store the REQUIRES flags gathered in
+! it.
+
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+! XFAIL: *
+
+!CHECK: module attributes {
+!CHECK-SAME:omp.requires = #omp
+block data
+  !$omp requires unified_shared_memory
+  integer :: x
+  common /block/ x
+  data x / 10 /
+end
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -78,9 +78,7 @@
 static void gatherFuncAndVarSyms(
 const Fortran::parser::OmpObjectList &objList,
 mlir::omp::DeclareTargetCaptureClause clause,
-llvm::SmallVectorImpl>
-&symbolAndClause) {
+llvm::SmallVectorImpl &symbolAndClause) {
   for (const Fortran::parser::OmpObject &ompObject : objList.v) {
 Fortran::common::visit(
 Fortran::common::visitors{
@@ -2474,6 +2472,71 @@
  reductionDeclSymbols));
 }
 
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::AbstractConverter &converter,
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+  

[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-09-13 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 556656.
skatrak added a comment.

Rebase.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

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
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1298,8 +1298,11 @@
 // TODO: set the flags when available
 llvm::OpenMPIRBuilderConfig config(
 isTargetDevice, isGPU,
-/* HasRequiresUnifiedSharedMemory */ false,
-/* OpenMPOffloadMandatory */ false);
+/* OpenMPOffloadMandatory = */ false,
+/* HasRequiresReverseOffload = */ false,
+/* HasRequiresUnifiedAddress = */ false,
+/* HasRequiresUnifiedSharedMemory = */ false,
+/* HasRequiresDynamicAllocators = */ false);
 ompBuilder->setConfig(config);
   }
   return ompBuilder.get();
Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -5128,7 +5128,7 @@
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
   F->setName("func");
   IRBuilder<> Builder(BB);
@@ -5206,7 +5206,8 @@
 
 TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.setConfig(OpenMPIRBuilderConfig(true, false, false, false));
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(true, false, false, false, false, false, false));
   OMPBuilder.initialize();
 
   F->setName("func");
@@ -5896,7 +5897,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);
@@ -5919,7 +5921,7 @@
 TEST_F(OpenMPIRBuilderTest, registerTargetGlobalVariable) {
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
 
   std::vector TargetTriple;
@@ -5996,8 +5998,11 @@
   OMPBuilder.initialize();
   OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ true,
/* IsGPU = */ true,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ false,
+   /* HasRequiresUnifiedAddress = */ false,
/* HasRequiresUnifiedSharedMemory = */ false,
-   /* OpenMPOffloadMandatory = */ false);
+   /* HasRequiresDynamicAllocators = */ false);
   OMPBuilder.setConfig(Config);
 
   FunctionCallee FnTypeAndCallee =
@@ -6033,4 +6038,44 @@
   EXPECT_TRUE(Fn->hasFnAttribute(Attribute::MustProgress));
 }
 
+TEST_F(OpenMPIRBuilderTest, CreateRegisterRequires) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+
+  OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ false,
+   /* IsGPU = */ false,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ true,
+   /* HasRequiresUnifiedAddress = */ false,
+   /* HasRequiresUnifiedSharedMemory = */ true,
+   /* HasRequiresDynamicAllocators = */ false);
+  OMPBuilder.setConfig(Config);
+
+  auto FName =
+  OMPBuilder.createPlatformSpecificName({"omp_offloading", "requires_reg"});
+  EXPECT_EQ(FName, ".omp_offloading.requires_reg");
+
+  Function *Fn = OMPBuilder.createRegisterRequires(FName);
+  EXPECT_NE(Fn, nullptr);
+  EXPECT_EQ(FName, Fn->getName());
+
+  EXPECT_EQ(Fn->getSection(), ".text.startup");
+  EXPECT_TRUE(Fn->hasInternalLinkage());
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoInline));
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoUnwind));
+  EXPECT_EQ(Fn->size(), 1u);
+
+  BasicBlo

[PATCH] D147219: [OpenMP][Flang][MLIR] Lowering of requires directive from MLIR to LLVM IR

2023-09-13 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 556657.
skatrak added a comment.

Rebase.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147219/new/

https://reviews.llvm.org/D147219

Files:
  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

Index: mlir/test/Target/LLVMIR/openmp-llvm.mlir
===
--- mlir/test/Target/LLVMIR/openmp-llvm.mlir
+++ mlir/test/Target/LLVMIR/openmp-llvm.mlir
@@ -2661,3 +2661,12 @@
 // 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} {}
Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1278,32 +1278,18 @@
 llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
   if (!ompBuilder) {
 ompBuilder = std::make_unique(*llvmModule);
+ompBuilder->initialize();
 
-bool isTargetDevice = false, isGPU = false;
-llvm::StringRef hostIRFilePath = "";
-
-if (auto deviceAttr =
-mlirModule->getAttrOfType("omp.is_target_device"))
-  isTargetDevice = deviceAttr.getValue();
-
-if (auto gpuAttr = mlirModule->getAttrOfType("omp.is_gpu"))
-  isGPU = gpuAttr.getValue();
-
-if (auto filepathAttr =
-mlirModule->getAttrOfType("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();
 }
Index: mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ 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 
 
 using namespace mlir;
 
@@ -1165,7 +1168,7 @@
 }
 
 /// Convert an Atomic Ordering attribute to llvm::AtomicOrdering.
-llvm::AtomicOrdering
+static llvm::AtomicOrdering
 convertAtomicOrdering(std::optional ao) {
   if (!ao)
 return llvm::AtomicOrdering::Monotonic; // Default Memory Ordering
@@ -1932,6 +1935,27 @@
   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 diale

[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-09-14 Thread Sergio Afonso via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG094a63a20bf5: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support 
for requires directive (authored by skatrak).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

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
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1298,8 +1298,11 @@
 // TODO: set the flags when available
 llvm::OpenMPIRBuilderConfig config(
 isTargetDevice, isGPU,
-/* HasRequiresUnifiedSharedMemory */ false,
-/* OpenMPOffloadMandatory */ false);
+/* OpenMPOffloadMandatory = */ false,
+/* HasRequiresReverseOffload = */ false,
+/* HasRequiresUnifiedAddress = */ false,
+/* HasRequiresUnifiedSharedMemory = */ false,
+/* HasRequiresDynamicAllocators = */ false);
 ompBuilder->setConfig(config);
   }
   return ompBuilder.get();
Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -5128,7 +5128,7 @@
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
   F->setName("func");
   IRBuilder<> Builder(BB);
@@ -5206,7 +5206,8 @@
 
 TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.setConfig(OpenMPIRBuilderConfig(true, false, false, false));
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(true, false, false, false, false, false, false));
   OMPBuilder.initialize();
 
   F->setName("func");
@@ -5896,7 +5897,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);
@@ -5919,7 +5921,7 @@
 TEST_F(OpenMPIRBuilderTest, registerTargetGlobalVariable) {
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
 
   std::vector TargetTriple;
@@ -5996,8 +5998,11 @@
   OMPBuilder.initialize();
   OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ true,
/* IsGPU = */ true,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ false,
+   /* HasRequiresUnifiedAddress = */ false,
/* HasRequiresUnifiedSharedMemory = */ false,
-   /* OpenMPOffloadMandatory = */ false);
+   /* HasRequiresDynamicAllocators = */ false);
   OMPBuilder.setConfig(Config);
 
   FunctionCallee FnTypeAndCallee =
@@ -6033,4 +6038,44 @@
   EXPECT_TRUE(Fn->hasFnAttribute(Attribute::MustProgress));
 }
 
+TEST_F(OpenMPIRBuilderTest, CreateRegisterRequires) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+
+  OpenMPIRBuilderConfig Config(/* IsTargetDevice = */ false,
+   /* IsGPU = */ false,
+   /* OpenMPOffloadMandatory = */ false,
+   /* HasRequiresReverseOffload = */ true,
+   /* HasRequiresUnifiedAddress = */ false,
+   /* HasRequiresUnifiedSharedMemory = */ true,
+   /* HasRequiresDynamicAllocators = */ false);
+  OMPBuilder.setConfig(Config);
+
+  auto FName =
+  OMPBuilder.createPlatformSpecificName({"omp_offloading", "requires_reg"});
+  EXPECT_EQ(FName, ".omp_offloading.requires_reg");
+
+  Function *Fn = OMPBuilder.createRegisterRequires(FName);
+  EXPECT_NE(Fn, nullptr);
+  EXPECT_EQ(FName, Fn->getName());
+
+  EXPECT_EQ(Fn->getSection(), ".text.startup");
+  EXPECT_TRUE(Fn->hasInternalLinkage());
+  EXPECT_TRUE(Fn->hasFnAttribute(Att

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-09-14 Thread Sergio Afonso via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG29aa749087be: [OpenMP][Flang][MLIR] Lowering of OpenMP 
requires directive from parse tree to… (authored by skatrak).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/Todo/requires-unnamed-common.f90
  flang/test/Lower/OpenMP/requires-common.f90
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,14 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+  !$omp target
+  !$omp end target
+end program requires
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,14 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/test/Lower/OpenMP/requires-common.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-common.f90
@@ -0,0 +1,19 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+block data init
+  !$omp requires unified_shared_memory
+  integer :: x
+  common /block/ x
+  data x / 10 /
+end
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/Todo/requires-unnamed-common.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/Todo/requires-unnamed-common.f90
@@ -0,0 +1,23 @@
+! This test checks the lowering of REQUIRES inside of an unnamed BLOCK DATA.
+! The symbol of the `symTab` scope of the `BlockDataUnit` PFT node is null in
+! this case, resulting in the inability to store the REQUIRES flags gathered in
+! it.
+
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck %s
+! XFAIL: *
+
+!CHECK: module attributes {
+!CHECK-SAME:omp.requires = #omp
+block data
+  !$omp requires unified_shared_memory
+  integer :: x
+  common /block/ x
+  data x / 10 /
+end
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -78,9 +78,7 @@
 static void gatherFuncAndVarSyms(
 const Fortran::parser::OmpObjectList &objList,
 mlir::omp::DeclareTargetCaptureClause clause,
-llvm::SmallVectorImpl>
-&symbolAndClause) {
+llvm::SmallVectorImpl &symbolAndClause) {
   for (const Fortran::parser::OmpObject &ompObject : objList.v) {
 Fortran::common::visit(
 Fortran::common::visitors{
@@ -2453,6 +2451,71 @@
  reductionDeclSymbols));
 }
 
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::AbstractConverter &converter,
+Fortran::lower::pft::

[PATCH] D147219: [OpenMP][Flang][MLIR] Lowering of requires directive from MLIR to LLVM IR

2023-09-14 Thread Sergio Afonso via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG9058762789c0: [OpenMP][Flang][MLIR] Lowering of requires 
directive from MLIR to LLVM IR (authored by skatrak).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147219/new/

https://reviews.llvm.org/D147219

Files:
  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

Index: mlir/test/Target/LLVMIR/openmp-llvm.mlir
===
--- mlir/test/Target/LLVMIR/openmp-llvm.mlir
+++ mlir/test/Target/LLVMIR/openmp-llvm.mlir
@@ -2661,3 +2661,12 @@
 // 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} {}
Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1278,32 +1278,18 @@
 llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
   if (!ompBuilder) {
 ompBuilder = std::make_unique(*llvmModule);
+ompBuilder->initialize();
 
-bool isTargetDevice = false, isGPU = false;
-llvm::StringRef hostIRFilePath = "";
-
-if (auto deviceAttr =
-mlirModule->getAttrOfType("omp.is_target_device"))
-  isTargetDevice = deviceAttr.getValue();
-
-if (auto gpuAttr = mlirModule->getAttrOfType("omp.is_gpu"))
-  isGPU = gpuAttr.getValue();
-
-if (auto filepathAttr =
-mlirModule->getAttrOfType("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();
 }
Index: mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ 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 
 
 using namespace mlir;
 
@@ -1165,7 +1168,7 @@
 }
 
 /// Convert an Atomic Ordering attribute to llvm::AtomicOrdering.
-llvm::AtomicOrdering
+static llvm::AtomicOrdering
 convertAtomicOrdering(std::optional ao) {
   if (!ao)
 return llvm::AtomicOrdering::Monotonic; // Default Memory Ordering
@@ -1932,6 +1935,27 @@
   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::appendToGlobal

[PATCH] D159212: [MLIR] Allow dialects to disable CSE for certain operations

2023-09-18 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak abandoned this revision.
skatrak added a comment.

I'll close this patch now, since the agreed approach has been to try using the 
`IsolatedFromAbove` trait and not to add an interface modifying the CSE pass 
behavior.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D159212/new/

https://reviews.llvm.org/D159212

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D154591: [OpenMP][OMPIRBuilder] Rename IsEmbedded and IsTargetCodegen flags

2023-07-06 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak created this revision.
skatrak added reviewers: jsjodin, jdoerfert, kiranchandramohan.
Herald added subscribers: gysit, Dinistro, bviyer, sunshaoce, Moerafaat, 
zero9178, bzcheeseman, mattd, gchakrabarti, awarzynski, sdasgup3, asavonic, 
wenzhicui, wrengr, cota, teijeong, rdzhabarov, tatianashp, msifontes, jurahul, 
Kayjukh, grosul1, Joonsoo, kerbowa, liufengdb, aartbik, mgester, arpith-jacob, 
csigg, antiagainst, shauheen, rriddle, mehdi_amini, mstorsjo, guansong, 
kbarton, hiraditya, yaxunl, jvesely, nemanjai.
Herald added a reviewer: sscalpone.
Herald added a reviewer: ftynse.
Herald added a reviewer: awarzynski.
Herald added projects: Flang, All.
skatrak requested review of this revision.
Herald added subscribers: llvm-commits, cfe-commits, jplehr, sstefan1, 
stephenneuendorffer, nicolasvasilache, MaskRay, jholewinski.
Herald added a reviewer: nicolasvasilache.
Herald added projects: clang, MLIR, LLVM.

This patch renames the `OpenMPIRBuilderConfig` flags to reduce confusion over
their meaning. `IsTargetCodegen` becomes `IsGPU`, whereas `IsEmbedded` becomes
`IsTargetDevice`. The `-fopenmp-is-device` compiler option is also renamed to
`-fopenmp-is-target-device` and the `omp.is_device` MLIR attribute is renamed
to `omp.is_target_device`. Getters and setters of all these renamed properties
are also updated accordingly. Many unit tests have been updated to use the new
names, but an alias for the `-fopenmp-is-device` option is created so that
external programs do not stop working after the name change.

`IsGPU` is set when the target triple is AMDGCN or NVIDIA PTX, and it is also
valid if `IsTargetDevice` is specified as well. `IsTargetDevice` is set by the
`-fopenmp-is-target-device` compiler frontend option, which is only added to
the OpenMP device invocation for offloading-enabled programs.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154591

Files:
  clang/docs/OffloadingDesign.rst
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/Decl.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/OpenMPClause.cpp
  clang/lib/Basic/Targets/NVPTX.cpp
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.h
  clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
  clang/lib/CodeGen/CGOpenMPRuntimeGPU.h
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CGVTables.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/Targets/NVPTX.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Frontend/CompilerInstance.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Frontend/InitPreprocessor.cpp
  clang/lib/Lex/LiteralSupport.cpp
  clang/lib/Sema/Sema.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaOpenMP.cpp
  clang/lib/Sema/SemaType.cpp
  clang/test/CodeGen/PowerPC/ppc64le-varargs-f128.c
  clang/test/CodeGen/ibm128-unsupported.c
  clang/test/Driver/openmp-offload-gpu.c
  clang/test/Driver/openmp-offload.c
  clang/test/Headers/__clang_hip_libdevice_declares.cpp
  clang/test/Headers/amdgcn-openmp-device-math-complex.c
  clang/test/Headers/amdgcn-openmp-device-math-complex.cpp
  clang/test/Headers/amdgcn_openmp_device_math.c
  clang/test/Headers/amdgcn_openmp_device_math_c.c
  clang/test/Headers/amdgcn_openmp_device_math_constexpr.cpp
  clang/test/Headers/nvptx_device_cmath_functions.c
  clang/test/Headers/nvptx_device_cmath_functions.cpp
  clang/test/Headers/nvptx_device_cmath_functions_cxx17.cpp
  clang/test/Headers/nvptx_device_math_complex.c
  clang/test/Headers/nvptx_device_math_complex.cpp
  clang/test/Headers/nvptx_device_math_functions.c
  clang/test/Headers/nvptx_device_math_functions.cpp
  clang/test/Headers/nvptx_device_math_functions_cxx17.cpp
  clang/test/Headers/nvptx_device_math_macro.cpp
  clang/test/Headers/nvptx_device_math_modf.cpp
  clang/test/Headers/nvptx_device_math_sin.c
  clang/test/Headers/nvptx_device_math_sin.cpp
  clang/test/Headers/nvptx_device_math_sin_cos.cpp
  clang/test/Headers/nvptx_device_math_sincos.cpp
  clang/test/Headers/openmp-device-functions-bool.c
  clang/test/Headers/openmp_device_math_isnan.cpp
  clang/test/Headers/openmp_new_nothrow.cpp
  clang/test/Headers/target_include_new.cpp
  clang/test/OpenMP/amdgcn-attributes.cpp
  clang/test/OpenMP/amdgcn_device_function_call.cpp
  clang/test/OpenMP/amdgcn_ldbl_check.cpp
  clang/test/OpenMP/amdgcn_target_codegen.cpp
  clang/test/OpenMP/amdgcn_target_global_constructor.cpp
  clang/test/OpenMP/amdgcn_target_init_temp_alloca.cpp
  clang/test/OpenMP/amdgpu_target_with_aligned_attribute.c
  clang/test/OpenMP/assumes_include_nvptx.cpp
  clang/test/OpenMP/aux-triple-macros.cpp
  clang/test/OpenMP/declare_target_codegen.cpp
  clang/test/OpenMP/declare_target_codegen_globalization.cpp
  clang/test/OpenMP/declare_target_constexpr_c

[PATCH] D154591: [OpenMP][OMPIRBuilder] Rename IsEmbedded and IsTargetCodegen flags

2023-07-06 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 537768.
skatrak added a comment.

Rebase to try resolving build failure.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154591/new/

https://reviews.llvm.org/D154591

Files:
  clang/docs/OffloadingDesign.rst
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/Decl.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/OpenMPClause.cpp
  clang/lib/Basic/Targets/NVPTX.cpp
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.h
  clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
  clang/lib/CodeGen/CGOpenMPRuntimeGPU.h
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CGVTables.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/Targets/NVPTX.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Frontend/CompilerInstance.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Frontend/InitPreprocessor.cpp
  clang/lib/Lex/LiteralSupport.cpp
  clang/lib/Sema/Sema.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaOpenMP.cpp
  clang/lib/Sema/SemaType.cpp
  clang/test/CodeGen/PowerPC/ppc64le-varargs-f128.c
  clang/test/CodeGen/ibm128-unsupported.c
  clang/test/Driver/openmp-offload-gpu.c
  clang/test/Driver/openmp-offload.c
  clang/test/Headers/__clang_hip_libdevice_declares.cpp
  clang/test/Headers/amdgcn-openmp-device-math-complex.c
  clang/test/Headers/amdgcn-openmp-device-math-complex.cpp
  clang/test/Headers/amdgcn_openmp_device_math.c
  clang/test/Headers/amdgcn_openmp_device_math_c.c
  clang/test/Headers/amdgcn_openmp_device_math_constexpr.cpp
  clang/test/Headers/nvptx_device_cmath_functions.c
  clang/test/Headers/nvptx_device_cmath_functions.cpp
  clang/test/Headers/nvptx_device_cmath_functions_cxx17.cpp
  clang/test/Headers/nvptx_device_math_complex.c
  clang/test/Headers/nvptx_device_math_complex.cpp
  clang/test/Headers/nvptx_device_math_functions.c
  clang/test/Headers/nvptx_device_math_functions.cpp
  clang/test/Headers/nvptx_device_math_functions_cxx17.cpp
  clang/test/Headers/nvptx_device_math_macro.cpp
  clang/test/Headers/nvptx_device_math_modf.cpp
  clang/test/Headers/nvptx_device_math_sin.c
  clang/test/Headers/nvptx_device_math_sin.cpp
  clang/test/Headers/nvptx_device_math_sin_cos.cpp
  clang/test/Headers/nvptx_device_math_sincos.cpp
  clang/test/Headers/openmp-device-functions-bool.c
  clang/test/Headers/openmp_device_math_isnan.cpp
  clang/test/Headers/openmp_new_nothrow.cpp
  clang/test/Headers/target_include_new.cpp
  clang/test/OpenMP/amdgcn-attributes.cpp
  clang/test/OpenMP/amdgcn_device_function_call.cpp
  clang/test/OpenMP/amdgcn_ldbl_check.cpp
  clang/test/OpenMP/amdgcn_target_codegen.cpp
  clang/test/OpenMP/amdgcn_target_global_constructor.cpp
  clang/test/OpenMP/amdgcn_target_init_temp_alloca.cpp
  clang/test/OpenMP/amdgpu_target_with_aligned_attribute.c
  clang/test/OpenMP/assumes_include_nvptx.cpp
  clang/test/OpenMP/aux-triple-macros.cpp
  clang/test/OpenMP/declare_target_codegen.cpp
  clang/test/OpenMP/declare_target_codegen_globalization.cpp
  clang/test/OpenMP/declare_target_constexpr_codegen.cpp
  clang/test/OpenMP/declare_target_link_codegen.cpp
  clang/test/OpenMP/declare_target_messages.cpp
  clang/test/OpenMP/declare_target_only_one_side_compilation.cpp
  clang/test/OpenMP/declare_variant_device_kind_codegen.cpp
  clang/test/OpenMP/declare_variant_mixed_codegen.c
  clang/test/OpenMP/distribute_codegen.cpp
  clang/test/OpenMP/distribute_simd_codegen.cpp
  clang/test/OpenMP/metadirective_device_arch_codegen.cpp
  clang/test/OpenMP/metadirective_device_isa_codegen_amdgcn.cpp
  clang/test/OpenMP/multiple_regions_per_line.cpp
  clang/test/OpenMP/nvptx_NRVO_variable.cpp
  clang/test/OpenMP/nvptx_SPMD_codegen.cpp
  clang/test/OpenMP/nvptx_allocate_codegen.cpp
  clang/test/OpenMP/nvptx_allocate_messages.cpp
  clang/test/OpenMP/nvptx_asm_delayed_diags.c
  clang/test/OpenMP/nvptx_data_sharing.cpp
  clang/test/OpenMP/nvptx_declare_target_var_ctor_dtor_codegen.cpp
  clang/test/OpenMP/nvptx_declare_variant_device_kind_codegen.cpp
  clang/test/OpenMP/nvptx_declare_variant_implementation_vendor_codegen.cpp
  clang/test/OpenMP/nvptx_declare_variant_name_mangling.cpp
  clang/test/OpenMP/nvptx_distribute_parallel_generic_mode_codegen.cpp
  clang/test/OpenMP/nvptx_lambda_capturing.cpp
  clang/test/OpenMP/nvptx_multi_target_parallel_codegen.cpp
  clang/test/OpenMP/nvptx_nested_parallel_codegen.cpp
  clang/test/OpenMP/nvptx_parallel_codegen.cpp
  clang/test/OpenMP/nvptx_parallel_for_codegen.cpp
  clang/test/OpenMP/nvptx_param_translate.c
  clang/test/OpenMP/nvptx_prohibit_thread_local.cpp
  clang/test/OpenMP/nvptx_target_codegen.cpp
  clang/test/OpenMP/nvptx_target_cuda_mode_messages.cpp
  clang/test/OpenMP/nvptx_target_exce

[PATCH] D154591: [OpenMP][OMPIRBuilder] Rename IsEmbedded and IsTargetCodegen flags

2023-07-07 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 538135.
skatrak added a comment.

Rebase and fix issues.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154591/new/

https://reviews.llvm.org/D154591

Files:
  clang/docs/OffloadingDesign.rst
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/Decl.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/OpenMPClause.cpp
  clang/lib/Basic/Targets/NVPTX.cpp
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.h
  clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
  clang/lib/CodeGen/CGOpenMPRuntimeGPU.h
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CGVTables.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/Targets/NVPTX.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Frontend/CompilerInstance.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Frontend/InitPreprocessor.cpp
  clang/lib/Lex/LiteralSupport.cpp
  clang/lib/Sema/Sema.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaOpenMP.cpp
  clang/lib/Sema/SemaType.cpp
  clang/test/CodeGen/PowerPC/ppc64le-varargs-f128.c
  clang/test/CodeGen/ibm128-unsupported.c
  clang/test/Driver/openmp-offload-gpu.c
  clang/test/Driver/openmp-offload.c
  clang/test/Headers/__clang_hip_libdevice_declares.cpp
  clang/test/Headers/amdgcn-openmp-device-math-complex.c
  clang/test/Headers/amdgcn-openmp-device-math-complex.cpp
  clang/test/Headers/amdgcn_openmp_device_math.c
  clang/test/Headers/amdgcn_openmp_device_math_c.c
  clang/test/Headers/amdgcn_openmp_device_math_constexpr.cpp
  clang/test/Headers/nvptx_device_cmath_functions.c
  clang/test/Headers/nvptx_device_cmath_functions.cpp
  clang/test/Headers/nvptx_device_cmath_functions_cxx17.cpp
  clang/test/Headers/nvptx_device_math_complex.c
  clang/test/Headers/nvptx_device_math_complex.cpp
  clang/test/Headers/nvptx_device_math_functions.c
  clang/test/Headers/nvptx_device_math_functions.cpp
  clang/test/Headers/nvptx_device_math_functions_cxx17.cpp
  clang/test/Headers/nvptx_device_math_macro.cpp
  clang/test/Headers/nvptx_device_math_modf.cpp
  clang/test/Headers/nvptx_device_math_sin.c
  clang/test/Headers/nvptx_device_math_sin.cpp
  clang/test/Headers/nvptx_device_math_sin_cos.cpp
  clang/test/Headers/nvptx_device_math_sincos.cpp
  clang/test/Headers/openmp-device-functions-bool.c
  clang/test/Headers/openmp_device_math_isnan.cpp
  clang/test/Headers/openmp_new_nothrow.cpp
  clang/test/Headers/target_include_new.cpp
  clang/test/OpenMP/amdgcn-attributes.cpp
  clang/test/OpenMP/amdgcn_device_function_call.cpp
  clang/test/OpenMP/amdgcn_ldbl_check.cpp
  clang/test/OpenMP/amdgcn_target_codegen.cpp
  clang/test/OpenMP/amdgcn_target_device_vla.cpp
  clang/test/OpenMP/amdgcn_target_global_constructor.cpp
  clang/test/OpenMP/amdgcn_target_init_temp_alloca.cpp
  clang/test/OpenMP/amdgpu_target_with_aligned_attribute.c
  clang/test/OpenMP/assumes_include_nvptx.cpp
  clang/test/OpenMP/aux-triple-macros.cpp
  clang/test/OpenMP/declare_target_codegen.cpp
  clang/test/OpenMP/declare_target_codegen_globalization.cpp
  clang/test/OpenMP/declare_target_constexpr_codegen.cpp
  clang/test/OpenMP/declare_target_link_codegen.cpp
  clang/test/OpenMP/declare_target_messages.cpp
  clang/test/OpenMP/declare_target_only_one_side_compilation.cpp
  clang/test/OpenMP/declare_variant_device_kind_codegen.cpp
  clang/test/OpenMP/declare_variant_mixed_codegen.c
  clang/test/OpenMP/distribute_codegen.cpp
  clang/test/OpenMP/distribute_simd_codegen.cpp
  clang/test/OpenMP/metadirective_device_arch_codegen.cpp
  clang/test/OpenMP/metadirective_device_isa_codegen_amdgcn.cpp
  clang/test/OpenMP/multiple_regions_per_line.cpp
  clang/test/OpenMP/nvptx_NRVO_variable.cpp
  clang/test/OpenMP/nvptx_SPMD_codegen.cpp
  clang/test/OpenMP/nvptx_allocate_codegen.cpp
  clang/test/OpenMP/nvptx_allocate_messages.cpp
  clang/test/OpenMP/nvptx_asm_delayed_diags.c
  clang/test/OpenMP/nvptx_data_sharing.cpp
  clang/test/OpenMP/nvptx_declare_target_var_ctor_dtor_codegen.cpp
  clang/test/OpenMP/nvptx_declare_variant_device_kind_codegen.cpp
  clang/test/OpenMP/nvptx_declare_variant_implementation_vendor_codegen.cpp
  clang/test/OpenMP/nvptx_declare_variant_name_mangling.cpp
  clang/test/OpenMP/nvptx_distribute_parallel_generic_mode_codegen.cpp
  clang/test/OpenMP/nvptx_lambda_capturing.cpp
  clang/test/OpenMP/nvptx_multi_target_parallel_codegen.cpp
  clang/test/OpenMP/nvptx_nested_parallel_codegen.cpp
  clang/test/OpenMP/nvptx_parallel_codegen.cpp
  clang/test/OpenMP/nvptx_parallel_for_codegen.cpp
  clang/test/OpenMP/nvptx_param_translate.c
  clang/test/OpenMP/nvptx_prohibit_thread_local.cpp
  clang/test/OpenMP/nvptx_target_codegen.cpp
  clang/test/OpenMP/nvptx_tar

[PATCH] D154591: [OpenMP][OMPIRBuilder] Rename IsEmbedded and IsTargetCodegen flags

2023-07-10 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 538564.
skatrak added a comment.

Rebase.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154591/new/

https://reviews.llvm.org/D154591

Files:
  clang/docs/OffloadingDesign.rst
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/Decl.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/OpenMPClause.cpp
  clang/lib/Basic/Targets/NVPTX.cpp
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.h
  clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
  clang/lib/CodeGen/CGOpenMPRuntimeGPU.h
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CGVTables.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/Targets/NVPTX.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Frontend/CompilerInstance.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Frontend/InitPreprocessor.cpp
  clang/lib/Lex/LiteralSupport.cpp
  clang/lib/Sema/Sema.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaOpenMP.cpp
  clang/lib/Sema/SemaType.cpp
  clang/test/CodeGen/PowerPC/ppc64le-varargs-f128.c
  clang/test/CodeGen/ibm128-unsupported.c
  clang/test/Driver/openmp-offload-gpu.c
  clang/test/Driver/openmp-offload.c
  clang/test/Headers/__clang_hip_libdevice_declares.cpp
  clang/test/Headers/amdgcn-openmp-device-math-complex.c
  clang/test/Headers/amdgcn-openmp-device-math-complex.cpp
  clang/test/Headers/amdgcn_openmp_device_math.c
  clang/test/Headers/amdgcn_openmp_device_math_c.c
  clang/test/Headers/amdgcn_openmp_device_math_constexpr.cpp
  clang/test/Headers/nvptx_device_cmath_functions.c
  clang/test/Headers/nvptx_device_cmath_functions.cpp
  clang/test/Headers/nvptx_device_cmath_functions_cxx17.cpp
  clang/test/Headers/nvptx_device_math_complex.c
  clang/test/Headers/nvptx_device_math_complex.cpp
  clang/test/Headers/nvptx_device_math_functions.c
  clang/test/Headers/nvptx_device_math_functions.cpp
  clang/test/Headers/nvptx_device_math_functions_cxx17.cpp
  clang/test/Headers/nvptx_device_math_macro.cpp
  clang/test/Headers/nvptx_device_math_modf.cpp
  clang/test/Headers/nvptx_device_math_sin.c
  clang/test/Headers/nvptx_device_math_sin.cpp
  clang/test/Headers/nvptx_device_math_sin_cos.cpp
  clang/test/Headers/nvptx_device_math_sincos.cpp
  clang/test/Headers/openmp-device-functions-bool.c
  clang/test/Headers/openmp_device_math_isnan.cpp
  clang/test/Headers/openmp_new_nothrow.cpp
  clang/test/Headers/target_include_new.cpp
  clang/test/OpenMP/amdgcn-attributes.cpp
  clang/test/OpenMP/amdgcn_device_function_call.cpp
  clang/test/OpenMP/amdgcn_ldbl_check.cpp
  clang/test/OpenMP/amdgcn_target_codegen.cpp
  clang/test/OpenMP/amdgcn_target_device_vla.cpp
  clang/test/OpenMP/amdgcn_target_global_constructor.cpp
  clang/test/OpenMP/amdgcn_target_init_temp_alloca.cpp
  clang/test/OpenMP/amdgpu_target_with_aligned_attribute.c
  clang/test/OpenMP/assumes_include_nvptx.cpp
  clang/test/OpenMP/aux-triple-macros.cpp
  clang/test/OpenMP/declare_target_codegen.cpp
  clang/test/OpenMP/declare_target_codegen_globalization.cpp
  clang/test/OpenMP/declare_target_constexpr_codegen.cpp
  clang/test/OpenMP/declare_target_link_codegen.cpp
  clang/test/OpenMP/declare_target_messages.cpp
  clang/test/OpenMP/declare_target_only_one_side_compilation.cpp
  clang/test/OpenMP/declare_variant_device_kind_codegen.cpp
  clang/test/OpenMP/declare_variant_mixed_codegen.c
  clang/test/OpenMP/distribute_codegen.cpp
  clang/test/OpenMP/distribute_simd_codegen.cpp
  clang/test/OpenMP/metadirective_device_arch_codegen.cpp
  clang/test/OpenMP/metadirective_device_isa_codegen_amdgcn.cpp
  clang/test/OpenMP/multiple_regions_per_line.cpp
  clang/test/OpenMP/nvptx_NRVO_variable.cpp
  clang/test/OpenMP/nvptx_SPMD_codegen.cpp
  clang/test/OpenMP/nvptx_allocate_codegen.cpp
  clang/test/OpenMP/nvptx_allocate_messages.cpp
  clang/test/OpenMP/nvptx_asm_delayed_diags.c
  clang/test/OpenMP/nvptx_data_sharing.cpp
  clang/test/OpenMP/nvptx_declare_target_var_ctor_dtor_codegen.cpp
  clang/test/OpenMP/nvptx_declare_variant_device_kind_codegen.cpp
  clang/test/OpenMP/nvptx_declare_variant_implementation_vendor_codegen.cpp
  clang/test/OpenMP/nvptx_declare_variant_name_mangling.cpp
  clang/test/OpenMP/nvptx_distribute_parallel_generic_mode_codegen.cpp
  clang/test/OpenMP/nvptx_lambda_capturing.cpp
  clang/test/OpenMP/nvptx_multi_target_parallel_codegen.cpp
  clang/test/OpenMP/nvptx_nested_parallel_codegen.cpp
  clang/test/OpenMP/nvptx_parallel_codegen.cpp
  clang/test/OpenMP/nvptx_parallel_for_codegen.cpp
  clang/test/OpenMP/nvptx_param_translate.c
  clang/test/OpenMP/nvptx_prohibit_thread_local.cpp
  clang/test/OpenMP/nvptx_target_codegen.cpp
  clang/test/OpenMP/nvptx_target_cuda_mode_m

[PATCH] D154591: [OpenMP][OMPIRBuilder] Rename IsEmbedded and IsTargetCodegen flags

2023-07-10 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added a comment.

This patch has been triggering a seemingly unrelated build error in libcxx, 
even after multiple rebases. Can I land it as-is or is this error something I 
should address first? Thank you.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154591/new/

https://reviews.llvm.org/D154591

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D154591: [OpenMP][OMPIRBuilder] Rename IsEmbedded and IsTargetCodegen flags

2023-07-10 Thread Sergio Afonso via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG63ca93c7d1d1: [OpenMP][OMPIRBuilder] Rename IsEmbedded and 
IsTargetCodegen flags (authored by skatrak).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154591/new/

https://reviews.llvm.org/D154591

Files:
  clang/docs/OffloadingDesign.rst
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/Decl.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/OpenMPClause.cpp
  clang/lib/Basic/Targets/NVPTX.cpp
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.h
  clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
  clang/lib/CodeGen/CGOpenMPRuntimeGPU.h
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CGVTables.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/Targets/NVPTX.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Frontend/CompilerInstance.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Frontend/InitPreprocessor.cpp
  clang/lib/Lex/LiteralSupport.cpp
  clang/lib/Sema/Sema.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaOpenMP.cpp
  clang/lib/Sema/SemaType.cpp
  clang/test/CodeGen/PowerPC/ppc64le-varargs-f128.c
  clang/test/CodeGen/ibm128-unsupported.c
  clang/test/Driver/openmp-offload-gpu.c
  clang/test/Driver/openmp-offload.c
  clang/test/Headers/__clang_hip_libdevice_declares.cpp
  clang/test/Headers/amdgcn-openmp-device-math-complex.c
  clang/test/Headers/amdgcn-openmp-device-math-complex.cpp
  clang/test/Headers/amdgcn_openmp_device_math.c
  clang/test/Headers/amdgcn_openmp_device_math_c.c
  clang/test/Headers/amdgcn_openmp_device_math_constexpr.cpp
  clang/test/Headers/nvptx_device_cmath_functions.c
  clang/test/Headers/nvptx_device_cmath_functions.cpp
  clang/test/Headers/nvptx_device_cmath_functions_cxx17.cpp
  clang/test/Headers/nvptx_device_math_complex.c
  clang/test/Headers/nvptx_device_math_complex.cpp
  clang/test/Headers/nvptx_device_math_functions.c
  clang/test/Headers/nvptx_device_math_functions.cpp
  clang/test/Headers/nvptx_device_math_functions_cxx17.cpp
  clang/test/Headers/nvptx_device_math_macro.cpp
  clang/test/Headers/nvptx_device_math_modf.cpp
  clang/test/Headers/nvptx_device_math_sin.c
  clang/test/Headers/nvptx_device_math_sin.cpp
  clang/test/Headers/nvptx_device_math_sin_cos.cpp
  clang/test/Headers/nvptx_device_math_sincos.cpp
  clang/test/Headers/openmp-device-functions-bool.c
  clang/test/Headers/openmp_device_math_isnan.cpp
  clang/test/Headers/openmp_new_nothrow.cpp
  clang/test/Headers/target_include_new.cpp
  clang/test/OpenMP/amdgcn-attributes.cpp
  clang/test/OpenMP/amdgcn_device_function_call.cpp
  clang/test/OpenMP/amdgcn_ldbl_check.cpp
  clang/test/OpenMP/amdgcn_target_codegen.cpp
  clang/test/OpenMP/amdgcn_target_device_vla.cpp
  clang/test/OpenMP/amdgcn_target_global_constructor.cpp
  clang/test/OpenMP/amdgcn_target_init_temp_alloca.cpp
  clang/test/OpenMP/amdgpu_target_with_aligned_attribute.c
  clang/test/OpenMP/assumes_include_nvptx.cpp
  clang/test/OpenMP/aux-triple-macros.cpp
  clang/test/OpenMP/declare_target_codegen.cpp
  clang/test/OpenMP/declare_target_codegen_globalization.cpp
  clang/test/OpenMP/declare_target_constexpr_codegen.cpp
  clang/test/OpenMP/declare_target_link_codegen.cpp
  clang/test/OpenMP/declare_target_messages.cpp
  clang/test/OpenMP/declare_target_only_one_side_compilation.cpp
  clang/test/OpenMP/declare_variant_device_kind_codegen.cpp
  clang/test/OpenMP/declare_variant_mixed_codegen.c
  clang/test/OpenMP/distribute_codegen.cpp
  clang/test/OpenMP/distribute_simd_codegen.cpp
  clang/test/OpenMP/metadirective_device_arch_codegen.cpp
  clang/test/OpenMP/metadirective_device_isa_codegen_amdgcn.cpp
  clang/test/OpenMP/multiple_regions_per_line.cpp
  clang/test/OpenMP/nvptx_NRVO_variable.cpp
  clang/test/OpenMP/nvptx_SPMD_codegen.cpp
  clang/test/OpenMP/nvptx_allocate_codegen.cpp
  clang/test/OpenMP/nvptx_allocate_messages.cpp
  clang/test/OpenMP/nvptx_asm_delayed_diags.c
  clang/test/OpenMP/nvptx_data_sharing.cpp
  clang/test/OpenMP/nvptx_declare_target_var_ctor_dtor_codegen.cpp
  clang/test/OpenMP/nvptx_declare_variant_device_kind_codegen.cpp
  clang/test/OpenMP/nvptx_declare_variant_implementation_vendor_codegen.cpp
  clang/test/OpenMP/nvptx_declare_variant_name_mangling.cpp
  clang/test/OpenMP/nvptx_distribute_parallel_generic_mode_codegen.cpp
  clang/test/OpenMP/nvptx_lambda_capturing.cpp
  clang/test/OpenMP/nvptx_multi_target_parallel_codegen.cpp
  clang/test/OpenMP/nvptx_nested_parallel_codegen.cpp
  clang/test/OpenMP/nvptx_parallel_codegen.cpp
  clang/test/OpenMP/nvptx_parallel_for_codegen.cpp
  clang/test

[PATCH] D159212: [MLIR] Allow dialects to disable CSE for certain operations

2023-08-30 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak created this revision.
skatrak added reviewers: ftynse, kiranchandramohan, jsjodin, domada, agozillon, 
raghavendhra, TIFitis, shraiysh.
Herald added subscribers: bviyer, Moerafaat, zero9178, bzcheeseman, sdasgup3, 
wenzhicui, wrengr, cota, teijeong, rdzhabarov, tatianashp, msifontes, jurahul, 
Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, mgester, arpith-jacob, 
antiagainst, shauheen, rriddle, mehdi_amini.
Herald added a project: All.
skatrak requested review of this revision.
Herald added a reviewer: nicolasvasilache.
Herald added subscribers: cfe-commits, stephenneuendorffer, nicolasvasilache.
Herald added projects: clang, MLIR.

This patch adds the `DialectCSEInterface`, which dialects can implement and 
register to prevent the common sub-expression elimination (CSE) pass from 
modifying regions of certain operations.

The result is that these operations would be treated by CSE as if they were 
`IsolatedFromAbove`, but without the restrictions that come with that trait.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159212

Files:
  clang/docs/tools/clang-formatted-files.txt
  mlir/include/mlir/Interfaces/CSEInterfaces.h
  mlir/lib/Transforms/CSE.cpp
  mlir/test/Transforms/cse.mlir
  mlir/test/lib/Dialect/Test/TestDialectInterfaces.cpp
  mlir/test/lib/Dialect/Test/TestOps.td

Index: mlir/test/lib/Dialect/Test/TestOps.td
===
--- mlir/test/lib/Dialect/Test/TestOps.td
+++ mlir/test/lib/Dialect/Test/TestOps.td
@@ -2703,6 +2703,10 @@
   }];
 }
 
+def NoCSEOneRegionOp : TEST_Op<"no_cse_one_region_op", []> {
+  let regions = (region AnyRegion);
+}
+
 //===--===//
 // Test Ops to upgrade base on the dialect versions
 //===--===//
Index: mlir/test/lib/Dialect/Test/TestDialectInterfaces.cpp
===
--- mlir/test/lib/Dialect/Test/TestDialectInterfaces.cpp
+++ mlir/test/lib/Dialect/Test/TestDialectInterfaces.cpp
@@ -7,6 +7,7 @@
 //===--===//
 
 #include "TestDialect.h"
+#include "mlir/Interfaces/CSEInterfaces.h"
 #include "mlir/Interfaces/FoldInterfaces.h"
 #include "mlir/Reducer/ReductionPatternInterface.h"
 #include "mlir/Transforms/InliningUtils.h"
@@ -273,6 +274,16 @@
   }
 };
 
+struct TestDialectCSEInterface : public DialectCSEInterface {
+  using DialectCSEInterface::DialectCSEInterface;
+
+  bool subexpressionExtractionAllowed(Operation *op) const final {
+// Don't allow extracting common subexpressions from the region of these
+// operations.
+return !isa(op);
+  }
+};
+
 /// This class defines the interface for handling inlining with standard
 /// operations.
 struct TestInlinerInterface : public DialectInlinerInterface {
@@ -385,6 +396,7 @@
   auto &blobInterface = addInterface();
   addInterface(blobInterface);
 
-  addInterfaces();
+  addInterfaces();
 }
Index: mlir/test/Transforms/cse.mlir
===
--- mlir/test/Transforms/cse.mlir
+++ mlir/test/Transforms/cse.mlir
@@ -520,3 +520,23 @@
   %2 = "test.op_with_memread"() : () -> (i32)
   return %0, %2, %1 : i32, i32, i32
 }
+
+// CHECK-LABEL: @no_cse_across_disabled_op
+func.func @no_cse_across_disabled_op() -> (i32) {
+  // CHECK-NEXT: %[[CONST1:.+]] = arith.constant 1 : i32
+  %0 = arith.constant 1 : i32
+
+  // CHECK-NEXT: test.no_cse_one_region_op
+  "test.no_cse_one_region_op"() ({
+%1 = arith.constant 1 : i32
+%2 = arith.addi %1, %1 : i32
+"foo.yield"(%2) : (i32) -> ()
+
+// CHECK-NEXT: %[[CONST2:.+]] = arith.constant 1 : i32
+// CHECK-NEXT: %[[SUM:.+]] = arith.addi %[[CONST2]], %[[CONST2]] : i32
+// CHECK-NEXT: "foo.yield"(%[[SUM]]) : (i32) -> ()
+  }) : () -> ()
+
+  // CHECK: return %[[CONST1]] : i32
+  return %0 : i32
+}
Index: mlir/lib/Transforms/CSE.cpp
===
--- mlir/lib/Transforms/CSE.cpp
+++ mlir/lib/Transforms/CSE.cpp
@@ -15,6 +15,7 @@
 
 #include "mlir/IR/Dominance.h"
 #include "mlir/IR/PatternMatch.h"
+#include "mlir/Interfaces/CSEInterfaces.h"
 #include "mlir/Interfaces/SideEffectInterfaces.h"
 #include "mlir/Pass/Pass.h"
 #include "mlir/Transforms/Passes.h"
@@ -61,7 +62,8 @@
 class CSEDriver {
 public:
   CSEDriver(RewriterBase &rewriter, DominanceInfo *domInfo)
-  : rewriter(rewriter), domInfo(domInfo) {}
+  : rewriter(rewriter), domInfo(domInfo),
+interfaces(rewriter.getContext()) {}
 
   /// Simplify all operations within the given op.
   void simplify(Operation *op, bool *changed = nullptr);
@@ -122,6 +124,9 @@
   DominanceInfo *domInfo = nullptr;
   MemEffectsCache memEffectsCache;
 
+  /// CSE interfaces in the present context that can modify CSE behavior.
+  DialectInterfaceCollection interfaces;
+
   // Va

[PATCH] D159212: [MLIR] Allow dialects to disable CSE for certain operations

2023-08-31 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added a comment.

Thank you for your comments @jsjodin and @kiranchandramohan , I created an RFC 
here 

 to try to get consensus on what the best solution should be.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D159212/new/

https://reviews.llvm.org/D159212

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D146075: [flang][driver][openmp] Write MLIR for -save-temps

2023-03-14 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak created this revision.
skatrak added reviewers: gregrodgers, dpalermo, domada, jsjodin, agozillon, 
kiranchandramohan, awarzynski.
Herald added subscribers: sunshaoce, bzcheeseman, rriddle, guansong, yaxunl.
Herald added a reviewer: sscalpone.
Herald added projects: Flang, All.
skatrak requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1, stephenneuendorffer, 
jdoerfert, MaskRay.
Herald added a reviewer: jdoerfert.
Herald added a project: clang.

This patch adds support for producing MLIR files when using -save-temps on
flang. One MLIR file will be produced before lowering and optimization passes,
containing the operations produced by the PFT-to-MLIR lowering bridge, and
another at the end of the process, just before LLVM IR generation.

This is accomplished by forwarding the -save-temps flag from the driver to the
frontend, and modifying it to output MLIR files accordingly.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146075

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/CodeGenOptions.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90

Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -170,6 +170,8 @@
 ! HELP-FC1-NEXT: -pic-level   Value for __PIC__
 ! HELP-FC1-NEXT: -plugin  Use the named plugin action instead of the default action (use "help" to list available options)
 ! HELP-FC1-NEXT: -P Disable linemarker output in -E mode
+! HELP-FC1-NEXT: -save-temps=Save intermediate compilation results.
+! HELP-FC1-NEXT: -save-tempsSave intermediate compilation results
 ! HELP-FC1-NEXT: -std=   Language standard to compile for
 ! HELP-FC1-NEXT: -S Only run preprocess and compilation steps
 ! HELP-FC1-NEXT: -target-cpu Target a specific cpu type
Index: flang/lib/Frontend/FrontendActions.cpp
===
--- flang/lib/Frontend/FrontendActions.cpp
+++ flang/lib/Frontend/FrontendActions.cpp
@@ -13,6 +13,7 @@
 #include "flang/Frontend/FrontendActions.h"
 #include "flang/Common/default-kinds.h"
 #include "flang/Frontend/CompilerInstance.h"
+#include "flang/Frontend/CompilerInvocation.h"
 #include "flang/Frontend/FrontendOptions.h"
 #include "flang/Frontend/PreprocessorOptions.h"
 #include "flang/Lower/Bridge.h"
@@ -52,10 +53,14 @@
 #include "llvm/Passes/PassPlugin.h"
 #include "llvm/Passes/StandardInstrumentations.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Transforms/Utils/ModuleUtils.h"
+#include 
 #include 
+#include 
 
 using namespace Fortran::frontend;
 
@@ -64,6 +69,52 @@
   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
 #include "llvm/Support/Extension.def"
 
+namespace {
+
+/// Save the given \c mlirModule to a temporary .mlir file, in a location
+/// decided by the -save-temps flag. No files are produced if the flag is not
+/// specified.
+bool saveMLIRTempFile(const CompilerInvocation &ci, mlir::ModuleOp mlirModule,
+  llvm::StringRef inputFile, llvm::StringRef outputTag) {
+  if (!ci.getCodeGenOpts().SaveTempsDir.has_value()) {
+return true;
+  }
+
+  const std::string &compilerOutFile = ci.getFrontendOpts().outputFile;
+  const std::string &dir = ci.getCodeGenOpts().SaveTempsDir.value();
+  std::string outDir =
+  llvm::StringSwitch(dir)
+  .Case("cwd", "")
+  .Case("obj", llvm::sys::path::parent_path(compilerOutFile).str())
+  .Default(dir);
+
+  if (llvm::sys::fs::is_directory(outDir)) {
+outDir.append("/");
+  }
+
+  // Build file name from the compiler output file name, triple, cpu and OpenMP
+  // information
+  llvm::StringRef input = llvm::sys::path::filename(inputFile);
+  std::string outFile = input.substr(0, input.find_last_of('.'))
+.str()
+.append("-")
+.append(outputTag.str())
+.append(".mlir");
+
+  std::error_code ec;
+  llvm::ToolOutputFile out(outDir + outFile, ec, llvm::sys::fs::OF_Text);
+  if (ec) {
+return false;
+  }
+  mlirModule->print(out.os());
+  out.os().close();
+  out.keep();
+
+  return true;
+}
+
+} // namespace
+
 //===--===//
 // Custom BeginSourceFileAction
 //===--===//
@@ -206,6 +257,15 @@
 return false;
   }
 
+  // FIR.mlir: This is 1st -save-temps file created for mlir
+  if (!saveMLIRTempFile(ci.getInvocation(), *ml

[PATCH] D146075: [flang][driver][openmp] Write MLIR for -save-temps

2023-03-21 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 507001.
skatrak added a comment.

Address reviewers comments and update tests


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146075/new/

https://reviews.llvm.org/D146075

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/CodeGenOptions.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/frontend-forwarding.f90
  flang/test/Driver/save-temps.f90

Index: flang/test/Driver/save-temps.f90
===
--- flang/test/Driver/save-temps.f90
+++ flang/test/Driver/save-temps.f90
@@ -53,3 +53,15 @@
 ! NO-TEMPS: "-fc1"
 ! NO-TEMPS-SAME: "-S"
 ! NO-TEMPS-SAME: "-o" "save-temps.s"
+
+!--
+! MLIR generation
+!--
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: %flang_fc1 -emit-llvm-bc -save-temps=obj -o %t/out.bc %s 2>&1
+! RUN: FileCheck %s -input-file=%t/save-temps-FIR.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=%t/save-temps-LLVMIR.mlir -check-prefix=MLIR-LLVMIR
+! MLIR-FIR: func.func @{{.*}}main() {
+! MLIR-LLVMIR: llvm.func @{{.*}}main() {
+
+end program
Index: flang/test/Driver/frontend-forwarding.f90
===
--- flang/test/Driver/frontend-forwarding.f90
+++ flang/test/Driver/frontend-forwarding.f90
@@ -15,7 +15,8 @@
 ! RUN: -fassociative-math \
 ! RUN: -freciprocal-math \
 ! RUN: -fpass-plugin=Bye%pluginext \
-! RUN: -mllvm -print-before-all\
+! RUN: -mllvm -print-before-all \
+! RUN: -save-temps=obj \
 ! RUN: -P \
 ! RUN:   | FileCheck %s
 
@@ -34,3 +35,4 @@
 ! CHECK: "-fconvert=little-endian"
 ! CHECK: "-fpass-plugin=Bye
 ! CHECK: "-mllvm" "-print-before-all"
+! CHECK: "-save-temps=obj"
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -170,6 +170,8 @@
 ! HELP-FC1-NEXT: -pic-level   Value for __PIC__
 ! HELP-FC1-NEXT: -plugin  Use the named plugin action instead of the default action (use "help" to list available options)
 ! HELP-FC1-NEXT: -P Disable linemarker output in -E mode
+! HELP-FC1-NEXT: -save-temps=Save intermediate compilation results.
+! HELP-FC1-NEXT: -save-tempsSave intermediate compilation results
 ! HELP-FC1-NEXT: -std=   Language standard to compile for
 ! HELP-FC1-NEXT: -S Only run preprocess and compilation steps
 ! HELP-FC1-NEXT: -target-cpu Target a specific cpu type
Index: flang/lib/Frontend/FrontendActions.cpp
===
--- flang/lib/Frontend/FrontendActions.cpp
+++ flang/lib/Frontend/FrontendActions.cpp
@@ -13,6 +13,7 @@
 #include "flang/Frontend/FrontendActions.h"
 #include "flang/Common/default-kinds.h"
 #include "flang/Frontend/CompilerInstance.h"
+#include "flang/Frontend/CompilerInvocation.h"
 #include "flang/Frontend/FrontendOptions.h"
 #include "flang/Frontend/PreprocessorOptions.h"
 #include "flang/Lower/Bridge.h"
@@ -52,10 +53,14 @@
 #include "llvm/Passes/PassPlugin.h"
 #include "llvm/Passes/StandardInstrumentations.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
 #include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Transforms/Utils/ModuleUtils.h"
 #include 
+#include 
 
 using namespace Fortran::frontend;
 
@@ -64,6 +69,48 @@
   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
 #include "llvm/Support/Extension.def"
 
+/// Save the given \c mlirModule to a temporary .mlir file, in a location
+/// decided by the -save-temps flag. No files are produced if the flag is not
+/// specified.
+static bool saveMLIRTempFile(const CompilerInvocation &ci,
+ mlir::ModuleOp mlirModule,
+ llvm::StringRef inputFile,
+ llvm::StringRef outputTag) {
+  if (!ci.getCodeGenOpts().SaveTempsDir.has_value())
+return true;
+
+  const std::string &compilerOutFile = ci.getFrontendOpts().outputFile;
+  const std::string &dir = ci.getCodeGenOpts().SaveTempsDir.value();
+  std::string outDir =
+  llvm::StringSwitch(dir)
+  .Case("cwd", "")
+  .Case("obj", llvm::sys::path::parent_path(compilerOutFile).str())
+  .Default(dir);
+
+  if (llvm::sys::fs::is_directory(outDir))
+outDir.append("/");
+
+  // Build file name from the compiler output file name, triple, cpu and OpenMP
+  // information
+  llvm::StringRef input = llvm::sys::path::filename(inputFile);
+  std::string outFile = input.substr(0, input.find_last_of('.'))
+ 

[PATCH] D146075: [flang][driver][openmp] Write MLIR for -save-temps

2023-03-21 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak marked 4 inline comments as done.
skatrak added a comment.

Thank you for the feedback! I'll wait for confirmation before landing this 
patch, in case there are further comments relating to tests.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146075/new/

https://reviews.llvm.org/D146075

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D146075: [flang][driver][openmp] Write MLIR for -save-temps

2023-03-22 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 507365.
skatrak marked 6 inline comments as done.
skatrak added a comment.

Changes to tests and path manipulation


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146075/new/

https://reviews.llvm.org/D146075

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/CodeGenOptions.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/frontend-forwarding.f90
  flang/test/Driver/save-temps.f90

Index: flang/test/Driver/save-temps.f90
===
--- flang/test/Driver/save-temps.f90
+++ flang/test/Driver/save-temps.f90
@@ -53,3 +53,36 @@
 ! NO-TEMPS: "-fc1"
 ! NO-TEMPS-SAME: "-S"
 ! NO-TEMPS-SAME: "-o" "save-temps.s"
+
+!--
+! MLIR generation
+!--
+! RUN: not %flang_fc1 -emit-llvm-bc -save-temps=#invalid-dir -o - %s 2>&1 | FileCheck %s -check-prefix=MLIR-ERROR
+! MLIR-ERROR: error: Saving MLIR temp file failed
+
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: pushd %t && %flang_fc1 -emit-llvm-bc -save-temps=cwd -o - %s 2>&1
+! RUN: FileCheck %s -input-file=save-temps-fir.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=save-temps-llvmir.mlir -check-prefix=MLIR-LLVMIR
+! RUN: popd
+
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: pushd %t && %flang_fc1 -emit-llvm-bc -save-temps -o - %s 2>&1
+! RUN: FileCheck %s -input-file=save-temps-fir.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=save-temps-llvmir.mlir -check-prefix=MLIR-LLVMIR
+! RUN: popd
+
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: %flang_fc1 -emit-llvm-bc -save-temps=obj -o %t/out.bc %s 2>&1
+! RUN: FileCheck %s -input-file=%t/save-temps-fir.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=%t/save-temps-llvmir.mlir -check-prefix=MLIR-LLVMIR
+
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: %flang_fc1 -emit-llvm-bc -save-temps=%t -o - %s 2>&1
+! RUN: FileCheck %s -input-file=%t/save-temps-fir.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=%t/save-temps-llvmir.mlir -check-prefix=MLIR-LLVMIR
+
+! MLIR-FIR: func.func @{{.*}}main() {
+! MLIR-LLVMIR: llvm.func @{{.*}}main() {
+
+end program
Index: flang/test/Driver/frontend-forwarding.f90
===
--- flang/test/Driver/frontend-forwarding.f90
+++ flang/test/Driver/frontend-forwarding.f90
@@ -15,7 +15,8 @@
 ! RUN: -fassociative-math \
 ! RUN: -freciprocal-math \
 ! RUN: -fpass-plugin=Bye%pluginext \
-! RUN: -mllvm -print-before-all\
+! RUN: -mllvm -print-before-all \
+! RUN: -save-temps=obj \
 ! RUN: -P \
 ! RUN:   | FileCheck %s
 
@@ -34,3 +35,4 @@
 ! CHECK: "-fconvert=little-endian"
 ! CHECK: "-fpass-plugin=Bye
 ! CHECK: "-mllvm" "-print-before-all"
+! CHECK: "-save-temps=obj"
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -170,6 +170,8 @@
 ! HELP-FC1-NEXT: -pic-level   Value for __PIC__
 ! HELP-FC1-NEXT: -plugin  Use the named plugin action instead of the default action (use "help" to list available options)
 ! HELP-FC1-NEXT: -P Disable linemarker output in -E mode
+! HELP-FC1-NEXT: -save-temps=Save intermediate compilation results.
+! HELP-FC1-NEXT: -save-tempsSave intermediate compilation results
 ! HELP-FC1-NEXT: -std=   Language standard to compile for
 ! HELP-FC1-NEXT: -S Only run preprocess and compilation steps
 ! HELP-FC1-NEXT: -target-cpu Target a specific cpu type
Index: flang/lib/Frontend/FrontendActions.cpp
===
--- flang/lib/Frontend/FrontendActions.cpp
+++ flang/lib/Frontend/FrontendActions.cpp
@@ -13,6 +13,7 @@
 #include "flang/Frontend/FrontendActions.h"
 #include "flang/Common/default-kinds.h"
 #include "flang/Frontend/CompilerInstance.h"
+#include "flang/Frontend/CompilerInvocation.h"
 #include "flang/Frontend/FrontendOptions.h"
 #include "flang/Frontend/PreprocessorOptions.h"
 #include "flang/Lower/Bridge.h"
@@ -39,6 +40,7 @@
 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticFrontend.h"
+#include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
@@ -52,10 +54,14 @@
 #include "llvm/Passes/PassPlugin.h"
 #include "llvm/Passes/StandardInstrumentations.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
 #include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/

[PATCH] D146075: [flang][driver][openmp] Write MLIR for -save-temps

2023-03-22 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added a comment.

Thanks again for helping improving this patch. I made most of the changes 
requested, and tried to clarify a bit better the approach followed for this 
implementation, to see if it is acceptable or if some more fundamental changes 
have to be done.




Comment at: flang/lib/Frontend/FrontendActions.cpp:103
+  std::error_code ec;
+  llvm::ToolOutputFile out(outDir + outFile, ec, llvm::sys::fs::OF_Text);
+  if (ec)

awarzynski wrote:
> Why not just [[ 
> https://github.com/llvm/llvm-project/blob/3b1951aceb8c58acd3d5c5ba2d042ad3d03c13c4/flang/lib/Frontend/FrontendActions.cpp#L793
>  | print ]] the MLIR module?
I'm not sure I completely understand the issue here, by looking at the code you 
linked to. Maybe it's related to the fact that this function is implemented by 
creating new files apart from the main output of the current compiler 
invocation. I developed the idea a bit more in the reply to your comment on 
"save-temps.f90".



Comment at: flang/test/Driver/save-temps.f90:14
 ! CHECK-NEXT: "-o" "save-temps.o"
 ! CHECK-NEXT: "-o" "a.out"
 

awarzynski wrote:
> Why are there no MLIR files here? Same comment for other invocations.
This is because the general way in which -save-temps works is different from 
what's implemented in this patch for MLIR in flang. In the other cases, the 
driver splits the work into several frontend invocations, where each step 
generally produces the input of the next. `-save-temps` makes sure these 
intermediate files are kept where the user specified and not deleted.

In this patch, instead of modifying the driver to create a frontend invocation 
to produce MLIR (generating the *-fir.mlir file), another one to optimize/lower 
that (generating the *-llvmir.mlir file), and a third one to translate lowered 
MLIR into LLVM IR, we just forward the flag to the frontend, which creates 
extra MLIR files at particular spots of the codegen process if the flag is set. 
Hence, MLIR files don't show in the output of `flang-new -###`.



Comment at: flang/test/Driver/save-temps.f90:61
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: %flang_fc1 -emit-llvm-bc -save-temps=obj -o %t/out.bc %s 2>&1
+! RUN: FileCheck %s -input-file=%t/save-temps-FIR.mlir -check-prefix=MLIR-FIR

awarzynski wrote:
> What happens in this case: `-save-temps`? (no dir is specified)
The same as if `-save-temps=cwd` is specified. The test is now updated to check 
all supported options.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146075/new/

https://reviews.llvm.org/D146075

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D146075: [flang][driver][openmp] Write MLIR for -save-temps

2023-03-23 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added a comment.

@awarzynski, can you confirm whether your concerns have been addressed? Thank 
you!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146075/new/

https://reviews.llvm.org/D146075

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D146075: [flang][driver][openmp] Write MLIR for -save-temps

2023-03-24 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 508099.
skatrak added a comment.
Herald added subscribers: Moerafaat, zero9178, sdasgup3, wenzhicui, wrengr, 
cota, teijeong, rdzhabarov, tatianashp, msifontes, jurahul, Kayjukh, grosul1, 
Joonsoo, liufengdb, aartbik, mgester, arpith-jacob, nicolasvasilache, 
antiagainst, shauheen, mehdi_amini, thopre.

Revision of tests


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146075/new/

https://reviews.llvm.org/D146075

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/CodeGenOptions.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/frontend-forwarding.f90
  flang/test/Driver/save-mlir-temps.f90

Index: flang/test/Driver/save-mlir-temps.f90
===
--- /dev/null
+++ flang/test/Driver/save-mlir-temps.f90
@@ -0,0 +1,58 @@
+! Tests for the `-save-temps` flag. Instead of checking the commands generated
+! by the driver with `-###` (like the save-temps.f90 test does), here we check
+! that the MLIR files are actually produced in the specified location because
+! the driver does not generate specific passes for MLIR. Instead, they are
+! generated during code generation as additional outputs.
+
+! As `flang` does not implement `-fc1as` (i.e. a driver for the integrated
+! assembler), we need to use `-fno-integrated-as` here.
+
+! UNSUPPORTED: system-windows
+
+!--
+! Invalid output directory
+!--
+! RUN: not %flang_fc1 -emit-llvm-bc -save-temps=#invalid-dir -o - %s 2>&1 | FileCheck %s -check-prefix=MLIR-ERROR
+! MLIR-ERROR: error: Saving MLIR temp file failed
+
+!--
+! Save to cwd
+!--
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: pushd %t && %flang -c -fno-integrated-as -save-temps=cwd -o out.o %s 2>&1
+! RUN: FileCheck %s -input-file=save-mlir-temps-fir.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=save-mlir-temps-llvmir.mlir -check-prefix=MLIR-LLVMIR
+! RUN: popd
+
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: pushd %t && %flang -c -fno-integrated-as -save-temps -o out.o %s 2>&1
+! RUN: FileCheck %s -input-file=save-mlir-temps-fir.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=save-mlir-temps-llvmir.mlir -check-prefix=MLIR-LLVMIR
+! RUN: popd
+
+!--
+! Save to output directory
+!--
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: %flang -c -fno-integrated-as -save-temps=obj -o %t/out.o %s 2>&1
+! RUN: FileCheck %s -input-file=%t/save-mlir-temps-fir.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=%t/save-mlir-temps-llvmir.mlir -check-prefix=MLIR-LLVMIR
+
+!--
+! Save to specific directory
+!--
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: %flang -c -fno-integrated-as -save-temps=%t -o %t/out.o %s 2>&1
+! RUN: FileCheck %s -input-file=%t/save-mlir-temps-fir.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=%t/save-mlir-temps-llvmir.mlir -check-prefix=MLIR-LLVMIR
+
+!--
+! Content to check from the MLIR outputs
+!--
+! MLIR-FIR-NOT: llvm.func
+! MLIR-FIR: func.func @{{.*}}main() {
+
+! MLIR-FIR-NOT: func.func
+! MLIR-LLVMIR: llvm.func @{{.*}}main() {
+
+end program
Index: flang/test/Driver/frontend-forwarding.f90
===
--- flang/test/Driver/frontend-forwarding.f90
+++ flang/test/Driver/frontend-forwarding.f90
@@ -15,7 +15,8 @@
 ! RUN: -fassociative-math \
 ! RUN: -freciprocal-math \
 ! RUN: -fpass-plugin=Bye%pluginext \
-! RUN: -mllvm -print-before-all\
+! RUN: -mllvm -print-before-all \
+! RUN: -save-temps=obj \
 ! RUN: -P \
 ! RUN:   | FileCheck %s
 
@@ -34,3 +35,4 @@
 ! CHECK: "-fconvert=little-endian"
 ! CHECK: "-fpass-plugin=Bye
 ! CHECK: "-mllvm" "-print-before-all"
+! CHECK: "-save-temps=obj"
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -170,6 +170,8 @@
 ! HELP-FC1-NEXT: -pic-level   Value for __PIC__
 ! HELP-FC1-NEXT: -plugin  Use the named plugin action instead of the default action (use "help" to list available options)
 ! HELP-FC1-NEXT: -P Disable linemarker output in -E mode
+! HELP-FC1-NEXT: -save-temps=Save intermediate compilation results.
+! HELP-FC1-NEXT: -save-tempsSave intermediate compilation results
 ! HELP-FC1-NEXT: -std=   Language standard to compile for
 ! HELP-FC1-NEXT: -S Only run preprocess and compilation steps
 ! HELP-FC1-NEXT: -target-cpu Target a specific cpu type
Index: flang/lib/Frontend/FrontendActions.cpp
=

[PATCH] D146075: [flang][driver][openmp] Write MLIR for -save-temps

2023-03-24 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak marked 3 inline comments as done.
skatrak added inline comments.



Comment at: flang/lib/Frontend/FrontendActions.cpp:103
+  std::error_code ec;
+  llvm::ToolOutputFile out(outDir + outFile, ec, llvm::sys::fs::OF_Text);
+  if (ec)

awarzynski wrote:
> skatrak wrote:
> > awarzynski wrote:
> > > Why not just [[ 
> > > https://github.com/llvm/llvm-project/blob/3b1951aceb8c58acd3d5c5ba2d042ad3d03c13c4/flang/lib/Frontend/FrontendActions.cpp#L793
> > >  | print ]] the MLIR module?
> > I'm not sure I completely understand the issue here, by looking at the code 
> > you linked to. Maybe it's related to the fact that this function is 
> > implemented by creating new files apart from the main output of the current 
> > compiler invocation. I developed the idea a bit more in the reply to your 
> > comment on "save-temps.f90".
> Apologies, I am blind - you were using `print` already!
No problem! That makes it two of us, then ;)



Comment at: flang/test/Driver/save-temps.f90:58
+!--
+! MLIR generation
+!--

awarzynski wrote:
> [nit] For MLIR you check the contents of the intermediate files (great!), but 
> then for all other temp files we don't (some of them are binary and that's 
> one reason). Given that testing for MLIR temp files is so different, I'd be 
> tempted to move to a different file. Or at least add a comment here to 
> explain the rationale for testing differently.
Thanks for the suggestion. I separated that into its own test which also 
contains an explanation of why it's tested differently.



Comment at: flang/test/Driver/save-temps.f90:14
 ! CHECK-NEXT: "-o" "save-temps.o"
 ! CHECK-NEXT: "-o" "a.out"
 

awarzynski wrote:
> skatrak wrote:
> > awarzynski wrote:
> > > Why are there no MLIR files here? Same comment for other invocations.
> > This is because the general way in which -save-temps works is different 
> > from what's implemented in this patch for MLIR in flang. In the other 
> > cases, the driver splits the work into several frontend invocations, where 
> > each step generally produces the input of the next. `-save-temps` makes 
> > sure these intermediate files are kept where the user specified and not 
> > deleted.
> > 
> > In this patch, instead of modifying the driver to create a frontend 
> > invocation to produce MLIR (generating the *-fir.mlir file), another one to 
> > optimize/lower that (generating the *-llvmir.mlir file), and a third one to 
> > translate lowered MLIR into LLVM IR, we just forward the flag to the 
> > frontend, which creates extra MLIR files at particular spots of the codegen 
> > process if the flag is set. Hence, MLIR files don't show in the output of 
> > `flang-new -###`.
> So, IIUC, without `-emit-llvm-bc` there should be no intermediate MLIR files? 
> I would add `CHECK-NOT`.
Actually, with this approach there are no changes to the output of `flang -###` 
either way. I was using `-fc1 -emit-llvm-bc` just because that triggers both 
MLIR temp outputs (the one before any optimizations/lowering, and the one right 
before LLVM IR generation), but I simplified that a bit by just using `flang 
-c` instead and avoid confusion.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146075/new/

https://reviews.llvm.org/D146075

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D146075: [flang][driver][openmp] Write MLIR for -save-temps

2023-03-24 Thread Sergio Afonso via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
skatrak marked an inline comment as done.
Closed by commit rG33be83415c9b: [flang][driver][openmp] Write MLIR for 
-save-temps (authored by skatrak).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146075/new/

https://reviews.llvm.org/D146075

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/CodeGenOptions.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/frontend-forwarding.f90
  flang/test/Driver/save-mlir-temps.f90

Index: flang/test/Driver/save-mlir-temps.f90
===
--- /dev/null
+++ flang/test/Driver/save-mlir-temps.f90
@@ -0,0 +1,58 @@
+! Tests for the `-save-temps` flag. Instead of checking the commands generated
+! by the driver with `-###` (like the save-temps.f90 test does), here we check
+! that the MLIR files are actually produced in the specified location because
+! the driver does not generate specific passes for MLIR. Instead, they are
+! generated during code generation as additional outputs.
+
+! As `flang` does not implement `-fc1as` (i.e. a driver for the integrated
+! assembler), we need to use `-fno-integrated-as` here.
+
+! UNSUPPORTED: system-windows
+
+!--
+! Invalid output directory
+!--
+! RUN: not %flang_fc1 -emit-llvm-bc -save-temps=#invalid-dir -o - %s 2>&1 | FileCheck %s -check-prefix=MLIR-ERROR
+! MLIR-ERROR: error: Saving MLIR temp file failed
+
+!--
+! Save to cwd
+!--
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: pushd %t && %flang -c -fno-integrated-as -save-temps=cwd -o out.o %s 2>&1
+! RUN: FileCheck %s -input-file=save-mlir-temps-fir.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=save-mlir-temps-llvmir.mlir -check-prefix=MLIR-LLVMIR
+! RUN: popd
+
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: pushd %t && %flang -c -fno-integrated-as -save-temps -o out.o %s 2>&1
+! RUN: FileCheck %s -input-file=save-mlir-temps-fir.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=save-mlir-temps-llvmir.mlir -check-prefix=MLIR-LLVMIR
+! RUN: popd
+
+!--
+! Save to output directory
+!--
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: %flang -c -fno-integrated-as -save-temps=obj -o %t/out.o %s 2>&1
+! RUN: FileCheck %s -input-file=%t/save-mlir-temps-fir.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=%t/save-mlir-temps-llvmir.mlir -check-prefix=MLIR-LLVMIR
+
+!--
+! Save to specific directory
+!--
+! RUN: rm -rf %t && mkdir -p %t
+! RUN: %flang -c -fno-integrated-as -save-temps=%t -o %t/out.o %s 2>&1
+! RUN: FileCheck %s -input-file=%t/save-mlir-temps-fir.mlir -check-prefix=MLIR-FIR
+! RUN: FileCheck %s -input-file=%t/save-mlir-temps-llvmir.mlir -check-prefix=MLIR-LLVMIR
+
+!--
+! Content to check from the MLIR outputs
+!--
+! MLIR-FIR-NOT: llvm.func
+! MLIR-FIR: func.func @{{.*}}main() {
+
+! MLIR-FIR-NOT: func.func
+! MLIR-LLVMIR: llvm.func @{{.*}}main() {
+
+end program
Index: flang/test/Driver/frontend-forwarding.f90
===
--- flang/test/Driver/frontend-forwarding.f90
+++ flang/test/Driver/frontend-forwarding.f90
@@ -15,7 +15,8 @@
 ! RUN: -fassociative-math \
 ! RUN: -freciprocal-math \
 ! RUN: -fpass-plugin=Bye%pluginext \
-! RUN: -mllvm -print-before-all\
+! RUN: -mllvm -print-before-all \
+! RUN: -save-temps=obj \
 ! RUN: -P \
 ! RUN:   | FileCheck %s
 
@@ -34,3 +35,4 @@
 ! CHECK: "-fconvert=little-endian"
 ! CHECK: "-fpass-plugin=Bye
 ! CHECK: "-mllvm" "-print-before-all"
+! CHECK: "-save-temps=obj"
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -170,6 +170,8 @@
 ! HELP-FC1-NEXT: -pic-level   Value for __PIC__
 ! HELP-FC1-NEXT: -plugin  Use the named plugin action instead of the default action (use "help" to list available options)
 ! HELP-FC1-NEXT: -P Disable linemarker output in -E mode
+! HELP-FC1-NEXT: -save-temps=Save intermediate compilation results.
+! HELP-FC1-NEXT: -save-tempsSave intermediate compilation results
 ! HELP-FC1-NEXT: -std=   Language standard to compile for
 ! HELP-FC1-NEXT: -S Only run preprocess and compilation steps
 ! HELP-FC1-NEXT: -target-cpu Target a specific cpu type
Index: flang/lib/Frontend/FrontendActions.cpp
===
--- flang/lib/Front

[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-13 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added inline comments.



Comment at: flang/test/Driver/omp-frontend-forwarding.f90:15
+! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} 
"-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK: "{{[^"]*}}clang-offload-packager" {{.*}} 
"--image=file={{.*}}gfx90a.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
+! CHECK-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" 
{{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.bc"

This unit test seems to be failing due to the pattern you've used here. 
Probably a simple fix to get working, this is the test output: 
https://reviews.llvm.org/harbormaster/unit/view/6153283/


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D145815/new/

https://reviews.llvm.org/D145815

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-06-26 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 534512.
skatrak added a comment.

Update to integrate with changes to parent patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,11 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -2676,16 +2676,14 @@
   converter.bindSymbol(sym, symThreadprivateExv);
 }
 
-void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
- Fortran::lower::pft::Evaluation &eval,
- const Fortran::parser::OpenMPDeclareTargetConstruct
- &declareTargetConstruct) {
-  llvm::SmallVector,
-0>
-  symbolAndClause;
-  mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
-
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+SmallVectorImpl> &symbolAndClause) {
+  // Gather the symbols and clauses
   auto findFuncAndVarSyms = [&](const Fortran::parser::OmpObjectList &objList,
 mlir::omp::DeclareTargetCaptureClause clause) {
 for (const Fortran::parser::OmpObject &ompObject : objList.v) {
@@ -2710,6 +2708,7 @@
   Fortran::parser::OmpDeviceTypeClause::Type::Any;
   const auto &spec = std::get(
   declareTargetConstruct.t);
+
   if (const auto *objectList{
   Fortran::parser::Unwrap(spec.u)}) {
 // Case: declare target(func, var1, var2)
@@ -2744,6 +2743,28 @@
 }
   }
 
+  switch (deviceType) {
+  case Fortran::parser::OmpDeviceTypeClause::Type::Any:
+return mlir::omp::DeclareTargetDeviceType::any;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Host:
+return mlir::omp::DeclareTargetDeviceType::host;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Nohost:
+return mlir::omp::DeclareTargetDeviceType::nohost;
+  }
+}
+
+void genDeclareTarget(Fortran::lower::AbstractConverter &converter,
+  Fortran::lower::pft::Evaluation &eval,
+  const Fortran::parser::OpenMPDeclareTargetConstruct
+  &declareTargetConstruct) {
+  llvm::SmallVector,
+0>
+  symbolAndClause;
+  mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
+  mlir::omp::DeclareTargetDeviceType deviceType =
+  getDeclareTargetInfo(eval, declareTargetConstruct, symbolAndClause);
+
   for (std::pair
symClause : symbolAndClause) {
@@ -2770,35 +2791,44 @@
   converter.getCurrentLocation(),
   "Attempt to apply declare target on unsupported operation");
 
-mlir::omp::DeclareTargetDeviceType newDeviceType;
-switch (deviceType) {
-case Fortran::parser::OmpDeviceTypeClause::Type::Nohost:
-  newDeviceType = mlir::omp::DeclareTargetDeviceType::nohost;
-  break;
-case Fortran::parser::OmpDeviceTypeClause::Type::Host:
-  newDeviceType = mlir::omp::DeclareTargetDeviceType::host;
-  break;
-case Fortran::parser::OmpDeviceTypeClause::Type::Any:
-  newDeviceType = mlir::omp::DeclareTargetDeviceType::any;
-  break;
-}
-
 // The function or global already has a declare target applied to it,
 // very likely through implicit capt

[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-03-30 Thread Sergio Afonso via Phabricator via cfe-commits
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(OMP_REQ_NONE);
+}
+
+void OpenMPIRBuilderConfig::setHasRequiresReverseOffload(bool Value) {
+  if (Value)
+RequiresFlags |= OMP_REQ_REVERSE_OFFLOAD;
+  else
+RequiresFlags &= ~OMP_REQ_REVERS

[PATCH] D147941: [Flang][Driver][OpenMP] Enable flags for filtering of offloading passes in flang

2023-04-10 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak created this revision.
skatrak added reviewers: dpalermo, jsjodin, domada, agozillon, TIFitis, 
awarzynski.
Herald added subscribers: sunshaoce, guansong, yaxunl.
Herald added a reviewer: sscalpone.
Herald added projects: Flang, All.
skatrak requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: cfe-commits, jplehr, sstefan1, jdoerfert.
Herald added a project: clang.

This patch adds support for the "--offload-device-only",
"--offload-host-only" and "--offload-host-device" options to the Flang
driver. These can be used to modify the behavior of the driver to select
which compilation passes are triggered during OpenMP offloading.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147941

Files:
  clang/include/clang/Driver/Options.td
  flang/test/Driver/driver-help-hidden.f90
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-frontend-forwarding.f90


Index: flang/test/Driver/omp-frontend-forwarding.f90
===
--- flang/test/Driver/omp-frontend-forwarding.f90
+++ flang/test/Driver/omp-frontend-forwarding.f90
@@ -7,6 +7,38 @@
 ! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}}.f90"
 ! CHECK-OPENMP-NOT: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
 
+! Test regular -fopenmp with offload, and pass filtering options
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefix=CHECK-OFFLOAD-HOST-DEVICE
+
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a --offload-host-device \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefix=CHECK-OFFLOAD-HOST-DEVICE
+
+! CHECK-OFFLOAD-HOST-DEVICE: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+! CHECK-OFFLOAD-HOST-DEVICE-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"amdgcn-amd-amdhsa"
+! CHECK-OFFLOAD-HOST-DEVICE: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a --offload-host-only \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefix=CHECK-OFFLOAD-HOST
+
+! CHECK-OFFLOAD-HOST: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+! CHECK-OFFLOAD-HOST-NOT: "-triple" "amdgcn-amd-amdhsa"
+
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a --offload-device-only \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefix=CHECK-OFFLOAD-DEVICE
+
+! CHECK-OFFLOAD-DEVICE: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+! CHECK-OFFLOAD-DEVICE-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"amdgcn-amd-amdhsa"
+! CHECK-OFFLOAD-DEVICE-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+
 ! Test regular -fopenmp with offload for basic fopenmp-is-device flag addition 
and correct fopenmp 
 ! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | 
FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
 ! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -65,6 +65,9 @@
 ! HELP-NEXT: -mmlir  Additional arguments to forward to MLIR's 
option processing
 ! HELP-NEXT: -module-dir   Put MODULE files in 
 ! HELP-NEXT: -nocpp Disable predefined and command line 
preprocessor macros
+! HELP-NEXT: --offload-device-only   Only compile for the offloading device.
+! HELP-NEXT: --offload-host-device   Only compile for the offloading host.
+! HELP-NEXT: --offload-host-only Only compile for the offloading host.
 ! HELP-NEXT: -o   Write output to 
 ! HELP-NEXT: -pedantic  Warn on language extensions
 ! HELP-NEXT: -print-effective-triple Print the effective target triple
Index: flang/test/Driver/driver-help-hidden.f90
===
--- flang/test/Driver/driver-help-hidden.f90
+++ flang/test/Driver/driver-help-hidden.f90
@@ -69,6 +69,9 @@
 ! CHECK-NEXT: -mmlir  Additional arguments to forward to MLIR's 
option processing
 ! CHECK-NEXT: -module-dir   Put MODULE files in 
 ! CHECK-NEXT: -nocpp Disable predefined and command line 
preprocessor macros
+! CHECK-NEXT: --offload-device-only   Only compile for the offloading device.
+! CHECK-NEXT: --offload-host-device   Only compile for the offloading host.
+! CHECK-NEXT: --offload-host-only Only compile for the offloading host.
 ! CHECK-NEXT: -o  Write output to 
 ! CHECK-NEXT: -pedantic  Warn on language extensions
 ! CHECK-NEXT: -print-effectiv

[PATCH] D147941: [Flang][Driver][OpenMP] Enable flags for filtering of offloading passes in flang

2023-04-10 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added a comment.

In D147941#4255458 , @awarzynski 
wrote:

> Could you add tests that demonstrate what these options actually do?

Thank you for the quick review! These options just modify which `flang-new 
-fc1` invocations are produced by the driver when compiling for device 
offloading. I have added tests that check that only the expected invocations 
are present, but if these tests are not what you'd expect I'd gladly make some 
more if you can explain a bit further what you had in mind.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147941/new/

https://reviews.llvm.org/D147941

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D147941: [Flang][Driver][OpenMP] Enable flags for filtering of offloading passes in flang

2023-04-11 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 512399.
skatrak added a comment.

Address reviewer's comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147941/new/

https://reviews.llvm.org/D147941

Files:
  clang/include/clang/Driver/Options.td
  flang/test/Driver/driver-help-hidden.f90
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-driver-offload.f90
  flang/test/Driver/omp-frontend-forwarding.f90


Index: flang/test/Driver/omp-driver-offload.f90
===
--- flang/test/Driver/omp-driver-offload.f90
+++ flang/test/Driver/omp-driver-offload.f90
@@ -7,6 +7,42 @@
 ! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}}.f90"
 ! CHECK-OPENMP-NOT: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
 
+! Test regular -fopenmp with offload, and invocation filtering options
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a --offload-arch=sm_70 \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefix=OFFLOAD-HOST-AND-DEVICE
+
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a --offload-arch=sm_70 
--offload-host-device \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefix=OFFLOAD-HOST-AND-DEVICE
+
+! OFFLOAD-HOST-AND-DEVICE: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+! OFFLOAD-HOST-AND-DEVICE-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"amdgcn-amd-amdhsa"
+! OFFLOAD-HOST-AND-DEVICE-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"nvptx64-nvidia-cuda"
+! OFFLOAD-HOST-AND-DEVICE: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a --offload-arch=sm_70 --offload-host-only 
\
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefix=OFFLOAD-HOST
+
+! OFFLOAD-HOST: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+! OFFLOAD-HOST-NOT: "-triple" "amdgcn-amd-amdhsa"
+! OFFLOAD-HOST-NOT: "-triple" "nvptx64-nvidia-cuda"
+! OFFLOAD-HOST-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a --offload-arch=sm_70 
--offload-device-only \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefix=OFFLOAD-DEVICE
+
+! OFFLOAD-DEVICE: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+! OFFLOAD-DEVICE-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"amdgcn-amd-amdhsa"
+! OFFLOAD-DEVICE-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"nvptx64-nvidia-cuda"
+! OFFLOAD-DEVICE-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+
 ! Test regular -fopenmp with offload for basic fopenmp-is-device flag addition 
and correct fopenmp 
 ! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | 
FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
 ! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -65,6 +65,9 @@
 ! HELP-NEXT: -mmlir  Additional arguments to forward to MLIR's 
option processing
 ! HELP-NEXT: -module-dir   Put MODULE files in 
 ! HELP-NEXT: -nocpp Disable predefined and command line 
preprocessor macros
+! HELP-NEXT: --offload-device-only   Only compile for the offloading device.
+! HELP-NEXT: --offload-host-device   Only compile for the offloading host.
+! HELP-NEXT: --offload-host-only Only compile for the offloading host.
 ! HELP-NEXT: -o   Write output to 
 ! HELP-NEXT: -pedantic  Warn on language extensions
 ! HELP-NEXT: -print-effective-triple Print the effective target triple
Index: flang/test/Driver/driver-help-hidden.f90
===
--- flang/test/Driver/driver-help-hidden.f90
+++ flang/test/Driver/driver-help-hidden.f90
@@ -69,6 +69,9 @@
 ! CHECK-NEXT: -mmlir  Additional arguments to forward to MLIR's 
option processing
 ! CHECK-NEXT: -module-dir   Put MODULE files in 
 ! CHECK-NEXT: -nocpp Disable predefined and command line 
preprocessor macros
+! CHECK-NEXT: --offload-device-only   Only compile for the offloading device.
+! CHECK-NEXT: --offload-host-device   Only compile for the offloading host.
+! CHECK-NEXT: --offload-host-only Only compile for the offloading host.
 ! CHECK-NEXT: -o  Write output to 
 ! CHECK-NEXT: -pedantic  Warn on language extensions
 ! CHECK-NEXT: -print-effective-triple Print the effective target triple
Index: clang/include/clang/Driver/Options.td
===

[PATCH] D147941: [Flang][Driver][OpenMP] Enable options for selecting offloading phases in flang

2023-04-11 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak marked 4 inline comments as done.
skatrak added a comment.

In D147941#4255622 , @awarzynski 
wrote:

> In the context of LLVM, I would normally associate "pass" with something 
> else. I'm not that familiar with offloading, so perhaps that's the language 
> that people use? Personally, in the context of the driver I'd normally use 
> the term "phase" rather than "pass".
>
> This patch makes sense, though I'd like the following to be addressed before 
> landing this:
>
> 1. Where's the logic that implements what `--offload-host-device`? It looks 
> like this is already implemented elsewhere and this patch simply "unlocks" 
> (rather than "implements") this option for Flang.
> 2. The name of "omp-frontend-forwarding.f90" is very misleading. "Forwarding" 
> would mean `flang-new --offload-host-only %s` --> `flang-new -fc1 
> --offload-host-only %s`, but that's not what's happening here, is it?
>
> For 1. you could just update the summary, and for 2. I suggest renaming 
> "omp-frontend-forwarding.f90" as e.g. "omp-compiler-flag-expansion.f90". WDYT?

I updated the patch title and summary, and renamed the test file to something 
that I think represents better the contents of the test. Thank you again for 
the suggestions, let me know if there are any other improvements to make before 
landing this patch.




Comment at: clang/include/clang/Driver/Options.td:2738
   HelpText<"Don't Use the new driver for offloading compilation.">;
-def offload_device_only : Flag<["--"], "offload-device-only">,
+def offload_device_only : Flag<["--"], "offload-device-only">, 
Flags<[CoreOption, FlangOption]>,
   HelpText<"Only compile for the offloading device.">;

awarzynski wrote:
> Why is `CoreOption` needed here? Wouldn't `FlangOption` be sufficient?
You're right, thanks for the comment.



Comment at: flang/test/Driver/omp-frontend-forwarding.f90:30
+
+! CHECK-OFFLOAD-HOST: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+! CHECK-OFFLOAD-HOST-NOT: "-triple" "amdgcn-amd-amdhsa"

awarzynski wrote:
> Shouldn't there be 2 driver invocation for the host, as in the the 
> `CHECK-OFFLOAD-HOST-DEVICE` case?
In this case only the first invocation would remain, but I added an additional 
`OFFLOAD-HOST-NOT` to make this behavior explicit


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147941/new/

https://reviews.llvm.org/D147941

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-04-12 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 512805.
skatrak added a comment.

Add unit test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

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
@@ -9,12 +9,14 @@
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
 #include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Constants.h"
 #include "llvm/IR/DIBuilder.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
+#include "llvm/IR/Type.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Support/Casting.h"
@@ -5731,7 +5733,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);
@@ -5746,4 +5749,43 @@
   GlobalValue::WeakAnyLinkage);
   EXPECT_TRUE(InfoManager.hasDeviceGlobalVarEntryInfo("gvar"));
 }
+
+TEST_F(OpenMPIRBuilderTest, CreateRegisterRequires) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(/*IsEmbedded=*/true,
+/*IsTargetCodegen=*/false,
+/*OpenMPOffloadMandatory=*/false,
+/*HasRequiresReverseOffload=*/true,
+/*HasRequiresUnifiedAddress=*/false,
+/*HasRequiresUnifiedSharedMemory=*/true,
+/*HasRequiresDynamicAllocators=*/false));
+
+  auto FName =
+  OMPBuilder.createPlatformSpecificName({"omp_offloading", "requires_reg"});
+  EXPECT_EQ(FName, ".omp_offloading.requires_reg");
+
+  Function *Fn = OMPBuilder.createRegisterRequires(FName);
+  EXPECT_EQ(FName, Fn->getName());
+
+  EXPECT_EQ(Fn->getSection(), ".text.startup");
+  EXPECT_TRUE(Fn->hasInternalLinkage());
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoInline));
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoUnwind));
+  EXPECT_EQ(Fn->size(), 1u);
+
+  BasicBlock *Entry = &Fn->getEntryBlock();
+  EXPECT_FALSE(Entry->empty());
+  EXPECT_EQ(Fn->getReturnType()->getTypeID(), Type::VoidTyID);
+
+  CallInst *Call = &cast(*Entry->begin());
+  EXPECT_EQ(Call->getCalledFunction()->getName(), "__tgt_register_requires");
+  EXPECT_EQ(Call->getNumOperands(), 2u);
+
+  Value *Flags = Call->getArgOperand(0);
+  EXPECT_EQ(cast(Flags)->getSExtValue(),
+OMPBuilder.Config.getRequiresFlags());
+}
 } // namespace
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

[PATCH] D147941: [Flang][Driver][OpenMP] Enable options for selecting offloading phases in flang

2023-04-12 Thread Sergio Afonso via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
skatrak marked 2 inline comments as done.
Closed by commit rG2266f4a3b38f: [Flang][Driver][OpenMP] Enable options for 
selecting offloading phases in flang (authored by skatrak).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147941/new/

https://reviews.llvm.org/D147941

Files:
  clang/include/clang/Driver/Options.td
  flang/test/Driver/driver-help-hidden.f90
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-driver-offload.f90
  flang/test/Driver/omp-frontend-forwarding.f90


Index: flang/test/Driver/omp-driver-offload.f90
===
--- flang/test/Driver/omp-driver-offload.f90
+++ flang/test/Driver/omp-driver-offload.f90
@@ -7,6 +7,42 @@
 ! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}}.f90"
 ! CHECK-OPENMP-NOT: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
 
+! Test regular -fopenmp with offload, and invocation filtering options
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a --offload-arch=sm_70 \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefix=OFFLOAD-HOST-AND-DEVICE
+
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a --offload-arch=sm_70 
--offload-host-device \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefix=OFFLOAD-HOST-AND-DEVICE
+
+! OFFLOAD-HOST-AND-DEVICE: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+! OFFLOAD-HOST-AND-DEVICE-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"amdgcn-amd-amdhsa"
+! OFFLOAD-HOST-AND-DEVICE-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"nvptx64-nvidia-cuda"
+! OFFLOAD-HOST-AND-DEVICE: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a --offload-arch=sm_70 --offload-host-only 
\
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefix=OFFLOAD-HOST
+
+! OFFLOAD-HOST: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+! OFFLOAD-HOST-NOT: "-triple" "amdgcn-amd-amdhsa"
+! OFFLOAD-HOST-NOT: "-triple" "nvptx64-nvidia-cuda"
+! OFFLOAD-HOST-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a --offload-arch=sm_70 
--offload-device-only \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefix=OFFLOAD-DEVICE
+
+! OFFLOAD-DEVICE: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+! OFFLOAD-DEVICE-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"amdgcn-amd-amdhsa"
+! OFFLOAD-DEVICE-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"nvptx64-nvidia-cuda"
+! OFFLOAD-DEVICE-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu"
+
 ! Test regular -fopenmp with offload for basic fopenmp-is-device flag addition 
and correct fopenmp 
 ! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | 
FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
 ! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -65,6 +65,9 @@
 ! HELP-NEXT: -mmlir  Additional arguments to forward to MLIR's 
option processing
 ! HELP-NEXT: -module-dir   Put MODULE files in 
 ! HELP-NEXT: -nocpp Disable predefined and command line 
preprocessor macros
+! HELP-NEXT: --offload-device-only   Only compile for the offloading device.
+! HELP-NEXT: --offload-host-device   Only compile for the offloading host.
+! HELP-NEXT: --offload-host-only Only compile for the offloading host.
 ! HELP-NEXT: -o   Write output to 
 ! HELP-NEXT: -pedantic  Warn on language extensions
 ! HELP-NEXT: -print-effective-triple Print the effective target triple
Index: flang/test/Driver/driver-help-hidden.f90
===
--- flang/test/Driver/driver-help-hidden.f90
+++ flang/test/Driver/driver-help-hidden.f90
@@ -69,6 +69,9 @@
 ! CHECK-NEXT: -mmlir  Additional arguments to forward to MLIR's 
option processing
 ! CHECK-NEXT: -module-dir   Put MODULE files in 
 ! CHECK-NEXT: -nocpp Disable predefined and command line 
preprocessor macros
+! CHECK-NEXT: --offload-device-only   Only compile for the offloading device.
+! CHECK-NEXT: --offload-host-device   Only compile for the offloading host.
+! CHECK-NEXT: --offload-host-only Only compile for the offloading host.
 ! CHECK-NEXT: -o  Write output to 
 ! CHECK-NEXT: -pedantic  Warn on language extensions
 ! CHECK-

[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-05-31 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added inline comments.



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:5291
 
+Function *OpenMPIRBuilder::createRegisterRequires(StringRef Name) {
+  // Skip the creation of the registration function if this is device codegen

jsjodin wrote:
> Perhaps this function should be part of the subsequent patches, since we're 
> only moving and using the configuration bits right now.
Thank you for the comment. My thinking was to keep all OMPIRBuilder changes 
self-contained in a single patch, which also allowed me to create a unit test 
for it independent from other changes and make it easier to review. I could 
merge this function and the new unit test into the dependent patch D147219, but 
maybe that one is large enough as it is.

I suppose your concern is having this function make its way into the trunk 
without any uses. Would keeping these changes on this patch but only landing 
them whenever both patches are accepted be a reasonable solution?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

https://reviews.llvm.org/D147217

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-06-01 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 527354.
skatrak added a comment.

Rebase and address reviewer's comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,11 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -2494,14 +2494,14 @@
   converter.bindSymbol(sym, symThreadprivateExv);
 }
 
-void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
- Fortran::lower::pft::Evaluation &eval,
- const Fortran::parser::OpenMPDeclareTargetConstruct
- &declareTargetConstruct) {
-  llvm::SmallVector,
-0>
-  symbolAndClause;
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+SmallVectorImpl> &symbolAndClause) {
+  // Gather the symbols and clauses
   auto findFuncAndVarSyms = [&](const Fortran::parser::OmpObjectList &objList,
 mlir::omp::DeclareTargetCaptureClause clause) {
 for (const auto &ompObject : objList.v) {
@@ -2521,12 +2521,11 @@
 }
   };
 
+  // The default capture type
+  auto deviceType{Fortran::parser::OmpDeviceTypeClause::Type::Any};
   const auto &spec{std::get(
   declareTargetConstruct.t)};
-  auto mod = converter.getFirOpBuilder().getModule();
 
-  // The default capture type
-  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   if (const auto *objectList{
   Fortran::parser::Unwrap(spec.u)}) {
 // Case: declare target(func, var1, var2)
@@ -2562,6 +2561,28 @@
 }
   }
 
+  switch (deviceType) {
+  case Fortran::parser::OmpDeviceTypeClause::Type::Any:
+return mlir::omp::DeclareTargetDeviceType::any;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Host:
+return mlir::omp::DeclareTargetDeviceType::host;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Nohost:
+return mlir::omp::DeclareTargetDeviceType::nohost;
+  }
+}
+
+void genDeclareTarget(Fortran::lower::AbstractConverter &converter,
+  Fortran::lower::pft::Evaluation &eval,
+  const Fortran::parser::OpenMPDeclareTargetConstruct
+  &declareTargetConstruct) {
+  llvm::SmallVector,
+0>
+  symbolAndClause;
+  mlir::omp::DeclareTargetDeviceType deviceType =
+  getDeclareTargetInfo(eval, declareTargetConstruct, symbolAndClause);
+
+  mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
   for (auto sym : symbolAndClause) {
 auto *op = mod.lookupSymbol(converter.mangleName(std::get<1>(sym)));
 
@@ -2571,32 +2592,19 @@
   converter.getCurrentLocation(),
   "Attempt to apply declare target on unsupproted operation");
 
-mlir::omp::DeclareTargetDeviceType newDeviceType;
-switch (deviceType) {
-case Fortran::parser::OmpDeviceTypeClause::Type::Nohost:
-  newDeviceType = mlir::omp::DeclareTargetDeviceType::nohost;
-  break;
-case Fortran::parser::OmpDeviceTypeClause::Type::Host:
-  newDeviceType = mlir::omp::DeclareTargetDeviceType::host;
-  break;
-case Fortran::parser::OmpDeviceTypeClause::Type::Any:
-  newDeviceType = mlir::omp:

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-06-01 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak marked 2 inline comments as done.
skatrak added a comment.

Thank you for the comments, they should have all been addressed now.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-06-01 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 527380.
skatrak added a comment.

Update with latest main branch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

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
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1291,6 +1291,9 @@
 // TODO: set the flags when available
 llvm::OpenMPIRBuilderConfig config(
 isDevice, /* IsTargetCodegen */ false,
+/* OpenMPOffloadMandatory */ false,
+/* HasRequiresReverseOffload */ false,
+/* HasRequiresUnifiedAddress */ false,
 /* HasRequiresUnifiedSharedMemory */ false,
 /* OpenMPOffloadMandatory */ false);
 ompBuilder->setConfig(config);
Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -10,12 +10,14 @@
 #include "llvm/Frontend/OpenMP/OMPDeviceConstants.h"
 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
 #include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Constants.h"
 #include "llvm/IR/DIBuilder.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
+#include "llvm/IR/Type.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Support/Casting.h"
@@ -5124,7 +5126,7 @@
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
   F->setName("func");
   IRBuilder<> Builder(BB);
@@ -5178,7 +5180,8 @@
 
 TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.setConfig(OpenMPIRBuilderConfig(true, false, false, false));
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(true, false, false, false, false, false, false));
   OMPBuilder.initialize();
 
   F->setName("func");
@@ -5840,7 +5843,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);
@@ -5855,4 +5859,44 @@
   GlobalValue::WeakAnyLinkage);
   EXPECT_TRUE(InfoManager.hasDeviceGlobalVarEntryInfo("gvar"));
 }
+
+TEST_F(OpenMPIRBuilderTest, CreateRegisterRequires) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(/*IsEmbedded=*/false,
+/*IsTargetCodegen=*/false,
+/*OpenMPOffloadMandatory=*/false,
+/*HasRequiresReverseOffload=*/true,
+/*HasRequiresUnifiedAddress=*/false,
+/*HasRequiresUnifiedSharedMemory=*/true,
+/*HasRequiresDynamicAllocators=*/false));
+
+  auto FName =
+  OMPBuilder.createPlatformSpecificName({"omp_offloading", "requires_reg"});
+  EXPECT_EQ(FName, ".omp_offloading.requires_reg");
+
+  Function *Fn = OMPBuilder.createRegisterRequires(FName);
+  EXPECT_NE(Fn, nullptr);
+  EXPECT_EQ(FName, Fn->getName());
+
+  EXPECT_EQ(Fn->getSection(), ".text.startup");
+  EXPECT_TRUE(Fn->hasInternalLinkage());
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoInline));
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoUnwind));
+  EXPECT_EQ(Fn->size(), 1u);
+
+  BasicBlock *Entry = &Fn->getEntryBlock();
+  EXPECT_FALSE(Entry->empty());
+  EXPECT_EQ(Fn->getReturnType()->getTypeID(), Type::VoidTyID);
+
+  CallInst *Call = &cast(*Entry->begin());
+  EXPECT_EQ(Call->getCalledFunction()->getName(), "__tgt_register_requires");
+  EXPECT_EQ(Call->getNumOperands(), 2u);
+
+  Value *Flags = Call->getArgOperand(0);
+  EXPECT_EQ(cast(Flags)->getSExtValue(),
+OMPBuilder.Config.getRequiresFlags());
+}
 } // namespace
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Fro

[PATCH] D147219: [OpenMP][Flang][MLIR] Lowering of requires directive from MLIR to LLVM IR

2023-06-02 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 527854.
skatrak added a comment.
Herald added subscribers: cfe-commits, hiraditya.
Herald added a project: clang.

Update and remove OpenMP dialect dependency from the generic LLVM IR 
translation.

Followed @kiranchandramohan's suggestion in the comments for D147172 
, since the
current approach prevents the use of OpenMP dialect-specific attributes for
initializing the `OpenMPIRBuilder` configuration. One of these is defined for
representing 'requires' clauses, which need to be stored in the
`OpenMPIRBuilderConfig`, so a different approach is necessary. The approach
implemented in this review is based on the `amendOperation` translation flow.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147219/new/

https://reviews.llvm.org/D147219

Files:
  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

Index: mlir/test/Target/LLVMIR/openmp-llvm.mlir
===
--- mlir/test/Target/LLVMIR/openmp-llvm.mlir
+++ mlir/test/Target/LLVMIR/openmp-llvm.mlir
@@ -2543,3 +2543,13 @@
 // CHECK: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 1
 // CHECK: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0
 module attributes {omp.flags = #omp.flags} {}
+
+// -
+
+// 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} {}
Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -20,8 +20,6 @@
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
 #include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
 #include "mlir/Dialect/LLVMIR/Transforms/LegalizeForExport.h"
-#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
-#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
 #include "mlir/IR/Attributes.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/BuiltinTypes.h"
@@ -1273,30 +1271,18 @@
 llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
   if (!ompBuilder) {
 ompBuilder = std::make_unique(*llvmModule);
+ompBuilder->initialize();
 
-bool isDevice = false;
-llvm::StringRef hostIRFilePath = "";
-
-if (Attribute deviceAttr = mlirModule->getAttr("omp.is_device"))
-  if (::llvm::isa(deviceAttr))
-isDevice = ::llvm::dyn_cast(deviceAttr).getValue();
-
-if (Attribute filepath = mlirModule->getAttr("omp.host_ir_filepath"))
-  if (::llvm::isa(filepath))
-hostIRFilePath =
-::llvm::dyn_cast(filepath).getValue();
-
-ompBuilder->initialize(hostIRFilePath);
-
-// TODO: set the flags when available
-llvm::OpenMPIRBuilderConfig config(
-isDevice, /* IsTargetCodegen */ false,
+// Flags represented as top-level OpenMP dialect attributes are set in
+// OpenMPDialectLLVMIRTranslationInterface::amendOperation(). Here we set
+// the default configuration.
+ompBuilder->setConfig(llvm::OpenMPIRBuilderConfig(
+/* IsEmbedded */ false, /* IsTargetCodegen */ false,
 /* OpenMPOffloadMandatory */ false,
 /* HasRequiresReverseOffload */ false,
 /* HasRequiresUnifiedAddress */ false,
 /* HasRequiresUnifiedSharedMemory */ false,
-/* OpenMPOffloadMandatory */ false);
-ompBuilder->setConfig(config);
+/* OpenMPOffloadMandatory */ false));
   }
   return ompBuilder.get();
 }
@@ -1383,11 +1369,17 @@
 return nullptr;
   if (failed(translator.createTBAAMetadata()))
 return nullptr;
+
+  // Convert module itself before any functions and operations inside, so that
+  // the OpenMPIRBuilder is configured with the OpenMP dialect attributes
+  // attached to the module by the amendOperation() flow before then.
+  llvm::IRBuilder<> llvmBuilder(llvmContext);
+  if (failed(translator.convertOperation(*module, llvmBuilder)))
+return nullptr;
   if (failed(translator.convertFunctions()))
 return nullptr;
 
   // Convert other top-level operations if possible.
-  llvm::IRBuilder<> llvmBuilder(llvmContext);
   for (Operation &o : getModuleBody(module).getOperations()) {
 if (!isa(&o) &&
@@ -1397,10 +1389,6 @@
 }
   }
 
-  // Convert module itself.
-  if (failed(translator.convertOperation(*module, llvmBuilder)))
-return nullptr;
-
   if (llvm::verifyModule(*translator.llv

[PATCH] D143493: [flang][driver] Add support for -include flag in flang -fc1

2023-02-07 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak created this revision.
skatrak added reviewers: dpalermo, jsjodin, domada, raghavendhra.
Herald added a reviewer: sscalpone.
Herald added a reviewer: awarzynski.
Herald added a subscriber: sunshaoce.
Herald added projects: Flang, All.
skatrak requested review of this revision.
Herald added subscribers: cfe-commits, jdoerfert.
Herald added a project: clang.

This patch adds support for the -include flag to include header files before
parsing the input. It should implement the same behavior as the flag with the
same name in clang.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143493

Files:
  clang/include/clang/Driver/Options.td
  flang/include/flang/Frontend/PreprocessorOptions.h
  flang/include/flang/Parser/parsing.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Parser/parsing.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/include-file.f90

Index: flang/test/Driver/include-file.f90
===
--- /dev/null
+++ flang/test/Driver/include-file.f90
@@ -0,0 +1,13 @@
+! Ensure argument -include works as expected with an included header file.
+
+! RUN: %flang_fc1 -E -include "%S/Inputs/basic-header-one.h" %s  2>&1 | FileCheck %s --check-prefix=INCLUDED
+! RUN: not %flang_fc1 -E -include does-not-exist.h %s  2>&1 | FileCheck %s --check-prefix=MISSING
+
+! INCLUDED: program MainDirectoryOne
+! INCLUDED-NOT: program X
+
+! MISSING: error: Source file 'does-not-exist.h' was not found
+! MISSING-NOT: program
+
+program X
+end
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -144,6 +144,7 @@
 ! HELP-FC1-NEXT: -fsyntax-only  Run the preprocessor, parser and semantic analysis stages
 ! HELP-FC1-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
 ! HELP-FC1-NEXT: -help  Display available options
+! HELP-FC1-NEXT: -include Include file before parsing
 ! HELP-FC1-NEXT: -init-only Only execute frontend initialization
 ! HELP-FC1-NEXT: -IAdd directory to the end of the list of include search paths
 ! HELP-FC1-NEXT: -load Load the named plugin (dynamic shared object)
Index: flang/lib/Parser/parsing.cpp
===
--- flang/lib/Parser/parsing.cpp
+++ flang/lib/Parser/parsing.cpp
@@ -84,6 +84,24 @@
 prescanner.AddCompilerDirectiveSentinel("$omp");
 prescanner.AddCompilerDirectiveSentinel("$"); // OMP conditional line
   }
+
+  // Prescan -include's
+  for (auto include : options.includes) {
+const SourceFile *includeFile =
+allSources.Open(include, fileError, "."s /*prepend to search path*/);
+
+if (!fileError.str().empty()) {
+  ProvenanceRange range{allSources.AddCompilerInsertion(include)};
+  messages_.Say(range, "%s"_err_en_US, fileError.str());
+  return sourceFile;
+}
+CHECK(includeFile);
+
+ProvenanceRange range{
+allSources.AddIncludedFile(*includeFile, ProvenanceRange{})};
+prescanner.Prescan(range);
+  }
+
   ProvenanceRange range{allSources.AddIncludedFile(
   *sourceFile, ProvenanceRange{}, options.isModuleFile)};
   prescanner.Prescan(range);
Index: flang/lib/Frontend/CompilerInvocation.cpp
===
--- flang/lib/Frontend/CompilerInvocation.cpp
+++ flang/lib/Frontend/CompilerInvocation.cpp
@@ -543,6 +543,11 @@
args.filtered(clang::driver::options::OPT_fintrinsic_modules_path))
 opts.searchDirectoriesFromIntrModPath.emplace_back(currentArg->getValue());
 
+  // Add the ordered list of -include's
+  for (const auto *currentArg :
+   args.filtered(clang::driver::options::OPT_include))
+opts.includes.emplace_back(currentArg->getValue());
+
   // -cpp/-nocpp
   if (const auto *currentArg = args.getLastArg(
   clang::driver::options::OPT_cpp, clang::driver::options::OPT_nocpp))
@@ -914,6 +919,11 @@
   preprocessorOptions.searchDirectoriesFromIntrModPath.begin(),
   preprocessorOptions.searchDirectoriesFromIntrModPath.end());
 
+  // Adding includes specified by -include
+  fortranOptions.includes.insert(fortranOptions.includes.end(),
+ preprocessorOptions.includes.begin(),
+ preprocessorOptions.includes.end());
+
   //  Add the default intrinsic module directory
   fortranOptions.intrinsicModuleDirectories.emplace_back(getIntrinsicDir());
 
Index: flang/include/flang/Parser/parsing.h
===
--- flang/include/flang/Parser/parsing.h
+++ flang/include/flang/Parser/parsing.h
@@ -34,6 +34,7 @@
   std::vector searchDirectories;
   std::vector intrinsicModuleDirectories;
   std::vector predefinitions;
+  std::vector includes;
   bool instrumentedParse{false};
   bool i

[PATCH] D143493: [flang][driver] Add support for -include flag in flang -fc1

2023-02-07 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added a comment.

In D143493#4110230 , @awarzynski 
wrote:

> In what cases would this flag be used in practice? I've scanned Clang and 
> couldn't find any answers.

It is introduced to the arguments list in `Clang::AddPreprocessingOptions` in 
certain cases to add the OpenMP wrapper "__clang_openmp_device_functions.h" to 
the list of includes for device offload. It seemed likely that we would need to 
add a similar mechanism eventually to flang as well, although I suppose it 
could also be useful in other situations.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143493/new/

https://reviews.llvm.org/D143493

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D143493: [flang][driver] Add support for -include flag in flang -fc1

2023-02-07 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added a comment.

In D143493#4110323 , @awarzynski 
wrote:

> In D143493#4110272 , @skatrak wrote:
>
>> It is introduced to the arguments list in `Clang::AddPreprocessingOptions` 
>> in certain cases to add the OpenMP wrapper 
>> "__clang_openmp_device_functions.h" to the list of includes for device 
>> offload.
>
> This is a use case in Clang ;-)
>
>> It seemed likely that we would need to add a similar mechanism eventually to 
>> flang as well
>
> Possibly, but it's not needed yet?
>
> I would rather refrain from adding flags to Flang only because a similar flag 
> exists in Clang. I suggest abandoning this until there's a clear need for 
> this in Flang.

Sure, sounds reasonable. I suppose this will most likely come together with the 
rest of changes needed for basic offload then. Thank you!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143493/new/

https://reviews.llvm.org/D143493

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-05-08 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 520337.
skatrak added a comment.

Remove handling of `atomic_default_mem_order`, as it's now done in semantics. 
Fix conflicts with changes to parent patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90
  mlir/lib/Dialect/OpenMP/CMakeLists.txt

Index: mlir/lib/Dialect/OpenMP/CMakeLists.txt
===
--- mlir/lib/Dialect/OpenMP/CMakeLists.txt
+++ mlir/lib/Dialect/OpenMP/CMakeLists.txt
@@ -12,4 +12,5 @@
   LINK_LIBS PUBLIC
   MLIRIR
   MLIRLLVMDialect
+  MLIRFuncDialect
   )
Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,11 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -24,7 +24,9 @@
 #include "flang/Semantics/tools.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/Dialect/SCF/IR/SCF.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
+#include 
 
 using namespace mlir;
 
@@ -2262,14 +2264,14 @@
   converter.bindSymbol(sym, symThreadprivateExv);
 }
 
-void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
- Fortran::lower::pft::Evaluation &eval,
- const Fortran::parser::OpenMPDeclareTargetConstruct
- &declareTargetConstruct) {
-  llvm::SmallVector,
-0>
-  symbolAndClause;
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+SmallVectorImpl> &symbolAndClause) {
+  // Gather the symbols and clauses
   auto findFuncAndVarSyms = [&](const Fortran::parser::OmpObjectList &objList,
 mlir::omp::DeclareTargetCaptureClause clause) {
 for (const auto &ompObject : objList.v) {
@@ -2289,12 +2291,11 @@
 }
   };
 
+  // The default capture type
+  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   const auto &spec{std::get(
   declareTargetConstruct.t)};
-  auto mod = converter.getFirOpBuilder().getModule();
 
-  // The default capture type
-  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   if (const auto *objectList{
   Fortran::parser::Unwrap(spec.u)}) {
 // Case: declare target(func, var1, var2)
@@ -2330,6 +2331,28 @@
 }
   }
 
+  switch (deviceType) {
+  case Fortran::parser::OmpDeviceTypeClause::Type::Any:
+return mlir::omp::DeclareTargetDeviceType::any;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Host:
+return mlir::omp::DeclareTargetDeviceType::host;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Nohost:
+return mlir::omp::DeclareTargetDeviceType::nohost;
+  }
+}
+
+void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
+ Fortran::lower::pft::Evaluation &eval,
+ const Fortran::parser::OpenMPDeclareTargetConstruct
+ &declareTargetConstruct) {
+  llvm::SmallVector,
+0>
+  symbolAndClause;
+  auto deviceType =
+  getDeclareTargetInfo(eval, declareTargetConstruct, symbolAndClause);
+
+  auto mod = converter.getFirOpBuilder().getModule();
   for (auto sym : symbolAndClause) {
 auto *op = mod.look

[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-05-08 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 520374.
skatrak added a comment.

Rebase.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

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
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1269,8 +1269,11 @@
 // TODO: set the flags when available
 llvm::OpenMPIRBuilderConfig Config(
 isDevice, /* IsTargetCodegen */ false,
+/* OpenMPOffloadMandatory */ false,
+/* HasRequiresReverseOffload */ false,
+/* HasRequiresUnifiedAddress */ false,
 /* HasRequiresUnifiedSharedMemory */ false,
-/* OpenMPOffloadMandatory */ false);
+/* HasRequiresDynamicAllocators */ false);
 ompBuilder->setConfig(Config);
   }
   return ompBuilder.get();
Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -9,12 +9,14 @@
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
 #include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Constants.h"
 #include "llvm/IR/DIBuilder.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
+#include "llvm/IR/Type.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Support/Casting.h"
@@ -5123,7 +5125,7 @@
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
   F->setName("func");
   IRBuilder<> Builder(BB);
@@ -5751,7 +5753,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);
@@ -5766,4 +5769,44 @@
   GlobalValue::WeakAnyLinkage);
   EXPECT_TRUE(InfoManager.hasDeviceGlobalVarEntryInfo("gvar"));
 }
+
+TEST_F(OpenMPIRBuilderTest, CreateRegisterRequires) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(/*IsEmbedded=*/false,
+/*IsTargetCodegen=*/false,
+/*OpenMPOffloadMandatory=*/false,
+/*HasRequiresReverseOffload=*/true,
+/*HasRequiresUnifiedAddress=*/false,
+/*HasRequiresUnifiedSharedMemory=*/true,
+/*HasRequiresDynamicAllocators=*/false));
+
+  auto FName =
+  OMPBuilder.createPlatformSpecificName({"omp_offloading", "requires_reg"});
+  EXPECT_EQ(FName, ".omp_offloading.requires_reg");
+
+  Function *Fn = OMPBuilder.createRegisterRequires(FName);
+  EXPECT_NE(Fn, nullptr);
+  EXPECT_EQ(FName, Fn->getName());
+
+  EXPECT_EQ(Fn->getSection(), ".text.startup");
+  EXPECT_TRUE(Fn->hasInternalLinkage());
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoInline));
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoUnwind));
+  EXPECT_EQ(Fn->size(), 1u);
+
+  BasicBlock *Entry = &Fn->getEntryBlock();
+  EXPECT_FALSE(Entry->empty());
+  EXPECT_EQ(Fn->getReturnType()->getTypeID(), Type::VoidTyID);
+
+  CallInst *Call = &cast(*Entry->begin());
+  EXPECT_EQ(Call->getCalledFunction()->getName(), "__tgt_register_requires");
+  EXPECT_EQ(Call->getNumOperands(), 2u);
+
+  Value *Flags = Call->getArgOperand(0);
+  EXPECT_EQ(cast(Flags)->getSExtValue(),
+OMPBuilder.Config.getRequiresFlags());
+}
 } // namespace
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"
 #i

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-05-08 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 520398.
skatrak added a comment.

Rebase.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90
  mlir/lib/Dialect/OpenMP/CMakeLists.txt

Index: mlir/lib/Dialect/OpenMP/CMakeLists.txt
===
--- mlir/lib/Dialect/OpenMP/CMakeLists.txt
+++ mlir/lib/Dialect/OpenMP/CMakeLists.txt
@@ -12,4 +12,5 @@
   LINK_LIBS PUBLIC
   MLIRIR
   MLIRLLVMDialect
+  MLIRFuncDialect
   )
Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,11 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -23,7 +23,10 @@
 #include "flang/Parser/parse-tree.h"
 #include "flang/Semantics/tools.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
+#include "mlir/Dialect/SCF/IR/SCF.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
+#include 
 
 using namespace mlir;
 
@@ -2469,14 +2472,14 @@
   converter.bindSymbol(sym, symThreadprivateExv);
 }
 
-void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
- Fortran::lower::pft::Evaluation &eval,
- const Fortran::parser::OpenMPDeclareTargetConstruct
- &declareTargetConstruct) {
-  llvm::SmallVector,
-0>
-  symbolAndClause;
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+SmallVectorImpl> &symbolAndClause) {
+  // Gather the symbols and clauses
   auto findFuncAndVarSyms = [&](const Fortran::parser::OmpObjectList &objList,
 mlir::omp::DeclareTargetCaptureClause clause) {
 for (const auto &ompObject : objList.v) {
@@ -2496,12 +2499,11 @@
 }
   };
 
+  // The default capture type
+  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   const auto &spec{std::get(
   declareTargetConstruct.t)};
-  auto mod = converter.getFirOpBuilder().getModule();
 
-  // The default capture type
-  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   if (const auto *objectList{
   Fortran::parser::Unwrap(spec.u)}) {
 // Case: declare target(func, var1, var2)
@@ -2537,6 +2539,28 @@
 }
   }
 
+  switch (deviceType) {
+  case Fortran::parser::OmpDeviceTypeClause::Type::Any:
+return mlir::omp::DeclareTargetDeviceType::any;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Host:
+return mlir::omp::DeclareTargetDeviceType::host;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Nohost:
+return mlir::omp::DeclareTargetDeviceType::nohost;
+  }
+}
+
+void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
+ Fortran::lower::pft::Evaluation &eval,
+ const Fortran::parser::OpenMPDeclareTargetConstruct
+ &declareTargetConstruct) {
+  llvm::SmallVector,
+0>
+  symbolAndClause;
+  auto deviceType =
+  getDeclareTargetInfo(eval, declareTargetConstruct, symbolAndClause);
+
+  auto mod = converter.getFirOpBuilder().getModule();
   for (auto sym : symbolAndClause) {
 auto *op = mod.lookupSymbol(converter.mangleName(std::get<1>(sym)));
 
@@ -2546,32 +2570,19 @@

[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-04-21 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 515684.
skatrak added a comment.

Avoid creating registration function for the device.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

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
@@ -9,12 +9,14 @@
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
 #include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Constants.h"
 #include "llvm/IR/DIBuilder.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
+#include "llvm/IR/Type.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Support/Casting.h"
@@ -5731,7 +5733,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);
@@ -5746,4 +5749,44 @@
   GlobalValue::WeakAnyLinkage);
   EXPECT_TRUE(InfoManager.hasDeviceGlobalVarEntryInfo("gvar"));
 }
+
+TEST_F(OpenMPIRBuilderTest, CreateRegisterRequires) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(/*IsEmbedded=*/false,
+/*IsTargetCodegen=*/false,
+/*OpenMPOffloadMandatory=*/false,
+/*HasRequiresReverseOffload=*/true,
+/*HasRequiresUnifiedAddress=*/false,
+/*HasRequiresUnifiedSharedMemory=*/true,
+/*HasRequiresDynamicAllocators=*/false));
+
+  auto FName =
+  OMPBuilder.createPlatformSpecificName({"omp_offloading", "requires_reg"});
+  EXPECT_EQ(FName, ".omp_offloading.requires_reg");
+
+  Function *Fn = OMPBuilder.createRegisterRequires(FName);
+  EXPECT_NE(Fn, nullptr);
+  EXPECT_EQ(FName, Fn->getName());
+
+  EXPECT_EQ(Fn->getSection(), ".text.startup");
+  EXPECT_TRUE(Fn->hasInternalLinkage());
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoInline));
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoUnwind));
+  EXPECT_EQ(Fn->size(), 1u);
+
+  BasicBlock *Entry = &Fn->getEntryBlock();
+  EXPECT_FALSE(Entry->empty());
+  EXPECT_EQ(Fn->getReturnType()->getTypeID(), Type::VoidTyID);
+
+  CallInst *Call = &cast(*Entry->begin());
+  EXPECT_EQ(Call->getCalledFunction()->getName(), "__tgt_register_requires");
+  EXPECT_EQ(Call->getNumOperands(), 2u);
+
+  Value *Flags = Call->getArgOperand(0);
+  EXPECT_EQ(cast(Flags)->getSExtValue(),
+OMPBuilder.Config.getRequiresFlags());
+}
 } // namespace
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(/*LargestV

[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-04-24 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added a comment.

I have been able to track down the failed `lld.wasm::stub_library.s` unit test 
to be due to the buildbot picking up line endings for the 
`lld/test/wasm/Inputs/libstub.so` to be Windows ones ("\r\n"), so the condition 
`if (mbref.getBuffer().starts_with("#STUB\n"))` in `lld/wasm/Driver.cpp:285` 
evaluates to false and the file is not processed. I'm not sure why this problem 
is being specifically picked up by the buildbot for this patch and whether it's 
already fixed upstream.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

https://reviews.llvm.org/D147217

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-04-25 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 516729.
skatrak added a comment.
Herald added subscribers: llvm-commits, cfe-commits, bviyer, Moerafaat, 
zero9178, awarzynski, sdasgup3, wenzhicui, wrengr, cota, teijeong, rdzhabarov, 
tatianashp, msifontes, jurahul, Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, 
mgester, arpith-jacob, csigg, nicolasvasilache, antiagainst, shauheen, thopre, 
hiraditya.
Herald added a reviewer: nicolasvasilache.
Herald added a reviewer: ftynse.
Herald added projects: clang, MLIR, LLVM.

Try to fix issue with applying patch


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/lib/Semantics/CMakeLists.txt
  flang/lib/Semantics/finalize-omp.cpp
  flang/lib/Semantics/finalize-omp.h
  flang/lib/Semantics/semantics.cpp
  flang/test/Lower/OpenMP/Todo/omp-declare-target.f90
  flang/test/Lower/OpenMP/omp-declare-target.f90
  flang/test/Lower/OpenMP/requires-error.f90
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90
  flang/test/Semantics/OpenMP/declare-target-implicit-capture-rewrite.f90
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
  mlir/include/mlir/Dialect/OpenMP/OpenMPDialect.h
  mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
  mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
  mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
  mlir/lib/Target/LLVMIR/CMakeLists.txt
  mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
  mlir/test/Dialect/OpenMP/attr.mlir
  mlir/test/Target/LLVMIR/openmp-llvm.mlir

Index: mlir/test/Target/LLVMIR/openmp-llvm.mlir
===
--- mlir/test/Target/LLVMIR/openmp-llvm.mlir
+++ mlir/test/Target/LLVMIR/openmp-llvm.mlir
@@ -2537,3 +2537,51 @@
 // CHECK: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0
 module attributes {omp.flags = #omp.flags, 
omp.is_device = #omp.isdevice} {}
+
+// -
+
+// Check that the atomic default memory order is picked up by atomic operations.
+module attributes {
+  omp.atomic_default_mem_order = #omp
+} {
+  // CHECK-LABEL: @omp_atomic_default_mem_order
+  // CHECK-SAME: (ptr %[[ARG0:.*]], ptr %[[ARG1:.*]], i32 %[[EXPR:.*]])
+  llvm.func @omp_atomic_default_mem_order(%arg0 : !llvm.ptr,
+  %arg1 : !llvm.ptr,
+  %expr : i32) -> () {
+
+// CHECK: %[[X1:.*]] = load atomic i32, ptr %[[ARG0]] seq_cst, align 4
+// CHECK: store i32 %[[X1]], ptr %[[ARG1]], align 4
+omp.atomic.read %arg1 = %arg0 : !llvm.ptr, i32
+
+// CHECK: store atomic i32 %[[EXPR]], ptr %[[ARG1]] seq_cst, align 4
+// CHECK: call void @__kmpc_flush(ptr @{{.*}})
+omp.atomic.write %arg1 = %expr : !llvm.ptr, i32
+
+// CHECK: atomicrmw add ptr %[[ARG1]], i32 %[[EXPR]] seq_cst
+omp.atomic.update %arg1 : !llvm.ptr {
+^bb0(%xval: i32):
+  %newval = llvm.add %xval, %expr : i32
+  omp.yield(%newval : i32)
+}
+
+// CHECK: %[[xval:.*]] = atomicrmw xchg ptr %[[ARG0]], i32 %[[EXPR]] seq_cst
+// CHECK: store i32 %[[xval]], ptr %[[ARG1]]
+omp.atomic.capture {
+  omp.atomic.read %arg1 = %arg0 : !llvm.ptr, i32
+  omp.atomic.write %arg0 = %expr : !llvm.ptr, i32
+}
+
+llvm.return
+  }
+}
+
+// -
+
+// 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} {}
Index: mlir/test/Dialect/OpenMP/attr.mlir
===
--- mlir/test/Dialect/OpenMP/attr.mlir
+++ mlir/test/Dialect/OpenMP/attr.mlir
@@ -29,3 +29,23 @@
 
 // CHECK: module attributes {omp.flags = #omp.flags} {
 module attributes {omp.flags = #omp.flags} {}
+
+// 
+
+// CHECK-LABEL: func @omp_decl_tar_host
+// CHECK-SAME: {{.*}} attributes {omp.declare_target = #omp} {
+func.func @omp_decl_tar_host() -> () attributes {omp.declare_target = #omp} {
+  return
+}
+
+// CHECK-LABEL: func @omp_decl_tar_nohost
+// CHECK-SAME: {{.*}} attributes {omp.declare_target = #omp} {
+func.func @omp_decl_tar_nohost() -> () attributes {omp.declare_target = #omp} {
+  return
+}
+
+// CHECK-LABEL: func @omp_decl_tar_any
+// CHECK-SAME: {{.*}} attributes {omp.declare_target = #omp} {
+func.func @omp_decl_tar_any() -> () attributes {omp.declare_target = #

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-04-25 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 516730.
skatrak added a comment.

Try undoing last change.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-error.f90
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,14 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.atomic_default_mem_order = #omp
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,12 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.atomic_default_mem_order = #omp
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/test/Lower/OpenMP/requires-error.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-error.f90
@@ -0,0 +1,15 @@
+! RUN: not %flang_fc1 -emit-fir -fopenmp %s -o - 2>&1 | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  error: {{.*}} conflicting atomic_default_mem_order clause found:
+!CHECK-SAME: acq_rel != seq_cst
+program requires
+  !$omp requires atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f()
+  !$omp requires atomic_default_mem_order(acq_rel)
+end subroutine f
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -24,7 +24,9 @@
 #include "flang/Semantics/tools.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/Dialect/SCF/IR/SCF.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
+#include 
 
 using namespace mlir;
 
@@ -2239,11 +2241,13 @@
   converter.bindSymbol(sym, symThreadprivateExv);
 }
 
-void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
- Fortran::lower::pft::Evaluation &eval,
- const Fortran::parser::OpenMPDeclareTargetConstruct
- &declareTargetConstruct) {
-  llvm::SmallVector symbols;
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+SmallVectorImpl &symbols) {
+  // Gather the symbols
   auto findFuncAndVarSyms = [&](const Fortran::parser::OmpObjectList &objList) {
 for (const auto &ompObject : objList.v) {
   Fortran::common::visit(
@@ -2261,12 +2265,11 @@
 }
   };
 
+  // The default capture type
+  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   const auto &spec{std::get(
   declareTargetConstruct.t)};
-  auto mod = converter.getFirOpBuilder().getModule();
 
-  // The default capture type
-  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   if (const auto *objectList{
   Fortran::parser::Unwrap(spec.u)}) {
 // Case: declare target(func, var1, var2)
@@ -2288,8 +2291,7 @@
  std::get_if(
  &clause.u)}) {
 // Case: declare target link(var1, var2)...
-TODO(converter.getCurrentLocation(),
- "the link clause is currently unsupported");
+TODO_NOLOC("the link clause is currently unsupported");
   } else if (const auto *deviceClause{
  std::get_if(
  &clause.u)}) {
@@ -2299,6 +2301,24 @@
 }
   }
 
+  switch (deviceType) {
+  case Fortran::parser::OmpDeviceTypeClause::Type::Any:
+return mlir::omp::DeclareTargetDeviceType::any;
+

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-04-27 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 517616.
skatrak added a comment.

Remove checks from lowering that belong in semantic analysis


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-error.f90
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,14 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.atomic_default_mem_order = #omp
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,12 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.atomic_default_mem_order = #omp
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/test/Lower/OpenMP/requires-error.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-error.f90
@@ -0,0 +1,15 @@
+! RUN: not %flang_fc1 -emit-fir -fopenmp %s -o - 2>&1 | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  error: {{.*}} conflicting atomic_default_mem_order clause found:
+!CHECK-SAME: acq_rel != seq_cst
+program requires
+  !$omp requires atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f()
+  !$omp requires atomic_default_mem_order(acq_rel)
+end subroutine f
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -24,7 +24,9 @@
 #include "flang/Semantics/tools.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/Dialect/SCF/IR/SCF.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
+#include 
 
 using namespace mlir;
 
@@ -2239,11 +2241,13 @@
   converter.bindSymbol(sym, symThreadprivateExv);
 }
 
-void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
- Fortran::lower::pft::Evaluation &eval,
- const Fortran::parser::OpenMPDeclareTargetConstruct
- &declareTargetConstruct) {
-  llvm::SmallVector symbols;
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+SmallVectorImpl &symbols) {
+  // Gather the symbols
   auto findFuncAndVarSyms = [&](const Fortran::parser::OmpObjectList &objList) {
 for (const auto &ompObject : objList.v) {
   Fortran::common::visit(
@@ -2261,12 +2265,11 @@
 }
   };
 
+  // The default capture type
+  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   const auto &spec{std::get(
   declareTargetConstruct.t)};
-  auto mod = converter.getFirOpBuilder().getModule();
 
-  // The default capture type
-  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   if (const auto *objectList{
   Fortran::parser::Unwrap(spec.u)}) {
 // Case: declare target(func, var1, var2)
@@ -2288,8 +2291,7 @@
  std::get_if(
  &clause.u)}) {
 // Case: declare target link(var1, var2)...
-TODO(converter.getCurrentLocation(),
- "the link clause is currently unsupported");
+TODO_NOLOC("the link clause is currently unsupported");
   } else if (const auto *deviceClause{
  std::get_if(
  &clause.u)}) {
@@ -2299,6 +2301,24 @@
 }
   }
 
+  switch (deviceType) {
+  case Fortran::parser::OmpDeviceTypeClause::Type::Any:
+return mlir::

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-04-28 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 517844.
skatrak added a comment.

Remove leftover unit test


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,14 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.atomic_default_mem_order = #omp
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,12 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.atomic_default_mem_order = #omp
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -24,7 +24,9 @@
 #include "flang/Semantics/tools.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/Dialect/SCF/IR/SCF.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
+#include 
 
 using namespace mlir;
 
@@ -2239,11 +2241,13 @@
   converter.bindSymbol(sym, symThreadprivateExv);
 }
 
-void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
- Fortran::lower::pft::Evaluation &eval,
- const Fortran::parser::OpenMPDeclareTargetConstruct
- &declareTargetConstruct) {
-  llvm::SmallVector symbols;
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+SmallVectorImpl &symbols) {
+  // Gather the symbols
   auto findFuncAndVarSyms = [&](const Fortran::parser::OmpObjectList &objList) {
 for (const auto &ompObject : objList.v) {
   Fortran::common::visit(
@@ -2261,12 +2265,11 @@
 }
   };
 
+  // The default capture type
+  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   const auto &spec{std::get(
   declareTargetConstruct.t)};
-  auto mod = converter.getFirOpBuilder().getModule();
 
-  // The default capture type
-  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   if (const auto *objectList{
   Fortran::parser::Unwrap(spec.u)}) {
 // Case: declare target(func, var1, var2)
@@ -2288,8 +2291,7 @@
  std::get_if(
  &clause.u)}) {
 // Case: declare target link(var1, var2)...
-TODO(converter.getCurrentLocation(),
- "the link clause is currently unsupported");
+TODO_NOLOC("the link clause is currently unsupported");
   } else if (const auto *deviceClause{
  std::get_if(
  &clause.u)}) {
@@ -2299,6 +2301,24 @@
 }
   }
 
+  switch (deviceType) {
+  case Fortran::parser::OmpDeviceTypeClause::Type::Any:
+return mlir::omp::DeclareTargetDeviceType::any;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Host:
+return mlir::omp::DeclareTargetDeviceType::host;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Nohost:
+return mlir::omp::DeclareTargetDeviceType::nohost;
+  }
+}
+
+void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
+ Fortran::lower::pft::Evaluation &eval,
+ const Fortran::parser::OpenMPDeclareTargetConstruct
+ &declareTargetConstruct) {
+  SmallVector symbols;
+  auto deviceType = getDeclareTargetInfo(eval, declareTargetConstruct, symbols);
+
+  auto mod = converter.getFirOpBuilder().getModule();
   for (auto sym : symbols) {
 auto *op = mod.lookupSymbol(converter.mangleName(sym));
 
@@ -2309,20 +2329,6 @@

[PATCH] D147217: [OpenMP][OMPIRBuilder] OpenMPIRBuilder support for requires directive

2023-05-02 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 518677.
skatrak edited the summary of this revision.
skatrak added a comment.
Herald added subscribers: bviyer, Moerafaat, zero9178, awarzynski, sdasgup3, 
wenzhicui, wrengr, cota, teijeong, rdzhabarov, tatianashp, msifontes, jurahul, 
Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, mgester, arpith-jacob, csigg, 
nicolasvasilache, antiagainst, shauheen, mehdi_amini, thopre.
Herald added a reviewer: ftynse.
Herald added a project: MLIR.

Rebase


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147217/new/

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
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1269,8 +1269,11 @@
 // TODO: set the flags when available
 llvm::OpenMPIRBuilderConfig Config(
 isDevice, /* IsTargetCodegen */ false,
+/* OpenMPOffloadMandatory */ false,
+/* HasRequiresReverseOffload */ false,
+/* HasRequiresUnifiedAddress */ false,
 /* HasRequiresUnifiedSharedMemory */ false,
-/* OpenMPOffloadMandatory */ false);
+/* HasRequiresDynamicAllocators */ false);
 ompBuilder->setConfig(Config);
   }
   return ompBuilder.get();
Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -9,12 +9,14 @@
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
 #include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Constants.h"
 #include "llvm/IR/DIBuilder.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
+#include "llvm/IR/Type.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Support/Casting.h"
@@ -5123,7 +5125,7 @@
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
   OMPBuilder.initialize();
-  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
   OMPBuilder.setConfig(Config);
   F->setName("func");
   IRBuilder<> Builder(BB);
@@ -5751,7 +5753,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);
@@ -5766,4 +5769,44 @@
   GlobalValue::WeakAnyLinkage);
   EXPECT_TRUE(InfoManager.hasDeviceGlobalVarEntryInfo("gvar"));
 }
+
+TEST_F(OpenMPIRBuilderTest, CreateRegisterRequires) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+
+  OMPBuilder.setConfig(
+  OpenMPIRBuilderConfig(/*IsEmbedded=*/false,
+/*IsTargetCodegen=*/false,
+/*OpenMPOffloadMandatory=*/false,
+/*HasRequiresReverseOffload=*/true,
+/*HasRequiresUnifiedAddress=*/false,
+/*HasRequiresUnifiedSharedMemory=*/true,
+/*HasRequiresDynamicAllocators=*/false));
+
+  auto FName =
+  OMPBuilder.createPlatformSpecificName({"omp_offloading", "requires_reg"});
+  EXPECT_EQ(FName, ".omp_offloading.requires_reg");
+
+  Function *Fn = OMPBuilder.createRegisterRequires(FName);
+  EXPECT_NE(Fn, nullptr);
+  EXPECT_EQ(FName, Fn->getName());
+
+  EXPECT_EQ(Fn->getSection(), ".text.startup");
+  EXPECT_TRUE(Fn->hasInternalLinkage());
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoInline));
+  EXPECT_TRUE(Fn->hasFnAttribute(Attribute::NoUnwind));
+  EXPECT_EQ(Fn->size(), 1u);
+
+  BasicBlock *Entry = &Fn->getEntryBlock();
+  EXPECT_FALSE(Entry->empty());
+  EXPECT_EQ(Fn->getReturnType()->getTypeID(), Type::VoidTyID);
+
+  CallInst *Call = &cast(*Entry->begin());
+  EXPECT_EQ(Call->getCalledFunction()->getName(), "__tgt_register_requires");
+  EXPECT_EQ(Call->getNumOperands(), 2u);
+
+  Value *Flags = Call->getArgOperand(0);
+  EXPECT_EQ(cast(Flags)->getSExtValue(),
+OMPBuilder.Config.getRequiresFlags());
+}
 } // namespace
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
=

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-05-02 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 518698.
skatrak added a comment.

Rebase. Disable some known failing 'declare target' tests temporarily until 
they are fixed by parent patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/omp-declare-target-data.f90
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,14 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.atomic_default_mem_order = #omp
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,12 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.atomic_default_mem_order = #omp
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/test/Lower/OpenMP/omp-declare-target-data.f90
===
--- flang/test/Lower/OpenMP/omp-declare-target-data.f90
+++ flang/test/Lower/OpenMP/omp-declare-target-data.f90
@@ -1,7 +1,8 @@
 !RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s 
 !RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s
-!RUN: %flang_fc1 -emit-llvm-bc -fopenmp -o %t.bc %s | llvm-dis %t.bc -o - | FileCheck %s --check-prefix=HOST
-!RUN: %flang_fc1 -emit-llvm -fopenmp -fopenmp-is-device -fopenmp-host-ir-file-path %t.bc -o - %s 2>&1 | FileCheck %s --check-prefix=DEVICE
+!COM: TODO Uncomment following commands, once all required declare target lowering work lands
+!COM: %flang_fc1 -emit-llvm-bc -fopenmp -o %t.bc %s | llvm-dis %t.bc -o - | FileCheck %s --check-prefix=HOST
+!COM: %flang_fc1 -emit-llvm -fopenmp -fopenmp-is-device -fopenmp-host-ir-file-path %t.bc -o - %s 2>&1 | FileCheck %s --check-prefix=DEVICE
 
 !HOST-DAG: %struct.__tgt_offload_entry = type { ptr, ptr, i64, i32, i32 }
 !HOST-DAG: !omp_offload.info = !{!{{.*}}}
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -24,7 +24,9 @@
 #include "flang/Semantics/tools.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/Dialect/SCF/IR/SCF.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
+#include 
 
 using namespace mlir;
 
@@ -2262,14 +2264,14 @@
   converter.bindSymbol(sym, symThreadprivateExv);
 }
 
-void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
- Fortran::lower::pft::Evaluation &eval,
- const Fortran::parser::OpenMPDeclareTargetConstruct
- &declareTargetConstruct) {
-  llvm::SmallVector,
-0>
-  symbolAndClause;
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+SmallVectorImpl> &symbolAndClause) {
+  // Gather the symbols and clauses
   auto findFuncAndVarSyms = [&](const Fortran::parser::OmpObjectList &objList,
 mlir::omp::DeclareTargetCaptureClause clause) {
 for (const auto &ompObject : objList.v) {
@@ -2289,12 +2291,11 @@
 }
   };
 
+  // The default capture type
+  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   const auto &spec{std::get(
   declareTargetConstruct.t)};
-  auto mod = converter.getFirOpBuilder().getModule();
 
-  // The default capture type
-  auto deviceType = Fortran::parser::OmpDeviceTypeClause::Type::Any;
   if (const auto *objectList{
   Fortran::parser::Unwrap(spec.u)}) {
 // Case: declare target(func, var1, var2)
@@ -2330,22 +2331,31 @@
 }
   }
 
+  switch (d

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-06-12 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak added a comment.

Thank you for the feedback. I expect I will be able to refactor the 
`analyzeOpenMPDeclarative()` function to OpenMP.cpp as suggested, but I'll 
bring that together with other changes to make it integrate cleanly with the 
latest changes to D149337 . Since that patch 
now moves a big part of the gathering of 'requires' clauses to the prior 
semantics step, this patch can be simplified to rely on that.




Comment at: flang/lib/Lower/Bridge.cpp:271
+bridge{bridge}, foldingContext{bridge.createFoldingContext()},
+ompRequiresFlags{mlir::omp::ClauseRequires::none} {}
   virtual ~FirConverter() = default;

kiranchandramohan wrote:
> Is this set anywhere? If not is it required? Can this be made available 
> through the bridge?
The `ompRequiresFlags` field is set by the `analyzeOpenMPDeclarative` method, 
called for each 'omp requires' directive in the PFT.

I'm not sure if I understand what you mean by making this available through the 
bridge. Do you suggest creating a public method to access these flags?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-06-14 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 531437.
skatrak added a comment.

Update patch to integrate with related patch D149337 
 and address reviewer's comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,11 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -2594,16 +2594,14 @@
   converter.bindSymbol(sym, symThreadprivateExv);
 }
 
-void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
- Fortran::lower::pft::Evaluation &eval,
- const Fortran::parser::OpenMPDeclareTargetConstruct
- &declareTargetConstruct) {
-  llvm::SmallVector,
-0>
-  symbolAndClause;
-  mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
-
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+SmallVectorImpl> &symbolAndClause) {
+  // Gather the symbols and clauses
   auto findFuncAndVarSyms = [&](const Fortran::parser::OmpObjectList &objList,
 mlir::omp::DeclareTargetCaptureClause clause) {
 for (const Fortran::parser::OmpObject &ompObject : objList.v) {
@@ -2628,6 +2626,7 @@
   Fortran::parser::OmpDeviceTypeClause::Type::Any;
   const auto &spec = std::get(
   declareTargetConstruct.t);
+
   if (const auto *objectList{
   Fortran::parser::Unwrap(spec.u)}) {
 // Case: declare target(func, var1, var2)
@@ -2662,6 +2661,28 @@
 }
   }
 
+  switch (deviceType) {
+  case Fortran::parser::OmpDeviceTypeClause::Type::Any:
+return mlir::omp::DeclareTargetDeviceType::any;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Host:
+return mlir::omp::DeclareTargetDeviceType::host;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Nohost:
+return mlir::omp::DeclareTargetDeviceType::nohost;
+  }
+}
+
+void genDeclareTarget(Fortran::lower::AbstractConverter &converter,
+  Fortran::lower::pft::Evaluation &eval,
+  const Fortran::parser::OpenMPDeclareTargetConstruct
+  &declareTargetConstruct) {
+  llvm::SmallVector,
+0>
+  symbolAndClause;
+  mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
+  mlir::omp::DeclareTargetDeviceType deviceType =
+  getDeclareTargetInfo(eval, declareTargetConstruct, symbolAndClause);
+
   for (std::pair
symClause : symbolAndClause) {
@@ -2688,35 +2709,44 @@
   converter.getCurrentLocation(),
   "Attempt to apply declare target on unsupported operation");
 
-mlir::omp::DeclareTargetDeviceType newDeviceType;
-switch (deviceType) {
-case Fortran::parser::OmpDeviceTypeClause::Type::Nohost:
-  newDeviceType = mlir::omp::DeclareTargetDeviceType::nohost;
-  break;
-case Fortran::parser::OmpDeviceTypeClause::Type::Host:
-  newDeviceType = mlir::omp::DeclareTargetDeviceType::host;
-  break;
-case Fortran::parser::OmpDeviceTypeClause::Type::Any:
-  newDeviceType = mlir::omp::DeclareTargetDeviceType::any;
-  break;
-}
-
 // The function or global already has a

[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-06-14 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak requested review of this revision.
skatrak added a comment.

Changing status to "Request Review" again due to some significant changes to 
the approach.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D147218: [OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR

2023-06-15 Thread Sergio Afonso via Phabricator via cfe-commits
skatrak updated this revision to Diff 531717.
skatrak added a comment.

Rebasde to fix build error.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147218/new/

https://reviews.llvm.org/D147218

Files:
  flang/include/flang/Lower/OpenMP.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/OpenMP.cpp
  flang/test/Lower/OpenMP/requires-notarget.f90
  flang/test/Lower/OpenMP/requires.f90

Index: flang/test/Lower/OpenMP/requires.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks the lowering of requires into MLIR
+
+!CHECK:  module attributes {
+!CHECK-SAME: omp.requires = #omp
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
+
+subroutine f
+  !$omp declare target
+end subroutine f
Index: flang/test/Lower/OpenMP/requires-notarget.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/requires-notarget.f90
@@ -0,0 +1,11 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
+
+! This test checks that requires lowering into MLIR skips creating the
+! omp.requires attribute with target-related clauses if there are no device
+! functions in the compilation unit
+
+!CHECK:  module attributes {
+!CHECK-NOT:  omp.requires
+program requires
+  !$omp requires unified_shared_memory reverse_offload atomic_default_mem_order(seq_cst)
+end program requires
Index: flang/lib/Lower/OpenMP.cpp
===
--- flang/lib/Lower/OpenMP.cpp
+++ flang/lib/Lower/OpenMP.cpp
@@ -2603,16 +2603,14 @@
   converter.bindSymbol(sym, symThreadprivateExv);
 }
 
-void handleDeclareTarget(Fortran::lower::AbstractConverter &converter,
- Fortran::lower::pft::Evaluation &eval,
- const Fortran::parser::OpenMPDeclareTargetConstruct
- &declareTargetConstruct) {
-  llvm::SmallVector,
-0>
-  symbolAndClause;
-  mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
-
+/// Extract the list of function and variable symbols affected by the given
+/// 'declare target' directive and return the intended device type for them.
+static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
+Fortran::lower::pft::Evaluation &eval,
+const Fortran::parser::OpenMPDeclareTargetConstruct &declareTargetConstruct,
+SmallVectorImpl> &symbolAndClause) {
+  // Gather the symbols and clauses
   auto findFuncAndVarSyms = [&](const Fortran::parser::OmpObjectList &objList,
 mlir::omp::DeclareTargetCaptureClause clause) {
 for (const Fortran::parser::OmpObject &ompObject : objList.v) {
@@ -2637,6 +2635,7 @@
   Fortran::parser::OmpDeviceTypeClause::Type::Any;
   const auto &spec = std::get(
   declareTargetConstruct.t);
+
   if (const auto *objectList{
   Fortran::parser::Unwrap(spec.u)}) {
 // Case: declare target(func, var1, var2)
@@ -2671,6 +2670,28 @@
 }
   }
 
+  switch (deviceType) {
+  case Fortran::parser::OmpDeviceTypeClause::Type::Any:
+return mlir::omp::DeclareTargetDeviceType::any;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Host:
+return mlir::omp::DeclareTargetDeviceType::host;
+  case Fortran::parser::OmpDeviceTypeClause::Type::Nohost:
+return mlir::omp::DeclareTargetDeviceType::nohost;
+  }
+}
+
+void genDeclareTarget(Fortran::lower::AbstractConverter &converter,
+  Fortran::lower::pft::Evaluation &eval,
+  const Fortran::parser::OpenMPDeclareTargetConstruct
+  &declareTargetConstruct) {
+  llvm::SmallVector,
+0>
+  symbolAndClause;
+  mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
+  mlir::omp::DeclareTargetDeviceType deviceType =
+  getDeclareTargetInfo(eval, declareTargetConstruct, symbolAndClause);
+
   for (std::pair
symClause : symbolAndClause) {
@@ -2697,35 +2718,44 @@
   converter.getCurrentLocation(),
   "Attempt to apply declare target on unsupported operation");
 
-mlir::omp::DeclareTargetDeviceType newDeviceType;
-switch (deviceType) {
-case Fortran::parser::OmpDeviceTypeClause::Type::Nohost:
-  newDeviceType = mlir::omp::DeclareTargetDeviceType::nohost;
-  break;
-case Fortran::parser::OmpDeviceTypeClause::Type::Host:
-  newDeviceType = mlir::omp::DeclareTargetDeviceType::host;
-  break;
-case Fortran::parser::OmpDeviceTypeClause::Type::Any:
-  newDeviceType = mlir::omp::DeclareTargetDeviceType::any;
-  break;
-}
-
 // The function or global already has a declare target applied to it,
 // very likely through implicit capture (usage in another