gpt4 book ai didi

llvm - 当我不为 main 创建方法 + 基本 block 时,CreateGlobalStringPtr 崩溃

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

CreateGlobalStringPtr 中的以下崩溃:

#include "llvm/Support/DataTypes.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Constants.h"
#include "llvm/Function.h"
#include "llvm/BasicBlock.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/Support/IRBuilder.h"

#include <vector>
#include <cstdio>
#include <string>

int main()
{
llvm::LLVMContext & context = llvm::getGlobalContext();
llvm::Module *module = new llvm::Module("asdf", context);
llvm::IRBuilder<> builder(context);
llvm::Value *helloWorld = builder.CreateGlobalStringPtr("hello world!\n"); // crash here
}

而这个没有,知道这里发生了什么:
#include "llvm/Support/DataTypes.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Constants.h"
#include "llvm/Function.h"
#include "llvm/BasicBlock.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/Support/IRBuilder.h"

#include <vector>
#include <cstdio>
#include <string>

int main()
{
llvm::LLVMContext & context = llvm::getGlobalContext();
llvm::Module *module = new llvm::Module("asdf", context);
llvm::IRBuilder<> builder(context);

// Added extra
llvm::FunctionType *funcType = llvm::FunctionType::get(builder.getVoidTy(), false);
llvm::Function *mainFunc = llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "main", module);
llvm::BasicBlock *entry = llvm::BasicBlock::Create(context, "entry", mainFunc);
builder.SetInsertPoint(entry);

llvm::Value *helloWorld = builder.CreateGlobalStringPtr("hello world!\n");
}

不确定如何创建主例程和入口点可以消除崩溃。以为他们没有血缘关系。以下是我使用 g++ 进行编译的选项:
llvm-config --cxxflags --ldflags --libs
-I/usr/include -DNDEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -O3 -fno-exceptions -fno-rtti -fno-common -Woverloaded-virtual -Wcast-qual
-L/usr/lib -lpthread -lm
-lLLVMXCoreCodeGen -lLLVMTableGen -lLLVMSystemZCodeGen -lLLVMSparcCodeGen -lLLVMPTXCodeGen -lLLVMPowerPCCodeGen -lLLVMMSP430CodeGen -lLLVMMipsCodeGen -lLLVMMCJIT -lLLVMRuntimeDyld -lLLVMObject -lLLVMMCDisassembler -lLLVMXCoreDesc -lLLVMXCoreInfo -lLLVMSystemZDesc -lLLVMSystemZInfo -lLLVMSparcDesc -lLLVMSparcInfo -lLLVMPowerPCDesc -lLLVMPowerPCInfo -lLLVMPowerPCAsmPrinter -lLLVMPTXDesc -lLLVMPTXInfo -lLLVMPTXAsmPrinter -lLLVMMipsDesc -lLLVMMipsInfo -lLLVMMipsAsmPrinter -lLLVMMSP430Desc -lLLVMMSP430Info -lLLVMMSP430AsmPrinter -lLLVMMBlazeDisassembler -lLLVMMBlazeAsmParser -lLLVMMBlazeCodeGen -lLLVMMBlazeDesc -lLLVMMBlazeAsmPrinter -lLLVMMBlazeInfo -lLLVMLinker -lLLVMipo -lLLVMInterpreter -lLLVMInstrumentation -lLLVMJIT -lLLVMExecutionEngine -lLLVMDebugInfo -lLLVMCppBackend -lLLVMCppBackendInfo -lLLVMCellSPUCodeGen -lLLVMCellSPUDesc -lLLVMCellSPUInfo -lLLVMCBackend -lLLVMCBackendInfo -lLLVMBlackfinCodeGen -lLLVMBlackfinDesc -lLLVMBlackfinInfo -lLLVMBitWriter -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMX86Desc -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMX86Info -lLLVMAsmParser -lLLVMARMDisassembler -lLLVMARMAsmParser -lLLVMARMCodeGen -lLLVMARMDesc -lLLVMARMAsmPrinter -lLLVMARMInfo -lLLVMArchive -lLLVMBitReader -lLLVMAlphaCodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMMCParser -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMTarget -lLLVMCore -lLLVMAlphaDesc -lLLVMAlphaInfo -lLLVMMC -lLLVMSupport

最佳答案

CreateGlobalStringPtr 正在为您的字符串创建一个 GEP,构建器需要一个函数和基本块来将指令插入其中。

见include/llvm/Support/IRBuilder.h

关于llvm - 当我不为 main 创建方法 + 基本 block 时,CreateGlobalStringPtr 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8966890/

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