Skip to content

Commit acab11e

Browse files
committed
[CIR] Upstream ComplexRealPtrOp for ComplexType
1 parent 1aa6b99 commit acab11e

File tree

9 files changed

+164
-3
lines changed

9 files changed

+164
-3
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2521,6 +2521,36 @@ def ComplexImagOp : CIR_Op<"complex.imag", [Pure]> {
25212521
let hasFolder = 1;
25222522
}
25232523

2524+
//===----------------------------------------------------------------------===//
2525+
// ComplexRealPtrOp
2526+
//===----------------------------------------------------------------------===//
2527+
2528+
def ComplexRealPtrOp : CIR_Op<"complex.real_ptr", [Pure]> {
2529+
let summary = "Derive a pointer to the real part of a complex value";
2530+
let description = [{
2531+
`cir.complex.real_ptr` operation takes a pointer operand that points to a
2532+
complex value of type `!cir.complex` and yields a pointer to the real part
2533+
of the operand.
2534+
2535+
Example:
2536+
2537+
```mlir
2538+
%1 = cir.complex.real_ptr %0 : !cir.ptr<!cir.complex<!cir.double>>
2539+
-> !cir.ptr<!cir.double>
2540+
```
2541+
}];
2542+
2543+
let results = (outs CIR_PtrToIntOrFloatType:$result);
2544+
let arguments = (ins CIR_PtrToComplexType:$operand);
2545+
2546+
let assemblyFormat = [{
2547+
$operand `:`
2548+
qualified(type($operand)) `->` qualified(type($result)) attr-dict
2549+
}];
2550+
2551+
let hasVerifier = 1;
2552+
}
2553+
25242554
//===----------------------------------------------------------------------===//
25252555
// Bit Manipulation Operations
25262556
//===----------------------------------------------------------------------===//

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ def CIR_AnyIntOrFloatType : AnyTypeOf<[CIR_AnyFloatType, CIR_AnyIntType],
159159
let cppFunctionName = "isAnyIntegerOrFloatingPointType";
160160
}
161161

162+
//===----------------------------------------------------------------------===//
163+
// Complex Type predicates
164+
//===----------------------------------------------------------------------===//
165+
166+
def CIR_AnyComplexType : CIR_TypeBase<"::cir::ComplexType", "complex type">;
167+
162168
//===----------------------------------------------------------------------===//
163169
// Pointer Type predicates
164170
//===----------------------------------------------------------------------===//
@@ -180,6 +186,17 @@ class CIR_PtrToPtrTo<code type, string summary>
180186
: CIR_ConfinedType<CIR_AnyPtrType, [CIR_IsPtrToPtrToPred<type>],
181187
"pointer to pointer to " # summary>;
182188

189+
// Pointee type constraint bases
190+
class CIR_PointeePred<Pred pred> : SubstLeaves<"$_self",
191+
"::mlir::cast<::cir::PointerType>($_self).getPointee()", pred>;
192+
193+
class CIR_PtrToAnyOf<list<Type> types, string summary = "">
194+
: CIR_ConfinedType<CIR_AnyPtrType,
195+
[Or<!foreach(type, types, CIR_PointeePred<type.predicate>)>],
196+
!if(!empty(summary),
197+
"pointer to " # CIR_TypeSummaries<types>.value,
198+
summary)>;
199+
183200
// Void pointer type constraints
184201
def CIR_VoidPtrType
185202
: CIR_PtrTo<"::cir::VoidType", "void type">,
@@ -192,6 +209,13 @@ def CIR_PtrToVoidPtrType
192209
"$_builder.getType<" # cppType # ">("
193210
"cir::VoidType::get($_builder.getContext())))">;
194211

212+
class CIR_PtrToType<Type type> : CIR_PtrToAnyOf<[type]>;
213+
214+
// Pointer to type constraints
215+
def CIR_PtrToIntOrFloatType : CIR_PtrToType<CIR_AnyIntOrFloatType>;
216+
217+
def CIR_PtrToComplexType : CIR_PtrToType<CIR_AnyComplexType>;
218+
195219
//===----------------------------------------------------------------------===//
196220
// Vector Type predicates
197221
//===----------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,20 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
364364
return create<cir::ComplexImagOp>(loc, operandTy.getElementType(), operand);
365365
}
366366

367+
/// Create a cir.complex.real_ptr operation that derives a pointer to the real
368+
/// part of the complex value pointed to by the specified pointer value.
369+
mlir::Value createComplexRealPtr(mlir::Location loc, mlir::Value value) {
370+
auto srcPtrTy = mlir::cast<cir::PointerType>(value.getType());
371+
auto srcComplexTy = mlir::cast<cir::ComplexType>(srcPtrTy.getPointee());
372+
return create<cir::ComplexRealPtrOp>(
373+
loc, getPointerTo(srcComplexTy.getElementType()), value);
374+
}
375+
376+
Address createComplexRealPtr(mlir::Location loc, Address addr) {
377+
return Address{createComplexRealPtr(loc, addr.getPointer()),
378+
addr.getAlignment()};
379+
}
380+
367381
/// Create a cir.ptr_stride operation to get access to an array element.
368382
/// \p idx is the index of the element to access, \p shouldDecay is true if
369383
/// the result should decay to a pointer to the element type.

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,29 @@ LValue CIRGenFunction::emitUnaryOpLValue(const UnaryOperator *e) {
621621
}
622622
case UO_Real:
623623
case UO_Imag: {
624-
cgm.errorNYI(e->getSourceRange(), "UnaryOp real/imag");
625-
return LValue();
624+
if (op == UO_Imag) {
625+
cgm.errorNYI(e->getSourceRange(), "UnaryOp real/imag");
626+
return LValue();
627+
}
628+
629+
LValue lv = emitLValue(e->getSubExpr());
630+
assert(lv.isSimple() && "real/imag on non-ordinary l-value");
631+
632+
// __real is valid on scalars. This is a faster way of testing that.
633+
// __imag can only produce an rvalue on scalars.
634+
if (e->getOpcode() == UO_Real &&
635+
!mlir::isa<cir::ComplexType>(lv.getAddress().getElementType())) {
636+
assert(e->getSubExpr()->getType()->isArithmeticType());
637+
return lv;
638+
}
639+
640+
QualType exprTy = getContext().getCanonicalType(e->getSubExpr()->getType());
641+
QualType elemTy = exprTy->castAs<clang::ComplexType>()->getElementType();
642+
mlir::Location loc = getLoc(e->getExprLoc());
643+
Address component = builder.createComplexRealPtr(loc, lv.getAddress());
644+
LValue elemLV = makeAddrLValue(component, elemTy);
645+
elemLV.getQuals().addQualifiers(lv.getQuals());
646+
return elemLV;
626647
}
627648
case UO_PreInc:
628649
case UO_PreDec: {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,24 @@ OpFoldResult cir::ComplexImagOp::fold(FoldAdaptor adaptor) {
20892089
return complex ? complex.getImag() : nullptr;
20902090
}
20912091

2092+
//===----------------------------------------------------------------------===//
2093+
// ComplexRealPtrOp
2094+
//===----------------------------------------------------------------------===//
2095+
2096+
LogicalResult cir::ComplexRealPtrOp::verify() {
2097+
mlir::Type resultPointeeTy = getType().getPointee();
2098+
cir::PointerType operandPtrTy = getOperand().getType();
2099+
auto operandPointeeTy =
2100+
mlir::cast<cir::ComplexType>(operandPtrTy.getPointee());
2101+
2102+
if (resultPointeeTy != operandPointeeTy.getElementType()) {
2103+
emitOpError() << ": result type does not match operand type";
2104+
return failure();
2105+
}
2106+
2107+
return success();
2108+
}
2109+
20922110
//===----------------------------------------------------------------------===//
20932111
// TableGen'd op method definitions
20942112
//===----------------------------------------------------------------------===//

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,7 @@ void ConvertCIRToLLVMPass::runOnOperation() {
20422042
CIRToLLVMComplexCreateOpLowering,
20432043
CIRToLLVMComplexImagOpLowering,
20442044
CIRToLLVMComplexRealOpLowering,
2045+
CIRToLLVMComplexRealPtrOpLowering,
20452046
CIRToLLVMConstantOpLowering,
20462047
CIRToLLVMExpectOpLowering,
20472048
CIRToLLVMFuncOpLowering,
@@ -2384,6 +2385,23 @@ mlir::LogicalResult CIRToLLVMComplexImagOpLowering::matchAndRewrite(
23842385
return mlir::success();
23852386
}
23862387

2388+
mlir::LogicalResult CIRToLLVMComplexRealPtrOpLowering::matchAndRewrite(
2389+
cir::ComplexRealPtrOp op, OpAdaptor adaptor,
2390+
mlir::ConversionPatternRewriter &rewriter) const {
2391+
cir::PointerType operandTy = op.getOperand().getType();
2392+
mlir::Type resultLLVMTy = getTypeConverter()->convertType(op.getType());
2393+
mlir::Type elementLLVMTy =
2394+
getTypeConverter()->convertType(operandTy.getPointee());
2395+
2396+
mlir::LLVM::GEPArg gepIndices[2] = {0, 0};
2397+
mlir::LLVM::GEPNoWrapFlags inboundsNuw =
2398+
mlir::LLVM::GEPNoWrapFlags::inbounds | mlir::LLVM::GEPNoWrapFlags::nuw;
2399+
rewriter.replaceOpWithNewOp<mlir::LLVM::GEPOp>(
2400+
op, resultLLVMTy, elementLLVMTy, adaptor.getOperand(), gepIndices,
2401+
inboundsNuw);
2402+
return mlir::success();
2403+
}
2404+
23872405
mlir::LogicalResult CIRToLLVMGetBitfieldOpLowering::matchAndRewrite(
23882406
cir::GetBitfieldOp op, OpAdaptor adaptor,
23892407
mlir::ConversionPatternRewriter &rewriter) const {

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,16 @@ class CIRToLLVMComplexImagOpLowering
513513
mlir::ConversionPatternRewriter &) const override;
514514
};
515515

516+
class CIRToLLVMComplexRealPtrOpLowering
517+
: public mlir::OpConversionPattern<cir::ComplexRealPtrOp> {
518+
public:
519+
using mlir::OpConversionPattern<cir::ComplexRealPtrOp>::OpConversionPattern;
520+
521+
mlir::LogicalResult
522+
matchAndRewrite(cir::ComplexRealPtrOp op, OpAdaptor,
523+
mlir::ConversionPatternRewriter &) const override;
524+
};
525+
516526
class CIRToLLVMGetBitfieldOpLowering
517527
: public mlir::OpConversionPattern<cir::GetBitfieldOp> {
518528
public:

clang/test/CIR/CodeGen/complex.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ void foo9(double a, double b) {
216216
// OGCG: store double %[[TMP_A]], ptr %[[C_REAL_PTR]], align 8
217217
// OGCG: store double %[[TMP_B]], ptr %[[C_IMAG_PTR]], align 8
218218

219+
void foo10() {
220+
double _Complex c;
221+
double *realPtr = &__real__ c;
222+
}
223+
224+
// CIR: %[[COMPLEX:.*]] = cir.alloca !cir.complex<!cir.double>, !cir.ptr<!cir.complex<!cir.double>>, ["c"]
225+
// CIR: %[[REAL_PTR:.*]] = cir.complex.real_ptr %[[COMPLEX]] : !cir.ptr<!cir.complex<!cir.double>> -> !cir.ptr<!cir.double>
226+
227+
// LLVM: %[[COMPLEX:.*]] = alloca { double, double }, i64 1, align 8
228+
// LLVM: %[[REAL_PTR:.*]] = getelementptr inbounds nuw { double, double }, ptr %[[COMPLEX]], i32 0, i32 0
229+
230+
// OGCG: %[[COMPLEX:.*]] = alloca { double, double }, align 8
231+
// OGCG: %[[REAL_PTR:.*]] = getelementptr inbounds nuw { double, double }, ptr %[[COMPLEX]], i32 0, i32 0
232+
219233
void foo12() {
220234
double _Complex c;
221235
double imag = __imag__ c;
@@ -733,4 +747,4 @@ void foo29() {
733747
// OGCG: %[[INIT_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[INIT]], i32 0, i32 0
734748
// OGCG: %[[INIT_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[INIT]], i32 0, i32 1
735749
// OGCG: store i32 0, ptr %[[INIT_REAL_PTR]], align 4
736-
// OGCG: store i32 0, ptr %[[INIT_IMAG_PTR]], align 4
750+
// OGCG: store i32 0, ptr %[[INIT_IMAG_PTR]], align 4

clang/test/CIR/IR/invalid-complex.cir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ module {
4545
cir.return
4646
}
4747
}
48+
49+
50+
// -----
51+
52+
module {
53+
cir.func @complex_real_ptr_invalid_result_type() -> !cir.double {
54+
%0 = cir.alloca !cir.complex<!cir.double>, !cir.ptr<!cir.complex<!cir.double>>, ["c"]
55+
// expected-error @below {{result type does not match operand type}}
56+
%1 = cir.complex.real_ptr %0 : !cir.ptr<!cir.complex<!cir.double>> -> !cir.ptr<!cir.float>
57+
cir.return
58+
}
59+
}

0 commit comments

Comments
 (0)