[flang-commits] [PATCH] D114063: [fir] Add fir.constc conversion
Diana Picus via Phabricator via flang-commits
flang-commits at lists.llvm.org
Tue Nov 16 23:55:29 PST 2021
rovka created this revision.
rovka added reviewers: clementval, awarzynski, kiranchandramohan, schweitz, jeanPerier, AlexisPerry.
rovka added a project: Flang.
Herald added subscribers: mehdi_amini, jdoerfert.
rovka requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Add the codegen for fir.constc.
This patch is part of the upstreaming effort from fir-dev.
Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>
Co-authored-by: Jean Perier <jperier at nvidia.com>
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D114063
Files:
flang/lib/Optimizer/CodeGen/CodeGen.cpp
flang/test/Fir/convert-to-llvm.fir
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,24 @@
// -----
+// Test constc.
+
+func @test_constc() -> !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_constc
+// 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)>
+
+// -----
+
// Test `fir.store` --> `llvm.store` conversion
func @test_store_index(%val_to_store : index, %addr : !fir.ref<index>) {
Index: flang/lib/Optimizer/CodeGen/CodeGen.cpp
===================================================================
--- flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -566,6 +566,34 @@
}
};
+struct ConstcOpConversion : public FIROpConversion<fir::ConstcOp> {
+ 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 ri = mlir::FloatAttr::get(ety, getValue(conc.getReal()));
+ auto rp = rewriter.create<mlir::LLVM::ConstantOp>(loc, ety, ri);
+ auto ii = mlir::FloatAttr::get(ety, getValue(conc.getImaginary()));
+ auto ip = rewriter.create<mlir::LLVM::ConstantOp>(loc, ety, ii);
+ auto c0 = mlir::ArrayAttr::get(ctx, rewriter.getI32IntegerAttr(0));
+ auto c1 = mlir::ArrayAttr::get(ctx, rewriter.getI32IntegerAttr(1));
+ auto r = rewriter.create<mlir::LLVM::UndefOp>(loc, ty);
+ auto rr = rewriter.create<mlir::LLVM::InsertValueOp>(loc, ty, r, rp, c0);
+ rewriter.replaceOpWithNewOp<mlir::LLVM::InsertValueOp>(conc, ty, rr, ip,
+ c1);
+ return success();
+ }
+
+ inline APFloat getValue(mlir::Attribute attr) const {
+ return attr.cast<fir::RealAttr>().getValue();
+ }
+};
+
/// convert value of from-type to value of to-type
struct ConvertOpConversion : public FIROpConversion<fir::ConvertOp> {
using FIROpConversion::FIROpConversion;
@@ -1628,17 +1656,17 @@
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, 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,
+ StoreOpConversion, StringLitOpConversion, SubcOpConversion,
+ UnboxCharOpConversion, UndefOpConversion, UnreachableOpConversion,
+ ZeroOpConversion>(typeConverter);
mlir::populateStdToLLVMConversionPatterns(typeConverter, pattern);
mlir::arith::populateArithmeticToLLVMConversionPatterns(typeConverter,
pattern);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114063.387847.patch
Type: text/x-patch
Size: 4833 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20211117/800bcc6b/attachment.bin>
More information about the flang-commits
mailing list