[PATCH] D74691: [Attributor] Detect SCCs with unbounded cycles

Stefanos Baziotis via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 23 15:20:37 PST 2020


baziotis added a comment.

> btw, I recommend that you try to answer questions by following the code - it's always a learning experience).

Since we took that road, let's have a little more fun. Let's start simple by printing the SCCs we get. A simple way to do that is with sth like:

  for (scc_iterator<Function *> It = scc_begin(&F), IE = scc_end(&F); It != IE;
       ++It) {
  
    const std::vector<BasicBlock *> &SCCBBs = *It;
    for (BasicBlock *BB : SCCBBs) {
      dbgs() << *BB << "\n";
    }
    dbgs() << "----- END -----\n\n\n\n\n";
  }

But, we can actually do better if we take a graphical view of the CFG, with `view-cfg`. You can do that with sth like:
`./bin/opt -view-cfg test.ll`
Assuming that you are in the `llvm-project` dir, that you have built `opt` and that your file is `test.ll`. Now, this will generate a `.dot` file.
This is a special "graphics" format for which we should not care about right now. If you do that, you'll probably see something like:

  ...
  Writing '/tmp/cfgnon_loop_inside_loop-a0c3a3.dot'...  done. 
  Trying 'xdg-open' program... Remember to erase graph file: /tmp/cfgnon_loop_inside_loop-a0c3a3.dot
  gio: file:///tmp/cfgnon_loop_inside_loop-a0c3a3.dot: No application is registered as handling this file 

For me, it created `/tmp/cfgnon_loop_inside_loop-a0c3a3.dot` (you won't necessarily have the same name, that's ok). Then, it will try to find
default program to open it (with the `xdg-open` command), which as you can see, for me it didn't work. Basically, you now have to have a program that understands
that file. The `dot` app will do the job, but you'll probably won't have it by default. Search online for how to install graphviz package. For example, in Ubuntu I think you can do it with `sudo apt-get install graphviz`.
Finally, you should be able to create a PDF out of the `.dot` file as: 
`dot -Tpdf /tmp/cfgnon_loop_inside_loop-a0c3a3.dot -o <pdf_filename>.pdf`
Then, you can open the pdf and expect to see sth like this: https://imgur.com/a/oW2xgNt
I think viewing CFGs like that, at times can be //very// helpful. In this case for example, it becomes instantly apparent that the `for` and the `while` don't form a SCC.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74691/new/

https://reviews.llvm.org/D74691





More information about the llvm-commits mailing list