Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

A look at LLVM Advanced Data Types and trivially copyable types

April 1, 2019
Serge Guelton
Related topics:
Linux

Share:

    A few bugs have been lurking in the LLVM Bugzilla for a long time, namely #39427 and #35978, which are related to a custom implementation of the is_trivially_copyable data type, and they have a bad impact on the Application Binary Interface (ABI) of LLVM libraries. In this article, I will take a closer look at these issues and describe potential workarounds.

    The LLVM compiler infrastructure relies on several Advanced Data Types (ADT) to provide different speed/size trade-offs than the containers from the Standard Template Library (STL). Additionally, this ADT library provides features from future standard versions, but implemented in the C++ version (currently C++11) that LLVM supports as a code base. Finally, these ADTs must be compatible with the compiler requirements of the LLVM code base; basically, GCC version >= 4.8 and Clang version >= 3.1. (If you are interested in LLVM ADTs, Chandler Carruth did a nice talk on the subject at CppCon 2016.)

    Among these data types is the llvm::SmallVector type, an alternative to std::vector that uses in-place storage, if the array contains fewer than N + 1 elements, and heap storage otherwise. Interestingly, llvm::SmallVector has a specialization when T is known to be trivially copyable that allows less and faster data movement when pruning, copying, or moving data from one container to another. The specialization basically looks like this:

    template<class T>
    class SmallVectorBase {
       ... ;
    };
    template<class T>
    class SmallVector : public SmallVectorBase<T> {
       ... ;
    };
    

    Unfortunately, std::is_trivially_copyable is not supported by older versions of GCC, so the LLVM code base used to provide its own version in this (simplified) form:

    template<class T>
    struct is_trivially_copyable {
        static constexpr bool value =
        #if defined(__GNUC__) && __GNUC__ >= 5
            std::is_trivially_copyable<T>::value
        #else
            !std::is_class<T>::value
        #endif
        ;
    }
    

    There's an inherent problem in that implementation, and it's not a validity issue. Consider the following compilation units:

    // lib.cpp
    #include 
    struct DataType {
        struct SomeRandomType { int Value;};
        llvm::SmallVector<SomeRandomType> Data;
    };
        
    DataType Global;
    
    // user.cpp
    #include 
    struct DataType {
        struct SomeRandomType { int Value;};
        llvm::SmallVector<SomeRandomType> Data;
    };
       
    extern DataType Global;
    DataType Local;
    

    What happens if lib.cpp gets compiled with GCC 4.9 and user.cpp gets compiled with GCC 5.1? A quick look at the symbol table (e.g., through nm -C) shows that lib.o defines the symbol llvm::SmallVectorTemplateBase::SmallVectorTemplateBase(unsigned long), whereas user.o defines the symbol llvm::SmallVectorTemplateBase::SmallVectorTemplateBase(unsigned long). This approach can lead to various errors, from types with the same name but different layout to link errors.

    This type of scenario may happen on binary distribution, where the compiler used to compile system libraries and the compiler used to compile user code may differ, and it's one of the possible instances of ABI error. Avoiding such errors is one of the software packager's tasks.

    Workarounds

    As a workaround, llvm::is_trivially_copyable is specialized for various types to enforce the expected property, even if the trait implementation says the opposite. This is tedious to maintain, and error-prone (e.g., as of C++11, a pair of int is not trivially copyable; see https://godbolt.org/z/184QEc).

    What is the solution then? Avoid the per-compiler-version implementation of llvm::is_trivially_copyable and provide a generic one. This is not an easy task, as it is generally implemented as a compiler built-in (namely, __is_trivially_copyable for clang). Fortunately, there is a way out, but to understand it, we need to understand what it means to be trivially copyable. A trivially copyable type verifies the following properties:

    • Every copy constructor is trivial or deleted
    • Every move constructor is trivial or deleted
    • Every copy assignment operator is trivial or deleted
    • Every move assignment operator is trivial or deleted
    • Trivial non-deleted destructor

    Starting with C++17, there's also the requirement that at least a copy/move constructor or assignment operator must exist, but the LLVM code base is not affected by this (yet).

    Checking whether a constructor or assignment has been deleted can typically be achieved through a "substitution failure is not an error" (SFINAE), as in:

    template <class T>
    struct is_copy_assignable {
        template <class F>
        static auto get(F*) -> decltype(std::declval() = std::declval(), std::true_type{});
        static std::false_type get(...);
        static constexpr bool value = decltype(get((T*)nullptr))::value;
    };
    

    Checking whether the implementation is the default one is slightly trickier. Fortunately, starting with C++11:

    If a union contains a non-static data member with a non-trivial special
    member function (copy/move constructor, copy/move assignment, or
    destructor), that function is deleted by default in the union and needs to
    be defined explicitly by the programmer.

    This means that instantiating is_copy_assignable for the following type:

    template
    union trivial_helper {
        T t;
    };
    

    tells us whether the associated type is trivially copyable assignable.

    Once this tooling is set up, the trivially copyable trait can be summed up as:

    static constexpr bool value =
      has_trivial_destructor<T> &&
      (has_deleted_move_assign<T> || has_trivial_move_assign<T>) &&
      (has_deleted_move_constructor<T> || has_trivial_move_constructor<T>) &&
      (has_deleted_copy_assign<T> || has_trivial_copy_assign<T>) &&
      (has_deleted_copy_constructor<T> || has_trivial_copy_constructor<T>);
    

    To verify the consistency of the implementation with respect to the std one, the following guarded static assertion can be added:

    #ifdef HAVE_STD_IS_TRIVIALLY_COPYABLE
      static_assert(value == std::is_trivially_copyable<T>::value,
                    "inconsistent behavior between llvm:: and std:: implementation of is_trivially_copyable");
    #endif
    

    In the end, we get a fix for the ABI instability of the llvm::SmallVector implementation, hurray! As a sad (or happy, depending on your perspective) note, LLVM is getting close to requiring GCC 5.1 or higher, which makes this whole exploration obsolete.

    Last updated: March 28, 2019

    Recent Posts

    • Ollama or vLLM? How to choose the right LLM serving tool for your use case

    • How to build a Model-as-a-Service platform

    • How Quarkus works with OpenTelemetry on OpenShift

    • Our top 10 articles of 2025 (so far)

    • The benefits of auto-merging GitHub and GitLab repositories

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue