1 #include "llvm/Analysis/CallGraph.h"
2 #include "llvm/AsmParser/Parser.h"
3 #include "llvm/Config/config.h"
4 #include "llvm/Passes/PassBuilder.h"
5 #include "llvm/Passes/PassPlugin.h"
6 #include "llvm/Support/CommandLine.h"
7 #include "llvm/Support/raw_ostream.h"
8 #include "llvm/Testing/Support/Error.h"
9 #include "gtest/gtest.h"
17 static std::string
libPath(const std::string Name
= "InlineAdvisorPlugin") {
18 const auto &Argvs
= testing::internal::GetArgvs();
20 Argvs
.size() > 0 ? Argvs
[0].c_str() : "PluginInlineAdvisorAnalysisTest";
21 void *Ptr
= (void *)(intptr_t)anchor
;
22 std::string Path
= sys::fs::getMainExecutable(Argv0
, Ptr
);
23 llvm::SmallString
<256> Buf
{sys::path::parent_path(Path
)};
24 sys::path::append(Buf
, (Name
+ LLVM_PLUGIN_EXT
).c_str());
25 return std::string(Buf
.str());
28 // Example of a custom InlineAdvisor that only inlines calls to functions called
30 class FooOnlyInlineAdvisor
: public InlineAdvisor
{
32 FooOnlyInlineAdvisor(Module
&M
, FunctionAnalysisManager
&FAM
,
33 InlineParams Params
, InlineContext IC
)
34 : InlineAdvisor(M
, FAM
, IC
) {}
36 std::unique_ptr
<InlineAdvice
> getAdviceImpl(CallBase
&CB
) override
{
37 if (CB
.getCalledFunction()->getName() == "foo")
38 return std::make_unique
<InlineAdvice
>(this, CB
, getCallerORE(CB
), true);
39 return std::make_unique
<InlineAdvice
>(this, CB
, getCallerORE(CB
), false);
43 static InlineAdvisor
*fooOnlyFactory(Module
&M
, FunctionAnalysisManager
&FAM
,
44 InlineParams Params
, InlineContext IC
) {
45 return new FooOnlyInlineAdvisor(M
, FAM
, Params
, IC
);
48 struct CompilerInstance
{
50 ModulePassManager MPM
;
54 LoopAnalysisManager LAM
;
55 FunctionAnalysisManager FAM
;
56 CGSCCAnalysisManager CGAM
;
57 ModuleAnalysisManager MAM
;
61 // connect the plugin to our compiler instance
63 auto PluginPath
= libPath();
64 ASSERT_NE("", PluginPath
);
65 Expected
<PassPlugin
> Plugin
= PassPlugin::Load(PluginPath
);
66 ASSERT_TRUE(!!Plugin
) << "Plugin path: " << PluginPath
;
67 Plugin
->registerPassBuilderCallbacks(PB
);
68 ASSERT_THAT_ERROR(PB
.parsePassPipeline(MPM
, "dynamic-inline-advisor"),
72 // connect the FooOnlyInlineAdvisor to our compiler instance
75 [&] { return PluginInlineAdvisorAnalysis(fooOnlyFactory
); });
79 IP
= getInlineParams(3, 0);
80 PB
.registerModuleAnalyses(MAM
);
81 PB
.registerCGSCCAnalyses(CGAM
);
82 PB
.registerFunctionAnalyses(FAM
);
83 PB
.registerLoopAnalyses(LAM
);
84 PB
.crossRegisterProxies(LAM
, FAM
, CGAM
, MAM
);
85 MPM
.addPass(ModuleInlinerPass(IP
, InliningAdvisorMode::Default
,
86 ThinOrFullLTOPhase::None
));
90 // Reset the static variable that tracks if the plugin has been registered.
91 // This is needed to allow the test to run multiple times.
92 PluginInlineAdvisorAnalysis::HasBeenRegistered
= false;
96 std::unique_ptr
<Module
> outputM
;
98 // run with the default inliner
99 auto run_default(StringRef IR
) {
100 PluginInlineAdvisorAnalysis::HasBeenRegistered
= false;
101 outputM
= parseAssemblyString(IR
, Error
, Ctx
);
102 MPM
.run(*outputM
, MAM
);
103 ASSERT_TRUE(outputM
);
105 raw_string_ostream o_stream
{output
};
106 outputM
->print(o_stream
, nullptr);
110 // run with the dnamic inliner
111 auto run_dynamic(StringRef IR
) {
112 // note typically the constructor for the DynamicInlineAdvisorAnalysis
113 // will automatically set this to true, we controll it here only to
114 // altenate between the default and dynamic inliner in our test
115 PluginInlineAdvisorAnalysis::HasBeenRegistered
= true;
116 outputM
= parseAssemblyString(IR
, Error
, Ctx
);
117 MPM
.run(*outputM
, MAM
);
118 ASSERT_TRUE(outputM
);
120 raw_string_ostream o_stream
{output
};
121 outputM
->print(o_stream
, nullptr);
126 StringRef TestIRS
[] = {
127 // Simple 3 function inline case
141 // Test that has 5 functions of which 2 are recursive
164 // test with 2 mutually recursive functions and 1 function with a loop
188 // test that has a function that computes fibonacci in a loop, one in a
189 // recurisve manner, and one that calls both and compares them
191 define i32 @fib_loop(i32 %n){
195 store i32 1, i32* %curr
196 store i32 1, i32* %last
200 %i_val = load i32, i32* %i
201 %cmp = icmp slt i32 %i_val, %n
202 br i1 %cmp, label %loop_body, label %loop_end
204 %curr_val = load i32, i32* %curr
205 %last_val = load i32, i32* %last
206 %add = add i32 %curr_val, %last_val
207 store i32 %add, i32* %last
208 store i32 %curr_val, i32* %curr
209 %i_val2 = load i32, i32* %i
210 %add2 = add i32 %i_val2, 1
211 store i32 %add2, i32* %i
214 %curr_val3 = load i32, i32* %curr
218 define i32 @fib_rec(i32 %n){
219 %cmp = icmp eq i32 %n, 0
220 %cmp2 = icmp eq i32 %n, 1
221 %or = or i1 %cmp, %cmp2
222 br i1 %or, label %if_true, label %if_false
227 %call = call i32 @fib_rec(i32 %sub)
228 %sub2 = sub i32 %n, 2
229 %call2 = call i32 @fib_rec(i32 %sub2)
230 %add = add i32 %call, %call2
234 define i32 @fib_check(){
235 %correct = alloca i32
237 store i32 1, i32* %correct
241 %i_val = load i32, i32* %i
242 %cmp = icmp slt i32 %i_val, 10
243 br i1 %cmp, label %loop_body, label %loop_end
245 %i_val2 = load i32, i32* %i
246 %call = call i32 @fib_loop(i32 %i_val2)
247 %i_val3 = load i32, i32* %i
248 %call2 = call i32 @fib_rec(i32 %i_val3)
249 %cmp2 = icmp ne i32 %call, %call2
250 br i1 %cmp2, label %if_true, label %if_false
252 store i32 0, i32* %correct
257 %i_val4 = load i32, i32* %i
258 %add = add i32 %i_val4, 1
259 store i32 %add, i32* %i
262 %correct_val = load i32, i32* %correct
269 // check that loading a plugin works
270 // the plugin being loaded acts identically to the default inliner
271 TEST(PluginInlineAdvisorTest
, PluginLoad
) {
272 #if !defined(LLVM_ENABLE_PLUGINS)
273 // Skip the test if plugins are disabled.
276 CompilerInstance CI
{};
279 for (StringRef IR
: TestIRS
) {
281 std::string default_output
= CI
.output
;
283 std::string dynamic_output
= CI
.output
;
284 ASSERT_EQ(default_output
, dynamic_output
);
288 // check that the behaviour of a custom inliner is correct
289 // the custom inliner inlines all functions that are not named "foo"
290 // this testdoes not require plugins to be enabled
291 TEST(PluginInlineAdvisorTest
, CustomAdvisor
) {
292 CompilerInstance CI
{};
295 for (StringRef IR
: TestIRS
) {
297 CallGraph CGraph
= CallGraph(*CI
.outputM
);
298 for (auto &node
: CGraph
) {
299 for (auto &edge
: *node
.second
) {
302 ASSERT_NE(edge
.second
->getFunction()->getName(), "foo");