Codegen from memory fails, works from .bc and .ll (X86 + NVPTX)


I’m working on getting LDC (the LVM D Compiler) to produce both host code and PTX with one pass though our front end.
Compiling for host works. Compiling for PTX works .We error out on the use of global variables in code destined for PTX.

I've hit what appears to be an impossible bug:

I can generate both .bc and .ll’s that can be passed to llc to generate .ptx but if I try to output it straight from the in memory representation I get

LLVM ERROR: Cannot select: 0x7f8854268fa0: i64 = GlobalTLSAddress<i32 addrspace(1)* @_variable> 0
In function: _function
(This error originates from SelectionDAGISel)

if I have a thread local global in my host code. If I change that to a c style global I get a segfault instead.

Both the host code and PTX code share a context, but are codegen'd into different LLVM modules.
Both ‘variable’ (defined in addrspace 0) and ‘function' are defined and used exclusively in the code for the host.
Code generation (IR construction and writing files out to disk) for the host is done before any codegen for PTX.

In the case of the thread local global it still appears to produce and successfully write to disk a valid .ptx
(and obviously doesn’t for the segfaulting case).

Tested with LDC master LLVM trunk (a few days old) on both windows and OS X.

A few questions, before I submit a bug report:

Is this an LLVM fault, or a producer fault?
If it is an LLVM fault, do you have any tips on how to get a test case (beyond LDC and the project that triggers it)?
A .bc is already just a dump of the in-memory representation isn’t it?

The LDC bug report: