D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12033 - align() for array arguments of functions
Summary: align() for array arguments of functions
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-01-29 08:32 UTC by bearophile_hugs
Modified: 2020-03-21 03:56 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2014-01-29 08:32:15 UTC
At the moment this enhancement proposal is not fully fleshed out, but it's a starting point for future discussions.

Perhaps it can be useful to support a syntax like:


void foo(align(4) ubyte[] buf) {}
void bar(align(16) double[] arr) {}


In a debug build it performs a run-time test that the .ptr field is aligned to 4 or 16 bytes. This is very handy for many situations, including functions that need to work with the CPU SIMD registers.

It's equivalent to code like:

void bar(double[] arr)
in {
    assert(arr.ptr % 16 == 0);
} body {}


But ideally the D type system should carry around the "align(4)" information at compile-time as an optional part of the type of an array (and avoid some of those compile-time tests). Once this information is an optional part of the type of an array, a templated function can tell apart the two cases:

void bar(double[] arr) if (__traits(alignof, arr) == 16) {}
void bar(double[] arr) if (__traits(alignof, arr) != 16) {}

If an array type doesn't have a specified alignment, it is assumed to have the natural alignment of the type it contains.

I think that in a statically type system language a compile-time knowledge of the alignment of a pointer or array is a very valuable information in many cases. I find it strange that in D you can use int[5] to express the idea of a compile-time knowledge of an array of 5 ints, but you can't express the alignment of the data using the built-in type system of D.
Comment 1 basile-z 2020-02-20 15:53:13 UTC
This stuff cant work. alignment can only works on real declaration, not parameters. Also the .ptr address is controlled by the GC. The proper way to do that is to use AlignedMallocator + makeArray.