[llvm] r320725 - [opt-viewer] Support unicode characters in function names

Adam Nemet via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 14 10:42:43 PST 2017


Author: anemet
Date: Thu Dec 14 10:42:42 2017
New Revision: 320725

URL: http://llvm.org/viewvc/llvm-project?rev=320725&view=rev
Log:
[opt-viewer] Support unicode characters in function names

This is a Swift feature.  The output stream for the index page and the source
HTML page is utf-8 now.

The next patch will add the HTML magic to properly render these characters in
the browser.

Added:
    llvm/trunk/test/tools/opt-viewer/Inputs/unicode-function-name/
    llvm/trunk/test/tools/opt-viewer/Inputs/unicode-function-name/s.opt.yaml
    llvm/trunk/test/tools/opt-viewer/Inputs/unicode-function-name/s.swift
    llvm/trunk/test/tools/opt-viewer/Outputs/unicode-function-name/
    llvm/trunk/test/tools/opt-viewer/Outputs/unicode-function-name/index.html
    llvm/trunk/test/tools/opt-viewer/Outputs/unicode-function-name/s.swift.html
    llvm/trunk/test/tools/opt-viewer/unicode-function-name.test
Modified:
    llvm/trunk/tools/opt-viewer/opt-viewer.py
    llvm/trunk/tools/opt-viewer/optrecord.py

Added: llvm/trunk/test/tools/opt-viewer/Inputs/unicode-function-name/s.opt.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/opt-viewer/Inputs/unicode-function-name/s.opt.yaml?rev=320725&view=auto
==============================================================================
--- llvm/trunk/test/tools/opt-viewer/Inputs/unicode-function-name/s.opt.yaml (added)
+++ llvm/trunk/test/tools/opt-viewer/Inputs/unicode-function-name/s.opt.yaml Thu Dec 14 10:42:42 2017
@@ -0,0 +1,22 @@
+--- !Passed
+Pass:            sil-inliner
+Name:            sil.Inlined
+DebugLoc:
+  File:            s.swift
+  Line:            12
+  Column:          16
+Function:        main
+Args:
+  - Callee:          '"s.• infix(_:_:)"'
+    DebugLoc:
+      File:            s.swift
+      Line:            3
+      Column:          6
+  - String:          ' inlined into '
+  - Caller:          '"main"'
+  - String:          ' (cost = '
+  - Cost:            '2'
+  - String:          ', benefit = '
+  - Benefit:         '40'
+  - String:          ')'
+...

Added: llvm/trunk/test/tools/opt-viewer/Inputs/unicode-function-name/s.swift
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/opt-viewer/Inputs/unicode-function-name/s.swift?rev=320725&view=auto
==============================================================================
--- llvm/trunk/test/tools/opt-viewer/Inputs/unicode-function-name/s.swift (added)
+++ llvm/trunk/test/tools/opt-viewer/Inputs/unicode-function-name/s.swift Thu Dec 14 10:42:42 2017
@@ -0,0 +1,12 @@
+infix operator •: AdditionPrecedence
+
+func • (a: Int, b: Int) -> Int {
+  return a * b
+}
+
+ at inline(never)
+func g(a: Int) -> Int{
+  return a + 1
+}
+
+let i = g(a: 1 • 2)

Added: llvm/trunk/test/tools/opt-viewer/Outputs/unicode-function-name/index.html
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/opt-viewer/Outputs/unicode-function-name/index.html?rev=320725&view=auto
==============================================================================
--- llvm/trunk/test/tools/opt-viewer/Outputs/unicode-function-name/index.html (added)
+++ llvm/trunk/test/tools/opt-viewer/Outputs/unicode-function-name/index.html Thu Dec 14 10:42:42 2017
@@ -0,0 +1,25 @@
+
+<html>
+<head>
+<link rel='stylesheet' type='text/css' href='style.css'>
+</head>
+<body>
+<div class="centered">
+<table>
+<tr>
+<td>Source Location</td>
+<td>Hotness</td>
+<td>Function</td>
+<td>Pass</td>
+</tr>
+
+<tr>
+<td class="column-entry-0"><a href="s.swift.html#L12">s.swift:12:16</a></td>
+<td class="column-entry-0"></td>
+<td class="column-entry-0">main</td>
+<td class="column-entry-green">sil-inliner</td>
+</tr>
+
+</table>
+</body>
+</html>

Added: llvm/trunk/test/tools/opt-viewer/Outputs/unicode-function-name/s.swift.html
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/opt-viewer/Outputs/unicode-function-name/s.swift.html?rev=320725&view=auto
==============================================================================
--- llvm/trunk/test/tools/opt-viewer/Outputs/unicode-function-name/s.swift.html (added)
+++ llvm/trunk/test/tools/opt-viewer/Outputs/unicode-function-name/s.swift.html Thu Dec 14 10:42:42 2017
@@ -0,0 +1,122 @@
+
+<html>
+<head>
+<link rel='stylesheet' type='text/css' href='style.css'>
+</head>
+<body>
+<div class="centered">
+<table class="source">
+<thead>
+<tr>
+<th style="width: 2%">Line</td>
+<th style="width: 3%">Hotness</td>
+<th style="width: 10%">Optimization</td>
+<th style="width: 70%">Source</td>
+<th style="width: 15%">Inline Context</td>
+</tr>
+</thead>
+<tbody>
+
+<tr>
+<td><a name="L1">1</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>infix operator •: AdditionPrecedence</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L2">2</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre></pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L3">3</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>func • (a: Int, b: Int) -> Int {</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L4">4</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>  return a * b</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L5">5</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>}</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L6">6</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre></pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L7">7</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>@inline(never)</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L8">8</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>func g(a: Int) -> Int{</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L9">9</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>  return a + 1</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L10">10</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>}</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L11">11</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre></pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L12">12</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>let i = g(a: 1 • 2)</pre></div></td>
+</tr>
+
+<tr>
+<td></td>
+<td></td>
+<td class="column-entry-green">sil-inliner</td>
+<td><pre style="display:inline">               </pre><span class="column-entry-yellow"> <a href="s.swift.html#L3">"s.• infix(_:_:)"</a> inlined into "main" (cost = 2, benefit = 40) </span></td>
+<td class="column-entry-yellow">main</td>
+</tr>
+
+<tr>
+<td><a name="L13">13</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre></pre></div></td>
+</tr>
+
+</tbody>
+</table>
+</body>
+</html>

Added: llvm/trunk/test/tools/opt-viewer/unicode-function-name.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/opt-viewer/unicode-function-name.test?rev=320725&view=auto
==============================================================================
--- llvm/trunk/test/tools/opt-viewer/unicode-function-name.test (added)
+++ llvm/trunk/test/tools/opt-viewer/unicode-function-name.test Thu Dec 14 10:42:42 2017
@@ -0,0 +1,7 @@
+RUN: %opt-viewer -s %p/Inputs/unicode-function-name -o %t %p/Inputs/unicode-function-name/s.opt.yaml --no-highlight --demangler=llvm-cxxfilt
+RUN: diff %p/Outputs/unicode-function-name/index.html %t/index.html
+RUN: diff %p/Outputs/unicode-function-name/s.swift.html %t/s.swift.html
+
+# Also test the pygments code path without actually checking the output since
+# that may slightly vary between pygment versions
+RUN: %opt-viewer -s %p/Inputs/unicode-function-name -o %t.unused %p/Inputs/unicode-function-name/s.opt.yaml --demangler=llvm-cxxfilt

Modified: llvm/trunk/tools/opt-viewer/opt-viewer.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt-viewer/opt-viewer.py?rev=320725&r1=320724&r2=320725&view=diff
==============================================================================
--- llvm/trunk/tools/opt-viewer/opt-viewer.py (original)
+++ llvm/trunk/tools/opt-viewer/opt-viewer.py Thu Dec 14 10:42:42 2017
@@ -4,6 +4,7 @@ from __future__ import print_function
 
 import argparse
 import cgi
+import codecs
 import errno
 import functools
 from multiprocessing import cpu_count
@@ -51,7 +52,7 @@ class SourceFileRenderer:
             if os.path.exists(fn):
                 existing_filename = fn
 
-        self.stream = open(os.path.join(output_dir, optrecord.html_file_name(filename)), 'w')
+        self.stream = codecs.open(os.path.join(output_dir, optrecord.html_file_name(filename)), 'w', encoding='utf-8')
         if existing_filename:
             self.source_stream = open(existing_filename)
         else:
@@ -69,16 +70,19 @@ class SourceFileRenderer:
         file_text = stream.read()
 
         if args.no_highlight:
-            html_highlighted = file_text
+            html_highlighted = file_text.decode('utf-8')
         else:
             html_highlighted = highlight(
             file_text,
                 self.cpp_lexer,
                 self.html_formatter)
 
-            # On Python 3, pygments.highlight() returns a bytes object, not a str.
-            if sys.version_info >= (3, 0):
-              html_highlighted = html_highlighted.decode('utf-8')
+            # Note that the API is different between Python 2 and 3.  On
+            # Python 3, pygments.highlight() returns a bytes object, so we
+            # have to decode.  On Python 2, the output is str but since we
+            # support unicode characters and the output streams is unicode we
+            # decode too.
+            html_highlighted = html_highlighted.decode('utf-8')
 
             # Take off the header and footer, these must be
             #   reapplied line-wise, within the page structure
@@ -86,7 +90,7 @@ class SourceFileRenderer:
             html_highlighted = html_highlighted.replace('</pre></div>', '')
 
         for (linenum, html_line) in enumerate(html_highlighted.split('\n'), start=1):
-            print('''
+            print(u'''
 <tr>
 <td><a name=\"L{linenum}\">{linenum}</a></td>
 <td></td>
@@ -111,7 +115,7 @@ class SourceFileRenderer:
         indent = line[:max(r.Column, 1) - 1]
         indent = re.sub('\S', ' ', indent)
 
-        print('''
+        print(u'''
 <tr>
 <td></td>
 <td>{r.RelativeHotness}</td>
@@ -153,12 +157,12 @@ class SourceFileRenderer:
 
 class IndexRenderer:
     def __init__(self, output_dir, should_display_hotness):
-        self.stream = open(os.path.join(output_dir, 'index.html'), 'w')
+        self.stream = codecs.open(os.path.join(output_dir, 'index.html'), 'w', encoding='utf-8')
         self.should_display_hotness = should_display_hotness
 
     def render_entry(self, r, odd):
         escaped_name = cgi.escape(r.DemangledFunctionName)
-        print('''
+        print(u'''
 <tr>
 <td class=\"column-entry-{odd}\"><a href={r.Link}>{r.DebugLocString}</a></td>
 <td class=\"column-entry-{odd}\">{r.RelativeHotness}</td>

Modified: llvm/trunk/tools/opt-viewer/optrecord.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt-viewer/optrecord.py?rev=320725&r1=320724&r2=320725&view=diff
==============================================================================
--- llvm/trunk/tools/opt-viewer/optrecord.py (original)
+++ llvm/trunk/tools/opt-viewer/optrecord.py Thu Dec 14 10:42:42 2017
@@ -79,7 +79,11 @@ class Remark(yaml.YAMLObject):
     def _reduce_memory(self):
         self.Pass = intern(self.Pass)
         self.Name = intern(self.Name)
-        self.Function = intern(self.Function)
+        try:
+            # Can't intern unicode strings.
+            self.Function = intern(self.Function)
+        except:
+            pass
 
         def _reduce_memory_dict(old_dict):
             new_dict = dict()
@@ -156,7 +160,7 @@ class Remark(yaml.YAMLObject):
 
         if dl and key != 'Caller':
             dl_dict = dict(list(dl))
-            return "<a href={}>{}</a>".format(
+            return u"<a href={}>{}</a>".format(
                 make_link(dl_dict['File'], dl_dict['Line']), value)
         else:
             return value




More information about the llvm-commits mailing list