From: Eric Schweitz Date: Thu, 25 Feb 2021 22:16:23 +0000 (-0800) Subject: [flang][fir] Upstream utility function valueHasFirAttribute() X-Git-Tag: llvmorg-14-init~13878 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ac473bb2b2add52e51b1c18b5acc7d9bfa7619a9;p=platform%2Fupstream%2Fllvm.git [flang][fir] Upstream utility function valueHasFirAttribute() This function will be used in subsequent upstreaming merges. Author: Jean Perier Differential Revision: https://reviews.llvm.org/D97502 --- diff --git a/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h b/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h index 2d9ad28..dcca1ab 100644 --- a/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h +++ b/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#ifndef OPTIMIZER_DIALECT_FIROPSSUPPORT_H -#define OPTIMIZER_DIALECT_FIROPSSUPPORT_H +#ifndef FORTRAN_OPTIMIZER_DIALECT_FIROPSSUPPORT_H +#define FORTRAN_OPTIMIZER_DIALECT_FIROPSSUPPORT_H #include "flang/Optimizer/Dialect/FIROps.h" #include "mlir/Dialect/StandardOps/IR/Ops.h" @@ -59,6 +59,21 @@ fir::GlobalOp createGlobalOp(mlir::Location loc, mlir::ModuleOp module, llvm::StringRef name, mlir::Type type, llvm::ArrayRef attrs = {}); +/// Attribute to mark Fortran entities with the CONTIGUOUS attribute. +constexpr llvm::StringRef getContiguousAttrName() { return "fir.contiguous"; } +/// Attribute to mark Fortran entities with the OPTIONAL attribute. +constexpr llvm::StringRef getOptionalAttrName() { return "fir.optional"; } + +/// Tell if \p value is: +/// - a function argument that has attribute \p attributeName +/// - or, the result of fir.alloca/fir.allocamem op that has attribute \p +/// attributeName +/// - or, the result of a fir.address_of of a fir.global that has attribute \p +/// attributeName +/// - or, a fir.box loaded from a fir.ref that matches one of the +/// previous cases. +bool valueHasFirAttribute(mlir::Value value, llvm::StringRef attributeName); + } // namespace fir -#endif // OPTIMIZER_DIALECT_FIROPSSUPPORT_H +#endif // FORTRAN_OPTIMIZER_DIALECT_FIROPSSUPPORT_H diff --git a/flang/lib/Optimizer/Dialect/FIROps.cpp b/flang/lib/Optimizer/Dialect/FIROps.cpp index 79b1a33..2f09de6 100644 --- a/flang/lib/Optimizer/Dialect/FIROps.cpp +++ b/flang/lib/Optimizer/Dialect/FIROps.cpp @@ -1966,6 +1966,47 @@ fir::GlobalOp fir::createGlobalOp(mlir::Location loc, mlir::ModuleOp module, return result; } +bool fir::valueHasFirAttribute(mlir::Value value, + llvm::StringRef attributeName) { + // If this is a fir.box that was loaded, the fir attributes will be on the + // related fir.ref creation. + if (value.getType().isa()) + if (auto definingOp = value.getDefiningOp()) + if (auto loadOp = mlir::dyn_cast(definingOp)) + value = loadOp.memref(); + // If this is a function argument, look in the argument attributes. + if (auto blockArg = value.dyn_cast()) { + if (blockArg.getOwner() && blockArg.getOwner()->isEntryBlock()) + if (auto funcOp = + mlir::dyn_cast(blockArg.getOwner()->getParentOp())) + if (funcOp.getArgAttr(blockArg.getArgNumber(), attributeName)) + return true; + return false; + } + + if (auto definingOp = value.getDefiningOp()) { + // If this is an allocated value, look at the allocation attributes. + if (mlir::isa(definingOp) || + mlir::isa(definingOp)) + return definingOp->hasAttr(attributeName); + // If this is an imported global, look at AddrOfOp and GlobalOp attributes. + // Both operations are looked at because use/host associated variable (the + // AddrOfOp) can have ASYNCHRONOUS/VOLATILE attributes even if the ultimate + // entity (the globalOp) does not have them. + if (auto addressOfOp = mlir::dyn_cast(definingOp)) { + if (addressOfOp->hasAttr(attributeName)) + return true; + if (auto module = definingOp->getParentOfType()) + if (auto globalOp = + module.lookupSymbol(addressOfOp.symbol())) + return globalOp->hasAttr(attributeName); + } + } + // TODO: Construct associated entities attributes. Decide where the fir + // attributes must be placed/looked for in this case. + return false; +} + // Tablegen operators #define GET_OP_CLASSES