如何使用LLVM C Api / llvm-fs绑定添加元数据节点 [英] How to add metadata nodes using the LLVM C Api/llvm-fs bindings

查看:233
本文介绍了如何使用LLVM C Api / llvm-fs绑定添加元数据节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将元数据节点添加到程序中,或者添加到指令中,或者作为全局元数据。我如何用LLVM C API这样做?它现在提供了一个功能 LLVMAddNamedMetadataOperand (位于此问题) ),但我似乎不能看到如何使用它。这必须与 llvm-fs 中的 addNamedMetadataOperand a>绑定。我试过这个:

  addNamedMetadataOperand myModulefoobar(mDStringcat3u)

希望它创建一些名为 foobar 的元数据节点,但它不工作 - 抱怨铸造错误。我想可能你应该在一条指令中使用 addNamedMetadataOperand ,所以我试过:

  let ret = buildRet bldr(constInt i32 0UL)
addNamedMetadataOperand myModulefoobarret

但是它不喜欢这个。

我添加了两个新的F#友好的函数:mdNode和mdNodeInContext在此提交。使用该提交,我可以修改您的示例代码:

 打开LLVM.Core 
打开LLVM.Generated.Core
open LLVM.Generated.BitWriter

let i32 = int32Type()
let i32zero = constInt i32 0UL false

[< EntryPoint>]
let main argv =
//设置模块/函数
let module_ = moduleCreateWithNamefoobar
//让上下文= getModuleContext module_
let funcTy = functionType i32 [ ||]
let func = addFunction module_mainfuncTy
let bldr = createBuilder()

let entry = appendBasicBlock funcentry
positionBuilderAtEnd bldr entry

//创建Metadata节点并尝试将其附加到ret
// let mdnode = mDStringInContext contextbazquux7u
let mdstring = mDStringbazquux7u
let ret = buildRet bldr i32zero
//从http://llvm.org/docs/doxygen/html/classllvm_1_1LLVMContext.html
// MD_dbg = 0,MD_tbaa = 1,MD_prof = 2, MD_fpmath = 3,MD_range = 4,MD_tbaa_struct = 5
//此处不存在
// setMetadata ret 0u mdnode
let myMDName =my_MD_kind
setMetadata ret(getMDKindID myMDName myMDName.Length))(mdNode [| mdstring |])

//将位元码储存到档案
writeBitcodeToFile module_metadatatest.bc
/ pre>

其中给出了位码:

  ModuleID ='metadatatest.bc'

定义i32 @main(){
条目:
ret i32 0,!my_MD_kind!0
}
b $ b!0 = metadata!{metadata!bazquux}



我使用getMDKindID而不是一个的预定义MD类型,因为当我使用0u我没有得到没有元数据输出。我没有深入了解为什么,但从查看 http://llvm.org/docs/LangRef .html#metadata 似乎预定义的元数据类型有一些约束,它被应用到的指令不满足。无论如何,让我知道,如果你看到更多的问题。它不是我目前使用的API的一部分,但我希望它尽可能好地工作。


I'm trying to add metadata nodes to a program, either onto the instructions or as global metadata. How do I do this with the LLVM C API? It now provides a function LLVMAddNamedMetadataOperand (as found from this question) but I can't seem to see how to use it. This is bound to addNamedMetadataOperand in the llvm-fs bindings. I tried this:

addNamedMetadataOperand myModule "foobar" (mDString "cat" 3u)

expecting it to make some metadata node called foobar but it doesn't work - complains about cast errors. I thought maybe you were supposed to use addNamedMetadataOperand on an instruction, so I tried:

let ret = buildRet bldr (constInt i32 0UL)
addNamedMetadataOperand myModule "foobar" ret

but it didn't like this either.

解决方案

I added two new "F# friendly functions": mdNode and mdNodeInContext in this commit. With that commit I can modify your example code to:

open LLVM.Core
open LLVM.Generated.Core
open LLVM.Generated.BitWriter

let i32 = int32Type ()
let i32zero = constInt i32 0UL false

[<EntryPoint>]
let main argv =
    // Set up the module/function
    let module_ = moduleCreateWithName "foobar"
    //let context = getModuleContext module_
    let funcTy = functionType i32 [||]
    let func = addFunction module_ "main" funcTy
    let bldr = createBuilder ()

    let entry = appendBasicBlock func "entry"
    positionBuilderAtEnd bldr entry

    // Make a Metadata node and try and attach it to a ret
    //let mdnode = mDStringInContext context "bazquux" 7u
    let mdstring = mDString "bazquux" 7u
    let ret = buildRet bldr i32zero
    // From http://llvm.org/docs/doxygen/html/classllvm_1_1LLVMContext.html
    // MD_dbg = 0, MD_tbaa = 1, MD_prof = 2, MD_fpmath = 3, MD_range = 4, MD_tbaa_struct = 5
    // Fails here
    //setMetadata ret 0u mdnode
    let myMDName = "my_MD_kind"
    setMetadata ret (getMDKindID myMDName (uint32 myMDName.Length)) (mdNode [|mdstring|])

    // Save bitcode to file
    writeBitcodeToFile module_ "metadatatest.bc"

Which gives the bitcode:

; ModuleID = 'metadatatest.bc'

define i32 @main() {
entry:
  ret i32 0, !my_MD_kind !0
}

!0 = metadata !{metadata !"bazquux"}

I used getMDKindID rather than one of the pre-defined MD kinds because when I was using 0u I was getting no metadata output. I haven't looked deep into why but from looking at http://llvm.org/docs/LangRef.html#metadata it seems that the predefined metadata types have some constraints that the instruction it was applied to wasn't meeting. Anyhow, let me know if you see more problems with this. It's not a part of the API that I'm using at the moment but I do want it to work as well as possible.

这篇关于如何使用LLVM C Api / llvm-fs绑定添加元数据节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆