Skip to content

Commit a1c48ae

Browse files
committed
Split cir.complex.extract into cir.complex.real and cir.complex.imag
1 parent 84fd158 commit a1c48ae

File tree

5 files changed

+54
-35
lines changed

5 files changed

+54
-35
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

+32-23
Original file line numberDiff line numberDiff line change
@@ -993,45 +993,54 @@ def CmpOp : CIR_Op<"cmp", [Pure, SameTypeOperands]> {
993993
}
994994

995995
//===----------------------------------------------------------------------===//
996-
// ComplexExtractOp
996+
// ComplexRealOp and ComplexImagOp
997997
//===----------------------------------------------------------------------===//
998998

999-
def ComplexComponent_Real : I32EnumAttrCase<"real", 1>;
1000-
def ComplexComponent_Imag : I32EnumAttrCase<"imag", 2>;
999+
def ComplexRealOp : CIR_Op<"complex.real", [Pure]> {
1000+
let summary = "Extract the real part of a complex value";
1001+
let description = [{
1002+
`cir.complex.real` operation takes an operand of complex type and returns
1003+
the real part of it.
10011004

1002-
def ComplexComponent : I32EnumAttr<
1003-
"ComplexComponent", "complex number component",
1004-
[ComplexComponent_Real, ComplexComponent_Imag]> {
1005-
let cppNamespace = "::mlir::cir";
1005+
Example:
1006+
1007+
```mlir
1008+
!complex = !cir.complex<!cir.float>
1009+
%0 = cir.const(#cir.complex<#cir.fp<1.0>, #cir.fp<2.0>> : !complex) : !complex
1010+
%1 = cir.complex.real(%0 : !complex) : !cir.float
1011+
```
1012+
}];
1013+
1014+
let results = (outs CIR_AnyType:$result);
1015+
let arguments = (ins CIR_ComplexType:$operand);
1016+
1017+
let assemblyFormat = [{
1018+
`(` $operand `:` qualified(type($operand)) `)` `:` type($result) attr-dict
1019+
}];
1020+
1021+
let hasVerifier = 1;
10061022
}
10071023

1008-
def ComplexExtractOp : CIR_Op<"complex.extract", [Pure]> {
1009-
let summary = "Extract a specified part of a complex number.";
1024+
def ComplexImagOp : CIR_Op<"complex.imag", [Pure]> {
1025+
let summary = "Extract the imaginary part of a complex value";
10101026
let description = [{
1011-
`cir.complex.extract` operation reads a component of the operand complex
1012-
number value.
1013-
1014-
The parameter `$component` specifies which component to extract. If it is
1015-
`real`, the real part of the complex number will be extracted. If it is
1016-
`imag`, the imaginary part of the complex number will be extracted.
1027+
`cir.complex.imag` operation takes an operand of complex type and returns
1028+
the imaginary part of it.
10171029

10181030
Example:
10191031

10201032
```mlir
1021-
!fcmp = !cir.complex<!cir.float>
1022-
%0 = cir.const(#cir.complex<#cir.fp<1.0>, #cir.fp<2.0>> : !fcmp) : !fcmp
1023-
%1 = cir.complex.extract(real, %0 : !fcmp) : !cir.float
1024-
%2 = cir.complex.extract(imag, %0 : !fcmp) : !cir.float
1033+
!complex = !cir.complex<!cir.float>
1034+
%0 = cir.const(#cir.complex<#cir.fp<1.0>, #cir.fp<2.0>> : !complex) : !complex
1035+
%1 = cir.complex.imag(%0 : !complex) : !cir.float
10251036
```
10261037
}];
10271038

10281039
let results = (outs CIR_AnyType:$result);
1029-
let arguments = (ins Arg<ComplexComponent, "component to extract">:$component,
1030-
CIR_ComplexType:$operand);
1040+
let arguments = (ins CIR_ComplexType:$operand);
10311041

10321042
let assemblyFormat = [{
1033-
`(` $component `,` $operand `:` qualified(type($operand)) `)`
1034-
`:` type($result) attr-dict
1043+
`(` $operand `:` qualified(type($operand)) `)` `:` type($result) attr-dict
10351044
}];
10361045

10371046
let hasVerifier = 1;

clang/lib/CIR/CodeGen/CIRGenBuilder.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -845,13 +845,16 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
845845
return createNot(createPtrToBoolCast(ptr));
846846
}
847847

848-
mlir::Value createComplexExtract(mlir::Location loc,
849-
mlir::cir::ComplexComponent component,
850-
mlir::Value operand) {
848+
mlir::Value createComplexReal(mlir::Location loc, mlir::Value operand) {
851849
auto operandComplexTy = operand.getType().cast<mlir::cir::ComplexType>();
852850
auto resultTy = operandComplexTy.getElementTy();
853-
return create<mlir::cir::ComplexExtractOp>(loc, resultTy, component,
854-
operand);
851+
return create<mlir::cir::ComplexRealOp>(loc, resultTy, operand);
852+
}
853+
854+
mlir::Value createComplexImag(mlir::Location loc, mlir::Value operand) {
855+
auto operandComplexTy = operand.getType().cast<mlir::cir::ComplexType>();
856+
auto resultTy = operandComplexTy.getElementTy();
857+
return create<mlir::cir::ComplexImagOp>(loc, resultTy, operand);
855858
}
856859

857860
mlir::Value createComplexIsZero(mlir::Location loc, mlir::Value operand) {

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1659,8 +1659,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
16591659
case CK_FloatingComplexToReal:
16601660
case CK_IntegralComplexToReal: {
16611661
auto Src = Visit(const_cast<Expr *>(E));
1662-
return Builder.createComplexExtract(CGF.getLoc(CE->getSourceRange()),
1663-
mlir::cir::ComplexComponent::real, Src);
1662+
return Builder.createComplexReal(CGF.getLoc(CE->getSourceRange()), Src);
16641663
}
16651664
case CK_FloatingComplexToBoolean:
16661665
case CK_IntegralComplexToBoolean: {

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,21 @@ LogicalResult CastOp::verify() {
523523
}
524524

525525
//===----------------------------------------------------------------------===//
526-
// ComplexExtractOp
526+
// ComplexRealOp and ComplexImagOp
527527
//===----------------------------------------------------------------------===//
528528

529-
LogicalResult ComplexExtractOp::verify() {
529+
LogicalResult ComplexRealOp::verify() {
530530
if (getType() != getOperand().getType().getElementTy()) {
531-
emitOpError()
532-
<< "cir.complex.extract result type does not match operand type";
531+
emitOpError() << "cir.complex.real result type does not match operand type";
532+
return failure();
533+
}
534+
535+
return success();
536+
}
537+
538+
LogicalResult ComplexImagOp::verify() {
539+
if (getType() != getOperand().getType().getElementTy()) {
540+
emitOpError() << "cir.complex.imag result type does not match operand type";
533541
return failure();
534542
}
535543

clang/test/CIR/CodeGen/complex.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ float complex_to_element(float _Complex x) {
5454
}
5555

5656
// CHECK: cir.func @complex_to_element(%{{.+}}: !cir.complex<!cir.float> loc({{.+}})) -> !cir.float
57-
// CHECK: %{{.+}} = cir.complex.extract(real, %{{.+}} : !cir.complex<!cir.float>) : !cir.float
57+
// CHECK: %{{.+}} = cir.complex.real(%{{.+}} : !cir.complex<!cir.float>) : !cir.float
5858
// CHECK: }
5959

6060
_Bool complex_to_bool(float _Complex x) {

0 commit comments

Comments
 (0)