Bitwidth attribute in clang

Hello,

I am working on adding attribute support for arbitrary integer bit
width for clang. This is useful for hardware synthesis where smaller
integers translate to smaller circuits.

There is a similar open bug against llvm-gcc which explains this.
http://llvm.org/bugs/show_bug.cgi?id=1284

Example:
typedef int int5 __attribute__((bitwidth(5)));

I followed efriedma's 51577 patch and added the new attribute syntax
without a problem.
http://llvm.org/viewvc/llvm-project?view=rev&revision=51577

I wanted to consult with the list on the best way to implement this.
It looks like the current type classes cannot support arbitrary
bitlength integers. The BuiltinType class supports only the standard
types (UChar, UInt, Int, etc). Would you suggest deriving another new
type from Type (similar to complex, pointer, etc). After I do this I
will have to change many other places in the code.

Is there another better way to implement this ?

Thank you,
Nadav Rotem

Hello,

I am working on adding attribute support for arbitrary integer bit
width for clang. This is useful for hardware synthesis where smaller
integers translate to smaller circuits.

There is a similar open bug against llvm-gcc which explains this.
1284 – Support bitwidth attribute in llvm-gcc

Example:
typedef int int5 __attribute__((bitwidth(5)));

I followed efriedma's 51577 patch and added the new attribute syntax
without a problem.
http://llvm.org/viewvc/llvm-project?view=rev&revision=51577

I wanted to consult with the list on the best way to implement this.
It looks like the current type classes cannot support arbitrary
bitlength integers. The BuiltinType class supports only the standard
types (UChar, UInt, Int, etc). Would you suggest deriving another new
type from Type (similar to complex, pointer, etc). After I do this I
will have to change many other places in the code.

To create the new type, you will have to modify Type, ASTContext, and Sema (ProcessDeclAttribute). See VectorType/ExtVectorType for a couple types created from attributes. Once the type is created, you will need to hack Sema for type checking/analysis.

Is there another better way to implement this ?

Not really...you have the right idea.

snaroff

Hi Steve,

I enjoyed watching your presentation from the devmtg. It was very good.

I started with a different solution. I added a new 'unsigned bitwidth'
member to the BuiltinType. If it is set to zero then the default type
behaviour is used, otherwise the bitwidth field is used. With this
implementation I don't have to implement the signed/unsigned float/int
behaviour details.
Would this also work ?

I have another question, For this code :
typedef int int4 __attribute__((bitwidth(4)));

I added a handle for the new attribute. From the handle I am given a
"TypedefDecl" object. From this I would like to get the definition of
the new type ("int4") and change it. However, the getUnderlyingType()
API returns the Type object for *int. I could not find the API to get
the Type object for "int4".

Thank you,
Nadav

Hi Steve,

I enjoyed watching your presentation from the devmtg. It was very good.

Glad you enjoyed it!

I started with a different solution. I added a new 'unsigned bitwidth'
member to the BuiltinType. If it is set to zero then the default type
behaviour is used, otherwise the bitwidth field is used. With this
implementation I don't have to implement the signed/unsigned float/int
behaviour details.
Would this also work ?

I imagine so, however it doesn't seem as clean (since the behavior isn't a built-in property of C).

I have another question, For this code :
typedef int int4 __attribute__((bitwidth(4)));

I added a handle for the new attribute. From the handle I am given a
"TypedefDecl" object. From this I would like to get the definition of
the new type ("int4") and change it. However, the getUnderlyingType()
API returns the Type object for *int. I could not find the API to get
the Type object for "int4".

Have you looked at SemaDeclAttr.cpp:HandleModeAttr()? Is this GCC-ism close to what you are trying to accomplish?

snaroff