<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Thanks for your insightful suggestions.</div><div><br></div><div>Yes, I am programming for a real device that does modular arithmetic (and only modular arithmetic). The modulus N is fixed during a single launch of a program. One way I could also come up with is to simply use add i256 %a, %b to represent a + b mod n, and let LLVM passes to reason about possible optimizations. However these are not semantically identical and it may produce a wrong result, though in a rather low possibility.</div>

<div> </div><div>Here is another issue I have been thinking these days. As far as I know, legalizing a DAG node may expand the node to fit into the ISA. What happens if a node is not representable by any combination of the instructions from the ISA? An extremely opposite situation is that, two nodes must have some particular semantic in the DAG so it can be translate to a single instruction. For example, if MUL a, b of the ISA actually does a * b * 12345. So only</div>

<div><br></div><div>%1 = mul i256 %a, %b<br></div><div>%2 = mul i256 %1, i256 12345</div><div><br></div><div>can be translated into a legal instruction (and they may be scattered around the DAG after some passes), but I think</div>

<div><br></div><div>%1 = mul i256 %a, %b<br></div><div><br></div><div>might fail to match. Is there any workaround?</div><div><br></div><div>Shang-Yi</div><div><br></div></div><div class="gmail_quote"><br></div><div class="gmail_quote">

2013/11/16 David Tweed <span dir="ltr"><<a href="mailto:david.tweed@gmail.com" target="_blank">david.tweed@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div dir="ltr"><div><div><div><div>Hi,<br><br></div>My personal opinion: Just to be sure I understand what you're considering: you want to write a backend that will produce optimized machine code for a device with modular arithmetic instructions (not simulate such a device on a standard CPU)? In which case, won't the same assumptions that are embodied in the transformations for the case of unsigned 2's complement arithmetic (in your case with 256 bit unsigned words) with wraparound result in correct code? I suspect two issues will come up:</div>

</div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div><div><div>


<br></div>1. The optimizations in LLVM which can reason about "useful for optimization" features of wrapping unsigned values are probably not particularly strong at the moment, since most interest tends to be in the case of optimizations that are valid in the case it's known there won't be a wraparound (eg, monotonically increasing of the result of adding elements). This is going to be just a lack of implementation rather than a fundamental issue.<br>


<br></div>2. It's not clear from your description whether changing the value in the "N register" is a very infrequent event or not. If it happens so infrequently you can get away with just putting compiler barriers before and after the change that would fit reasonably. On the other hand if it's changed frequently getting performance might require allowing the optimizer to move instructions around significantly enough that it's necessary to keep track of what the N register should be for each instruction. LLVM doesn't really have any built-in notion of this, so you're going to have to make significant changes to the compiler to do that. Whether this means writing your own compiler would be less work I don't know.<br>


<br></div>Hope this helps,<br></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div class="h5">On Fri, Nov 15, 2013 at 9:15 AM, Shang-Yi Yang <span dir="ltr"><<a href="mailto:ilway25@gmail.com" target="_blank">ilway25@gmail.com</a>></span> wrote:<br>


</div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div class="h5"><div dir="ltr">I've been playing around with LLVM to write a backend for a rather "simple" (co-)processor. Assume that only three arithmetic instructions exist: ADD mod N, SUB mod N and MUL mod N. The modulus N is programmable and stored in a register. No ordinary arithmetic instructions are available. The word size is 256-bit.<div>




<div><br></div><div>In other words, the following function, b + c mod N, corresponds to only one instruction on my target machine, given the modulus N set by another instruction to an auxiliary register.</div><div><br></div>




<div># this should translate to one instruction like: r3 = add r1, r2<br></div><div># n is extended to 257-bit for simplicity</div><div><div>define i256 @add(i256 %b, i256 %c, i257 %n) {</div><div>  %1 = zext i256 %b to i257</div>




<div>  %2 = zext i256 %c to i257</div><div>  %3 = add i257 %2, %1</div><div>  %4 = icmp uge i257 %3, %n</div><div>  %5 = select i1 %4, i257 %n, i257 0</div><div>  %6 = sub i257 %3, %5</div><div>  %7 = trunc i257 %6 to i256</div>




<div>  ret i256 %7</div><div>}</div></div><div><br></div><div>The ultimate goal is to use LLVM to optimize and emit machine code for programs full of modular addition, subtraction and multiplication. For example, I might want off-the-shelf LLVM passes to optimize (a+b+c)-(a+c) mod p = b mod p for me. For a large syntax tree, say more than 10,000 nodes, I might even need common subexpression elimination. Of course, we can target the same program to ARM or X86 for performance comparison.</div>




</div><div><br></div><div>The questions is: We have to recognize the patterns for modular arithmetic from optimized IR and translate into corresponding instructions. After investigation, I think LLVM can not do a good job on such a simple but unordinary instruction set. Is it possible that the patterns still recognizable after optimization? On the whole, is this viable using LLVM or is it better to write a compiler for this particular DSL from scratch?</div>




<div><br></div><div>Thanks,</div><div>Shang-Yi Yang</div><div><br></div></div>
<br></div></div>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><span class=""><font color="#888888"><br><br clear="all"><br>-- <br><div>cheers, dave tweed__________________________</div><div>high-performance computing and machine vision expert: <a href="mailto:david.tweed@gmail.com" target="_blank">david.tweed@gmail.com</a></div>


<div>"while having code so boring anyone can maintain it, use Python." -- attempted insult seen on slashdot</div><div> </div>
</font></span></div>
</blockquote></div><br></div></div>