d: Update language attribute support, and implement gcc.attributes

D attribute support has been updated to have a baseline parity with the
LLVM D compiler's own `ldc.attributes'.

The handler that extracts GCC attributes from a list of UDAs has been
improved to take care of some mistakes that could have been warnings.
UDAs attached to field variables are also now processed for any GCC
attributes attached to them.

The following new attributes have been added to the D front-end:

 - @attribute("alloc_size")
 - @attribute("used")
 - @attribute("optimize")
 - @attribute("restrict")
 - @attribute("cold")
 - @attribute("noplt")
 - @attribute("target_clones")
 - @attribute("no_icf")
 - @attribute("noipa")
 - @attribute("symver")

With convenience aliases in a new `gcc.attributes' module to match
the same naming convention as `ldc.attributes':

 - @allocSize()
 - @assumeUsed
 - @fastmath
 - @naked
 - @restrict
 - @cold
 - @noplt
 - @optStrategy()
 - @polly
 - @section()
 - @target()
 - @weak

The old gcc.attribute module has been deprecated, along with the removal
of the following attribute handlers:

 - @attribute("alias"): Has been superseded by `pragma(mangle)'.
 - @attribute("forceinline"): Renamed to always_inline.

gcc/d/ChangeLog:

	* d-attribs.cc: Include fold-const.h and opts.h.
	(attr_noreturn_exclusions): Add alloc_size.
	(attr_const_pure_exclusions): Likewise.
	(attr_inline_exclusions): Add target_clones.
	(attr_noinline_exclusions): Rename forceinline to always_inline.
	(attr_target_exclusions): New array.
	(attr_target_clones_exclusions): New array.
	(attr_alloc_exclusions): New array.
	(attr_cold_hot_exclusions): New array.
	(d_langhook_common_attribute_table): Add new D attribute handlers.
	(build_attributes): Update to look for gcc.attributes.  Issue warning
	if not given a struct literal.  Handle void initialized arguments.
	(handle_always_inline_attribute): Remove function.
	(d_handle_noinline_attribute): Don't extract TYPE_LANG_FRONTEND.
	(d_handle_forceinline_attribute): Rename to...
	(d_handle_always_inline_attribute): ...this.  Remove special handling.
	(d_handle_flatten_attribute): Don't extract TYPE_LANG_FRONTEND.
	(d_handle_target_attribute): Likewise.  Warn about empty arguments.
	(d_handle_target_clones_attribute): New function.
	(optimize_args): New static variable.
	(parse_optimize_options): New function.
	(d_handle_optimize_attribute): New function.
	(d_handle_noclone_attribute): Don't extract TYPE_LANG_FRONTEND.
	(d_handle_alias_attribute): Remove function.
	(d_handle_noicf_attribute): New function.
	(d_handle_noipa_attribute): New function.
	(d_handle_section_attribute): Call the handle_generic_attribute target
	hook after performing target independent processing.
	(d_handle_symver_attribute): New function.
	(d_handle_noplt_attribute): New function.
	(positional_argument): New function.
	(d_handle_alloc_size_attribute): New function.
	(d_handle_cold_attribute): New function.
	(d_handle_restrict_attribute): New function.
	(d_handle_used_attribute): New function.
	* decl.cc (gcc_attribute_p): Update to look for gcc.attributes.
	(get_symbol_decl): Update decl source location of old prototypes to
	the new declaration being merged.
	* types.cc (layout_aggregate_members): Apply user defined attributes
	on fields.

libphobos/ChangeLog:

	* libdruntime/Makefile.am (DRUNTIME_DSOURCES): Add
	  gcc/attributes.d.
	* libdruntime/Makefile.in: Regenerate.
	* libdruntime/gcc/attribute.d: Deprecate module, publicly import
	gcc.attributes.
	* libdruntime/gcc/deh.d: Update imports.
	* libdruntime/gcc/attributes.d: New file.

gcc/testsuite/ChangeLog:

	* gdc.dg/gdc108.d: Update test.
	* gdc.dg/gdc142.d: Likewise.
	* gdc.dg/pr90136a.d: Likewise.
	* gdc.dg/pr90136b.d: Likewise.
	* gdc.dg/pr90136c.d: Likewise.
	* gdc.dg/pr95173.d: Likewise.
	* gdc.dg/attr_allocsize1.d: New test.
	* gdc.dg/attr_allocsize2.d: New test.
	* gdc.dg/attr_alwaysinline1.d: New test.
	* gdc.dg/attr_cold1.d: New test.
	* gdc.dg/attr_exclusions1.d: New test.
	* gdc.dg/attr_exclusions2.d: New test.
	* gdc.dg/attr_flatten1.d: New test.
	* gdc.dg/attr_module.d: New test.
	* gdc.dg/attr_noclone1.d: New test.
	* gdc.dg/attr_noicf1.d: New test.
	* gdc.dg/attr_noinline1.d: New test.
	* gdc.dg/attr_noipa1.d: New test.
	* gdc.dg/attr_noplt1.d: New test.
	* gdc.dg/attr_optimize1.d: New test.
	* gdc.dg/attr_optimize2.d: New test.
	* gdc.dg/attr_optimize3.d: New test.
	* gdc.dg/attr_optimize4.d: New test.
	* gdc.dg/attr_restrict1.d: New test.
	* gdc.dg/attr_section1.d: New test.
	* gdc.dg/attr_symver1.d: New test.
	* gdc.dg/attr_target1.d: New test.
	* gdc.dg/attr_targetclones1.d: New test.
	* gdc.dg/attr_used1.d: New test.
	* gdc.dg/attr_used2.d: New test.
	* gdc.dg/attr_weak1.d: New test.
	* gdc.dg/imports/attributes.d: New test.
This commit is contained in:
Iain Buclaw 2020-04-14 09:29:10 +02:00
parent 855ce47588
commit 8e84a14291
40 changed files with 1918 additions and 188 deletions

File diff suppressed because it is too large Load Diff

View File

@ -108,7 +108,7 @@ gcc_attribute_p (Dsymbol *decl)
if (md && md->packages && md->packages->length == 1) if (md && md->packages && md->packages->length == 1)
{ {
if (!strcmp ((*md->packages)[0]->toChars (), "gcc") if (!strcmp ((*md->packages)[0]->toChars (), "gcc")
&& !strcmp (md->id->toChars (), "attribute")) && !strcmp (md->id->toChars (), "attributes"))
return true; return true;
} }
@ -1124,6 +1124,10 @@ get_symbol_decl (Declaration *decl)
tree olddecl = decl->csym; tree olddecl = decl->csym;
decl->csym = get_symbol_decl (other); decl->csym = get_symbol_decl (other);
/* Update the symbol location to the current definition. */
if (DECL_EXTERNAL (decl->csym) && !DECL_INITIAL (decl->csym))
DECL_SOURCE_LOCATION (decl->csym) = DECL_SOURCE_LOCATION (olddecl);
/* The current declaration is a prototype or marked extern, merge /* The current declaration is a prototype or marked extern, merge
applied user attributes and return. */ applied user attributes and return. */
if (DECL_EXTERNAL (olddecl) && !DECL_INITIAL (olddecl)) if (DECL_EXTERNAL (olddecl) && !DECL_INITIAL (olddecl))

View File

@ -354,6 +354,7 @@ layout_aggregate_members (Dsymbols *members, tree context, bool inherited_p)
const char *ident = var->ident ? var->ident->toChars () : NULL; const char *ident = var->ident ? var->ident->toChars () : NULL;
tree field = create_field_decl (declaration_type (var), ident, tree field = create_field_decl (declaration_type (var), ident,
inherited_p, inherited_p); inherited_p, inherited_p);
apply_user_attributes (var, field);
insert_aggregate_field (context, field, var->offset); insert_aggregate_field (context, field, var->offset);
/* Because the front-end shares field decls across classes, don't /* Because the front-end shares field decls across classes, don't
@ -403,6 +404,7 @@ layout_aggregate_members (Dsymbols *members, tree context, bool inherited_p)
/* And make the corresponding data member. */ /* And make the corresponding data member. */
tree field = create_field_decl (type, NULL, 0, 0); tree field = create_field_decl (type, NULL, 0, 0);
apply_user_attributes (ad, field);
insert_aggregate_field (context, field, ad->anonoffset); insert_aggregate_field (context, field, ad->anonoffset);
continue; continue;
} }

View File

@ -0,0 +1,44 @@
// { dg-do compile }
import gcc.attributes;
@alloc_size(1)
int ignoredfunc(int size); // { dg-warning ".alloc_size. attribute ignored on a function returning .int." }
@alloc_size(0) int var; // { dg-warning ".alloc_size. attribute only applies to function types" }
@attribute("alloc_size", "1")
void* invalid1(int size); // { dg-warning ".alloc_size. attribute argument is invalid" }
@attribute("alloc_size", 1, "2")
void* invalid2(int count, int size); // { dg-warning ".alloc_size. attribute argument 2 is invalid" }
@attribute("alloc_size", 0.1)
void* wrongtype1(int size); // { dg-warning ".alloc_size. attribute argument has type .double." }
@attribute("alloc_size", 1, 0.2)
void* wrongtype2(int count, int size); // { dg-warning ".alloc_size. attribute argument 2 has type .double." }
@alloc_size(0)
void* malloc0(int size); // { dg-warning ".alloc_size. attribute argument value .0. does not refer to a function parameter" }
@alloc_size(1, 0)
void* malloc0(int count, int size); // { dg-warning ".alloc_size. attribute argument 2 value .0. does not refer to a function parameter" }
@alloc_size(1, 0, true)
void* malloc0pos(int count, int size);
@alloc_size(1, -1, true)
void* mallocminus1(int count, int size); // { dg-warning ".alloc_size. attribute argument 2 value .-1. does not refer to a function parameter" }
@alloc_size(99)
void* malloc99(int size); // { dg-warning ".alloc_size. attribute argument value .99. exceeds the number of function parameters 1" }
@alloc_size(1, 99)
void* malloc99(int count, int size); // { dg-warning ".alloc_size. attribute argument 2 value .99. exceeds the number of function parameters 2" }
@alloc_size(1)
void* mallocdouble(double size); // { dg-warning ".alloc_size. attribute argument value .1. refers to parameter type .double." }
@alloc_size(2, 1)
void* mallocdouble(int count, double size); // { dg-warning ".alloc_size. attribute argument 1 value .2. refers to parameter type .double." }

View File

@ -0,0 +1,13 @@
// { dg-do compile }
import gcc.attributes;
void* my_calloc(size_t num, size_t size) @allocSize(1, 0)
{
return null;
}
void* my_malloc(int a, int b, size_t size, int c) @allocSize(2)
{
return null;
}

View File

@ -0,0 +1,17 @@
// { dg-do compile }
// { dg-options "-O0" }
import gcc.attributes;
int func()
{
int nested_function() @always_inline
{
return 13;
}
return nested_function();
}
@always_inline int var = 0; // { dg-warning ".always_inline. attribute ignored" }
// { dg-final { scan-assembler-not "nested_function" } }

View File

@ -0,0 +1,13 @@
// { dg-do compile }
// { dg-options "-O2 -fdump-tree-optimized" }
import gcc.attributes;
int func() @cold
{
return 0;
}
@cold int var = 0; // { dg-warning ".cold. attribute ignored" }
// { dg-final { scan-tree-dump "func\[^\r\n\]*(unlikely executed)" "optimized" } }

View File

@ -0,0 +1,19 @@
// { dg-do compile }
import gcc.attributes;
// always_inline
@noinline
@always_inline
void i0(); // { dg-warning "ignoring attribute .always_inline. because it conflicts with attribute .noinline." }
@target_clones("")
@always_inline
void i1(); // { dg-warning "ignoring attribute .always_inline. because it conflicts with attribute .target_clones." }
// noinline
@always_inline
@noinline
void n0(); // { dg-warning "ignoring attribute .noinline. because it conflicts with attribute .always_inline." }

View File

@ -0,0 +1,19 @@
// { dg-do compile { target x86_64-*-* } }
import gcc.attributes;
// target
@target_clones("default")
@target("default")
void tc0(); // { dg-warning "ignoring attribute .target. because it conflicts with attribute .target_clones." }
// target_clones
@target("default")
@target_clones("default")
void t0(); // { dg-warning "ignoring attribute .target_clones. because it conflicts with attribute .target." }
@always_inline
@target_clones("default")
void tc1(); // { dg-warning "ignoring attribute .target_clones. because it conflicts with attribute .always_inline." }

View File

@ -0,0 +1,21 @@
// { dg-do compile }
// { dg-options "-O1" }
import gcc.attributes;
int func() @flatten
{
__gshared int count = 0;
int nested_function()
{
return count++;
}
static foreach (_; 0 .. 1000)
nested_function();
return nested_function();
}
@flatten int var = 0; // { dg-warning ".flatten. attribute ignored" }
// { dg-final { scan-assembler-not "nested_function" } }

View File

@ -0,0 +1,40 @@
// { dg-do compile }
// { dg-additional-sources "imports/attributes.d" }
import gcc.attributes;
@value_ignored
int f0()
{
return 0;
}
@type_symbol // { dg-warning ".type_symbol. attribute has no effect" }
int f1()
{
return 1;
}
@template_symbol // { dg-warning ".template_symbol. attribute has no effect" }
int f2()
{
return 2;
}
@struct_wrong_field(123) // { dg-warning "unknown attribute .123." }
int f3()
{
return 3;
}
@struct_void_init()
int f4()
{
return 4;
}
@unknown_attribute() // { dg-warning "unknown attribute .made up name." }
int f5()
{
return 5;
}

View File

@ -0,0 +1,12 @@
/* { dg-do compile } */
/* { dg-options "-O2" } */
import gcc.attributes;
@target_clones("avx", "default")
@noclone
void func() // { dg-error "clones for .target_clones. attribute cannot be created" }
{ // { dg-message "function .func. can never be copied because it has .noclone. attribute" "" { target *-*-* } .-1 }
}
@noclone int var = 0; // { dg-warning ".noclone. attribute ignored" }

View File

@ -0,0 +1,30 @@
// { dg-do compile }
// { dg-options "-O2 -fno-inline" }
import gcc.attributes;
extern int t();
int func()
{
__gshared int var = 42;
int nested_1() @no_icf
{
return var++;
}
int nested_2()
{
return var++;
}
int nested_3()
{
return var++;
}
return nested_1() + nested_2() + nested_3();
}
@no_icf int var = 0; // { dg-warning ".no_icf. attribute ignored" }
// { dg-final { scan-assembler "nested_1" } }
// { dg-final { scan-assembler "nested_2" } }
// { dg-final { scan-assembler-not "nested_3" } }

View File

@ -0,0 +1,19 @@
// { dg-do compile }
// { dg-options "-O2 -finline-functions -fno-ipa-icf" }
import gcc.attributes;
extern int t();
void func()
{
void nested_function() @noinline
{
t();
}
nested_function();
}
@noinline int var = 0; // { dg-warning ".noinline. attribute ignored" }
// { dg-final { scan-assembler "nested_function" } }

View File

@ -0,0 +1,17 @@
// { dg-do compile }
// { dg-options "-O2 -finline-functions" }
import gcc.attributes;
int func(int x)
{
int nested_function(int y, int z) @noipa
{
return y + z;
}
return nested_function(x, 0);
}
@noipa int var = 0; // { dg-warning ".noipa. attribute ignored" }
// { dg-final { scan-assembler "nested_function" } }

View File

@ -0,0 +1,13 @@
// { dg-do compile { target x86_64-*-linux* } }
// { dg-options "-O2 -fno-pic" }
import gcc.attributes;
@noplt int func();
@noplt int var = 0; // { dg-warning ".noplt. attribute ignored" }
int main()
{
return func();
}

View File

@ -0,0 +1,48 @@
// { dg-do compile }
import gcc.attributes;
int func()
{
int return_zero() @optimize(0)
{
return 0;
}
int return_one() @optimize("0")
{
return 1;
}
int return_two() @optimize("s")
{
return 2;
}
int return_three() @optimize("O3")
{
return 3;
}
int return_four() @optimize("fast-math")
{
return 4;
}
return return_one + return_two + return_three + return_four;
}
@optimize(3)
int var = 0; // { dg-warning ".optimize. attribute ignored" }
@optimize("-f_")
int bad_option() // { dg-warning "bad option .-f_. to attribute .optimize." }
{
return 0;
}
@optimize("-z")
int bad_option2() // { dg-warning "bad option .-z. to attribute .optimize." }
{
return 0;
}

View File

@ -0,0 +1,9 @@
// { dg-do compile }
import gcc.attributes;
@optimize(-1)
int non_negative() // { dg-error "argument to .-O. should be a non-negative integer" }
{
return 0;
}

View File

@ -0,0 +1,19 @@
// { dg-do compile }
// { dg-options "-O0 -fdump-tree-optimized-raw" }
import gcc.attributes;
double notfast(double x)
{
return x * x * x * x * x * x * x * x;
}
// { dg-final { scan-tree-dump-times "mult_expr, _" 7 "optimized" } }
@fastmath
static double fast(double x)
{
return x * x * x * x * x * x * x * x;
}
// { dg-final { scan-tree-dump-times "mult_expr, powmult_" 3 "optimized" } }

View File

@ -0,0 +1,45 @@
// { dg-do compile }
// { dg-options "-O3 -fdump-tree-optimized-raw" }
import gcc.attributes;
int glob1;
int easily_inlinable(int i) { glob1 = i; return 2; }
@optStrategy("none")
int call_easily_inlinable(int i)
{
return easily_inlinable(i);
}
// { dg-final { scan-tree-dump "gimple_call <easily_inlinable" "optimized" } }
pragma(inline, true) int always_inline() { return 321; }
@optStrategy("none")
int call_always_inline()
{
return always_inline();
}
// { dg-final { scan-tree-dump "gimple_call <always_inline" "optimized" } }
int glob2;
@optStrategy("none") void optnone_function(int i) { glob2 = i; }
void call_optnone()
{
optnone_function(1);
}
// { dg-final { scan-tree-dump "gimple_call <optnone_function" "optimized" } }
@optStrategy("optsize")
void optsize_fn()
{
}
@optStrategy("minsize")
void minsize_fn()
{
}

View File

@ -0,0 +1,18 @@
// { dg-do compile }
// { dg-options "-fdump-tree-optimized" }
import gcc.attributes;
int func(@restrict int ignored) // { dg-warning ".restrict. attribute ignored" }
{
return 0;
}
int func(@restrict int *parm)
{
return 0;
}
@restrict int var = 0; // { dg-warning ".restrict. attribute ignored" }
// { dg-final { scan-tree-dump "restrict parm" "optimized" } }

View File

@ -0,0 +1,23 @@
// { dg-do compile }
// { dg-require-named-sections "" }
import gcc.attributes;
@section("types")
struct S {} // { dg-warning ".section. attribute does not apply to types" }
@attribute("section", 123)
int f1(); // { dg-error "section attribute argument not a string constant" }
int f2(@section("param") int a) // { dg-error "section attribute not allowed for .a." }
{
@section("local") int v; // { dg-error "section attribute cannot be specified for local variables" }
return a;
}
@section("c1") void conflict();
@section("c2") void conflict(); // { dg-error "section of .conflict. conflicts with previous declaration" }
@section("c3")
@section("c4")
void conflict2(); // { dg-error "section of .conflict2. conflicts with previous declaration" }

View File

@ -0,0 +1,24 @@
// { dg-do compile }
// { dg-skip-if "only works for ELF targets" { *-*-darwin* *-*-aix* } }
import gcc.attributes;
@symver("type")
struct S {} // { dg-warning ".symver. attribute does not apply to types" }
@attribute("symver", 123)
int f1(); // { dg-error ".symver. attribute argument not a string constant" }
@symver("format")
int f2() // { dg-error "symver attribute argument must have format .name@nodename'" }
{ // { dg-error ".symver. attribute argument .format. must contain one or two .@." "" { target *-*-* } .-1 }
return 0;
}
int f3(@symver("param@VER_1") int param) // { dg-warning ".symver. attribute only applies to functions and variables" }
{
return param;
}
@symver("extern@VER_2")
extern int f4(); // { dg-error "symbol needs to be defined to have a version" }

View File

@ -0,0 +1,20 @@
// { dg-do compile { target i?86*-*-* x86_64-*-* } }
import gcc.attributes;
@target("default")
int foo() { return 1; }
@target("arch=core2", "")
int foo2() { return 2; } // { dg-warning "empty string in attribute .target." }
@target("sse4.2", "", "")
int foo3() { return 3; } // { dg-warning "empty string in attribute .target." }
@target("default")
int var = 0; // { dg-warning ".target. attribute ignored" }
int main()
{
return foo() + foo2() + foo3();
}

View File

@ -0,0 +1,12 @@
// { dg-do compile { target i?86*-*-* x86_64-*-* } }
import gcc.attributes;
@target_clones("default")
int func() // { dg-warning "single .target_clones. attribute is ignored" }
{
return 0;
}
@target_clones("default")
int var = 0; // { dg-warning ".target_clones. attribute ignored" }

View File

@ -0,0 +1,16 @@
// { dg-do compile }
// { dg-options "-O2" }
import gcc.attributes;
int func()
{
int nested_function() @used
{
return 1;
}
@used int var = 0; // { dg-warning ".used. attribute ignored" }
return var;
}
// { dg-final { scan-assembler "nested_function" } }

View File

@ -0,0 +1,16 @@
// { dg-do compile }
// { dg-options "-O2" }
import gcc.attributes;
int func()
{
int nested_function() @assumeUsed
{
return 1;
}
@assumeUsed int var = 0; // { dg-warning ".used. attribute ignored" }
return var;
}
// { dg-final { scan-assembler "nested_function" } }

View File

@ -0,0 +1,14 @@
// { dg-do compile }
import gcc.attributes;
pragma(inline, true)
@weak int func() // { dg-warning "inline function .func. declared weak" }
{
return 0;
}
int parm(@weak int a) // { dg-warning ".weak. attribute ignored" }
{
return a;
}

View File

@ -1,9 +1,9 @@
// https://bugzilla.gdcproject.org/show_bug.cgi?id=108 // https://bugzilla.gdcproject.org/show_bug.cgi?id=108
// { dg-do compile } // { dg-do compile }
import gcc.attribute; import gcc.attributes;
@attribute("forceinline") @attribute("always_inline")
void forceinline108() void forceinline108()
{ {
} }

View File

@ -1,7 +1,7 @@
// https://bugzilla.gdcproject.org/show_bug.cgi?id=142 // https://bugzilla.gdcproject.org/show_bug.cgi?id=142
// { dg-do compile } // { dg-do compile }
import gcc.attribute; import gcc.attributes;
@attribute("noinline") @attribute("noinline")
int test142a()() int test142a()()

View File

@ -0,0 +1,28 @@
// Pretend to be the GDC attributes module.
module gcc.attributes;
immutable value_ignored = 42;
enum type_symbol { _ }
auto template_symbol(A...)(A arguments)
{
return arguments;
}
struct struct_wrong_field
{
int bad_field;
}
struct struct_void_init
{
string name_field = "noinline";
int void_field = void;
string ignored;
}
struct unknown_attribute
{
string name_field = "made up name";
}

View File

@ -3,9 +3,9 @@
// { dg-options "-fdump-tree-optimized" } // { dg-options "-fdump-tree-optimized" }
// { dg-final { scan-tree-dump-times "sum_array \\(array\\)" 0 "optimized"} } // { dg-final { scan-tree-dump-times "sum_array \\(array\\)" 0 "optimized"} }
import gcc.attribute; import gcc.attributes;
@attribute("forceinline") int sum_array(int[] input); @attribute("always_inline") int sum_array(int[] input);
int sum_array(int[] input) int sum_array(int[] input)
{ {

View File

@ -3,7 +3,7 @@
// { dg-options "-fdump-tree-optimized" } // { dg-options "-fdump-tree-optimized" }
// { dg-final { scan-tree-dump-times "sum_array \\(array\\)" 1 "optimized"} } // { dg-final { scan-tree-dump-times "sum_array \\(array\\)" 1 "optimized"} }
import gcc.attribute; import gcc.attributes;
@attribute("noinline") int sum_array(int[] input); @attribute("noinline") int sum_array(int[] input);

View File

@ -2,8 +2,8 @@
// { dg-do compile } // { dg-do compile }
// { dg-options "-Wattributes" } // { dg-options "-Wattributes" }
import gcc.attribute; import gcc.attributes;
@attribute("forceinline") int sum_array(int[] input); @attribute("always_inline") int sum_array(int[] input);
@attribute("noinline") int sum_array(int[] input); @attribute("noinline") int sum_array(int[] input);
// { dg-warning "ignoring attribute .noinline. because it conflicts with attribute .forceinline." "" { target *-*-* } .-1 } // { dg-warning "ignoring attribute .noinline. because it conflicts with attribute .always_inline." "" { target *-*-* } .-1 }

View File

@ -2,7 +2,7 @@
// { dg-do compile } // { dg-do compile }
// { dg-options "-Wattributes" } // { dg-options "-Wattributes" }
import gcc.attribute; import gcc.attributes;
@attribute("foo") // { dg-warning "unknown attribute .foo." } @attribute("foo") // { dg-warning "unknown attribute .foo." }
void f95173() void f95173()

View File

@ -184,8 +184,8 @@ DRUNTIME_DSOURCES = core/atomic.d core/attribute.d core/bitop.d \
core/thread/threadbase.d core/thread/threadgroup.d core/thread/types.d \ core/thread/threadbase.d core/thread/threadgroup.d core/thread/types.d \
core/time.d core/vararg.d core/volatile.d gc/bits.d gc/config.d \ core/time.d core/vararg.d core/volatile.d gc/bits.d gc/config.d \
gc/gcinterface.d gc/impl/conservative/gc.d gc/impl/manual/gc.d gc/os.d \ gc/gcinterface.d gc/impl/conservative/gc.d gc/impl/manual/gc.d gc/os.d \
gc/pooltable.d gc/proxy.d gcc/attribute.d gcc/backtrace.d \ gc/pooltable.d gc/proxy.d gcc/attribute.d gcc/attributes.d \
gcc/builtins.d gcc/deh.d gcc/emutls.d gcc/gthread.d \ gcc/backtrace.d gcc/builtins.d gcc/deh.d gcc/emutls.d gcc/gthread.d \
gcc/sections/android.d gcc/sections/elf_shared.d gcc/sections/osx.d \ gcc/sections/android.d gcc/sections/elf_shared.d gcc/sections/osx.d \
gcc/sections/package.d gcc/sections/win32.d gcc/sections/win64.d \ gcc/sections/package.d gcc/sections/win32.d gcc/sections/win64.d \
gcc/unwind/arm.d gcc/unwind/arm_common.d gcc/unwind/c6x.d \ gcc/unwind/arm.d gcc/unwind/arm_common.d gcc/unwind/c6x.d \

View File

@ -209,11 +209,12 @@ am__objects_1 = core/atomic.lo core/attribute.lo core/bitop.lo \
core/thread/types.lo core/time.lo core/vararg.lo \ core/thread/types.lo core/time.lo core/vararg.lo \
core/volatile.lo gc/bits.lo gc/config.lo gc/gcinterface.lo \ core/volatile.lo gc/bits.lo gc/config.lo gc/gcinterface.lo \
gc/impl/conservative/gc.lo gc/impl/manual/gc.lo gc/os.lo \ gc/impl/conservative/gc.lo gc/impl/manual/gc.lo gc/os.lo \
gc/pooltable.lo gc/proxy.lo gcc/attribute.lo gcc/backtrace.lo \ gc/pooltable.lo gc/proxy.lo gcc/attribute.lo gcc/attributes.lo \
gcc/builtins.lo gcc/deh.lo gcc/emutls.lo gcc/gthread.lo \ gcc/backtrace.lo gcc/builtins.lo gcc/deh.lo gcc/emutls.lo \
gcc/sections/android.lo gcc/sections/elf_shared.lo \ gcc/gthread.lo gcc/sections/android.lo \
gcc/sections/osx.lo gcc/sections/package.lo \ gcc/sections/elf_shared.lo gcc/sections/osx.lo \
gcc/sections/win32.lo gcc/sections/win64.lo gcc/unwind/arm.lo \ gcc/sections/package.lo gcc/sections/win32.lo \
gcc/sections/win64.lo gcc/unwind/arm.lo \
gcc/unwind/arm_common.lo gcc/unwind/c6x.lo \ gcc/unwind/arm_common.lo gcc/unwind/c6x.lo \
gcc/unwind/generic.lo gcc/unwind/package.lo gcc/unwind/pe.lo \ gcc/unwind/generic.lo gcc/unwind/package.lo gcc/unwind/pe.lo \
object.lo rt/aApply.lo rt/aApplyR.lo rt/aaA.lo rt/adi.lo \ object.lo rt/aApply.lo rt/aApplyR.lo rt/aaA.lo rt/adi.lo \
@ -816,8 +817,8 @@ DRUNTIME_DSOURCES = core/atomic.d core/attribute.d core/bitop.d \
core/thread/threadbase.d core/thread/threadgroup.d core/thread/types.d \ core/thread/threadbase.d core/thread/threadgroup.d core/thread/types.d \
core/time.d core/vararg.d core/volatile.d gc/bits.d gc/config.d \ core/time.d core/vararg.d core/volatile.d gc/bits.d gc/config.d \
gc/gcinterface.d gc/impl/conservative/gc.d gc/impl/manual/gc.d gc/os.d \ gc/gcinterface.d gc/impl/conservative/gc.d gc/impl/manual/gc.d gc/os.d \
gc/pooltable.d gc/proxy.d gcc/attribute.d gcc/backtrace.d \ gc/pooltable.d gc/proxy.d gcc/attribute.d gcc/attributes.d \
gcc/builtins.d gcc/deh.d gcc/emutls.d gcc/gthread.d \ gcc/backtrace.d gcc/builtins.d gcc/deh.d gcc/emutls.d gcc/gthread.d \
gcc/sections/android.d gcc/sections/elf_shared.d gcc/sections/osx.d \ gcc/sections/android.d gcc/sections/elf_shared.d gcc/sections/osx.d \
gcc/sections/package.d gcc/sections/win32.d gcc/sections/win64.d \ gcc/sections/package.d gcc/sections/win32.d gcc/sections/win64.d \
gcc/unwind/arm.d gcc/unwind/arm_common.d gcc/unwind/c6x.d \ gcc/unwind/arm.d gcc/unwind/arm_common.d gcc/unwind/c6x.d \
@ -1209,6 +1210,7 @@ gcc/$(am__dirstamp):
@$(MKDIR_P) gcc @$(MKDIR_P) gcc
@: > gcc/$(am__dirstamp) @: > gcc/$(am__dirstamp)
gcc/attribute.lo: gcc/$(am__dirstamp) gcc/attribute.lo: gcc/$(am__dirstamp)
gcc/attributes.lo: gcc/$(am__dirstamp)
gcc/backtrace.lo: gcc/$(am__dirstamp) gcc/backtrace.lo: gcc/$(am__dirstamp)
gcc/builtins.lo: gcc/$(am__dirstamp) gcc/builtins.lo: gcc/$(am__dirstamp)
gcc/deh.lo: gcc/$(am__dirstamp) gcc/deh.lo: gcc/$(am__dirstamp)

View File

@ -20,14 +20,7 @@
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>. // <http://www.gnu.org/licenses/>.
deprecated("Import gcc.attributes instead")
module gcc.attribute; module gcc.attribute;
private struct Attribute(A...) public import gcc.attributes;
{
A args;
}
auto attribute(A...)(A args) if (A.length > 0 && is(A[0] == string))
{
return Attribute!A(args);
}

View File

@ -0,0 +1,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");

View File

@ -28,7 +28,7 @@ import gcc.unwind;
import gcc.unwind.pe; import gcc.unwind.pe;
import gcc.builtins; import gcc.builtins;
import gcc.config; import gcc.config;
import gcc.attribute; import gcc.attributes;
extern(C) extern(C)
{ {