1

I was wondering if anyone knew how to declare a global integer instance in LLVM IR. So far, I've been doing the following:

// Create symbol to identify previous block. Added by Justin.
llvm::Type::TypeID stupidTypeID = llvm::Type::IntegerTyID;
llvm::Type* typePtr = llvm::Type::getPrimitiveType(_context, stupidTypeID);
llvm::GlobalVariable* prevBlockID = new llvm::GlobalVariable(typePtr,
                                                           false,
                                                          llvm::GlobalValue::LinkerPrivateLinkage,
                                                           NULL,
                                                           "PREV_BLOCK_ID");

When I try to run, I get the following error:

static llvm::PointerType* llvm::PointerType::get(llvm::Type*, unsigned int): Assertion `EltTy && "Can't get a pointer to <null> type!"' failed.
4

1 回答 1

2

是因为类型不对。您可以在这里Type::getPrimitiveType查看实现。简而言之,这不是建议您使用的 API;对于 IntegerType,它返回 nullptr。此外,在llvm/IR/Type.h的定义中,有评论说:TypeID

/// 注意:如果你添加一个元素到这里,你需要添加一个元素到
/// Type::getPrimitiveType 函数,否则事情会中断!

基本上,您可以通过 2 种方法生成类型:

  • get指定类型的静态API
    在您的情况下,

    IntegerType *iTy = IntegerType::get(ctx, 32);  // if it's 32bit INT
    
  • 一个名为TypeBuilder
    It 的辅助类使类型生成更加容易和普遍。TypeBuilder当您需要定义更复杂的类型时特别有用和直观,例如,FunctionType当然要以缓慢编译源代码为代价(您应该关心吗?)。

    IntegerType *intType = TypeBuilder<int, false>::get(ctx);  // normal C int
    IntegerType *intTy = TypeBuilder<types::i<32>, false>::get(ctx);  // if it's 32bit INT
    

BTW,你也可以试试ELLCC在线编译器来获取对应的C++代码,用于生成当前c/c++ src的LLVM IR,这里需要选择Output Options的target为LLVM C++ API code。或者,您可以在您的机器上自己尝试(因为在内部,在线编译器只是调用llc):

llc input.ll -march=cpp -o -
于 2014-09-19T14:32:40.493 回答