ORC實(shí)例總結(jié)
總結(jié)
- 因?yàn)锳PI茫茫多,邏輯上的一些概念需要搞清,編碼時(shí)會(huì)容易很多。
- JIT的運(yùn)行實(shí)體使用LLVMOrcCreateLLJIT可以創(chuàng)建出來(lái),邏輯上的JIT實(shí)例。
- JIT實(shí)例需要加入運(yùn)行庫(kù)(依賴庫(kù))和用戶定義的context(運(yùn)行內(nèi)容)才能運(yùn)行,LLVMOrcLLJITAddLLVMIRModule函數(shù)負(fù)責(zé)將運(yùn)行庫(kù)和ctx加入JIT實(shí)例。
- context相當(dāng)于給用戶自定義代碼的上下文,其中可以加入多個(gè)module,每個(gè)module中又可以加入多個(gè)function??梢岳斫鉃橐粋€(gè)工程(context)里面加入多個(gè)文件,每個(gè)文件(module)中又包含多個(gè)函數(shù)(function)。
ps. module的構(gòu)造還可以構(gòu)造machine在上面構(gòu)造module
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-726365.html
注釋文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-726365.html
LLVMOrcThreadSafeModuleRef createDemoModule(void) {
// 創(chuàng)建一個(gè)新的ThreadSafeContext和底層的LLVMContext。
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
// 獲取底層的LLVMContext的引用。
LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx);
// 創(chuàng)建一個(gè)新的LLVM模塊。
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
// 添加一個(gè)名為"sum"的函數(shù):
// - 創(chuàng)建函數(shù)類型和函數(shù)實(shí)例。
LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
LLVMTypeRef SumFunctionType =
LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
// - 向函數(shù)添加一個(gè)基本塊。
LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
// - 創(chuàng)建一個(gè)IR構(gòu)建器并將其定位到基本塊的末尾。
LLVMBuilderRef Builder = LLVMCreateBuilder();
LLVMPositionBuilderAtEnd(Builder, EntryBB);
// - 獲取兩個(gè)函數(shù)參數(shù),并使用它們構(gòu)造一個(gè)"add"指令。
LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);
LLVMValueRef SumArg1 = LLVMGetParam(SumFunction, 1);
LLVMValueRef Result = LLVMBuildAdd(Builder, SumArg0, SumArg1, "result");
// - 構(gòu)建返回指令。
LLVMBuildRet(Builder, Result);
// 演示模塊現(xiàn)在已經(jīng)完成。將其和ThreadSafeContext封裝到ThreadSafeModule中。
LLVMOrcThreadSafeModuleRef TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);
// 釋放本地的ThreadSafeContext值。底層的LLVMContext將由ThreadSafeModule TSM保持活動(dòng)狀態(tài)。
LLVMOrcDisposeThreadSafeContext(TSCtx);
// 返回結(jié)果。
return TSM;
}
int main(int argc, char *argv[]) {
int MainResult = 0;
// 解析命令行參數(shù)并初始化LLVM Core。
LLVMParseCommandLineOptions(argc, (const char **)argv, "");
LLVMInitializeCore(LLVMGetGlobalPassRegistry());
// 初始化本地目標(biāo)代碼生成和匯編打印器。
LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
// 創(chuàng)建LLJIT實(shí)例。
LLVMOrcLLJITRef J;
{
LLVMErrorRef Err;
if ((Err = LLVMOrcCreateLLJIT(&J, 0))) {
MainResult = handleError(Err);
goto llvm_shutdown;
}
}
// 創(chuàng)建演示模塊。
LLVMOrcThreadSafeModuleRef TSM = createDemoModule();
// 將演示模塊添加到JIT中。
{
LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J);
LLVMErrorRef Err;
if ((Err = LLVMOrcLLJITAddLLVMIRModule(J, MainJD, TSM))) {
// 如果添加ThreadSafeModule失敗,我們需要自己清理它。
// 如果添加成功,JIT將管理內(nèi)存。
LLVMOrcDisposeThreadSafeModule(TSM);
MainResult = handleError(Err);
goto jit_cleanup;
}
}
// 查找演示入口點(diǎn)的地址。
LLVMOrcJITTargetAddress SumAddr;
{
LLVMErrorRef Err;
if ((Err = LLVMOrcLLJITLookup(J, &SumAddr, "sum"))) {
MainResult = handleError(Err);
goto jit_cleanup;
}
}
// 如果程序執(zhí)行到這里,說(shuō)明一切順利。執(zhí)行JIT生成的代碼。
int32_t (Sum)(int32_t, int32_t) = (int32_t()(int32_t, int32_t))SumAddr;
int32_t Result = Sum(1, 2);
// 打印結(jié)果。
printf("1 + 2 = %i\n", Result);
jit_cleanup:
// 銷(xiāo)毀JIT實(shí)例。這將清理JIT所擁有的任何內(nèi)存。
// 這個(gè)操作是非平凡的(例如,可能需要JIT靜態(tài)析構(gòu)函數(shù)),也可能失敗。
// 如果失敗,我們希望將錯(cuò)誤輸出到stderr,但不要覆蓋任何現(xiàn)有的返回值。
{
LLVMErrorRef Err;
if ((Err = LLVMOrcDisposeLLJIT(J))) {
int NewFailureResult = handleError(Err);
if (MainResult == 0) MainResult = NewFailureResult;
}
}
llvm_shutdown:
// 關(guān)閉LLVM。
LLVMShutdown();
return MainResult;
}
ORC完整
//===------ OrcV2CBindingsBasicUsage.c - Basic OrcV2 C Bindings Demo ------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <stdio.h>
#include "llvm-c/Core.h"
#include "llvm-c/Error.h"
#include "llvm-c/Initialization.h"
#include "llvm-c/LLJIT.h"
#include "llvm-c/Support.h"
#include "llvm-c/Target.h"
int handleError(LLVMErrorRef Err) {
char *ErrMsg = LLVMGetErrorMessage(Err);
fprintf(stderr, "Error: %s\n", ErrMsg);
LLVMDisposeErrorMessage(ErrMsg);
return 1;
}
LLVMOrcThreadSafeModuleRef createDemoModule(void) {
// Create a new ThreadSafeContext and underlying LLVMContext.
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
// Get a reference to the underlying LLVMContext.
LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx);
// Create a new LLVM module.
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
// Add a "sum" function":
// - Create the function type and function instance.
LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
LLVMTypeRef SumFunctionType =
LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
// - Add a basic block to the function.
LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
// - Add an IR builder and point it at the end of the basic block.
LLVMBuilderRef Builder = LLVMCreateBuilder();
LLVMPositionBuilderAtEnd(Builder, EntryBB);
// - Get the two function arguments and use them co construct an "add"
// instruction.
LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);
LLVMValueRef SumArg1 = LLVMGetParam(SumFunction, 1);
LLVMValueRef Result = LLVMBuildAdd(Builder, SumArg0, SumArg1, "result");
// - Build the return instruction.
LLVMBuildRet(Builder, Result);
// Our demo module is now complete. Wrap it and our ThreadSafeContext in a
// ThreadSafeModule.
LLVMOrcThreadSafeModuleRef TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);
// Dispose of our local ThreadSafeContext value. The underlying LLVMContext
// will be kept alive by our ThreadSafeModule, TSM.
LLVMOrcDisposeThreadSafeContext(TSCtx);
// Return the result.
return TSM;
}
int main(int argc, char *argv[]) {
int MainResult = 0;
// Parse command line arguments and initialize LLVM Core.
LLVMParseCommandLineOptions(argc, (const char **)argv, "");
LLVMInitializeCore(LLVMGetGlobalPassRegistry());
// Initialize native target codegen and asm printer.
LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
// Create the JIT instance.
LLVMOrcLLJITRef J;
{
LLVMErrorRef Err;
if ((Err = LLVMOrcCreateLLJIT(&J, 0))) {
MainResult = handleError(Err);
goto llvm_shutdown;
}
}
// Create our demo module.
LLVMOrcThreadSafeModuleRef TSM = createDemoModule();
// Add our demo module to the JIT.
{
LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J);
LLVMErrorRef Err;
if ((Err = LLVMOrcLLJITAddLLVMIRModule(J, MainJD, TSM))) {
// If adding the ThreadSafeModule fails then we need to clean it up
// ourselves. If adding it succeeds the JIT will manage the memory.
LLVMOrcDisposeThreadSafeModule(TSM);
MainResult = handleError(Err);
goto jit_cleanup;
}
}
// Look up the address of our demo entry point.
LLVMOrcJITTargetAddress SumAddr;
{
LLVMErrorRef Err;
if ((Err = LLVMOrcLLJITLookup(J, &SumAddr, "sum"))) {
MainResult = handleError(Err);
goto jit_cleanup;
}
}
// If we made it here then everything succeeded. Execute our JIT'd code.
int32_t (*Sum)(int32_t, int32_t) = (int32_t(*)(int32_t, int32_t))SumAddr;
int32_t Result = Sum(1, 2);
// Print the result.
printf("1 + 2 = %i\n", Result);
jit_cleanup:
// Destroy our JIT instance. This will clean up any memory that the JIT has
// taken ownership of. This operation is non-trivial (e.g. it may need to
// JIT static destructors) and may also fail. In that case we want to render
// the error to stderr, but not overwrite any existing return value.
{
LLVMErrorRef Err;
if ((Err = LLVMOrcDisposeLLJIT(J))) {
int NewFailureResult = handleError(Err);
if (MainResult == 0) MainResult = NewFailureResult;
}
}
llvm_shutdown:
// Shut down LLVM.
LLVMShutdown();
return MainResult;
}
到了這里,關(guān)于LLVM(5)ORC實(shí)例分析的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!