[PATCH] D59105: [RFC] Create an Arbitrary Precision Integer Type.

Erich Keane via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 7 12:28:28 PST 2019


erichkeane created this revision.
erichkeane added a reviewer: aaron.ballman.
Herald added a reviewer: martong.
Herald added subscribers: jdoerfert, arphaman.

Introduction
As we all know, LLVM-IR supports arbitrary precision integers via the iN
syntax.  Integers that aren’t powers-of-two aren’t particularly
interesting/useful on most hardware however, so this functionality isn’t
typically used, and in fact, Clang does not have such support.  However,
certain architectures DO exist where the ability to express these values
(and do math without promotion to full integers) is valuable from a
performance perspective, since these values can be expressed more
efficiently in hardware.

I’ve developed a solution to expose these types in clang, and am
proposing to contribute it back to the Clang community if it is
sufficiently acceptable to the group.

Syntax
The syntax I’ve chosen for this is as a Typedef Attribute.  This permits
the consumer to define various ways to expose this functionality in
their code.  Additionally, we’ve limited it to int/unsigned typedefs
only for simplicity’s sake. The code looks something like:

// Typical way to expose this in C:
typedef int __attribute__((__ap_int(3))) ap_int3;
typedef unsigned __attribute__((__ap_int(3))) ap_uint3;

// Better way to expose it in modern C++:
template<unsigned bits> using ap_int = int
__attribute__((__ap_int(bits)));
template<unsigned bits> using ap_uint = unsigned
__attribute__((__ap_int(bits)));

For our usages, we just wrapped these in a type that would better
express the further configurable semantics, but I suspect these are
useful in their own right.

Conversions/Promotions
We consider conversions to/from integers integral promotions that follow
normal conversion rules with the exception of automatic promotion to
int, and bool conversions are also supported (just like integers).
AP-Int math is done at the size of the largest operand in order to
prevent promotions that would inflate the size required hardware (such
as in an FPGA).

Size/Align
One important consideration that was made is how size/alignment is
expressed in the language.  Sizeof, for example works exclusively on
bytes, so an ap_int<13> couldn’t be directly expressed.  Alignments are
required to be a power-of-two, otherwise they are not expressable on
some hardware.  In implementation, we chose to use the next largest
alignment (up to 64 bits).  Additionally, for the sake of the standard
library (and common array patterns), we are required to make sizeof
also round up to the next power of two.  This is most consistent with
what a majority of LLVM backends will do (for example, an ap_int<24>
will be expressed as a int-32 on most platforms), and was required to
make common array patterns work.  Unfortunately, this results in arrays
on these platforms having ‘padding’ bits, but sufficiently motivated
code generation can repair this problem.


https://reviews.llvm.org/D59105

Files:
  include/clang/AST/ASTContext.h
  include/clang/AST/RecursiveASTVisitor.h
  include/clang/AST/Type.h
  include/clang/AST/TypeLoc.h
  include/clang/AST/TypeNodes.def
  include/clang/Basic/Attr.td
  include/clang/Basic/AttrDocs.td
  include/clang/Basic/DiagnosticSemaKinds.td
  include/clang/Sema/Sema.h
  include/clang/Serialization/ASTBitCodes.h
  lib/AST/ASTContext.cpp
  lib/AST/ASTStructuralEquivalence.cpp
  lib/AST/ExprConstant.cpp
  lib/AST/ItaniumMangle.cpp
  lib/AST/MicrosoftMangle.cpp
  lib/AST/ODRHash.cpp
  lib/AST/RecordLayoutBuilder.cpp
  lib/AST/Type.cpp
  lib/AST/TypePrinter.cpp
  lib/CodeGen/CGDebugInfo.cpp
  lib/CodeGen/CGDebugInfo.h
  lib/CodeGen/CGExprCXX.cpp
  lib/CodeGen/CGExprScalar.cpp
  lib/CodeGen/CodeGenFunction.cpp
  lib/CodeGen/CodeGenTypes.cpp
  lib/CodeGen/ItaniumCXXABI.cpp
  lib/Sema/SemaChecking.cpp
  lib/Sema/SemaExpr.cpp
  lib/Sema/SemaLookup.cpp
  lib/Sema/SemaOverload.cpp
  lib/Sema/SemaTemplate.cpp
  lib/Sema/SemaTemplateDeduction.cpp
  lib/Sema/SemaType.cpp
  lib/Sema/TreeTransform.h
  lib/Serialization/ASTReader.cpp
  lib/Serialization/ASTWriter.cpp
  test/CodeGen/arb_prec_int.c
  test/CodeGenCXX/arb_prec_int.cpp
  test/CodeGenCXX/arb_prec_int_struct_layout.cpp
  test/CodeGenOpenCL/arb_prec_int.cl
  test/Misc/pragma-attribute-supported-attributes-list.test
  test/Sema/arb_prec_int.c
  test/SemaCXX/arb_prec_int.cpp
  tools/libclang/CIndex.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59105.189768.patch
Type: text/x-patch
Size: 125631 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190307/b401c020/attachment-0001.bin>


More information about the cfe-commits mailing list