pombreda / llvm-py

Automatically exported from code.google.com/p/llvm-py
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

calling LLVM intrinsic function fails #17

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
  I've been trying to get llvm-py to compile on my Mac (OS X 10.4.11, Python 2.5, gcc 4.0.1). 
While it's not listed as a supported platform, I was curious if it would work.

  I installed LLVM 2.4 without a problem. I installed llvm-py from version control and had a 
compile problem. Here's the patch to get it to compile, but I don't know if 
it's the right solution.

Index: extra.cpp
===========================================================
========
--- extra.cpp   (revision 63)
+++ extra.cpp   (working copy)
@@ -316,7 +316,7 @@

     llvm::Module *modulep;
     llvm::ParseError error;
-    if (!(modulep = llvm::ParseAssemblyString(asmtext, NULL, error))) {
+    if (!(modulep = llvm::ParseAssemblyString(asmtext, NULL, &error))) {
         *out = strdup(error.getRawMessage().c_str());
         return NULL;
     }

With that patch, the code compiles. I ran "testall.py" and it passed all tests.

I was curious about the ctpop intrinsic functions, so I tried to write 
something which uses 
intrinsics. That did not work. Here's my code

# Import the llvm-py modules.
from llvm import *
from llvm.core import *
from llvm.ee import *

# Create a module, as in the previous example.
my_module = Module.new('my_module')
ty_int = Type.int()   # by default 32 bits
ty_func = Type.function(ty_int, [ty_int])
f_popcount = my_module.add_function(ty_func, "popcount")
f_popcount.args[0].name = "x"
x = f_popcount.args[0]
block = f_popcount.append_basic_block("entry")
builder = Builder.new(block)

# get a reference to the llvm.bswap intrinsic
ctpop = Function.intrinsic(my_module, INTR_CTPOP, [ty_int])

popcnt = builder.call(ctpop, [x], "popcnt")
builder.ret(popcnt)

and the error message (in builder.call)

Instructions.cpp:289: failed assertion `(NumParams == FTy->getNumParams() || 
(FTy-
>isVarArg() && NumParams > FTy->getNumParams())) && "Calling a function with 
bad 
signature!"'
Abort

I went back to 'test/intrinsic.py' and ran that, only to get the same error 
message.

I then decided that perhaps SVN was too cutting edge, so I installed 
llvm-py-0.5 . That compiled 
without a hitch, but when I ran 'testall.py' I got

Traceback (most recent call last):
  File "testall.py", line 571, in <module>
    main()
  File "testall.py", line 566, in main
    do_llvm_core()
  File "testall.py", line 466, in do_llvm_core
    do_module()
  File "testall.py", line 53, in do_module
    m = Module.new('test')
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-
packages/llvm/core.py", line 843, in new
    return Module(_core.LLVMModuleCreateWithName(id))
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-
packages/llvm/core.py", line 880, in __init__
    _report_new_module(ptr, self)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-
packages/llvm/core.py", line 809, in _report_new_module
    __all_modules[_core.PyCObjectVoidPtrToPyLong(ptr)] = obj
AttributeError: 'module' object has no attribute 'PyCObjectVoidPtrToPyLong'

I checked, and llvm._core.PyCObjectVoidPtrToPyLong does not exist. It doesn't 
even exist in the 
source code for _core.c. I copied the definition from the SVN version into 
_core.c, and with that 
change "testall.py" passed all tests.

However, running "test/intrinsic.py" still gives me the following error message

Instructions.cpp:293: failed assertion `(i >= FTy->getNumParams() || 
FTy->getParamType(i) == 
Params[i]->getType()) && "Calling a function with a bad signature!"'

How do I call an LLVM intrinsic function?

                Andrew
                dalke@dalkescientific.com

Original issue reported on code.google.com by andrewda...@gmail.com on 22 Feb 2009 at 2:44

GoogleCodeExporter commented 9 years ago
SVN r60 and above work only with LLVM 2.5. LLVM 2.5 should be out next week, so 
I
felt it was "okay" to break the trunk with 2.4.

The errors you're getting with llvm-py-0.5 is because the .so/.pyc/.py from the 
SVN
build is still lying around (try deleting them manually from the python site 
packages
directory).

Alternatively, you can use LLVM 2.5 SVN with llvm-py SVN.

Original comment by mdevan.f...@gmail.com on 22 Feb 2009 at 3:22

GoogleCodeExporter commented 9 years ago
2.5 eh? Okay, building from SVN.

I didn't really consider that there was a version mismatch because the README 
from llvm-py SVN says "This 
package will work only with LLVM 2.4 release version"

Still checking out ... configure ... make ... still making ... installing .. 
rebuild llvm-py ... reverting my patch to 
extra.cpp .. rebuild .. install ... testall.py passes .. intrinsic.py passes .. 
and my ctpop code works!

Thanks for the help!

Original comment by andrewda...@gmail.com on 22 Feb 2009 at 10:04

GoogleCodeExporter commented 9 years ago
Closing.

Original comment by mdevan.f...@gmail.com on 23 Feb 2009 at 5:23