Skip to content

[flang] fix procedure fir.box_addr identification in boxed-procedure #79290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2024

Conversation

jeanPerier
Copy link
Contributor

The pass was mistakenly identifying a fir.box_addr on a fir.box/fir.class of a derived type with procedure pointer components as being a fir.box_addr on a procedure.

Simply check if the input type is a fir.box_proc or function type (if input already rewritten) and insert convert only in this case.

This caused "invalid fir.convert" internal error.

The pass was mistakenly identifying a fir.box_addr on a
fir.box/fir.class of a derived type with procedure pointer components
as being a fir.box_addr on a procedure.

Simply check if the input type is a fir.box_proc or function type (if
input already rewritten) and insert convert only in this case.

This caused "invalid fir.convert" internal error.
@jeanPerier jeanPerier requested a review from vzakhari January 24, 2024 13:44
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:codegen labels Jan 24, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 24, 2024

@llvm/pr-subscribers-flang-codegen

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

Author: None (jeanPerier)

Changes

The pass was mistakenly identifying a fir.box_addr on a fir.box/fir.class of a derived type with procedure pointer components as being a fir.box_addr on a procedure.

Simply check if the input type is a fir.box_proc or function type (if input already rewritten) and insert convert only in this case.

This caused "invalid fir.convert" internal error.


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

2 Files Affected:

  • (modified) flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp (+2-2)
  • (modified) flang/test/Fir/boxproc-2.fir (+8)
diff --git a/flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp b/flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp
index 7d73af4d7103dcd..83333e2bae5b3db 100644
--- a/flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp
+++ b/flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp
@@ -207,8 +207,8 @@ class BoxedProcedurePass
         if (auto addr = mlir::dyn_cast<BoxAddrOp>(op)) {
           mlir::Type ty = addr.getVal().getType();
           mlir::Type resTy = addr.getResult().getType();
-          if (typeConverter.needsConversion(ty) ||
-              ty.isa<mlir::FunctionType>()) {
+          if (llvm::isa<mlir::FunctionType>(ty) ||
+              llvm::isa<fir::BoxProcType>(ty)) {
             // Rewrite all `fir.box_addr` ops on values of type `!fir.boxproc`
             // or function type to be `fir.convert` ops.
             rewriter.setInsertionPoint(addr);
diff --git a/flang/test/Fir/boxproc-2.fir b/flang/test/Fir/boxproc-2.fir
index 1550cddac5db9ed..b7a5bc69e687e5a 100644
--- a/flang/test/Fir/boxproc-2.fir
+++ b/flang/test/Fir/boxproc-2.fir
@@ -94,3 +94,11 @@ func.func @block_arg_rewrite(%arg0: !fir.ref<!fir.type<t{p:!fir.boxproc<() -> ()
 // CHECK:           fir.call @dosomething(%[[VAL_3]]) : (!fir.ref<!fir.type<tUnboxProc{p:() -> ()}>>) -> ()
 
 func.func private @dosomething(!fir.ref<!fir.type<t{p:!fir.boxproc<() -> ()>}>>)
+
+!t2 = !fir.type<t2{i:!fir.boxproc<() -> ()>}>
+func.func @box_addr_test(%arg0: !fir.box<!t2>) -> !fir.ref<!t2> {
+  %3 = fir.box_addr %arg0 : (!fir.box<!t2>) -> !fir.ref<!t2>
+  return %3 : !fir.ref<!t2>
+}
+// CHECK: func.func @box_addr_test(
+// CHECK:   fir.box_addr %{{.*}} : (!fir.box<!fir.type<t2UnboxProc{i:() -> ()}>>) -> !fir.ref<!fir.type<t2UnboxProc{i:() -> ()}>>

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.

LGTM

@jeanPerier jeanPerier merged commit a15ebe0 into llvm:main Jan 25, 2024
@jeanPerier jeanPerier deleted the jpr-box-proc-fix branch January 25, 2024 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:codegen 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.

3 participants