LLVM IR: 'class llvm::Instruction' has no member named 'getAddressSpace'

I have following LLVM IR instruction:

%ptrA = getelementptr float, float addrspace(1)* %A, i32 %id

To get the addrspace value, do I use function call getAddressSpace() or getPointerAddressSpace() ?

Using getAddressSpace() results in following error:

‘class llvm::Instruction’ has no member named ‘getAddressSpace’

But I see these methods in Instruction.h implemented for getelementptr inst class.

Thank you.

Regards,

Gurunath

P.S.: is this the right forum to ask such questions? I see very high-level discussions going on here. If this is not the right forum then please guide me to the appropriate one. Otherwise thanks.

You need to cast the instruction to the right subclass before being able to call the method that are implemented for the particular subclass.

If you have an “Instruction *” you can only call on it methods that defined in the class Instruction. (Note that this is not LLVM specific, this is generic C++).

LLVM has its own RTTI implementation, when you have an instruction and you don’t know if it is a particular subclass, you can “try” to cast it to the type you want:

if (GetElementPtrInst *GEP = dyn_cast(I)) {
… // use GEP
}

I have following LLVM IR instruction:

%ptrA = getelementptr float, float addrspace(1)* %A, i32 %id

To get the addrspace value, do I use function call getAddressSpace() or getPointerAddressSpace() ?

Using getAddressSpace() results in following error:

'class llvm::Instruction' has no member named 'getAddressSpace'

But I see these methods in Instruction.h implemented for getelementptr inst class.

GetElementPtrInst subclasses Instruction, so you need a cast. E.g:

  if (auto *GEP = dyn_cast<llvm::GetElementPtrInst>(I))
    return GEP->getAddressSpace();

P.S.: is this the right forum to ask such questions? I see very high-level discussions going on here. If this is not the right forum then please guide me to the appropriate one. Otherwise thanks.

llvm-dev is fine.

best,
vedant