[PATCH] D113992: [fir] Add fir.string_lit conversion

Valentin Clement via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 16 05:55:08 PST 2021


clementval created this revision.
clementval added reviewers: jeanPerier, svedanayagam, sscalpone, kiranchandramohan, jdoerfert, schweitz, pmccormick, rovka, AlexisPerry, PeteSteinfeld, awarzynski.
Herald added a subscriber: mehdi_amini.
Herald added a project: Flang.
clementval requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch adds the conversion pattern for
the fir.string_lit operation.

This patch is part of the upstreaming effort from fir-dev branch.

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/D113992

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
@@ -1200,3 +1200,23 @@
 // CHECK-NEXT:  %[[ptr:.*]] = llvm.mlir.null : !llvm.ptr<i64>
 // CHECK-NEXT:  %[[ret_val:.*]] = llvm.call @is_present(%[[ptr]]) : (!llvm.ptr<i64>) -> i1
 // CHECK-NEXT:  llvm.return %[[ret_val]] : i1
+
+// -----
+
+// Test `fir.string_lit` conversion.
+
+func @string_lit0() {
+  %1 = fir.string_lit "Hello, World!"(13) : !fir.char<1>
+  return
+}
+
+// CHECK-LABEL: llvm.func @string_lit0
+// CHECK: %{{.*}} = llvm.mlir.constant("Hello, World!") : !llvm.array<13 x i8>
+
+func @string_lit1() {
+  %2 = fir.string_lit [158, 2345](2) : !fir.char<2>
+  return
+}
+
+// CHECK-LABEL: llvm.func @string_lit1
+// %{{.*}} = llvm.mlir.constant(dense<[158, 2345]> : vector<2xi16>) : !llvm.array<2 x i16>
Index: flang/lib/Optimizer/CodeGen/CodeGen.cpp
===================================================================
--- flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -465,6 +465,38 @@
   }
 };
 
+/// Lower `fir.string_lit` to LLVM IR dialect operation.
+struct StringLitOpConversion : public FIROpConversion<fir::StringLitOp> {
+  using FIROpConversion::FIROpConversion;
+
+  mlir::LogicalResult
+  matchAndRewrite(fir::StringLitOp constop, OpAdaptor adaptor,
+                  mlir::ConversionPatternRewriter &rewriter) const override {
+    auto ty = convertType(constop.getType());
+    auto attr = constop.getValue();
+    if (attr.isa<mlir::StringAttr>()) {
+      rewriter.replaceOpWithNewOp<mlir::LLVM::ConstantOp>(constop, ty, attr);
+    } else {
+      auto arr = attr.cast<mlir::ArrayAttr>();
+      auto charTy = constop.getType().cast<fir::CharacterType>();
+      unsigned bits = lowerTy().characterBitsize(charTy);
+      mlir::Type intTy = rewriter.getIntegerType(bits);
+      auto attrs = llvm::map_range(
+          arr.getValue(), [intTy, bits](mlir::Attribute attr) -> Attribute {
+            return mlir::IntegerAttr::get(
+                intTy,
+                attr.cast<mlir::IntegerAttr>().getValue().sextOrTrunc(bits));
+          });
+      mlir::Type vecType = mlir::VectorType::get(arr.size(), intTy);
+      auto denseAttr = mlir::DenseElementsAttr::get(
+          vecType.cast<mlir::ShapedType>(), llvm::to_vector<8>(attrs));
+      rewriter.replaceOpWithNewOp<mlir::arith::ConstantOp>(constop, ty,
+                                                           denseAttr);
+    }
+    return success();
+  }
+};
+
 // `fir.call` -> `llvm.call`
 struct CallOpConversion : public FIROpConversion<fir::CallOp> {
   using FIROpConversion::FIROpConversion;
@@ -1521,8 +1553,9 @@
         IsPresentOpConversion, LoadOpConversion, NegcOpConversion,
         MulcOpConversion, SelectCaseOpConversion, SelectOpConversion,
         SelectRankOpConversion, SelectTypeOpConversion, StoreOpConversion,
-        SubcOpConversion, UnboxCharOpConversion, UndefOpConversion,
-        UnreachableOpConversion, ZeroOpConversion>(typeConverter);
+        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: D113992.387596.patch
Type: text/x-patch
Size: 3497 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211116/fe017d45/attachment.bin>


More information about the llvm-commits mailing list