arbitrary bit number


I wanted to ask one general question ( for now it is hard to check it manually, maybe there is a fast answer):

can I construct an operation, say, addition, from operand_0 - 5 bits size and operand_1 - 3 bit size β†’ receive result as 6 bit size . I am basically reducing these sizes in the whole IR, so I want that all operands can have arbitrary sizes ( llvm pass is not really good because it converts sizes only to some predefined sizes). However , I am not sure if there will be internal checks on these sizes after I will transform the tree. Or is it better to create some my own attributes, that can describe the sizes?


LLVM allows to work with arbitrary bit sizes (see, but please bear in mind that working with unusual sizes might expose bugs, as the corresponding code paths are less tested.

Answering your specific question: yes, it’s definitely possible, however you need to define how to add 3-bit operand to 5-bit operand in terms of operations supported by LLVM. One way is to, for instance, zero-extend both operands to 6-bits and then add:

define i6 @foo(i5 %a, i3 %b) {
%a_zext = zext i5 %a to i6
%b_zext = zext i3 %b to i6
%c = add i6 %a_zext, %b_zext
ret i6 %c



thank you for the answer, great that this is possible, I will try to come up with some solution.

Also, don't expect it to work for large widths. We make some effort to
expand (say) an i128 add in terms of two 64-bit adds, but the code
very quickly becomes terrible; and division simply doesn't work above
128-bits (typically).

If you want arbitrary precision arithmetic you should be looking for a
library to do it properly.