TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Why does WebAssembly need the relooper algorithm, instead of gotos? (2019)

144 pointsby tbodtabout 5 years ago

10 comments

int_19habout 5 years ago
"There is one thing that may have been a major factor to the decision not to adopt arbitrary CFGs for WebAssembly control flow. I believe that V8 is an exception to most compilers in that it doesn’t represent code as a CFG at all - it maintains roughly JS-compatible control flow all the way from front-end to codegen. In order to support this V8 would have to be converted to use CFGs, they’d have to implement something like Relooper internally, or they’d have to write a new WebAssembly runtime from scratch. Google was and is a major roadblock to getting this implemented as they have multiple people on the committee in charge of WebAssembly who have veto power."
评论 #22400947 未加载
评论 #22402078 未加载
评论 #22400774 未加载
saagarjhaabout 5 years ago
WebAssembly makes a number of strange choices that seemingly fly in the face of convention; this is one of them. It would be really nice if it someone took some time it make a FAQ on the website to answer questions like “why doesn’t WebAssembly do x like everything else”, because otherwise it just seems that they’re just doing things differently because they can…
评论 #22399710 未加载
评论 #22401045 未加载
emmanueloga_about 5 years ago
META: I don&#x27;t understand why some authors go out of the way to obscure important pieces of information:<p>* Who wrote this article?<p>* When was this written?<p>Was lucky to find this pieces of information in Github [1].<p>1: <a href="https:&#x2F;&#x2F;github.com&#x2F;Vurich&#x2F;troubles.md&#x2F;blob&#x2F;master&#x2F;content&#x2F;posts&#x2F;why-do-we-need-the-relooper-algorithm-again.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Vurich&#x2F;troubles.md&#x2F;blob&#x2F;master&#x2F;content&#x2F;po...</a>
评论 #22403632 未加载
RodgerTheGreatabout 5 years ago
Something this article doesn&#x27;t appear to spell out is that the structured format WASM is constrained to means that program CFGs will never contain irreducible loops. As noted, this approach can make it harder for the AoT compilers (they have to untangle the knots to generate valid WASM bytecode), but it makes it much easier for the JIT compilers to perform analysis and transformations at runtime. In that context, it seems like a sensible engineering decision.
yuri91about 5 years ago
The article says about the Stackifier algorithm: &quot;no link for the latter, it’s only mentioned by this name in LLVM internal discussions&quot;.<p>Since the release of this article, I wrote a blog post explaining how Stackifier works, for those who are interested:<p><a href="https:&#x2F;&#x2F;medium.com&#x2F;leaningtech&#x2F;solving-the-structured-control-flow-problem-once-and-for-all-5123117b1ee2" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;leaningtech&#x2F;solving-the-structured-contro...</a>
评论 #22400441 未加载
titzerabout 5 years ago
<i>sigh</i><p>I am partly responsible for why WebAssembly is this way. You can thank&#x2F;blame me for the if-else bytecodes. They are indeed, a form of compression, as they don&#x27;t add expressive power. I measured carefully and they make a big difference in code size. That&#x27;s why they&#x27;re there!<p>The structured control flow requirement is to benefit <i>all consumers</i>. It is only a burden on producers that come from CFGs, not from ASTs and other tree-like IRs. If you have a dominator tree, then you can generate structured control flow in linear time. LLVM does this a particular way, but there are straightforward algorithms.<p>No, this wasn&#x27;t Google throwing around its veto power or something like that. There is a good reason why control flow is structured, as hinted in comments here.<p>1. Structured control flow rules out irreducible loops. Irreducible loops cause problems for <i>all</i> JIT compilers in all browser engines, not just V8, and even in JVMs. Things get really complicated, particularly in register allocation. [1]<p>2. Structured control flow guarantees a stack discipline for the use of labels, mirroring the stack discipline for values. This is not only a nice symmetry, it means that a consumer that needs to allocate space per label can reuse that space as soon as a control construct is closed. That is essentially optimal for use of consumer space resources.<p>[1] No kidding. If you have an irreducible loop in Java bytecode, which is possible, you will never be JITed and will get stuck running 100x slower in the interpreter. We thought this through very carefully in V8. If you allow irreducible loops in Wasm, you force all engines to either stick to their lowest execution tier and run 2-100x slower, do relooping themselves, or handle the general case of irreducible loops spending multiple person-years complicating their optimizing tiers&#x27; backends for a case that is <i>incredibly rare</i> (and probably introducing lots of bugs). In V8 we would have probably gone for the relooper option because the other two options are bad. So that&#x27;s a lose, because now the engine is more complicated, doing a rewrite of the code that could as well be done better and more efficiently offline by a producer. And there is no benefit because the engine&#x27;s code would be no better than what the producer would have come up with. So we&#x27;d choose the lesser of the complexity options, but get no performance benefit, in order to avoid the absurdly bad performance hit of not being able to use the optimizing tier. Bad tradeoff now matter how you slice it, IMHO.<p>I am fully convinced we made the right choice here.<p>We should have communicated better and the relooper algorithm and tools should textbook, off-the-shelf stuff.
评论 #22402466 未加载
评论 #22403961 未加载
评论 #22401987 未加载
albertzeyerabout 5 years ago
Interestingly I came up with a similar algorithm like Relooper when I translated C to Python code. First I translate all C code to mostly equivalent Python code but including some gotos, and then I get rid of those gotos by introducing some more loops. The current solution is simple but the resulting function could be slow.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;albertz&#x2F;PyCParser&#x2F;blob&#x2F;080a7b0472444fd366006c44fc15e388122989bd&#x2F;goto.py#L102" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;albertz&#x2F;PyCParser&#x2F;blob&#x2F;080a7b0472444fd366...</a>
6510about 5 years ago
Could someone reduce the GOTO complaints to the arguments why it is harmful. (Preferably without the considerations?)
heretooabout 5 years ago
The article cites Dijkstra&#x27;s &quot;considered harmful&quot; essay, which asserts that &quot;goto&quot; should be removed from &quot;higher level&quot; languages, not &quot;machine language&quot;.<p>Without reading the remainder of the article, this would seem to undermine any further assertions.<p>UPDATE: after reading the article, it seems the opposite is being shown. my bad.
评论 #22399840 未加载
The_rationalistabout 5 years ago
Unpopular opinion: GraalVM is what should have been webassembly.<p>Reasons: * True polyglotism * existing, feature complete implementation * compatibility with the existing JVM platform, and through polyglotism compatibility with almost any platform&#x2F;library.
评论 #22401052 未加载
评论 #22401026 未加载
评论 #22400401 未加载
评论 #22400387 未加载
评论 #22400504 未加载