Subclassing LLVM Type

Hi,

I was wondering if it was possible to create a Dummy Type by subclassing LLVM Type.

I do not want to create a new type or even do much with this dummy type , all I need

is a placeholder type until I generate IR.

I want to use this approach as most of my AST is already configured to
accept a Type* and not a DummyType* ( which is my own class )

I explored this a bit and did manage to create one.

========== DummyType class =========

class DummyType : public llvm::Type
{
private:
std::string typeIdentifier;

public:
DummyType(const std::string& typeIdentifier,TypeID tyId):typeIdentifier(typeIdentifier),Type(TheContext,tyId)
{
}

std::string getTypeIdentifierString() const
{
return this->typeIdentifier ;
}

};

LLVM has its own lightweight dynamic casting infrastructure which the
Type hierarchy already makes use of.

You have to implement a "classof" function in your DummyType
definition. That means you'll need a new type ID. Appropriating an
unused one is slightly dodgy, but probably OK in practice if you're
careful.

After that you'd write "dyn_cast" instead of "dynamic_cast", since
that's what the LLVM version is called.

Cheers.

Tim.

This [1] might worth reading, Malhar.

[1] http://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html

Hi Tim,

Thanks for that , I have read the documentation on this , but I would like to know
if it would be the right thing to do , ie , changing the enum within llvm::Type

and I’m slightly unclear on how I can update the C binding ?

Can someone kindly clarify that ?

Thanks,

Malhar

Thanks for that , I have read the documentation on this , but I would like to know
if it would be the right thing to do , ie , changing the enum within llvm::Type

Well, I find the idea of a non-canonical DummyType slightly disturbing
(I don't think it's something we'd ever support officially). But you
might be able to make it work and if you need dyn_cast then modifying
that enum is about the only way to get it.

Do you know that there's such a thing as an "opaque type" in LLVM, by
the way? You haven't said what you need this DummyType for but it
might serve as a replacement. Basically you start off with an
unspecified struct type that you can then fill the details in for
later.

and I'm slightly unclear on how I can update the C binding ?
Can someone kindly clarify that ?

Can't help you there, I'm afraid. I've never used the C API and barely
looked at it.

Tim.

Hi Tim,

Thanks for your reply.

I am just interested in having a dyn_cast functionality for my dummy type.
I do not care if it behaves as a type in any other way nor require LLVM
Backend to interact with it in any way.

The language that I want to compile is actually like this -

(bits(N)) test2(bits(N) x, bits(N) y)
{

bits(N) res = x EOR y;

// return result ( implciilty extended it - simple )
return res;

}

(integer) main()
{
bits(5) a= ‘01100’;
bits(5) b= ‘01110’;

// on seeing function call , we concretize the types
// in the function template above.
bits(5) d = test2(a,b);
print(d);
}

My current AST accepts a concrete type.
The part in red is a non-concrete type.
I don’t want to change my AST structure to accept a
non-concrete type.

Hence I wish to use a dummy class.
The code I showed earlier works but the
only issue is I need LLVM “dyn_cast”.
Would changing the enum in the llvm::Type
class be enough ?

Thanks,
Malhar

I don't want to change my AST structure to accept a
non-concrete type.

That sounds like a hack to me. If it's expected to convey no actual
information what about an llvm::Optional, or even the dreaded nullptr?
A Type that's sort of valid sometimes but not really seems like the
worst of all possible worlds.

Would changing the enum in the llvm::Type
class be enough ?

No, you'd also have to implement "classof" in your DummyType.

Cheers.

Tim.

Hi,

I need to create a template function after parsing the following.
Later on seeing a fucntion call I concretize the types.-

( bits(N)) test ( bits(5) x , bits(M) y)
{
bits(N) x = x EOR y ;

}

Let’s say I manage to implement “classof” correctly
and dyn_cast<> works.

An issue that I think may be possible would be that I would
not be able to distinguish between “N” and “M”
here. I mean LLVM uses immutable types , and the llvm::Type
file mentions that I will need to modify llvm::Type::getPrimitiveType()
to return my new type , and there can only be a single instance.

If I am required to distinguish between “N” and “M” , I need to dyn_cast
and there must be more than one instance of my Type.

Thanks,
Malhar