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

[root6] Fix missing declarations in the code interpreted by Cling #451

Merged
merged 4 commits into from
Feb 7, 2023

Conversation

plexoos
Copy link
Member

@plexoos plexoos commented Nov 28, 2022

This should have been a part of #448 as the changes are closely related but unfortunately that PR was merged before I had a chance to update it.

A couple of issues seen in the ROOT6 CI tests are taken care of:

  1. Include missing definitions when interpreting AgMLGeometry. Unlike rootcint, rootcling needs to see explicit definitions of the types used in an interpreted code.

    LoadTable: .L /star-sw/StarDb/AgMLGeometry/Geometry.y2021a.C
    In file included from input_line_732:1:
    In file included from /star-sw/StarDb/AgMLGeometry/Geometry.y2021a.C:2:
    /star-sw/StarDb/AgMLGeometry/CreateGeometry.h:10:8: error: use of undeclared identifier 'gGeoManager'
      if ( gGeoManager ) {
           ^
    
  2. StBFChain: Declare global pointer to chain in compiled code. It appears that the ROOT6 interpreter prefers to see the declaration of the main StBFChain in the compiled code over the one in the bfc.C macro.

    LoadTable: .L /star-sw/StarDb/AgMLGeometry/Geometry.y2021a.C
    IncrementalExecutor::executeFunction: symbol 'chain' unresolved while linking [cling interface function]!
    root4star: .sl79_gcc485/OBJ/StRoot/St_db_Maker/St_db_Maker.cxx:934: virtual TDataSet*   St_db_Maker::LoadTable(TDataSet*): Assertion `!ee' failed.
    

    Could it be that global variables in interpreted code are not visible in other interpreted scripts?

    From the C++ point of view, the error actually makes sense because of the extern StBFChain* chain; statement in StarDb/AgMLGeometry/CreateGeometry.h

As suggested during the review, local variables were renamed to avoid confusion with global name chain

@plexoos plexoos added the ROOT6 Issues and changes related to transition from ROOT5 to ROOT6 label Nov 28, 2022
@klendathu2k
Copy link
Contributor

[snip]

   Could it be that global variables in interpreted code are not visible in other interpreted scripts?
   From the C++ point of view, the error actually makes sense because of the `extern StBFChain* chain;` statement in `StarDb/AgMLGeometry/CreateGeometry.h`

In principle, the BFC.C macro is compiled by cling. So I would expect that any macro which is loaded at a later point would be able to access a global variable that it declares. But ... this is not the first place where my expectations about cling fail.

StRoot/macros/bfc.C line 32 should be changed to extern StBFChain* chain; so that we only have the single definition in StBFChain.

plexoos added a commit to plexoos/star-sw that referenced this pull request Nov 28, 2022
@plexoos
Copy link
Member Author

plexoos commented Nov 28, 2022

In principle, the BFC.C macro is compiled by cling.

If by "compiled" you mean the JIT compilation by clang, it is still should be considered as interpretation of the code although an enhanced one. I would stick to the term because that is how they use it in early Cling presentations as opposed to "normal" external compilation or via ACLiC.

I would expect that any macro which is loaded at a later point would be able to access a global variable

I would think so too. But in this case a macro (Geometry.blah.C) is interpreted in a compiled library (libStBFChain.so) which in turn is loaded from an interpreted macro (bfc.C). Not sure though if an extra layer of indirection plays any role here...

plexoos added a commit to plexoos/star-sw that referenced this pull request Nov 29, 2022
Copy link
Contributor

@genevb genevb left a comment

Choose a reason for hiding this comment

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

chain is the handle assigned to a local variable within code in StBFChain.cxx, so I'm not happy with assigning that handle to a global variable.

@plexoos
Copy link
Member Author

plexoos commented Nov 29, 2022

Could you point to the code where such assignment take place?

@genevb
Copy link
Contributor

genevb commented Nov 29, 2022

Could you point to the code where such assignment take place?

Roughly 15 lines below where you've inserted the global assignment.

@plexoos
Copy link
Member Author

plexoos commented Nov 29, 2022

This line in the original file?

TString chain("BFC.C");

I don't think there is a conflict from the language point of view between the global and the local variables in this case, and I also don't see any compiler warning messages... But let's rename the local variable to avoid any confusion. How does that sound?

@genevb
Copy link
Contributor

genevb commented Nov 29, 2022

The compiler may differentiate local vs. global as the local interpretation gets priority at compile time. But it is good to avoid potentially confusing human code readers :-)

I don't have strong feelings about whether the global or local variable should retain the handle chain, but I'm not sure why you would pick changing the old one over the new one. But go for it - I don't care too much which one changes handles.

@plexoos
Copy link
Member Author

plexoos commented Nov 30, 2022

but I'm not sure why you would pick changing the old one over the new one

Well because the "new" one is not really new :) It is essentially re-declaration of the global variable defined in the bfc.C macro:

StBFChain *chain=0;

We can't rename it because other codes using it (e.g. the Geometry macros) expect this variable to be the global pointer to the current chain. So, to avoid confusing humans renaming the local variable is the easiest option.

Just to reiterate, the reason why we move the declaration from the macro to the library is that Cling cannot see it in another macro loaded/interpreted at runtime. Formally declaring chain in the compiled library appears to help Cling, and CINT seems to be fine with that as well.

@plexoos
Copy link
Member Author

plexoos commented Dec 1, 2022

StRoot/macros/bfc.C line 32 should be changed to extern StBFChain* chain; so that we only have the single definition in StBFChain.

This suggestion was tried in 64b449d but the jobs (ROOT5, gcc 4.8.5) fail with the following error:

Error: Symbol chain is not defined in current scope  CreateGeometry.h:26:
*** Interpreter error recovered ***
root4star: .sl79_gcc485/OBJ/StRoot/St_db_Maker/St_db_Maker.cxx:934: virtual TDataSet* St_db_Maker::LoadTable(TDataSet*): Assertion `!ee' failed.
LoadTable: .L /star-sw/StarDb/AgMLGeometry/Geometry.y2022a.C
Error: Process completed with exit code 1.

I am reverting the change. Any more suggestions or shall we merge?

@veprbl
Copy link
Member

veprbl commented Dec 1, 2022

Since this looks like a cint bug, how about we put extern behind a #ifndef __CINT__?

@plexoos
Copy link
Member Author

plexoos commented Dec 1, 2022

Fine by me but I think __CINT__ is also defined in ROOT6 along with __CLING__. Are you proposing to hide extern StBFChain* chain; from ROOT5/Cint completely?

@veprbl
Copy link
Member

veprbl commented Dec 1, 2022

#if defined(__CLING__) || !defined(__CINT__)
extern
#endif
StBFChain* chain;

@plexoos plexoos force-pushed the pr/root6_fix_undeclared branch from a192a94 to cb7c25d Compare December 10, 2022 18:23
Unlike `rootcint` `rootcling` needs to see explicit definitions of the types
used in an interpreted code.

```
LoadTable: .L /star-sw/StarDb/AgMLGeometry/Geometry.y2021a.C
In file included from input_line_732:1:
In file included from /star-sw/StarDb/AgMLGeometry/Geometry.y2021a.C:2:
/star-sw/StarDb/AgMLGeometry/CreateGeometry.h:10:8: error: use of undeclared identifier 'gGeoManager'
  if ( gGeoManager ) {
       ^
/star-sw/StarDb/AgMLGeometry/CreateGeometry.h:11:56: error: use of undeclared identifier 'gGeoManager'
    cout << "AgML geometry:  Existing TGeoManager " << gGeoManager->GetName()
                                                       ^
/star-sw/StarDb/AgMLGeometry/CreateGeometry.h:36:7: error: use of undeclared identifier 'gGeoManager'
      gGeoManager = 0;
      ^
/star-sw/StarDb/AgMLGeometry/CreateGeometry.h:38:7: error: incomplete type 'TGeoManager' named in nested name specifier
      TGeoManager::Import( filename );
      ^~~~~~~~~~~~~
```

We also hide the included code from the ROOT5 interpreter because it
utilizes C++11 features
It appears that the ROOT6 interpreter prefers to see the declaration of
the main StBFChain in the compiled code over the one in the `bfc.C`
macro:

```
LoadTable: .L /star-sw/StarDb/AgMLGeometry/Geometry.y2021a.C
IncrementalExecutor::executeFunction: symbol 'chain' unresolved while linking [cling interface function]!
root4star: .sl79_gcc485/OBJ/StRoot/St_db_Maker/St_db_Maker.cxx:934: virtual TDataSet* St_db_Maker::LoadTable(TDataSet*): Assertion `!ee' failed.
```

Could it be that global variables in interpreted code are not visible in
other interpreted scripts?

Hide valid and appropriate C++ code from ROOT5 interpreter
@plexoos plexoos force-pushed the pr/root6_fix_undeclared branch from cb7c25d to 8d88050 Compare December 21, 2022 19:56
Copy link
Member

@veprbl veprbl left a comment

Choose a reason for hiding this comment

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

Looks good to me

@plexoos
Copy link
Member Author

plexoos commented Feb 6, 2023

If there are no further comments let's merge this pull request

@plexoos plexoos merged commit d23dce3 into star-bnl:main Feb 7, 2023
@plexoos plexoos deleted the pr/root6_fix_undeclared branch February 7, 2023 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ROOT6 Issues and changes related to transition from ROOT5 to ROOT6
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants