[flang-commits] [flang] ac473bb - [flang][fir] Upstream utility function valueHasFirAttribute()
Eric Schweitz via flang-commits
flang-commits at lists.llvm.org
Fri Feb 26 18:31:55 PST 2021
Author: Eric Schweitz
Date: 2021-02-26T18:31:32-08:00
New Revision: ac473bb2b2add52e51b1c18b5acc7d9bfa7619a9
URL: https://github.com/llvm/llvm-project/commit/ac473bb2b2add52e51b1c18b5acc7d9bfa7619a9
DIFF: https://github.com/llvm/llvm-project/commit/ac473bb2b2add52e51b1c18b5acc7d9bfa7619a9.diff
LOG: [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
Added:
Modified:
flang/include/flang/Optimizer/Dialect/FIROpsSupport.h
flang/lib/Optimizer/Dialect/FIROps.cpp
Removed:
################################################################################
diff --git a/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h b/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h
index 2d9ad28981ab..dcca1ab55ee0 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<mlir::NamedAttribute> 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<fir.box> 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 79b1a33369bf..2f09de6b6a86 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<fir.box> creation.
+ if (value.getType().isa<fir::BoxType>())
+ if (auto definingOp = value.getDefiningOp())
+ if (auto loadOp = mlir::dyn_cast<fir::LoadOp>(definingOp))
+ value = loadOp.memref();
+ // If this is a function argument, look in the argument attributes.
+ if (auto blockArg = value.dyn_cast<mlir::BlockArgument>()) {
+ if (blockArg.getOwner() && blockArg.getOwner()->isEntryBlock())
+ if (auto funcOp =
+ mlir::dyn_cast<mlir::FuncOp>(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<fir::AllocMemOp>(definingOp) ||
+ mlir::isa<AllocaOp>(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<fir::AddrOfOp>(definingOp)) {
+ if (addressOfOp->hasAttr(attributeName))
+ return true;
+ if (auto module = definingOp->getParentOfType<mlir::ModuleOp>())
+ if (auto globalOp =
+ module.lookupSymbol<fir::GlobalOp>(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
More information about the flang-commits
mailing list