[Mlir-commits] [mlir] EmitC: Add emitc.global and emitc.get_global (#145) (PR #88701)
Matthias Gehre
llvmlistbot at llvm.org
Mon Apr 22 05:57:44 PDT 2024
================
@@ -896,3 +889,111 @@ LogicalResult mlir::emitc::OpaqueType::verify(
}
return success();
}
+
+//===----------------------------------------------------------------------===//
+// GlobalOp
+//===----------------------------------------------------------------------===//
+static void printEmitCGlobalOpTypeAndInitialValue(OpAsmPrinter &p, GlobalOp op,
+ TypeAttr type,
+ Attribute initialValue) {
+ p << type;
+ if (initialValue) {
+ p << " = ";
+ p.printAttributeWithoutType(initialValue);
+ }
+}
+
+static Type getInitializerTypeForGlobal(Type type) {
+ if (auto array = llvm::dyn_cast<ArrayType>(type))
+ return RankedTensorType::get(array.getShape(), array.getElementType());
+ return type;
+}
+
+static ParseResult
+parseEmitCGlobalOpTypeAndInitialValue(OpAsmParser &parser, TypeAttr &typeAttr,
+ Attribute &initialValue) {
+ Type type;
+ if (parser.parseType(type))
+ return failure();
+
+ typeAttr = TypeAttr::get(type);
+
+ if (parser.parseOptionalEqual())
+ return success();
+
+ if (parser.parseAttribute(initialValue, getInitializerTypeForGlobal(type)))
+ return failure();
+
+ if (!llvm::isa<ElementsAttr, IntegerAttr, FloatAttr>(initialValue))
+ return parser.emitError(parser.getNameLoc())
+ << "initial value should be a unit, integer, float or elements "
+ "attribute";
+ return success();
+}
+
+LogicalResult GlobalOp::verify() {
+ if (getInitialValue().has_value()) {
+ Attribute initValue = getInitialValue().value();
+ // Check that the type of the initial value is compatible with the type of
+ // the global variable.
+ if (auto elementsAttr = llvm::dyn_cast<ElementsAttr>(initValue)) {
+ auto arrayType = llvm::dyn_cast<ArrayType>(getType());
+ if (!arrayType)
+ return emitOpError("expected array type, but got ") << getType();
+
+ Type initType = elementsAttr.getType();
+ Type tensorType = getInitializerTypeForGlobal(getType());
+ if (initType != tensorType) {
+ return emitOpError("initial value expected to be of type ")
+ << getType() << ", but was of type " << initType;
+ }
+ } else if (auto intAttr = dyn_cast<IntegerAttr>(initValue)) {
+ if (intAttr.getType() != getType()) {
+ return emitOpError("initial value expected to be of type ")
+ << getType() << ", but was of type " << intAttr.getType();
+ }
+ } else if (auto floatAttr = dyn_cast<FloatAttr>(initValue)) {
+ if (floatAttr.getType() != getType()) {
+ return emitOpError("initial value expected to be of type ")
+ << getType() << ", but was of type " << floatAttr.getType();
+ }
+ } else {
+ return emitOpError(
----------------
mgehre-amd wrote:
True, I now allow opaque attributes here.
https://github.com/llvm/llvm-project/pull/88701
More information about the Mlir-commits
mailing list