microsoft / microsoft/STL

Clang doesn't propagate correct alignment for vector extensions to STL

Open
#3,608 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

info needed
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Describe the bug
We have a simd library based on the gcc/clang extensions. And it doesn't fail on 4 other platforms, but it does fail on Microsoft clang when using Microsoft STL. We had to add an __aligned__(16) into the ext_vector_type, or else it was getting stuck on 8B boundaries when used inside a std::array, std::unordered_array, and std::vector. But the library only sets __aligned__(16) on 32B/64B typically. The other clang builds can infer that float2 is 8B, and float3/4 are 16B types.

Here's a failing text case of the basic source code with the float3 case. We have exceptions and RTTI off. I have the following off too that affect the STL. It's a debug x64 build with typical compile settings.

CXXFLAGS += -D_ITERATOR_DEBUG_LEVEL=0 -D_D_HAS_EXCEPTIONS=0

The type when used in a struct or class with a vtable is aligned correctly. Adding a float4 on either side still aligns the float3 to 16B properly. It is only when used in a std:: container, that I suddenly see 8B alignment to the struct and then all alignment is off. I also have x64 struct alignment set to Default, so there isn't a /Zp8 set, and malloc is always return 16B since this is x64 when I use heap.

Command-line test case

#include <array>

// This should be aligned to 16B, but isn't below.  It's using 8B align.
typedef __attribute__((__ext_vector_type__(3))) float my_simd_float3;

// This works.
//typedef __attribute__((__ext_vector_type__(3),  __aligned__(16))) float my_simd_float3;

struct TestStruct
{
	my_simd_float3 v0 = { 1,0,0 };
};

class TestStruct1
{
public:
	// getting rid of this vtable works, since 8B alignment isn't there.
	virtual ~TestStruct1() {}
	std::array< TestStruct, 10 > testArray;
};

intmain()
{
	// This works.  Also makes case below not fail, but simply due to realignment.
	// TestStruct array[10] = {}; 

	// here's the crash repro, Microsoft STL isn't getting alignment
	TestStruct1 test;

   return 0;
}

Expected behavior
Expected that this code would be aligned to 16B like it requires. Otherwise we get simd crashes from data aligned to 8B. It's like it stuffs the vtable ptr into the class, and then immediately packs the simd vector (or matrix) after that. That's not supposed to happen.

STL version
Microsoft Visual Studio Professional 2019 (2)
Version 16.11.16
C++14 build

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the command-line test case and reproduce it using Microsoft Visual Studio 2019, C++14, and the shown ext_vector_type float3 definition. Compare alignment inside std::array and the surrounding class, then verify that the vector remains 16-byte aligned without requiring an explicit aligned(16) attribute.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.