If the ‘GeneralDynamicTLSModel’ part is uncommented, I got a crash in the last line. I have searched for the whole day, no luck finding any resources guiding how to allocate a thread-local global variable.
Please give me some suggestions if there is something that I missed.
Nothing strikes me as incorrect here, what is the crash? At this point I’d build in debug more and attach a debugger, look at the stack trace to understand where is it crashing.
I’ve doing it for the whole day, but as a new comer, this LLVM implementation is pretty overwhelming to me.
I happen to notice this code in executionengine.cpp
void ExecutionEngine::emitGlobalVariable(const GlobalVariable *GV) {
void *GA = getPointerToGlobalIfAvailable(GV);
if (!GA) {
// If it's not already specified, allocate memory for the global.
GA = getMemoryForGV(GV);
// If we failed to allocate memory for this global, return.
if (!GA) return;
addGlobalMapping(GV, GA);
}
// Don't initialize if it's thread local, let the client do it.
if (!GV->isThreadLocal())
InitializeMemory(GV->getInitializer(), GA);
Type *ElTy = GV->getValueType();
size_t GVSize = (size_t)getDataLayout().getTypeAllocSize(ElTy);
NumInitBytes += (unsigned)GVSize;
++NumGlobals;
}
It is pretty clear that a thread_local global variable doesn’t get initialized. This makes sense to me because depending on the thread context, this may have a different initializer.
Do you have any exampling demonstrating how to initialize global variable like it says, ‘let the client do it’?
OK I thought that you were seeing a crash during compilation, not during execution.
I assume your LLVM is already built in debug mode (or with assertions enabled?), I would expect an error message instead of a crash.
I know that handling thread local in the JIT engine is a bit more tricky, but I’m not sure about the details, someone more knowledgeable will have to chime in here.
It turns out the same code works well on Ubuntu, but it does crash on Windows and MacOS. The bias behaviour across multiple system leads to my concern of how much I can trust this feature implementation inside LLVM, I guess I may have to figure out some workaround to tackle this problem.
I still won’t go that far to say LLVM is buggy, but the limited document and resources on this topic are blocking my progress, for which reason I will have to find alternative workaround.
Oh I didn’t know you were on Windows! The JIT support there is fairly recent I believe, and I not sure TLS is expected to work (It’d be nice to have a better error message than a crash though).