[flang-commits] [flang] [flang] Inline scalar-to-scalar TRANSFER for same-size trivial types (PR #191589)

Zhen Wang via flang-commits flang-commits at lists.llvm.org
Mon Apr 13 19:46:58 PDT 2026


================
@@ -8680,6 +8680,36 @@ IntrinsicLibrary::genTransfer(mlir::Type resultType,
 
   assert(args.size() >= 2); // args.size() == 2 when size argument is omitted.
 
+  bool absentSize = (args.size() == 2);
+
+  // Inline scalar-to-scalar transfers when the result is a trivial type
+  // (integer, real, etc.) and both source and result have the same storage
+  // size.
+  if (absentSize && fir::isa_trivial(resultType)) {
+    mlir::Value sourceBase = fir::getBase(args[0]);
+    mlir::Type sourceType = fir::unwrapRefType(sourceBase.getType());
+    if (fir::isa_ref_type(sourceBase.getType()) &&
+        !mlir::isa<fir::SequenceType>(sourceType)) {
+      auto sourceSizeAndAlign = fir::getTypeSizeAndAlignment(
+          loc, sourceType, builder.getDataLayout(), builder.getKindMap());
+      auto resultSizeAndAlign = fir::getTypeSizeAndAlignment(
+          loc, resultType, builder.getDataLayout(), builder.getKindMap());
+      if (sourceSizeAndAlign && resultSizeAndAlign &&
+          sourceSizeAndAlign->first == resultSizeAndAlign->first) {
+        if (mlir::isa<mlir::IntegerType, mlir::FloatType>(sourceType) &&
+            mlir::isa<mlir::IntegerType, mlir::FloatType>(resultType)) {
+          mlir::Value val = fir::LoadOp::create(builder, loc, sourceBase);
+          if (sourceType != resultType)
+            val = mlir::arith::BitcastOp::create(builder, loc, resultType, val);
+          return val;
+        }
+        mlir::Type refTy = builder.getRefType(resultType);
+        mlir::Value cast = builder.createConvert(loc, refTy, sourceBase);
----------------
wangzpgi wrote:

Please correct me if I am wrong @vzakhari, I don't think special TBAA handling is needed here. The load's memref traces through fir::ConvertOp, which getSource can't look through, so the source is SourceKind::Unknown and AddAliasTags conservatively assigns no TBAA tag. The arith.bitcast path is also safe since it loads through the original source ref.

https://github.com/llvm/llvm-project/pull/191589


More information about the flang-commits mailing list