6.4.2.12 LoongArch Attributes

The following attributes are supported by the LoongArch back end:

target (options)

This attribute applies to functions.

As discussed in Common Attributes, the target function attribute allows you to specify target-specific compilation options on a per-function basis. These options mirror the behavior of similar command-line options (see LoongArch Options).

Multiple options can be specified in the same attribute by separating them with a comma. For example:

__attribute__((target("arch=la64v1.1,lasx")))
int
foo (int a)
{
  return a + 5;
}

is valid and compiles function foo for LA64V1.1 with lasx.

These are the permitted options:

strict-align
no-strict-align

strict-align indicates that the compiler should not assume that unaligned memory references are handled by the system. To allow the compiler to assume that aligned memory references are handled by the system, the inverse attribute no-strict-align can be specified. The behavior is same as for the command-line option -mstrict-align and -mno-strict-align.

cmodel=

Indicates that code should be generated for a particular code model for this function. The behavior and permissible arguments are the same as for the command-line option -mcmodel=.

arch=

Specifies the architecture version and architectural extensions to use for this function. The behavior and permissible arguments are the same as for the -march= command-line option.

tune=

Specifies the core for which to tune the performance of this function. The behavior and permissible arguments are the same as for the -mtune= command-line option.

lsx
no-lsx

lsx indicates that vector instruction generation is allowed (not allowed) when compiling the function. The behavior is same as for the command-line option -mlsx and -mno-lsx.

lasx
no-lasx

lasx indicates that lasx instruction generation is allowed (not allowed) when compiling the function. The behavior is slightly different from the command-line option -mno-lasx. Example:

test.c:
typedef int v4i32
  __attribute__ ((vector_size(16),  aligned(16)));
v4i32 a, b, c;
#ifdef WITH_ATTR
__attribute__ ((target("no-lasx"))) void
#else
void
#endif
test ()
{
  c = a + b;
}

Compiled with

$ gcc test.c -o test.s -O2 -mlasx -DWITH_ATTR

128-bit vectorization is possible. But the following method cannot perform 128-bit vectorization.

$ gcc test.c -o test.s -O2 -mlasx -mno-lasx
recipe
no-recipe

recipe indicates that frecipe.{s/d} and frsqrt.{s/d} instruction generation is allowed (not allowed) when compiling the function. The behavior is same as for the command-line options -mrecipe and -mno-recipe.

div32
no-div32

div32 determines whether div.w[u] and mod.w[u] instructions on 64-bit machines are evaluated based only on the lower 32 bits of the input registers. The behavior is same as for the command-line options -mdiv32 and -mno-div32.

lam-bh
no-lam-bh

lam-bh indicates that am{swap/add}[_db].{b/h} instruction generation is allowed (not allowed) when compiling the function. The behavior is same as for the command-line options -mlam-bh and -mno-lam-bh.

lamcas
no-lamcas

lamcas indicates that amcas[_db].{b/h/w/d} instruction generation is allowed (not allowed) when compiling the function. The behavior is same as for the command-line options -mlamcas and -mno-lamcas.

scq
no-scq

scq indicates that sc.q instruction generation is allowed (not allowed) when compiling the function. The behavior is same as for the command-line options -mscq and -mno-scq.

ld-seq-sa
no-ld-seq-sa

ld-seq-sa indicates that same-address load-load barriers (dbar 0x700) are needed. The behavior is same as for the command-line options -mld-seq-sa and -mno-ld-seq-sa.

Specifying target attributes on individual functions or performing link-time optimization across translation units compiled with different target options can affect function inlining rules.

In particular, a function can be inlined only if the architectural features available to the callee are a subset of the features available to the caller.

Note that when the callee function does not have the always_inline attribute, it is not inlined if the code model of the caller function is different from the code model of the callee function.

target_clones (options)

This attribute applies to functions.

As discussed in Common Attributes, the target_clones attribute causes duplicate copies of the function to be compiled with each of the options provided. See Function Multiversioning, for more details.

options is a list of comma-separated strings. These options are allowed, and have similar meaning to those for the target attribute:

  • default
  • strict-align
  • arch=
  • lsx
  • lasx
  • frecipe
  • div32
  • lam-bh
  • lamcas
  • scq
  • ld-seq-sa

You can also set the priority of options (except ‘default’) in target_clones. For example:

__attribute__((target_clones ("default","arch=la64v1.1","lsx;priority=1")))
int
foo (int a)
{
  return a + 5;
}

The default priority from low to high is:

  • default
  • arch=loongarch64
  • strict-align
  • frecipe’, ‘div32’, ‘lam-bh’, ‘lamcas’, ‘scq’, ‘ld-seq-sa
  • lsx
  • arch=la64v1.0
  • arch=la64v1.1
  • lasx

If a priority is set for a option in target_clones, then the priority of this option is higher than ‘lasx’.

For example:

__attribute__((target_clones ("default","arch=la64v1.1","lsx;priority=1")))
int
foo (int a)
{
  return a + 5;
}

In this test case, the priority of ‘lsx’ is higher than that of ‘arch=la64v1.1’.

If the same priority is explicitly set for two options, the priority is still calculated according to the priority list above.

For example:

__attribute__((target_clones ("default",
                              "arch=la64v1.1;priority=1",
                              "lsx;priority=1")))
int
foo (int a)
{
  return a + 5;
}

In this test case, the priority of ‘arch=la64v1.1;priority=1’ is higher than that of ‘lsx;priority=1’.

Note that the option values on the GCC command line are not considered when calculating the priority.

target_version (option)

This attribute applies to functions.

As discussed in Common Attributes, the target_version attribute creates a version of the function matching the single option string provided. You can put this attribute on multiple definitions of the function with that name, which do not need to be identical. See Function Multiversioning, for more details.

The supported options and priorities that may appear in option are the same as for target_clones. Note that this attribute requires the GNU C Library version 2.38 or newer, that supports HWCAP.

For example, this code using the target_clones attribute:

__attribute__((target_clones ("default",
			      "arch=la64v1.1",
			      "lsx;priority=1")))
int
foo (int a)
{
  return a + 5;
}

is equivalent to this code using the target_version attribute:

__attribute__((target_version ("default")))
int
foo (int a)
{
  return a + 5;
}
__attribute__((target_version ("arch=la64v1.1")))
int
foo (int a)
{
  return a + 5;
}
__attribute__((target_version ("lsx;priority=1")))
int
foo (int a)
{
  return a + 5;
}
model("name")

This attribute applies to variables.

Use this variable attribute on the LoongArch to use a different code model for addressing this variable than the code model specified by the global -mcmodel option. This attribute is mostly useful if a section attribute and/or a linker script locates this object specially. Currently the only supported values of name are normal and extreme.