arrow_back_ios Back to List

Structure Type Duplication

Offload KB - features

Old Content Alert

Please note that this is a old document archive and the will most likely be out-dated or superseded by various other products and is purely here for historical purposes.

Pointer types inside structs

By default pointer types of members inside structure definitions are __outer pointers. This is required to ensure structure type compatibility between PPU and SPU data.

struct T1
{
	void * ptr;
	T1()
	{
		ptr = 0;
	}
};

T1 ppudata;

void func()
{
	__blockingoffload()
	{
		T1 spudata = ppudata; // spudata and ppudata have the same type - member ptr is an __outer ptr.
	};
}

Structure type duplication with local pointers

Any reference to 'struct' in this document can also be applied to 'class' types as well.

For performance reasons it is often desirable to use the same structure declaration on the SPU so that fields with pointer types are now local pointers pointing to local fast SPU data.

Codeplay Offload therefore allows structure type duplication with pointer type substitution. Note that duplicated structure types will be incompatible with the original structure type (see example below).

Initialising a pointer field of a structure used in offload context with a local pointer value may substitute the default (outer) pointer type of a field with a local type. This substitution will create a new structure type.

struct T2
{
	void * ptr;
};

T2 global;
__blockingoffload
{
	T2 var; // original type; T2::ptr is an outer pointer
	var.ptr = & var; // var is of type 'T2 __attribute__((local(ptr)))'
	var = global;  // error incompatible types 'T2 __attribute__((local(ptr)))' = T2
}

This has various consequences for function duplication (i.e. the methods for this newly duplicated structure type will be separately compiled automatically).

Type substitution will happen in duplicated constructors (and also in other duplicated functions), so functions may return duplicated struct types. Once a non-duplicated field is used in an expression it cannot be duplicated with a local pointer.

struct T3
{
	T3();
	T3(int * arg): ptr (arg) {}
	int * ptr;
};

void func()
{
	__blockingoffload()
	{
		T3 var2 = T3(); // normal T3 because constructor definition not known yet
		int localvar;
		T3 var1 = T3(& localvar) // T3 __attribute__((local(ptr)))
	};
}

T3::T3()
{
	ptr = 0;
}

If required, duplicated structure types may be specified using the GNU attribute syntax which has been extended with the attribute: __local(field1, ... ,fieldn). This attribute can only follow a typename denoting a defined struct type. The identifiers field1..fieldn are the names of the pointer fields to be substituted for with local pointer types.

struct T4
{
	int * ptr;
	int * buffer;
	void do_something();
};

int main()
{
	__offload
	{
		T4 __attribute__((__local(ptr))) var; // declares var of type T4 with ptr as __inner, buffer as __outer
		var.do_something();//calling do_something on var with duplicated type
	}
}

Duplicated structure types contain local pointers and therefore can only be instantiated inside an offload context. Instantiations outside offload contexts will result in a compile error.