Repository: pytorch
Issue: Vague linkage when using libcxxabi built torch in torch_glow
Labels: oncall: jit

## 🐛 Bug

When torch is built with clang tools and libcxxabi and torch_glow.to_glow is called we get a runtime exception[1]:
<pre>
--[1]--
>       return torch._C._jit_to_backend("glow", model, method_compile_spec)
E       RuntimeError: The following operation failed in the TorchScript interpreter.
E       Traceback of TorchScript (most recent call last):
E         File "<string>", line 10, in __setstate__
E                           self.__create_backend_debug_info()
E                       if self.__backend.is_available() :
E                           self.__handles = self.__backend.compile(self.__processed_module, self.__method_compile_spec)
E                                            ~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
E                       else:
E                           raise Exception("Backend is not available.")
E       RuntimeError: required keyword attribute 'name' has the wrong type
</pre>

## To Reproduce

Steps to reproduce the behavior:

1. Build Clang toolchain using bootstrap method:
git clone -b release/10.x --single-branch https://github.com/llvm/llvm-project.git
pushd llvm-project
mkdir build && cd build
1a. Build Clang with GCC
cmake -GNinja -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi;compiler-rt;lld;libunwind" -DLLVM_TARGETS_TO_BUILD="X86" -DCLANG_DEFAULT_RTLIB=compiler-rt -DCLANG_DEFAULT_CXX_STDLIB=libc++ -DCLANG_DEFAULT_LINKER=lld -DLIBCXXABI_USE_LLVM_UNWINDER=ON -DLIBCXX_USE_COMPILER_RT=ON -DLIBCXXABI_USE_COMPILER_RT=ON -DLIBUNWIND_USE_COMPILER_RT=ON -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_INSTALL_PREFIX=$GLOW_REQ -DCMAKE_PREFIX_PATH=$GLOW_REQ -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER=gcc ../llvm
ninja all
ninja install
1b. Build Clang toolchain with itself
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi;compiler-rt;lld;libunwind;openmp" -DLLVM_TARGETS_TO_BUILD="X86" -DCLANG_DEFAULT_RTLIB=compiler-rt -DCLANG_DEFAULT_CXX_STDLIB=libc++ -DCLANG_DEFAULT_LINKER=lld -DLIBCXXABI_USE_LLVM_UNWINDER=ON -DLIBCXX_USE_COMPILER_RT=ON -DLIBCXXABI_USE_COMPILER_RT=ON -DLIBUNWIND_USE_COMPILER_RT=ON -DLIBOMP_LIBFLAGS="-lm" -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_INSTALL_PREFIX=$GLOW_REQ -DCMAKE_PREFIX_PATH=$GLOW_REQ -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang ../llvm
ninja all
ninja install
popd
2. Build torch with Clang
git clone https://github.com/pytorch/pytorch.git --recursive --depth 1
pushd pytorch
CC=clang CXX=clang++ BUILD_BINARY=OFF BUILD_TEST=0 BUILD_CAFFE2_OPS=0 USE_FBGEMM=ON python setup.py install
popd
3. Build torch_glow with Clang
git clone git@github.com:pytorch/glow.git
pushd glow/torch_glow
CC=clang CXX=clang++ python setup.py --cmake_prefix_path=$GLOW_REQ install 
CC=clang CXX=clang++ python setup.py --cmake_prefix_path=$GLOW_REQ test
4. Run a testcase
python -m pytest tests/functionality/conv_to_glow_test.py

hits runtime exception[1]

## Expected behavior

Tests passing without any runtime exceptions.

## Environment

Please copy and paste the output from our
[environment collection script](https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py)
(or fill out the checklist below manually).
Collecting environment information...
PyTorch version: 1.10.0a0+git9157a28
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (GCC) 9.4.0
Clang version: 10.0.1 (GLOW_REQ/llvm-project/clang 7512b932fdef0ab951620d6807b47417f6ac7cd2)
CMake version: version 3.17.3
Libc version: glibc-2.9

Python version: 3.6.2 |Anaconda, Inc.| (default, Oct  5 2017, 07:59:26)  [GCC 7.2.0] (64-bit runtime)
Python platform: Linux-4.15.0-143-generic-x86_64-with-debian-buster-sid
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.5
[pip3] torch==1.10.0a0+git9157a28
[pip3] torch-glow==0.0.0(gitf51435bf0)
[pip3] torchvision==0.10.0
[conda] mkl                       2021.3.0           h06a4308_520
[conda] mkl-include               2021.3.0           h06a4308_520
[conda] numpy                     1.17.0           py36h99e49ec_0
[conda] numpy-base                1.17.0           py36h2f8d375_0

## Additional context
When torch is built with libgcc toolchain we do not see this runtime exception and test passes.
<!-- Add any other context about the problem here. -->
When _LIBCXX_DYNAMIC_FALLBACK is defined in libcxxabi($GLOW_REQ/llvm-project/libcxxabi/src/private_typeinfo.cpp) the runtime exception still occurs when torch_glow.to_glow is called but the error is different:
<pre>
>       return torch._C._jit_to_backend("glow", model, method_compile_spec)
E       RuntimeError: The following operation failed in the TorchScript interpreter.
E       Traceback of TorchScript (most recent call last):
E         File "<string>", line 6, in __setstate__
E                       self.__processed_module = state[1]
E                       self.__create_backend()
E                       self.__handles = self.__backend.compile(self.__processed_module, self.__method_compile_spec)
E                                        ~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
E
E       RuntimeError: Can't find class id in custom class type map
</pre>