Skip to content

Conversation

glguy
Copy link
Member

@glguy glguy commented Sep 19, 2025

Constants can depend on the addresses of global variables

int flag;
int * const flag_ptr = &flag;

@glguy
Copy link
Member Author

glguy commented Sep 19, 2025

Fixes #2622

@glguy
Copy link
Member Author

glguy commented Sep 19, 2025

This probably needs more design before being considered for merging

(pureVal enable_alloc_all_globals)
Experimental
[ "Enable allocation of all globals automatically. This is necessary when"
, " constants depend on the addresses of globals."
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add something here about it affecting the behavior of llvm_load_module and mir_load_module? (Does it affect the Java end of things? AFAICR things work somewhat differently there.)

Also it's probably a good idea to mention what this means in terms of llvm_alloc_global, and maybe also say that this is likely a temporary workaround, and point at the issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't affect the behavior of the JVM or MIR backends at all. Besides the fact that this PR doesn't touch any code in those two backends, it is also worth nothing that both backends handle globals differently enough from the LLVM backend that it's not really meaningful to talk about how enable_alloc_all_globals would interact with them.

Copy link
Contributor

Choose a reason for hiding this comment

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

I apparently thought it was updating shared code. N/M

@sauclovian-g
Copy link
Contributor

(The build failures are because you need to initialize the new thing in the remote API's dummy copy of the interpreter state.)

Copy link
Contributor

@RyanGlScott RyanGlScott left a comment

Choose a reason for hiding this comment

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

Please add a test case and mention the new commands in the changelog.

(pureVal enable_alloc_all_globals)
Experimental
[ "Enable allocation of all globals automatically. This is necessary when"
, " constants depend on the addresses of globals."
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't affect the behavior of the JVM or MIR backends at all. Besides the fact that this PR doesn't touch any code in those two backends, it is also worth nothing that both backends handle globals differently enough from the LLVM backend that it's not really meaningful to talk about how enable_alloc_all_globals would interact with them.

This is necessary when global constants depend on the addresses of global variables.
When using no automatic allocations, constant globals must be manually allocated just the same as mutable globals.

```
let llvm_use_constant name = do {
  llvm_alloc_global name;
  llvm_points_to (llvm_global name)
    (llvm_global_initializer name);
};
```
@glguy glguy force-pushed the consts-depend-on-globals branch from 9d6b21a to 083a004 Compare September 26, 2025 17:34
=<< Crucible.initializeMemoryConstGlobals bak ctx llvm_mod
LLVMAllocAllGlobals ->
Crucible.populateConstGlobals bak (view Crucible.globalInitMap mtrans)
=<< Crucible.initializeAllMemory bak ctx llvm_mod
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a populateAllGlobals on the Crucible side and I would hazard a guess you probably want it here...

Copy link
Member Author

Choose a reason for hiding this comment

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

I was assuming that would set the mutable globals to their initial values, which is a stronger precondition than I want.

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