1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
// GNU D Compiler attribute support declarations.
// Copyright (C) 2021 Free Software Foundation, Inc.
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
module gcc.attributes;
// Private helper templates.
private struct Attribute(A...)
{
A arguments;
}
private enum bool isStringValue(alias T) = is(typeof(T) == string);
private enum bool isStringOrIntValue(alias T)
= is(typeof(T) == string) || is(typeof(T) == int);
private template allSatisfy(alias F, T...)
{
static if (T.length == 0)
enum allSatisfy = true;
else static if (T.length == 1)
enum allSatisfy = F!(T[0]);
else
{
enum allSatisfy = allSatisfy!(F, T[ 0 .. $/2])
&& allSatisfy!(F, T[$/2 .. $ ]);
}
}
/**
* Generic entrypoint for applying GCC attributes to a function or type.
* There is no type checking done, as well as no deprecation path for
* attributes removed from the compiler. So the recommendation is to use any
, of the other UDAs available unless it is a target-specific attribute.
*
* Function attributes introduced by the @attribute UDA are used in the
* declaration of a function, followed by an attribute name string and
* any arguments separated by commas enclosed in parentheses.
*
* Example:
* ---
* import gcc.attributes;
*
* @attribute("regparm", 1) int func(int size);
* ---
*/
@system
auto attribute(A...)(A arguments)
if (A.length > 0 && is(A[0] == string))
{
return Attribute!A(arguments);
}
///////////////////////////////////////////////////////////////////////////////
//
// Supported common attributes exposed by GDC.
//
///////////////////////////////////////////////////////////////////////////////
/**
* The `@alloc_size` attribute may be applied to a function that returns a
* pointer and takes at least one argument of an integer or enumerated type.
* It indicates that the returned pointer points to memory whose size is given
* by the function argument at `sizeArgIdx`, or by the product of the arguments
* at `sizeArgIdx` and `numArgIdx`. Meaningful sizes are positive values less
* than `ptrdiff_t.max`. Unless `zeroBasedNumbering` is true, argument
* numbering starts at one for ordinary functions, and at two for non-static
* member functions.
*
* If `numArgIdx` is less than `0`, it is taken to mean there is no argument
* specifying the element count.
*
* Example:
* ---
* import gcc.attributes;
*
* @alloc_size(1) extern(C) void* malloc(size_t size);
* @alloc_size(3,2) extern(C) void* reallocarray(void *ptr, size_t nmemb,
* size_t size);
* @alloc_size(1,2) void* my_calloc(size_t element_size, size_t count,
* bool irrelevant);
* ---
*/
auto alloc_size(int sizeArgIdx)
{
return attribute("alloc_size", sizeArgIdx);
}
/// ditto
auto alloc_size(int sizeArgIdx, int numArgIdx)
{
return attribute("alloc_size", sizeArgIdx, numArgIdx);
}
/// ditto
auto alloc_size(int sizeArgIdx, int numArgIdx, bool zeroBasedNumbering)
{
return attribute("alloc_size", sizeArgIdx, numArgIdx, zeroBasedNumbering);
}
auto alloc_size(A...)(A arguments)
{
assert(false, "alloc_size attribute argument value is not an integer constant");
}
/**
* The `@always_inline` attribute inlines the function independent of any
* restrictions that otherwise apply to inlining. Failure to inline such a
* function is diagnosed as an error.
*
* Example:
* ---
* import gcc.attributes;
*
* @always_inline int func();
* ---
*/
enum always_inline = attribute("always_inline");
/**
* The `@cold` attribute on functions is used to inform the compiler that the
* function is unlikely to be executed. The function is optimized for size
* rather than speed and on many targets it is placed into a special subsection
* of the text section so all cold functions appear close together, improving
* code locality of non-cold parts of program. The paths leading to calls of
* cold functions within code are considered to be cold too.
*
* Example:
* ---
* import gcc.attributes;
*
* @cold int func();
* ---
*/
enum cold = attribute("cold");
/**
* The `@flatten` attribute is used to inform the compiler that every call
* inside this function should be inlined, if possible. Functions declared with
* attribute `@noinline` and similar are not inlined.
*
* Example:
* ---
* import gcc.attributes;
*
* @flatten int func();
* ---
*/
enum flatten = attribute("flatten");
/**
* The `@no_icf` attribute prevents a functions from being merged with another
* semantically equivalent function.
*
* Example:
* ---
* import gcc.attributes;
*
* @no_icf int func();
* ---
*/
enum no_icf = attribute("no_icf");
/**
* The `@noclone` attribute prevents a function from being considered for
* cloning - a mechanism that produces specialized copies of functions and
* which is (currently) performed by interprocedural constant propagation.
*
* Example:
* ---
* import gcc.attributes;
*
* @noclone int func();
* ---
*/
enum noclone = attribute("noclone");
/**
* The `@noinline` attribute prevents a function from being considered for
* inlining. If the function does not have side effects, there are
* optimizations other than inlining that cause function calls to be optimized
* away, although the function call is live. To keep such calls from being
* optimized away, put `asm { ""; }` in the called function, to serve as a
* special side effect.
*
* Example:
* ---
* import gcc.attributes;
*
* @noinline int func();
* ---
*/
enum noinline = attribute("noinline");
/**
* The `@noipa` attribute disables interprocedural optimizations between the
* function with this attribute and its callers, as if the body of the function
* is not available when optimizing callers and the callers are unavailable when
* optimizing the body. This attribute implies `@noinline`, `@noclone`, and
* `@no_icf` attributes. However, this attribute is not equivalent to a
* combination of other attributes, because its purpose is to suppress existing
* and future optimizations employing interprocedural analysis, including those
* that do not have an attribute suitable for disabling them individually.
*
* This attribute is supported mainly for the purpose of testing the compiler.
*
* Example:
* ---
* import gcc.attributes;
*
* @noipa int func();
* ---
*/
enum noipa = attribute("noipa");
/**
* The `@optimize` attribute is used to specify that a function is to be
* compiled with different optimization options than specified on the command
* line. Valid `arguments` are constant non-negative integers and strings.
* Multiple arguments can be provided, separated by commas to specify multiple
* options. Each numeric argument specifies an optimization level. Each string
* argument that begins with the letter O refers to an optimization option such
* as `-O0` or `-Os`. Other options are taken as suffixes to the `-f` prefix
* jointly forming the name of an optimization option.
*
* Not every optimization option that starts with the `-f` prefix specified by
* the attribute necessarily has an effect on the function. The `@optimize`
* attribute should be used for debugging purposes only. It is not suitable in
* production code.
*
* Example:
* ---
* import gcc.attributes;
*
* @optimize(2) double fn0(double x);
* @optimize("2") double fn1(double x);
* @optimize("s") double fn2(double x);
* @optimize("Ofast") double fn3(double x);
* @optimize("-O2") double fn4(double x);
* @optimize("tree-vectorize") double fn5(double x);
* @optimize("-ftree-vectorize") double fn6(double x);
* @optimize("no-finite-math-only", 3) double fn7(double x);
* ---
*/
auto optimize(A...)(A arguments)
if (allSatisfy!(isStringOrIntValue, arguments))
{
return attribute("optimize", arguments);
}
auto optimize(A...)(A arguments)
if (!allSatisfy!(isStringOrIntValue, arguments))
{
assert(false, "optimize attribute argument not a string or integer constant");
}
/**
* The `@restrict` attribute specifies that a function parameter is to be
* restrict-qualified in the C99 sense of the term. The parameter needs to
* boil down to either a pointer or reference type, such as a D pointer,
* class reference, or a `ref` parameter.
*
* Example:
* ---
* import gcc.attributes;
*
* void func(@restrict ref const float[16] array);
* ---
*/
enum restrict = attribute("restrict");
/**
* The `@section` attribute specifies that a function lives in a particular
* section. For when you need certain particular functions to appear in
* special sections.
*
* Some file formats do not support arbitrary sections so the section attribute
* is not available on all platforms. If you need to map the entire contents
* of a module to a particular section, consider using the facilities of the
* linker instead.
*
* Example:
* ---
* import gcc.attributes;
*
* @section("bar") extern void func();
* ---
*/
auto section(string sectionName)
{
return attribute("section", sectionName);
}
auto section(A...)(A arguments)
{
assert(false, "section attribute argument not a string constant");
}
/**
* The `@symver` attribute creates a symbol version on ELF targets. The syntax
* of the string parameter is `name@nodename`. The `name` part of the parameter
* is the actual name of the symbol by which it will be externally referenced.
* The `nodename` portion should be the name of a node specified in the version
* script supplied to the linker when building a shared library. Versioned
* symbol must be defined and must be exported with default visibility.
*
* Finally if the parameter is `name@@nodename` then in addition to creating a
* symbol version (as if `name@nodename` was used) the version will be also used
* to resolve `name` by the linker.
*
* Example:
* ---
* import gcc.attributes;
*
* @symver("foo@VERS_1") int foo_v1();
* ---
*/
auto symver(A...)(A arguments)
if (allSatisfy!(isStringValue, arguments))
{
return attribute("symver", arguments);
}
auto symver(A...)(A arguments)
if (!allSatisfy!(isStringValue, arguments))
{
assert(false, "symver attribute argument not a string constant");
}
/**
* The `@target` attribute is used to specify that a function is to be
* compiled with different target options than specified on the command line.
* One or more strings can be provided as arguments, separated by commas to
* specify multiple options. Each string consists of one or more
* comma-separated suffixes to the `-m` prefix jointly forming the name of a
* machine-dependent option.
*
* The target attribute can be used for instance to have a function compiled
* with a different ISA (instruction set architecture) than the default.
*
* The options supported are specific to each target.
*
* Example:
* ---
* import gcc.attributes;
*
* @target("arch=core2") void core2_func();
* @target("sse3") void sse3_func();
* ---
*/
auto target(A...)(A arguments)
if (allSatisfy!(isStringValue, arguments))
{
return attribute("target", arguments);
}
auto target(A...)(A arguments)
if (!allSatisfy!(isStringValue, arguments))
{
assert(false, "target attribute argument not a string constant");
}
/**
* The `@target_clones` attribute is used to specify that a function be cloned
* into multiple versions compiled with different target `options` than
* specified on the command line. The supported options and restrictions are
* the same as for `@target` attribute.
*
* It also creates a resolver function that dynamically selects a clone suitable
* for current architecture. The resolver is created only if there is a usage
* of a function with `@target_clones` attribute.
*
* Example:
* ---
* import gcc.attributes;
*
* @target_clones("sse4.1,avx,default") double func(double x);
* ---
*/
auto target_clones(A...)(A arguments)
if (allSatisfy!(isStringValue, arguments))
{
return attribute("target_clones", arguments);
}
auto target_clones(A...)(A arguments)
if (!allSatisfy!(isStringValue, arguments))
{
assert(false, "target attribute argument not a string constant");
}
/**
* The `@used` attribute, annotated to a function, means that code must be
* emitted for the function even if it appears that the function is not
* referenced. This is useful, for example, when the function is referenced
* only in inline assembly.
*
* Example:
* ---
* import gcc.attributes;
*
* @used __gshared int var = 0x1000;
* ---
*/
enum used = attribute("used");
/**
* The `@weak` attribute causes a declaration of an external symbol to be
* emitted as a weak symbol rather than a global. This is primarily useful in
* defining library functions that can be overridden in user code, though it can
* also be used with non-function declarations. The overriding symbol must have
* the same type as the weak symbol. In addition, if it designates a variable
* it must also have the same size and alignment as the weak symbol.
*
* Weak symbols are supported for ELF targets, and also for a.out targets when
* using the GNU assembler and linker.
*
* Example:
* ---
* import gcc.attributes;
*
* @weak int func() { return 1; }
* ---
*/
enum weak = attribute("weak");
/**
* The `@noplt` attribute is the counterpart to option `-fno-plt`. Calls to
* functions marked with this attribute in position-independent code do not use
* the PLT in position-independent code.
*
* In position-dependant code, a few targets also convert call to functions
* that are marked to not use the PLT to use the GOT instead.
*
* Example:
* ---
* import gcc.attributes;
*
* @noplt int func();
*
* ---
*/
enum noplt = attribute("noplt");
///////////////////////////////////////////////////////////////////////////////
//
// Attributes defined for compatibility with LDC.
//
///////////////////////////////////////////////////////////////////////////////
/**
* Specifies that the function returns `null` or a pointer to at least a
* certain number of allocated bytes. `sizeArgIdx` and `numArgIdx` specify
* the 0-based index of the function arguments that should be used to calculate
* the number of bytes returned.
*
* Example:
* ---
* import gcc.attributes;
*
* @allocSize(0) extern(C) void* malloc(size_t size);
* @allocSize(2,1) extern(C) void* reallocarray(void *ptr, size_t nmemb,
* size_t size);
* @allocSize(0,1) void* my_calloc(size_t element_size, size_t count,
* bool irrelevant);
* ---
*/
auto allocSize(int sizeArgIdx, int numArgIdx = int.min)
{
return alloc_size(sizeArgIdx, numArgIdx, true);
}
auto allocSize(A...)(A arguments)
{
assert(false, "allocSize attribute argument value is not an integer constant");
}
/**
* When applied to a global symbol, the compiler, assembler, and linker are
* required to treat the symbol as if there is a reference to the symbol that
* it cannot see (which is why they have to be named). For example, it
* prevents the deletion by the linker of an unreferenced symbol.
*
* Example:
* ---
* import gcc.attributes;
*
* @assumeUsed __gshared int var = 0x1000;
* ---
*/
alias assumeUsed = used;
/// This attribute has no effect.
enum dynamicCompile = false;
/// ditto
enum dynamicCompileConst = false;
/// ditto
enum dynamicCompileEmit = false;
/**
* Explicitly sets "fast-math" for a function, enabling aggressive math
* optimizations. These optimizations may dramatically change the outcome of
* floating point calculations (e.g. because of reassociation).
*
* Example:
* ---
* import gcc.attributes;
*
* @fastmath
* double dot(double[] a, double[] b) {
* double s = 0;
* foreach(size_t i; 0..a.length)
* {
* // will result in vectorized fused-multiply-add instructions
* s += a * b;
* }
* return s;
* }
* ---
*/
enum fastmath = optimize("Ofast");
/**
* Adds GCC's "naked" attribute to a function, disabling function prologue /
* epilogue emission.
* Intended to be used in combination with basic `asm` statement. While using
* extended `asm` or a mixture of basic `asm` and D code may appear to work,
* they cannot be depended upon to work reliably and are not supported.
*
* Example:
* ---
* import gcc.attributes;
*
* @naked void abort() {
* asm { "ud2"; }
* }
* ---
*/
enum naked = attribute("naked");
/**
* Sets the optimization strategy for a function.
* Valid strategies are "none", "optsize", "minsize". The strategies are
* mutually exclusive.
*
* Example:
* ---
* import gcc.attributes;
*
* @optStrategy("none")
* int func() {
* return call();
* }
* ---
*/
auto optStrategy(string strategy)
{
if (strategy == "none")
return optimize("O0");
else if (strategy == "optsize" || strategy == "minsize")
return optimize("Os");
else
{
assert(false, "unrecognized parameter `" ~ strategy
~ "` for `gcc.attribute.optStrategy`");
}
}
auto optStrategy(A...)(A arguments)
{
assert(false, "optStrategy attribute argument value is not a string constant");
}
/**
* When applied to a function, specifies that the function should be optimzed
* by Graphite, GCC's polyhedral optimizer. Useful for optimizing loops for
* data locality, vectorization and parallelism.
*
* Experimental!
*
* Only effective when GDC was built with ISL included.
*/
enum polly = optimize("loop-parallelize-all", "loop-nest-optimize");
|