Jesper Louis Andersen
2009-Feb-28 14:22 UTC
[LLVMdev] Question about documentation of BitCast CExps.
In the documentation at [http://llvm.org/docs/LangRef.html], we have the section [#aggregateconstants] telling us that the aggregate constants are: * Structure constants * Array constants * Vector constants * Zeroinitialization However, in [#constantexprs] we have a bitcast(CST, TYPE) operator with the documentation (emphasis by me): Convert a constant, CST, to another TYPE. The size of CST and TYPE must be identical (same number of bits). The conversion is done as if the CST value was stored to memory and read back as TYPE. In other words, no bits change with this operator, just the type. {{{This can be used for conversion of vector types to any other type, as long as they have the same bit width}}}. For pointers it is only valid to cast to another pointer type. {{{It is not valid to bitcast to or from an aggregate type.}}} The first emphasis states that it is possible to use the bitcast operation for conversion of vector types. However, the second emphasis says that this is not valid (A vector is an aggregate type). So my questions are: What is right? Is this a mistake? Am I reading this wrong in any way? If so, where? The reason I am asking is because I am in the process of implementing a rudimentary type-checker phase for LLVM in Standard ML. It is mostly done because it is a tremendous way to learn about the finer details of the language. I am forced to think and know details about LLVM because of the process, so hence my question. -- J.
Nick Lewycky
2009-Feb-28 17:39 UTC
[LLVMdev] Question about documentation of BitCast CExps.
Jesper Louis Andersen wrote:> In the documentation at [http://llvm.org/docs/LangRef.html], we have > the section [#aggregateconstants] telling us that the aggregate > constants are: > > * Structure constants > * Array constants > * Vector constants > * Zeroinitialization > > However, in [#constantexprs] we have a bitcast(CST, TYPE) operator > with the documentation (emphasis by me): > > Convert a constant, CST, to another TYPE. The size of CST and TYPE > must be identical (same number of bits). The conversion is done as if > the CST value was stored to memory and read back as TYPE. In other > words, no bits change with this operator, just the type. {{{This can > be used for conversion of vector types to any other type, as long as > they have the same bit width}}}. For pointers it is only valid to cast > to another pointer type. {{{It is not valid to bitcast to or from an > aggregate type.}}}Thanks for the report. I've updated this text to read: "Convert a constant, CST, to another TYPE. The size of CST and TYPE must be identical (same number of bits). The conversion is done as if the CST value was stored to memory and read back as TYPE. In other words, no bits change with this operator, just the type. This can be used for conversion of aggregate types to any aggregate type, as long as they have the same bit width. Vector types may also be casted to and from any other type as long as they have the same bit width. For pointers it is only valid to cast to another pointer type." This should make it clear that aggregates may be bitcasted to other aggregates, and that additionally vectors may be bitcasted to and from other types, but not pointers or labels or functions or void, because those will never have the same width as a vector (tricky!). Nick> The first emphasis states that it is possible to use the bitcast > operation for conversion of vector types. However, the second emphasis > says that this is not valid (A vector is an aggregate type). So my > questions are: > > What is right? > Is this a mistake? > Am I reading this wrong in any way? If so, where? > > The reason I am asking is because I am in the process of implementing > a rudimentary type-checker phase for LLVM in Standard ML. It is mostly > done because it is a tremendous way to learn about the finer details > of the language. I am forced to think and know details about LLVM > because of the process, so hence my question. >
Chris Lattner
2009-Feb-28 18:34 UTC
[LLVMdev] Question about documentation of BitCast CExps.
On Feb 28, 2009, at 6:22 AM, Jesper Louis Andersen wrote:> In the documentation at [http://llvm.org/docs/LangRef.html], we have > the section [#aggregateconstants] telling us that the aggregate > constants are: > > * Structure constants > * Array constants > * Vector constants > * ZeroinitializationThis is very confusing, and I just changed it. These aren't "aggregate" in the same sense that aggregate types are. I changed them to "complex constants" to avoid confusion.> However, in [#constantexprs] we have a bitcast(CST, TYPE) operator > with the documentation (emphasis by me):There was no specific reason to duplicate the constraints for bitcast, so I removed it and now point to the bitcast instruction documentation (#i_bitcast). Bitcasting pointers to pointers is ok, integers/fp/vectors can all be converted among them selves. You cannot bitcast a struct or array. -Chris
Nick Lewycky
2009-Feb-28 19:13 UTC
[LLVMdev] Question about documentation of BitCast CExps.
Nick Lewycky wrote:> Jesper Louis Andersen wrote: >> In the documentation at [http://llvm.org/docs/LangRef.html], we have >> the section [#aggregateconstants] telling us that the aggregate >> constants are: >> >> * Structure constants >> * Array constants >> * Vector constants >> * Zeroinitialization >> >> However, in [#constantexprs] we have a bitcast(CST, TYPE) operator >> with the documentation (emphasis by me): >> >> Convert a constant, CST, to another TYPE. The size of CST and TYPE >> must be identical (same number of bits). The conversion is done as if >> the CST value was stored to memory and read back as TYPE. In other >> words, no bits change with this operator, just the type. {{{This can >> be used for conversion of vector types to any other type, as long as >> they have the same bit width}}}. For pointers it is only valid to cast >> to another pointer type. {{{It is not valid to bitcast to or from an >> aggregate type.}}} > > Thanks for the report. I've updated this text to read: > > "Convert a constant, CST, to another TYPE. The size of CST and TYPE must > be identical (same number of bits). The conversion is done as if the CST > value was stored to memory and read back as TYPE. In other words, no > bits change with this operator, just the type. This can be used for > conversion of aggregate types to any aggregate type, as long as they > have the same bit width. Vector types may also be casted to and from any > other type as long as they have the same bit width. For pointers it is > only valid to cast to another pointer type." > > This should make it clear that aggregates may be bitcasted to other > aggregates, and that additionally vectors may be bitcasted to and from > other types, but not pointers or labels or functions or void, because > those will never have the same width as a vector (tricky!).Just to follow up on myself, Chris has gone ahead and removed this text from the LangRef and over IRC informed me that I was mistaken. The fact that we accepted aggregate to aggregate bitcasts was a bug, not a deliberate feature of the IR. Those are still invalid, and we should update the verifier to catch them. Nick> Nick > >> The first emphasis states that it is possible to use the bitcast >> operation for conversion of vector types. However, the second emphasis >> says that this is not valid (A vector is an aggregate type). So my >> questions are: >> >> What is right? >> Is this a mistake? >> Am I reading this wrong in any way? If so, where? >> >> The reason I am asking is because I am in the process of implementing >> a rudimentary type-checker phase for LLVM in Standard ML. It is mostly >> done because it is a tremendous way to learn about the finer details >> of the language. I am forced to think and know details about LLVM >> because of the process, so hence my question. >> > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >