New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interface to LLVM::DataLayout:: methods #360
Comments
This is actually very straightforward to do, because LLVM exposes this through the C API: https://llvm.org/doxygen/group__LLVMCTarget.html However, I don't quite understand exactly what you mean by "not being able to influence code generation". Do you have an example? |
Allocating a tagged union: you want to allocate enough space for the biggest member (this cannot reliably be achieved by inspecting the llvm-hs types who don't know the pointer size and alignment details) |
Ah, I see. I was only thinking about datatypes that LLVM provides, but a tagged union is indeed tricky. You would need to iterate over the union types and statically determine which is largest. Using Sounds like the use case is |
A couple other use-cases:
|
I just ran also in the issue of needing
Basically: I need " |
I added this as a sort of hack some time back https://hackage.haskell.org/package/llvm-hs-pure-9.0.0/docs/LLVM-AST-Constant.html#v:sizeof |
@jfaure I don't think that works? |
That's no problem; It wraps the type with some llvm instructions, the size won't be available for you like with datalayout, but you can use it in the emitted llvm where it will hopefully be constant folded |
@jfaure How then? It just doesn't typecheck..? Also there's no function to go from BTW: here's what I tried:
@andrew-wja I'm not familiar with the codebase but if you give me some high level pointers on how to best approach this, I can try giving it a shot.. |
@luc-tielen I understand what you want to do, but I don't think it's possible with LLVM wants you to pass an integer to the So you really do need to ask libLLVM through an IO operation what the size of the laid-out struct type is. Right now, it isn't possible using |
This got me a little further for my specific case (it looks like some datalayout functionality is exposed in internals?): experiment :: ModuleBuilderT IO ()
experiment = do
s <- typedef "struct_t" $ Just $ StructureType False [i8, i64]
size <- liftIO $ do
s' <- Context.withContext $ flip runEncodeAST $ encodeM s
let dl = defaultDataLayout LittleEndian
DL.withFFIDataLayout dl $ flip DL.getTypeAllocSize s'
print ("size =", size) This snippet works if you use If I could get an up-to-date |
@luc-tielen mixing and matching between the high-level I think what is happening is that the explicit
|
@andrew-wja I tried my hand at it today, but a fix is non-obvious (atleast to me). |
Did another attempt today: experiment :: ModuleBuilderT IO ()
experiment = do
let n = "struct_t"
ty = StructureType False [i8, i64]
s <- typedef n $ Just ty
size <- liftIO $ do
withHostTargetMachine PIC JITDefault None $ \tm -> do
dl <- getTargetMachineDataLayout tm
Context.withContext $ flip runEncodeAST $ do
createType n ty
s' <- encodeM s
liftIO $ DL.withFFIDataLayout dl $ flip DL.getTypeAllocSize s'
print ("size = ", size)
createType :: Name -> Type -> EncodeAST ()
createType n ty = do
(t', n') <- createNamedType n
defineType n n' t'
setNamedType t' ty This prints out size = 16 for me. Not obvious at all, but it works. Now I need to refactor it and figure out a way to nicely integrate it in my compiler 😅. |
It iss realistically necessary to be able to use getPointerSize and getTypeAllocSize (at least) from https://llvm.org/doxygen/classllvm_1_1DataLayout.html
The sizeof as a gep to then end of a nullpointer hack has the disadvantage of not being able to influence code generation.
getTypeAllocSize in particular requires us to pre-convert llvm-hs types to C++ so this may be tricky.
The text was updated successfully, but these errors were encountered: