================
@@ -62,4 +103,396 @@
OpenACCPointerLikeModel<cir::PointerType>::getPointeeTypeCategory(
return mlir::acc::VariableTypeCategory::uncategorized;
}
+// Incomplete records and types without a data layout have no size.
+static bool hasKnownLayout(mlir::Type type) {
+ if (auto record = mlir::dyn_cast<cir::RecordType>(type))
+ return record.isComplete();
+ if (auto array = mlir::dyn_cast<cir::ArrayType>(type))
+ return hasKnownLayout(array.getElementType());
+ return type.hasTrait<mlir::DataLayoutTypeInterface::Trait>();
+}
+
+// A scalar whose size comes from the data layout.
+static bool isKnownSizeScalar(mlir::Type type) {
+ return hasKnownLayout(type) &&
+ (isAnyIntegerOrFloatingPointType(type) ||
+ mlir::isa<cir::BoolType, cir::PointerType, cir::ComplexType>(type));
+}
+
+static bool isKnownSizeArray(mlir::Type type) {
+ return mlir::isa<cir::ArrayType>(type) && hasKnownLayout(type);
+}
+
+// Alignment comes from the module that contains the insertion point.
+static std::optional<mlir::IntegerAttr>
+allocaAlignment(mlir::OpBuilder &builder, mlir::Type pointee) {
+ mlir::Operation *scope = nullptr;
+ if (mlir::Block *block = builder.getInsertionBlock())
+ scope = block->getParentOp();
+ std::optional<mlir::DataLayout> layout = mlir::acc::getDataLayout(scope);
+ if (!layout)
+ return std::nullopt;
+ uint64_t align = layout->getTypeABIAlignment(pointee);
+ if (align == 0)
+ return std::nullopt;
+ return builder.getI64IntegerAttr(align);
+}
+
+// A dynamic extent is a non-zero constant.
+static std::optional<uint64_t> constantCount(mlir::Value size) {
+ auto constant = size.getDefiningOp<cir::ConstantOp>();
+ if (!constant)
+ return std::nullopt;
+ auto attr = mlir::dyn_cast<cir::IntAttr>(constant.getValue());
+ if (!attr || attr.getUInt() == 0)
+ return std::nullopt;
+ return attr.getUInt();
+}
+
+static mlir::Value constantSizeHere(mlir::OpBuilder &builder,
+ mlir::Location loc, mlir::Value size) {
+ if (!constantCount(size))
+ return {};
+ return cir::ConstantOp::create(
+ builder, loc, size.getDefiningOp<cir::ConstantOp>().getValue());
+}
+
+static cir::PointerType pointerTo(mlir::Type pointee, cir::PointerType like) {
+ if (like.getAddrSpace())
+ return cir::PointerType::get(pointee, like.getAddrSpace());
+ return cir::PointerType::get(pointee);
+}
+
+// `count` is set for a dynamic alloca. A constant count is folded into
+// `!cir.array` when no size operand is requested.
+struct Object {
+ mlir::Type type;
+ mlir::Value count;
+};
+
+struct Resolved {
+ bool sawAlloca = false;
+ std::optional<Object> object;
+};
+
+// The scalar or constant-size array described by `pointer`. `needCount` keeps
+// a dynamic size as a value. Otherwise a constant count is part of the type.
+static Resolved resolveObject(mlir::OpBuilder &builder, mlir::Location loc,
+ mlir::Value pointer, bool needCount) {
+ AllocaInfo found = pointer ? findAlloca(pointer) : AllocaInfo{};
+ if (!found)
+ return {};
+
+ if (!found.baseEntity)
+ return {true, std::nullopt};
+
+ mlir::Type type = found.alloca.getAllocaType();
+ if (!isKnownSizeScalar(type) && !isKnownSizeArray(type))
+ return {true, std::nullopt};
+ if (!found.alloca.isDynamic())
+ return {true, Object{type, {}}};
+
+ std::optional<uint64_t> count =
constantCount(found.alloca.getDynAllocSize());
+ if (!count)
+ return {true, std::nullopt};
+ if (!needCount)
+ return {true, Object{cir::ArrayType::get(type, *count), {}}};
+
+ mlir::Value size =
+ constantSizeHere(builder, loc, found.alloca.getDynAllocSize());
+ if (!size)
+ return {true, std::nullopt};
+ return {true, Object{type, size}};
+}
+
+// The scalar or constant-size array described by `pointer`. Without an
+// alloca, `fallback` is that object when its size is known.
+static std::optional<Object>
+knownSizeObject(mlir::OpBuilder &builder, mlir::Location loc,
+ mlir::Value pointer, mlir::Type fallback, bool needCount) {
+ Resolved resolved = resolveObject(builder, loc, pointer, needCount);
+ if (resolved.sawAlloca && !resolved.object)
+ return std::nullopt;
+ if (resolved.object)
+ return resolved.object;
+ if (fallback && (isKnownSizeScalar(fallback) || isKnownSizeArray(fallback)))
+ return Object{fallback, {}};
+ return std::nullopt;
+}
+
+// Pointer-to-pointer conversions, and integer-to-pointer conversions. A
+// pointer bitcast is used when the pointee changes, then an address-space
+// cast when the address space changes. Value-to-value conversions are left
+// to the caller.
+static mlir::Value castPointer(mlir::OpBuilder &builder, mlir::Location loc,
+ mlir::Value value, mlir::Type resultType) {
+ mlir::Type srcType = value.getType();
+ if (srcType == resultType)
+ return value;
+
+ auto srcPtr = mlir::dyn_cast<cir::PointerType>(srcType);
+ auto dstPtr = mlir::dyn_cast<cir::PointerType>(resultType);
+ if (!dstPtr)
+ return {};
+ if (!srcPtr) {
+ if (!mlir::isa<cir::IntType>(srcType))
+ return {};
+ return cir::CastOp::create(builder, loc, resultType,
+ cir::CastKind::int_to_ptr, value);
+ }
+
+ mlir::Value current = value;
+ if (srcPtr.getPointee() != dstPtr.getPointee()) {
+ auto midTy =
+ cir::PointerType::get(dstPtr.getPointee(), srcPtr.getAddrSpace());
+ if (current.getType() != midTy)
+ current = cir::CastOp::create(builder, loc, midTy,
cir::CastKind::bitcast,
+ current);
+ }
+ if (current.getType() != resultType)
+ current = cir::CastOp::create(builder, loc, resultType,
+ cir::CastKind::address_space, current);
+ return current;
+}
+
+// A memref element is a builtin scalar, complex, or fixed vector. CIR signs
+// are dropped: memref storage is signless.
+static mlir::Type convertMemRefElementType(mlir::Type type,
+ const mlir::DataLayout &layout) {
+ if (auto intTy = mlir::dyn_cast<cir::IntType>(type)) {
+ if (intTy.isBitInt() || intTy.getWidth() == 0)
+ return {};
+ return mlir::IntegerType::get(type.getContext(), intTy.getWidth());
+ }
+ if (mlir::isa<cir::BoolType>(type)) {
+ llvm::TypeSize bits = layout.getTypeSizeInBits(type);
+ if (!bits.isFixed() || !bits.getFixedValue())
+ return {};
+ return mlir::IntegerType::get(type.getContext(),
+ static_cast<unsigned>(bits.getFixedValue()));
+ }
+ if (mlir::isa<cir::SingleType>(type))
+ return mlir::Float32Type::get(type.getContext());
+ if (mlir::isa<cir::DoubleType>(type))
+ return mlir::Float64Type::get(type.getContext());
+ if (mlir::isa<cir::FP16Type>(type))
+ return mlir::Float16Type::get(type.getContext());
+ if (mlir::isa<cir::BF16Type>(type))
+ return mlir::BFloat16Type::get(type.getContext());
+ if (mlir::isa<cir::FP80Type>(type))
+ return mlir::Float80Type::get(type.getContext());
+ if (mlir::isa<cir::FP128Type>(type))
+ return mlir::Float128Type::get(type.getContext());
+ if (auto longDouble = mlir::dyn_cast<cir::LongDoubleType>(type))
+ return convertMemRefElementType(longDouble.getUnderlying(), layout);
+ if (auto complex = mlir::dyn_cast<cir::ComplexType>(type)) {
+ mlir::Type element =
+ convertMemRefElementType(complex.getElementType(), layout);
+ if (!element)
+ return {};
+ return mlir::ComplexType::get(element);
+ }
+ if (auto vector = mlir::dyn_cast<cir::VectorType>(type)) {
+ if (vector.isScalable())
+ return {};
+ mlir::Type element =
+ convertMemRefElementType(vector.getElementType(), layout);
+ if (!element || !element.isIntOrIndexOrFloat())
+ return {};
+ return mlir::VectorType::get({static_cast<int64_t>(vector.getFixedSize())},
+ element);
+ }
+ return {};
+}
+
+// `shape` is outermost first, matching `!cir.array` nesting.
+static bool collectMemRefShape(mlir::Type type,
+ llvm::SmallVectorImpl<int64_t> &shape,
+ mlir::Type &element,
+ const mlir::DataLayout &layout) {
+ if (auto array = mlir::dyn_cast<cir::ArrayType>(type)) {
+ shape.push_back(static_cast<int64_t>(array.getSize()));
+ return collectMemRefShape(array.getElementType(), shape, element, layout);
+ }
+ if (mlir::isa<cir::PointerType>(type))
+ return false;
+ element = convertMemRefElementType(type, layout);
+ return static_cast<bool>(element) &&
+ mlir::BaseMemRefType::isValidElementType(element);
+}
+
+static mlir::MemRefType
+convertPointerToMemRefType(mlir::Type pointer, const mlir::DataLayout &layout)
{
+ auto ptrTy = mlir::dyn_cast<cir::PointerType>(pointer);
+ if (!ptrTy)
+ return {};
+
+ mlir::Type pointee = ptrTy.getPointee();
+ if (!hasKnownLayout(pointee))
+ return {};
+
+ llvm::SmallVector<int64_t> shape;
+ mlir::Type element;
+ if (!collectMemRefShape(pointee, shape, element, layout))
+ return {};
+ return mlir::MemRefType::get(shape, element);
+}
+
+template <>
+mlir::Value OpenACCPointerLikeModel<cir::PointerType>::genAllocate(
----------------
razvanlupusoru wrote:
Yes, I saw this too. The API probably needs to change the way `MappableType`
does, taking both the block argument and the host variable. I have not made
that change here, because the right shape needs more thought.
So in this PR I focused on strategies to recover variable info; the walk
handles the case where the type does not carry enough information, as long as
the value still has the operations that formed it. The gap is that recipe
generation passes the block argument, which lacks those operations, and the
host variable that does have them is outside the region. The API would have to
pass that information in.
https://github.com/llvm/llvm-project/pull/244703
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits