1 //===- OpenMPToLLVMIRTranslation.cpp - Translate OpenMP dialect to LLVM IR-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements a translation between the MLIR OpenMP dialect and LLVM 10 // IR. 11 // 12 //===----------------------------------------------------------------------===// 13 #include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h" 14 #include "mlir/Dialect/OpenMP/OpenMPDialect.h" 15 #include "mlir/IR/BlockAndValueMapping.h" 16 #include "mlir/IR/Operation.h" 17 #include "mlir/Support/LLVM.h" 18 #include "mlir/Target/LLVMIR/ModuleTranslation.h" 19 20 #include "llvm/ADT/SetVector.h" 21 #include "llvm/ADT/TypeSwitch.h" 22 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" 23 #include "llvm/IR/IRBuilder.h" 24 25 using namespace mlir; 26 27 namespace { 28 /// ModuleTranslation stack frame for OpenMP operations. This keeps track of the 29 /// insertion points for allocas. 30 class OpenMPAllocaStackFrame 31 : public LLVM::ModuleTranslation::StackFrameBase<OpenMPAllocaStackFrame> { 32 public: 33 explicit OpenMPAllocaStackFrame(llvm::OpenMPIRBuilder::InsertPointTy allocaIP) 34 : allocaInsertPoint(allocaIP) {} 35 llvm::OpenMPIRBuilder::InsertPointTy allocaInsertPoint; 36 }; 37 38 /// ModuleTranslation stack frame containing the partial mapping between MLIR 39 /// values and their LLVM IR equivalents. 40 class OpenMPVarMappingStackFrame 41 : public LLVM::ModuleTranslation::StackFrameBase< 42 OpenMPVarMappingStackFrame> { 43 public: 44 explicit OpenMPVarMappingStackFrame( 45 const DenseMap<Value, llvm::Value *> &mapping) 46 : mapping(mapping) {} 47 48 DenseMap<Value, llvm::Value *> mapping; 49 }; 50 } // namespace 51 52 /// Find the insertion point for allocas given the current insertion point for 53 /// normal operations in the builder. 54 static llvm::OpenMPIRBuilder::InsertPointTy 55 findAllocaInsertPoint(llvm::IRBuilderBase &builder, 56 const LLVM::ModuleTranslation &moduleTranslation) { 57 // If there is an alloca insertion point on stack, i.e. we are in a nested 58 // operation and a specific point was provided by some surrounding operation, 59 // use it. 60 llvm::OpenMPIRBuilder::InsertPointTy allocaInsertPoint; 61 WalkResult walkResult = moduleTranslation.stackWalk<OpenMPAllocaStackFrame>( 62 [&](const OpenMPAllocaStackFrame &frame) { 63 allocaInsertPoint = frame.allocaInsertPoint; 64 return WalkResult::interrupt(); 65 }); 66 if (walkResult.wasInterrupted()) 67 return allocaInsertPoint; 68 69 // Otherwise, insert to the entry block of the surrounding function. 70 llvm::BasicBlock &funcEntryBlock = 71 builder.GetInsertBlock()->getParent()->getEntryBlock(); 72 return llvm::OpenMPIRBuilder::InsertPointTy( 73 &funcEntryBlock, funcEntryBlock.getFirstInsertionPt()); 74 } 75 76 /// Converts the given region that appears within an OpenMP dialect operation to 77 /// LLVM IR, creating a branch from the `sourceBlock` to the entry block of the 78 /// region, and a branch from any block with an successor-less OpenMP terminator 79 /// to `continuationBlock`. Populates `continuationBlockPHIs` with the PHI nodes 80 /// of the continuation block if provided. 81 static void convertOmpOpRegions( 82 Region ®ion, StringRef blockName, llvm::BasicBlock &sourceBlock, 83 llvm::BasicBlock &continuationBlock, llvm::IRBuilderBase &builder, 84 LLVM::ModuleTranslation &moduleTranslation, LogicalResult &bodyGenStatus, 85 SmallVectorImpl<llvm::PHINode *> *continuationBlockPHIs = nullptr) { 86 llvm::LLVMContext &llvmContext = builder.getContext(); 87 for (Block &bb : region) { 88 llvm::BasicBlock *llvmBB = llvm::BasicBlock::Create( 89 llvmContext, blockName, builder.GetInsertBlock()->getParent(), 90 builder.GetInsertBlock()->getNextNode()); 91 moduleTranslation.mapBlock(&bb, llvmBB); 92 } 93 94 llvm::Instruction *sourceTerminator = sourceBlock.getTerminator(); 95 96 // Terminators (namely YieldOp) may be forwarding values to the region that 97 // need to be available in the continuation block. Collect the types of these 98 // operands in preparation of creating PHI nodes. 99 SmallVector<llvm::Type *> continuationBlockPHITypes; 100 bool operandsProcessed = false; 101 unsigned numYields = 0; 102 for (Block &bb : region.getBlocks()) { 103 if (omp::YieldOp yield = dyn_cast<omp::YieldOp>(bb.getTerminator())) { 104 if (!operandsProcessed) { 105 for (unsigned i = 0, e = yield->getNumOperands(); i < e; ++i) { 106 continuationBlockPHITypes.push_back( 107 moduleTranslation.convertType(yield->getOperand(i).getType())); 108 } 109 operandsProcessed = true; 110 } else { 111 assert(continuationBlockPHITypes.size() == yield->getNumOperands() && 112 "mismatching number of values yielded from the region"); 113 for (unsigned i = 0, e = yield->getNumOperands(); i < e; ++i) { 114 llvm::Type *operandType = 115 moduleTranslation.convertType(yield->getOperand(i).getType()); 116 (void)operandType; 117 assert(continuationBlockPHITypes[i] == operandType && 118 "values of mismatching types yielded from the region"); 119 } 120 } 121 numYields++; 122 } 123 } 124 125 // Insert PHI nodes in the continuation block for any values forwarded by the 126 // terminators in this region. 127 if (!continuationBlockPHITypes.empty()) 128 assert( 129 continuationBlockPHIs && 130 "expected continuation block PHIs if converted regions yield values"); 131 if (continuationBlockPHIs) { 132 llvm::IRBuilderBase::InsertPointGuard guard(builder); 133 continuationBlockPHIs->reserve(continuationBlockPHITypes.size()); 134 builder.SetInsertPoint(&continuationBlock, continuationBlock.begin()); 135 for (llvm::Type *ty : continuationBlockPHITypes) 136 continuationBlockPHIs->push_back(builder.CreatePHI(ty, numYields)); 137 } 138 139 // Convert blocks one by one in topological order to ensure 140 // defs are converted before uses. 141 SetVector<Block *> blocks = 142 LLVM::detail::getTopologicallySortedBlocks(region); 143 for (Block *bb : blocks) { 144 llvm::BasicBlock *llvmBB = moduleTranslation.lookupBlock(bb); 145 // Retarget the branch of the entry block to the entry block of the 146 // converted region (regions are single-entry). 147 if (bb->isEntryBlock()) { 148 assert(sourceTerminator->getNumSuccessors() == 1 && 149 "provided entry block has multiple successors"); 150 assert(sourceTerminator->getSuccessor(0) == &continuationBlock && 151 "ContinuationBlock is not the successor of the entry block"); 152 sourceTerminator->setSuccessor(0, llvmBB); 153 } 154 155 llvm::IRBuilderBase::InsertPointGuard guard(builder); 156 if (failed( 157 moduleTranslation.convertBlock(*bb, bb->isEntryBlock(), builder))) { 158 bodyGenStatus = failure(); 159 return; 160 } 161 162 // Special handling for `omp.yield` and `omp.terminator` (we may have more 163 // than one): they return the control to the parent OpenMP dialect operation 164 // so replace them with the branch to the continuation block. We handle this 165 // here to avoid relying inter-function communication through the 166 // ModuleTranslation class to set up the correct insertion point. This is 167 // also consistent with MLIR's idiom of handling special region terminators 168 // in the same code that handles the region-owning operation. 169 Operation *terminator = bb->getTerminator(); 170 if (isa<omp::TerminatorOp, omp::YieldOp>(terminator)) { 171 builder.CreateBr(&continuationBlock); 172 173 for (unsigned i = 0, e = terminator->getNumOperands(); i < e; ++i) 174 (*continuationBlockPHIs)[i]->addIncoming( 175 moduleTranslation.lookupValue(terminator->getOperand(i)), llvmBB); 176 } 177 } 178 // After all blocks have been traversed and values mapped, connect the PHI 179 // nodes to the results of preceding blocks. 180 LLVM::detail::connectPHINodes(region, moduleTranslation); 181 182 // Remove the blocks and values defined in this region from the mapping since 183 // they are not visible outside of this region. This allows the same region to 184 // be converted several times, that is cloned, without clashes, and slightly 185 // speeds up the lookups. 186 moduleTranslation.forgetMapping(region); 187 } 188 189 /// Convert ProcBindKind from MLIR-generated enum to LLVM enum. 190 static llvm::omp::ProcBindKind getProcBindKind(omp::ClauseProcBindKind kind) { 191 switch (kind) { 192 case omp::ClauseProcBindKind::close: 193 return llvm::omp::ProcBindKind::OMP_PROC_BIND_close; 194 case omp::ClauseProcBindKind::master: 195 return llvm::omp::ProcBindKind::OMP_PROC_BIND_master; 196 case omp::ClauseProcBindKind::primary: 197 return llvm::omp::ProcBindKind::OMP_PROC_BIND_primary; 198 case omp::ClauseProcBindKind::spread: 199 return llvm::omp::ProcBindKind::OMP_PROC_BIND_spread; 200 } 201 } 202 203 /// Converts the OpenMP parallel operation to LLVM IR. 204 static LogicalResult 205 convertOmpParallel(omp::ParallelOp opInst, llvm::IRBuilderBase &builder, 206 LLVM::ModuleTranslation &moduleTranslation) { 207 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; 208 // TODO: support error propagation in OpenMPIRBuilder and use it instead of 209 // relying on captured variables. 210 LogicalResult bodyGenStatus = success(); 211 212 auto bodyGenCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP, 213 llvm::BasicBlock &continuationBlock) { 214 // Save the alloca insertion point on ModuleTranslation stack for use in 215 // nested regions. 216 LLVM::ModuleTranslation::SaveStack<OpenMPAllocaStackFrame> frame( 217 moduleTranslation, allocaIP); 218 219 // ParallelOp has only one region associated with it. 220 convertOmpOpRegions(opInst.getRegion(), "omp.par.region", 221 *codeGenIP.getBlock(), continuationBlock, builder, 222 moduleTranslation, bodyGenStatus); 223 }; 224 225 // TODO: Perform appropriate actions according to the data-sharing 226 // attribute (shared, private, firstprivate, ...) of variables. 227 // Currently defaults to shared. 228 auto privCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP, 229 llvm::Value &, llvm::Value &vPtr, 230 llvm::Value *&replacementValue) -> InsertPointTy { 231 replacementValue = &vPtr; 232 233 return codeGenIP; 234 }; 235 236 // TODO: Perform finalization actions for variables. This has to be 237 // called for variables which have destructors/finalizers. 238 auto finiCB = [&](InsertPointTy codeGenIP) {}; 239 240 llvm::Value *ifCond = nullptr; 241 if (auto ifExprVar = opInst.if_expr_var()) 242 ifCond = moduleTranslation.lookupValue(ifExprVar); 243 llvm::Value *numThreads = nullptr; 244 if (auto numThreadsVar = opInst.num_threads_var()) 245 numThreads = moduleTranslation.lookupValue(numThreadsVar); 246 auto pbKind = llvm::omp::OMP_PROC_BIND_default; 247 if (auto bind = opInst.proc_bind_val()) 248 pbKind = getProcBindKind(*bind); 249 // TODO: Is the Parallel construct cancellable? 250 bool isCancellable = false; 251 252 llvm::OpenMPIRBuilder::LocationDescription ompLoc( 253 builder.saveIP(), builder.getCurrentDebugLocation()); 254 builder.restoreIP(moduleTranslation.getOpenMPBuilder()->createParallel( 255 ompLoc, findAllocaInsertPoint(builder, moduleTranslation), bodyGenCB, 256 privCB, finiCB, ifCond, numThreads, pbKind, isCancellable)); 257 258 return bodyGenStatus; 259 } 260 261 /// Converts an OpenMP 'master' operation into LLVM IR using OpenMPIRBuilder. 262 static LogicalResult 263 convertOmpMaster(Operation &opInst, llvm::IRBuilderBase &builder, 264 LLVM::ModuleTranslation &moduleTranslation) { 265 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; 266 // TODO: support error propagation in OpenMPIRBuilder and use it instead of 267 // relying on captured variables. 268 LogicalResult bodyGenStatus = success(); 269 270 auto bodyGenCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP, 271 llvm::BasicBlock &continuationBlock) { 272 // MasterOp has only one region associated with it. 273 auto ®ion = cast<omp::MasterOp>(opInst).getRegion(); 274 convertOmpOpRegions(region, "omp.master.region", *codeGenIP.getBlock(), 275 continuationBlock, builder, moduleTranslation, 276 bodyGenStatus); 277 }; 278 279 // TODO: Perform finalization actions for variables. This has to be 280 // called for variables which have destructors/finalizers. 281 auto finiCB = [&](InsertPointTy codeGenIP) {}; 282 283 llvm::OpenMPIRBuilder::LocationDescription ompLoc( 284 builder.saveIP(), builder.getCurrentDebugLocation()); 285 builder.restoreIP(moduleTranslation.getOpenMPBuilder()->createMaster( 286 ompLoc, bodyGenCB, finiCB)); 287 return success(); 288 } 289 290 /// Converts an OpenMP 'critical' operation into LLVM IR using OpenMPIRBuilder. 291 static LogicalResult 292 convertOmpCritical(Operation &opInst, llvm::IRBuilderBase &builder, 293 LLVM::ModuleTranslation &moduleTranslation) { 294 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; 295 auto criticalOp = cast<omp::CriticalOp>(opInst); 296 // TODO: support error propagation in OpenMPIRBuilder and use it instead of 297 // relying on captured variables. 298 LogicalResult bodyGenStatus = success(); 299 300 auto bodyGenCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP, 301 llvm::BasicBlock &continuationBlock) { 302 // CriticalOp has only one region associated with it. 303 auto ®ion = cast<omp::CriticalOp>(opInst).getRegion(); 304 convertOmpOpRegions(region, "omp.critical.region", *codeGenIP.getBlock(), 305 continuationBlock, builder, moduleTranslation, 306 bodyGenStatus); 307 }; 308 309 // TODO: Perform finalization actions for variables. This has to be 310 // called for variables which have destructors/finalizers. 311 auto finiCB = [&](InsertPointTy codeGenIP) {}; 312 313 llvm::OpenMPIRBuilder::LocationDescription ompLoc( 314 builder.saveIP(), builder.getCurrentDebugLocation()); 315 llvm::LLVMContext &llvmContext = moduleTranslation.getLLVMContext(); 316 llvm::Constant *hint = nullptr; 317 318 // If it has a name, it probably has a hint too. 319 if (criticalOp.nameAttr()) { 320 // The verifiers in OpenMP Dialect guarentee that all the pointers are 321 // non-null 322 auto symbolRef = criticalOp.nameAttr().cast<SymbolRefAttr>(); 323 auto criticalDeclareOp = 324 SymbolTable::lookupNearestSymbolFrom<omp::CriticalDeclareOp>(criticalOp, 325 symbolRef); 326 hint = llvm::ConstantInt::get(llvm::Type::getInt32Ty(llvmContext), 327 static_cast<int>(criticalDeclareOp.hint())); 328 } 329 builder.restoreIP(moduleTranslation.getOpenMPBuilder()->createCritical( 330 ompLoc, bodyGenCB, finiCB, criticalOp.name().getValueOr(""), hint)); 331 return success(); 332 } 333 334 /// Returns a reduction declaration that corresponds to the given reduction 335 /// operation in the given container. Currently only supports reductions inside 336 /// WsLoopOp but can be easily extended. 337 static omp::ReductionDeclareOp findReductionDecl(omp::WsLoopOp container, 338 omp::ReductionOp reduction) { 339 SymbolRefAttr reductionSymbol; 340 for (unsigned i = 0, e = container.getNumReductionVars(); i < e; ++i) { 341 if (container.reduction_vars()[i] != reduction.accumulator()) 342 continue; 343 reductionSymbol = (*container.reductions())[i].cast<SymbolRefAttr>(); 344 break; 345 } 346 assert(reductionSymbol && 347 "reduction operation must be associated with a declaration"); 348 349 return SymbolTable::lookupNearestSymbolFrom<omp::ReductionDeclareOp>( 350 container, reductionSymbol); 351 } 352 353 /// Populates `reductions` with reduction declarations used in the given loop. 354 static void 355 collectReductionDecls(omp::WsLoopOp loop, 356 SmallVectorImpl<omp::ReductionDeclareOp> &reductions) { 357 Optional<ArrayAttr> attr = loop.reductions(); 358 if (!attr) 359 return; 360 361 reductions.reserve(reductions.size() + loop.getNumReductionVars()); 362 for (auto symbolRef : attr->getAsRange<SymbolRefAttr>()) { 363 reductions.push_back( 364 SymbolTable::lookupNearestSymbolFrom<omp::ReductionDeclareOp>( 365 loop, symbolRef)); 366 } 367 } 368 369 /// Translates the blocks contained in the given region and appends them to at 370 /// the current insertion point of `builder`. The operations of the entry block 371 /// are appended to the current insertion block, which is not expected to have a 372 /// terminator. If set, `continuationBlockArgs` is populated with translated 373 /// values that correspond to the values omp.yield'ed from the region. 374 static LogicalResult inlineConvertOmpRegions( 375 Region ®ion, StringRef blockName, llvm::IRBuilderBase &builder, 376 LLVM::ModuleTranslation &moduleTranslation, 377 SmallVectorImpl<llvm::Value *> *continuationBlockArgs = nullptr) { 378 if (region.empty()) 379 return success(); 380 381 // Special case for single-block regions that don't create additional blocks: 382 // insert operations without creating additional blocks. 383 if (llvm::hasSingleElement(region)) { 384 moduleTranslation.mapBlock(®ion.front(), builder.GetInsertBlock()); 385 if (failed(moduleTranslation.convertBlock( 386 region.front(), /*ignoreArguments=*/true, builder))) 387 return failure(); 388 389 // The continuation arguments are simply the translated terminator operands. 390 if (continuationBlockArgs) 391 llvm::append_range( 392 *continuationBlockArgs, 393 moduleTranslation.lookupValues(region.front().back().getOperands())); 394 395 // Drop the mapping that is no longer necessary so that the same region can 396 // be processed multiple times. 397 moduleTranslation.forgetMapping(region); 398 return success(); 399 } 400 401 // Create the continuation block manually instead of calling splitBlock 402 // because the current insertion block may not have a terminator. 403 llvm::BasicBlock *continuationBlock = 404 llvm::BasicBlock::Create(builder.getContext(), blockName + ".cont", 405 builder.GetInsertBlock()->getParent(), 406 builder.GetInsertBlock()->getNextNode()); 407 builder.CreateBr(continuationBlock); 408 409 LogicalResult bodyGenStatus = success(); 410 SmallVector<llvm::PHINode *> phis; 411 convertOmpOpRegions(region, blockName, *builder.GetInsertBlock(), 412 *continuationBlock, builder, moduleTranslation, 413 bodyGenStatus, &phis); 414 if (failed(bodyGenStatus)) 415 return failure(); 416 if (continuationBlockArgs) 417 llvm::append_range(*continuationBlockArgs, phis); 418 builder.SetInsertPoint(continuationBlock, 419 continuationBlock->getFirstInsertionPt()); 420 return success(); 421 } 422 423 namespace { 424 /// Owning equivalents of OpenMPIRBuilder::(Atomic)ReductionGen that are used to 425 /// store lambdas with capture. 426 using OwningReductionGen = std::function<llvm::OpenMPIRBuilder::InsertPointTy( 427 llvm::OpenMPIRBuilder::InsertPointTy, llvm::Value *, llvm::Value *, 428 llvm::Value *&)>; 429 using OwningAtomicReductionGen = 430 std::function<llvm::OpenMPIRBuilder::InsertPointTy( 431 llvm::OpenMPIRBuilder::InsertPointTy, llvm::Type *, llvm::Value *, 432 llvm::Value *)>; 433 } // namespace 434 435 /// Create an OpenMPIRBuilder-compatible reduction generator for the given 436 /// reduction declaration. The generator uses `builder` but ignores its 437 /// insertion point. 438 static OwningReductionGen 439 makeReductionGen(omp::ReductionDeclareOp decl, llvm::IRBuilderBase &builder, 440 LLVM::ModuleTranslation &moduleTranslation) { 441 // The lambda is mutable because we need access to non-const methods of decl 442 // (which aren't actually mutating it), and we must capture decl by-value to 443 // avoid the dangling reference after the parent function returns. 444 OwningReductionGen gen = 445 [&, decl](llvm::OpenMPIRBuilder::InsertPointTy insertPoint, 446 llvm::Value *lhs, llvm::Value *rhs, 447 llvm::Value *&result) mutable { 448 Region &reductionRegion = decl.reductionRegion(); 449 moduleTranslation.mapValue(reductionRegion.front().getArgument(0), lhs); 450 moduleTranslation.mapValue(reductionRegion.front().getArgument(1), rhs); 451 builder.restoreIP(insertPoint); 452 SmallVector<llvm::Value *> phis; 453 if (failed(inlineConvertOmpRegions(reductionRegion, 454 "omp.reduction.nonatomic.body", 455 builder, moduleTranslation, &phis))) 456 return llvm::OpenMPIRBuilder::InsertPointTy(); 457 assert(phis.size() == 1); 458 result = phis[0]; 459 return builder.saveIP(); 460 }; 461 return gen; 462 } 463 464 /// Create an OpenMPIRBuilder-compatible atomic reduction generator for the 465 /// given reduction declaration. The generator uses `builder` but ignores its 466 /// insertion point. Returns null if there is no atomic region available in the 467 /// reduction declaration. 468 static OwningAtomicReductionGen 469 makeAtomicReductionGen(omp::ReductionDeclareOp decl, 470 llvm::IRBuilderBase &builder, 471 LLVM::ModuleTranslation &moduleTranslation) { 472 if (decl.atomicReductionRegion().empty()) 473 return OwningAtomicReductionGen(); 474 475 // The lambda is mutable because we need access to non-const methods of decl 476 // (which aren't actually mutating it), and we must capture decl by-value to 477 // avoid the dangling reference after the parent function returns. 478 OwningAtomicReductionGen atomicGen = 479 [&, decl](llvm::OpenMPIRBuilder::InsertPointTy insertPoint, llvm::Type *, 480 llvm::Value *lhs, llvm::Value *rhs) mutable { 481 Region &atomicRegion = decl.atomicReductionRegion(); 482 moduleTranslation.mapValue(atomicRegion.front().getArgument(0), lhs); 483 moduleTranslation.mapValue(atomicRegion.front().getArgument(1), rhs); 484 builder.restoreIP(insertPoint); 485 SmallVector<llvm::Value *> phis; 486 if (failed(inlineConvertOmpRegions(atomicRegion, 487 "omp.reduction.atomic.body", builder, 488 moduleTranslation, &phis))) 489 return llvm::OpenMPIRBuilder::InsertPointTy(); 490 assert(phis.empty()); 491 return builder.saveIP(); 492 }; 493 return atomicGen; 494 } 495 496 /// Converts an OpenMP 'ordered' operation into LLVM IR using OpenMPIRBuilder. 497 static LogicalResult 498 convertOmpOrdered(Operation &opInst, llvm::IRBuilderBase &builder, 499 LLVM::ModuleTranslation &moduleTranslation) { 500 auto orderedOp = cast<omp::OrderedOp>(opInst); 501 502 omp::ClauseDepend dependType = *orderedOp.depend_type_val(); 503 bool isDependSource = dependType == omp::ClauseDepend::dependsource; 504 unsigned numLoops = orderedOp.num_loops_val().getValue(); 505 SmallVector<llvm::Value *> vecValues = 506 moduleTranslation.lookupValues(orderedOp.depend_vec_vars()); 507 508 llvm::OpenMPIRBuilder::LocationDescription ompLoc( 509 builder.saveIP(), builder.getCurrentDebugLocation()); 510 size_t indexVecValues = 0; 511 while (indexVecValues < vecValues.size()) { 512 SmallVector<llvm::Value *> storeValues; 513 storeValues.reserve(numLoops); 514 for (unsigned i = 0; i < numLoops; i++) { 515 storeValues.push_back(vecValues[indexVecValues]); 516 indexVecValues++; 517 } 518 builder.restoreIP(moduleTranslation.getOpenMPBuilder()->createOrderedDepend( 519 ompLoc, findAllocaInsertPoint(builder, moduleTranslation), numLoops, 520 storeValues, ".cnt.addr", isDependSource)); 521 } 522 return success(); 523 } 524 525 /// Converts an OpenMP 'ordered_region' operation into LLVM IR using 526 /// OpenMPIRBuilder. 527 static LogicalResult 528 convertOmpOrderedRegion(Operation &opInst, llvm::IRBuilderBase &builder, 529 LLVM::ModuleTranslation &moduleTranslation) { 530 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; 531 auto orderedRegionOp = cast<omp::OrderedRegionOp>(opInst); 532 533 // TODO: The code generation for ordered simd directive is not supported yet. 534 if (orderedRegionOp.simd()) 535 return failure(); 536 537 // TODO: support error propagation in OpenMPIRBuilder and use it instead of 538 // relying on captured variables. 539 LogicalResult bodyGenStatus = success(); 540 541 auto bodyGenCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP, 542 llvm::BasicBlock &continuationBlock) { 543 // OrderedOp has only one region associated with it. 544 auto ®ion = cast<omp::OrderedRegionOp>(opInst).getRegion(); 545 convertOmpOpRegions(region, "omp.ordered.region", *codeGenIP.getBlock(), 546 continuationBlock, builder, moduleTranslation, 547 bodyGenStatus); 548 }; 549 550 // TODO: Perform finalization actions for variables. This has to be 551 // called for variables which have destructors/finalizers. 552 auto finiCB = [&](InsertPointTy codeGenIP) {}; 553 554 llvm::OpenMPIRBuilder::LocationDescription ompLoc( 555 builder.saveIP(), builder.getCurrentDebugLocation()); 556 builder.restoreIP( 557 moduleTranslation.getOpenMPBuilder()->createOrderedThreadsSimd( 558 ompLoc, bodyGenCB, finiCB, !orderedRegionOp.simd())); 559 return bodyGenStatus; 560 } 561 562 static LogicalResult 563 convertOmpSections(Operation &opInst, llvm::IRBuilderBase &builder, 564 LLVM::ModuleTranslation &moduleTranslation) { 565 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; 566 using StorableBodyGenCallbackTy = 567 llvm::OpenMPIRBuilder::StorableBodyGenCallbackTy; 568 569 auto sectionsOp = cast<omp::SectionsOp>(opInst); 570 571 // TODO: Support the following clauses: private, firstprivate, lastprivate, 572 // reduction, allocate 573 if (!sectionsOp.private_vars().empty() || 574 !sectionsOp.firstprivate_vars().empty() || 575 !sectionsOp.lastprivate_vars().empty() || 576 !sectionsOp.reduction_vars().empty() || sectionsOp.reductions() || 577 !sectionsOp.allocate_vars().empty() || 578 !sectionsOp.allocators_vars().empty()) 579 return emitError(sectionsOp.getLoc()) 580 << "private, firstprivate, lastprivate, reduction and allocate " 581 "clauses are not supported for sections construct"; 582 583 LogicalResult bodyGenStatus = success(); 584 SmallVector<StorableBodyGenCallbackTy> sectionCBs; 585 586 for (Operation &op : *sectionsOp.region().begin()) { 587 auto sectionOp = dyn_cast<omp::SectionOp>(op); 588 if (!sectionOp) // omp.terminator 589 continue; 590 591 Region ®ion = sectionOp.region(); 592 auto sectionCB = [®ion, &builder, &moduleTranslation, &bodyGenStatus]( 593 InsertPointTy allocaIP, InsertPointTy codeGenIP, 594 llvm::BasicBlock &finiBB) { 595 builder.restoreIP(codeGenIP); 596 builder.CreateBr(&finiBB); 597 convertOmpOpRegions(region, "omp.section.region", *codeGenIP.getBlock(), 598 finiBB, builder, moduleTranslation, bodyGenStatus); 599 }; 600 sectionCBs.push_back(sectionCB); 601 } 602 603 // No sections within omp.sections operation - skip generation. This situation 604 // is only possible if there is only a terminator operation inside the 605 // sections operation 606 if (sectionCBs.empty()) 607 return success(); 608 609 assert(isa<omp::SectionOp>(*sectionsOp.region().op_begin())); 610 611 // TODO: Perform appropriate actions according to the data-sharing 612 // attribute (shared, private, firstprivate, ...) of variables. 613 // Currently defaults to shared. 614 auto privCB = [&](InsertPointTy, InsertPointTy codeGenIP, llvm::Value &, 615 llvm::Value &vPtr, 616 llvm::Value *&replacementValue) -> InsertPointTy { 617 replacementValue = &vPtr; 618 return codeGenIP; 619 }; 620 621 // TODO: Perform finalization actions for variables. This has to be 622 // called for variables which have destructors/finalizers. 623 auto finiCB = [&](InsertPointTy codeGenIP) {}; 624 625 llvm::OpenMPIRBuilder::LocationDescription ompLoc( 626 builder.saveIP(), builder.getCurrentDebugLocation()); 627 builder.restoreIP(moduleTranslation.getOpenMPBuilder()->createSections( 628 ompLoc, findAllocaInsertPoint(builder, moduleTranslation), sectionCBs, 629 privCB, finiCB, false, sectionsOp.nowait())); 630 return bodyGenStatus; 631 } 632 633 /// Converts an OpenMP workshare loop into LLVM IR using OpenMPIRBuilder. 634 static LogicalResult 635 convertOmpWsLoop(Operation &opInst, llvm::IRBuilderBase &builder, 636 LLVM::ModuleTranslation &moduleTranslation) { 637 auto loop = cast<omp::WsLoopOp>(opInst); 638 // TODO: this should be in the op verifier instead. 639 if (loop.lowerBound().empty()) 640 return failure(); 641 642 // Static is the default. 643 auto schedule = 644 loop.schedule_val().getValueOr(omp::ClauseScheduleKind::Static); 645 646 // Find the loop configuration. 647 llvm::Value *step = moduleTranslation.lookupValue(loop.step()[0]); 648 llvm::Type *ivType = step->getType(); 649 llvm::Value *chunk = nullptr; 650 if (loop.schedule_chunk_var()) { 651 llvm::Value *chunkVar = 652 moduleTranslation.lookupValue(loop.schedule_chunk_var()); 653 llvm::Type *chunkVarType = chunkVar->getType(); 654 assert(chunkVarType->isIntegerTy() && 655 "chunk size must be one integer expression"); 656 if (chunkVarType->getIntegerBitWidth() < ivType->getIntegerBitWidth()) 657 chunk = builder.CreateSExt(chunkVar, ivType); 658 else if (chunkVarType->getIntegerBitWidth() > ivType->getIntegerBitWidth()) 659 chunk = builder.CreateTrunc(chunkVar, ivType); 660 else 661 chunk = chunkVar; 662 } 663 664 SmallVector<omp::ReductionDeclareOp> reductionDecls; 665 collectReductionDecls(loop, reductionDecls); 666 llvm::OpenMPIRBuilder::InsertPointTy allocaIP = 667 findAllocaInsertPoint(builder, moduleTranslation); 668 669 // Allocate space for privatized reduction variables. 670 SmallVector<llvm::Value *> privateReductionVariables; 671 DenseMap<Value, llvm::Value *> reductionVariableMap; 672 unsigned numReductions = loop.getNumReductionVars(); 673 privateReductionVariables.reserve(numReductions); 674 if (numReductions != 0) { 675 llvm::IRBuilderBase::InsertPointGuard guard(builder); 676 builder.restoreIP(allocaIP); 677 for (unsigned i = 0; i < numReductions; ++i) { 678 auto reductionType = 679 loop.reduction_vars()[i].getType().cast<LLVM::LLVMPointerType>(); 680 llvm::Value *var = builder.CreateAlloca( 681 moduleTranslation.convertType(reductionType.getElementType())); 682 privateReductionVariables.push_back(var); 683 reductionVariableMap.try_emplace(loop.reduction_vars()[i], var); 684 } 685 } 686 687 // Store the mapping between reduction variables and their private copies on 688 // ModuleTranslation stack. It can be then recovered when translating 689 // omp.reduce operations in a separate call. 690 LLVM::ModuleTranslation::SaveStack<OpenMPVarMappingStackFrame> mappingGuard( 691 moduleTranslation, reductionVariableMap); 692 693 // Before the loop, store the initial values of reductions into reduction 694 // variables. Although this could be done after allocas, we don't want to mess 695 // up with the alloca insertion point. 696 for (unsigned i = 0; i < numReductions; ++i) { 697 SmallVector<llvm::Value *> phis; 698 if (failed(inlineConvertOmpRegions(reductionDecls[i].initializerRegion(), 699 "omp.reduction.neutral", builder, 700 moduleTranslation, &phis))) 701 return failure(); 702 assert(phis.size() == 1 && "expected one value to be yielded from the " 703 "reduction neutral element declaration region"); 704 builder.CreateStore(phis[0], privateReductionVariables[i]); 705 } 706 707 // Set up the source location value for OpenMP runtime. 708 llvm::DISubprogram *subprogram = 709 builder.GetInsertBlock()->getParent()->getSubprogram(); 710 const llvm::DILocation *diLoc = 711 moduleTranslation.translateLoc(opInst.getLoc(), subprogram); 712 llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder.saveIP(), 713 llvm::DebugLoc(diLoc)); 714 715 // Generator of the canonical loop body. 716 // TODO: support error propagation in OpenMPIRBuilder and use it instead of 717 // relying on captured variables. 718 SmallVector<llvm::CanonicalLoopInfo *> loopInfos; 719 SmallVector<llvm::OpenMPIRBuilder::InsertPointTy> bodyInsertPoints; 720 LogicalResult bodyGenStatus = success(); 721 auto bodyGen = [&](llvm::OpenMPIRBuilder::InsertPointTy ip, llvm::Value *iv) { 722 // Make sure further conversions know about the induction variable. 723 moduleTranslation.mapValue( 724 loop.getRegion().front().getArgument(loopInfos.size()), iv); 725 726 // Capture the body insertion point for use in nested loops. BodyIP of the 727 // CanonicalLoopInfo always points to the beginning of the entry block of 728 // the body. 729 bodyInsertPoints.push_back(ip); 730 731 if (loopInfos.size() != loop.getNumLoops() - 1) 732 return; 733 734 // Convert the body of the loop. 735 llvm::BasicBlock *entryBlock = ip.getBlock(); 736 llvm::BasicBlock *exitBlock = 737 entryBlock->splitBasicBlock(ip.getPoint(), "omp.wsloop.exit"); 738 convertOmpOpRegions(loop.region(), "omp.wsloop.region", *entryBlock, 739 *exitBlock, builder, moduleTranslation, bodyGenStatus); 740 }; 741 742 // Delegate actual loop construction to the OpenMP IRBuilder. 743 // TODO: this currently assumes WsLoop is semantically similar to SCF loop, 744 // i.e. it has a positive step, uses signed integer semantics. Reconsider 745 // this code when WsLoop clearly supports more cases. 746 llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder(); 747 for (unsigned i = 0, e = loop.getNumLoops(); i < e; ++i) { 748 llvm::Value *lowerBound = 749 moduleTranslation.lookupValue(loop.lowerBound()[i]); 750 llvm::Value *upperBound = 751 moduleTranslation.lookupValue(loop.upperBound()[i]); 752 llvm::Value *step = moduleTranslation.lookupValue(loop.step()[i]); 753 754 // Make sure loop trip count are emitted in the preheader of the outermost 755 // loop at the latest so that they are all available for the new collapsed 756 // loop will be created below. 757 llvm::OpenMPIRBuilder::LocationDescription loc = ompLoc; 758 llvm::OpenMPIRBuilder::InsertPointTy computeIP = ompLoc.IP; 759 if (i != 0) { 760 loc = llvm::OpenMPIRBuilder::LocationDescription(bodyInsertPoints.back(), 761 llvm::DebugLoc(diLoc)); 762 computeIP = loopInfos.front()->getPreheaderIP(); 763 } 764 loopInfos.push_back(ompBuilder->createCanonicalLoop( 765 loc, bodyGen, lowerBound, upperBound, step, 766 /*IsSigned=*/true, loop.inclusive(), computeIP)); 767 768 if (failed(bodyGenStatus)) 769 return failure(); 770 } 771 772 // Collapse loops. Store the insertion point because LoopInfos may get 773 // invalidated. 774 llvm::IRBuilderBase::InsertPoint afterIP = loopInfos.front()->getAfterIP(); 775 llvm::CanonicalLoopInfo *loopInfo = 776 ompBuilder->collapseLoops(diLoc, loopInfos, {}); 777 778 allocaIP = findAllocaInsertPoint(builder, moduleTranslation); 779 780 bool isSimd = loop.simd_modifier(); 781 782 if (schedule == omp::ClauseScheduleKind::Static) { 783 ompBuilder->applyStaticWorkshareLoop(ompLoc.DL, loopInfo, allocaIP, 784 !loop.nowait(), chunk); 785 } else { 786 llvm::omp::OMPScheduleType schedType; 787 switch (schedule) { 788 case omp::ClauseScheduleKind::Dynamic: 789 schedType = llvm::omp::OMPScheduleType::DynamicChunked; 790 break; 791 case omp::ClauseScheduleKind::Guided: 792 if (isSimd) 793 schedType = llvm::omp::OMPScheduleType::GuidedSimd; 794 else 795 schedType = llvm::omp::OMPScheduleType::GuidedChunked; 796 break; 797 case omp::ClauseScheduleKind::Auto: 798 schedType = llvm::omp::OMPScheduleType::Auto; 799 break; 800 case omp::ClauseScheduleKind::Runtime: 801 if (isSimd) 802 schedType = llvm::omp::OMPScheduleType::RuntimeSimd; 803 else 804 schedType = llvm::omp::OMPScheduleType::Runtime; 805 break; 806 default: 807 llvm_unreachable("Unknown schedule value"); 808 break; 809 } 810 811 if (Optional<omp::ScheduleModifier> modifier = loop.schedule_modifier()) { 812 switch (*modifier) { 813 case omp::ScheduleModifier::monotonic: 814 schedType |= llvm::omp::OMPScheduleType::ModifierMonotonic; 815 break; 816 case omp::ScheduleModifier::nonmonotonic: 817 schedType |= llvm::omp::OMPScheduleType::ModifierNonmonotonic; 818 break; 819 default: 820 // Nothing to do here. 821 break; 822 } 823 } 824 afterIP = ompBuilder->applyDynamicWorkshareLoop( 825 ompLoc.DL, loopInfo, allocaIP, schedType, !loop.nowait(), chunk); 826 } 827 828 // Continue building IR after the loop. Note that the LoopInfo returned by 829 // `collapseLoops` points inside the outermost loop and is intended for 830 // potential further loop transformations. Use the insertion point stored 831 // before collapsing loops instead. 832 builder.restoreIP(afterIP); 833 834 // Process the reductions if required. 835 if (numReductions == 0) 836 return success(); 837 838 // Create the reduction generators. We need to own them here because 839 // ReductionInfo only accepts references to the generators. 840 SmallVector<OwningReductionGen> owningReductionGens; 841 SmallVector<OwningAtomicReductionGen> owningAtomicReductionGens; 842 for (unsigned i = 0; i < numReductions; ++i) { 843 owningReductionGens.push_back( 844 makeReductionGen(reductionDecls[i], builder, moduleTranslation)); 845 owningAtomicReductionGens.push_back( 846 makeAtomicReductionGen(reductionDecls[i], builder, moduleTranslation)); 847 } 848 849 // Collect the reduction information. 850 SmallVector<llvm::OpenMPIRBuilder::ReductionInfo> reductionInfos; 851 reductionInfos.reserve(numReductions); 852 for (unsigned i = 0; i < numReductions; ++i) { 853 llvm::OpenMPIRBuilder::AtomicReductionGenTy atomicGen = nullptr; 854 if (owningAtomicReductionGens[i]) 855 atomicGen = owningAtomicReductionGens[i]; 856 llvm::Value *variable = 857 moduleTranslation.lookupValue(loop.reduction_vars()[i]); 858 reductionInfos.push_back({variable->getType()->getPointerElementType(), 859 variable, privateReductionVariables[i], 860 owningReductionGens[i], atomicGen}); 861 } 862 863 // The call to createReductions below expects the block to have a 864 // terminator. Create an unreachable instruction to serve as terminator 865 // and remove it later. 866 llvm::UnreachableInst *tempTerminator = builder.CreateUnreachable(); 867 builder.SetInsertPoint(tempTerminator); 868 llvm::OpenMPIRBuilder::InsertPointTy contInsertPoint = 869 ompBuilder->createReductions(builder.saveIP(), allocaIP, reductionInfos, 870 loop.nowait()); 871 if (!contInsertPoint.getBlock()) 872 return loop->emitOpError() << "failed to convert reductions"; 873 auto nextInsertionPoint = 874 ompBuilder->createBarrier(contInsertPoint, llvm::omp::OMPD_for); 875 tempTerminator->eraseFromParent(); 876 builder.restoreIP(nextInsertionPoint); 877 878 return success(); 879 } 880 881 // Convert an Atomic Ordering attribute to llvm::AtomicOrdering. 882 llvm::AtomicOrdering 883 convertAtomicOrdering(Optional<omp::ClauseMemoryOrderKind> ao) { 884 if (!ao) 885 return llvm::AtomicOrdering::Monotonic; // Default Memory Ordering 886 887 switch (*ao) { 888 case omp::ClauseMemoryOrderKind::seq_cst: 889 return llvm::AtomicOrdering::SequentiallyConsistent; 890 case omp::ClauseMemoryOrderKind::acq_rel: 891 return llvm::AtomicOrdering::AcquireRelease; 892 case omp::ClauseMemoryOrderKind::acquire: 893 return llvm::AtomicOrdering::Acquire; 894 case omp::ClauseMemoryOrderKind::release: 895 return llvm::AtomicOrdering::Release; 896 case omp::ClauseMemoryOrderKind::relaxed: 897 return llvm::AtomicOrdering::Monotonic; 898 } 899 } 900 901 // Convert omp.atomic.read operation to LLVM IR. 902 static LogicalResult 903 convertOmpAtomicRead(Operation &opInst, llvm::IRBuilderBase &builder, 904 LLVM::ModuleTranslation &moduleTranslation) { 905 906 auto readOp = cast<omp::AtomicReadOp>(opInst); 907 llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder(); 908 909 // Set up the source location value for OpenMP runtime. 910 llvm::DISubprogram *subprogram = 911 builder.GetInsertBlock()->getParent()->getSubprogram(); 912 const llvm::DILocation *diLoc = 913 moduleTranslation.translateLoc(opInst.getLoc(), subprogram); 914 llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder.saveIP(), 915 llvm::DebugLoc(diLoc)); 916 llvm::AtomicOrdering AO = convertAtomicOrdering(readOp.memory_order()); 917 llvm::Value *x = moduleTranslation.lookupValue(readOp.x()); 918 llvm::Value *v = moduleTranslation.lookupValue(readOp.v()); 919 llvm::OpenMPIRBuilder::AtomicOpValue V = {v, false, false}; 920 llvm::OpenMPIRBuilder::AtomicOpValue X = {x, false, false}; 921 builder.restoreIP(ompBuilder->createAtomicRead(ompLoc, X, V, AO)); 922 return success(); 923 } 924 925 /// Converts an omp.atomic.write operation to LLVM IR. 926 static LogicalResult 927 convertOmpAtomicWrite(Operation &opInst, llvm::IRBuilderBase &builder, 928 LLVM::ModuleTranslation &moduleTranslation) { 929 auto writeOp = cast<omp::AtomicWriteOp>(opInst); 930 llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder(); 931 932 // Set up the source location value for OpenMP runtime. 933 llvm::DISubprogram *subprogram = 934 builder.GetInsertBlock()->getParent()->getSubprogram(); 935 const llvm::DILocation *diLoc = 936 moduleTranslation.translateLoc(opInst.getLoc(), subprogram); 937 llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder.saveIP(), 938 llvm::DebugLoc(diLoc)); 939 llvm::AtomicOrdering ao = convertAtomicOrdering(writeOp.memory_order()); 940 llvm::Value *expr = moduleTranslation.lookupValue(writeOp.value()); 941 llvm::Value *dest = moduleTranslation.lookupValue(writeOp.address()); 942 llvm::OpenMPIRBuilder::AtomicOpValue x = {dest, /*isSigned=*/false, 943 /*isVolatile=*/false}; 944 builder.restoreIP(ompBuilder->createAtomicWrite(ompLoc, x, expr, ao)); 945 return success(); 946 } 947 948 /// Converts an OpenMP reduction operation using OpenMPIRBuilder. Expects the 949 /// mapping between reduction variables and their private equivalents to have 950 /// been stored on the ModuleTranslation stack. Currently only supports 951 /// reduction within WsLoopOp, but can be easily extended. 952 static LogicalResult 953 convertOmpReductionOp(omp::ReductionOp reductionOp, 954 llvm::IRBuilderBase &builder, 955 LLVM::ModuleTranslation &moduleTranslation) { 956 // Find the declaration that corresponds to the reduction op. 957 auto reductionContainer = reductionOp->getParentOfType<omp::WsLoopOp>(); 958 omp::ReductionDeclareOp declaration = 959 findReductionDecl(reductionContainer, reductionOp); 960 assert(declaration && "could not find reduction declaration"); 961 962 // Retrieve the mapping between reduction variables and their private 963 // equivalents. 964 const DenseMap<Value, llvm::Value *> *reductionVariableMap = nullptr; 965 moduleTranslation.stackWalk<OpenMPVarMappingStackFrame>( 966 [&](const OpenMPVarMappingStackFrame &frame) { 967 reductionVariableMap = &frame.mapping; 968 return WalkResult::interrupt(); 969 }); 970 assert(reductionVariableMap && "couldn't find private reduction variables"); 971 972 // Translate the reduction operation by emitting the body of the corresponding 973 // reduction declaration. 974 Region &reductionRegion = declaration.reductionRegion(); 975 llvm::Value *privateReductionVar = 976 reductionVariableMap->lookup(reductionOp.accumulator()); 977 llvm::Value *reductionVal = builder.CreateLoad( 978 moduleTranslation.convertType(reductionOp.operand().getType()), 979 privateReductionVar); 980 981 moduleTranslation.mapValue(reductionRegion.front().getArgument(0), 982 reductionVal); 983 moduleTranslation.mapValue( 984 reductionRegion.front().getArgument(1), 985 moduleTranslation.lookupValue(reductionOp.operand())); 986 987 SmallVector<llvm::Value *> phis; 988 if (failed(inlineConvertOmpRegions(reductionRegion, "omp.reduction.body", 989 builder, moduleTranslation, &phis))) 990 return failure(); 991 assert(phis.size() == 1 && "expected one value to be yielded from " 992 "the reduction body declaration region"); 993 builder.CreateStore(phis[0], privateReductionVar); 994 return success(); 995 } 996 997 namespace { 998 999 /// Implementation of the dialect interface that converts operations belonging 1000 /// to the OpenMP dialect to LLVM IR. 1001 class OpenMPDialectLLVMIRTranslationInterface 1002 : public LLVMTranslationDialectInterface { 1003 public: 1004 using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface; 1005 1006 /// Translates the given operation to LLVM IR using the provided IR builder 1007 /// and saving the state in `moduleTranslation`. 1008 LogicalResult 1009 convertOperation(Operation *op, llvm::IRBuilderBase &builder, 1010 LLVM::ModuleTranslation &moduleTranslation) const final; 1011 }; 1012 1013 } // namespace 1014 1015 /// Given an OpenMP MLIR operation, create the corresponding LLVM IR 1016 /// (including OpenMP runtime calls). 1017 LogicalResult OpenMPDialectLLVMIRTranslationInterface::convertOperation( 1018 Operation *op, llvm::IRBuilderBase &builder, 1019 LLVM::ModuleTranslation &moduleTranslation) const { 1020 1021 llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder(); 1022 1023 return llvm::TypeSwitch<Operation *, LogicalResult>(op) 1024 .Case([&](omp::BarrierOp) { 1025 ompBuilder->createBarrier(builder.saveIP(), llvm::omp::OMPD_barrier); 1026 return success(); 1027 }) 1028 .Case([&](omp::TaskwaitOp) { 1029 ompBuilder->createTaskwait(builder.saveIP()); 1030 return success(); 1031 }) 1032 .Case([&](omp::TaskyieldOp) { 1033 ompBuilder->createTaskyield(builder.saveIP()); 1034 return success(); 1035 }) 1036 .Case([&](omp::FlushOp) { 1037 // No support in Openmp runtime function (__kmpc_flush) to accept 1038 // the argument list. 1039 // OpenMP standard states the following: 1040 // "An implementation may implement a flush with a list by ignoring 1041 // the list, and treating it the same as a flush without a list." 1042 // 1043 // The argument list is discarded so that, flush with a list is treated 1044 // same as a flush without a list. 1045 ompBuilder->createFlush(builder.saveIP()); 1046 return success(); 1047 }) 1048 .Case([&](omp::ParallelOp op) { 1049 return convertOmpParallel(op, builder, moduleTranslation); 1050 }) 1051 .Case([&](omp::ReductionOp reductionOp) { 1052 return convertOmpReductionOp(reductionOp, builder, moduleTranslation); 1053 }) 1054 .Case([&](omp::MasterOp) { 1055 return convertOmpMaster(*op, builder, moduleTranslation); 1056 }) 1057 .Case([&](omp::CriticalOp) { 1058 return convertOmpCritical(*op, builder, moduleTranslation); 1059 }) 1060 .Case([&](omp::OrderedRegionOp) { 1061 return convertOmpOrderedRegion(*op, builder, moduleTranslation); 1062 }) 1063 .Case([&](omp::OrderedOp) { 1064 return convertOmpOrdered(*op, builder, moduleTranslation); 1065 }) 1066 .Case([&](omp::WsLoopOp) { 1067 return convertOmpWsLoop(*op, builder, moduleTranslation); 1068 }) 1069 .Case([&](omp::AtomicReadOp) { 1070 return convertOmpAtomicRead(*op, builder, moduleTranslation); 1071 }) 1072 .Case([&](omp::AtomicWriteOp) { 1073 return convertOmpAtomicWrite(*op, builder, moduleTranslation); 1074 }) 1075 .Case([&](omp::SectionsOp) { 1076 return convertOmpSections(*op, builder, moduleTranslation); 1077 }) 1078 .Case<omp::YieldOp, omp::TerminatorOp, omp::ReductionDeclareOp, 1079 omp::CriticalDeclareOp>([](auto op) { 1080 // `yield` and `terminator` can be just omitted. The block structure 1081 // was created in the region that handles their parent operation. 1082 // `reduction.declare` will be used by reductions and is not 1083 // converted directly, skip it. 1084 // `critical.declare` is only used to declare names of critical 1085 // sections which will be used by `critical` ops and hence can be 1086 // ignored for lowering. The OpenMP IRBuilder will create unique 1087 // name for critical section names. 1088 return success(); 1089 }) 1090 .Default([&](Operation *inst) { 1091 return inst->emitError("unsupported OpenMP operation: ") 1092 << inst->getName(); 1093 }); 1094 } 1095 1096 void mlir::registerOpenMPDialectTranslation(DialectRegistry ®istry) { 1097 registry.insert<omp::OpenMPDialect>(); 1098 registry.addDialectInterface<omp::OpenMPDialect, 1099 OpenMPDialectLLVMIRTranslationInterface>(); 1100 } 1101 1102 void mlir::registerOpenMPDialectTranslation(MLIRContext &context) { 1103 DialectRegistry registry; 1104 registerOpenMPDialectTranslation(registry); 1105 context.appendDialectRegistry(registry); 1106 } 1107