Skip to content

Conversation

d-parks
Copy link
Contributor

@d-parks d-parks commented Aug 27, 2025

Add the ability to have pre and post call back functions to ExecutionEnvironment::Configure() to allow further customization of the flang runtime environment (called from _FortranAStartProgam) in situations where either the desired features/functionality are proprietary or are too specific to be accepted by the flang community.

Example:
Custom constructor object linked with flang objects:

#include "flang-rt/runtime/environment.h"
#include "flang/Runtime/entry-names.h"
#include "flang/Runtime/extensions.h"

namespace Fortran::runtime {

// Do something specific to the flang runtime environment prior to the
// core logic of ExecutionEnvironment::Configure().
static void
CustomPreConfigureEnv(int argc, const char *argv[], const char *envp[],
                      const EnvironmentDefaultList *envDefaultList) {
  puts(__func__);
}

// Do something specific to the flang runtime environment after running the
// core logic of ExecutionEnvironment::Configure().
static void
CustomPostConfigureEnv(int argc, const char *argv[], const char *envp[],
                       const EnvironmentDefaultList *envDefaultList) {
  puts(__func__);
}

void __attribute__((constructor)) CustomInitCstor(void) {
  // Possibilities:
  // RTNAME(RegisterConfigureEnv)(&CustomPreConfigureEnv,
  // &CustomPostConfigureEnv); RTNAME(RegisterConfigureEnv)(nullptr,
  // &CustomPostConfigureEnv);
  RTNAME(RegisterConfigureEnv)(&CustomPreConfigureEnv, nullptr);
}
} // namespace Fortran::runtime

…o further customize the runtime environment.

	modified:   flang-rt/include/flang-rt/runtime/environment.h
	modified:   flang-rt/lib/runtime/environment.cpp
@d-parks d-parks requested review from klausler and vzakhari August 27, 2025 15:59
@d-parks d-parks changed the title [flang-rt] Add the ability to have user supplied callback functions too further customize the runtime environment. [flang-rt] Add the ability to have user supplied callback functions to further customize the runtime environment. Aug 27, 2025

// Optional callback routines to be invoked pre and post
// execution environment setup.
void (*PreConfigureEnv)(int, const char *[], const char *[],
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you, Dave!

It looks good to me, except maybe we should typedef the type of the callback functions (probably within ExecutionEnvironment struct), so that we can reduce text here and in the declaration of RegisterConfigureEnv.

@klausler
Copy link
Contributor

Should there be support for multiple callbacks?

David Parks added 3 commits August 27, 2025 13:57
	modified:   ../../include/flang-rt/runtime/environment.h
	modified:   environment.cpp
	modified:   ../../include/flang-rt/runtime/environment.h
…he order they are registered.

	modified:   environment.cpp
// The pre and post callback functions are called upon entry and exit
// of ExecutionEnvironment::Configure() respectively.

void RTNAME(RegisterConfigureEnv)(
Copy link
Contributor

Choose a reason for hiding this comment

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

Who's able to call this early enough to take effect?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the example above, the constructor will be called before _QQmain.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. Where from?

// for either pre or post functions.

static constexpr int nConfigEnvCallback{8};
int nPreConfigEnvCallback{0};
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that you want to define a variable like this in a header file.

if (0 != nPreConfigEnvCallback) {
// Run an optional callback function after the core of the
// ExecutionEnvironment() logic.
for (auto i = 0; i != nPreConfigEnvCallback; ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

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

int i{0}; please in the runtime

if (0 != nPostConfigEnvCallback) {
// Run an optional callback function in reverse order of registration
// after the core of the ExecutionEnvironment() logic.
for (auto i = 0; i != nPostConfigEnvCallback; ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

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

int i{0};

David Parks added 2 commits August 28, 2025 13:18
	modified:   ../../include/flang-rt/runtime/environment.h
	modified:   environment.cpp
	modified:   environment.cpp
@d-parks d-parks merged commit f9cac5f into llvm:main Aug 29, 2025
9 checks passed
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.

3 participants