diff options
| author | Johannes Doerfert <johannes@jdoerfert.de> | 2019-11-05 18:57:44 -0600 |
|---|---|---|
| committer | Johannes Doerfert <johannes@jdoerfert.de> | 2019-12-11 14:38:49 -0600 |
| commit | d23c61490c282a7a8f29aaa5c021cbfdaf87fb6f (patch) | |
| tree | fa809d5b6ada579a2e063c1cb53c9759661273eb /llvm/lib/Frontend | |
| parent | 6d64162a2d0df2230faf02ff7ee677c448faf4af (diff) | |
| download | bcm5719-llvm-d23c61490c282a7a8f29aaa5c021cbfdaf87fb6f.tar.gz bcm5719-llvm-d23c61490c282a7a8f29aaa5c021cbfdaf87fb6f.zip | |
[OpenMP] Introduce the OpenMP-IR-Builder
This is the initial patch for the OpenMP-IR-Builder, as discussed on the
mailing list ([1] and later) and at the US Dev Meeting'19.
The design is similar to D61953 but:
- in a non-WIP status, with proper documentation and working.
- using a OpenMPKinds.def file to manage lists of directives, runtime
functions, types, ..., similar to the current Clang implementation.
- restricted to handle only (simple) barriers, to implement most
`#pragma omp barrier` directives and most implicit barriers.
- properly hooked into Clang to be used if possible (D69922).
- compatible with the remaining code generation.
Parts have been extracted into D69853.
The plan is to have multiple people working on moving logic from Clang
here once the initial scaffolding (=this patch) landed.
[1] http://lists.flang-compiler.org/pipermail/flang-dev_lists.flang-compiler.org/2019-May/000197.html
Reviewers: kiranchandramohan, ABataev, RaviNarayanaswamy, gtbercea, grokos, sdmitriev, JonChesterfield, hfinkel, fghanim
Subscribers: mgorny, hiraditya, bollu, guansong, jfb, cfe-commits, llvm-commits, penzn, ppenzin
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D69785
Diffstat (limited to 'llvm/lib/Frontend')
| -rw-r--r-- | llvm/lib/Frontend/OpenMP/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | llvm/lib/Frontend/OpenMP/OMPConstants.cpp | 53 | ||||
| -rw-r--r-- | llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 236 |
3 files changed, 291 insertions, 1 deletions
diff --git a/llvm/lib/Frontend/OpenMP/CMakeLists.txt b/llvm/lib/Frontend/OpenMP/CMakeLists.txt index 5d74bcf3b9f..e7d1f8e57f6 100644 --- a/llvm/lib/Frontend/OpenMP/CMakeLists.txt +++ b/llvm/lib/Frontend/OpenMP/CMakeLists.txt @@ -1,9 +1,10 @@ add_llvm_component_library(LLVMFrontendOpenMP OMPConstants.cpp + OMPIRBuilder.cpp ADDITIONAL_HEADER_DIRS ${LLVM_MAIN_INCLUDE_DIR}/llvm/Frontend - ${LLVM_MAIN_INCLUDE_DIR}/llvm/Frontend/OpenMP + ${LLVM_MAIN_INCLUDE_DIR}/llvm/Frontend/OpenMP/OMP DEPENDS intrinsics_gen diff --git a/llvm/lib/Frontend/OpenMP/OMPConstants.cpp b/llvm/lib/Frontend/OpenMP/OMPConstants.cpp index 994fb4e2fa8..67d88ffb17a 100644 --- a/llvm/lib/Frontend/OpenMP/OMPConstants.cpp +++ b/llvm/lib/Frontend/OpenMP/OMPConstants.cpp @@ -12,9 +12,12 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSwitch.h" +#include "llvm/IR/Module.h" +#include "llvm/IR/Type.h" using namespace llvm; using namespace omp; +using namespace types; Directive llvm::omp::getOpenMPDirectiveKind(StringRef Str) { return llvm::StringSwitch<Directive>(Str) @@ -32,3 +35,53 @@ StringRef llvm::omp::getOpenMPDirectiveName(Directive Kind) { } llvm_unreachable("Invalid OpenMP directive kind"); } + +/// Declarations for LLVM-IR types (simple, function and structure) are +/// generated below. Their names are defined and used in OpenMPKinds.def. Here +/// we provide the declarations, the initializeTypes function will provide the +/// values. +/// +///{ + +#define OMP_TYPE(VarName, InitValue) Type *llvm::omp::types::VarName = nullptr; +#define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \ + FunctionType *llvm::omp::types::VarName = nullptr; \ + PointerType *llvm::omp::types::VarName##Ptr = nullptr; +#define OMP_STRUCT_TYPE(VarName, StrName, ...) \ + StructType *llvm::omp::types::VarName = nullptr; \ + PointerType *llvm::omp::types::VarName##Ptr = nullptr; +#include "llvm/Frontend/OpenMP/OMPKinds.def" + +///} + +void llvm::omp::types::initializeTypes(Module &M) { + if (Void) + return; + + LLVMContext &Ctx = M.getContext(); + // Create all simple and struct types exposed by the runtime and remember + // the llvm::PointerTypes of them for easy access later. + StructType *T; +#define OMP_TYPE(VarName, InitValue) VarName = InitValue; +#define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \ + VarName = FunctionType::get(ReturnType, {__VA_ARGS__}, IsVarArg); \ + VarName##Ptr = PointerType::getUnqual(T); +#define OMP_STRUCT_TYPE(VarName, StructName, ...) \ + T = M.getTypeByName(StructName); \ + if (!T) \ + T = StructType::create(Ctx, {__VA_ARGS__}, StructName); \ + VarName = T; \ + VarName##Ptr = PointerType::getUnqual(T); +#include "llvm/Frontend/OpenMP/OMPKinds.def" +} + +void llvm::omp::types::uninitializeTypes() { +#define OMP_TYPE(VarName, InitValue) VarName = nullptr; +#define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \ + VarName = nullptr; \ + VarName##Ptr = nullptr; +#define OMP_STRUCT_TYPE(VarName, StrName, ...) \ + VarName = nullptr; \ + VarName##Ptr = nullptr; +#include "llvm/Frontend/OpenMP/OMPKinds.def" +} diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp new file mode 100644 index 00000000000..13733394b07 --- /dev/null +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -0,0 +1,236 @@ +//===- OpenMPIRBuilder.cpp - Builder for LLVM-IR for OpenMP directives ----===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// \file +/// +/// This file implements the OpenMPIRBuilder class, which is used as a +/// convenient way to create LLVM instructions for OpenMP directives. +/// +//===----------------------------------------------------------------------===// + +#include "llvm/Frontend/OpenMP/OMPIRBuilder.h" + +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/IR/DebugInfo.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Transforms/Utils/BasicBlockUtils.h" + +#include <sstream> + +#define DEBUG_TYPE "openmp-ir-builder" + +using namespace llvm; +using namespace omp; +using namespace types; + +static cl::opt<bool> + OptimisticAttributes("openmp-ir-builder-optimistic-attributes", cl::Hidden, + cl::desc("Use optimistic attributes describing " + "'as-if' properties of runtime calls."), + cl::init(false)); + +void OpenMPIRBuilder::addAttributes(omp::RuntimeFunction FnID, Function &Fn) { + LLVMContext &Ctx = Fn.getContext(); + +#define OMP_ATTRS_SET(VarName, AttrSet) AttributeSet VarName = AttrSet; +#include "llvm/Frontend/OpenMP/OMPKinds.def" + + // Add attributes to the new declaration. + switch (FnID) { +#define OMP_RTL_ATTRS(Enum, FnAttrSet, RetAttrSet, ArgAttrSets) \ + case Enum: \ + Fn.setAttributes( \ + AttributeList::get(Ctx, FnAttrSet, RetAttrSet, ArgAttrSets)); \ + break; +#include "llvm/Frontend/OpenMP/OMPKinds.def" + default: + // Attributes are optional. + break; + } +} + +Function *OpenMPIRBuilder::getOrCreateRuntimeFunction(RuntimeFunction FnID) { + Function *Fn = nullptr; + + // Try to find the declation in the module first. + switch (FnID) { +#define OMP_RTL(Enum, Str, IsVarArg, ReturnType, ...) \ + case Enum: \ + Fn = M.getFunction(Str); \ + break; +#include "llvm/Frontend/OpenMP/OMPKinds.def" + } + + if (!Fn) { + // Create a new declaration if we need one. + switch (FnID) { +#define OMP_RTL(Enum, Str, IsVarArg, ReturnType, ...) \ + case Enum: \ + Fn = Function::Create(FunctionType::get(ReturnType, \ + ArrayRef<Type *>{__VA_ARGS__}, \ + IsVarArg), \ + GlobalValue::ExternalLinkage, Str, M); \ + break; +#include "llvm/Frontend/OpenMP/OMPKinds.def" + } + + addAttributes(FnID, *Fn); + } + + assert(Fn && "Failed to create OpenMP runtime function"); + return Fn; +} + +void OpenMPIRBuilder::initialize() { initializeTypes(M); } + +Value *OpenMPIRBuilder::getOrCreateIdent(Constant *SrcLocStr, + IdentFlag LocFlags) { + // Enable "C-mode". + LocFlags |= OMP_IDENT_FLAG_KMPC; + + GlobalVariable *&DefaultIdent = IdentMap[{SrcLocStr, uint64_t(LocFlags)}]; + if (!DefaultIdent) { + Constant *I32Null = ConstantInt::getNullValue(Int32); + Constant *IdentData[] = {I32Null, + ConstantInt::get(Int32, uint64_t(LocFlags)), + I32Null, I32Null, SrcLocStr}; + Constant *Initializer = ConstantStruct::get( + cast<StructType>(IdentPtr->getPointerElementType()), IdentData); + + // Look for existing encoding of the location + flags, not needed but + // minimizes the difference to the existing solution while we transition. + for (GlobalVariable &GV : M.getGlobalList()) + if (GV.getType() == IdentPtr && GV.hasInitializer()) + if (GV.getInitializer() == Initializer) + return DefaultIdent = &GV; + + DefaultIdent = new GlobalVariable(M, IdentPtr->getPointerElementType(), + /* isConstant = */ false, + GlobalValue::PrivateLinkage, Initializer); + DefaultIdent->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); + DefaultIdent->setAlignment(Align(8)); + } + return DefaultIdent; +} + +Constant *OpenMPIRBuilder::getOrCreateSrcLocStr(StringRef LocStr) { + Constant *&SrcLocStr = SrcLocStrMap[LocStr]; + if (!SrcLocStr) { + Constant *Initializer = + ConstantDataArray::getString(M.getContext(), LocStr); + + // Look for existing encoding of the location, not needed but minimizes the + // difference to the existing solution while we transition. + for (GlobalVariable &GV : M.getGlobalList()) + if (GV.isConstant() && GV.hasInitializer() && + GV.getInitializer() == Initializer) + return SrcLocStr = ConstantExpr::getPointerCast(&GV, Int8Ptr); + + SrcLocStr = Builder.CreateGlobalStringPtr(LocStr); + } + return SrcLocStr; +} + +Constant *OpenMPIRBuilder::getOrCreateDefaultSrcLocStr() { + return getOrCreateSrcLocStr(";unknown;unknown;0;0;;"); +} + +Constant * +OpenMPIRBuilder::getOrCreateSrcLocStr(const LocationDescription &Loc) { + DILocation *DIL = Loc.DL.get(); + if (!DIL) + return getOrCreateDefaultSrcLocStr(); + StringRef Filename = + !DIL->getFilename().empty() ? DIL->getFilename() : M.getName(); + StringRef Function = DIL->getScope()->getSubprogram()->getName(); + Function = + !Function.empty() ? Function : Loc.IP.getBlock()->getParent()->getName(); + std::string LineStr = std::to_string(DIL->getLine()); + std::string ColumnStr = std::to_string(DIL->getColumn()); + std::stringstream SrcLocStr; + SrcLocStr << ";" << Filename.data() << ";" << Function.data() << ";" + << LineStr << ";" << ColumnStr << ";;"; + return getOrCreateSrcLocStr(SrcLocStr.str()); +} + +Value *OpenMPIRBuilder::getOrCreateThreadID(Value *Ident) { + return Builder.CreateCall( + getOrCreateRuntimeFunction(OMPRTL___kmpc_global_thread_num), Ident, + "omp_global_thread_num"); +} + +OpenMPIRBuilder::InsertPointTy +OpenMPIRBuilder::CreateBarrier(const LocationDescription &Loc, Directive DK, + bool ForceSimpleCall, bool CheckCancelFlag) { + if (!updateToLocation(Loc)) + return Loc.IP; + return emitBarrierImpl(Loc, DK, ForceSimpleCall, CheckCancelFlag); +} + +OpenMPIRBuilder::InsertPointTy +OpenMPIRBuilder::emitBarrierImpl(const LocationDescription &Loc, Directive Kind, + bool ForceSimpleCall, bool CheckCancelFlag) { + // Build call __kmpc_cancel_barrier(loc, thread_id) or + // __kmpc_barrier(loc, thread_id); + + IdentFlag BarrierLocFlags; + switch (Kind) { + case OMPD_for: + BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_IMPL_FOR; + break; + case OMPD_sections: + BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_IMPL_SECTIONS; + break; + case OMPD_single: + BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_IMPL_SINGLE; + break; + case OMPD_barrier: + BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_EXPL; + break; + default: + BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_IMPL; + break; + } + + Constant *SrcLocStr = getOrCreateSrcLocStr(Loc); + Value *Args[] = {getOrCreateIdent(SrcLocStr, BarrierLocFlags), + getOrCreateThreadID(getOrCreateIdent(SrcLocStr))}; + + // If we are in a cancellable parallel region, barriers are cancellation + // points. + // TODO: Check why we would force simple calls or to ignore the cancel flag. + bool UseCancelBarrier = !ForceSimpleCall && CancellationBlock; + + Value *Result = Builder.CreateCall( + getOrCreateRuntimeFunction(UseCancelBarrier ? OMPRTL___kmpc_cancel_barrier + : OMPRTL___kmpc_barrier), + Args); + + if (UseCancelBarrier && CheckCancelFlag) { + // For a cancel barrier we create two new blocks. + BasicBlock *BB = Builder.GetInsertBlock(); + BasicBlock *NonCancellationBlock = BasicBlock::Create( + BB->getContext(), BB->getName() + ".cont", BB->getParent()); + + // Jump to them based on the return value. + Value *Cmp = Builder.CreateIsNull(Result); + Builder.CreateCondBr(Cmp, NonCancellationBlock, CancellationBlock, + /* TODO weight */ nullptr, nullptr); + + Builder.SetInsertPoint(NonCancellationBlock); + assert(CancellationBlock->getParent() == BB->getParent() && + "Unexpected cancellation block parent!"); + + // TODO: This is a workaround for now, we always reset the cancellation + // block until we manage it ourselves here. + CancellationBlock = nullptr; + } + + return Builder.saveIP(); +} |

