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

Update prune-clones.cc #35

Open
wants to merge 1 commit into
base: 2024-S-prune-clone-functions
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions gcc/prune-clones.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,66 @@
#include "ipa-ref.h"
#include "ipa-param-manipulation.h"
#include "cgraph.h"
#include "gcc-plugin.h"
#include "plugin-version.h"
#include "tree-pass.h"
#include "context.h"
#include "prune-clones.h"

unsigned int prune_clones(cgraph_node *node);

// Constructor definition for pass_prune_clones
pass_prune_clones::pass_prune_clones(gcc::context *ctxt)
: gimple_opt_pass(pass_data_prune_clones, ctxt) {}

unsigned int pass_prune_clones::execute(function *fun) {
for (cgraph_node *node = cgraph_nodes; node; node = node->next) {
if (node->simd_clone) {
printf("Found SIMD clones for function: %s\n", cgraph_node_name(node));
prune_clones(node);
}
}
return 0;
}

namespace {
// Define the pass data structure
const pass_data pass_data_prune_clones = {
GIMPLE_PASS, // type
"prune_clones", // name
OPTGROUP_NONE, // optinfo_flags
TV_NONE, // tv_id
0, // properties_required
PROP_gimple_any, // PROP_gimple
0, // properties_provided
0, // properties_destroyed
0, // todo_flags_start
0 // todo_flags_finish
};

// Define the pass class
class pass_prune_clones : public gimple_opt_pass {
public:
pass_prune_clones(gcc::context *ctxt)
: gimple_opt_pass(pass_data_prune_clones, ctxt) {}

unsigned int execute(function *fun) final override;
};

// Execute function for the pass
unsigned int pass_prune_clones::execute(function *fun) {
// Walk each function in the call graph
FOR_EACH_FUNCTION(node) {
// If node points to SIMD clones
if (node->simd_clones) {
prune_clones(node);
}
}
return 0;
gimple_opt_pass *make_pass_prune_clones(gcc::context *ctxt) {
return new pass_prune_clones(ctxt);
}
} // namespace

// Function to prune duplicate clones
unsigned int prune_clones(cgraph_node *node) {
printf("Pruning clones for function: %s\n", cgraph_node_name(node));
cgraph_function_version_info *default_fvi = node->function_version();
cgraph_function_version_info *curr_fvi = node->function_version();
cgraph_function_version_info *prev_fvi = nullptr;

while (curr_fvi->next) {
prev_fvi = curr_fvi;
// Get the next clone
curr_fvi = curr_fvi->next;
// Compare to the default node
// Prune if duplicate
if(curr_fvi->clone_of == default_fvi->clone_of) {
printf("Removing duplicate clone: %s\n", cgraph_node_name(curr_fvi->clone_of));
prev_fvi->next = curr_fvi->next;
delete curr_fvi;
}
}

return 0;
}

// Function to create a new instance of the gimple_opt_pass
gimple_opt_pass *make_pass_prune_clones(gcc::context *ctxt) {
return new pass_prune_clones(ctxt);
}