Allocate the module provider in the Kaleidoscope code on the heap, not the stack, so that it can be properly deleted. Also update the tutorial with the new code. This fixes PR4762, hopefully better than the last time.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@80138 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Kleckner
2009-08-26 20:58:25 +00:00
parent 7309be6735
commit 60130f0e90
5 changed files with 169 additions and 170 deletions

View File

@@ -171,26 +171,28 @@ add a set of optimizations to run. The code looks like this:</p>
<div class="doc_code"> <div class="doc_code">
<pre> <pre>
ExistingModuleProvider OurModuleProvider(TheModule); ExistingModuleProvider *OurModuleProvider =
FunctionPassManager OurFPM(&amp;OurModuleProvider); new ExistingModuleProvider(TheModule);
// Set up the optimizer pipeline. Start with registering info about how the FunctionPassManager OurFPM(OurModuleProvider);
// target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
// Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions.
OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions.
OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass());
// Set the global so the code gen can use this. // Set up the optimizer pipeline. Start with registering info about how the
TheFPM = &amp;OurFPM; // target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
// Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions.
OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions.
OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass());
// Run the main "interpreter loop" now. // Set the global so the code gen can use this.
MainLoop(); TheFPM = &amp;OurFPM;
// Run the main "interpreter loop" now.
MainLoop();
</pre> </pre>
</div> </div>
@@ -298,8 +300,8 @@ by adding a global variable and a call in <tt>main</tt>:</p>
... ...
int main() { int main() {
.. ..
<b>// Create the JIT. <b>// Create the JIT. This takes ownership of the module and module provider.
TheExecutionEngine = EngineBuilder(TheModule).create();</b> TheExecutionEngine = EngineBuilder(OurModuleProvider).create();</b>
.. ..
} }
</pre> </pre>
@@ -1077,36 +1079,36 @@ int main() {
// Make the module, which holds all the code. // Make the module, which holds all the code.
TheModule = new Module("my cool jit", getGlobalContext()); TheModule = new Module("my cool jit", getGlobalContext());
// Create the JIT. ExistingModuleProvider *OurModuleProvider =
TheExecutionEngine = EngineBuilder(TheModule).create(); new ExistingModuleProvider(TheModule);
{ // Create the JIT. This takes ownership of the module and module provider.
ExistingModuleProvider OurModuleProvider(TheModule); TheExecutionEngine = EngineBuilder(OurModuleProvider).create();
FunctionPassManager OurFPM(&amp;OurModuleProvider);
// Set up the optimizer pipeline. Start with registering info about how the FunctionPassManager OurFPM(OurModuleProvider);
// target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
// Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions.
OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions.
OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass());
// Set the global so the code gen can use this. // Set up the optimizer pipeline. Start with registering info about how the
TheFPM = &amp;OurFPM; // target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
// Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions.
OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions.
OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass());
// Run the main "interpreter loop" now. // Set the global so the code gen can use this.
MainLoop(); TheFPM = &amp;OurFPM;
TheFPM = 0; // Run the main "interpreter loop" now.
MainLoop();
// Print out all of the generated code. TheFPM = 0;
TheModule-&gt;dump();
} // Free module provider (and thus the module) and pass manager. // Print out all of the generated code.
TheModule-&gt;dump();
return 0; return 0;
} }

View File

@@ -1711,35 +1711,36 @@ int main() {
// Make the module, which holds all the code. // Make the module, which holds all the code.
TheModule = new Module("my cool jit", getGlobalContext()); TheModule = new Module("my cool jit", getGlobalContext());
// Create the JIT. ExistingModuleProvider *OurModuleProvider =
TheExecutionEngine = EngineBuilder(TheModule).create(); new ExistingModuleProvider(TheModule);
{ // Create the JIT. This takes ownership of the module and module provider.
ExistingModuleProvider OurModuleProvider(TheModule); TheExecutionEngine = EngineBuilder(OurModuleProvider).create();
FunctionPassManager OurFPM(&amp;OurModuleProvider);
// Set up the optimizer pipeline. Start with registering info about how the FunctionPassManager OurFPM(OurModuleProvider);
// target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
// Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions.
OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions.
OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass());
// Set the global so the code gen can use this.
TheFPM = &amp;OurFPM;
// Run the main "interpreter loop" now. // Set up the optimizer pipeline. Start with registering info about how the
MainLoop(); // target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
// Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions.
OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions.
OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass());
TheFPM = 0; // Set the global so the code gen can use this.
TheFPM = &amp;OurFPM;
// Print out all of the generated code. // Run the main "interpreter loop" now.
TheModule-&gt;dump(); MainLoop();
} // Free module provider (and thus the module) and pass manager.
TheFPM = 0;
// Print out all of the generated code.
TheModule-&gt;dump();
return 0; return 0;
} }

View File

@@ -1750,35 +1750,36 @@ int main() {
// Make the module, which holds all the code. // Make the module, which holds all the code.
TheModule = new Module("my cool jit", getGlobalContext()); TheModule = new Module("my cool jit", getGlobalContext());
// Create the JIT. ExistingModuleProvider *OurModuleProvider =
TheExecutionEngine = EngineBuilder(TheModule).create(); new ExistingModuleProvider(TheModule);
{ // Create the JIT. This takes ownership of the module and module provider.
ExistingModuleProvider OurModuleProvider(TheModule); TheExecutionEngine = EngineBuilder(OurModuleProvider).create();
FunctionPassManager OurFPM(&amp;OurModuleProvider);
// Set up the optimizer pipeline. Start with registering info about how the FunctionPassManager OurFPM(OurModuleProvider);
// target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
// Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions.
OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions.
OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass());
// Set the global so the code gen can use this.
TheFPM = &amp;OurFPM;
// Run the main "interpreter loop" now. // Set up the optimizer pipeline. Start with registering info about how the
MainLoop(); // target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
// Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions.
OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions.
OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass());
TheFPM = 0; // Set the global so the code gen can use this.
TheFPM = &amp;OurFPM;
// Print out all of the generated code. // Run the main "interpreter loop" now.
TheModule-&gt;dump(); MainLoop();
} // Free module provider (and thus the module) and pass manager.
TheFPM = 0;
// Print out all of the generated code.
TheModule-&gt;dump();
return 0; return 0;
} }

View File

@@ -2102,39 +2102,36 @@ int main() {
// Make the module, which holds all the code. // Make the module, which holds all the code.
TheModule = new Module("my cool jit", getGlobalContext()); TheModule = new Module("my cool jit", getGlobalContext());
// Create the JIT. ExistingModuleProvider *OurModuleProvider =
TheExecutionEngine = EngineBuilder(TheModule).create(); new ExistingModuleProvider(TheModule);
{ // Create the JIT. This takes ownership of the module and module provider.
ExistingModuleProvider OurModuleProvider(TheModule); TheExecutionEngine = EngineBuilder(OurModuleProvider).create();
FunctionPassManager OurFPM(&amp;OurModuleProvider);
// Set up the optimizer pipeline. Start with registering info about how the FunctionPassManager OurFPM(OurModuleProvider);
// target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
// Promote allocas to registers.
OurFPM.add(createPromoteMemoryToRegisterPass());
// Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions.
OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions.
OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass());
// Set the global so the code gen can use this. // Set up the optimizer pipeline. Start with registering info about how the
TheFPM = &amp;OurFPM; // target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
// Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions.
OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions.
OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass());
// Run the main "interpreter loop" now. // Set the global so the code gen can use this.
MainLoop(); TheFPM = &amp;OurFPM;
TheFPM = 0; // Run the main "interpreter loop" now.
MainLoop();
// Print out all of the generated code. TheFPM = 0;
TheModule-&gt;dump();
} // Free module provider (and thus the module) and pass manager. // Print out all of the generated code.
TheModule-&gt;dump();
return 0; return 0;
} }

View File

@@ -1108,42 +1108,40 @@ int main() {
// Make the module, which holds all the code. // Make the module, which holds all the code.
TheModule = new Module("my cool jit", Context); TheModule = new Module("my cool jit", Context);
{ ExistingModuleProvider *OurModuleProvider =
ExistingModuleProvider OurModuleProvider(TheModule); new ExistingModuleProvider(TheModule);
// Create the JIT. // Create the JIT. This takes ownership of the module and module provider.
TheExecutionEngine = EngineBuilder(&OurModuleProvider).create(); TheExecutionEngine = EngineBuilder(OurModuleProvider).create();
FunctionPassManager OurFPM(&OurModuleProvider); FunctionPassManager OurFPM(OurModuleProvider);
// Set up the optimizer pipeline. Start with registering info about how the // Set up the optimizer pipeline. Start with registering info about how the
// target lays out data structures. // target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData())); OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
// Promote allocas to registers. // Promote allocas to registers.
OurFPM.add(createPromoteMemoryToRegisterPass()); OurFPM.add(createPromoteMemoryToRegisterPass());
// Do simple "peephole" optimizations and bit-twiddling optzns. // Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass()); OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions. // Reassociate expressions.
OurFPM.add(createReassociatePass()); OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions. // Eliminate Common SubExpressions.
OurFPM.add(createGVNPass()); OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc). // Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass()); OurFPM.add(createCFGSimplificationPass());
OurFPM.doInitialization(); OurFPM.doInitialization();
// Set the global so the code gen can use this. // Set the global so the code gen can use this.
TheFPM = &OurFPM; TheFPM = &OurFPM;
// Run the main "interpreter loop" now. // Run the main "interpreter loop" now.
MainLoop(); MainLoop();
TheFPM = 0; TheFPM = 0;
// Print out all of the generated code. // Print out all of the generated code.
TheModule->dump(); TheModule->dump();
} // Free module provider (and thus the module) and pass manager.
return 0; return 0;
} }