首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在LLVM自定义优化过程中删除Segfault指令

在LLVM自定义优化过程中删除Segfault指令
EN

Stack Overflow用户
提问于 2020-12-28 17:53:12
回答 1查看 137关注 0票数 0

这是我写的优化过程。它将任何以零作为其操作数之一的乘法替换为简单的零。

代码语言:javascript
复制
struct FoldConstant : public llvm::FunctionPass {
    static char ID;
    FoldConstant() : llvm::FunctionPass(ID) {}
    bool runOnFunction(llvm::Function &func) override {
        for (auto &block : func) {
            for (auto &inst : block) {
                if (inst.isBinaryOp()) {
                    llvm::Value *left = inst.getOperand(0);
                    llvm::Value *right = inst.getOperand(1);
                    if (inst.getOpcode() == 17) {
                        if (llvm::ConstantInt *lc =
                                llvm::dyn_cast<llvm::ConstantInt>(left)) {
                            if (lc->getSExtValue() == 0) {
                                inst.replaceAllUsesWith(left);
                                inst.eraseFromParent();
                            }
                        }
                        if (llvm::ConstantInt *rc =
                                llvm::dyn_cast<llvm::ConstantInt>(right)) {
                            if (rc->getSExtValue() == 0) {
                                inst.replaceAllUsesWith(right);
                                inst.eraseFromParent();
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
};

现在,当我在下面的IR源上运行它时

代码语言:javascript
复制
; ModuleID = 'bootleg c compiler'
source_filename = "bootleg c compiler"

define i32 @constant_folding(i32 %b) {
entry:
  %b1 = alloca i32
  store i32 %b, i32* %b1
  %b2 = load i32, i32* %b1
  %mul = mul i32 %b2, 0
  ret i32 %mul
}

它给出了一个段错误,下面是堆栈跟踪。

代码语言:javascript
复制
sumit@HAL9001:~/Coding/cc$ opt -S -load ./opt.so -foldconst < output.ll
Stack dump:
0.  Program arguments: opt -S -load ./opt.so -foldconst 
1.  Running pass 'Function Pass Manager' on module '<stdin>'.
2.  Running pass 'constant folding' on function '@constant_folding'
 #0 0x00007f5e0522e4ff llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/lib/x86_64-linux-gnu/libLLVM-10.so.1+0x9814ff)
 #1 0x00007f5e0522c7b0 llvm::sys::RunSignalHandlers() (/lib/x86_64-linux-gnu/libLLVM-10.so.1+0x97f7b0)
 #2 0x00007f5e0522eac5 (/lib/x86_64-linux-gnu/libLLVM-10.so.1+0x981ac5)
 #3 0x00007f5e08f563c0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x153c0)
 #4 0x00007f5e08f73986 llvm::Value::getValueID() const /usr/lib/llvm-10/include/llvm/IR/Value.h:485:12
 #5 0x00007f5e08f73ae2 llvm::Instruction::getOpcode() const /usr/lib/llvm-10/include/llvm/IR/Instruction.h:125:54
 #6 0x00007f5e08f73b04 llvm::Instruction::isBinaryOp() const /usr/lib/llvm-10/include/llvm/IR/Instruction.h:130:46
 #7 0x00007f5e08f74857 FoldConstant::runOnFunction(llvm::Function&) /home/sumit/Coding/cc/c.opt.cpp:75:17
 #8 0x00007f5e05333d76 llvm::FPPassManager::runOnFunction(llvm::Function&) (/lib/x86_64-linux-gnu/libLLVM-10.so.1+0xa86d76)
 #9 0x00007f5e05333ff3 llvm::FPPassManager::runOnModule(llvm::Module&) (/lib/x86_64-linux-gnu/libLLVM-10.so.1+0xa86ff3)
#10 0x00007f5e053344a0 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/lib/x86_64-linux-gnu/libLLVM-10.so.1+0xa874a0)
#11 0x00000000004bf3f8 main (/usr/lib/llvm-10/bin/opt+0x4bf3f8)
#12 0x00007f5e043970b3 __libc_start_main /build/glibc-ZN95T4/glibc-2.31/csu/../csu/libc-start.c:342:3
#13 0x00000000004ad0de _start (/usr/lib/llvm-10/bin/opt+0x4ad0de)
Segmentation fault (core dumped)

到目前为止,我已经尝试将代码更改为使用

代码语言:javascript
复制
llvm::BasicBlock::iterator iter(inst);
llvm::ReplaceInstWithValue(block.getInstList(), iter, right);

但这并没有什么不同。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-29 14:24:05

就像@500-internal-server-error在我的问题的注释中指出的那样,由于我在遍历容器(基本块)时从容器(基本块)中移除了元素(指令),所以出现了server。我现在正在使用下面的代码,它可以正常工作。

代码语言:javascript
复制
struct FoldConstant : public llvm::FunctionPass {
    static char ID;
    FoldConstant() : llvm::FunctionPass(ID) {}
    bool runOnFunction(llvm::Function &func) override {
        bool changed = false;
        vector<llvm::Instruction *> instsToDelete;

        for (auto &block : func) {
            for (auto &inst : block) {
                if (inst.isBinaryOp()) {
                    llvm::Value *left = inst.getOperand(0);
                    llvm::Value *right = inst.getOperand(1);
                    if (inst.getOpcode() == 17) {
                        if (llvm::ConstantInt *lc =
                                llvm::dyn_cast<llvm::ConstantInt>(left)) {
                            if (lc->getSExtValue() == 0) {
                                instsToDelete.push_back(&inst);
                                inst.replaceAllUsesWith(left);
                                changed = true;
                            }
                        }
                        if (llvm::ConstantInt *rc =
                                llvm::dyn_cast<llvm::ConstantInt>(right)) {
                            if (rc->getSExtValue() == 0) {
                                instsToDelete.push_back(&inst);
                                inst.replaceAllUsesWith(right);
                                changed = true;
                            }
                        }
                    }
                    
                }
            }
        }
        for (auto inst : instsToDelete) {
            inst->eraseFromParent();
        }
        return changed;
    }
};

我将指向需要删除的指令的指针存储在一个向量中,并在以后删除它们。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65475761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档