[flang] [clang] [Flang] Add code-object-version option (PR #72638)

2023-11-17 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin edited 
https://github.com/llvm/llvm-project/pull/72638
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang] Add code-object-version option (PR #72638)

2023-11-17 Thread Jan Leyonberg via cfe-commits


@@ -264,6 +263,37 @@ static void addDepdendentLibs(mlir::ModuleOp &mlirModule,
   }
 }
 
+// Add to MLIR code target specific items which are dependent on target
+// configuration specified by the user
+static void addTargetSpecificMLIRItems(mlir::ModuleOp &mlirModule,
+   CompilerInstance &ci) {
+  const TargetOptions &targetOpts = ci.getInvocation().getTargetOpts();

jsjodin wrote:

Create a separate function for adding the code object version like 
addDependentLibs is a separate function.

https://github.com/llvm/llvm-project/pull/72638
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang] Add code-object-version option (PR #72638)

2023-11-17 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin requested changes to this pull request.


https://github.com/llvm/llvm-project/pull/72638
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [clang] [flang] [Flang] Add code-object-version option (PR #72638)

2023-11-28 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/72638
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [clang] [mlir] [OpenMP] Migrate GPU Reductions CodeGen from Clang to OMPIRBuilder (PR #80343)

2024-02-06 Thread Jan Leyonberg via cfe-commits

jsjodin wrote:

I think the patch should be split up into at least 2 pieces. First would be the 
migration work and changes to Clang, the second would be working on adding 
reduction support for flang. 

https://github.com/llvm/llvm-project/pull/80343
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [mlir] [llvm] [OpenMP] Migrate GPU Reductions CodeGen from Clang to OMPIRBuilder (PR #80343)

2024-02-06 Thread Jan Leyonberg via cfe-commits


@@ -2719,10 +4051,24 @@ getKmpcForStaticLoopForType(Type *Ty, OpenMPIRBuilder 
*OMPBuilder,
 static void createTargetLoopWorkshareCall(
 OpenMPIRBuilder *OMPBuilder, WorksharingLoopType LoopType,
 BasicBlock *InsertBlock, Value *Ident, Value *LoopBodyArg,
-Type *ParallelTaskPtr, Value *TripCount, Function &LoopBodyFn) {
-  Type *TripCountTy = TripCount->getType();
+Type *ParallelTaskPtr, Value *TripCountOrig, Function &LoopBodyFn) {
   Module &M = OMPBuilder->M;
   IRBuilder<> &Builder = OMPBuilder->Builder;
+  Value *TripCount = TripCountOrig;
+  // FIXME(JAN): The trip count is 1 larger than it should be for GPU, this may
+  // not be the right way to fix it, but this works for now.

jsjodin wrote:

Will this give the correct trip count in Clang? I know Flang's trip count value 
is +1 compared to what Clang was generating so that's why this was added 
(temporarily). It would be best if we could agree on the same trip count and 
this adjustment could go away. Maybe that would have to be a separate patch?

https://github.com/llvm/llvm-project/pull/80343
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [mlir] [clang] [OpenMP] Migrate GPU Reductions CodeGen from Clang to OMPIRBuilder (PR #80343)

2024-02-06 Thread Jan Leyonberg via cfe-commits


@@ -2042,35 +2057,1378 @@ OpenMPIRBuilder::createSection(const 
LocationDescription &Loc,
   /*IsCancellable*/ true);
 }
 
-/// Create a function with a unique name and a "void (i8*, i8*)" signature in
-/// the given module and return it.
-Function *getFreshReductionFunc(Module &M) {
-  Type *VoidTy = Type::getVoidTy(M.getContext());
-  Type *Int8PtrTy = PointerType::getUnqual(M.getContext());
-  auto *FuncTy =
-  FunctionType::get(VoidTy, {Int8PtrTy, Int8PtrTy}, /* IsVarArg */ false);
-  return Function::Create(FuncTy, GlobalVariable::InternalLinkage,
-  M.getDataLayout().getDefaultGlobalsAddressSpace(),
-  ".omp.reduction.func", &M);
+static OpenMPIRBuilder::InsertPointTy getInsertPointAfterInstr(Instruction *I) 
{
+  BasicBlock::iterator IT(I);
+  IT++;
+  return OpenMPIRBuilder::InsertPointTy(I->getParent(), IT);
 }
 
-OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createReductions(
-const LocationDescription &Loc, InsertPointTy AllocaIP,
-ArrayRef ReductionInfos, bool IsNoWait) {
-  for (const ReductionInfo &RI : ReductionInfos) {
+void OpenMPIRBuilder::emitUsed(StringRef Name,
+   std::vector &List) {
+  if (List.empty())
+return;
+
+  // Convert List to what ConstantArray needs.
+  SmallVector UsedArray;
+  UsedArray.resize(List.size());
+  for (unsigned I = 0, E = List.size(); I != E; ++I)
+UsedArray[I] = ConstantExpr::getPointerBitCastOrAddrSpaceCast(
+cast(&*List[I]), Builder.getPtrTy());
+
+  if (UsedArray.empty())
+return;
+  ArrayType *ATy = ArrayType::get(Builder.getPtrTy(), UsedArray.size());
+
+  auto *GV = new GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
+ConstantArray::get(ATy, UsedArray), Name);
+
+  GV->setSection("llvm.metadata");
+}
+
+Value *OpenMPIRBuilder::getGPUThreadID() {
+  return Builder.CreateCall(
+  getOrCreateRuntimeFunction(M,
+ 
OMPRTL___kmpc_get_hardware_thread_id_in_block),
+  {});
+}
+
+Value *OpenMPIRBuilder::getGPUWarpSize() {
+  return Builder.CreateCall(
+  getOrCreateRuntimeFunction(M, OMPRTL___kmpc_get_warp_size), {});
+}
+
+Value *OpenMPIRBuilder::getNVPTXWarpID() {
+  unsigned LaneIDBits = Log2_32(Config.getGridValue().GV_Warp_Size);
+  return Builder.CreateAShr(getGPUThreadID(), LaneIDBits, "nvptx_warp_id");
+}
+
+Value *OpenMPIRBuilder::getNVPTXLaneID() {
+  unsigned LaneIDBits = Log2_32(Config.getGridValue().GV_Warp_Size);
+  assert(LaneIDBits < 32 && "Invalid LaneIDBits size in NVPTX device.");
+  unsigned LaneIDMask = ~0u >> (32u - LaneIDBits);
+  return Builder.CreateAnd(getGPUThreadID(), Builder.getInt32(LaneIDMask),
+   "nvptx_lane_id");
+}
+
+Value *OpenMPIRBuilder::castValueToType(InsertPointTy AllocaIP, Value *From,
+Type *ToType) {
+  Type *FromType = From->getType();
+  uint64_t FromSize = M.getDataLayout().getTypeStoreSize(FromType);
+  uint64_t ToSize = M.getDataLayout().getTypeStoreSize(ToType);
+  assert(FromSize > 0 && "From size must be greater than zero");
+  assert(ToSize > 0 && "To size must be greater than zero");
+  if (FromType == ToType)
+return From;
+  if (FromSize == ToSize)
+return Builder.CreateBitCast(From, ToType);
+  if (ToType->isIntegerTy() && FromType->isIntegerTy())
+return Builder.CreateIntCast(From, ToType, /*isSigned*/ true);
+  InsertPointTy SaveIP = Builder.saveIP();
+  Builder.restoreIP(AllocaIP);
+  Value *CastItem = Builder.CreateAlloca(ToType);
+  Builder.restoreIP(SaveIP);
+
+  Value *ValCastItem = Builder.CreatePointerBitCastOrAddrSpaceCast(
+  CastItem, FromType->getPointerTo());
+  Builder.CreateStore(From, ValCastItem);
+  return Builder.CreateLoad(ToType, CastItem);
+}
+
+Value *OpenMPIRBuilder::createRuntimeShuffleFunction(InsertPointTy AllocaIP,
+ Value *Element,
+ Type *ElementType,
+ Value *Offset) {
+  uint64_t Size = M.getDataLayout().getTypeStoreSize(ElementType);
+  assert(Size <= 8 && "Unsupported bitwidth in shuffle instruction");
+
+  // Cast all types to 32- or 64-bit values before calling shuffle routines.
+  Type *CastTy = Builder.getIntNTy(Size <= 4 ? 32 : 64);
+  Value *ElemCast = castValueToType(AllocaIP, Element, CastTy);
+  Value *WarpSize =
+  Builder.CreateIntCast(getGPUWarpSize(), Builder.getInt16Ty(), true);
+  Function *ShuffleFunc = getOrCreateRuntimeFunctionPtr(
+  Size <= 4 ? RuntimeFunction::OMPRTL___kmpc_shuffle_int32
+: RuntimeFunction::OMPRTL___kmpc_shuffle_int64);
+  Value *WarpSizeCast =
+  Builder.CreateIntCast(WarpSize, Builder.getInt16Ty(), /*isSigned=*/true);
+  Value *ShuffleCall =
+  Builder.CreateCall(ShuffleFunc, {ElemCast, Offset, WarpSizeCast});
+  return castV

[llvm] [mlir] [clang] [OpenMP] Migrate GPU Reductions CodeGen from Clang to OMPIRBuilder (PR #80343)

2024-02-06 Thread Jan Leyonberg via cfe-commits

jsjodin wrote:

> @jsjodin This PR only has the migration work. Flang/MLIR is left unchanged 
> other than the few necessary changes.
> 
> Once this patch is merged, I'll put up a PR for having MLIR use these 
> additions.

There is some code that is related to Flang/MLIR that shouldn't be in the 
patch. e.g. ReductionInfoManager is not used anywhere. I am currently working 
on a patch that refactors this code a bit and how it is used in the MLIR 
lowering.

https://github.com/llvm/llvm-project/pull/80343
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [mlir] [clang] [OpenMP] Migrate GPU Reductions CodeGen from Clang to OMPIRBuilder (PR #80343)

2024-02-06 Thread Jan Leyonberg via cfe-commits


@@ -2042,35 +2057,1378 @@ OpenMPIRBuilder::createSection(const 
LocationDescription &Loc,
   /*IsCancellable*/ true);
 }
 
-/// Create a function with a unique name and a "void (i8*, i8*)" signature in
-/// the given module and return it.
-Function *getFreshReductionFunc(Module &M) {
-  Type *VoidTy = Type::getVoidTy(M.getContext());
-  Type *Int8PtrTy = PointerType::getUnqual(M.getContext());
-  auto *FuncTy =
-  FunctionType::get(VoidTy, {Int8PtrTy, Int8PtrTy}, /* IsVarArg */ false);
-  return Function::Create(FuncTy, GlobalVariable::InternalLinkage,
-  M.getDataLayout().getDefaultGlobalsAddressSpace(),
-  ".omp.reduction.func", &M);
+static OpenMPIRBuilder::InsertPointTy getInsertPointAfterInstr(Instruction *I) 
{
+  BasicBlock::iterator IT(I);
+  IT++;
+  return OpenMPIRBuilder::InsertPointTy(I->getParent(), IT);
 }
 
-OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createReductions(
-const LocationDescription &Loc, InsertPointTy AllocaIP,
-ArrayRef ReductionInfos, bool IsNoWait) {
-  for (const ReductionInfo &RI : ReductionInfos) {
+void OpenMPIRBuilder::emitUsed(StringRef Name,
+   std::vector &List) {
+  if (List.empty())
+return;
+
+  // Convert List to what ConstantArray needs.
+  SmallVector UsedArray;
+  UsedArray.resize(List.size());
+  for (unsigned I = 0, E = List.size(); I != E; ++I)
+UsedArray[I] = ConstantExpr::getPointerBitCastOrAddrSpaceCast(
+cast(&*List[I]), Builder.getPtrTy());
+
+  if (UsedArray.empty())
+return;
+  ArrayType *ATy = ArrayType::get(Builder.getPtrTy(), UsedArray.size());
+
+  auto *GV = new GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
+ConstantArray::get(ATy, UsedArray), Name);
+
+  GV->setSection("llvm.metadata");
+}
+
+Value *OpenMPIRBuilder::getGPUThreadID() {
+  return Builder.CreateCall(
+  getOrCreateRuntimeFunction(M,
+ 
OMPRTL___kmpc_get_hardware_thread_id_in_block),
+  {});
+}
+
+Value *OpenMPIRBuilder::getGPUWarpSize() {
+  return Builder.CreateCall(
+  getOrCreateRuntimeFunction(M, OMPRTL___kmpc_get_warp_size), {});
+}
+
+Value *OpenMPIRBuilder::getNVPTXWarpID() {
+  unsigned LaneIDBits = Log2_32(Config.getGridValue().GV_Warp_Size);
+  return Builder.CreateAShr(getGPUThreadID(), LaneIDBits, "nvptx_warp_id");
+}
+
+Value *OpenMPIRBuilder::getNVPTXLaneID() {
+  unsigned LaneIDBits = Log2_32(Config.getGridValue().GV_Warp_Size);
+  assert(LaneIDBits < 32 && "Invalid LaneIDBits size in NVPTX device.");
+  unsigned LaneIDMask = ~0u >> (32u - LaneIDBits);
+  return Builder.CreateAnd(getGPUThreadID(), Builder.getInt32(LaneIDMask),
+   "nvptx_lane_id");
+}
+
+Value *OpenMPIRBuilder::castValueToType(InsertPointTy AllocaIP, Value *From,
+Type *ToType) {
+  Type *FromType = From->getType();
+  uint64_t FromSize = M.getDataLayout().getTypeStoreSize(FromType);
+  uint64_t ToSize = M.getDataLayout().getTypeStoreSize(ToType);
+  assert(FromSize > 0 && "From size must be greater than zero");
+  assert(ToSize > 0 && "To size must be greater than zero");
+  if (FromType == ToType)
+return From;
+  if (FromSize == ToSize)
+return Builder.CreateBitCast(From, ToType);
+  if (ToType->isIntegerTy() && FromType->isIntegerTy())
+return Builder.CreateIntCast(From, ToType, /*isSigned*/ true);
+  InsertPointTy SaveIP = Builder.saveIP();
+  Builder.restoreIP(AllocaIP);
+  Value *CastItem = Builder.CreateAlloca(ToType);
+  Builder.restoreIP(SaveIP);
+
+  Value *ValCastItem = Builder.CreatePointerBitCastOrAddrSpaceCast(
+  CastItem, FromType->getPointerTo());
+  Builder.CreateStore(From, ValCastItem);
+  return Builder.CreateLoad(ToType, CastItem);
+}
+
+Value *OpenMPIRBuilder::createRuntimeShuffleFunction(InsertPointTy AllocaIP,
+ Value *Element,
+ Type *ElementType,
+ Value *Offset) {
+  uint64_t Size = M.getDataLayout().getTypeStoreSize(ElementType);
+  assert(Size <= 8 && "Unsupported bitwidth in shuffle instruction");
+
+  // Cast all types to 32- or 64-bit values before calling shuffle routines.
+  Type *CastTy = Builder.getIntNTy(Size <= 4 ? 32 : 64);
+  Value *ElemCast = castValueToType(AllocaIP, Element, CastTy);
+  Value *WarpSize =
+  Builder.CreateIntCast(getGPUWarpSize(), Builder.getInt16Ty(), true);
+  Function *ShuffleFunc = getOrCreateRuntimeFunctionPtr(
+  Size <= 4 ? RuntimeFunction::OMPRTL___kmpc_shuffle_int32
+: RuntimeFunction::OMPRTL___kmpc_shuffle_int64);
+  Value *WarpSizeCast =
+  Builder.CreateIntCast(WarpSize, Builder.getInt16Ty(), /*isSigned=*/true);
+  Value *ShuffleCall =
+  Builder.CreateCall(ShuffleFunc, {ElemCast, Offset, WarpSizeCast});
+  return castV

[clang] [OpenMP] Remove complex reduction variable support (PR #82497)

2024-03-04 Thread Jan Leyonberg via cfe-commits

jsjodin wrote:

@alexey-bataev do you have any concerns about removing this case?


https://github.com/llvm/llvm-project/pull/82497
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)

2024-06-07 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin created 
https://github.com/llvm/llvm-project/pull/94763

This patch enables the -mlink-builtin-bitcode flag in fc1 so that bitcode 
libraries can be linked in. This is needed for OpenMP offloading libraries.

>From adfeb69ba312e971cb4eae71f185f38b1cac9b8f Mon Sep 17 00:00:00 2001
From: Jan Leyonberg 
Date: Wed, 5 Jun 2024 10:56:41 -0400
Subject: [PATCH] [flang] Add -mlink-builtin-bitcode option to fc1

This patch enables the -mlink-builtin-bitcode flag in fc1 so that bitcode
libraries can be linked in. This is needed for OpenMP offloading libraries.
---
 clang/include/clang/Driver/Options.td |   9 ++-
 flang/include/flang/Frontend/CodeGenOptions.h |   4 ++
 .../include/flang/Frontend/FrontendActions.h  |   5 +-
 flang/lib/Frontend/CompilerInvocation.cpp |   5 ++
 flang/lib/Frontend/FrontendActions.cpp|  56 ++
 flang/test/Driver/Inputs/bclib.bc | Bin 0 -> 1700 bytes
 flang/test/Driver/mlink-builtin-bc.f90|  18 ++
 7 files changed, 93 insertions(+), 4 deletions(-)
 create mode 100644 flang/test/Driver/Inputs/bclib.bc
 create mode 100644 flang/test/Driver/mlink-builtin-bc.f90

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index d44faa55c456f..490538ce753e0 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -7028,6 +7028,12 @@ def as_secure_log_file : Separate<["-"], 
"as-secure-log-file">,
 
 } // let Visibility = [CC1Option, CC1AsOption]
 
+let Visibility = [CC1Option, FC1Option] in {
+def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
+  HelpText<"Link and internalize needed symbols from the given bitcode file "
+   "before performing optimizations.">;
+} // let Visibility = [CC1Option, FC1Option]
+
 let Visibility = [CC1Option] in {
 
 def llvm_verify_each : Flag<["-"], "llvm-verify-each">,
@@ -7130,9 +7136,6 @@ defm constructor_aliases : 
BoolMOption<"constructor-aliases",
   " emitting complete constructors and destructors as aliases when 
possible">>;
 def mlink_bitcode_file : Separate<["-"], "mlink-bitcode-file">,
   HelpText<"Link the given bitcode file before performing optimizations.">;
-def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
-  HelpText<"Link and internalize needed symbols from the given bitcode file "
-   "before performing optimizations.">;
 defm link_builtin_bitcode_postopt: BoolMOption<"link-builtin-bitcode-postopt",
   CodeGenOpts<"LinkBitcodePostopt">, DefaultFalse,
   PosFlag OffloadObjects;
 
+  /// List of filenames passed in using the -mlink-builtin-bitcode. These
+  /// are bc libraries that should be linked in and internalized;
+  std::vector BuiltinBCLibs;
+
   /// The directory where temp files are stored if specified by -save-temps
   std::optional SaveTempsDir;
 
diff --git a/flang/include/flang/Frontend/FrontendActions.h 
b/flang/include/flang/Frontend/FrontendActions.h
index 7823565eb815f..0a9a9c3401d1d 100644
--- a/flang/include/flang/Frontend/FrontendActions.h
+++ b/flang/include/flang/Frontend/FrontendActions.h
@@ -223,9 +223,12 @@ class CodeGenAction : public FrontendAction {
   std::unique_ptr llvmCtx;
   std::unique_ptr llvmModule;
 
-  /// Embeds offload objects given with specified with -fembed-offload-object
+  /// Embeds offload objects specified with -fembed-offload-object
   void embedOffloadObjects();
 
+  /// Links in BC libraries spefified with -fmlink-builtin-bitcode
+  void linkBuiltinBCLibs();
+
   /// Runs pass pipeline to lower HLFIR into FIR
   void lowerHLFIRToFIR();
 
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp 
b/flang/lib/Frontend/CompilerInvocation.cpp
index f64a939b785ef..f96d72f1ad691 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -347,6 +347,11 @@ static void 
parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
   if (auto *a = args.getLastArg(clang::driver::options::OPT_save_temps_EQ))
 opts.SaveTempsDir = a->getValue();
 
+  // -mlink-builtin-bitcode
+  for (auto *a :
+   args.filtered(clang::driver::options::OPT_mlink_builtin_bitcode))
+opts.BuiltinBCLibs.push_back(a->getValue());
+
   // -mrelocation-model option.
   if (const llvm::opt::Arg *a =
   args.getLastArg(clang::driver::options::OPT_mrelocation_model)) {
diff --git a/flang/lib/Frontend/FrontendActions.cpp 
b/flang/lib/Frontend/FrontendActions.cpp
index b1b6391f1439c..c2e6af58ffd79 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -43,6 +43,8 @@
 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticFrontend.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/FileSystemOptions.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
@@ -54,6 +56,7 @@
 #include "

[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)

2024-06-10 Thread Jan Leyonberg via cfe-commits


@@ -0,0 +1,18 @@
+
+!--
+! RUN lines
+!--
+! Embed something that can be easily checked
+! RUN: %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu -o - 
-mlink-builtin-bitcode %S/Inputs/bclib.bc %s 2>&1 | FileCheck %s

jsjodin wrote:

The triple matters because when linking the data layout in bclib.bc must match 
the data layout of the compiled source code, and depending on the host it may 
not be the same so it is safest to specify the triple to ensure they are the 
same.

https://github.com/llvm/llvm-project/pull/94763
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)

2024-06-10 Thread Jan Leyonberg via cfe-commits


@@ -1146,6 +1150,54 @@ void CodeGenAction::embedOffloadObjects() {
   }
 }
 
+void CodeGenAction::linkBuiltinBCLibs() {

jsjodin wrote:

It does come from clang but it is distributed across a few classes with a lot 
more options, so there was not simple way to simply re-use the code. I don't 
know if we want to bring in all that code since it would add a lot more 
complexity, and I don't know if we need this in the future or not.

https://github.com/llvm/llvm-project/pull/94763
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)

2024-06-10 Thread Jan Leyonberg via cfe-commits




jsjodin wrote:

Yes, it should be possible to add a command to generate the .bc file, that is 
probably a better option anyway since we would avoid specifying the triple.

https://github.com/llvm/llvm-project/pull/94763
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)

2024-06-10 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin edited 
https://github.com/llvm/llvm-project/pull/94763
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)

2024-06-11 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin updated 
https://github.com/llvm/llvm-project/pull/94763

>From adfeb69ba312e971cb4eae71f185f38b1cac9b8f Mon Sep 17 00:00:00 2001
From: Jan Leyonberg 
Date: Wed, 5 Jun 2024 10:56:41 -0400
Subject: [PATCH 1/2] [flang] Add -mlink-builtin-bitcode option to fc1

This patch enables the -mlink-builtin-bitcode flag in fc1 so that bitcode
libraries can be linked in. This is needed for OpenMP offloading libraries.
---
 clang/include/clang/Driver/Options.td |   9 ++-
 flang/include/flang/Frontend/CodeGenOptions.h |   4 ++
 .../include/flang/Frontend/FrontendActions.h  |   5 +-
 flang/lib/Frontend/CompilerInvocation.cpp |   5 ++
 flang/lib/Frontend/FrontendActions.cpp|  56 ++
 flang/test/Driver/Inputs/bclib.bc | Bin 0 -> 1700 bytes
 flang/test/Driver/mlink-builtin-bc.f90|  18 ++
 7 files changed, 93 insertions(+), 4 deletions(-)
 create mode 100644 flang/test/Driver/Inputs/bclib.bc
 create mode 100644 flang/test/Driver/mlink-builtin-bc.f90

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index d44faa55c456f..490538ce753e0 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -7028,6 +7028,12 @@ def as_secure_log_file : Separate<["-"], 
"as-secure-log-file">,
 
 } // let Visibility = [CC1Option, CC1AsOption]
 
+let Visibility = [CC1Option, FC1Option] in {
+def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
+  HelpText<"Link and internalize needed symbols from the given bitcode file "
+   "before performing optimizations.">;
+} // let Visibility = [CC1Option, FC1Option]
+
 let Visibility = [CC1Option] in {
 
 def llvm_verify_each : Flag<["-"], "llvm-verify-each">,
@@ -7130,9 +7136,6 @@ defm constructor_aliases : 
BoolMOption<"constructor-aliases",
   " emitting complete constructors and destructors as aliases when 
possible">>;
 def mlink_bitcode_file : Separate<["-"], "mlink-bitcode-file">,
   HelpText<"Link the given bitcode file before performing optimizations.">;
-def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
-  HelpText<"Link and internalize needed symbols from the given bitcode file "
-   "before performing optimizations.">;
 defm link_builtin_bitcode_postopt: BoolMOption<"link-builtin-bitcode-postopt",
   CodeGenOpts<"LinkBitcodePostopt">, DefaultFalse,
   PosFlag OffloadObjects;
 
+  /// List of filenames passed in using the -mlink-builtin-bitcode. These
+  /// are bc libraries that should be linked in and internalized;
+  std::vector BuiltinBCLibs;
+
   /// The directory where temp files are stored if specified by -save-temps
   std::optional SaveTempsDir;
 
diff --git a/flang/include/flang/Frontend/FrontendActions.h 
b/flang/include/flang/Frontend/FrontendActions.h
index 7823565eb815f..0a9a9c3401d1d 100644
--- a/flang/include/flang/Frontend/FrontendActions.h
+++ b/flang/include/flang/Frontend/FrontendActions.h
@@ -223,9 +223,12 @@ class CodeGenAction : public FrontendAction {
   std::unique_ptr llvmCtx;
   std::unique_ptr llvmModule;
 
-  /// Embeds offload objects given with specified with -fembed-offload-object
+  /// Embeds offload objects specified with -fembed-offload-object
   void embedOffloadObjects();
 
+  /// Links in BC libraries spefified with -fmlink-builtin-bitcode
+  void linkBuiltinBCLibs();
+
   /// Runs pass pipeline to lower HLFIR into FIR
   void lowerHLFIRToFIR();
 
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp 
b/flang/lib/Frontend/CompilerInvocation.cpp
index f64a939b785ef..f96d72f1ad691 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -347,6 +347,11 @@ static void 
parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
   if (auto *a = args.getLastArg(clang::driver::options::OPT_save_temps_EQ))
 opts.SaveTempsDir = a->getValue();
 
+  // -mlink-builtin-bitcode
+  for (auto *a :
+   args.filtered(clang::driver::options::OPT_mlink_builtin_bitcode))
+opts.BuiltinBCLibs.push_back(a->getValue());
+
   // -mrelocation-model option.
   if (const llvm::opt::Arg *a =
   args.getLastArg(clang::driver::options::OPT_mrelocation_model)) {
diff --git a/flang/lib/Frontend/FrontendActions.cpp 
b/flang/lib/Frontend/FrontendActions.cpp
index b1b6391f1439c..c2e6af58ffd79 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -43,6 +43,8 @@
 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticFrontend.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/FileSystemOptions.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
@@ -54,6 +56,7 @@
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Linker/Linker.h"
 #include "llvm/Ob

[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)

2024-06-11 Thread Jan Leyonberg via cfe-commits


@@ -0,0 +1,18 @@
+
+!--
+! RUN lines
+!--
+! Embed something that can be easily checked
+! RUN: %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu -o - 
-mlink-builtin-bitcode %S/Inputs/bclib.bc %s 2>&1 | FileCheck %s
+
+! CHECK: define internal void @libfun_
+
+! RUN1: not %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu -o - 
-mlink-builtin-bitcode %S/Inputs/no-bclib.bc %s 2>&1 | FileCheck %s

jsjodin wrote:

My mistake, I was testing things out and it got in.

https://github.com/llvm/llvm-project/pull/94763
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)

2024-06-11 Thread Jan Leyonberg via cfe-commits


@@ -0,0 +1,18 @@
+
+!--
+! RUN lines
+!--
+! Embed something that can be easily checked
+! RUN: %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu -o - 
-mlink-builtin-bitcode %S/Inputs/bclib.bc %s 2>&1 | FileCheck %s
+
+! CHECK: define internal void @libfun_
+
+! RUN1: not %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu -o - 
-mlink-builtin-bitcode %S/Inputs/no-bclib.bc %s 2>&1 | FileCheck %s
+
+! ERROR1: error: could not open {{.*}} no-bclib.bc

jsjodin wrote:

Same.

https://github.com/llvm/llvm-project/pull/94763
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)

2024-06-12 Thread Jan Leyonberg via cfe-commits


@@ -0,0 +1,15 @@
+! Test -mlink-builtin-bitcode flag
+! RUN: %flang -emit-llvm -c -o %t.bc %S/Inputs/libfun.f90
+! RUN: %flang_fc1 -emit-llvm -o - -mlink-builtin-bitcode %t.bc %s 2>&1 | 
FileCheck %s
+
+! CHECK: define internal void @libfun_
+
+! RUN: not %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu -o - 
-mlink-builtin-bitcode %no-%t.bc %s 2>&1 | FileCheck %s --check-prefix=ERROR

jsjodin wrote:

Yes, I forgot to remove this when I fixed the tests. Thanks for finding it.

https://github.com/llvm/llvm-project/pull/94763
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)

2024-06-12 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin updated 
https://github.com/llvm/llvm-project/pull/94763

>From adfeb69ba312e971cb4eae71f185f38b1cac9b8f Mon Sep 17 00:00:00 2001
From: Jan Leyonberg 
Date: Wed, 5 Jun 2024 10:56:41 -0400
Subject: [PATCH 1/3] [flang] Add -mlink-builtin-bitcode option to fc1

This patch enables the -mlink-builtin-bitcode flag in fc1 so that bitcode
libraries can be linked in. This is needed for OpenMP offloading libraries.
---
 clang/include/clang/Driver/Options.td |   9 ++-
 flang/include/flang/Frontend/CodeGenOptions.h |   4 ++
 .../include/flang/Frontend/FrontendActions.h  |   5 +-
 flang/lib/Frontend/CompilerInvocation.cpp |   5 ++
 flang/lib/Frontend/FrontendActions.cpp|  56 ++
 flang/test/Driver/Inputs/bclib.bc | Bin 0 -> 1700 bytes
 flang/test/Driver/mlink-builtin-bc.f90|  18 ++
 7 files changed, 93 insertions(+), 4 deletions(-)
 create mode 100644 flang/test/Driver/Inputs/bclib.bc
 create mode 100644 flang/test/Driver/mlink-builtin-bc.f90

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index d44faa55c456f..490538ce753e0 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -7028,6 +7028,12 @@ def as_secure_log_file : Separate<["-"], 
"as-secure-log-file">,
 
 } // let Visibility = [CC1Option, CC1AsOption]
 
+let Visibility = [CC1Option, FC1Option] in {
+def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
+  HelpText<"Link and internalize needed symbols from the given bitcode file "
+   "before performing optimizations.">;
+} // let Visibility = [CC1Option, FC1Option]
+
 let Visibility = [CC1Option] in {
 
 def llvm_verify_each : Flag<["-"], "llvm-verify-each">,
@@ -7130,9 +7136,6 @@ defm constructor_aliases : 
BoolMOption<"constructor-aliases",
   " emitting complete constructors and destructors as aliases when 
possible">>;
 def mlink_bitcode_file : Separate<["-"], "mlink-bitcode-file">,
   HelpText<"Link the given bitcode file before performing optimizations.">;
-def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
-  HelpText<"Link and internalize needed symbols from the given bitcode file "
-   "before performing optimizations.">;
 defm link_builtin_bitcode_postopt: BoolMOption<"link-builtin-bitcode-postopt",
   CodeGenOpts<"LinkBitcodePostopt">, DefaultFalse,
   PosFlag OffloadObjects;
 
+  /// List of filenames passed in using the -mlink-builtin-bitcode. These
+  /// are bc libraries that should be linked in and internalized;
+  std::vector BuiltinBCLibs;
+
   /// The directory where temp files are stored if specified by -save-temps
   std::optional SaveTempsDir;
 
diff --git a/flang/include/flang/Frontend/FrontendActions.h 
b/flang/include/flang/Frontend/FrontendActions.h
index 7823565eb815f..0a9a9c3401d1d 100644
--- a/flang/include/flang/Frontend/FrontendActions.h
+++ b/flang/include/flang/Frontend/FrontendActions.h
@@ -223,9 +223,12 @@ class CodeGenAction : public FrontendAction {
   std::unique_ptr llvmCtx;
   std::unique_ptr llvmModule;
 
-  /// Embeds offload objects given with specified with -fembed-offload-object
+  /// Embeds offload objects specified with -fembed-offload-object
   void embedOffloadObjects();
 
+  /// Links in BC libraries spefified with -fmlink-builtin-bitcode
+  void linkBuiltinBCLibs();
+
   /// Runs pass pipeline to lower HLFIR into FIR
   void lowerHLFIRToFIR();
 
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp 
b/flang/lib/Frontend/CompilerInvocation.cpp
index f64a939b785ef..f96d72f1ad691 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -347,6 +347,11 @@ static void 
parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
   if (auto *a = args.getLastArg(clang::driver::options::OPT_save_temps_EQ))
 opts.SaveTempsDir = a->getValue();
 
+  // -mlink-builtin-bitcode
+  for (auto *a :
+   args.filtered(clang::driver::options::OPT_mlink_builtin_bitcode))
+opts.BuiltinBCLibs.push_back(a->getValue());
+
   // -mrelocation-model option.
   if (const llvm::opt::Arg *a =
   args.getLastArg(clang::driver::options::OPT_mrelocation_model)) {
diff --git a/flang/lib/Frontend/FrontendActions.cpp 
b/flang/lib/Frontend/FrontendActions.cpp
index b1b6391f1439c..c2e6af58ffd79 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -43,6 +43,8 @@
 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticFrontend.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/FileSystemOptions.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
@@ -54,6 +56,7 @@
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Linker/Linker.h"
 #include "llvm/Ob

[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)

2024-06-13 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin updated 
https://github.com/llvm/llvm-project/pull/94763

>From adfeb69ba312e971cb4eae71f185f38b1cac9b8f Mon Sep 17 00:00:00 2001
From: Jan Leyonberg 
Date: Wed, 5 Jun 2024 10:56:41 -0400
Subject: [PATCH 1/4] [flang] Add -mlink-builtin-bitcode option to fc1

This patch enables the -mlink-builtin-bitcode flag in fc1 so that bitcode
libraries can be linked in. This is needed for OpenMP offloading libraries.
---
 clang/include/clang/Driver/Options.td |   9 ++-
 flang/include/flang/Frontend/CodeGenOptions.h |   4 ++
 .../include/flang/Frontend/FrontendActions.h  |   5 +-
 flang/lib/Frontend/CompilerInvocation.cpp |   5 ++
 flang/lib/Frontend/FrontendActions.cpp|  56 ++
 flang/test/Driver/Inputs/bclib.bc | Bin 0 -> 1700 bytes
 flang/test/Driver/mlink-builtin-bc.f90|  18 ++
 7 files changed, 93 insertions(+), 4 deletions(-)
 create mode 100644 flang/test/Driver/Inputs/bclib.bc
 create mode 100644 flang/test/Driver/mlink-builtin-bc.f90

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index d44faa55c456f..490538ce753e0 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -7028,6 +7028,12 @@ def as_secure_log_file : Separate<["-"], 
"as-secure-log-file">,
 
 } // let Visibility = [CC1Option, CC1AsOption]
 
+let Visibility = [CC1Option, FC1Option] in {
+def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
+  HelpText<"Link and internalize needed symbols from the given bitcode file "
+   "before performing optimizations.">;
+} // let Visibility = [CC1Option, FC1Option]
+
 let Visibility = [CC1Option] in {
 
 def llvm_verify_each : Flag<["-"], "llvm-verify-each">,
@@ -7130,9 +7136,6 @@ defm constructor_aliases : 
BoolMOption<"constructor-aliases",
   " emitting complete constructors and destructors as aliases when 
possible">>;
 def mlink_bitcode_file : Separate<["-"], "mlink-bitcode-file">,
   HelpText<"Link the given bitcode file before performing optimizations.">;
-def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
-  HelpText<"Link and internalize needed symbols from the given bitcode file "
-   "before performing optimizations.">;
 defm link_builtin_bitcode_postopt: BoolMOption<"link-builtin-bitcode-postopt",
   CodeGenOpts<"LinkBitcodePostopt">, DefaultFalse,
   PosFlag OffloadObjects;
 
+  /// List of filenames passed in using the -mlink-builtin-bitcode. These
+  /// are bc libraries that should be linked in and internalized;
+  std::vector BuiltinBCLibs;
+
   /// The directory where temp files are stored if specified by -save-temps
   std::optional SaveTempsDir;
 
diff --git a/flang/include/flang/Frontend/FrontendActions.h 
b/flang/include/flang/Frontend/FrontendActions.h
index 7823565eb815f..0a9a9c3401d1d 100644
--- a/flang/include/flang/Frontend/FrontendActions.h
+++ b/flang/include/flang/Frontend/FrontendActions.h
@@ -223,9 +223,12 @@ class CodeGenAction : public FrontendAction {
   std::unique_ptr llvmCtx;
   std::unique_ptr llvmModule;
 
-  /// Embeds offload objects given with specified with -fembed-offload-object
+  /// Embeds offload objects specified with -fembed-offload-object
   void embedOffloadObjects();
 
+  /// Links in BC libraries spefified with -fmlink-builtin-bitcode
+  void linkBuiltinBCLibs();
+
   /// Runs pass pipeline to lower HLFIR into FIR
   void lowerHLFIRToFIR();
 
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp 
b/flang/lib/Frontend/CompilerInvocation.cpp
index f64a939b785ef..f96d72f1ad691 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -347,6 +347,11 @@ static void 
parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
   if (auto *a = args.getLastArg(clang::driver::options::OPT_save_temps_EQ))
 opts.SaveTempsDir = a->getValue();
 
+  // -mlink-builtin-bitcode
+  for (auto *a :
+   args.filtered(clang::driver::options::OPT_mlink_builtin_bitcode))
+opts.BuiltinBCLibs.push_back(a->getValue());
+
   // -mrelocation-model option.
   if (const llvm::opt::Arg *a =
   args.getLastArg(clang::driver::options::OPT_mrelocation_model)) {
diff --git a/flang/lib/Frontend/FrontendActions.cpp 
b/flang/lib/Frontend/FrontendActions.cpp
index b1b6391f1439c..c2e6af58ffd79 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -43,6 +43,8 @@
 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticFrontend.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/FileSystemOptions.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
@@ -54,6 +56,7 @@
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Linker/Linker.h"
 #include "llvm/Ob

[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)

2024-06-17 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin closed 
https://github.com/llvm/llvm-project/pull/94763
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][OpenMP] Propagate debug location to OMPIRBuilder reduction codegen (PR #100358)

2024-07-24 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin edited 
https://github.com/llvm/llvm-project/pull/100358
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [OpenMP] Migrate GPU Reductions CodeGen from Clang to OMPIRBuilder (PR #80343)

2024-04-24 Thread Jan Leyonberg via cfe-commits


@@ -607,15 +621,17 @@ class OpenMPIRBuilder {
   /// Generator for '#omp barrier'
   ///
   /// \param Loc The location where the barrier directive was encountered.
-  /// \param DK The kind of directive that caused the barrier.
+  /// \param Kind The kind of directive that caused the barrier.
   /// \param ForceSimpleCall Flag to force a simple (=non-cancellation) 
barrier.
   /// \param CheckCancelFlag Flag to indicate a cancel barrier return value
   ///should be checked and acted upon.
+  /// \param ThreadID Optional parameter to pass in any existing ThreadID 
value.
   ///
   /// \returns The insertion point after the barrier.
-  InsertPointTy createBarrier(const LocationDescription &Loc, omp::Directive 
DK,
-  bool ForceSimpleCall = false,
-  bool CheckCancelFlag = true);
+  InsertPointTy createBarrier(const LocationDescription &Loc,
+  omp::Directive Kind, bool ForceSimpleCall = 
false,
+  bool CheckCancelFlag = true,
+  Value *ThreadID = nullptr);

jsjodin wrote:

The ThreadID doesn't seem to be passed in the original code. If there was reuse 
before perhaps some optimization is not happening? Probably worth looking into 
the cause of this instead of adding the parameter.

https://github.com/llvm/llvm-project/pull/80343
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [OpenMPIRBuilder][Clang][NFC] - Combine `emitOffloadingArrays` and `emitOffloadingArraysArgument` in OpenMPIRBuilder (PR #97088)

2024-07-12 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin edited 
https://github.com/llvm/llvm-project/pull/97088
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [OpenMPIRBuilder][Clang][NFC] - Combine `emitOffloadingArrays` and `emitOffloadingArraysArgument` in OpenMPIRBuilder (PR #97088)

2024-07-12 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin approved this pull request.

One nit, otherwise LGTM.


https://github.com/llvm/llvm-project/pull/97088
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [OpenMPIRBuilder][Clang][NFC] - Combine `emitOffloadingArrays` and `emitOffloadingArraysArgument` in OpenMPIRBuilder (PR #97088)

2024-07-12 Thread Jan Leyonberg via cfe-commits


@@ -9536,18 +9506,65 @@ static void emitTargetCallKernelLaunch(
   MEHandler.adjustMemberOfForLambdaCaptures(
   OMPBuilder, LambdaPointers, CombinedInfo.BasePointers,
   CombinedInfo.Pointers, CombinedInfo.Types);
+}
+static void
+genMapInfo(MappableExprsHandler &MEHandler, CodeGenFunction &CGF,
+   MappableExprsHandler::MapCombinedInfoTy &CombinedInfo,
+   llvm::OpenMPIRBuilder &OMPBuilder,
+   const llvm::DenseSet> &SkippedVarSet =
+   llvm::DenseSet>()) {
+
+  CodeGenModule &CGM = CGF.CGM;
   // Map any list items in a map clause that were not captures because they
   // weren't referenced within the construct.
-  MEHandler.generateAllInfo(CombinedInfo, OMPBuilder, MappedVarSet);
+  MEHandler.generateAllInfo(CombinedInfo, OMPBuilder, SkippedVarSet);
+
+  auto FillInfoMap = [&](MappableExprsHandler::MappingExprInfo &MapExpr) {
+return emitMappingInformation(CGF, OMPBuilder, MapExpr);
+  };
+  if (CGM.getCodeGenOpts().getDebugInfo() !=
+  llvm::codegenoptions::NoDebugInfo) {
+CombinedInfo.Names.resize(CombinedInfo.Exprs.size());
+llvm::transform(CombinedInfo.Exprs, CombinedInfo.Names.begin(),
+FillInfoMap);
+  }
+}
+
+static void genMapInfo(const OMPExecutableDirective &D, CodeGenFunction &CGF,
+   const CapturedStmt &CS,
+   llvm::SmallVectorImpl &CapturedVars,
+   llvm::OpenMPIRBuilder &OMPBuilder,
+   MappableExprsHandler::MapCombinedInfoTy &CombinedInfo) {
+  // Get mappable expression information.
+  MappableExprsHandler MEHandler(D, CGF);
+  llvm::DenseMap LambdaPointers;

jsjodin wrote:

Instead of passing in LambdaPointers, can it be internal to 
getMapInfoForCaptures? It doesn't seem to be used outside this function.

https://github.com/llvm/llvm-project/pull/97088
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [OpenMP] Migrate GPU Reductions CodeGen from Clang to OMPIRBuilder (PR #80343)

2024-06-25 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin approved this pull request.

This looks good to me now. You can wait a few days before merging to let 
@jdoerfert look it over again.


https://github.com/llvm/llvm-project/pull/80343
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [OpenMP][Clang] Migrate OpenMP UserDefinedMapper from Clang to OMPIRBuilder (PR #110001)

2024-11-26 Thread Jan Leyonberg via cfe-commits


@@ -9041,257 +9041,65 @@ void CGOpenMPRuntime::emitUserDefinedMapper(const 
OMPDeclareMapperDecl *D,
 return;
   ASTContext &C = CGM.getContext();
   QualType Ty = D->getType();
-  QualType PtrTy = C.getPointerType(Ty).withRestrict();
-  QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, 
/*Signed=*/true);
   auto *MapperVarDecl =
   cast(cast(D->getMapperVarRef())->getDecl());
-  SourceLocation Loc = D->getLocation();

jsjodin wrote:

Is the location information propagated? I am wondering if debug info might be 
lost like what happened in the reduction migration patch.

https://github.com/llvm/llvm-project/pull/110001
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [OpenMP][Clang] Migrate OpenMP UserDefinedMapper from Clang to OMPIRBuilder (PR #110001)

2024-12-17 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/110001
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [MLIR][OpenMP] Add codegen for teams reductions (PR #133310)

2025-04-03 Thread Jan Leyonberg via cfe-commits


@@ -4645,12 +4789,23 @@ initTargetDefaultAttrs(omp::TargetOp targetOp, 
Operation *capturedOp,
   (maxThreadsVal >= 0 && maxThreadsVal < combinedMaxThreadsVal))
 combinedMaxThreadsVal = maxThreadsVal;
 
+  // Calculate reduction data size, limited to single reduction variable for
+  // now.
+  int32_t reductionDataSize = 0;
+  if (isGPU && capturedOp) {
+if (auto teamsOp = castOrGetParentOfType(capturedOp))
+  reductionDataSize = getReductionDataSize(teamsOp);
+  }
+
   // Update kernel bounds structure for the `OpenMPIRBuilder` to use.
   attrs.ExecFlags = targetOp.getKernelExecFlags(capturedOp);
   attrs.MinTeams = minTeamsVal;
   attrs.MaxTeams.front() = maxTeamsVal;
   attrs.MinThreads = 1;
   attrs.MaxThreads.front() = combinedMaxThreadsVal;
+  attrs.ReductionDataSize = reductionDataSize;
+  if (attrs.ReductionDataSize != 0)
+attrs.ReductionBufferLength = 1024;

jsjodin wrote:

I looked into this a bit more and it is basically a fixed value in clang that 
can be modified with the fopenmp-cuda-teams-reduction-recs-num flang. I added a 
TODO for now.

https://github.com/llvm/llvm-project/pull/133310
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [MLIR][OpenMP] Add codegen for teams reductions (PR #133310)

2025-04-03 Thread Jan Leyonberg via cfe-commits


@@ -3688,27 +3708,95 @@ static Function *getFreshReductionFunc(Module &M) {
   ".omp.reduction.func", &M);
 }
 
-OpenMPIRBuilder::InsertPointOrErrorTy
-OpenMPIRBuilder::createReductions(const LocationDescription &Loc,
-  InsertPointTy AllocaIP,
-  ArrayRef ReductionInfos,
-  ArrayRef IsByRef, bool IsNoWait) {
-  assert(ReductionInfos.size() == IsByRef.size());
-  for (const ReductionInfo &RI : ReductionInfos) {
-(void)RI;
-assert(RI.Variable && "expected non-null variable");
-assert(RI.PrivateVariable && "expected non-null private variable");
-assert(RI.ReductionGen && "expected non-null reduction generator 
callback");
-assert(RI.Variable->getType() == RI.PrivateVariable->getType() &&
-   "expected variables and their private equivalents to have the same "
-   "type");
-assert(RI.Variable->getType()->isPointerTy() &&
-   "expected variables to be pointers");
+static Error populateReductionFunction(
+Function *ReductionFunc,
+ArrayRef ReductionInfos,
+IRBuilder<> &Builder, ArrayRef IsByRef, bool IsGPU) {
+  Module *Module = ReductionFunc->getParent();
+  BasicBlock *ReductionFuncBlock =
+  BasicBlock::Create(Module->getContext(), "", ReductionFunc);
+  Builder.SetInsertPoint(ReductionFuncBlock);
+  Value *LHSArrayPtr = nullptr;
+  Value *RHSArrayPtr = nullptr;
+  if (IsGPU) {
+// Need to alloca memory here and deal with the pointers before getting
+// LHS/RHS pointers out
+//
+Argument *Arg0 = ReductionFunc->getArg(0);
+Argument *Arg1 = ReductionFunc->getArg(1);
+Type *Arg0Type = Arg0->getType();
+Type *Arg1Type = Arg1->getType();
+
+Value *LHSAlloca =
+Builder.CreateAlloca(Arg0Type, nullptr, Arg0->getName() + ".addr");
+Value *RHSAlloca =
+Builder.CreateAlloca(Arg1Type, nullptr, Arg1->getName() + ".addr");
+Value *LHSAddrCast =
+Builder.CreatePointerBitCastOrAddrSpaceCast(LHSAlloca, Arg0Type);
+Value *RHSAddrCast =
+Builder.CreatePointerBitCastOrAddrSpaceCast(RHSAlloca, Arg1Type);
+Builder.CreateStore(Arg0, LHSAddrCast);
+Builder.CreateStore(Arg1, RHSAddrCast);
+LHSArrayPtr = Builder.CreateLoad(Arg0Type, LHSAddrCast);
+RHSArrayPtr = Builder.CreateLoad(Arg1Type, RHSAddrCast);
+  } else {
+LHSArrayPtr = ReductionFunc->getArg(0);
+RHSArrayPtr = ReductionFunc->getArg(1);
   }
 
+  unsigned NumReductions = ReductionInfos.size();
+  Type *RedArrayTy = ArrayType::get(Builder.getPtrTy(), NumReductions);
+
+  for (auto En : enumerate(ReductionInfos)) {
+const OpenMPIRBuilder::ReductionInfo &RI = En.value();
+Value *LHSI8PtrPtr = Builder.CreateConstInBoundsGEP2_64(
+RedArrayTy, LHSArrayPtr, 0, En.index());
+Value *LHSI8Ptr = Builder.CreateLoad(Builder.getPtrTy(), LHSI8PtrPtr);
+Value *LHSPtr = Builder.CreatePointerBitCastOrAddrSpaceCast(
+LHSI8Ptr, RI.Variable->getType());
+Value *LHS = Builder.CreateLoad(RI.ElementType, LHSPtr);
+Value *RHSI8PtrPtr = Builder.CreateConstInBoundsGEP2_64(
+RedArrayTy, RHSArrayPtr, 0, En.index());
+Value *RHSI8Ptr = Builder.CreateLoad(Builder.getPtrTy(), RHSI8PtrPtr);
+Value *RHSPtr = Builder.CreatePointerBitCastOrAddrSpaceCast(
+RHSI8Ptr, RI.PrivateVariable->getType());
+Value *RHS = Builder.CreateLoad(RI.ElementType, RHSPtr);
+Value *Reduced;
+OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
+RI.ReductionGen(Builder.saveIP(), LHS, RHS, Reduced);
+if (!AfterIP)
+  return AfterIP.takeError();
+
+Builder.restoreIP(*AfterIP);
+// TODO: Consider flagging an error.
+if (!Builder.GetInsertBlock())
+  return Error::success();

jsjodin wrote:

I'm going to skip this for now. I have some thoughts how to improve the IP 
handling, but we can discuss that separately.

https://github.com/llvm/llvm-project/pull/133310
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [MLIR][OpenMP] Add codegen for teams reductions (PR #133310)

2025-03-27 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin created 
https://github.com/llvm/llvm-project/pull/133310

This patch adds the lowering of teams reductions from the omp dialect to 
LLVM-IR. Some minor cleanup was done in clang to remove an unused parameter. 

>From 50fefbb31f4de7352c241c48fe5382785daaef21 Mon Sep 17 00:00:00 2001
From: Jan Leyonberg 
Date: Sun, 23 Mar 2025 09:56:51 -0400
Subject: [PATCH 1/8] Initial modifications to support reductions in flang.

---
 clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp  |  3 +-
 .../llvm/Frontend/OpenMP/OMPIRBuilder.h   |  3 --
 llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 30 +++
 3 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
index feb2448297542..d30bef9e7f0ba 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
@@ -1659,7 +1659,6 @@ void CGOpenMPRuntimeGPU::emitReduction(
 return;
 
   bool ParallelReduction = isOpenMPParallelDirective(Options.ReductionKind);
-  bool DistributeReduction = 
isOpenMPDistributeDirective(Options.ReductionKind);
   bool TeamsReduction = isOpenMPTeamsDirective(Options.ReductionKind);
 
   ASTContext &C = CGM.getContext();
@@ -1756,7 +1755,7 @@ void CGOpenMPRuntimeGPU::emitReduction(
   llvm::OpenMPIRBuilder::InsertPointTy AfterIP =
   cantFail(OMPBuilder.createReductionsGPU(
   OmpLoc, AllocaIP, CodeGenIP, ReductionInfos, false, TeamsReduction,
-  DistributeReduction, 
llvm::OpenMPIRBuilder::ReductionGenCBKind::Clang,
+  llvm::OpenMPIRBuilder::ReductionGenCBKind::Clang,
   CGF.getTarget().getGridValue(),
   C.getLangOpts().OpenMPCUDAReductionBufNum, RTLoc));
   CGF.Builder.restoreIP(AfterIP);
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h 
b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
index 28909cef4748d..9b67d0c050e46 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -1905,8 +1905,6 @@ class OpenMPIRBuilder {
   ///   nowait.
   /// \param IsTeamsReduction   Optional flag set if it is a teams
   ///   reduction.
-  /// \param HasDistribute  Optional flag set if it is a
-  ///   distribute reduction.
   /// \param GridValue  Optional GPU grid value.
   /// \param ReductionBufNumOptional OpenMPCUDAReductionBufNumValue to be
   /// used for teams reduction.
@@ -1915,7 +1913,6 @@ class OpenMPIRBuilder {
   const LocationDescription &Loc, InsertPointTy AllocaIP,
   InsertPointTy CodeGenIP, ArrayRef ReductionInfos,
   bool IsNoWait = false, bool IsTeamsReduction = false,
-  bool HasDistribute = false,
   ReductionGenCBKind ReductionGenCBKind = ReductionGenCBKind::MLIR,
   std::optional GridValue = {}, unsigned ReductionBufNum = 1024,
   Value *SrcLocInfo = nullptr);
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp 
b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 2e5ce5308eea5..0001626086d7c 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -3495,9 +3495,9 @@ 
checkReductionInfos(ArrayRef ReductionInfos,
 OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
 const LocationDescription &Loc, InsertPointTy AllocaIP,
 InsertPointTy CodeGenIP, ArrayRef ReductionInfos,
-bool IsNoWait, bool IsTeamsReduction, bool HasDistribute,
-ReductionGenCBKind ReductionGenCBKind, std::optional GridValue,
-unsigned ReductionBufNum, Value *SrcLocInfo) {
+bool IsNoWait, bool IsTeamsReduction, ReductionGenCBKind 
ReductionGenCBKind,
+std::optional GridValue, unsigned ReductionBufNum,
+Value *SrcLocInfo) {
   if (!updateToLocation(Loc))
 return InsertPointTy();
   Builder.restoreIP(CodeGenIP);
@@ -3514,6 +3514,16 @@ OpenMPIRBuilder::InsertPointOrErrorTy 
OpenMPIRBuilder::createReductionsGPU(
   if (ReductionInfos.size() == 0)
 return Builder.saveIP();
 
+  BasicBlock *ContinuationBlock = nullptr;
+  if (ReductionGenCBKind != ReductionGenCBKind::Clang) {
+// Copied code from createReductions
+BasicBlock *InsertBlock = Loc.IP.getBlock();
+ContinuationBlock =
+InsertBlock->splitBasicBlock(Loc.IP.getPoint(), "reduce.finalize");
+InsertBlock->getTerminator()->eraseFromParent();
+Builder.SetInsertPoint(InsertBlock, InsertBlock->end());
+  }
+
   Function *CurFunc = Builder.GetInsertBlock()->getParent();
   AttributeList FuncAttrs;
   AttrBuilder AttrBldr(Ctx);
@@ -3669,11 +3679,21 @@ OpenMPIRBuilder::InsertPointOrErrorTy 
OpenMPIRBuilder::createReductionsGPU(
ReductionFunc;
   });
 } else {
-  assert(false && "Unhandled ReductionGenCBKind");
+  Value *LHSValue = Builder.CreateLoad(RI.ElementType, LHS, "final.lhs");
+  Value *RHSValue = Builder.CreateLoad(RI.ElementType, RHS, "final.r

[clang] [llvm] [mlir] [MLIR][OpenMP] Add codegen for teams reductions (PR #133310)

2025-03-27 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin updated 
https://github.com/llvm/llvm-project/pull/133310

>From 50fefbb31f4de7352c241c48fe5382785daaef21 Mon Sep 17 00:00:00 2001
From: Jan Leyonberg 
Date: Sun, 23 Mar 2025 09:56:51 -0400
Subject: [PATCH 1/9] Initial modifications to support reductions in flang.

---
 clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp  |  3 +-
 .../llvm/Frontend/OpenMP/OMPIRBuilder.h   |  3 --
 llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 30 +++
 3 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
index feb2448297542..d30bef9e7f0ba 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
@@ -1659,7 +1659,6 @@ void CGOpenMPRuntimeGPU::emitReduction(
 return;
 
   bool ParallelReduction = isOpenMPParallelDirective(Options.ReductionKind);
-  bool DistributeReduction = 
isOpenMPDistributeDirective(Options.ReductionKind);
   bool TeamsReduction = isOpenMPTeamsDirective(Options.ReductionKind);
 
   ASTContext &C = CGM.getContext();
@@ -1756,7 +1755,7 @@ void CGOpenMPRuntimeGPU::emitReduction(
   llvm::OpenMPIRBuilder::InsertPointTy AfterIP =
   cantFail(OMPBuilder.createReductionsGPU(
   OmpLoc, AllocaIP, CodeGenIP, ReductionInfos, false, TeamsReduction,
-  DistributeReduction, 
llvm::OpenMPIRBuilder::ReductionGenCBKind::Clang,
+  llvm::OpenMPIRBuilder::ReductionGenCBKind::Clang,
   CGF.getTarget().getGridValue(),
   C.getLangOpts().OpenMPCUDAReductionBufNum, RTLoc));
   CGF.Builder.restoreIP(AfterIP);
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h 
b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
index 28909cef4748d..9b67d0c050e46 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -1905,8 +1905,6 @@ class OpenMPIRBuilder {
   ///   nowait.
   /// \param IsTeamsReduction   Optional flag set if it is a teams
   ///   reduction.
-  /// \param HasDistribute  Optional flag set if it is a
-  ///   distribute reduction.
   /// \param GridValue  Optional GPU grid value.
   /// \param ReductionBufNumOptional OpenMPCUDAReductionBufNumValue to be
   /// used for teams reduction.
@@ -1915,7 +1913,6 @@ class OpenMPIRBuilder {
   const LocationDescription &Loc, InsertPointTy AllocaIP,
   InsertPointTy CodeGenIP, ArrayRef ReductionInfos,
   bool IsNoWait = false, bool IsTeamsReduction = false,
-  bool HasDistribute = false,
   ReductionGenCBKind ReductionGenCBKind = ReductionGenCBKind::MLIR,
   std::optional GridValue = {}, unsigned ReductionBufNum = 1024,
   Value *SrcLocInfo = nullptr);
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp 
b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 2e5ce5308eea5..0001626086d7c 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -3495,9 +3495,9 @@ 
checkReductionInfos(ArrayRef ReductionInfos,
 OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
 const LocationDescription &Loc, InsertPointTy AllocaIP,
 InsertPointTy CodeGenIP, ArrayRef ReductionInfos,
-bool IsNoWait, bool IsTeamsReduction, bool HasDistribute,
-ReductionGenCBKind ReductionGenCBKind, std::optional GridValue,
-unsigned ReductionBufNum, Value *SrcLocInfo) {
+bool IsNoWait, bool IsTeamsReduction, ReductionGenCBKind 
ReductionGenCBKind,
+std::optional GridValue, unsigned ReductionBufNum,
+Value *SrcLocInfo) {
   if (!updateToLocation(Loc))
 return InsertPointTy();
   Builder.restoreIP(CodeGenIP);
@@ -3514,6 +3514,16 @@ OpenMPIRBuilder::InsertPointOrErrorTy 
OpenMPIRBuilder::createReductionsGPU(
   if (ReductionInfos.size() == 0)
 return Builder.saveIP();
 
+  BasicBlock *ContinuationBlock = nullptr;
+  if (ReductionGenCBKind != ReductionGenCBKind::Clang) {
+// Copied code from createReductions
+BasicBlock *InsertBlock = Loc.IP.getBlock();
+ContinuationBlock =
+InsertBlock->splitBasicBlock(Loc.IP.getPoint(), "reduce.finalize");
+InsertBlock->getTerminator()->eraseFromParent();
+Builder.SetInsertPoint(InsertBlock, InsertBlock->end());
+  }
+
   Function *CurFunc = Builder.GetInsertBlock()->getParent();
   AttributeList FuncAttrs;
   AttrBuilder AttrBldr(Ctx);
@@ -3669,11 +3679,21 @@ OpenMPIRBuilder::InsertPointOrErrorTy 
OpenMPIRBuilder::createReductionsGPU(
ReductionFunc;
   });
 } else {
-  assert(false && "Unhandled ReductionGenCBKind");
+  Value *LHSValue = Builder.CreateLoad(RI.ElementType, LHS, "final.lhs");
+  Value *RHSValue = Builder.CreateLoad(RI.ElementType, RHS, "final.rhs");
+  Value *Reduced;
+  InsertPointOrErrorTy AfterIP =
+  RI.ReductionGen(Builder.saveIP(), RHSValue, LHSValue, Reduced);
+  if (

[clang] [llvm] [mlir] [MLIR][OpenMP] Add codegen for teams reductions (PR #133310)

2025-03-28 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin updated 
https://github.com/llvm/llvm-project/pull/133310

>From 50fefbb31f4de7352c241c48fe5382785daaef21 Mon Sep 17 00:00:00 2001
From: Jan Leyonberg 
Date: Sun, 23 Mar 2025 09:56:51 -0400
Subject: [PATCH 01/10] Initial modifications to support reductions in flang.

---
 clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp  |  3 +-
 .../llvm/Frontend/OpenMP/OMPIRBuilder.h   |  3 --
 llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 30 +++
 3 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
index feb2448297542..d30bef9e7f0ba 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
@@ -1659,7 +1659,6 @@ void CGOpenMPRuntimeGPU::emitReduction(
 return;
 
   bool ParallelReduction = isOpenMPParallelDirective(Options.ReductionKind);
-  bool DistributeReduction = 
isOpenMPDistributeDirective(Options.ReductionKind);
   bool TeamsReduction = isOpenMPTeamsDirective(Options.ReductionKind);
 
   ASTContext &C = CGM.getContext();
@@ -1756,7 +1755,7 @@ void CGOpenMPRuntimeGPU::emitReduction(
   llvm::OpenMPIRBuilder::InsertPointTy AfterIP =
   cantFail(OMPBuilder.createReductionsGPU(
   OmpLoc, AllocaIP, CodeGenIP, ReductionInfos, false, TeamsReduction,
-  DistributeReduction, 
llvm::OpenMPIRBuilder::ReductionGenCBKind::Clang,
+  llvm::OpenMPIRBuilder::ReductionGenCBKind::Clang,
   CGF.getTarget().getGridValue(),
   C.getLangOpts().OpenMPCUDAReductionBufNum, RTLoc));
   CGF.Builder.restoreIP(AfterIP);
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h 
b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
index 28909cef4748d..9b67d0c050e46 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -1905,8 +1905,6 @@ class OpenMPIRBuilder {
   ///   nowait.
   /// \param IsTeamsReduction   Optional flag set if it is a teams
   ///   reduction.
-  /// \param HasDistribute  Optional flag set if it is a
-  ///   distribute reduction.
   /// \param GridValue  Optional GPU grid value.
   /// \param ReductionBufNumOptional OpenMPCUDAReductionBufNumValue to be
   /// used for teams reduction.
@@ -1915,7 +1913,6 @@ class OpenMPIRBuilder {
   const LocationDescription &Loc, InsertPointTy AllocaIP,
   InsertPointTy CodeGenIP, ArrayRef ReductionInfos,
   bool IsNoWait = false, bool IsTeamsReduction = false,
-  bool HasDistribute = false,
   ReductionGenCBKind ReductionGenCBKind = ReductionGenCBKind::MLIR,
   std::optional GridValue = {}, unsigned ReductionBufNum = 1024,
   Value *SrcLocInfo = nullptr);
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp 
b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 2e5ce5308eea5..0001626086d7c 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -3495,9 +3495,9 @@ 
checkReductionInfos(ArrayRef ReductionInfos,
 OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
 const LocationDescription &Loc, InsertPointTy AllocaIP,
 InsertPointTy CodeGenIP, ArrayRef ReductionInfos,
-bool IsNoWait, bool IsTeamsReduction, bool HasDistribute,
-ReductionGenCBKind ReductionGenCBKind, std::optional GridValue,
-unsigned ReductionBufNum, Value *SrcLocInfo) {
+bool IsNoWait, bool IsTeamsReduction, ReductionGenCBKind 
ReductionGenCBKind,
+std::optional GridValue, unsigned ReductionBufNum,
+Value *SrcLocInfo) {
   if (!updateToLocation(Loc))
 return InsertPointTy();
   Builder.restoreIP(CodeGenIP);
@@ -3514,6 +3514,16 @@ OpenMPIRBuilder::InsertPointOrErrorTy 
OpenMPIRBuilder::createReductionsGPU(
   if (ReductionInfos.size() == 0)
 return Builder.saveIP();
 
+  BasicBlock *ContinuationBlock = nullptr;
+  if (ReductionGenCBKind != ReductionGenCBKind::Clang) {
+// Copied code from createReductions
+BasicBlock *InsertBlock = Loc.IP.getBlock();
+ContinuationBlock =
+InsertBlock->splitBasicBlock(Loc.IP.getPoint(), "reduce.finalize");
+InsertBlock->getTerminator()->eraseFromParent();
+Builder.SetInsertPoint(InsertBlock, InsertBlock->end());
+  }
+
   Function *CurFunc = Builder.GetInsertBlock()->getParent();
   AttributeList FuncAttrs;
   AttrBuilder AttrBldr(Ctx);
@@ -3669,11 +3679,21 @@ OpenMPIRBuilder::InsertPointOrErrorTy 
OpenMPIRBuilder::createReductionsGPU(
ReductionFunc;
   });
 } else {
-  assert(false && "Unhandled ReductionGenCBKind");
+  Value *LHSValue = Builder.CreateLoad(RI.ElementType, LHS, "final.lhs");
+  Value *RHSValue = Builder.CreateLoad(RI.ElementType, RHS, "final.rhs");
+  Value *Reduced;
+  InsertPointOrErrorTy AfterIP =
+  RI.ReductionGen(Builder.saveIP(), RHSValue, LHSValue, Reduced);
+  if

[clang] [llvm] [mlir] [MLIR][OpenMP] Add codegen for teams reductions (PR #133310)

2025-04-05 Thread Jan Leyonberg via cfe-commits


@@ -4161,6 +4247,37 @@ convertOmpDistribute(Operation &opInst, 
llvm::IRBuilderBase &builder,
   if (failed(checkImplementationStatus(opInst)))
 return failure();
 
+  /// Process teams op reduction in distribute if the reduction is contained in
+  /// the distribute op.
+  omp::TeamsOp teamsOp = opInst.getParentOfType();
+  bool doDistributeReduction =
+  teamsOp ? teamsReductionContainedInDistribute(teamsOp) : false;
+
+  DenseMap reductionVariableMap;

jsjodin wrote:

I can make a separate cleanup PR for this to make this one smaller.

https://github.com/llvm/llvm-project/pull/133310
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [MLIR][OpenMP] Add codegen for teams reductions (PR #133310)

2025-04-05 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin updated 
https://github.com/llvm/llvm-project/pull/133310

>From 8193adb194d456c8d8f719869d920b6214f0505e Mon Sep 17 00:00:00 2001
From: Jan Leyonberg 
Date: Sun, 23 Mar 2025 09:56:51 -0400
Subject: [PATCH 1/9] Initial modifications to support reductions in flang.

---
 clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp  |  3 +-
 .../llvm/Frontend/OpenMP/OMPIRBuilder.h   |  3 --
 llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 30 +++
 3 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
index feb2448297542..d30bef9e7f0ba 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
@@ -1659,7 +1659,6 @@ void CGOpenMPRuntimeGPU::emitReduction(
 return;
 
   bool ParallelReduction = isOpenMPParallelDirective(Options.ReductionKind);
-  bool DistributeReduction = 
isOpenMPDistributeDirective(Options.ReductionKind);
   bool TeamsReduction = isOpenMPTeamsDirective(Options.ReductionKind);
 
   ASTContext &C = CGM.getContext();
@@ -1756,7 +1755,7 @@ void CGOpenMPRuntimeGPU::emitReduction(
   llvm::OpenMPIRBuilder::InsertPointTy AfterIP =
   cantFail(OMPBuilder.createReductionsGPU(
   OmpLoc, AllocaIP, CodeGenIP, ReductionInfos, false, TeamsReduction,
-  DistributeReduction, 
llvm::OpenMPIRBuilder::ReductionGenCBKind::Clang,
+  llvm::OpenMPIRBuilder::ReductionGenCBKind::Clang,
   CGF.getTarget().getGridValue(),
   C.getLangOpts().OpenMPCUDAReductionBufNum, RTLoc));
   CGF.Builder.restoreIP(AfterIP);
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h 
b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
index 28909cef4748d..9b67d0c050e46 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -1905,8 +1905,6 @@ class OpenMPIRBuilder {
   ///   nowait.
   /// \param IsTeamsReduction   Optional flag set if it is a teams
   ///   reduction.
-  /// \param HasDistribute  Optional flag set if it is a
-  ///   distribute reduction.
   /// \param GridValue  Optional GPU grid value.
   /// \param ReductionBufNumOptional OpenMPCUDAReductionBufNumValue to be
   /// used for teams reduction.
@@ -1915,7 +1913,6 @@ class OpenMPIRBuilder {
   const LocationDescription &Loc, InsertPointTy AllocaIP,
   InsertPointTy CodeGenIP, ArrayRef ReductionInfos,
   bool IsNoWait = false, bool IsTeamsReduction = false,
-  bool HasDistribute = false,
   ReductionGenCBKind ReductionGenCBKind = ReductionGenCBKind::MLIR,
   std::optional GridValue = {}, unsigned ReductionBufNum = 1024,
   Value *SrcLocInfo = nullptr);
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp 
b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index cb2376f59e32e..98ccb3306242b 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -3495,9 +3495,9 @@ 
checkReductionInfos(ArrayRef ReductionInfos,
 OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
 const LocationDescription &Loc, InsertPointTy AllocaIP,
 InsertPointTy CodeGenIP, ArrayRef ReductionInfos,
-bool IsNoWait, bool IsTeamsReduction, bool HasDistribute,
-ReductionGenCBKind ReductionGenCBKind, std::optional GridValue,
-unsigned ReductionBufNum, Value *SrcLocInfo) {
+bool IsNoWait, bool IsTeamsReduction, ReductionGenCBKind 
ReductionGenCBKind,
+std::optional GridValue, unsigned ReductionBufNum,
+Value *SrcLocInfo) {
   if (!updateToLocation(Loc))
 return InsertPointTy();
   Builder.restoreIP(CodeGenIP);
@@ -3514,6 +3514,16 @@ OpenMPIRBuilder::InsertPointOrErrorTy 
OpenMPIRBuilder::createReductionsGPU(
   if (ReductionInfos.size() == 0)
 return Builder.saveIP();
 
+  BasicBlock *ContinuationBlock = nullptr;
+  if (ReductionGenCBKind != ReductionGenCBKind::Clang) {
+// Copied code from createReductions
+BasicBlock *InsertBlock = Loc.IP.getBlock();
+ContinuationBlock =
+InsertBlock->splitBasicBlock(Loc.IP.getPoint(), "reduce.finalize");
+InsertBlock->getTerminator()->eraseFromParent();
+Builder.SetInsertPoint(InsertBlock, InsertBlock->end());
+  }
+
   Function *CurFunc = Builder.GetInsertBlock()->getParent();
   AttributeList FuncAttrs;
   AttrBuilder AttrBldr(Ctx);
@@ -3669,11 +3679,21 @@ OpenMPIRBuilder::InsertPointOrErrorTy 
OpenMPIRBuilder::createReductionsGPU(
ReductionFunc;
   });
 } else {
-  assert(false && "Unhandled ReductionGenCBKind");
+  Value *LHSValue = Builder.CreateLoad(RI.ElementType, LHS, "final.lhs");
+  Value *RHSValue = Builder.CreateLoad(RI.ElementType, RHS, "final.rhs");
+  Value *Reduced;
+  InsertPointOrErrorTy AfterIP =
+  RI.ReductionGen(Builder.saveIP(), RHSValue, LHSValue, Reduced);
+  if (

[clang] [llvm] [mlir] [MLIR][OpenMP] Add codegen for teams reductions (PR #133310)

2025-04-07 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin updated 
https://github.com/llvm/llvm-project/pull/133310

>From 78f53d76e6b5fd778cd2ec4fc1aa80b0c42ce582 Mon Sep 17 00:00:00 2001
From: Jan Leyonberg 
Date: Sun, 23 Mar 2025 09:56:51 -0400
Subject: [PATCH 1/9] Initial modifications to support reductions in flang.

---
 clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp  |  3 +-
 .../llvm/Frontend/OpenMP/OMPIRBuilder.h   |  3 --
 llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 30 +++
 3 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
index e86fbffe63252..f697c13f4c522 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
@@ -1660,7 +1660,6 @@ void CGOpenMPRuntimeGPU::emitReduction(
 return;
 
   bool ParallelReduction = isOpenMPParallelDirective(Options.ReductionKind);
-  bool DistributeReduction = 
isOpenMPDistributeDirective(Options.ReductionKind);
   bool TeamsReduction = isOpenMPTeamsDirective(Options.ReductionKind);
 
   ASTContext &C = CGM.getContext();
@@ -1757,7 +1756,7 @@ void CGOpenMPRuntimeGPU::emitReduction(
   llvm::OpenMPIRBuilder::InsertPointTy AfterIP =
   cantFail(OMPBuilder.createReductionsGPU(
   OmpLoc, AllocaIP, CodeGenIP, ReductionInfos, false, TeamsReduction,
-  DistributeReduction, 
llvm::OpenMPIRBuilder::ReductionGenCBKind::Clang,
+  llvm::OpenMPIRBuilder::ReductionGenCBKind::Clang,
   CGF.getTarget().getGridValue(),
   C.getLangOpts().OpenMPCUDAReductionBufNum, RTLoc));
   CGF.Builder.restoreIP(AfterIP);
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h 
b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
index ec013d1822439..a024612345082 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -1907,8 +1907,6 @@ class OpenMPIRBuilder {
   ///   nowait.
   /// \param IsTeamsReduction   Optional flag set if it is a teams
   ///   reduction.
-  /// \param HasDistribute  Optional flag set if it is a
-  ///   distribute reduction.
   /// \param GridValue  Optional GPU grid value.
   /// \param ReductionBufNumOptional OpenMPCUDAReductionBufNumValue to be
   /// used for teams reduction.
@@ -1917,7 +1915,6 @@ class OpenMPIRBuilder {
   const LocationDescription &Loc, InsertPointTy AllocaIP,
   InsertPointTy CodeGenIP, ArrayRef ReductionInfos,
   bool IsNoWait = false, bool IsTeamsReduction = false,
-  bool HasDistribute = false,
   ReductionGenCBKind ReductionGenCBKind = ReductionGenCBKind::MLIR,
   std::optional GridValue = {}, unsigned ReductionBufNum = 1024,
   Value *SrcLocInfo = nullptr);
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp 
b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 68b1fa42934ad..98ce6da315a70 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -3495,9 +3495,9 @@ 
checkReductionInfos(ArrayRef ReductionInfos,
 OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
 const LocationDescription &Loc, InsertPointTy AllocaIP,
 InsertPointTy CodeGenIP, ArrayRef ReductionInfos,
-bool IsNoWait, bool IsTeamsReduction, bool HasDistribute,
-ReductionGenCBKind ReductionGenCBKind, std::optional GridValue,
-unsigned ReductionBufNum, Value *SrcLocInfo) {
+bool IsNoWait, bool IsTeamsReduction, ReductionGenCBKind 
ReductionGenCBKind,
+std::optional GridValue, unsigned ReductionBufNum,
+Value *SrcLocInfo) {
   if (!updateToLocation(Loc))
 return InsertPointTy();
   Builder.restoreIP(CodeGenIP);
@@ -3514,6 +3514,16 @@ OpenMPIRBuilder::InsertPointOrErrorTy 
OpenMPIRBuilder::createReductionsGPU(
   if (ReductionInfos.size() == 0)
 return Builder.saveIP();
 
+  BasicBlock *ContinuationBlock = nullptr;
+  if (ReductionGenCBKind != ReductionGenCBKind::Clang) {
+// Copied code from createReductions
+BasicBlock *InsertBlock = Loc.IP.getBlock();
+ContinuationBlock =
+InsertBlock->splitBasicBlock(Loc.IP.getPoint(), "reduce.finalize");
+InsertBlock->getTerminator()->eraseFromParent();
+Builder.SetInsertPoint(InsertBlock, InsertBlock->end());
+  }
+
   Function *CurFunc = Builder.GetInsertBlock()->getParent();
   AttributeList FuncAttrs;
   AttrBuilder AttrBldr(Ctx);
@@ -3669,11 +3679,21 @@ OpenMPIRBuilder::InsertPointOrErrorTy 
OpenMPIRBuilder::createReductionsGPU(
ReductionFunc;
   });
 } else {
-  assert(false && "Unhandled ReductionGenCBKind");
+  Value *LHSValue = Builder.CreateLoad(RI.ElementType, LHS, "final.lhs");
+  Value *RHSValue = Builder.CreateLoad(RI.ElementType, RHS, "final.rhs");
+  Value *Reduced;
+  InsertPointOrErrorTy AfterIP =
+  RI.ReductionGen(Builder.saveIP(), RHSValue, LHSValue, Reduced);
+  if (

[clang] [llvm] [mlir] [MLIR][OpenMP] Add codegen for teams reductions (PR #133310)

2025-04-07 Thread Jan Leyonberg via cfe-commits

https://github.com/jsjodin closed 
https://github.com/llvm/llvm-project/pull/133310
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits