码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LLVM系列第二十八章:写一个JIT Hello World


    系列文章目录

    LLVM系列第一章:编译LLVM源码
    LLVM系列第二章:模块Module
    LLVM系列第三章:函数Function
    LLVM系列第四章:逻辑代码块Block
    LLVM系列第五章:全局变量Global Variable
    LLVM系列第六章:函数返回值Return
    LLVM系列第七章:函数参数Function Arguments
    LLVM系列第八章:算术运算语句Arithmetic Statement
    LLVM系列第九章:控制流语句if-else
    LLVM系列第十章:控制流语句if-else-phi
    LLVM系列第十一章:写一个Hello World
    LLVM系列第十二章:写一个简单的词法分析器Lexer
    LLVM系列第十三章:写一个简单的语法分析器Parser
    LLVM系列第十四章:写一个简单的语义分析器Semantic Analyzer
    LLVM系列第十五章:写一个简单的中间代码生成器IR Generator
    LLVM系列第十六章:写一个简单的编译器
    LLVM系列第十七章:for循环
    LLVM系列第十八章:写一个简单的IR处理流程Pass
    LLVM系列第十九章:写一个简单的Module Pass
    LLVM系列第二十章:写一个简单的Function Pass
    LLVM系列第二十一章:写一个简单的Loop Pass
    LLVM系列第二十二章:写一个简单的编译时函数调用统计器(Pass)
    LLVM系列第二十三章:写一个简单的运行时函数调用统计器(Pass)
    LLVM系列第二十四章:用Xcode编译调试LLVM源码
    LLVM系列第二十五章:简单统计一下LLVM源码行数
    LLVM系列第二十六章:理解LLVMContext
    LLVM系列第二十七章:理解IRBuilder
    LLVM系列第二十八章:写一个JIT Hello World


    本文目录

    • 前言
    • 一、Hello JIT
    • 二、编译
    • 三、运行
    • 四、总结


    前言

    在此记录下基于LLVM提供的ORC JIT引擎写一个简单的Hello World的过程,以备查阅。

    开发环境的配置请参考第一章 《LLVM系列第一章:编译LLVM源码》。

    ORC(On Request Compilation)是LLVM提供的新一代JIT引擎。JIT(Just In Time)也是一个程序,它在运行时,创建并执行了一些新的代码,而这些新代码并不是属于JIT程序本身的。以下是原英文解释:

    Whenever a program, while running, creates and runs some new executable code which was not part of the program when it was stored on disk, it’s a JIT.

    LLVM提供了三种JIT的实现:

    1. Legacy JIT (LLVM 1.0 - 3.5),引入了ExecutionEngine、延迟编译(lazy compilation),在当前进程中运行(in-process only),在 LLVM 3.5之后被移除了
    2. MCJIT (LLVM 2.9 - 现在),实现了ExecutionEngine,可编译链接多个目标(cross-target),不支持延迟编译
    3. ORC JIT (LLVM 3.7 - 现在),提供预先查询(forward looking) API,没有实现ExecutionEngine

    本章我们就来写一个简单的Hello World,初步感受一下LLVM的ORC JIT引擎是什么样子的。

    一、Hello JIT

    为了简单起见,我们就用以下IR代码为例子,试着调用ORC JIT来编译并执行它:

    ; ModuleID = 'Add.c'
    source_filename = "Add.c"
    
    define i32 @Add(i32 %a, i32 %b) {
    entry:
      %result = add i32 %a, %b
      ret i32 %result
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    其对应的C代码如下(示例):

    // Add.c
    
    int Add(int a, int b)
    {
        return a + b;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    以下我们就来调用LLVM API生成IR代码,并调用ORC JIT对其进行编译执行(示例):

    // HelloJIT.cpp
    
    #include "llvm/ExecutionEngine/Orc/LLJIT.h"
    #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
    #include "llvm/IR/BasicBlock.h"
    #include "llvm/IR/Function.h"
    #include "llvm/IR/IRBuilder.h"
    #include "llvm/IR/LLVMContext.h"
    #include "llvm/IR/Module.h"
    #include "llvm/IR/Verifier.h"
    #include "llvm/Support/InitLLVM.h"
    #include "llvm/Support/TargetSelect.h"
    #include "llvm/Support/raw_ostream.h"
    
    #include 
    #include 
    #include 
    
    using namespace llvm;
    using namespace llvm::orc;
    
    typedef int (*AddFunctionType)(int, int);
    
    // Create a module to represent the following C function
    //   int Add(int a, int b)
    //   {
    //       return a + b;
    //   }
    ThreadSafeModule CreateModule()
    {
        auto context = std::make_unique<LLVMContext>();
        IRBuilder<> builder(*context);
    
        // Create a module
        auto module = std::make_unique<Module>("Add.c", *context);
    
        // Create a function that looks like:
        //   int Add(int a, int b)
        std::vector<Type*> parameters(2, builder.getInt32Ty());
        FunctionType* functionType = FunctionType::get(builder.getInt32Ty(), parameters, false);
        Function* function = Function::Create(functionType, GlobalValue::ExternalLinkage, "Add", module.get());
    
        // Set arguments for the function
        function->getArg(0)->setName("a");
        function->getArg(1)->setName("b");
    
        // Create a block in the function
        BasicBlock* block = BasicBlock::Create(*context, "entry", function);
        builder.SetInsertPoint(block);
    
        // Create an instruction to add a and b:
        //   return a + b;
        Value* a = function->getArg(0);
        Value* b = function->getArg(1);
        Value* result = builder.CreateAdd(a, b, "result");
        builder.CreateRet(result);
    
        // Print the IR
        verifyFunction(*function);
        module->print(outs(), nullptr);
    
        return ThreadSafeModule(std::move(module), std::move(context));
    }
    
    // Compile and run the "Add()" function
    int main(int argc, char* argv[])
    {
        // Do initialization for JIT
        InitializeNativeTarget();
        InitializeNativeTargetAsmPrinter();
    
        // Create the JIT
        ExitOnError ExitOnErr;
        auto jit = ExitOnErr(LLJITBuilder().create());
        auto module = CreateModule();
        ExitOnErr(jit->addIRModule(std::move(module)));
    
        // Find the "Add()" function
        auto functionSymbol = ExitOnErr(jit->lookup("Add"));
        AddFunctionType add = (AddFunctionType)functionSymbol.getAddress();
    
        // Use the "Add()" function
        int result = add(12, 34);
        std::cout << "\n-----------------\n";
        std::cout << "Add(12, 34) = " << result << std::endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

    二、编译

    用clang++进行编译(示例):

    # Set up C++ standard library and header path
    export SDKROOT=$(xcrun --sdk macosx --show-sdk-path)
    
    # Compile
    clang++ -w -o HelloJIT `llvm-config --cxxflags --ldflags --system-libs --libs all` HelloJIT.cpp
    
    • 1
    • 2
    • 3
    • 4
    • 5

    以上命令会生成一个名为HelloJIT的可执行程序。

    三、运行

    运行HelloJIT(示例):

    ./HelloJIT
    
    • 1

    输出结果如下(示例):

    ; ModuleID = 'Add.c'
    source_filename = "Add.c"
    
    define i32 @Add(i32 %a, i32 %b) {
    entry:
      %add.result = add i32 %a, %b
      ret i32 %add.result
    }
    
    -----------------
    Add(12, 34) = 46
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    四、总结

    我们利用LLVM提供的OCR JIT引擎,编译并执行了一段简单的IR代码。

  • 相关阅读:
    (附源码)计算机毕业设计Java坝上长尾鸡养殖管理系统
    细数APDL中的流程控制命令
    RK3568平台开发系列讲解(驱动篇)Linux自带LED子系统驱动实验
    Java-数据库操作
    xss原理分析
    小米屡次违反GPL协议,疑成“惯犯”
    .NET GC
    SpringMVC之JSR303与拦截器
    vue.js javascript js判断是值否为空
    Mobx 数据通信
  • 原文地址:https://blog.csdn.net/Zhanglin_Wu/article/details/126047521
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号