Started reworking access discovery pass

This commit is contained in:
York Jasper Niebuhr 2025-10-13 15:35:46 +02:00
parent 9743d397c7
commit 75b7db5a35
7 changed files with 133 additions and 7 deletions

View File

@ -1,4 +1,4 @@
add_library(selfpatch-slr SHARED main.cpp attrib.cpp) add_library(selfpatch-slr SHARED main.cpp attrib.cpp access_discover.cpp)
set_target_properties(selfpatch-slr PROPERTIES PREFIX "") set_target_properties(selfpatch-slr PROPERTIES PREFIX "")
target_compile_definitions(selfpatch-slr PRIVATE _GNU_SOURCE) target_compile_definitions(selfpatch-slr PRIVATE _GNU_SOURCE)

View File

@ -0,0 +1,83 @@
#include "access_discover.h"
#include <iostream>
static void print_gimple_tree(tree t, int depth, int indent = 2) {
if (!t)
return;
for (int i = 0; i < depth; ++i)
std::cout << " ";
std::cout << get_tree_code_name(TREE_CODE(t));
if (TREE_CODE(t) == FIELD_DECL && DECL_NAME(t))
std::cout << " <" << IDENTIFIER_POINTER(DECL_NAME(t)) << ">";
else if (TREE_CODE(t) == SSA_NAME && SSA_NAME_VAR(t) && DECL_NAME(SSA_NAME_VAR(t)))
std::cout << " <ssa " << IDENTIFIER_POINTER(DECL_NAME(SSA_NAME_VAR(t))) << ">";
std::cout << std::endl;
for (int i = 0; i < TREE_CODE_LENGTH(TREE_CODE(t)); ++i)
print_gimple_tree(TREE_OPERAND(t, i), depth + indent, indent);
}
static void print_gimple_statement(gimple* stmt) {
if (!stmt)
return;
print_gimple_stmt(stdout, stmt, 0, TDF_NONE);
for (unsigned i = 0; i < gimple_num_ops(stmt); ++i) {
tree operand = gimple_op(stmt, i);
print_gimple_tree(operand, 2, 2);
}
}
static int scan_gimple_statement(const char* funcname, gimple_stmt_iterator* gsi) {
/*
gimple *stmt = gsi_stmt(gsi);
scan_stmt_for_offsetof(funcname, &gsi);
for (unsigned i = 0; i < gimple_num_ops(stmt); ++i) {
tree op = gimple_op(stmt, i);
scan_tree_for_components(op, funcname, &gsi);
}
Build map: location->LocationPattern
*/
print_gimple_statement(gsi_stmt(*gsi));
return 0;
}
static void clean_unnecessary_locations() {
// Remove all LocationPatterns that do no do any relevant struct accesses
}
static const pass_data access_discover_pass_data = {
GIMPLE_PASS,
"access_discover",
OPTGROUP_NONE,
TV_NONE,
0,0,0,0,
TODO_update_ssa | TODO_cleanup_cfg | TODO_verify_il
};
access_discover_pass::access_discover_pass(gcc::context *ctxt) : gimple_opt_pass(access_discover_pass_data, ctxt) {}
unsigned int access_discover_pass::execute(function* fun) {
const char* funcname = fun->decl && DECL_NAME(fun->decl)
? IDENTIFIER_POINTER(DECL_NAME(fun->decl))
: "<anonymous>";
basic_block bb;
FOR_EACH_BB_FN(bb, fun) {
for (gimple_stmt_iterator gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
int stmt_scan_success;
if ((stmt_scan_success = scan_gimple_statement(funcname, &gsi)) != 0)
return stmt_scan_success;
}
}
clean_unnecessary_locations();
return 0;
}

11
plugin/access_discover.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "gcc_includes.h"
struct access_discover_pass : gimple_opt_pass {
access_discover_pass(gcc::context *ctxt);
unsigned int execute(function* fun) override;
};
#define ACCESS_DISCOVER_PASS_REFPASS "optimized"
#define ACCESS_DISCOVER_PASS_POSOP PASS_POS_INSERT_AFTER

View File

@ -1,7 +1,6 @@
#include "attrib.h" #include "attrib.h"
#include <gcc-plugin.h> #include "gcc_includes.h"
#include <tree.h>
#include <unordered_set> #include <unordered_set>
#include <string> #include <string>

23
plugin/gcc_includes.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef GCC_PLUGIN_INCLUDES_H
#define GCC_PLUGIN_INCLUDES_H
#include <gcc-plugin.h>
#include <plugin-version.h>
#include <tree.h>
#include <gimple.h>
#include <gimplify.h>
#include <gimple-iterator.h>
#include <basic-block.h>
#include <gimple-ssa.h>
#include <tree-pass.h>
#include <pretty-print.h>
#include <tree-pretty-print.h>
#include <diagnostic.h>
#include <gimple-pretty-print.h>
#include <obstack.h>
#include <cgraph.h>
#include <context.h>
#include <function.h>
#include <builtins.h>
#endif

View File

@ -1,8 +1,8 @@
#include <iostream> #include <iostream>
#include <gcc-plugin.h>
#include <plugin-version.h>
#include "gcc_includes.h"
#include "attrib.h" #include "attrib.h"
#include "access_discover.h"
int plugin_is_GPL_compatible; int plugin_is_GPL_compatible;
@ -14,5 +14,12 @@ int plugin_init(plugin_name_args *plugin_info, plugin_gcc_version *version) {
register_callback(plugin_info->base_name, PLUGIN_ATTRIBUTES, register_attributes, NULL); register_callback(plugin_info->base_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);
struct register_pass_info access_discover_pass_info;
access_discover_pass_info.pass = new access_discover_pass(nullptr);
access_discover_pass_info.ref_pass_instance_number = 1;
access_discover_pass_info.reference_pass_name = ACCESS_DISCOVER_PASS_REFPASS;
access_discover_pass_info.pos_op = ACCESS_DISCOVER_PASS_POSOP;
register_callback(plugin_info->base_name, PLUGIN_PASS_MANAGER_SETUP, nullptr, &access_discover_pass_info);
return 0; return 0;
} }

View File

@ -10,9 +10,12 @@ static size_t __spslr_offsetof(const char *t, const char *m, size_t v) {
return v; return v;
} }
/* Replace __builtin_offsetof with encoding of type and member that survives C frontend */ /*
Replace __builtin_offsetof with encoding of type and member that survives C frontend
Macro recursion prevention leaves the inner __builtin_offsetof there, otherwise use "(((size_t)&((T*)0)->M))"
*/
#undef __builtin_offsetof #undef __builtin_offsetof
#define __builtin_offsetof(T,M) \ #define __builtin_offsetof(T,M) \
((size_t (*)(const char*, const char*, size_t))__spslr_offsetof)((#T), (#M), (((size_t)&((T*)0)->M))) ((size_t (*)(const char*, const char*, size_t))__spslr_offsetof)((#T), (#M), __builtin_offsetof(T,M))
#endif #endif