@@ -403,18 +403,21 @@ class FirConverter : public Fortran::lower::AbstractConverter {
403403 [&](Fortran::lower::pft::FunctionLikeUnit &f) {
404404 if (f.isMainProgram ())
405405 hasMainProgram = true ;
406- declareFunction (f);
406+ createGlobalOutsideOfFunctionLowering (
407+ [&]() { declareFunction (f); });
407408 if (!globalOmpRequiresSymbol)
408409 globalOmpRequiresSymbol = f.getScope ().symbol ();
409410 },
410411 [&](Fortran::lower::pft::ModuleLikeUnit &m) {
411412 lowerModuleDeclScope (m);
412- for (Fortran::lower::pft::ContainedUnit &unit :
413- m.containedUnitList )
414- if (auto *f =
415- std::get_if<Fortran::lower::pft::FunctionLikeUnit>(
416- &unit))
417- declareFunction (*f);
413+ createGlobalOutsideOfFunctionLowering ([&]() {
414+ for (Fortran::lower::pft::ContainedUnit &unit :
415+ m.containedUnitList )
416+ if (auto *f =
417+ std::get_if<Fortran::lower::pft::FunctionLikeUnit>(
418+ &unit))
419+ declareFunction (*f);
420+ });
418421 },
419422 [&](Fortran::lower::pft::BlockDataUnit &b) {
420423 if (!globalOmpRequiresSymbol)
@@ -463,19 +466,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
463466
464467 // / Declare a function.
465468 void declareFunction (Fortran::lower::pft::FunctionLikeUnit &funit) {
466- // Since this is a recursive function, we only need to create a new builder
467- // for each top-level declaration. It would be simpler to have a single
468- // builder for the entire translation unit, but that requires a lot of
469- // changes to the code.
470- // FIXME: Once createGlobalOutsideOfFunctionLowering is fixed, we can
471- // remove this code and share the module builder.
472- bool newBuilder = false ;
473- if (!builder) {
474- newBuilder = true ;
475- builder = new fir::FirOpBuilder (bridge.getModule (), bridge.getKindMap (),
476- &mlirSymbolTable);
477- }
478- CHECK (builder && " FirOpBuilder did not instantiate" );
469+ CHECK (builder && " declareFunction called with uninitialized builder" );
479470 setCurrentPosition (funit.getStartingSourceLoc ());
480471 for (int entryIndex = 0 , last = funit.entryPointList .size ();
481472 entryIndex < last; ++entryIndex) {
@@ -503,11 +494,6 @@ class FirConverter : public Fortran::lower::AbstractConverter {
503494 for (Fortran::lower::pft::ContainedUnit &unit : funit.containedUnitList )
504495 if (auto *f = std::get_if<Fortran::lower::pft::FunctionLikeUnit>(&unit))
505496 declareFunction (*f);
506-
507- if (newBuilder) {
508- delete builder;
509- builder = nullptr ;
510- }
511497 }
512498
513499 // / Get the scope that is defining or using \p sym. The returned scope is not
@@ -5624,9 +5610,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
56245610 LLVM_DEBUG (llvm::dbgs () << " \n [bridge - startNewFunction]" ;
56255611 if (auto *sym = scope.symbol ()) llvm::dbgs () << " " << *sym;
56265612 llvm::dbgs () << " \n " );
5627- // I don't think setting the builder is necessary here, because callee
5613+ // Setting the builder is not necessary here, because callee
56285614 // always looks up the FuncOp from the module. If there was a function that
5629- // was not declared yet. This call to callee will cause an assertion
5615+ // was not declared yet, this call to callee will cause an assertion
56305616 // failure.
56315617 Fortran::lower::CalleeInterface callee (funit, *this );
56325618 mlir::func::FuncOp func = callee.addEntryBlockAndMapArguments ();
0 commit comments