xref: /llvm-project/flang/lib/Optimizer/CodeGen/CGOps.cpp (revision cd5ee2715e89ad31982f91cc85fc3939977f2f4e)
197d8972cSEric Schweitz //===-- CGOps.cpp -- FIR codegen operations -------------------------------===//
297d8972cSEric Schweitz //
397d8972cSEric Schweitz // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
497d8972cSEric Schweitz // See https://llvm.org/LICENSE.txt for license information.
597d8972cSEric Schweitz // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
697d8972cSEric Schweitz //
797d8972cSEric Schweitz //===----------------------------------------------------------------------===//
897d8972cSEric Schweitz //
997d8972cSEric Schweitz // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
1097d8972cSEric Schweitz //
1197d8972cSEric Schweitz //===----------------------------------------------------------------------===//
1297d8972cSEric Schweitz 
13*cd5ee271SAbid Qadeer #include "flang/Optimizer/CodeGen/CGOps.h"
1497d8972cSEric Schweitz #include "flang/Optimizer/Dialect/FIRDialect.h"
1597d8972cSEric Schweitz #include "flang/Optimizer/Dialect/FIROps.h"
1697d8972cSEric Schweitz #include "flang/Optimizer/Dialect/FIRType.h"
1797d8972cSEric Schweitz 
1897d8972cSEric Schweitz /// FIR codegen dialect constructor.
FIRCodeGenDialect(mlir::MLIRContext * ctx)1997d8972cSEric Schweitz fir::FIRCodeGenDialect::FIRCodeGenDialect(mlir::MLIRContext *ctx)
2097d8972cSEric Schweitz     : mlir::Dialect("fircg", ctx, mlir::TypeID::get<FIRCodeGenDialect>()) {
2197d8972cSEric Schweitz   addOperations<
2297d8972cSEric Schweitz #define GET_OP_LIST
2397d8972cSEric Schweitz #include "flang/Optimizer/CodeGen/CGOps.cpp.inc"
2497d8972cSEric Schweitz       >();
2597d8972cSEric Schweitz }
2697d8972cSEric Schweitz 
2797d8972cSEric Schweitz // anchor the class vtable to this compilation unit
~FIRCodeGenDialect()2897d8972cSEric Schweitz fir::FIRCodeGenDialect::~FIRCodeGenDialect() {
2997d8972cSEric Schweitz   // do nothing
3097d8972cSEric Schweitz }
3197d8972cSEric Schweitz 
3297d8972cSEric Schweitz #define GET_OP_CLASSES
3397d8972cSEric Schweitz #include "flang/Optimizer/CodeGen/CGOps.cpp.inc"
3497d8972cSEric Schweitz 
getOutRank()3597d8972cSEric Schweitz unsigned fir::cg::XEmboxOp::getOutRank() {
36a89b0480SValentin Clement   if (getSlice().empty())
3797d8972cSEric Schweitz     return getRank();
38a89b0480SValentin Clement   auto outRank = fir::SliceOp::getOutputRank(getSlice());
3997d8972cSEric Schweitz   assert(outRank >= 1);
4097d8972cSEric Schweitz   return outRank;
4197d8972cSEric Schweitz }
4297d8972cSEric Schweitz 
getOutRank()4397d8972cSEric Schweitz unsigned fir::cg::XReboxOp::getOutRank() {
44fac349a1SChristian Sigg   if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(
45fac349a1SChristian Sigg           fir::dyn_cast_ptrOrBoxEleTy(getType())))
4697d8972cSEric Schweitz     return seqTy.getDimension();
4797d8972cSEric Schweitz   return 0;
4897d8972cSEric Schweitz }
4997d8972cSEric Schweitz 
getRank()5097d8972cSEric Schweitz unsigned fir::cg::XReboxOp::getRank() {
51fac349a1SChristian Sigg   if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(
52fac349a1SChristian Sigg           fir::dyn_cast_ptrOrBoxEleTy(getBox().getType())))
5397d8972cSEric Schweitz     return seqTy.getDimension();
5497d8972cSEric Schweitz   return 0;
5597d8972cSEric Schweitz }
5697d8972cSEric Schweitz 
getRank()5797d8972cSEric Schweitz unsigned fir::cg::XArrayCoorOp::getRank() {
58a89b0480SValentin Clement   auto memrefTy = getMemref().getType();
59fac349a1SChristian Sigg   if (mlir::isa<fir::BaseBoxType>(memrefTy))
60fac349a1SChristian Sigg     if (auto seqty = mlir::dyn_cast<fir::SequenceType>(
61fac349a1SChristian Sigg             fir::dyn_cast_ptrOrBoxEleTy(memrefTy)))
6297d8972cSEric Schweitz       return seqty.getDimension();
63a89b0480SValentin Clement   return getShape().size();
6497d8972cSEric Schweitz }
65