Unable to recover address space from a register?

I’m trying to recover the address space associated with a register and it’s underlying type, but I’m getting a LLT_INVALID from a simple code like this:

define i32 @load_from_addr_space(i32 addrspace(1)* %d) local_unnamed_addr #0 {
    %1 = load i32, i32 addrspace(1)* %d
    ret i32 %1

And I tried to plug it in the X86AsmPrinter::emitInstruction:

void X86AsmPrinter::emitInstruction(const MachineInstr *MI) {
  X86MCInstLower MCInstLowering(*MF, *this);

  // Dump address spaces
  for (auto &MO : MI->operands()) {
    if (MO.isReg()) {
      std::string Comment;
      raw_string_ostream CS(Comment);
      CS << "register: ";
      CS << MO.getReg().id();
      // Print address space
      auto ptrTy = MF->getRegInfo().getType(MO.getReg());
      CS << " ";

Though, none of the operands have an address space information. I can see that the memoperands() does have it.
But, if I wanted to recover at every single instruction which register holds a pointer with a specific address space, is that possible from a MachineInstr?
I can see in many places in the codebase that the access through getRegInfo() does provide address space information, but not sure why it is not working in the case above. (Maybe a problem between virtual reg vs physical reg?)


The register types are only for generic registers in GlobalISel, before instruction selection. They’re discarded and shouldn’t be relied on for selected instructions. The address space may be present in the memory operand, but we currently don’t have a guarantee any arbitrary instruction will preserve its memory operands (although we probably should)

1 Like

Thanks, that makes sense. I wanted to use them for GC roots, but I can see that the only way to recover GC roots is to use addrspace in conjunction with safepoints.

Incidentally, I watched yesterday the video 2019 LLVM Developers’ Meeting: M. Arsenault “Address Spaces in LLVM” - YouTube that triggered my interest, little world! :sweat_smile: