Skip to content

Commit 0066733

Browse files
committed
[CIR] Refactor complex type
Apply `CIR_AnyIntOrFloat` type constraint on element type and rename `elementTy` to `elementType`.
1 parent aeabdc6 commit 0066733

File tree

12 files changed

+86
-64
lines changed

12 files changed

+86
-64
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,12 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
311311

312312
mlir::Value createComplexReal(mlir::Location loc, mlir::Value operand) {
313313
auto operandTy = mlir::cast<cir::ComplexType>(operand.getType());
314-
return create<cir::ComplexRealOp>(loc, operandTy.getElementTy(), operand);
314+
return create<cir::ComplexRealOp>(loc, operandTy.getElementType(), operand);
315315
}
316316

317317
mlir::Value createComplexImag(mlir::Location loc, mlir::Value operand) {
318318
auto operandTy = mlir::cast<cir::ComplexType>(operand.getType());
319-
return create<cir::ComplexImagOp>(loc, operandTy.getElementTy(), operand);
319+
return create<cir::ComplexImagOp>(loc, operandTy.getElementType(), operand);
320320
}
321321

322322
mlir::Value createComplexBinOp(mlir::Location loc, mlir::Value lhs,

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,10 @@ def ComplexCreateOp : CIR_Op<"complex.create", [Pure, SameTypeOperands]> {
13861386
}];
13871387

13881388
let results = (outs CIR_ComplexType:$result);
1389-
let arguments = (ins CIR_AnyIntOrFloat:$real, CIR_AnyIntOrFloat:$imag);
1389+
let arguments = (ins
1390+
CIR_AnyIntOrFloatType:$real,
1391+
CIR_AnyIntOrFloatType:$imag
1392+
);
13901393

13911394
let assemblyFormat = [{
13921395
$real `,` $imag
@@ -1414,7 +1417,7 @@ def ComplexRealOp : CIR_Op<"complex.real", [Pure]> {
14141417
```
14151418
}];
14161419

1417-
let results = (outs CIR_AnyIntOrFloat:$result);
1420+
let results = (outs CIR_AnyIntOrFloatType:$result);
14181421
let arguments = (ins CIR_ComplexType:$operand);
14191422

14201423
let assemblyFormat = [{
@@ -1439,7 +1442,7 @@ def ComplexImagOp : CIR_Op<"complex.imag", [Pure]> {
14391442
```
14401443
}];
14411444

1442-
let results = (outs CIR_AnyIntOrFloat:$result);
1445+
let results = (outs CIR_AnyIntOrFloatType:$result);
14431446
let arguments = (ins CIR_ComplexType:$operand);
14441447

14451448
let assemblyFormat = [{
@@ -5564,9 +5567,9 @@ def AtomicFetch : CIR_Op<"atomic.fetch",
55645567
%res = cir.atomic.fetch(add, %ptr : !cir.ptr<!s32i>,
55655568
%val : !s32i, seq_cst) : !s32i
55665569
}];
5567-
let results = (outs CIR_AnyIntOrFloat:$result);
5570+
let results = (outs CIR_AnyIntOrFloatType:$result);
55685571
let arguments = (ins Arg<PrimitiveIntOrFPPtr, "", [MemRead, MemWrite]>:$ptr,
5569-
CIR_AnyIntOrFloat:$val,
5572+
CIR_AnyIntOrFloatType:$val,
55705573
AtomicFetchKind:$binop,
55715574
Arg<MemOrder, "memory order">:$mem_order,
55725575
UnitAttr:$is_volatile,

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ def CIR_AnyFloatType : AnyTypeOf<[
135135
let cppFunctionName = "isAnyFloatingPointType";
136136
}
137137

138-
def CIR_AnyIntOrFloat : AnyTypeOf<[CIR_AnyFloatType, CIR_AnyIntType]>;
138+
def CIR_AnyIntOrFloatType : AnyTypeOf<[CIR_AnyFloatType, CIR_AnyIntType],
139+
"integer or floating point type"
140+
> {
141+
let cppFunctionName = "isAnyIntegerOrFloatingPointType";
142+
}
143+
144+
//===----------------------------------------------------------------------===//
145+
// Complex Type predicates
146+
//===----------------------------------------------------------------------===//
147+
148+
def CIR_AnyComplexType : CIR_TypeBase<"::cir::ComplexType", "complex type">;
139149

140150
#endif // CLANG_CIR_DIALECT_IR_CIRTYPECONSTRAINTS_TD

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

+18-7
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,35 @@ def CIR_ComplexType : CIR_Type<"Complex", "complex",
159159
CIR type that represents a C complex number. `cir.complex` models the C type
160160
`T _Complex`.
161161

162-
The parameter `elementTy` gives the type of the real and imaginary part of
163-
the complex number. `elementTy` must be either a CIR integer type or a CIR
162+
The type models complex values, per C99 6.2.5p11. It supports the C99
163+
complex float types as well as the GCC integer complex extensions.
164+
165+
The parameter `elementType` gives the type of the real and imaginary part of
166+
the complex number. `elementType` must be either a CIR integer type or a CIR
164167
floating-point type.
165168
}];
166169

167-
let parameters = (ins "mlir::Type":$elementTy);
170+
let parameters = (ins CIR_AnyIntOrFloatType:$elementType);
168171

169172
let builders = [
170-
TypeBuilderWithInferredContext<(ins "mlir::Type":$elementTy), [{
171-
return $_get(elementTy.getContext(), elementTy);
173+
TypeBuilderWithInferredContext<(ins "mlir::Type":$elementType), [{
174+
return $_get(elementType.getContext(), elementType);
172175
}]>,
173176
];
174177

175178
let assemblyFormat = [{
176-
`<` $elementTy `>`
179+
`<` $elementType `>`
177180
}];
178181

179-
let genVerifyDecl = 1;
182+
let extraClassDeclaration = [{
183+
bool isFloatingPointComplex() const {
184+
return isAnyFloatingPointType(getElementType());
185+
}
186+
187+
bool isIntegralComplex() const {
188+
return mlir::isa<cir::IntType>(getElementType());
189+
}
190+
}];
180191
}
181192

182193
//===----------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/CIRGenBuilder.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
816816
auto srcPtrTy = mlir::cast<cir::PointerType>(value.getType());
817817
auto srcComplexTy = mlir::cast<cir::ComplexType>(srcPtrTy.getPointee());
818818
return create<cir::ComplexRealPtrOp>(
819-
loc, getPointerTo(srcComplexTy.getElementTy()), value);
819+
loc, getPointerTo(srcComplexTy.getElementType()), value);
820820
}
821821

822822
Address createRealPtr(mlir::Location loc, Address addr) {
@@ -830,7 +830,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
830830
auto srcPtrTy = mlir::cast<cir::PointerType>(value.getType());
831831
auto srcComplexTy = mlir::cast<cir::ComplexType>(srcPtrTy.getPointee());
832832
return create<cir::ComplexImagPtrOp>(
833-
loc, getPointerTo(srcComplexTy.getElementTy()), value);
833+
loc, getPointerTo(srcComplexTy.getElementType()), value);
834834
}
835835

836836
Address createImagPtr(mlir::Location loc, Address addr) {

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ mlir::Value
831831
ComplexExprEmitter::VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
832832
auto Loc = CGF.getLoc(IL->getExprLoc());
833833
auto Ty = mlir::cast<cir::ComplexType>(CGF.convertType(IL->getType()));
834-
auto ElementTy = Ty.getElementTy();
834+
auto ElementTy = Ty.getElementType();
835835

836836
mlir::TypedAttr RealValueAttr;
837837
mlir::TypedAttr ImagValueAttr;
@@ -876,7 +876,7 @@ mlir::Value CIRGenFunction::emitPromotedComplexExpr(const Expr *E,
876876
mlir::Value CIRGenFunction::emitPromotedValue(mlir::Value result,
877877
QualType PromotionType) {
878878
assert(mlir::isa<cir::CIRFPTypeInterface>(
879-
mlir::cast<cir::ComplexType>(result.getType()).getElementTy()) &&
879+
mlir::cast<cir::ComplexType>(result.getType()).getElementType()) &&
880880
"integral complex will never be promoted");
881881
return builder.createCast(cir::CastKind::float_complex, result,
882882
convertType(PromotionType));
@@ -885,7 +885,7 @@ mlir::Value CIRGenFunction::emitPromotedValue(mlir::Value result,
885885
mlir::Value CIRGenFunction::emitUnPromotedValue(mlir::Value result,
886886
QualType UnPromotionType) {
887887
assert(mlir::isa<cir::CIRFPTypeInterface>(
888-
mlir::cast<cir::ComplexType>(result.getType()).getElementTy()) &&
888+
mlir::cast<cir::ComplexType>(result.getType()).getElementType()) &&
889889
"integral complex will never be promoted");
890890
return builder.createCast(cir::CastKind::float_complex, result,
891891
convertType(UnPromotionType));

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,12 @@ LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
357357
LogicalResult ComplexAttr::verify(function_ref<InFlightDiagnostic()> emitError,
358358
cir::ComplexType type, mlir::TypedAttr real,
359359
mlir::TypedAttr imag) {
360-
auto elemTy = type.getElementTy();
361-
if (real.getType() != elemTy) {
360+
auto elemType = type.getElementType();
361+
if (real.getType() != elemType) {
362362
emitError() << "type of the real part does not match the complex type";
363363
return failure();
364364
}
365-
if (imag.getType() != elemTy) {
365+
if (imag.getType() != elemType) {
366366
emitError() << "type of the imaginary part does not match the complex type";
367367
return failure();
368368
}

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

+24-19
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ LogicalResult cir::CastOp::verify() {
655655
auto resComplexTy = mlir::dyn_cast<cir::ComplexType>(resType);
656656
if (!resComplexTy)
657657
return emitOpError() << "requires !cir.complex type for result";
658-
if (srcType != resComplexTy.getElementTy())
658+
if (srcType != resComplexTy.getElementType())
659659
return emitOpError() << "requires source type match result element type";
660660
return success();
661661
}
@@ -665,7 +665,7 @@ LogicalResult cir::CastOp::verify() {
665665
auto resComplexTy = mlir::dyn_cast<cir::ComplexType>(resType);
666666
if (!resComplexTy)
667667
return emitOpError() << "requires !cir.complex type for result";
668-
if (srcType != resComplexTy.getElementTy())
668+
if (srcType != resComplexTy.getElementType())
669669
return emitOpError() << "requires source type match result element type";
670670
return success();
671671
}
@@ -675,7 +675,7 @@ LogicalResult cir::CastOp::verify() {
675675
return emitOpError() << "requires !cir.complex type for source";
676676
if (!mlir::isa<cir::CIRFPTypeInterface>(resType))
677677
return emitOpError() << "requires !cir.float type for result";
678-
if (srcComplexTy.getElementTy() != resType)
678+
if (srcComplexTy.getElementType() != resType)
679679
return emitOpError() << "requires source element type match result type";
680680
return success();
681681
}
@@ -685,14 +685,14 @@ LogicalResult cir::CastOp::verify() {
685685
return emitOpError() << "requires !cir.complex type for source";
686686
if (!mlir::isa<cir::IntType>(resType))
687687
return emitOpError() << "requires !cir.int type for result";
688-
if (srcComplexTy.getElementTy() != resType)
688+
if (srcComplexTy.getElementType() != resType)
689689
return emitOpError() << "requires source element type match result type";
690690
return success();
691691
}
692692
case cir::CastKind::float_complex_to_bool: {
693693
auto srcComplexTy = mlir::dyn_cast<cir::ComplexType>(srcType);
694694
if (!srcComplexTy ||
695-
!mlir::isa<cir::CIRFPTypeInterface>(srcComplexTy.getElementTy()))
695+
!mlir::isa<cir::CIRFPTypeInterface>(srcComplexTy.getElementType()))
696696
return emitOpError()
697697
<< "requires !cir.complex<!cir.float> type for source";
698698
if (!mlir::isa<cir::BoolType>(resType))
@@ -701,7 +701,8 @@ LogicalResult cir::CastOp::verify() {
701701
}
702702
case cir::CastKind::int_complex_to_bool: {
703703
auto srcComplexTy = mlir::dyn_cast<cir::ComplexType>(srcType);
704-
if (!srcComplexTy || !mlir::isa<cir::IntType>(srcComplexTy.getElementTy()))
704+
if (!srcComplexTy ||
705+
!mlir::isa<cir::IntType>(srcComplexTy.getElementType()))
705706
return emitOpError()
706707
<< "requires !cir.complex<!cir.float> type for source";
707708
if (!mlir::isa<cir::BoolType>(resType))
@@ -711,43 +712,47 @@ LogicalResult cir::CastOp::verify() {
711712
case cir::CastKind::float_complex: {
712713
auto srcComplexTy = mlir::dyn_cast<cir::ComplexType>(srcType);
713714
if (!srcComplexTy ||
714-
!mlir::isa<cir::CIRFPTypeInterface>(srcComplexTy.getElementTy()))
715+
!mlir::isa<cir::CIRFPTypeInterface>(srcComplexTy.getElementType()))
715716
return emitOpError()
716717
<< "requires !cir.complex<!cir.float> type for source";
717718
auto resComplexTy = mlir::dyn_cast<cir::ComplexType>(resType);
718719
if (!resComplexTy ||
719-
!mlir::isa<cir::CIRFPTypeInterface>(resComplexTy.getElementTy()))
720+
!mlir::isa<cir::CIRFPTypeInterface>(resComplexTy.getElementType()))
720721
return emitOpError()
721722
<< "requires !cir.complex<!cir.float> type for result";
722723
return success();
723724
}
724725
case cir::CastKind::float_complex_to_int_complex: {
725726
auto srcComplexTy = mlir::dyn_cast<cir::ComplexType>(srcType);
726727
if (!srcComplexTy ||
727-
!mlir::isa<cir::CIRFPTypeInterface>(srcComplexTy.getElementTy()))
728+
!mlir::isa<cir::CIRFPTypeInterface>(srcComplexTy.getElementType()))
728729
return emitOpError()
729730
<< "requires !cir.complex<!cir.float> type for source";
730731
auto resComplexTy = mlir::dyn_cast<cir::ComplexType>(resType);
731-
if (!resComplexTy || !mlir::isa<cir::IntType>(resComplexTy.getElementTy()))
732+
if (!resComplexTy ||
733+
!mlir::isa<cir::IntType>(resComplexTy.getElementType()))
732734
return emitOpError() << "requires !cir.complex<!cir.int> type for result";
733735
return success();
734736
}
735737
case cir::CastKind::int_complex: {
736738
auto srcComplexTy = mlir::dyn_cast<cir::ComplexType>(srcType);
737-
if (!srcComplexTy || !mlir::isa<cir::IntType>(srcComplexTy.getElementTy()))
739+
if (!srcComplexTy ||
740+
!mlir::isa<cir::IntType>(srcComplexTy.getElementType()))
738741
return emitOpError() << "requires !cir.complex<!cir.int> type for source";
739742
auto resComplexTy = mlir::dyn_cast<cir::ComplexType>(resType);
740-
if (!resComplexTy || !mlir::isa<cir::IntType>(resComplexTy.getElementTy()))
743+
if (!resComplexTy ||
744+
!mlir::isa<cir::IntType>(resComplexTy.getElementType()))
741745
return emitOpError() << "requires !cir.complex<!cir.int> type for result";
742746
return success();
743747
}
744748
case cir::CastKind::int_complex_to_float_complex: {
745749
auto srcComplexTy = mlir::dyn_cast<cir::ComplexType>(srcType);
746-
if (!srcComplexTy || !mlir::isa<cir::IntType>(srcComplexTy.getElementTy()))
750+
if (!srcComplexTy ||
751+
!mlir::isa<cir::IntType>(srcComplexTy.getElementType()))
747752
return emitOpError() << "requires !cir.complex<!cir.int> type for source";
748753
auto resComplexTy = mlir::dyn_cast<cir::ComplexType>(resType);
749754
if (!resComplexTy ||
750-
!mlir::isa<cir::CIRFPTypeInterface>(resComplexTy.getElementTy()))
755+
!mlir::isa<cir::CIRFPTypeInterface>(resComplexTy.getElementType()))
751756
return emitOpError()
752757
<< "requires !cir.complex<!cir.float> type for result";
753758
return success();
@@ -912,7 +917,7 @@ LogicalResult cir::DerivedMethodOp::verify() {
912917
//===----------------------------------------------------------------------===//
913918

914919
LogicalResult cir::ComplexCreateOp::verify() {
915-
if (getType().getElementTy() != getReal().getType()) {
920+
if (getType().getElementType() != getReal().getType()) {
916921
emitOpError()
917922
<< "operand type of cir.complex.create does not match its result type";
918923
return failure();
@@ -945,7 +950,7 @@ OpFoldResult cir::ComplexCreateOp::fold(FoldAdaptor adaptor) {
945950
//===----------------------------------------------------------------------===//
946951

947952
LogicalResult cir::ComplexRealOp::verify() {
948-
if (getType() != getOperand().getType().getElementTy()) {
953+
if (getType() != getOperand().getType().getElementType()) {
949954
emitOpError() << "cir.complex.real result type does not match operand type";
950955
return failure();
951956
}
@@ -960,7 +965,7 @@ OpFoldResult cir::ComplexRealOp::fold(FoldAdaptor adaptor) {
960965
}
961966

962967
LogicalResult cir::ComplexImagOp::verify() {
963-
if (getType() != getOperand().getType().getElementTy()) {
968+
if (getType() != getOperand().getType().getElementType()) {
964969
emitOpError() << "cir.complex.imag result type does not match operand type";
965970
return failure();
966971
}
@@ -984,7 +989,7 @@ LogicalResult cir::ComplexRealPtrOp::verify() {
984989
auto operandPointeeTy =
985990
mlir::cast<cir::ComplexType>(operandPtrTy.getPointee());
986991

987-
if (resultPointeeTy != operandPointeeTy.getElementTy()) {
992+
if (resultPointeeTy != operandPointeeTy.getElementType()) {
988993
emitOpError()
989994
<< "cir.complex.real_ptr result type does not match operand type";
990995
return failure();
@@ -999,7 +1004,7 @@ LogicalResult cir::ComplexImagPtrOp::verify() {
9991004
auto operandPointeeTy =
10001005
mlir::cast<cir::ComplexType>(operandPtrTy.getPointee());
10011006

1002-
if (resultPointeeTy != operandPointeeTy.getElementTy()) {
1007+
if (resultPointeeTy != operandPointeeTy.getElementType()) {
10031008
emitOpError()
10041009
<< "cir.complex.imag_ptr result type does not match operand type";
10051010
return failure();

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

+2-16
Original file line numberDiff line numberDiff line change
@@ -781,18 +781,6 @@ bool cir::isIntOrIntVectorTy(mlir::Type t) {
781781
// ComplexType Definitions
782782
//===----------------------------------------------------------------------===//
783783

784-
mlir::LogicalResult cir::ComplexType::verify(
785-
llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
786-
mlir::Type elementTy) {
787-
if (!mlir::isa<cir::IntType, cir::CIRFPTypeInterface>(elementTy)) {
788-
emitError() << "element type of !cir.complex must be either a "
789-
"floating-point type or an integer type";
790-
return failure();
791-
}
792-
793-
return success();
794-
}
795-
796784
llvm::TypeSize
797785
cir::ComplexType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
798786
mlir::DataLayoutEntryListRef params) const {
@@ -801,8 +789,7 @@ cir::ComplexType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
801789
// as an array type containing exactly two elements of the corresponding
802790
// real type.
803791

804-
auto elementTy = getElementTy();
805-
return dataLayout.getTypeSizeInBits(elementTy) * 2;
792+
return dataLayout.getTypeSizeInBits(getElementType()) * 2;
806793
}
807794

808795
uint64_t
@@ -813,8 +800,7 @@ cir::ComplexType::getABIAlignment(const mlir::DataLayout &dataLayout,
813800
// as an array type containing exactly two elements of the corresponding
814801
// real type.
815802

816-
auto elementTy = getElementTy();
817-
return dataLayout.getTypeABIAlignment(elementTy);
803+
return dataLayout.getTypeABIAlignment(getElementType());
818804
}
819805

820806
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)