LLVM 生成for迴圈

2021-10-07 10:03:22 字數 4254 閱讀 4914

1、生成函式

2、使用phi表示式實現for迴圈

3、本例中,kaleidoscopejit原始碼位於./llvm-8.0.1.src/examples/kaleidoscope/include/kaleidoscopejit.h

phi的概念可參考

本例中,生成的llvm ir如下:

define double @myfor(double %a)
phi的邏輯,如果從 myentry 來(比如剛進來),則 %i就是 0,否則%i 的值就是%nextvar(比如迴圈完一次)

**:

#include

"./llvm-8.0.1.src/examples/kaleidoscope/include/kaleidoscopejit.h"

#include

"llvm/adt/apfloat.h"

#include

"llvm/adt/stlextras.h"

#include

"llvm/ir/basicblock.h"

#include

"llvm/ir/constants.h"

#include

"llvm/ir/derivedtypes.h"

#include

"llvm/ir/function.h"

#include

"llvm/ir/irbuilder.h"

#include

"llvm/ir/llvmcontext.h"

#include

"llvm/ir/legacypassmanager.h"

#include

"llvm/ir/module.h"

#include

"llvm/ir/type.h"

#include

"llvm/ir/verifier.h"

#include

"llvm/support/targetselect.h"

#include

"llvm/target/targetmachine.h"

#include

"llvm/transforms/instcombine/instcombine.h"

#include

"llvm/transforms/scalar.h"

#include

"llvm/transforms/scalar/**n.h"

#include

#include

#include

using

namespace std;

using

namespace llvm;

using

namespace llvm::orc;

//llvm items

static llvmcontext thecontext;

static irbuilder<

>

builder

(thecontext)

;static std::unique_ptr themodule;

//jit

static std::unique_ptr thejit;

/* *double myfor(double a)

* * return a

*} *

*/int

main()

//this function's basic block

basicblock *bb = basicblock::

create

(thecontext,

"myentry"

, thefunction)

;//create the loop basicblock

basicblock *loopbb = basicblock::

create

(thecontext,

"loop"

, thefunction)

;//exit the block

basicblock *afterbb = basicblock::

create

(thecontext,

"afterloop"

, thefunction)

; builder.

setinsertpoint

(bb)

;//add goto loopbb in bb

builder.

createbr

(loopbb)

;//start with loopbb

builder.

setinsertpoint

(loopbb)

;//start with 0

value *startval = constantfp::

get(thecontext,

apfloat

(0.0))

;//step is 1

value *stepval = constantfp::

get(thecontext,

apfloat

(1.0))

;//local variable which name is a

phinode *variable = builder.

createphi

(type::

getdoublety

(thecontext),1

,"i");

//if it's from start block, set variable 0

variable-

>

addincoming

(startval, bb)

;//do the body then do cond

//emit loop body in loopbb

//body: arg_a += 1

value *ret = builder.

createfadd

(innerargs[0]

, constantfp::

get(thecontext,

apfloat

(2.0))

,"a");

//do the cond,if variable >= 2 then break(goto afterbb)

value *nextvar = builder.

createfadd

(stepval, variable,

"nextvar");

//if variable < 10 then goto loopbb or gotot afterbb

value *cond = builder.

createfcmpult

(variable, constantfp::

get(thecontext,

apfloat

(10.0))

,"cmptmp");

builder.

createcondbr

(cond, loopbb, afterbb)

; builder.

setinsertpoint

(afterbb)

; variable-

>

addincoming

(nextvar, loopbb)

; builder.

createret

(ret)

; thefunction-

>

print

(errs()

);//using jit to run this code

auto h = thejit-

>

addmodule

(std::

move

(themodule));

auto exprsymbol = thejit-

>

findsymbol

("myfor");

double

(*myfor)

(double)=

(double(*

)(double))

(intptr_t)

cantfail

(exprsymbol.

getaddress()

);cout <<

myfor(40

)<}

llvm安裝小結

之前在網上找了一篇指導安裝的文章 但是在test階段執行lli命令時老是會提醒段錯誤 這個毛病比較要命,而且不好除錯,花了好幾個晚上都沒有除錯出來 可能我用的是xshell linux客戶端版本的緣故。而且既然llvm課題組已經出了自己的前端clang並且已經確定了其正確性了,而且clang的安裝過...

編譯原理 llvm

程式設計師的人生就是,將工作交給機器來做。編譯器的工作就是,將程式交給機器來做。我昨天看了幾個llvm的簡介,大概意思就是,有一群歪果仁學習編譯原理的時候,發現gcc太高冷了,讓人望而生畏。於是他們就折騰了乙個llvm。雖然llvm是相對gcc還比較年輕的,但llvm的歷史還是要從我讀幼兒園的時候說...

通過 LLVM 加速 Python numba

numba是cython的競爭專案。同樣的,numba把python原始碼通過llvmpy生成jit後的.so檔案來加速。不同點 1.numba是以jit為主的,加速對原始碼的侵入性較小。2.cython則重點在加速高效能python模組的開發上,不依賴llvmpy專案。3.numba還很不成熟,目...