Skip to content

Commit af8a149

Browse files
authored
[flang][cuda] Add utility function cuf::hasDataAttr (#154422)
1 parent 402109e commit af8a149

File tree

2 files changed

+24
-0
lines changed
  • flang
    • include/flang/Optimizer/Dialect/CUF/Attributes
    • lib/Optimizer/Dialect/CUF/Attributes

2 files changed

+24
-0
lines changed

flang/include/flang/Optimizer/Dialect/CUF/Attributes/CUFAttr.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ namespace llvm {
2020
class StringRef;
2121
}
2222

23+
namespace mlir {
24+
class Operation;
25+
}
26+
2327
#include "flang/Optimizer/Dialect/CUF/Attributes/CUFEnumAttr.h.inc"
2428

2529
#define GET_ATTRDEF_CLASSES
@@ -28,6 +32,7 @@ class StringRef;
2832
namespace cuf {
2933

3034
/// Attribute to mark Fortran entities with the CUDA attribute.
35+
static constexpr llvm::StringRef dataAttrName = "data_attr";
3136
static constexpr llvm::StringRef getDataAttrName() { return "cuf.data_attr"; }
3237
static constexpr llvm::StringRef getProcAttrName() { return "cuf.proc_attr"; }
3338

@@ -101,6 +106,9 @@ getProcAttribute(mlir::MLIRContext *mlirContext,
101106
return {};
102107
}
103108

109+
/// Returns true if the operation has a data attribute with the given value.
110+
bool hasDataAttr(mlir::Operation *op);
111+
104112
} // namespace cuf
105113

106114
#endif // FORTRAN_OPTIMIZER_DIALECT_CUF_CUFATTR_H

flang/lib/Optimizer/Dialect/CUF/Attributes/CUFAttr.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "mlir/IR/BuiltinTypes.h"
1717
#include "mlir/IR/DialectImplementation.h"
1818
#include "mlir/IR/OpDefinition.h"
19+
#include "mlir/IR/Operation.h"
1920
#include "llvm/ADT/TypeSwitch.h"
2021

2122
#include "flang/Optimizer/Dialect/CUF/Attributes/CUFEnumAttr.cpp.inc"
@@ -29,4 +30,19 @@ void CUFDialect::registerAttributes() {
2930
LaunchBoundsAttr, ProcAttributeAttr>();
3031
}
3132

33+
bool hasDataAttr(mlir::Operation *op, cuf::DataAttribute value) {
34+
if (!op)
35+
return false;
36+
37+
cuf::DataAttributeAttr dataAttr =
38+
op->getAttrOfType<cuf::DataAttributeAttr>(cuf::getDataAttrName());
39+
// When the attribute is declared on the operation, it doesn't have a prefix.
40+
if (!dataAttr)
41+
dataAttr = op->getAttrOfType<cuf::DataAttributeAttr>(cuf::dataAttrName);
42+
if (!dataAttr)
43+
return false;
44+
45+
return dataAttr.getValue() == value;
46+
}
47+
3248
} // namespace cuf

0 commit comments

Comments
 (0)