-
Notifications
You must be signed in to change notification settings - Fork 115
/
Inliner.cs
157 lines (136 loc) · 5.44 KB
/
Inliner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// -----------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2016-2019 Marcel Koester
// www.ilgpu.net
//
// File: Inliner.cs
//
// This file is part of ILGPU and is distributed under the University of
// Illinois Open Source License. See LICENSE.txt for details
// -----------------------------------------------------------------------------
using ILGPU.Frontend;
using ILGPU.IR.Analyses;
using ILGPU.IR.Values;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace ILGPU.IR.Transformations
{
/// <summary>
/// Represents a function inliner.
/// </summary>
public sealed class Inliner : OrderedTransformation
{
#region Constants
/// <summary>
/// The maximum number of IL instructions to inline.
/// </summary>
private const int MaxNumILInstructionsToInline = 32;
#endregion
#region Static
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void SetupInliningAttributes(
IRContext context,
Method method,
DisassembledMethod disassembledMethod)
{
// Check whether we are allowed to inline this method
if (context.HasFlags(ContextFlags.NoInlining))
return;
if (method.HasSource)
{
var source = method.Source;
if ((source.MethodImplementationFlags & MethodImplAttributes.NoInlining)
== MethodImplAttributes.NoInlining)
return;
if ((source.MethodImplementationFlags & MethodImplAttributes.AggressiveInlining)
== MethodImplAttributes.AggressiveInlining ||
source.Module.Name == Context.FullAssemblyModuleName)
method.AddFlags(MethodFlags.Inline);
}
// Evaluate a simple inlining heuristic
if (context.HasFlags(ContextFlags.AggressiveInlining) ||
disassembledMethod.Instructions.Length <= MaxNumILInstructionsToInline)
{
method.AddFlags(MethodFlags.Inline);
}
}
#endregion
/// <summary>
/// Constructs a new inliner that inlines all methods marked with
/// <see cref="MethodFlags.Inline"/> flags.
/// </summary>
public Inliner() { }
/// <summary cref="OrderedTransformation.PerformTransformation{TScopeProvider}(Method.Builder, Landscape, Landscape{object}.Entry, TScopeProvider)"/>
protected override bool PerformTransformation<TScopeProvider>(
Method.Builder builder,
Landscape landscape,
Landscape.Entry current,
TScopeProvider scopeProvider)
{
var processed = new HashSet<BasicBlock>();
var toProcess = new Stack<BasicBlock>();
bool result = false;
var currentBlock = current.Scope.EntryBlock;
while (true)
{
if (processed.Add(currentBlock))
{
if (result = InlineCalls(
builder,
current,
scopeProvider,
ref currentBlock))
{
result = true;
continue;
}
var successors = currentBlock.Successors;
if (successors.Length > 0)
{
currentBlock = successors[0];
for (int i = 1, e = successors.Length; i < e; ++i)
toProcess.Push(successors[1]);
continue;
}
}
if (toProcess.Count < 1)
break;
currentBlock = toProcess.Pop();
}
return result;
}
/// <summary>
/// Tries to inline method calls.
/// </summary>
/// <param name="builder">The current method builder.</param>
/// <param name="caller">The parent caller entry.</param>
/// <param name="scopeProvider">The scope provider.</param>
/// <param name="currentBlock">The current block (may be modified).</param>
/// <returns>True, in case of an inlined call.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool InlineCalls<TScopeProvider>(
Method.Builder builder,
Landscape.Entry caller,
TScopeProvider scopeProvider,
ref BasicBlock currentBlock)
where TScopeProvider : IScopeProvider
{
foreach (var valueEntry in currentBlock)
{
if (!(valueEntry.Value is MethodCall call))
continue;
if (call.Target.HasFlags(MethodFlags.Inline))
{
var targetScope = scopeProvider[call.Target];
var blockBuilder = builder[currentBlock];
var tempBlock = blockBuilder.SpecializeCall(call, targetScope);
// We can continue our search in the temp block
currentBlock = tempBlock.BasicBlock;
return true;
}
}
return false;
}
}
}