Skip to content

Conversation

@EbinJose2002
Copy link
Contributor

@EbinJose2002 EbinJose2002 commented Jul 3, 2025

Fixes #122823
An unnecessary convert instruction was generated in the case of reference to boxchar conversion. So excluded them in the condition. The output is generating correctly.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Jul 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 3, 2025

@llvm/pr-subscribers-flang-fir-hlfir

Author: Ebin-McW (EbinJose2002)

Changes

Solves #122823
An unnecessary convert instruction was generated in the case of reference to boxchar conversion. So excluded them in the condition. The output is generating correctly.


Full diff: https://github.com/llvm/llvm-project/pull/146851.diff

2 Files Affected:

  • (modified) flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp (+2-1)
  • (added) flang/test/Lower/adjustr.f90 (+14)
diff --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
index 58f2b57712974..240b79138b0e1 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
@@ -446,7 +446,8 @@ struct AssociateOpConversion
             ((mlir::isa<fir::BoxCharType>(sourceVar.getType()) &&
               !mlir::isa<fir::BoxCharType>(assocType)))) {
           sourceVar = builder.create<fir::BoxAddrOp>(loc, assocType, sourceVar);
-        } else {
+        } else if (!mlir::isa<fir::ReferenceType>(sourceVar.getType()) ||
+                   !mlir::isa<fir::BoxCharType>(assocType)) {
           sourceVar = builder.createConvert(loc, assocType, sourceVar);
         }
         return sourceVar;
diff --git a/flang/test/Lower/adjustr.f90 b/flang/test/Lower/adjustr.f90
new file mode 100644
index 0000000000000..4813834614bd6
--- /dev/null
+++ b/flang/test/Lower/adjustr.f90
@@ -0,0 +1,14 @@
+! RUN: bbc -emit-fir -o - %s | FileCheck %s
+
+! Checking no reference to boxchar conversion is in the emitted fir.
+! CHECK-NOT: (!fir.ref<!fir.char<1,4>>) -> !fir.boxchar<1>
+
+program main
+  logical         ,dimension(1):: mask = .true.
+  character(len=2),dimension(1):: d1 = "a "
+  character(len=4),dimension(1):: d4
+  where (mask)
+    d4 = adjustr(d1 // d1)
+  end where
+  write(6,*) "d4     =", d4
+end

@vzakhari
Copy link
Contributor

vzakhari commented Jul 7, 2025

Thank you for the changes, but I think we cannot just avoid generating proper "conversion" here. Maybe it is not the case currently, but imagine that both results of hlfir.associate actually have uses. In this case, the users that expect !fir.boxchar<1> operand will receive !fir.ref<.... I think we need to generate fir.emboxchar here to produce the new result-0 with the expected data type.

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please generate fir.emboxchar instead.

@github-actions
Copy link

github-actions bot commented Jul 10, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update!

This will only work for the types with constant length. I do not know if we should be here for non-constant lengths, but I would just take the type parameters from the associate operation - this should work for all the cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delayed reply.
I have made the change to access the length from associate instruction itself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the checks to show what kind of emboxchar we generate after this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added check for the emboxchar that is generated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more context into the checks, e.g. the operands of fir.emboxchar, where they are coming from, and how the results of fir.emboxchar are used.

sourceVar = builder.create<fir::BoxAddrOp>(loc, assocType, sourceVar);
} else if (mlir::isa<fir::ReferenceType>(sourceVar.getType()) &&
mlir::isa<fir::BoxCharType>(assocType)) {
mlir::Value lenVal = associate.getTypeparams()[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please assert that getTypeparams() returns a vector of length one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more context into the checks, e.g. the operands of fir.emboxchar, where they are coming from, and how the results of fir.emboxchar are used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:fir-hlfir flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Flang] Compilation abnormally terminates when using concat-op as an argument to adjustr intrinsic function in where construct

3 participants