checking for virtual members

I need a way easily check to make sure a function has no virtual functions, or other stuff that would inject extra stuff into an object or structure, as part of the validation for the vecreturn attribute. How do I do it?

Checking the CXXRecordDecl::PlainOldData flag with isPOD doesn’t work, because the the strict definition of POD goes too far for this case. Basically, I need a case like the following (to be build with clang -cc1 -fsyntax-only -faltivec -triple=powerpc file.cpp) to work:

class Scalar
{
public:
Scalar();
~Scalar();
private:
vector float m_value;
} attribute((vecreturn));

The following code in HandleVecReturnAttr produces the error because the class is not strictly POD:

if (isa(record)) {
if (!cast(record)->isPOD()) {
S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
return;
}
}

Thanks.

-John

Nov 1, 2010 kl. 5:05 PM skrev John Thompson:

I need a way easily check to make sure a function has no virtual functions, or other stuff that would inject extra stuff into an object or structure, as part of the validation for the vecreturn attribute. How do I do it?

Hi John,

CXXRecordDecl::isDynamicClass() checks if the class has a vtable (either virtual functions or virtual bases).

- Anders

Why? Is this what GCC does?

  - Doug

Why? Is this what GCC does?

Yes. The class example I enclosed previously will build with our gcc, whereas Clang would not, as Clang only allows a strict POD class or struct in the original code. I just posted a patch on cfe-commits to fix this.

-John

Why? Is this what GCC does?

Yes. The class example I enclosed previously will build with our gcc, whereas Clang would not, as Clang only allows a strict POD class or struct in the original code. I just posted a patch on cfe-commits to fix this.

Okay. This patch uses isDynamicClass(), which means that we still permit classes that involve multiple inheritance. Is that intended? Does it make sense?

It seems to me that the right language idea might be the C++0x standard-layout class, rather than the idea of a non-dynamic class.

  • Doug

I have no idea. Bascially, I just need something that tells me that there won’t be any other stuff in the actual object, since vecreturn wants to return the object in a vector register. Could you tell me which functions or function I should call?

Thanks.

-John

I have no idea. Bascially, I just need something that tells me that there won’t be any other stuff in the actual object, since vecreturn wants to return the object in a vector register. Could you tell me which functions or function I should call?

Not without knowing what the semantics are. Please try to nail down exactly what kinds of classes are/aren’t allowed with vecreturn, and we can find the right predicate to implement that in Clang. Unless GCC is doing something silly, we should follow their behavior.

  • Doug

Doug,

I’m afraid I don’t know the appropriate compiler terms to explain it most correctly. Basically, the main idea for the attribute is to optimize a class that has one data member, which must be a vector, such that the whole object can be stored in a vector register, thus avoiding referencing memory. Therefore, anything that would add extra storage, such as a vtable pointer would have to be detected. I don’t know that that would preclude multiple inheritance, but I would imagine it wouldn’t make sense to have multiple base classes that don’t add data members or virtual functions. I was thinking that just checking for a vtable and only one member which must be a vector would be sufficient.

Regarding the one data member restriction, if the class inherits from a base class, would those members directly appear in the CXXRecordDecl too? The current iterator look check I put in there assumes that, but I haven’t verified it yet.

Thanks.

-John

Doug,

I forgot to mention that the vecreturn attribute is probably mainly to be used for classes wrapping an AltiVec vector (at least that’s my assuption). My main interest at this point is just that my game compiles don’t error when this attribute is seen. The enclosed change seems to fix the current handler code (which I wrote), which produces errors for me because it erroneously checks for a POD class, which thus rules out most C++ class. May I check it in as is, unless there is some other function I should be calling?

-John

vecreturn6.patch (2.6 KB)

Is it really important that it have one member? If you hadn't said that
I'd suggest that the correct thing to check was whether it has a trivial
copy constructor.

John Bytheway