Skip to content

Commit cf90520

Browse files
committed
added inline-everything flag
1 parent 83aec29 commit cf90520

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

include/shady/ir.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ struct CompilerConfig_ {
286286
bool after_every_pass;
287287
bool delete_unused_instructions;
288288
} cleanup;
289+
bool inline_everything;
289290
} optimisations;
290291

291292
struct {

src/driver/cli.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ F(logging.print_internal, print-internal) \
9898
F(logging.print_generated, print-builtin) \
9999
F(logging.print_generated, print-generated) \
100100
F(lower.simt_to_explicit_simd, lower-simt-to-simd) \
101+
F(optimisations.inline_everything, inline-everything) \
101102

102103
void cli_parse_compiler_config_args(CompilerConfig* config, int* pargc, char** argv) {
103104
int argc = *pargc;

src/shady/passes/opt_inline.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef struct {
1919

2020
typedef struct {
2121
Rewriter rewriter;
22+
const CompilerConfig* config;
2223
CallGraph* graph;
2324
const Node* old_fun;
2425
Node* fun;
@@ -59,7 +60,7 @@ typedef struct {
5960
bool can_be_eliminated;
6061
} FnInliningCriteria;
6162

62-
static FnInliningCriteria get_inlining_heuristic(CGNode* fn_node) {
63+
static FnInliningCriteria get_inlining_heuristic(const CompilerConfig* config, CGNode* fn_node) {
6364
FnInliningCriteria crit = { 0 };
6465

6566
CGEdge e;
@@ -70,10 +71,8 @@ static FnInliningCriteria get_inlining_heuristic(CGNode* fn_node) {
7071
crit.num_inlineable_calls++;
7172
}
7273

73-
debugv_print("%s has %d callers\n", get_abstraction_name(fn_node->fn), crit.num_calls);
74-
7574
// a function can be inlined if it has exactly one inlineable call...
76-
if (crit.num_inlineable_calls <= 1)
75+
if (crit.num_inlineable_calls <= 1 || config->optimisations.inline_everything)
7776
crit.can_be_inlined = true;
7877

7978
// avoid inlining recursive things for now
@@ -91,14 +90,24 @@ static FnInliningCriteria get_inlining_heuristic(CGNode* fn_node) {
9190
if (!is_call_safely_removable(fn_node->fn))
9291
crit.can_be_eliminated = false;
9392

93+
debugv_print("inlining heuristic for '%s': num_calls=%d num_inlineable_calls=%d safely_removable=%d address_leaks=%d recursive=%d inlineable=%d can_be_eliminated=%d\n",
94+
get_abstraction_name(fn_node->fn),
95+
crit.num_calls,
96+
crit.num_inlineable_calls,
97+
is_call_safely_removable(fn_node->fn),
98+
fn_node->is_address_captured,
99+
fn_node->is_recursive,
100+
crit.can_be_inlined,
101+
crit.can_be_eliminated);
102+
94103
return crit;
95104
}
96105

97106
/// inlines the abstraction with supplied arguments
98107
static const Node* inline_call(Context* ctx, const Node* ocallee, Nodes nargs, const Node* return_to) {
99108
assert(is_abstraction(ocallee));
100109

101-
log_string(DEBUG, "Inlining '%s' inside '%s'\n", get_abstraction_name(ctx->fun), get_abstraction_name(ocallee));
110+
log_string(DEBUG, "Inlining '%s' inside '%s'\n", get_abstraction_name(ocallee), get_abstraction_name(ctx->fun));
102111
Context inline_context = *ctx;
103112
inline_context.rewriter.map = clone_dict(inline_context.rewriter.map);
104113

@@ -136,7 +145,7 @@ static const Node* process(Context* ctx, const Node* node) {
136145
case Function_TAG: {
137146
if (ctx->graph) {
138147
CGNode* fn_node = *find_value_dict(const Node*, CGNode*, ctx->graph->fn2cgn, node);
139-
if (get_inlining_heuristic(fn_node).can_be_eliminated) {
148+
if (get_inlining_heuristic(ctx->config, fn_node).can_be_eliminated) {
140149
debugv_print("Eliminating %s because it has exactly one caller\n", get_abstraction_name(fn_node->fn));
141150
return NULL;
142151
}
@@ -166,7 +175,7 @@ static const Node* process(Context* ctx, const Node* node) {
166175
ocallee = ignore_immediate_fn_addr(ocallee);
167176
if (ocallee->tag == Function_TAG) {
168177
CGNode* fn_node = *find_value_dict(const Node*, CGNode*, ctx->graph->fn2cgn, ocallee);
169-
if (get_inlining_heuristic(fn_node).can_be_inlined && is_call_potentially_inlineable(ctx->old_fun, ocallee)) {
178+
if (get_inlining_heuristic(ctx->config, fn_node).can_be_inlined && is_call_potentially_inlineable(ctx->old_fun, ocallee)) {
170179
debugv_print("Inlining call to %s\n", get_abstraction_name(ocallee));
171180
Nodes nargs = rewrite_nodes(&ctx->rewriter, oargs);
172181

@@ -211,7 +220,7 @@ static const Node* process(Context* ctx, const Node* node) {
211220
ocallee = ignore_immediate_fn_addr(ocallee);
212221
if (ocallee->tag == Function_TAG) {
213222
CGNode* fn_node = *find_value_dict(const Node*, CGNode*, ctx->graph->fn2cgn, ocallee);
214-
if (get_inlining_heuristic(fn_node).can_be_inlined) {
223+
if (get_inlining_heuristic(ctx->config, fn_node).can_be_inlined) {
215224
debugv_print("Inlining tail call to %s\n", get_abstraction_name(ocallee));
216225
Nodes nargs = rewrite_nodes(&ctx->rewriter, node->payload.tail_call.args);
217226
return inline_call(ctx, ocallee, nargs, NULL);
@@ -231,9 +240,10 @@ static const Node* process(Context* ctx, const Node* node) {
231240
KeyHash hash_node(const Node**);
232241
bool compare_node(const Node**, const Node**);
233242

234-
void opt_simplify_cf(SHADY_UNUSED const CompilerConfig* config, Module* src, Module* dst) {
243+
void opt_simplify_cf(const CompilerConfig* config, Module* src, Module* dst) {
235244
Context ctx = {
236245
.rewriter = create_rewriter(src, dst, (RewriteNodeFn) process),
246+
.config = config,
237247
.graph = NULL,
238248
.fun = NULL,
239249
.inlined_call = NULL,

0 commit comments

Comments
 (0)