- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试从全局变量中获取浮点值并将其设置为指令的操作数。
这是我想做的:
@a = private constant float 0x3FB99999A0000000
...
%1 = load float, float* @a ---> removed
%3 = fmul fast %1, %2 ---> %3 = fmul fast float 0x3FB99999A0000000, %2
下面是我到目前为止尝试过的:
for (auto gv_iter = llvm_module.global_begin();gv_iter != llvm_module.global_end(); gv_iter++){
llvm::GlobalVariable* gv = &*gv_iter;
for(auto user_of_gv : gv->users()){
llvm::Instruction *instr_ld_gv = llvm::dyn_cast<llvm::Instruction>(user_of_gv);
llvm::Value *val_gv = llvm::cast<llvm::Value>(instr_ld_gv);
llvm::Constant *const_gv = gv->getInitializer();
llvm::ConstantFP *constfp_gv = llvm::dyn_cast<llvm::ConstantFP>(const_gv);
float gv_fpval = (constfp_gv->getValueAPF()).convertToFloat();
llvm::Constant *const_gv_opd = llvm::ConstantFP::get(llvm::Type::getFloatTy(llvm_context),gv_fpval);
for(auto user_of_load : val_gv->users()){
llvm::Instruction *instr_exe_gv = llvm::dyn_cast<llvm::Instruction>(user_of_load);
//P
for(int operand_num = 0;operand_num < instr_exe_gv->getNumOperands();operand_num++){
llvm::Value *val_instr_op = instr_exe_gv->getOperand(operand_num);
if(val_instr_op == val_gv){
instr_exe_gv->setOperand(operand_num,const_gv_opd);
instr_ld_gv->removeFromParent();
}
}
}
}
}
但是,当我尝试运行我的代码时,它会导致段错误。
我确定我已经访问了我想要的全局变量和指令通过打印
的值gv_fpval
是 0.1
因为 0x3FB99999A0000000 等于 0.10000000149011612 的双数
精度。程序似乎在 setOperand()
处崩溃。
最佳答案
考虑下面的例子
你好.cpp
#include <stdio.h>
// Global Constant value
float a=1.4f;
float Multiply(){
float b=2.2f;
float c=4.32f;
float d= a*c;
return d;
}
int main(int argc, char const *argv[])
{
printf("%f\n",Multiply());
return 0;
}
模块 pass 将循环查找浮点全局变量,程序中的任何使用都将替换为常量 FP 值。 LLVM pass 如下所示 ConstantReplacementPass.cpp
:-
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/DebugInfo.h"
using namespace llvm;
/* StackOverflow : https://stackoverflow.com/questions/48212351/how-to-get-llvm-global-variable-constant-value* /
/**Bernard Nongpoh */
namespace {
class ConstantReplacementPass : public ModulePass {
public:
static char ID;
ConstantReplacementPass() : ModulePass(ID) {
srand (time(NULL));
}
virtual bool runOnModule(Module &M) {
// list to collect instruction
/*
* You cannot change an iterator while iterating over it
• To remove instructions or modify, first collect the instructions to remove/modify
•
*
* **/
// This are the list of load to delete
SmallVector<Instruction*,128> *WorkListLoad=new SmallVector<Instruction*,128>();
// This is the list of instruction to modify the source operand
SmallVector<Instruction*,128> *WorkListUserOfLoad=new SmallVector<Instruction*,128>();
for (auto gv_iter = M.global_begin();gv_iter != M.global_end(); gv_iter++) {
/* GLOBAL DATA INFO*/
GlobalVariable *gv = &*gv_iter;
Constant *const_gv = gv->getInitializer();
ConstantFP *Fvalue;
if(!const_gv->isNullValue()) {
if (ConstantFP *constfp_gv = llvm::dyn_cast<llvm::ConstantFP>(const_gv)) {
float gv_fpval = (constfp_gv->getValueAPF()).convertToFloat();
Fvalue = constfp_gv;
errs() << gv_fpval; // Value retrieved here
// Collect Instruction to modify
}
for (auto user_of_gv: gv->users()) {
// Collect in a worklist
if (llvm::Instruction *instr_ld_gv = llvm::dyn_cast<Instruction>(user_of_gv)) {
if (LoadInst *loadInst = dyn_cast<LoadInst>(instr_ld_gv)) {
WorkListLoad->push_back(loadInst);
for (auto user_of_load:loadInst->users()) {
user_of_load->dump();
Instruction *instruction1 = dyn_cast<Instruction>(user_of_load);
instruction1->dump();
//instruction1->setOperand(0, Fvalue);
//instruction1->dump();
// if(Instruction *instruction1 = dyn_cast<Instruction>(user_of_load))
WorkListUserOfLoad->push_back(instruction1);
//instruction1->setOperand(0, Fvalue);
//instruction1->dump();
}
}
}
}
// Modify Here
while (!WorkListUserOfLoad->empty()) {
Instruction *instruction = WorkListUserOfLoad->pop_back_val();
instruction->setOperand(0, Fvalue);
instruction->dump();
}
// Removing all loads that are used by the global variable
while (!WorkListLoad->empty()) {
Instruction *instruction = WorkListLoad->pop_back_val();
instruction->eraseFromParent();
}
}
}
return true;
}
};
}
char ConstantReplacementPass::ID = 0;
static RegisterPass<ConstantReplacementPass> F0("constantREP", "Constant Replacement Pass "
, false,true);
要点:-
在对指令进行任何修改之前。在工作列表中首先收集。
对工作列表进行修改。
您不能在使用迭代器时进行修改。
我在上面的源码hello.cpp
上测试成功,pass后对应的IR如下:-
entry:
%b = alloca float, align 4
%c = alloca float, align 4
%d = alloca float, align 4
call void @llvm.dbg.declare(metadata float* %b, metadata !14, metadata !15),
... !dbg !16
store float 0x40019999A0000000, float* %b, align 4, !dbg !16
call void @llvm.dbg.declare(metadata float* %c, metadata !17, metadata !15),
... !dbg !18
store float 0x401147AE20000000, float* %c, align 4, !dbg !18
call void @llvm.dbg.declare(metadata float* %d, metadata !19, metadata !15),
... !dbg !20
%0 = load float, float* %c, align 4, !dbg !21
%mul = fmul float 0x3FF6666660000000, %0, !dbg !22
store float %mul, float* %d, align 4, !dbg !20
%1 = load float, float* %d, align 4, !dbg !23
ret float %1, !dbg !24
也许使用 -O3 优化标志会清除一切...
希望这有帮助..
关于c++ - 如何获取LLVM全局变量常量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48212351/
我指的是 https://llvm.org/docs/GettingStarted.html从其源代码构建 LLVM。我正在使用 Ubuntu 18.04。 $ cmake -G Ninja -DCM
使用 ModulePass,我的目标是向上遍历 SSA 图:从一个具有 0..2 个操作数的语句(大多数操作码属于该语句)开始,我想找出两件事: 操作数是元数据/常量(简单:只需尝试转换为常量类型)还
注意:我注意到 this question与这个问题有很大关系,所以如果您对我的问题感兴趣,那么您一定要阅读另一个问题及其答案。 我可以想到一些 OOP 语言前端可以做的优化,例如创建临时变量来保存来
我正在尝试使用 clang-3.2 创建自动矢量化代码 从这里的幻灯片 - http://llvm.org/devmtg/2012-04-12/Slides/Hal_Finkel.pdf 我应该能够使
我想问的是,我们可以将现有pass生成的信息转化为新pass吗? 如果是,那么如何? 例如 -loops 给出了自然循环信息,所以我们可以通过调用它来将这些信息用于新的传递。 最佳答案 您可以通过覆盖
在 LLVM IR 中,当我想从一个数组中获取一个值时,似乎有三种方法可以做到这一点:使用 extractvalue、使用 extractelement 和使用 getelementptr 然后加载。
我想逐步介绍一下我生成的LLVM IR代码。就llc和lli而言,该代码在语法上是有效的并且类型有效,但是结果不是我所期望的。 这些块足够大,以至于我无法简单地读取该错误就无法成功完成,并且我很难制作
我想弄清楚如何使用 trampoline intrinsics在 LLVM 中。该文档提到了存储蹦床所需的一些存储量,这取决于平台。我的问题是,我如何确定需要多少? 我找到了 this example
我需要使用抽象解释来使用 LLVM 进行一些分析。这可能吗?或者我需要更轻松地使用分析工具。如果我可以通过 LLVM 做到这一点,哪些类将帮助我从原始源代码中制定语句以获取变量之间的关系(以及每个变量
我正在创建一种静态编译的编程语言,并使用 LLVM 作为其后端。我希望我的语言在发生整数溢出时陷入/崩溃。 我知道类似 llvm.sadd.with.overflow 的事情,但我认为这不是最佳/有效
我正在尝试学习 LLVM tablegen。 http://llvm.org/docs/TableGen/LangRef.html表明 field关键字存在但不解释其含义。有人知道这是什么意思吗? 我
Fibonacci LLVM 示例使用 errs() getIR() ) 我一直在搜索 llvm::Module Class Reference并没有得到任何帮助。 Fibonacci.cpp 的一部
我想在 llvm IR 中找到对 llvm.pow.f64 函数的所有函数调用。请建议我一个方法来做到这一点。 最佳答案 嗯,这是一个基本的 FunctionPass找到所有对函数的调用: class
我正在尝试从 llvm 中间代码中删除未使用的 block 。 首先,我通过从入口 basicblock 开始并从它们的终止符指令添加所有 basicblock 后继者来构建可访问的 basicblo
我想获取 llvm 中每条指令的后继列表。如果我没理解错的话,对于除了branch(br)之外的所有指令,后继指令就是下一条。但是对于分支指令来说有点棘手。 例如,如果我有以下 C 代码: int m
我有文本格式的 LLVM IR 代码。我想做的是能够解析它并修改该代码。是否有可以帮助解析 LLVM IR 代码的 API?我的系统中应该有哪些库?此刻我有clang编译器也安装了 LLVM,因为我可
(1) @str = private constant [13 x i8] c"Hello World\0A\00" (2) define i32 @main(){ (3) %r2 = getelem
如果我有一组基本块和边,我需要为它们创建一个具有新入口和端点的新函数。 我可以直接在 LLVM 中创建它吗,就像 createFunction(F) 一样然后 F.insert(bb, edges)其
我通过教程使用 LLVM http://llvm.org/releases/3.1/docs/tutorial/ 编写玩具编译器 但是关于符号表处理的内容并不多。 有一个命令 llvm-nm 显示符号
我希望编写一个 LLVM 传递两个 i32 类型的参数来选择函数。我的第一次尝试(如下所示)失败了: bool MyFunctionPass::runOnFunction(Function &f) {
我是一名优秀的程序员,十分优秀!