Skip to content
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

[flang][cuda] Support memory cleanup at a return statement #116304

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

khaki3
Copy link
Contributor

@khaki3 khaki3 commented Nov 15, 2024

We generate cuf.free and func.return twice if a return statement exists at the end of program.

program test
  integer, device :: a(10)
  return
end
% flang -x cuda test.cuf -mmlir --mlir-print-ir-after-all
error: loc("/path/to/test.cuf":3:3): 'func.return' op must be the last operation in the parent block
// -----// IR Dump After Fortran::lower::VerifierPass Failed () //----- //

Dumped IR:

  "func.func"() <{function_type = () -> (), sym_name = "_QQmain"}> ({
...
    "cuf.free"(%5#1) <{data_attr = #cuf.cuda<device>}> : (!fir.ref<!fir.array<10xi32>>) -> ()
    "func.return"() : () -> ()
    "cuf.free"(%5#1) <{data_attr = #cuf.cuda<device>}> : (!fir.ref<!fir.array<10xi32>>) -> ()
    "func.return"() : () -> ()
}
...

The routine genExitRoutine in Bridge.cpp is guarded by blockIsUnterminated() to make sure that func.return is generated only at the end of a block. However, we redundantly run bridge.fctCtx().finalizeAndKeep() before genExitRoutine in this case, resulting in two pairs of cuf.free and func.return. This PR fixes Bridge.cpp by using blockIsUnterminated() to guard finalizeAndKeep as well.

@khaki3 khaki3 changed the title [flang][cuf] Support memory finalization at a final return statement [flang][cuf] Support memory cleanup at a return statement Nov 15, 2024
@khaki3 khaki3 changed the title [flang][cuf] Support memory cleanup at a return statement [flang][cuda] Support memory cleanup at a return statement Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant