Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,10 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
}

mlir::Value createComplexImag(mlir::Location loc, mlir::Value operand) {
auto operandTy = mlir::cast<cir::ComplexType>(operand.getType());
return cir::ComplexImagOp::create(*this, loc, operandTy.getElementType(),
operand);
auto resultType = operand.getType();
if (auto complexResultType = mlir::dyn_cast<cir::ComplexType>(resultType))
resultType = complexResultType.getElementType();
return cir::ComplexImagOp::create(*this, loc, resultType, operand);
}

mlir::Value createComplexBinOp(mlir::Location loc, mlir::Value lhs,
Expand Down
10 changes: 6 additions & 4 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1539,18 +1539,20 @@ def CIR_ComplexRealOp : CIR_Op<"complex.real", [Pure]> {
def CIR_ComplexImagOp : CIR_Op<"complex.imag", [Pure]> {
let summary = "Extract the imaginary part of a complex value";
let description = [{
`cir.complex.imag` operation takes an operand of `!cir.complex` type and
yields the imaginary part of it.
`cir.complex.imag` operation takes an operand of `!cir.complex`, `!cir.int`
or `!cir.float`. If the operand is `!cir.complex`, the imag part of it will
be returned, otherwise a zero value will be returned.

Example:

```mlir
%1 = cir.complex.imag %0 : !cir.complex<!cir.float> -> !cir.float
%imag = cir.complex.imag %complex : !cir.complex<!cir.float> -> !cir.float
%imag = cir.complex.imag %scalar : !cir.float -> !cir.float
```
}];

let results = (outs CIR_AnyIntOrFloatType:$result);
let arguments = (ins CIR_ComplexType:$operand);
let arguments = (ins CIR_AnyComplexOrIntOrFloatType:$operand);

let assemblyFormat = [{
$operand `:` qualified(type($operand)) `->` qualified(type($result))
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2127,8 +2127,8 @@ mlir::Value ScalarExprEmitter::VisitImag(const UnaryOperator *E) {
// TODO(cir): handle scalar promotion.

Expr *Op = E->getSubExpr();
mlir::Location Loc = CGF.getLoc(E->getExprLoc());
if (Op->getType()->isAnyComplexType()) {
mlir::Location Loc = CGF.getLoc(E->getExprLoc());

// If it's an l-value, load through the appropriate subobject l-value.
// Note that we have to ask E because Op might be an l-value that
Expand All @@ -2142,7 +2142,7 @@ mlir::Value ScalarExprEmitter::VisitImag(const UnaryOperator *E) {
return Builder.createComplexImag(Loc, CGF.emitComplexExpr(Op));
}

return Visit(Op);
return Builder.createComplexImag(Loc, Visit(Op));
}

// Conversion from bool, integral, or floating-point to integral or
Expand Down
10 changes: 9 additions & 1 deletion clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -969,14 +969,22 @@ OpFoldResult cir::ComplexRealOp::fold(FoldAdaptor adaptor) {
}

LogicalResult cir::ComplexImagOp::verify() {
if (getType() != getOperand().getType().getElementType()) {
mlir::Type operandTy = getOperand().getType();
if (auto complexOperandTy = mlir::dyn_cast<cir::ComplexType>(operandTy))
operandTy = complexOperandTy.getElementType();

if (getType() != operandTy) {
emitOpError() << ": result type does not match operand type";
return failure();
}

return success();
}

OpFoldResult cir::ComplexImagOp::fold(FoldAdaptor adaptor) {
if (!mlir::isa<cir::ComplexType>(getOperand().getType()))
return nullptr;

if (auto complexCreateOp = getOperand().getDefiningOp<cir::ComplexCreateOp>())
return complexCreateOp.getOperand(1);

Expand Down
15 changes: 13 additions & 2 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2597,8 +2597,19 @@ mlir::LogicalResult CIRToLLVMComplexImagOpLowering::matchAndRewrite(
cir::ComplexImagOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
auto resultLLVMTy = getTypeConverter()->convertType(op.getType());
rewriter.replaceOpWithNewOp<mlir::LLVM::ExtractValueOp>(
op, resultLLVMTy, adaptor.getOperand(), llvm::ArrayRef<std::int64_t>{1});
mlir::Value operand = adaptor.getOperand();
mlir::Location loc = op.getLoc();

if (mlir::isa<cir::ComplexType>(op.getOperand().getType())) {
operand = mlir::LLVM::ExtractValueOp::create(
rewriter, loc, resultLLVMTy, operand, llvm::ArrayRef<std::int64_t>{1});
} else {
mlir::TypedAttr zeroAttr = rewriter.getZeroAttr(resultLLVMTy);
operand =
mlir::LLVM::ConstantOp::create(rewriter, loc, resultLLVMTy, zeroAttr);
}

rewriter.replaceOp(op, operand);
return mlir::success();
}

Expand Down
20 changes: 20 additions & 0 deletions clang/test/CIR/CodeGen/complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,23 @@ void real_on_scalar_glvalue() {
// OGCG: %[[B_ADDR:.*]] = alloca float, align 4
// OGCG: %[[TMP_A:.*]] = load float, ptr %[[A_ADDR]], align 4
// OGCG: store float %[[TMP_A]], ptr %[[B_ADDR]], align 4

void imag_on_scalar_glvalue() {
float a;
float b = __imag__ a;
}

// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["a"]
// CIR: %[[B_ADDR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["b", init]
// CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!cir.float>, !cir.float
// CIR: %[[A_IMAG:.*]] = cir.complex.imag %[[TMP_A]] : !cir.float -> !cir.float
// CIR: cir.store{{.*}} %[[A_IMAG]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float>

// LLVM: %[[A_ADDR:.*]] = alloca float, i64 1, align 4
// LLVM: %[[B_ADDR:.*]] = alloca float, i64 1, align 4
// LLVM: %[[TMP_A:.*]] = load float, ptr %[[A_ADDR]], align 4
// LLVM: store float 0.000000e+00, ptr %[[B_ADDR]], align 4

// OGCG: %[[A_ADDR:.*]] = alloca float, align 4
// OGCG: %[[B_ADDR:.*]] = alloca float, align 4
// OGCG: store float 0.000000e+00, ptr %[[B_ADDR]], align 4