22static bool isScalarLikeType(
Type type) {
30 if (!varName.empty()) {
31 auto varNameAttr = acc::VarNameAttr::get(builder.
getContext(), varName);
37struct MemRefPointerLikeModel
38 :
public PointerLikeType::ExternalModel<MemRefPointerLikeModel<T>, T> {
40 return cast<T>(pointer).getElementType();
43 mlir::acc::VariableTypeCategory
46 if (
auto mappableTy = dyn_cast<MappableType>(varType)) {
47 return mappableTy.getTypeCategory(varPtr);
49 auto memrefTy = cast<T>(pointer);
50 if (!memrefTy.hasRank()) {
53 return mlir::acc::VariableTypeCategory::uncategorized;
56 if (memrefTy.getRank() == 0) {
57 if (isScalarLikeType(memrefTy.getElementType())) {
58 return mlir::acc::VariableTypeCategory::scalar;
62 return mlir::acc::VariableTypeCategory::uncategorized;
66 assert(memrefTy.getRank() > 0 &&
"rank expected to be positive");
67 return mlir::acc::VariableTypeCategory::array;
70 mlir::Value genAllocate(Type pointer, OpBuilder &builder, Location loc,
71 StringRef varName, Type varType, Value originalVar,
72 bool &needsFree)
const {
73 auto memrefTy = cast<MemRefType>(pointer);
77 if (memrefTy.hasStaticShape()) {
79 auto allocaOp = memref::AllocaOp::create(builder, loc, memrefTy);
80 attachVarNameAttr(allocaOp, builder, varName);
81 return allocaOp.getResult();
86 if (originalVar && originalVar.
getType() == memrefTy &&
88 SmallVector<Value> dynamicSizes;
89 for (int64_t i = 0; i < memrefTy.getRank(); ++i) {
90 if (memrefTy.isDynamicDim(i)) {
94 memref::DimOp::create(builder, loc, originalVar, indexValue);
95 dynamicSizes.push_back(dimSize);
102 memref::AllocOp::create(builder, loc, memrefTy, dynamicSizes);
103 attachVarNameAttr(allocOp, builder, varName);
104 return allocOp.getResult();
111 bool genFree(Type pointer, OpBuilder &builder, Location loc,
113 Type varType)
const {
116 Value valueToInspect = allocRes ? allocRes : memrefValue;
119 Value currentValue = valueToInspect;
120 Operation *originalAlloc =
nullptr;
124 while (currentValue) {
127 if (isa<memref::AllocOp, memref::AllocaOp>(definingOp)) {
128 originalAlloc = definingOp;
133 if (
auto castOp = dyn_cast<memref::CastOp>(definingOp)) {
134 currentValue = castOp.getSource();
139 if (
auto reinterpretCastOp =
140 dyn_cast<memref::ReinterpretCastOp>(definingOp)) {
141 currentValue = reinterpretCastOp.getSource();
153 if (isa<memref::AllocaOp>(originalAlloc)) {
157 if (isa<memref::AllocOp>(originalAlloc)) {
159 memref::DeallocOp::create(builder, loc, memrefValue);
168 bool genCopy(Type pointer, OpBuilder &builder, Location loc,
172 auto destMemref = dyn_cast_if_present<TypedValue<MemRefType>>(destination);
173 auto srcMemref = dyn_cast_if_present<TypedValue<MemRefType>>(source);
179 if (destMemref && srcMemref &&
180 destMemref.getType().getElementType() ==
181 srcMemref.getType().getElementType() &&
182 destMemref.getType().getShape() == srcMemref.getType().getShape()) {
183 memref::CopyOp::create(builder, loc, srcMemref, destMemref);
190 mlir::Value
genLoad(Type pointer, OpBuilder &builder, Location loc,
192 Type valueType)
const {
197 auto memrefValue = dyn_cast_if_present<TypedValue<MemRefType>>(srcPtr);
201 auto memrefTy = memrefValue.
getType();
204 if (memrefTy.getRank() != 0)
207 return memref::LoadOp::create(builder, loc, memrefValue,
ValueRange{});
210 bool genStore(Type pointer, OpBuilder &builder, Location loc,
216 auto memrefValue = dyn_cast_if_present<TypedValue<MemRefType>>(destPtr);
220 auto memrefTy = memrefValue.getType();
223 if (memrefTy.getRank() != 0)
226 memref::StoreOp::create(builder, loc, valueToStore, memrefValue);
230 Value
genCast(Type, OpBuilder &builder, Location loc, Value value,
231 Type resultType)
const {
232 if (value.
getType() == resultType)
235 if (isa<BaseMemRefType>(value.
getType()) &&
236 isa<BaseMemRefType>(resultType)) {
239 return memref::CastOp::create(builder, loc, resultType, value);
240 if (memref::MemorySpaceCastOp::areCastCompatible(
242 return memref::MemorySpaceCastOp::create(builder, loc, resultType,
249 if (
auto resPtrLike = dyn_cast<PointerLikeType>(resultType))
250 if (!isa<BaseMemRefType>(resPtrLike))
251 if (Value v = resPtrLike.genCast(builder, loc, value, resultType))
253 if (
auto valPtrLike = dyn_cast<PointerLikeType>(value.
getType()))
254 if (!isa<BaseMemRefType>(valPtrLike))
255 if (Value v = valPtrLike.genCast(builder, loc, value, resultType))
261 bool isDeviceAccessible(Type pointer, Value var,
262 SymbolTableCollection *symbolTables)
const {
263 auto memrefTy = cast<T>(pointer);
264 Attribute memSpace = memrefTy.getMemorySpace();
265 return isa_and_nonnull<gpu::AddressSpaceAttr>(memSpace);
268 MemRefType getAsMemRefType(Type pointer, ModuleOp module)
const {
270 return dyn_cast<MemRefType>(pointer);
274struct MemrefAddressOfGlobalModel
275 :
public AddressOfGlobalOpInterface::ExternalModel<
276 MemrefAddressOfGlobalModel, memref::GetGlobalOp> {
277 SymbolRefAttr getSymbol(Operation *op)
const {
278 auto getGlobalOp = cast<memref::GetGlobalOp>(op);
279 return getGlobalOp.getNameAttr();
283struct MemrefGlobalVariableModel
284 :
public GlobalVariableOpInterface::ExternalModel<MemrefGlobalVariableModel,
286 bool isConstant(Operation *op)
const {
287 auto globalOp = cast<memref::GlobalOp>(op);
288 return globalOp.getConstant();
291 bool hasInitializer(Operation *op)
const {
292 auto globalOp = cast<memref::GlobalOp>(op);
293 return globalOp.getInitialValue().has_value();
296 Region *getInitRegion(Operation *op)
const {
301 bool isDeviceAccessible(Operation *op)
const {
302 auto globalOp = cast<memref::GlobalOp>(op);
303 Attribute memSpace = globalOp.getType().getMemorySpace();
304 return isa_and_nonnull<gpu::AddressSpaceAttr>(memSpace);
307 bool isInDeviceMemory(Operation *op)
const {
311 return isDeviceAccessible(op);
314 bool isCompilerGenerated(Operation *op)
const {
return false; }
319 bool hasLocalLinkage(Operation *op)
const {
320 return cast<memref::GlobalOp>(op).getVisibility() !=
321 SymbolTable::Visibility::Public;
332 MemRefType::attachInterface<MemRefPointerLikeModel<MemRefType>>(*ctx);
333 UnrankedMemRefType::attachInterface<
334 MemRefPointerLikeModel<UnrankedMemRefType>>(*ctx);
335 memref::GetGlobalOp::attachInterface<MemrefAddressOfGlobalModel>(*ctx);
336 memref::GlobalOp::attachInterface<MemrefGlobalVariableModel>(*ctx);
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity,...
static void genStore(OpBuilder &builder, Location loc, Value val, Value mem, Value idx)
Generates a store with proper index typing and proper value.
static Value genLoad(OpBuilder &builder, Location loc, Value mem, Value idx)
Generates a load with proper index typing.
MLIRContext * getContext() const
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
T * getOrLoadDialect()
Get (or create) a dialect for the given derived dialect type.
This class helps build Operations.
Operation is the basic unit of execution within MLIR.
void setDiscardableAttr(StringAttr name, Attribute value)
Set a discardable attribute by name.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
bool isIntOrIndexOrFloat() const
Return true if this is an integer (of any signedness), index, or float type.
Type getType() const
Return the type of this value.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
void registerMemRefInterfaces(DialectRegistry ®istry)
Register OpenACC interfaces for MemRef IR.
static constexpr StringLiteral getVarNameAttrName()
Value genCast(OpBuilder &builder, Location loc, Value value, Type dstTy)
Add type casting between arith and index types when needed.
Include the generated interface declarations.
std::conditional_t< std::is_same_v< Ty, mlir::Type >, mlir::Value, detail::TypedValue< Ty > > TypedValue
If Ty is mlir::Type this will select Value instead of having a wrapper around it.