diff --git a/flang/include/flang/Optimizer/Dialect/CMakeLists.txt b/flang/include/flang/Optimizer/Dialect/CMakeLists.txt --- a/flang/include/flang/Optimizer/Dialect/CMakeLists.txt +++ b/flang/include/flang/Optimizer/Dialect/CMakeLists.txt @@ -12,6 +12,7 @@ mlir_tablegen(FIROps.cpp.inc -gen-op-defs) mlir_tablegen(FIROpsTypes.h.inc --gen-typedef-decls) mlir_tablegen(FIROpsTypes.cpp.inc --gen-typedef-defs) +add_mlir_interface(FortranVariableInterface) add_public_tablegen_target(FIROpsIncGen) set(LLVM_TARGET_DEFINITIONS CanonicalizationPatterns.td) diff --git a/flang/include/flang/Optimizer/Dialect/FortranVariableInterface.h b/flang/include/flang/Optimizer/Dialect/FortranVariableInterface.h new file mode 100644 --- /dev/null +++ b/flang/include/flang/Optimizer/Dialect/FortranVariableInterface.h @@ -0,0 +1,26 @@ +//===- FortranVariableInterface.h -------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file contains a set of interfaces for operations defining Fortran +// variables. +// +//===----------------------------------------------------------------------===// + +#ifndef FORTRAN_OPTIMIZER_DIALECT_FORTRANVARIABLEINTERFACE_H +#define FORTRAN_OPTIMIZER_DIALECT_FORTRANVARIABLEINTERFACE_H + +#include "flang/Optimizer/Dialect/FIRAttr.h" +#include "flang/Optimizer/Dialect/FIRType.h" +#include "mlir/IR/BuiltinTypes.h" +#include "mlir/IR/OpDefinition.h" + +namespace fir { +#include "flang/Optimizer/Dialect/FortranVariableInterface.h.inc" +} // namespace fir + +#endif // FORTRAN_OPTIMIZER_DIALECT_FORTRANVARIABLEINTERFACE_H diff --git a/flang/include/flang/Optimizer/Dialect/FortranVariableInterface.td b/flang/include/flang/Optimizer/Dialect/FortranVariableInterface.td new file mode 100644 --- /dev/null +++ b/flang/include/flang/Optimizer/Dialect/FortranVariableInterface.td @@ -0,0 +1,124 @@ +//===- FortranVariableInterface.td -------------------------*- tablegen -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file defines an interface for operations defining Fortran variables. +// +//===----------------------------------------------------------------------===// + +#ifndef FORTRANVARIABLEINTERFACE +#define FORTRANVARIABLEINTERFACE + +include "mlir/IR/OpBase.td" + + +def FortranVariableOpInterface : OpInterface<"FortranVariableOpInterface"> { + let description = [{ + Interface for operations that create Fortran like variables in order to + query about all their Fortran properties. + }]; + + let methods = [ + InterfaceMethod< + /*desc=*/"Get the address produced by the definition", + /*retTy=*/"mlir::Value", + /*methodName=*/"getBase", + /*args=*/(ins), + /*methodBody=*/[{}], + /*defaultImplementation=*/[{ + ConcreteOp op = mlir::cast(this->getOperation()); + return op.getResult(); + }] + >, + InterfaceMethod< + /*desc=*/"Get Fortran attributes", + /*retTy=*/"llvm::Optional", + /*methodName=*/"getFortranAttrs", + /*args=*/(ins), + /*methodBody=*/[{}], + /*defaultImplementation=*/[{ + ConcreteOp op = mlir::cast(this->getOperation()); + return op.getFortran_attrs(); + }] + >, + InterfaceMethod< + /*desc=*/"Get the shape of the variable", + /*retTy=*/"llvm::Optional", + /*methodName=*/"getShape", + /*args=*/(ins), + /*methodBody=*/[{}], + /*defaultImplementation=*/[{ + ConcreteOp op = mlir::cast(this->getOperation()); + return op.getShape(); + }] + >, + InterfaceMethod< + /*desc=*/"Get explicit type parameters of the variable", + /*retTy=*/"mlir::OperandRange", + /*methodName=*/"getExplicitTypeParams", + /*args=*/(ins), + /*methodBody=*/[{}], + /*defaultImplementation=*/[{ + ConcreteOp op = mlir::cast(this->getOperation()); + return op.getTypeparams(); + }] + >, + ]; + + let extraClassDeclaration = [{ + + /// Get the sequence type or scalar value type corresponding to this + /// variable. + mlir::Type getElementOrSequenceType() { + return fir::unwrapPassByRefType(getBase().getType()); + } + + /// Get the scalar value type corresponding to this variable. + mlir::Type getElementType() { + return fir::unwrapSequenceType(getElementOrSequenceType()); + } + + /// Is the variable an array ? + bool isArray() { + return getElementOrSequenceType().isa(); + } + + /// Is this variable a Fortran pointer ? + bool isPointer() { + auto attrs = getFortranAttrs(); + return attrs && bitEnumContainsAny(*attrs, + fir::FortranVariableFlagsEnum::pointer); + } + + /// Is this variable a Fortran allocatable ? + bool isAllocatable() { + auto attrs = getFortranAttrs(); + return attrs && bitEnumContainsAny(*attrs, + fir::FortranVariableFlagsEnum::allocatable); + } + + /// Is this a Fortran character variable ? + bool isCharacter() { + return getElementType().isa(); + } + + /// Is this a Fortran character variable with an explicit length ? + bool hasExplicitCharLen() { + return isCharacter() && !getExplicitTypeParams().empty(); + } + + /// Return the length of explicit length character variable. + mlir::Value getExplicitCharLen() { + assert(hasExplicitCharLen() && "must be an explicit length character"); + return getExplicitTypeParams()[0]; + } + + }]; + +} + +#endif // FORTRANVARIABLEINTERFACE diff --git a/flang/lib/Optimizer/Dialect/CMakeLists.txt b/flang/lib/Optimizer/Dialect/CMakeLists.txt --- a/flang/lib/Optimizer/Dialect/CMakeLists.txt +++ b/flang/lib/Optimizer/Dialect/CMakeLists.txt @@ -3,6 +3,7 @@ FIRDialect.cpp FIROps.cpp FIRType.cpp + FortranVariableInterface.cpp Inliner.cpp DEPENDS diff --git a/flang/lib/Optimizer/Dialect/FortranVariableInterface.cpp b/flang/lib/Optimizer/Dialect/FortranVariableInterface.cpp new file mode 100644 --- /dev/null +++ b/flang/lib/Optimizer/Dialect/FortranVariableInterface.cpp @@ -0,0 +1,17 @@ +//===-- FortranVariableInterface.cpp.cpp ----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ +// +//===----------------------------------------------------------------------===// + +#include "flang/Optimizer/Dialect/FortranVariableInterface.h" + +namespace fir { +#include "flang/Optimizer/Dialect/FortranVariableInterface.cpp.inc" +}