<badref> 当使用 CallInst::CreateMalloc 作为结构类型时
<badref> when using CallInst::CreateMalloc for a struct type
我正在尝试为代码(玩具语言(生成类似于以下C++代码的LLVM IR:
struct test {
int a;
int b;
int c;
};
int main() {
tempVar *a;
a = new test();
}
不幸的是,当我运行verifyModule
时,我收到以下错误消息:
Instruction referencing instruction not embedded in a basic block!
%malloccall = tail call i8* @malloc(i64 mul nuw (i64 ptrtoint (i32* getelementptr (i32, i32* null, i32 1) to i64), i64 3))
<badref> = bitcast i8* %malloccall to %test*
以下 MWE 重现了该问题:
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/raw_ostream.h"
#include <map>
#include <vector>
using namespace llvm;
std::map<std::string, StructType *> allocatedClasses;
std::map<std::string, std::vector<Type *>> classSizes;
static LLVMContext TheContext;
static IRBuilder<> Builder(TheContext);
static std::map<std::string, AllocaInst *> NamedValues;
int main() {
static std::unique_ptr<Module> TheModule;
TheModule = std::make_unique<Module>("inputFile", TheContext);
allocatedClasses["test"] = StructType::create(TheContext, "test");
classSizes["test"] = std::vector<Type *>(3, Type::getInt32Ty(TheContext));
allocatedClasses["test"]->setBody(classSizes["test"]);
FunctionType *mainType = FunctionType::get(Builder.getInt32Ty(), false);
Function *main = Function::Create(mainType, Function::ExternalLinkage, "main",
TheModule.get());
BasicBlock *entry = BasicBlock::Create(TheContext, "entry", main);
Builder.SetInsertPoint(entry);
std::string tV = "tempVar";
NamedValues[tV] = Builder.CreateAlloca(
PointerType::get(allocatedClasses["test"], 0), nullptr, tV);
auto typeSize = ConstantExpr::getSizeOf(allocatedClasses["test"]);
typeSize =
ConstantExpr::getTruncOrBitCast(typeSize, Type::getInt64Ty(TheContext));
CallInst::CreateMalloc(Builder.GetInsertBlock(), Type::getInt64Ty(TheContext),
allocatedClasses["test"], typeSize, nullptr, nullptr,
"");
Builder.CreateRet(ConstantInt::get(TheContext, APInt(32, 0)));
TheModule->print(outs(), nullptr);
Module *test = TheModule.get();
verifyModule(*test, &errs());
}
我编译这个clang++ `llvm-config --cxxflags --ldflags --system-libs --libs all` -g ex.cpp
,在x86上使用clang版本10.0.0-4ubuntu1
。执行时,程序输出:
; ModuleID = 'inputFile'
source_filename = "inputFile"
%test = type { i32, i32, i32 }
define i32 @main() {
entry:
%tempVar = alloca %test*
%malloccall = tail call i8* @malloc(i64 mul nuw (i64 ptrtoint (i32* getelementptr (i32, i32* null, i32 1) to i64), i64 3))
ret i32 0
}
declare noalias i8* @malloc(i64)
以及上面的错误消息。
我在这里做错了什么?
您调用的函数的文档显示"注意:此函数不会将位广播添加到基本块,这是调用方的责任。
我不知道为什么不,但来电者是你。
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- vscode g++链路故障:体系结构x86_64的未定义符号
- <badref> 当使用 CallInst::CreateMalloc 作为结构类型时