Skip to content

[WIP][PoC][flang] Re-use OpenMP data environemnt clauses for locality spec #128148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[flang] Basic lowering of fir.do_loop locality specifiers
  • Loading branch information
ergawy committed Mar 12, 2025
commit 6261c45d415b37a480d0f0fcb004a434eb6cb493
8 changes: 1 addition & 7 deletions flang/include/flang/Optimizer/Dialect/FIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -2284,20 +2284,14 @@ def fir_DoLoopOp : region_Op<"do_loop", [AttrSizedOperandSegments,
.slice(getNumControlOperands() + getNumReduceOperands(),
getNumIterOperands());
}
mlir::Operation::operand_range getPrivateOperands() {
return getOperands()
.slice(getNumControlOperands() + getNumReduceOperands()
+ getNumIterOperands(),
numPrivateBlockArgs());
}

void setLowerBound(mlir::Value bound) { (*this)->setOperand(0, bound); }
void setUpperBound(mlir::Value bound) { (*this)->setOperand(1, bound); }
void setStep(mlir::Value step) { (*this)->setOperand(2, step); }

/// Number of region arguments for loop-carried values
unsigned getNumRegionIterArgs() {
return getBody()->getNumArguments() - 1;
return getNumIterOperands();
}
/// Number of operands controlling the loop: lb, ub, step
unsigned getNumControlOperands() { return 3; }
Expand Down
37 changes: 37 additions & 0 deletions flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ using namespace fir;
using namespace mlir;

namespace {
/// Looks up from the operation from and returns the PrivateClauseOp with
/// name symbolName
///
/// TODO Copied from OpenMPToLLVMIRTranslation.cpp, move to a shared location.
/// Maybe a static function on the `PrivateClauseOp`.
static omp::PrivateClauseOp findPrivatizer(Operation *from,
SymbolRefAttr symbolName) {
omp::PrivateClauseOp privatizer =
SymbolTable::lookupNearestSymbolFrom<omp::PrivateClauseOp>(from,
symbolName);
assert(privatizer && "privatizer not found in the symbol table");
return privatizer;
}

// Conversion of fir control ops to more primitive control-flow.
//
Expand All @@ -57,6 +70,30 @@ class CfgLoopConv : public mlir::OpRewritePattern<fir::DoLoopOp> {
auto iofAttr = mlir::arith::IntegerOverflowFlagsAttr::get(
rewriter.getContext(), flags);

// Handle privatization
if (!loop.getPrivateVars().empty()) {
mlir::OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPointToStart(&loop.getRegion().front());

std::optional<ArrayAttr> privateSyms = loop.getPrivateSyms();

for (auto [privateVar, privateArg, privatizerSym] :
llvm::zip_equal(loop.getPrivateVars(), loop.getRegionPrivateArgs(),
*privateSyms)) {
SymbolRefAttr privatizerName = llvm::cast<SymbolRefAttr>(privatizerSym);
omp::PrivateClauseOp privatizer = findPrivatizer(loop, privatizerName);

mlir::Value localAlloc =
rewriter.create<fir::AllocaOp>(loop.getLoc(), privatizer.getType());
rewriter.replaceAllUsesWith(privateArg, localAlloc);
}

loop.getRegion().front().eraseArguments(1 + loop.getNumRegionIterArgs(),
loop.numPrivateBlockArgs());
loop.getPrivateVarsMutable().clear();
loop.setPrivateSymsAttr(nullptr);
}

// Create the start and end blocks that will wrap the DoLoopOp with an
// initalizer and an end point
auto *initBlock = rewriter.getInsertionBlock();
Expand Down