@@ -124,6 +124,14 @@ void cir::CIRDialect::initialize() {
124124 addInterfaces<CIROpAsmDialectInterface>();
125125}
126126
127+ Operation *cir::CIRDialect::materializeConstant (mlir::OpBuilder &builder,
128+ mlir::Attribute value,
129+ mlir::Type type,
130+ mlir::Location loc) {
131+ return builder.create <mlir::cir::ConstantOp>(
132+ loc, type, mlir::cast<mlir::TypedAttr>(value));
133+ }
134+
127135// ===----------------------------------------------------------------------===//
128136// Helpers
129137// ===----------------------------------------------------------------------===//
@@ -344,7 +352,8 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType,
344352 return success ();
345353 }
346354
347- if (mlir::isa<mlir::cir::IntAttr, mlir::cir::FPAttr>(attrType)) {
355+ if (mlir::isa<mlir::cir::IntAttr, mlir::cir::FPAttr, mlir::cir::ComplexAttr>(
356+ attrType)) {
348357 auto at = cast<TypedAttr>(attrType);
349358 if (at.getType () != opType) {
350359 return op->emitOpError (" result type (" )
@@ -748,6 +757,26 @@ LogicalResult ComplexCreateOp::verify() {
748757 return success ();
749758}
750759
760+ OpFoldResult ComplexCreateOp::fold (FoldAdaptor adaptor) {
761+ auto real = adaptor.getReal ();
762+ auto imag = adaptor.getImag ();
763+
764+ if (!real || !imag)
765+ return nullptr ;
766+
767+ // When both of real and imag are constants, we can fold the operation into an
768+ // `cir.const #cir.complex` operation.
769+
770+ auto realAttr = mlir::cast<mlir::TypedAttr>(real);
771+ auto imagAttr = mlir::cast<mlir::TypedAttr>(imag);
772+ assert (realAttr.getType () == imagAttr.getType () &&
773+ " real part and imag part should be of the same type" );
774+
775+ auto complexTy =
776+ mlir::cir::ComplexType::get (getContext (), realAttr.getType ());
777+ return mlir::cir::ComplexAttr::get (complexTy, realAttr, imagAttr);
778+ }
779+
751780// ===----------------------------------------------------------------------===//
752781// ComplexRealOp and ComplexImagOp
753782// ===----------------------------------------------------------------------===//
@@ -760,6 +789,14 @@ LogicalResult ComplexRealOp::verify() {
760789 return success ();
761790}
762791
792+ OpFoldResult ComplexRealOp::fold (FoldAdaptor adaptor) {
793+ auto input =
794+ mlir::cast_if_present<mlir::cir::ComplexAttr>(adaptor.getOperand ());
795+ if (input)
796+ return input.getReal ();
797+ return nullptr ;
798+ }
799+
763800LogicalResult ComplexImagOp::verify () {
764801 if (getType () != getOperand ().getType ().getElementTy ()) {
765802 emitOpError () << " cir.complex.imag result type does not match operand type" ;
@@ -768,6 +805,14 @@ LogicalResult ComplexImagOp::verify() {
768805 return success ();
769806}
770807
808+ OpFoldResult ComplexImagOp::fold (FoldAdaptor adaptor) {
809+ auto input =
810+ mlir::cast_if_present<mlir::cir::ComplexAttr>(adaptor.getOperand ());
811+ if (input)
812+ return input.getImag ();
813+ return nullptr ;
814+ }
815+
771816// ===----------------------------------------------------------------------===//
772817// ComplexRealPtrOp and ComplexImagPtrOp
773818// ===----------------------------------------------------------------------===//
0 commit comments