[llvm-dev] Subclassing LLVM Type

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

[llvm-dev] Subclassing LLVM Type

Gerolf Hoflehner via llvm-dev

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 ;
}
};


============================================

 Now , 


Type* ty = new DummyType()  works.


However , I wish to use "dynamic_cast<>" at runtime but I'm unable to do so.
example - DummyType* dum_type = dynamic_cast<DummyType>(ty) ; // where ty is a Type*


I get the following error - 

error: 'llvm::Type' is not polymorphic

Is there some way around this ( without suggesting a change to my entire AST ) ?
 Any help will be appreciated.

Thanks,
Malhar




_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Subclassing LLVM Type

Gerolf Hoflehner via llvm-dev
On 9 June 2017 at 12:03, Jajoo, Malhar via llvm-dev
<[hidden email]> wrote:
> Is there some way around this ( without suggesting a change to my entire AST ) ?

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.
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Subclassing LLVM Type

Gerolf Hoflehner via llvm-dev
This [1] might worth reading, Malhar.


2017-06-10 4:12 GMT+08:00 Tim Northover via llvm-dev <[hidden email]>:
On 9 June 2017 at 12:03, Jajoo, Malhar via llvm-dev
<[hidden email]> wrote:
> Is there some way around this ( without suggesting a change to my entire AST ) ?

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.
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



--
Wei-Ren Chen (陳韋任)
Homepage: https://people.cs.nctu.edu.tw/~chenwj

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Subclassing LLVM Type

Gerolf Hoflehner via llvm-dev

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


From: 陳韋任 <[hidden email]>
Sent: Friday, June 9, 2017 11:29:26 PM
To: Tim Northover
Cc: Jajoo, Malhar; [hidden email]
Subject: Re: [llvm-dev] Subclassing LLVM Type
 
This [1] might worth reading, Malhar.

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



2017-06-10 4:12 GMT+08:00 Tim Northover via llvm-dev <[hidden email]>:
On 9 June 2017 at 12:03, Jajoo, Malhar via llvm-dev
<[hidden email]> wrote:
> Is there some way around this ( without suggesting a change to my entire AST ) ?

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.
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



--
Wei-Ren Chen (陳韋任)
Homepage: https://people.cs.nctu.edu.tw/~chenwj

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Subclassing LLVM Type

Gerolf Hoflehner via llvm-dev
On 9 June 2017 at 15:29, Jajoo, Malhar via llvm-dev
<[hidden email]> wrote:
> 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.
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Subclassing LLVM Type

Gerolf Hoflehner via llvm-dev

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 


From: Tim Northover <[hidden email]>
Sent: Saturday, June 10, 2017 2:08:03 AM
To: Jajoo, Malhar
Cc: [hidden email]
Subject: Re: [llvm-dev] Subclassing LLVM Type
 
On 9 June 2017 at 15:29, Jajoo, Malhar via llvm-dev
<[hidden email]> wrote:
> 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.

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Subclassing LLVM Type

Gerolf Hoflehner via llvm-dev
On 9 June 2017 at 16:17, Jajoo, Malhar via llvm-dev
<[hidden email]> wrote:
> 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.
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Subclassing LLVM Type

Gerolf Hoflehner via llvm-dev

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


From: Tim Northover <[hidden email]>
Sent: Saturday, June 10, 2017 2:23:35 AM
To: Jajoo, Malhar
Cc: [hidden email]
Subject: Re: [llvm-dev] Subclassing LLVM Type
 
On 9 June 2017 at 16:17, Jajoo, Malhar via llvm-dev
<[hidden email]> wrote:
> 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.

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev