Index: flang/lib/Optimizer/CodeGen/CodeGen.cpp =================================================================== --- flang/lib/Optimizer/CodeGen/CodeGen.cpp +++ flang/lib/Optimizer/CodeGen/CodeGen.cpp @@ -566,6 +566,38 @@ } }; +/// Lower complex constants +struct ConstcOpConversion : public FIROpConversion { + using FIROpConversion::FIROpConversion; + + mlir::LogicalResult + matchAndRewrite(fir::ConstcOp conc, OpAdaptor, + mlir::ConversionPatternRewriter &rewriter) const override { + mlir::Location loc = conc.getLoc(); + mlir::MLIRContext *ctx = conc.getContext(); + mlir::Type ty = convertType(conc.getType()); + mlir::Type ety = convertType(getComplexEleTy(conc.getType())); + auto realFloatAttr = mlir::FloatAttr::get(ety, getValue(conc.getReal())); + auto realPart = + rewriter.create(loc, ety, realFloatAttr); + auto imFloatAttr = mlir::FloatAttr::get(ety, getValue(conc.getImaginary())); + auto imPart = + rewriter.create(loc, ety, imFloatAttr); + auto realIndex = mlir::ArrayAttr::get(ctx, rewriter.getI32IntegerAttr(0)); + auto imIndex = mlir::ArrayAttr::get(ctx, rewriter.getI32IntegerAttr(1)); + auto undef = rewriter.create(loc, ty); + auto setReal = rewriter.create( + loc, ty, undef, realPart, realIndex); + rewriter.replaceOpWithNewOp(conc, ty, setReal, + imPart, imIndex); + return success(); + } + + inline APFloat getValue(mlir::Attribute attr) const { + return attr.cast().getValue(); + } +}; + /// convert value of from-type to value of to-type struct ConvertOpConversion : public FIROpConversion { using FIROpConversion::FIROpConversion; @@ -1664,18 +1696,18 @@ AllocaOpConversion, BoxAddrOpConversion, BoxCharLenOpConversion, BoxDimsOpConversion, BoxEleSizeOpConversion, BoxIsAllocOpConversion, BoxIsArrayOpConversion, BoxIsPtrOpConversion, BoxRankOpConversion, - CallOpConversion, CmpcOpConversion, ConvertOpConversion, - DispatchOpConversion, DispatchTableOpConversion, DTEntryOpConversion, - DivcOpConversion, EmboxCharOpConversion, ExtractValueOpConversion, - HasValueOpConversion, GenTypeDescOpConversion, GlobalLenOpConversion, - GlobalOpConversion, InsertOnRangeOpConversion, InsertValueOpConversion, - IsPresentOpConversion, LoadOpConversion, NegcOpConversion, - MulcOpConversion, SelectCaseOpConversion, SelectOpConversion, - SelectRankOpConversion, SelectTypeOpConversion, ShapeOpConversion, - ShapeShiftOpConversion, ShiftOpConversion, SliceOpConversion, - StoreOpConversion, StringLitOpConversion, SubcOpConversion, - UnboxCharOpConversion, UndefOpConversion, UnreachableOpConversion, - ZeroOpConversion>(typeConverter); + CallOpConversion, CmpcOpConversion, ConstcOpConversion, + ConvertOpConversion, DispatchOpConversion, DispatchTableOpConversion, + DTEntryOpConversion, DivcOpConversion, EmboxCharOpConversion, + ExtractValueOpConversion, HasValueOpConversion, GenTypeDescOpConversion, + GlobalLenOpConversion, GlobalOpConversion, InsertOnRangeOpConversion, + InsertValueOpConversion, IsPresentOpConversion, LoadOpConversion, + NegcOpConversion, MulcOpConversion, SelectCaseOpConversion, + SelectOpConversion, SelectRankOpConversion, SelectTypeOpConversion, + ShapeOpConversion, ShapeShiftOpConversion, ShiftOpConversion, + SliceOpConversion, StoreOpConversion, StringLitOpConversion, + SubcOpConversion, UnboxCharOpConversion, UndefOpConversion, + UnreachableOpConversion, ZeroOpConversion>(typeConverter); mlir::populateStdToLLVMConversionPatterns(typeConverter, pattern); mlir::arith::populateArithmeticToLLVMConversionPatterns(typeConverter, pattern); Index: flang/test/Fir/convert-to-llvm.fir =================================================================== --- flang/test/Fir/convert-to-llvm.fir +++ flang/test/Fir/convert-to-llvm.fir @@ -690,6 +690,38 @@ // ----- +// Test constc. + +func @test_constc4() -> !fir.complex<4> { + %0 = fir.constc (#fir.real<4, 1.4>, #fir.real<4, 2.3>) : !fir.complex<4> + return %0 : !fir.complex<4> +} + +// CHECK-LABEL: @test_constc4 +// CHECK_SAME: () -> !llvm.struct<(f32, f32)> +// CHECK-DAG: [[rp:%.*]] = llvm.mlir.constant(1.400000e+00 : f32) : f32 +// CHECK-DAG: [[ip:%.*]] = llvm.mlir.constant(2.300000e+00 : f32) : f32 +// CHECK: [[undef:%.*]] = llvm.mlir.undef : !llvm.struct<(f32, f32)> +// CHECK: [[withr:%.*]] = llvm.insertvalue [[rp]], [[undef]][0 : i32] : !llvm.struct<(f32, f32)> +// CHECK: [[full:%.*]] = llvm.insertvalue [[ip]], [[withr]][1 : i32] : !llvm.struct<(f32, f32)> +// CHECK: return [[full]] : !llvm.struct<(f32, f32)> + +func @test_constc8() -> !fir.complex<8> { + %0 = fir.constc (#fir.real<8, 1.8>, #fir.real<8, 2.3>) : !fir.complex<8> + return %0 : !fir.complex<8> +} + +// CHECK-LABEL: @test_constc8 +// CHECK_SAME: () -> !llvm.struct<(f64, f64)> +// CHECK-DAG: [[rp:%.*]] = llvm.mlir.constant(1.800000e+00 : f64) : f64 +// CHECK-DAG: [[ip:%.*]] = llvm.mlir.constant(2.300000e+00 : f64) : f64 +// CHECK: [[undef:%.*]] = llvm.mlir.undef : !llvm.struct<(f64, f64)> +// CHECK: [[withr:%.*]] = llvm.insertvalue [[rp]], [[undef]][0 : i32] : !llvm.struct<(f64, f64)> +// CHECK: [[full:%.*]] = llvm.insertvalue [[ip]], [[withr]][1 : i32] : !llvm.struct<(f64, f64)> +// CHECK: return [[full]] : !llvm.struct<(f64, f64)> + +// ----- + // Test `fir.store` --> `llvm.store` conversion func @test_store_index(%val_to_store : index, %addr : !fir.ref) {