Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

A look at LLVM Advanced Data Types and trivially copyable types

April 1, 2019
Serge Guelton
Related topics:
Linux

    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

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.