Skip to content

Commit b6e720d

Browse files
committed
[CIR] Update ComplexImagOp to work on scalar type
1 parent 3763712 commit b6e720d

File tree

6 files changed

+51
-27
lines changed

6 files changed

+51
-27
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,10 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
155155
}
156156

157157
mlir::Value createComplexImag(mlir::Location loc, mlir::Value operand) {
158-
auto operandTy = mlir::cast<cir::ComplexType>(operand.getType());
159-
return cir::ComplexImagOp::create(*this, loc, operandTy.getElementType(),
160-
operand);
158+
auto resultType = operand.getType();
159+
if (auto complexResultType = mlir::dyn_cast<cir::ComplexType>(resultType))
160+
resultType = complexResultType.getElementType();
161+
return cir::ComplexImagOp::create(*this, loc, resultType, operand);
161162
}
162163

163164
cir::LoadOp createLoad(mlir::Location loc, mlir::Value ptr,

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3291,18 +3291,20 @@ def CIR_ComplexRealOp : CIR_Op<"complex.real", [Pure]> {
32913291
def CIR_ComplexImagOp : CIR_Op<"complex.imag", [Pure]> {
32923292
let summary = "Extract the imaginary part of a complex value";
32933293
let description = [{
3294-
`cir.complex.imag` operation takes an operand of `!cir.complex` type and
3295-
yields the imaginary part of it.
3294+
`cir.complex.imag` operation takes an operand of `!cir.complex`, `!cir.int`
3295+
or `!cir.float`. If the operand is `!cir.complex`, the imag part of it will
3296+
be returned, otherwise a zero value will be returned.
32963297

32973298
Example:
32983299

32993300
```mlir
3300-
%1 = cir.complex.imag %0 : !cir.complex<!cir.float> -> !cir.float
3301+
%imag = cir.complex.imag %complex : !cir.complex<!cir.float> -> !cir.float
3302+
%imag = cir.complex.imag %scalar : !cir.float -> !cir.float
33013303
```
33023304
}];
33033305

33043306
let results = (outs CIR_AnyIntOrFloatType:$result);
3305-
let arguments = (ins CIR_ComplexType:$operand);
3307+
let arguments = (ins CIR_AnyComplexOrIntOrFloatType:$operand);
33063308

33073309
let assemblyFormat = [{
33083310
$operand `:` qualified(type($operand)) `->` qualified(type($result))

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,16 +2159,16 @@ mlir::Value ScalarExprEmitter::VisitRealImag(const UnaryOperator *e,
21592159

21602160
// __imag on a scalar returns zero. Emit the subexpr to ensure side
21612161
// effects are evaluated, but not the actual value.
2162-
if (op->isGLValue())
2163-
cgf.emitLValue(op);
2164-
else if (!promotionTy.isNull())
2165-
cgf.emitPromotedScalarExpr(op, promotionTy);
2166-
else
2167-
cgf.emitScalarExpr(op);
2168-
2169-
mlir::Type valueTy =
2170-
cgf.convertType(promotionTy.isNull() ? e->getType() : promotionTy);
2171-
return builder.getNullValue(valueTy, loc);
2162+
mlir::Value operand;
2163+
if (op->isGLValue()) {
2164+
operand = cgf.emitLValue(op).getPointer();
2165+
operand = cir::LoadOp::create(builder, loc, operand);
2166+
} else if (!promotionTy.isNull()) {
2167+
operand = cgf.emitPromotedScalarExpr(op, promotionTy);
2168+
} else {
2169+
operand = cgf.emitScalarExpr(op);
2170+
}
2171+
return builder.createComplexImag(loc, operand);
21722172
}
21732173

21742174
/// Return the size or alignment of the type of argument of the sizeof

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2418,14 +2418,22 @@ OpFoldResult cir::ComplexRealOp::fold(FoldAdaptor adaptor) {
24182418
//===----------------------------------------------------------------------===//
24192419

24202420
LogicalResult cir::ComplexImagOp::verify() {
2421-
if (getType() != getOperand().getType().getElementType()) {
2421+
mlir::Type operandTy = getOperand().getType();
2422+
if (auto complexOperandTy = mlir::dyn_cast<cir::ComplexType>(operandTy)) {
2423+
operandTy = complexOperandTy.getElementType();
2424+
}
2425+
2426+
if (getType() != operandTy) {
24222427
emitOpError() << ": result type does not match operand type";
24232428
return failure();
24242429
}
24252430
return success();
24262431
}
24272432

24282433
OpFoldResult cir::ComplexImagOp::fold(FoldAdaptor adaptor) {
2434+
if (!mlir::isa<cir::ComplexType>(getOperand().getType()))
2435+
return nullptr;
2436+
24292437
if (auto complexCreateOp = getOperand().getDefiningOp<cir::ComplexCreateOp>())
24302438
return complexCreateOp.getOperand(1);
24312439

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3062,8 +3062,19 @@ mlir::LogicalResult CIRToLLVMComplexImagOpLowering::matchAndRewrite(
30623062
cir::ComplexImagOp op, OpAdaptor adaptor,
30633063
mlir::ConversionPatternRewriter &rewriter) const {
30643064
mlir::Type resultLLVMTy = getTypeConverter()->convertType(op.getType());
3065-
rewriter.replaceOpWithNewOp<mlir::LLVM::ExtractValueOp>(
3066-
op, resultLLVMTy, adaptor.getOperand(), llvm::ArrayRef<std::int64_t>{1});
3065+
mlir::Value operand = adaptor.getOperand();
3066+
mlir::Location loc = op.getLoc();
3067+
3068+
if (mlir::isa<cir::ComplexType>(op.getOperand().getType())) {
3069+
operand = mlir::LLVM::ExtractValueOp::create(
3070+
rewriter, loc, resultLLVMTy, operand, llvm::ArrayRef<std::int64_t>{1});
3071+
} else {
3072+
mlir::TypedAttr zeroAttr = rewriter.getZeroAttr(resultLLVMTy);
3073+
operand =
3074+
mlir::LLVM::ConstantOp::create(rewriter, loc, resultLLVMTy, zeroAttr);
3075+
}
3076+
3077+
rewriter.replaceOp(op, operand);
30673078
return mlir::success();
30683079
}
30693080

clang/test/CIR/CodeGen/complex.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,8 +1160,9 @@ void imag_on_scalar_glvalue() {
11601160

11611161
// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["a"]
11621162
// CIR: %[[B_ADDR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["b", init]
1163-
// CIR: %[[CONST_ZERO:.*]] = cir.const #cir.fp<0.000000e+00> : !cir.float
1164-
// CIR: cir.store{{.*}} %[[CONST_ZERO]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float>
1163+
// CIR: %[[TMP_A:.*]] = cir.load %[[A_ADDR]] : !cir.ptr<!cir.float>, !cir.float
1164+
// CIR: %[[A_IMAG:.*]] = cir.complex.imag %[[TMP_A]] : !cir.float -> !cir.float
1165+
// CIR: cir.store{{.*}} %[[A_IMAG]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float>
11651166

11661167
// LLVM: %[[A_ADDR:.*]] = alloca float, i64 1, align 4
11671168
// LLVM: %[[B_ADDR:.*]] = alloca float, i64 1, align 4
@@ -1205,9 +1206,10 @@ void imag_on_scalar_with_type_promotion() {
12051206

12061207
// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.f16, !cir.ptr<!cir.f16>, ["a"]
12071208
// CIR: %[[B_ADDR:.*]] = cir.alloca !cir.f16, !cir.ptr<!cir.f16>, ["b", init]
1208-
// CIR: %[[CONST_ZERO:.*]] = cir.const #cir.fp<0.000000e+00> : !cir.float
1209-
// CIR: %[[CONST_ZERO_F16:.*]] = cir.cast floating %[[CONST_ZERO]] : !cir.float -> !cir.f16
1210-
// CIR: cir.store{{.*}} %[[CONST_ZERO_F16]], %[[B_ADDR]] : !cir.f16, !cir.ptr<!cir.f16>
1209+
// CIR: %[[TMP_A:.*]] = cir.load %[[A_ADDR]] : !cir.ptr<!cir.f16>, !cir.f16
1210+
// CIR: %[[A_IMAG:.*]] = cir.complex.imag %[[TMP_A]] : !cir.f16 -> !cir.f16
1211+
// CIR: %[[A_IMAG_F16:.*]] = cir.cast floating %[[A_IMAG]] : !cir.f16 -> !cir.f16
1212+
// CIR: cir.store{{.*}} %[[A_IMAG_F16]], %[[B_ADDR]] : !cir.f16, !cir.ptr<!cir.f16>
12111213

12121214
// LLVM: %[[A_ADDR:.*]] = alloca half, i64 1, align 2
12131215
// LLVM: %[[B_ADDR:.*]] = alloca half, i64 1, align 2
@@ -1225,8 +1227,8 @@ void imag_on_const_scalar() {
12251227
// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["a"]
12261228
// CIR: %[[B_ADDR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["b", init]
12271229
// CIR: %[[CONST_ONE:.*]] = cir.const #cir.fp<1.000000e+00> : !cir.float
1228-
// CIR: %[[CONST_ZERO:.*]] = cir.const #cir.fp<0.000000e+00> : !cir.float
1229-
// CIR: cir.store{{.*}} %[[CONST_ZERO]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float>
1230+
// CIR: %[[CONST_IMAG:.*]] = cir.complex.imag %[[CONST_ONE]] : !cir.float -> !cir.float
1231+
// CIR: cir.store{{.*}} %[[CONST_IMAG]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float>
12301232

12311233
// LLVM: %[[A_ADDR:.*]] = alloca float, i64 1, align 4
12321234
// LLVM: %[[B_ADDR:.*]] = alloca float, i64 1, align 4

0 commit comments

Comments
 (0)