gpt4 book ai didi

llvm - 解析 LLVM IR 代码

转载 作者:行者123 更新时间:2023-12-05 04:17:17 24 4
gpt4 key购买 nike

我正在点击链接 Parsing and Modifying LLVM IR code读取 IR 文件并尝试解析它。但是我发现无论我在参数中写入什么输入文件(.ll 或.bc),它都不会解析文件并将其保存到变量中。

这是我的代码,

#include <iostream>
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Function.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/IRReader.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

int main(int argc, char** argv)
{
if (argc < 2) {
errs() << "Expected an argument - IR file name\n";
exit(1);
}

LLVMContext &Context = getGlobalContext();
SMDiagnostic Err;
Module *Mod = ParseIRFile(argv[1], Err, Context);

if (Mod) {
std::cout << "Mod is not null" << std::endl;
}
else {
std::cout << "Mod is null" << std::endl;
}
return 0;
}

通过使用 ll 或 bc 文件运行此代码,它始终显示 Mod 为空。

有人可以给我一些关于如何解决这个问题的提示吗?

这是我的IR代码

; ModuleID = 'bubble.bc'
target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.9.0"

@Sort = global [8 x i32] [i32 1, i32 4, i32 2, i32 5, i32 7, i32 3, i32 6, i32 3], align 16
@.str = private unnamed_addr constant [4 x i8] c"%d \00", align 1
@.str1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1

; Function Attrs: nounwind ssp uwtable
define void @bubble() #0 {
%tmp = alloca i32, align 4
%i = alloca i32, align 4
%j = alloca i32, align 4
store i32 0, i32* %i, align 4
br label %1

; <label>:1 ; preds = %41, %0
%2 = load i32* %i, align 4
%3 = icmp slt i32 %2, 8
br i1 %3, label %4, label %44

; <label>:4 ; preds = %1
%5 = load i32* %i, align 4
%6 = add nsw i32 %5, 1
store i32 %6, i32* %j, align 4
br label %7

; <label>:7 ; preds = %37, %4
%8 = load i32* %j, align 4
%9 = icmp slt i32 %8, 8
br i1 %9, label %10, label %40

; <label>:10 ; preds = %7
%11 = load i32* %i, align 4
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [8 x i32]* @Sort, i32 0, i64 %12
%14 = load i32* %13, align 4
%15 = load i32* %j, align 4
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [8 x i32]* @Sort, i32 0, i64 %16
%18 = load i32* %17, align 4
%19 = icmp sge i32 %14, %18
br i1 %19, label %20, label %36

//some similar stuff

!0 = metadata !{metadata !"Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)"}

错误 meg 是 ./IRparser:/home/mingaoIrparser/testcase/bubble.ll:10:23: error: expected '{' in function body定义 void @bubble() #0 { ^

一件事是这个 IR 是从 LLVM3.4 编译的,我现在使用的库是 2.9。有关系吗?

最佳答案

This repository有一堆使用 LLVM 和 Clang 作为库的最新示例。例如,this sample有您需要的(以及其他样本):

int main(int argc, char **argv) {
if (argc < 2) {
errs() << "Usage: " << argv[0] << " <IR file>\n";
return 1;
}

// Parse the input LLVM IR file into a module.
SMDiagnostic Err;
Module *Mod = ParseIRFile(argv[1], Err, getGlobalContext());
if (!Mod) {
Err.print(argv[0], errs());
return 1;
}

// ... use module
}

关于llvm - 解析 LLVM IR 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23915811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com