root-project/cling

LLVM 13 static threadlocal error above -O0

jeaye opened this issue · 3 comments

jeaye commented

Overview

Given a static member function of a struct template, which returns a static threadlocal, Cling behaves correctly when the host program is compiled with -O0, but not with anything greater, including -O1.

Sample code

Modifying the cling-demo to look like the following reproduces the issue for me.

wrapper.hpp

#pragma once

template <typename T>
struct wrapper
{
  static T& global()
  {
    thread_local static T t{};
    return t;
  }
};

main.cpp

#include <iostream>
#include <array>

#include <cling/Interpreter/Interpreter.h>
#include <cling/Interpreter/Value.h>

#include "wrapper.hpp"

int main(int const argc, char const **argv)
{
  std::array<char const*, 2> cling_args{ argv[0], "-std=c++17" };
  cling::Interpreter jit(cling_args.size(), cling_args.data(), LLVMDIR);

  wrapper<int>::global() = 5;

  jit.process("#include <iostream>");
  jit.process("#include \"wrapper.hpp\"");
  jit.process("std::cout << wrapper<int>::global() << std::endl;");
}

CMakeLists.txt

# Just tack on -O1 somewhere to trigger the issue.
target_compile_options(cling-demo PUBLIC -DLLVMDIR="${LLVM_BINARY_DIR}" -O1)

Program output

# With -O0
❯ ./cling-demo 
5

# With -O1
❯ ./cling-demo 
IncrementalExecutor::executeFunction: symbol '__emutls_v._ZZN7wrapperIiE6globalEvE1t' unresolved while linking [cling interface function]!

System details

Arch Linux, Cling built manually.

llvm_url="http://root.cern.ch/git/llvm.git"
llvm_branch="cling-patches-rrelease_13"
clang_url="http://root.cern.ch/git/clang.git"
clang_branch="cling-patches-rrelease_13"
cling_url="http://root.cern.ch/git/cling.git"
cling_branch="master" # commit da247bd77a92f0793abe95e10b373dbca7a7e5f1
jeaye commented

Hi! Following up on this since it's still an issue for me. Is there more info I can provide to help diagnose the problem?

Do I understand #483 (comment) correctly that this can be closed as well?

jeaye commented