[LLVMdev] DFG of machine functions

AbhishekR abhishekr1982 at gmail.com
Fri Jun 1 17:49:50 PDT 2012


I tried debugging it and the issue seems to be in the implementation of
MachineInstrIterator.h and the way it interacts with GraphWriter.h
functions. I found this by replacing the ( template <> struct
GraphTraits<MCDFGraph<MachineFunction*> >) with a similar MCDFGraph based
template of CFG similar to the one in MachineFunction.h (similarly
replacing the DOTGraphTraits with the one for CFG) and it works and pops up
the GraphViz window with the CFG for the machine function.

I am attaching the MachineInstrIterator.h file.

Also, I wrote a pass using the mc_inst_iterator (defined in the
MachineInstrIterator.h - the direct iterator over MachineInstr in a
MachineFunction) to iterate through the machine instructions in a machine
function and it works.

I tried building LLVM 3.1 using clang in order to get a better idea of the
error. Here is the build error output:

/home/arhishee/work/llvm-3.1.src/include/llvm/Support/GraphWriter.h:143:8:
note: candidate function not viable: no known conversion from
      'IIty' (aka 'llvm::MachineInstr') to 'NodeType &' (aka 'llvm::Value
&') for 1st argument;
  bool isNodeHidden(NodeType &Node) {
       ^
/home/arhishee/work/llvm-3.1.src/include/llvm/Support/GraphWriter.h:147:8:
note: candidate function not viable: no known conversion from
      'IIty' (aka 'llvm::MachineInstr') to 'NodeType *const *' (aka
'llvm::Value *const *') for 1st argument;
  bool isNodeHidden(NodeType *const *Node) {
       ^
/home/arhishee/work/llvm-3.1.src/include/llvm/Support/GraphWriter.h:151:8:
note: candidate function not viable: no known conversion from
      'IIty' (aka 'llvm::MachineInstr') to 'NodeType *' (aka 'llvm::Value
*') for 1st argument;
  bool isNodeHidden(NodeType *Node) {

This seems to be a clear type mismatch between the argument expected by
isNodeHidden in GraphWriter.h (either of 3 types - NodeType &Node, NodeType
*const *Node, NodeType *Node, where NodeType corresponds to llvm::Value)
and the llvm::MachineInstr type passed through the mc_inst_iterator
template. But I am not sure how to fix it. Can someone provide some debug
pointers?


On Wed, May 30, 2012 at 8:21 PM, AbhishekR <abhishekr1982 at gmail.com> wrote:

> Hi,
> I am trying to generate the DFG of machine functions.
>
> Initially, I added a pass to generate the DFG of LLVM IR functions. This
> was based on the mail thread -
> http://lists.cs.uiuc.edu/pipermail/llvmdev/2009-September/025582.html.
> This pass worked fine and I was able to generate DFG of LLVM IR functions.
>
> Later, I ported the DFG pass code for machine functions. I ported the
> InstIterator.h (which was required to use inst_iterator in the *template
> <> struct GraphTraits<DFG<Function*> >*) to MachineInstrIterator.h to
> iterate over machine instructions in a machine function. But the build is
> throwing the following error:
>
> llvm[2]: Compiling MC_DFG.cpp for Debug+Asserts build
> /home/abhi/work/llvm31/llvm/include/llvm/Support/GraphWriter.h: In member
> function ‘void llvm::GraphWriter<GraphType>::writeNodes() [with GraphType =
> llvm::MCDFGraph<llvm::MachineFunction*>]’:
> /home/abhi/work/llvm31/llvm/include/llvm/Support/GraphWriter.h:100:
> instantiated from ‘void llvm::GraphWriter<GraphType>::writeGraph(const
> std::string&) [with GraphType = llvm::MCDFGraph<llvm::MachineFunction*>]’
> /home/abhi/work/llvm31/llvm/include/llvm/Support/GraphWriter.h:304:
> instantiated from ‘llvm::raw_ostream& llvm::WriteGraph(llvm::raw_ostream&,
> const GraphType&, bool, const llvm::Twine&) [with GraphType =
> llvm::MCDFGraph<llvm::MachineFunction*>]’
> /home/abhi/work/llvm31/llvm/lib/CodeGen/MC_DFG.cpp:249:   instantiated
> from here
> */home/abhi/work/llvm31/llvm/include/llvm/Support/GraphWriter.h:139:
> error: no matching function for call to
> ‘llvm::GraphWriter<llvm::MCDFGraph<llvm::MachineFunction*>
> >::isNodeHidden(llvm::MachineInstr&)’*
> */home/abhi/work/llvm31/llvm/include/llvm/Support/GraphWriter.h:143:
> note: candidates are: bool
> llvm::GraphWriter<GraphType>::isNodeHidden(typename
> llvm::GraphTraits<T>::NodeType&) [with GraphType =
> llvm::MCDFGraph<llvm::MachineFunction*>]*
> */home/abhi/work/llvm31/llvm/include/llvm/Support/GraphWriter.h:147:
> note:                 bool
> llvm::GraphWriter<GraphType>::isNodeHidden(typename
> llvm::GraphTraits<T>::NodeType* const*) [with GraphType =
> llvm::MCDFGraph<llvm::MachineFunction*>]*
> */home/abhi/work/llvm31/llvm/include/llvm/Support/GraphWriter.h:151:
> note:                 bool
> llvm::GraphWriter<GraphType>::isNodeHidden(typename
> llvm::GraphTraits<T>::NodeType*) [with GraphType =
> llvm::MCDFGraph<llvm::MachineFunction*>]*
> */home/abhi/work/llvm31/llvm/include/llvm/Support/GraphWriter.h:140:
> error: no matching function for call to
> ‘llvm::GraphWriter<llvm::MCDFGraph<llvm::MachineFunction*>
> >::writeNode(llvm::MachineInstr&)’*
> */home/abhi/work/llvm31/llvm/include/llvm/Support/GraphWriter.h:155:
> note: candidates are: void llvm::GraphWriter<GraphType>::writeNode(typename
> llvm::GraphTraits<T>::NodeType&) [with GraphType =
> llvm::MCDFGraph<llvm::MachineFunction*>]*
> */home/abhi/work/llvm31/llvm/include/llvm/Support/GraphWriter.h:159:
> note:                 void llvm::GraphWriter<GraphType>::writeNode(typename
> llvm::GraphTraits<T>::NodeType* const*) [with GraphType =
> llvm::MCDFGraph<llvm::MachineFunction*>]*
> */home/abhi/work/llvm31/llvm/include/llvm/Support/GraphWriter.h:163:
> note:                 void llvm::GraphWriter<GraphType>::writeNode(typename
> llvm::GraphTraits<T>::NodeType*) [with GraphType =
> llvm::MCDFGraph<llvm::MachineFunction*>]*
> gmake[2]: ***
> [/home/abhi/work/llvm31/llvm-build-new/lib/CodeGen/Debug+Asserts/MC_DFG.o]
> Error 1
> gmake[2]: Leaving directory
> `/home/abhi/work/llvm31/llvm-build-new/lib/CodeGen'
> gmake[1]: *** [CodeGen/.makeall] Error 2
> gmake[1]: Leaving directory `/home/abhi/work/llvm31/llvm-build-new/lib'
> gmake: *** [all] Error 1
>
>
> The error seems to be a mismatch between types of the argument passed to
> the isNodeHidden and writeNode function in the GraphWriter.h file. But I am
> not sure where this type mismatch is originating from or how to fix it. Any
> idea what could be the issue here?
>
> Here are some of the code snippets:
>
> *//templates for DFG and specializations of GraphTraits*
>   template <typename T>
>   class MCDFGraph {
>   private:
>     T p;
>   public:
>     MCDFGraph(T t) : p(t) {}
>     T operator*() { return p; }
>   };
>
>   template <> struct GraphTraits<MCDFGraph<MachineFunction*> > : public
>   GraphTraits<Value*> {
>     typedef mc_inst_iterator nodes_iterator;
>
>     static nodes_iterator nodes_begin(MCDFGraph<MachineFunction *> F) {
>       return mc_inst_begin(*F);
>     }
>
>     static nodes_iterator nodes_end(MCDFGraph<MachineFunction *> F) {
>       return mc_inst_end(*F);
>     }
>   };
>
>   template<>
>   struct DOTGraphTraits<MCDFGraph<MachineFunction*> > : public
> DefaultDOTGraphTraits {
>
>     DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple)
> {}
>
>     static std::string getGraphName(MCDFGraph<MachineFunction *> F) {
>       return "DFG for the function";
>     }
>
>     static std::string getSimpleNodeLabel(Value *Node,
>                                           const MCDFGraph<MachineFunction
> *> &F) {
>       std::string Str;
>       raw_string_ostream OS(Str);
>
>       WriteAsOperand(OS, Node, false);
>       return OS.str();
>     }
>
>     static std::string getCompleteNodeLabel(Value *Node,
>                                             const
> MCDFGraph<MachineFunction *> &F) {
>       std::string Str;
>       raw_string_ostream OS(Str);
>
>       if (!Node->getName().empty()) {
>         WriteAsOperand(OS, Node, false);
>         OS << ":\n";
>       }
>
>       OS << *Node;
>       std::string OutStr = OS.str();
>       if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
>
>       // Process string output to make it nicer...
>       for (unsigned i = 0; i != OutStr.length(); ++i)
>         if (OutStr[i] == '\n') {                            // Left justify
>           OutStr[i] = '\\';
>           OutStr.insert(OutStr.begin()+i+1, 'l');
>         } else if (OutStr[i] == ';') {                      // Delete
> comments!
>           unsigned Idx = OutStr.find('\n', i+1);            // Find end of
> line
>           OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
>           --i;
>         }
>
>       return OutStr;
>     }
>
>     std::string getNodeLabel(Value *&Node,
>                              const MCDFGraph<MachineFunction *> &Graph) {
>       if (isSimple())
>         return getSimpleNodeLabel(Node, Graph);
>       else
>         return getCompleteNodeLabel(Node, Graph);
>     }
>
>   };
>
> *//Calls to ViewGraph and WriteGraph from the respective passes'
> runOnMachineFunction*
>     ViewGraph(MCDFGraph<MachineFunction*>(&F), "Function:" +
> (F.getFunction())->getName());
>
>           WriteGraph(File, (MCDFGraph<MachineFunction*>)&F);
>
>
> The MachineInstrIterator.h is quite lengthy, please let me know if I need
> to post it.
>
> --
> Regards,
> Abhishek
>



-- 
Regards,
Abhishek
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120601/3c9622cc/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: MachineInstrIterator.h
Type: text/x-chdr
Size: 6034 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120601/3c9622cc/attachment.h>


More information about the llvm-dev mailing list