[PATCH] D66729: [WebAssembly] Fix SSA rebuilding in SjLj transformation

Heejin Ahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 26 01:48:56 PDT 2019


aheejin created this revision.
aheejin added a reviewer: dschuff.
Herald added subscribers: llvm-commits, sunfish, hiraditya, jgravelle-google, sbc100.
Herald added a project: LLVM.
aheejin edited the summary of this revision.

Previously we skipped uses within the same BB as a def when rebuilding
SSA after SjLj transformation. For example, before transformation,

  bb:
    %0 = phi i32 [ %var, %bb.next ] ...
    %var = ...
    ...
    br i1 %tmp, label %bb.next, label %bb.0
  
  bb.next:                                          ; preds = %bb
    ...
    br %bb

In this BB, %var should be defined in all paths from %bb.next to make %0
valid. In the input it was true; %bb.next's only predecessor was %bb.
But after SjLj transformation, it is possible that %bb.next has other
predecessors that are reachable without reaching %bb.

  entry:
    ...
    br i1 %a, label %bb.1, label %bb.next
  
  bb:
    %0 = phi i32 [ %var, %bb.next ] ...             ; Not valid!
    %var = ...
    ...
    br i1 %tmp, label %bb.next, label %bb.other
  
  bb.next:                                          ; preds = %bb, %entry
    ...
    br %bb

In this case, we can't use %var in the `phi` instruction in %bb, because
%var is not defined in all paths through %bb.next (If the control flow
is %entry -> %bb.next -> %bb, %var has not been defined until we reach
the `phi`. But the previous code excluded users within the same BB,
skipping instructions within the same BB so they are not rewritten
properly. User instructions within the same BB also should be candidates
for rewriting if they are _before_ the original definition.

Fixes PR43097 <https://bugs.llvm.org/show_bug.cgi?id=43097>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66729

Files:
  llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
  llvm/test/CodeGen/WebAssembly/lower-em-sjlj.ll


Index: llvm/test/CodeGen/WebAssembly/lower-em-sjlj.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/lower-em-sjlj.ll
+++ llvm/test/CodeGen/WebAssembly/lower-em-sjlj.ll
@@ -226,6 +226,26 @@
   unreachable
 }
 
+; Tests if SSA rewrite works when a use and its def are within the same BB.
+define void @ssa_rewite_in_same_bb() {
+entry:
+  call void @foo()
+  br label %for.cond
+
+for.cond:                                         ; preds = %for.inc, %entry
+  ; CHECK: %{{.*}} = phi i32 [ %var[[VARNO:.*]], %for.inc.split ]
+  %0 = phi i32 [ %var, %for.inc ], [ undef, %entry ]
+  %var = add i32 0, 0
+  br label %for.inc
+
+for.inc:                                          ; preds = %for.cond
+  %call5 = call i32 @setjmp(%struct.__jmp_buf_tag* undef) #0
+  br label %for.cond
+
+; CHECK: for.inc.split:
+  ; CHECK: %var[[VARNO]] = phi i32 [ %var, %for.inc ]
+}
+
 declare void @foo()
 ; Function Attrs: returns_twice
 declare i32 @setjmp(%struct.__jmp_buf_tag*) #0
Index: llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
===================================================================
--- llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
@@ -636,15 +636,12 @@
   SSAUpdater SSA;
   for (BasicBlock &BB : F) {
     for (Instruction &I : BB) {
+      SSA.Initialize(I.getType(), I.getName());
+      SSA.AddAvailableValue(&BB, &I);
       for (auto UI = I.use_begin(), UE = I.use_end(); UI != UE;) {
         Use &U = *UI;
         ++UI;
-        SSA.Initialize(I.getType(), I.getName());
-        SSA.AddAvailableValue(&BB, &I);
         auto *User = cast<Instruction>(U.getUser());
-        if (User->getParent() == &BB)
-          continue;
-
         if (auto *UserPN = dyn_cast<PHINode>(User))
           if (UserPN->getIncomingBlock(U) == &BB)
             continue;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66729.217092.patch
Type: text/x-patch
Size: 1934 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190826/8d3e0dd2/attachment.bin>


More information about the llvm-commits mailing list