[llvm-commits] CVS: llvm/tools/lli/lli.cpp

Reid Spencer reid at x10sys.com
Sat Mar 3 10:22:01 PST 2007



Changes in directory llvm/tools/lli:

lli.cpp updated: 1.67 -> 1.68
---
Log message:

1. Handle errors around the ModuleProvider. This is necessary since it is
   reading bytecode. 
2. The interpreter can delete the ModuleProvider and replace it with 
   another so don't depend on it being around after the EE is created.
3. Don't just run llvm_shutdown on exit but actually delete the EE as well.
   This cleans up a vast amount of memory (but not all) that EE retained
   through exit.


---
Diffs of the changes:  (+23 -11)

 lli.cpp |   34 +++++++++++++++++++++++-----------
 1 files changed, 23 insertions(+), 11 deletions(-)


Index: llvm/tools/lli/lli.cpp
diff -u llvm/tools/lli/lli.cpp:1.67 llvm/tools/lli/lli.cpp:1.68
--- llvm/tools/lli/lli.cpp:1.67	Wed Feb  7 15:41:02 2007
+++ llvm/tools/lli/lli.cpp	Sat Mar  3 12:21:44 2007
@@ -54,11 +54,18 @@
                    cl::desc("Disable emission of core files if possible"));
 }
 
+static ExecutionEngine *EE = 0;
+
+static void do_shutdown() {
+  delete EE;
+  llvm_shutdown();
+}
+
 //===----------------------------------------------------------------------===//
 // main Driver function
 //
 int main(int argc, char **argv, char * const *envp) {
-  atexit(llvm_shutdown);  // Call llvm_shutdown() on exit.
+  atexit(do_shutdown);  // Call llvm_shutdown() on exit.
   try {
     cl::ParseCommandLineOptions(argc, argv,
                                 " llvm interpreter & dynamic compiler\n");
@@ -70,22 +77,27 @@
     
     // Load the bytecode...
     std::string ErrorMsg;
-    ModuleProvider *MP = 0;
-    MP = getBytecodeModuleProvider(InputFile, 
-                                   Compressor::decompressToNewBuffer,
-                                   &ErrorMsg);
+    ModuleProvider *MP = getBytecodeModuleProvider(InputFile, 
+                                              Compressor::decompressToNewBuffer,
+                                              &ErrorMsg);
     if (!MP) {
-      std::cerr << "Error loading program '" << InputFile << "': "
+      std::cerr << argv[0] << ": error loading program '" << InputFile << "': "
                 << ErrorMsg << "\n";
       exit(1);
     }
 
+    // Get the module as the MP could go away once EE takes over.
+    Module *Mod = MP->getModule();
+
     // If we are supposed to override the target triple, do so now.
     if (!TargetTriple.empty())
-      MP->getModule()->setTargetTriple(TargetTriple);
+      Mod->setTargetTriple(TargetTriple);
     
-    ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter);
-    assert(EE &&"Couldn't create an ExecutionEngine, not even an interpreter?");
+    EE = ExecutionEngine::create(MP, ForceInterpreter, &ErrorMsg);
+    if (!EE && !ErrorMsg.empty()) {
+      std::cerr << argv[0] << ":error creating EE: " << ErrorMsg << "\n";
+      exit(1);
+    }
 
     // If the user specifically requested an argv[0] to pass into the program,
     // do it now.
@@ -106,7 +118,7 @@
     // using the contents of Args to determine argc & argv, and the contents of
     // EnvVars to determine envp.
     //
-    Function *Fn = MP->getModule()->getFunction("main");
+    Function *Fn = Mod->getFunction("main");
     if (!Fn) {
       std::cerr << "'main' function not found in module.\n";
       return -1;
@@ -123,7 +135,7 @@
     
     // If the program didn't explicitly call exit, call exit now, for the
     // program. This ensures that any atexit handlers get called correctly.
-    Constant *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
+    Constant *Exit = Mod->getOrInsertFunction("exit", Type::VoidTy,
                                                           Type::Int32Ty, NULL);
     if (Function *ExitF = dyn_cast<Function>(Exit)) {
       std::vector<GenericValue> Args;






More information about the llvm-commits mailing list