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

C++: Global variable flow without explicit SSA definitions #15194

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

MathiasVP
Copy link
Contributor

This PR fixes a family of missing flow cases involving global variables.

First some background: We implement global variable flow by adding a "final use" at the exit of each function that writes to the global variable, and an "initial definition" at the entry of each function that reads the global variable. For example, for something like:

int global;

void set() {
  global = source();
}

void get() {
  sink(global);
}

we will generate a "final use" at the end of the set function (which represents the value of global as we're exiting the function), and an "initial definition" at the beginning of the get function (which represents the value of global as we're starting to execute the function).

It's kinda annoying that we have to put these syntactic restrictions (i.e., only put the "final use" into functions that actually write to the variable), but these are very much necessary for global variable flow to perform well.

This PR expands this syntactic list criteria we use to decide whether a "final use" of a global variable should be inserted at the end of a function. Specifically, we no longer just look for StoreInstructions that target the global variable, but also look for places where the global variable is passed into a function. For example:

int global;

void set_param(int* p) {
  *p = source();
}

void set_global() {
  set_param(&global); // this now generates a "final use of global" even though there's no `StoreInstruction` that writes to the address of `global`
}

void read_global() {
  sink(global);
}

and places where the global variable is a struct whose field is being written to by a function. For example:

struct S { int x; };

S global;

void set_field() {
  global.x = source(); // this now generates a "final use of global" even though there's no `StoreInstruction` that writes to the address of global `global`
}

void read_field() {
  sink(global.x);
}

@MathiasVP MathiasVP added the no-change-note-required This PR does not need a change note label Dec 22, 2023
@github-actions github-actions bot added the C++ label Dec 22, 2023
… is passed into a function, or if the global variable is used for field-flow.
@MathiasVP
Copy link
Contributor Author

Bahh! Suddenly there's an enormous blowup on vim/vim that wasn't there in any of my earlier experiments 😭

@MathiasVP
Copy link
Contributor Author

Mehh there's still some enourmous performance issues with this 😭. I've pulled out the final commit to #15282 instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant