From fc3f11d899b3fd048cb32e3e8c66b148ed982e27 Mon Sep 17 00:00:00 2001 From: "mdevan.foobar" Date: Mon, 11 Aug 2008 14:21:16 +0000 Subject: [PATCH] Added tests for bitcode. Updated CHANGELOG, inline docs. Switched to absolute imports. Added all passes to extra.{cpp,h}\! git-svn-id: http://llvm-py.googlecode.com/svn/trunk@31 8d1e9007-1d4e-0410-b67e-1979fd6579aa --- CHANGELOG | 2 ++ README | 13 ++++++-- llvm/__init__.py | 11 ++++++- llvm/_util.py | 2 +- llvm/core.py | 6 ++-- llvm/ee.py | 8 ++--- llvm/extra.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ llvm/extra.h | 73 ++++++++++++++++++++++++++++++++++++++++++ llvm/passes.py | 19 +++++++++-- test/testall.py | 17 ++++++++++ 10 files changed, 219 insertions(+), 14 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 4a1a9af..709c516 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ 0.3, in progress: + * Various bug fixes. + * Bitcode support: convert modules to bitcode and vice versa. * Intrinsics added. * JIT Tutorials ported (Sebastien Binet). * GenericValue added. Used by ExecutionEngine.run(). diff --git a/README b/README index 42b79d2..f861a66 100644 --- a/README +++ b/README @@ -10,9 +10,18 @@ Home page: http://mdevan.nfshost.com/llvm-py/ +Versions: +--------- + +This package will work only with LLVM 2.3 release version. If you need llvm-py +for the in-progress LLVM version (2.4), you've to get llvm-py from the SVN +repository -- see the website for more details. + + Quickstart: ----------- - 1. Get 2.3 version of LLVM, build it. 2.2 or earlier will *not* work. +----------- + 1. Get 2.3 version of LLVM, build it. Make sure '--enable-pic' is passed to + LLVM's 'configure'. 2. Unpack llvm-py, build and install: diff --git a/llvm/__init__.py b/llvm/__init__.py index 2cab079..aedeb89 100644 --- a/llvm/__init__.py +++ b/llvm/__init__.py @@ -2,13 +2,15 @@ """ -VERSION = '0.2' +VERSION = '0.3' + #===----------------------------------------------------------------------=== # Exceptions #===----------------------------------------------------------------------=== class LLVMException(Exception): + """Generic LLVM exception.""" def __init__(self, msg=""): Exception.__init__(self, msg) @@ -19,6 +21,13 @@ class LLVMException(Exception): #===----------------------------------------------------------------------=== class Ownable(object): + """Objects that can be owned. + + Modules and Module Providers can be owned, i.e., the responsibility of + destruction of ownable objects can be handed over to other objects. The + llvm.Ownable class represents objects that can be so owned. This class + is NOT intended for public use. + """ def __init__(self, ptr, del_fn): self.ptr = ptr diff --git a/llvm/_util.py b/llvm/_util.py index faab301..94a6614 100644 --- a/llvm/_util.py +++ b/llvm/_util.py @@ -2,7 +2,7 @@ Used only in other modules, not for public use.""" -import core +import llvm.core as core import llvm diff --git a/llvm/core.py b/llvm/core.py index 4afb2fc..1a646be 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -4,9 +4,9 @@ The llvm.core module contains classes and constants required to build the in-memory intermediate representation (IR) data structures.""" -import llvm # top-level, for common stuff -import _core # C wrappers -from _util import * # utility functions +import llvm # top-level, for common stuff +import llvm._core as _core # C wrappers +from llvm._util import * # utility functions #===----------------------------------------------------------------------=== diff --git a/llvm/ee.py b/llvm/ee.py index 3165173..c2e8b32 100644 --- a/llvm/ee.py +++ b/llvm/ee.py @@ -2,10 +2,10 @@ """ -import llvm # top-level, for common stuff -import core # module provider, function etc. -import _core # C wrappers -from _util import * # utility functions +import llvm # top-level, for common stuff +import llvm.core as core # module provider, function etc. +import llvm._core as _core # C wrappers +from llvm._util import * # utility functions #===----------------------------------------------------------------------=== diff --git a/llvm/extra.cpp b/llvm/extra.cpp index 4596fa9..bf78bf0 100644 --- a/llvm/extra.cpp +++ b/llvm/extra.cpp @@ -20,6 +20,14 @@ #include "llvm/Support/CallSite.h" #include "llvm/IntrinsicInst.h" #include "llvm/Analysis/Verifier.h" +// +includes for passes +#include "llvm/PassManager.h" +#include "llvm/Analysis/LoopPass.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/IPO.h" +#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" +#include "llvm/Transforms/Instrumentation.h" +// -includes for passes #include "llvm-c/Core.h" @@ -131,3 +139,77 @@ unsigned char *LLVMGetBitcodeFromModule(LLVMModuleRef M, unsigned *Len) *Len = len; return bytes; } + +/* passes */ + +#define define_pass(P) \ +void LLVMAdd ## P ## Pass (LLVMPassManagerRef PM) { \ + unwrap(PM)->add( create ## P ## Pass ()); \ +} + +define_pass( AggressiveDCE ) +define_pass( ArgumentPromotion ) +define_pass( BlockPlacement ) +define_pass( BreakCriticalEdges ) +define_pass( CodeGenPrepare ) +define_pass( CondPropagation ) +define_pass( ConstantMerge ) +//LLVM-C define_pass( ConstantPropagation ) +define_pass( DeadCodeElimination ) +define_pass( DeadArgElimination ) +define_pass( DeadTypeElimination ) +define_pass( DeadInstElimination ) +define_pass( DeadStoreElimination ) +define_pass( GCSE ) +define_pass( GlobalDCE ) +define_pass( GlobalOptimizer ) +//LLVM-C define_pass( GVN ) +define_pass( GVNPRE ) +define_pass( IndMemRem ) +define_pass( IndVarSimplify ) +define_pass( FunctionInlining ) +define_pass( BlockProfiler ) +define_pass( EdgeProfiler ) +define_pass( FunctionProfiler ) +define_pass( NullProfilerRS ) +define_pass( RSProfiling ) +//LLVM-C define_pass( InstructionCombining ) +/* we support only internalize(true) */ +ModulePass *createInternalizePass() { return llvm::createInternalizePass(true); } +define_pass( Internalize ) +define_pass( IPConstantPropagation ) +define_pass( IPSCCP ) +define_pass( JumpThreading ) +define_pass( LCSSA ) +define_pass( LICM ) +define_pass( LoopDeletion ) +define_pass( LoopExtractor ) +define_pass( SingleLoopExtractor ) +define_pass( LoopIndexSplit ) +define_pass( LoopStrengthReduce ) +define_pass( LoopRotate ) +define_pass( LoopUnroll ) +define_pass( LoopUnswitch ) +define_pass( LoopSimplify ) +define_pass( LowerAllocations ) +define_pass( LowerInvoke ) +define_pass( LowerSetJmp ) +define_pass( LowerSwitch ) +//LLVM-C define_pass( PromoteMemoryToRegister ) +define_pass( MemCpyOpt ) +define_pass( UnifyFunctionExitNodes ) +define_pass( PredicateSimplifier ) +define_pass( PruneEH ) +define_pass( RaiseAllocations ) +//LLVM-C define_pass( Reassociate ) +define_pass( DemoteRegisterToMemory ) +define_pass( ScalarReplAggregates ) +define_pass( SCCP ) +define_pass( SimplifyLibCalls ) +//LLVM-C define_pass( CFGSimplification ) +define_pass( StripSymbols ) +define_pass( StripDeadPrototypes ) +define_pass( StructRetPromotion ) +define_pass( TailCallElimination ) +define_pass( TailDuplication ) + diff --git a/llvm/extra.h b/llvm/extra.h index c90f93a..efd8163 100644 --- a/llvm/extra.h +++ b/llvm/extra.h @@ -46,13 +46,86 @@ LLVMValueRef LLVMBuildRetMultiple(LLVMBuilderRef, LLVMValueRef *Values, LLVMValueRef LLVMBuildGetResult(LLVMBuilderRef, LLVMValueRef V, unsigned Index, const char *Name); +/* intrinsics */ + LLVMValueRef LLVMGetIntrinsic(LLVMModuleRef B, int ID, LLVMTypeRef *Types, unsigned Count); +/* bitcode related */ + LLVMModuleRef LLVMGetModuleFromBitcode(const char *BC, unsigned Len, char **OutMessage); unsigned char *LLVMGetBitcodeFromModule(LLVMModuleRef M, unsigned *Len); +/* passes */ + +#define declare_pass(P) \ + void LLVMAdd ## P ## Pass (LLVMPassManagerRef PM); + +declare_pass( AggressiveDCE ) +declare_pass( ArgumentPromotion ) +declare_pass( BlockPlacement ) +declare_pass( BreakCriticalEdges ) +declare_pass( CodeGenPrepare ) +declare_pass( CondPropagation ) +declare_pass( ConstantMerge ) +//LLVM-C declare_pass( ConstantPropagation ) +declare_pass( DeadCodeElimination ) +declare_pass( DeadArgElimination ) +declare_pass( DeadTypeElimination ) +declare_pass( DeadInstElimination ) +declare_pass( DeadStoreElimination ) +declare_pass( GCSE ) +declare_pass( GlobalDCE ) +declare_pass( GlobalOptimizer ) +//LLVM-C declare_pass( GVN ) +declare_pass( GVNPRE ) +declare_pass( IndMemRem ) +declare_pass( IndVarSimplify ) +declare_pass( FunctionInlining ) +declare_pass( BlockProfiler ) +declare_pass( EdgeProfiler ) +declare_pass( FunctionProfiler ) +declare_pass( NullProfilerRS ) +declare_pass( RSProfiling ) +//LLVM-C declare_pass( InstructionCombining ) +declare_pass( Internalize ) +declare_pass( IPConstantPropagation ) +declare_pass( IPSCCP ) +declare_pass( JumpThreading ) +declare_pass( LCSSA ) +declare_pass( LICM ) +declare_pass( LoopDeletion ) +declare_pass( LoopExtractor ) +declare_pass( SingleLoopExtractor ) +declare_pass( LoopIndexSplit ) +declare_pass( LoopStrengthReduce ) +declare_pass( LoopRotate ) +declare_pass( LoopUnroll ) +declare_pass( LoopUnswitch ) +declare_pass( LoopSimplify ) +declare_pass( LowerAllocations ) +declare_pass( LowerInvoke ) +declare_pass( LowerSetJmp ) +declare_pass( LowerSwitch ) +//LLVM-C declare_pass( PromoteMemoryToRegister ) +declare_pass( MemCpyOpt ) +declare_pass( UnifyFunctionExitNodes ) +declare_pass( PredicateSimplifier ) +declare_pass( PruneEH ) +declare_pass( RaiseAllocations ) +//LLVM-C declare_pass( Reassociate ) +declare_pass( DemoteRegisterToMemory ) +declare_pass( ScalarReplAggregates ) +declare_pass( SCCP ) +declare_pass( SimplifyLibCalls ) +//LLVM-C declare_pass( CFGSimplification ) +declare_pass( StripSymbols ) +declare_pass( StripDeadPrototypes ) +declare_pass( StructRetPromotion ) +declare_pass( TailCallElimination ) +declare_pass( TailDuplication ) + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/llvm/passes.py b/llvm/passes.py index b976926..0f5d74a 100644 --- a/llvm/passes.py +++ b/llvm/passes.py @@ -1,10 +1,23 @@ """Pass managers and passes. +This module provides the LLVM pass managers and the passes themselves. +Passes that are currently available are: + + Simple Constant Propagation + Combine Redundant Instructions + Promote Memory to Register + Demote all values to stack slots + Reassociate expressions + Global Value Numbering + Simplify the CFG + +See http://www.llvm.org/docs/Passes.html for the full list of passes +available in LLVM. """ -import ee # target data -import _core # C wrappers -from _util import * # utility functions +import llvm.ee as ee # target data +import llvm._core as _core # C wrappers +from llvm._util import * # utility functions # passes diff --git a/test/testall.py b/test/testall.py index 1858bf1..f557e87 100644 --- a/test/testall.py +++ b/test/testall.py @@ -34,6 +34,23 @@ def do_module(): except LLVMException: pass + class strstream(object): + def __init__(self): + self.s = '' + + def write(self, data): + self.s += data + + def read(self): + return self.s + + ss = strstream() + m2 = Module.new('test') + m2.add_type_name('myint', ti) + m2.to_bitcode(ss) + m3 = Module.from_bitcode(ss) + t = m2 == m3 + def do_type(): print " Testing class Type"