[llvm-dev] How to create global symbol from record offset

Nat! via llvm-dev llvm-dev at lists.llvm.org
Sat Oct 31 03:19:39 PDT 2015

I have a problem getting this implemented, the following small test 
program invariably crashes with.

Assertion failed: (isa<X>(Val) && "cast<Ty>() argument of incompatible 
type!"), function cast

Help would be greatly appreciated.
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Constants.h"

int main(int argc, char const *argv[])
    llvm::LLVMContext &Context = llvm::getGlobalContext();

    llvm::Constant *Fields[3];
    Fields[0] = llvm::ConstantInt::get(Context, llvm::APInt( 32, 18, true));
    Fields[1] = llvm::ConstantInt::get(Context, llvm::APInt( 32, 48, true));
    Fields[2] = llvm::ConstantInt::get(Context, llvm::APInt( 32, 1848, 

    llvm::Type * FieldTypes[3];
    FieldTypes[0] = llvm::Type::getInt32Ty( Context);
    FieldTypes[1] = llvm::Type::getInt32Ty( Context);
    FieldTypes[2] = llvm::Type::getInt32Ty( Context);

    llvm::StructType *RecordType;
    RecordType = llvm::StructType::create(FieldTypes,"foo");

    llvm::Constant  *Foo;
    Foo = llvm::ConstantStruct::get( RecordType, Fields);

    llvm::Constant *Two;
    Two = llvm::ConstantInt::get( llvm::Type::getInt32Ty( Context), 2);

    llvm::Constant  *C;
    C = llvm::ConstantExpr::getGetElementPtr( RecordType, Foo, Two);

    return( 0);
clang -g -std=c++14 `llvm-config --cflags` -c foo.cpp
clang++ foo.o `llvm-config --cxxflags --ldflags --libs core 
--system-libs` -o foo

More information about the llvm-dev mailing list