brintos

brintos / llvm-project-archived public Read only

0
0
Text · 12.3 KiB · d52986d Raw
296 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8//9// This contains code to emit Decl nodes as CIR code.10//11//===----------------------------------------------------------------------===//12 13#include "CIRGenFunction.h"14#include "CIRGenOpenACCHelpers.h"15 16#include "mlir/Dialect/OpenACC/OpenACC.h"17#include "clang/AST/DeclOpenACC.h"18#include "llvm/Support/SaveAndRestore.h"19 20using namespace clang;21using namespace clang::CIRGen;22 23namespace {24struct OpenACCDeclareCleanup final : EHScopeStack::Cleanup {25  mlir::acc::DeclareEnterOp enterOp;26 27  OpenACCDeclareCleanup(mlir::acc::DeclareEnterOp enterOp) : enterOp(enterOp) {}28 29  template <typename OutTy, typename InTy>30  void createOutOp(CIRGenFunction &cgf, InTy inOp) {31    if constexpr (std::is_same_v<OutTy, mlir::acc::DeleteOp>) {32      auto outOp =33          OutTy::create(cgf.getBuilder(), inOp.getLoc(), inOp,34                        inOp.getStructured(), inOp.getImplicit(),35                        llvm::Twine(inOp.getNameAttr()), inOp.getBounds());36      outOp.setDataClause(inOp.getDataClause());37      outOp.setModifiers(inOp.getModifiers());38    } else {39      auto outOp =40          OutTy::create(cgf.getBuilder(), inOp.getLoc(), inOp, inOp.getVarPtr(),41                        inOp.getStructured(), inOp.getImplicit(),42                        llvm::Twine(inOp.getNameAttr()), inOp.getBounds());43      outOp.setDataClause(inOp.getDataClause());44      outOp.setModifiers(inOp.getModifiers());45    }46  }47 48  void emit(CIRGenFunction &cgf) override {49    auto exitOp = mlir::acc::DeclareExitOp::create(50        cgf.getBuilder(), enterOp.getLoc(), enterOp, {});51 52    // Some data clauses need to be referenced in 'exit', AND need to have an53    // operation after the exit.  Copy these from the enter operation.54    for (mlir::Value val : enterOp.getDataClauseOperands()) {55      if (auto copyin = val.getDefiningOp<mlir::acc::CopyinOp>()) {56        switch (copyin.getDataClause()) {57        default:58          llvm_unreachable(59              "OpenACC local declare clause copyin unexpected data clause");60          break;61        case mlir::acc::DataClause::acc_copy:62          createOutOp<mlir::acc::CopyoutOp>(cgf, copyin);63          break;64        case mlir::acc::DataClause::acc_copyin:65          createOutOp<mlir::acc::DeleteOp>(cgf, copyin);66          break;67        }68      } else if (auto create = val.getDefiningOp<mlir::acc::CreateOp>()) {69        switch (create.getDataClause()) {70        default:71          llvm_unreachable(72              "OpenACC local declare clause create unexpected data clause");73          break;74        case mlir::acc::DataClause::acc_copyout:75          createOutOp<mlir::acc::CopyoutOp>(cgf, create);76          break;77        case mlir::acc::DataClause::acc_create:78          createOutOp<mlir::acc::DeleteOp>(cgf, create);79          break;80        }81      } else if (auto present = val.getDefiningOp<mlir::acc::PresentOp>()) {82        createOutOp<mlir::acc::DeleteOp>(cgf, present);83      } else if (auto dev_res =84                     val.getDefiningOp<mlir::acc::DeclareDeviceResidentOp>()) {85        createOutOp<mlir::acc::DeleteOp>(cgf, dev_res);86      } else if (val.getDefiningOp<mlir::acc::DeclareLinkOp>()) {87        // Link has no exit clauses, and shouldn't be copied.88        continue;89      } else if (val.getDefiningOp<mlir::acc::DevicePtrOp>()) {90        // DevicePtr has no exit clauses, and shouldn't be copied.91        continue;92      } else {93        llvm_unreachable("OpenACC local declare clause unexpected defining op");94        continue;95      }96      exitOp.getDataClauseOperandsMutable().append(val);97    }98  }99};100} // namespace101 102void CIRGenModule::emitGlobalOpenACCDecl(const OpenACCConstructDecl *d) {103  if (const auto *rd = dyn_cast<OpenACCRoutineDecl>(d))104    emitGlobalOpenACCRoutineDecl(rd);105  else106    emitGlobalOpenACCDeclareDecl(cast<OpenACCDeclareDecl>(d));107}108 109void CIRGenFunction::emitOpenACCDeclare(const OpenACCDeclareDecl &d) {110  mlir::Location exprLoc = cgm.getLoc(d.getBeginLoc());111  auto enterOp = mlir::acc::DeclareEnterOp::create(112      builder, exprLoc, mlir::acc::DeclareTokenType::get(&cgm.getMLIRContext()),113      {});114 115  emitOpenACCClauses(enterOp, OpenACCDirectiveKind::Declare, d.clauses());116 117  ehStack.pushCleanup<OpenACCDeclareCleanup>(CleanupKind::NormalCleanup,118                                             enterOp);119}120 121// Helper function that gets the declaration referenced by the declare clause.122// This is a simplified verison of the work that `getOpenACCDataOperandInfo`123// does, as it only has to get forms that 'declare' does.124static const Decl *getDeclareReferencedDecl(const Expr *e) {125  const Expr *curVarExpr = e->IgnoreParenImpCasts();126 127  // Since we allow array sections, we have to unpack the array sections here.128  // We don't have to worry about other bounds, since only variable or array129  // name (plus array sections as an extension) are permitted.130  while (const auto *ase = dyn_cast<ArraySectionExpr>(curVarExpr))131    curVarExpr = ase->getBase()->IgnoreParenImpCasts();132 133  if (const auto *dre = dyn_cast<DeclRefExpr>(curVarExpr))134    return dre->getFoundDecl()->getCanonicalDecl();135 136  // MemberExpr is allowed when it is implicit 'this'.137  return cast<MemberExpr>(curVarExpr)->getMemberDecl()->getCanonicalDecl();138}139 140template <typename BeforeOpTy, typename DataClauseTy>141void CIRGenModule::emitGlobalOpenACCDeclareDataOperands(142    const Expr *varOperand, DataClauseTy dataClause,143    OpenACCModifierKind modifiers, bool structured, bool implicit,144    bool requiresDtor) {145  // This is a template argument so that we don't have to include all of146  // mlir::acc into CIRGenModule.147  static_assert(std::is_same_v<DataClauseTy, mlir::acc::DataClause>);148  mlir::Location exprLoc = getLoc(varOperand->getBeginLoc());149  const Decl *refedDecl = getDeclareReferencedDecl(varOperand);150  StringRef varName = getMangledName(GlobalDecl{cast<VarDecl>(refedDecl)});151 152  // We have to emit two separate functions in this case, an acc_ctor and an153  // acc_dtor. These two sections are/should remain reasonably equal, however154  // the order of the clauses/vs-enter&exit in them makes combining these two155  // sections not particularly attractive, so we have a bit of repetition.156  {157    mlir::OpBuilder::InsertionGuard guardCase(builder);158    auto ctorOp = mlir::acc::GlobalConstructorOp::create(159        builder, exprLoc, (varName + "_acc_ctor").str());160    getModule().push_back(ctorOp);161    mlir::Block *block = builder.createBlock(&ctorOp.getRegion(),162                                             ctorOp.getRegion().end(), {}, {});163    builder.setInsertionPointToEnd(block);164    // These things are close enough to a function handling-wise we can just165    // create this here.166    CIRGenFunction cgf{*this, builder, true};167    llvm::SaveAndRestore<CIRGenFunction *> savedCGF(curCGF, &cgf);168    cgf.curFn = ctorOp;169    CIRGenFunction::SourceLocRAIIObject fnLoc{cgf, exprLoc};170 171    // This gets the information we need, PLUS emits the bounds correctly, so we172    // have to do this in both enter and exit.173    CIRGenFunction::OpenACCDataOperandInfo inf =174        cgf.getOpenACCDataOperandInfo(varOperand);175    auto beforeOp =176        BeforeOpTy::create(builder, exprLoc, inf.varValue, structured, implicit,177                           inf.name, inf.bounds);178    beforeOp.setDataClause(dataClause);179    beforeOp.setModifiers(convertOpenACCModifiers(modifiers));180 181    mlir::acc::DeclareEnterOp::create(182        builder, exprLoc, mlir::acc::DeclareTokenType::get(&getMLIRContext()),183        beforeOp.getResult());184 185    mlir::acc::TerminatorOp::create(builder, exprLoc);186  }187 188  // copyin, create, and device_resident require a destructor, link does not. In189  // the case of the first three, they are all a 'getdeviceptr', followed by the190  // declare_exit, followed by a delete op in the destructor region.191  if (requiresDtor) {192    mlir::OpBuilder::InsertionGuard guardCase(builder);193    auto ctorOp = mlir::acc::GlobalDestructorOp::create(194        builder, exprLoc, (varName + "_acc_dtor").str());195    getModule().push_back(ctorOp);196    mlir::Block *block = builder.createBlock(&ctorOp.getRegion(),197                                             ctorOp.getRegion().end(), {}, {});198    builder.setInsertionPointToEnd(block);199 200    // These things are close enough to a function handling-wise we can just201    // create this here.202    CIRGenFunction cgf{*this, builder, true};203    llvm::SaveAndRestore<CIRGenFunction *> savedCGF(curCGF, &cgf);204    cgf.curFn = ctorOp;205    CIRGenFunction::SourceLocRAIIObject fnLoc{cgf, exprLoc};206 207    CIRGenFunction::OpenACCDataOperandInfo inf =208        cgf.getOpenACCDataOperandInfo(varOperand);209    auto getDevPtr = mlir::acc::GetDevicePtrOp::create(210        builder, exprLoc, inf.varValue, structured, implicit, inf.name,211        inf.bounds);212    getDevPtr.setDataClause(dataClause);213    getDevPtr.setModifiers(convertOpenACCModifiers(modifiers));214 215    mlir::acc::DeclareExitOp::create(builder, exprLoc, /*token=*/mlir::Value{},216                                     getDevPtr.getResult());217    auto deleteOp = mlir::acc::DeleteOp::create(218        builder, exprLoc, getDevPtr, structured, implicit, inf.name, {});219    deleteOp.setDataClause(dataClause);220    deleteOp.setModifiers(convertOpenACCModifiers(modifiers));221    mlir::acc::TerminatorOp::create(builder, exprLoc);222  }223}224namespace {225// This class emits all of the information for a 'declare' at a global/ns/class226// scope. Each clause results in its own acc_ctor and acc_dtor for the variable.227// This class creates those and emits them properly.228// This behavior is unique/special enough from the emission of statement-level229// clauses that it doesn't really make sense to use that clause visitor.230class OpenACCGlobalDeclareClauseEmitter final231    : public OpenACCClauseVisitor<OpenACCGlobalDeclareClauseEmitter> {232  CIRGenModule &cgm;233 234public:235  OpenACCGlobalDeclareClauseEmitter(CIRGenModule &cgm) : cgm(cgm) {}236 237  void VisitClause(const OpenACCClause &clause) {238    llvm_unreachable("Invalid OpenACC clause on global Declare");239  }240 241  void emitClauses(ArrayRef<const OpenACCClause *> clauses) {242    this->VisitClauseList(clauses);243  }244 245  void VisitCopyInClause(const OpenACCCopyInClause &clause) {246    for (const Expr *var : clause.getVarList())247      cgm.emitGlobalOpenACCDeclareDataOperands<mlir::acc::CopyinOp>(248          var, mlir::acc::DataClause::acc_copyin, clause.getModifierList(),249          /*structured=*/true,250          /*implicit=*/false, /*requiresDtor=*/true);251  }252 253  void VisitCreateClause(const OpenACCCreateClause &clause) {254    for (const Expr *var : clause.getVarList())255      cgm.emitGlobalOpenACCDeclareDataOperands<mlir::acc::CreateOp>(256          var, mlir::acc::DataClause::acc_create, clause.getModifierList(),257          /*structured=*/true,258          /*implicit=*/false, /*requiresDtor=*/true);259  }260 261  void VisitDeviceResidentClause(const OpenACCDeviceResidentClause &clause) {262    for (const Expr *var : clause.getVarList())263      cgm.emitGlobalOpenACCDeclareDataOperands<264          mlir::acc::DeclareDeviceResidentOp>(265          var, mlir::acc::DataClause::acc_declare_device_resident, {},266          /*structured=*/true,267          /*implicit=*/false, /*requiresDtor=*/true);268  }269 270  void VisitLinkClause(const OpenACCLinkClause &clause) {271    for (const Expr *var : clause.getVarList())272      cgm.emitGlobalOpenACCDeclareDataOperands<mlir::acc::DeclareLinkOp>(273          var, mlir::acc::DataClause::acc_declare_link, {},274          /*structured=*/true,275          /*implicit=*/false, /*requiresDtor=*/false);276  }277};278} // namespace279 280void CIRGenModule::emitGlobalOpenACCDeclareDecl(const OpenACCDeclareDecl *d) {281  // Declare creates 1 'acc_ctor' and 0-1 'acc_dtor' per clause, since it needs282  // a unique one on a per-variable basis. We can just use a clause emitter to283  // do all the work.284  mlir::OpBuilder::InsertionGuard guardCase(builder);285  OpenACCGlobalDeclareClauseEmitter em{*this};286  em.emitClauses(d->clauses());287}288 289void CIRGenFunction::emitOpenACCRoutine(const OpenACCRoutineDecl &d) {290  getCIRGenModule().errorNYI(d.getSourceRange(), "OpenACC Routine Construct");291}292 293void CIRGenModule::emitGlobalOpenACCRoutineDecl(const OpenACCRoutineDecl *d) {294  errorNYI(d->getSourceRange(), "OpenACC Global Routine Construct");295}296