[flang-commits] [PATCH] D139514: [flang] fir.emboxchar codegen with static length fir.char types

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Dec 7 01:17:41 PST 2022


jeanPerier created this revision.
jeanPerier added a reviewer: clementval.
Herald added a subscriber: mehdi_amini.
Herald added projects: Flang, All.
jeanPerier requested review of this revision.
Herald added a subscriber: jdoerfert.

The verifier of fir.emboxchar accepts `fir.char<kind, constant>` types,
but its codegen failed because of a missing cast. It is convenient and
safe to not require making the cast in FIR, so add it in codegen.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139514

Files:
  flang/lib/Optimizer/CodeGen/CodeGen.cpp
  flang/test/Fir/boxchar.fir


Index: flang/test/Fir/boxchar.fir
===================================================================
--- flang/test/Fir/boxchar.fir
+++ flang/test/Fir/boxchar.fir
@@ -16,6 +16,16 @@
   return
 }
 
+// CHECK-LABEL: define void @get_name_2
+func.func @get_name_2() {
+  %1 = fir.address_of (@name) : !fir.ref<!fir.char<1,9>>
+  %2 = arith.constant 9 : i64
+  %4 = fir.emboxchar %1, %2 : (!fir.ref<!fir.char<1,9>>, i64) -> !fir.boxchar<1>
+  // CHECK: call void @callee(ptr @name, i64 9)
+  fir.call @callee(%4) : (!fir.boxchar<1>) -> ()
+  return
+}
+
 fir.global @name constant : !fir.char<1,9> {
   %str = fir.string_lit "Your name"(9) : !fir.char<1,9>
   //constant 1
Index: flang/lib/Optimizer/CodeGen/CodeGen.cpp
===================================================================
--- flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -1114,6 +1114,12 @@
         llvmStructTy.cast<mlir::LLVM::LLVMStructType>().getBody()[1];
     mlir::Value lenAfterCast = integerCast(loc, rewriter, lenTy, charBufferLen);
 
+    mlir::Type addrTy =
+        llvmStructTy.cast<mlir::LLVM::LLVMStructType>().getBody()[0];
+    if (addrTy != charBuffer.getType())
+      charBuffer =
+          rewriter.create<mlir::LLVM::BitcastOp>(loc, addrTy, charBuffer);
+
     auto insertBufferOp = rewriter.create<mlir::LLVM::InsertValueOp>(
         loc, llvmStruct, charBuffer, 0);
     rewriter.replaceOpWithNewOp<mlir::LLVM::InsertValueOp>(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139514.480806.patch
Type: text/x-patch
Size: 1460 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221207/f7e5a2ee/attachment-0001.bin>


More information about the flang-commits mailing list