// splitting the Standard dialect.
let results = (outs /*SignlessIntegerOrFloatLike*/AnyType:$result);
- let builders = [
- OpBuilder<(ins "Attribute":$value, "Type":$type),
- [{ build($_builder, $_state, type, value); }]>,
- ];
-
let extraClassDeclaration = [{
/// Whether the constant op can be constructed with a particular value and
/// type.
static bool isBuildableWith(Attribute value, Type type);
+
+ /// Build the constant op with `value` and `type` if possible, otherwise
+ /// returns null.
+ static ConstantOp materialize(OpBuilder &builder, Attribute value,
+ Type type, Location loc);
}];
let hasFolder = 1;
Operation *AffineDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
Location loc) {
- return builder.create<arith::ConstantOp>(loc, type, value);
+ return arith::ConstantOp::materialize(builder, value, type, loc);
}
/// A utility function to check if a value is defined at the top level of an
Operation *arith::ArithDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
Location loc) {
- return builder.create<arith::ConstantOp>(loc, value, type);
+ return ConstantOp::materialize(builder, value, type, loc);
}
return value.isa<IntegerAttr, FloatAttr, ElementsAttr>();
}
+ConstantOp arith::ConstantOp::materialize(OpBuilder &builder, Attribute value,
+ Type type, Location loc) {
+ if (isBuildableWith(value, type))
+ return builder.create<arith::ConstantOp>(loc, cast<TypedAttr>(value));
+ return nullptr;
+}
+
OpFoldResult arith::ConstantOp::fold(FoldAdaptor adaptor) { return getValue(); }
void arith::ConstantIntOp::build(OpBuilder &builder, OperationState &result,
return builder.create<complex::ConstantOp>(loc, type,
value.cast<ArrayAttr>());
}
- if (arith::ConstantOp::isBuildableWith(value, type))
- return builder.create<arith::ConstantOp>(loc, type, value);
- return nullptr;
+ return arith::ConstantOp::materialize(builder, value, type, loc);
}
#define GET_ATTRDEF_CLASSES
Operation *LinalgDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
Location loc) {
- return builder.create<arith::ConstantOp>(loc, type, value);
+ return arith::ConstantOp::materialize(builder, value, type, loc);
}
}
// Create a constant scalar value from the splat constant.
- Value scalarConstant = rewriter.create<arith::ConstantOp>(
- def->getLoc(), constantAttr, constantAttr.getType());
+ Value scalarConstant =
+ rewriter.create<arith::ConstantOp>(def->getLoc(), constantAttr);
SmallVector<Value> outputOperands = genericOp.getOutputs();
auto fusedOp = rewriter.create<GenericOp>(
Operation *math::MathDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
Location loc) {
- return builder.create<arith::ConstantOp>(loc, value, type);
+ return arith::ConstantOp::materialize(builder, value, type, loc);
}
Operation *MemRefDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
Location loc) {
- if (arith::ConstantOp::isBuildableWith(value, type))
- return builder.create<arith::ConstantOp>(loc, value, type);
- return nullptr;
+ return arith::ConstantOp::materialize(builder, value, type, loc);
}
//===----------------------------------------------------------------------===//
return builder.create<ConstSizeOp>(loc, type, value.cast<IntegerAttr>());
if (type.isa<WitnessType>())
return builder.create<ConstWitnessOp>(loc, type, value.cast<BoolAttr>());
- if (arith::ConstantOp::isBuildableWith(value, type))
- return builder.create<arith::ConstantOp>(loc, type, value);
- return nullptr;
+ return arith::ConstantOp::materialize(builder, value, type, loc);
}
LogicalResult ShapeDialect::verifyOperationAttribute(Operation *op,
Operation *TensorDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
Location loc) {
- if (arith::ConstantOp::isBuildableWith(value, type))
- return builder.create<arith::ConstantOp>(loc, value, type);
+ if (auto op = arith::ConstantOp::materialize(builder, value, type, loc))
+ return op;
if (complex::ConstantOp::isBuildableWith(value, type))
return builder.create<complex::ConstantOp>(loc, type,
value.cast<ArrayAttr>());
Operation *VectorDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
Location loc) {
- return builder.create<arith::ConstantOp>(loc, type, value);
+ return arith::ConstantOp::materialize(builder, value, type, loc);
}
IntegerType vector::getVectorSubscriptType(Builder &builder) {