|
25 | 25 | import tempfile
|
26 | 26 | import argparse
|
27 | 27 |
|
| 28 | + |
28 | 29 | def ParseArgs():
|
29 |
| - parser = argparse.ArgumentParser( |
30 |
| - formatter_class=argparse.RawDescriptionHelpFormatter, description=__doc__) |
31 |
| - parser.add_argument( |
32 |
| - '-p', dest='desired_pass', metavar='<desired-pass>', required=True, |
33 |
| - help='stop before this module pass (per-function prepass not supported).') |
34 |
| - parser.add_argument( |
35 |
| - '-n', dest='invocation', metavar='<invocation>', type=int, default=1, |
36 |
| - help='pass invocation number on which to stop (default=1)') |
37 |
| - parser.add_argument( |
38 |
| - 'hlsl_file', metavar='<hlsl-file>', |
39 |
| - help='input HLSL file path to compile') |
40 |
| - parser.add_argument( |
41 |
| - '-o', dest='output_file', metavar='<output-file>', required=True, |
42 |
| - help='output file name') |
43 |
| - parser.add_argument( |
44 |
| - 'compilation_options', nargs='*', |
45 |
| - metavar='-- <DXC options>', |
46 |
| - help='set of compilation options needed to compile the HLSL file with DXC') |
47 |
| - return parser.parse_args() |
48 |
| - |
49 |
| -def SplitAtPass(passes, pass_name, invocation = 1): |
50 |
| - pass_name = '-' + pass_name |
51 |
| - before = [] |
52 |
| - fn_passes = True |
53 |
| - count = 0 |
54 |
| - after = None |
55 |
| - for line in passes: |
56 |
| - line = line.strip() |
57 |
| - if not line or line.startswith('#'): |
58 |
| - continue |
59 |
| - if line == '-opt-mod-passes': |
60 |
| - fn_passes = False |
61 |
| - if after: |
62 |
| - after.append(line) |
63 |
| - continue |
64 |
| - if not fn_passes: |
65 |
| - if line == pass_name: |
66 |
| - count += 1 |
67 |
| - if count >= invocation: |
68 |
| - after = [line] |
69 |
| - continue |
70 |
| - before.append(line) |
71 |
| - return before, after |
| 30 | + parser = argparse.ArgumentParser( |
| 31 | + formatter_class=argparse.RawDescriptionHelpFormatter, description=__doc__ |
| 32 | + ) |
| 33 | + parser.add_argument( |
| 34 | + "-p", |
| 35 | + dest="desired_pass", |
| 36 | + metavar="<desired-pass>", |
| 37 | + required=True, |
| 38 | + help="stop before this module pass (per-function prepass not supported).", |
| 39 | + ) |
| 40 | + parser.add_argument( |
| 41 | + "-n", |
| 42 | + dest="invocation", |
| 43 | + metavar="<invocation>", |
| 44 | + type=int, |
| 45 | + default=1, |
| 46 | + help="pass invocation number on which to stop (default=1)", |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + "hlsl_file", metavar="<hlsl-file>", help="input HLSL file path to compile" |
| 50 | + ) |
| 51 | + parser.add_argument( |
| 52 | + "-o", |
| 53 | + dest="output_file", |
| 54 | + metavar="<output-file>", |
| 55 | + required=True, |
| 56 | + help="output file name", |
| 57 | + ) |
| 58 | + parser.add_argument( |
| 59 | + "compilation_options", |
| 60 | + nargs="*", |
| 61 | + metavar="-- <DXC options>", |
| 62 | + help="set of compilation options needed to compile the HLSL file with DXC", |
| 63 | + ) |
| 64 | + return parser.parse_args() |
| 65 | + |
| 66 | + |
| 67 | +def SplitAtPass(passes, pass_name, invocation=1): |
| 68 | + pass_name = "-" + pass_name |
| 69 | + before = [] |
| 70 | + fn_passes = True |
| 71 | + count = 0 |
| 72 | + after = None |
| 73 | + for line in passes: |
| 74 | + line = line.strip() |
| 75 | + if not line or line.startswith("#"): |
| 76 | + continue |
| 77 | + if line == "-opt-mod-passes": |
| 78 | + fn_passes = False |
| 79 | + if after: |
| 80 | + after.append(line) |
| 81 | + continue |
| 82 | + if not fn_passes: |
| 83 | + if line == pass_name: |
| 84 | + count += 1 |
| 85 | + if count >= invocation: |
| 86 | + after = [line] |
| 87 | + continue |
| 88 | + before.append(line) |
| 89 | + return before, after |
| 90 | + |
72 | 91 |
|
73 | 92 | def GetTempFilename(*args, **kwargs):
|
74 |
| - "Get temp filename and close the file handle for use by others" |
75 |
| - fd, name = tempfile.mkstemp(*args, **kwargs) |
76 |
| - os.close(fd) |
77 |
| - return name |
| 93 | + "Get temp filename and close the file handle for use by others" |
| 94 | + fd, name = tempfile.mkstemp(*args, **kwargs) |
| 95 | + os.close(fd) |
| 96 | + return name |
| 97 | + |
78 | 98 |
|
79 | 99 | def main(args):
|
80 |
| - try: |
81 |
| - # 1. Gets the pass list for an HLSL compilation using -Odump |
82 |
| - cmd = ['dxc', '/Odump', args.hlsl_file] + args.compilation_options |
83 |
| - # print(cmd) |
84 |
| - all_passes = subprocess.check_output(cmd, text=True) |
85 |
| - all_passes = all_passes.splitlines() |
86 |
| - |
87 |
| - # 2. Compiles HLSL with -fcgl and outputs to intermediate IR |
88 |
| - fcgl_file = GetTempFilename('.ll') |
89 |
| - cmd = (['dxc', '-fcgl', '-Fc', fcgl_file, args.hlsl_file] |
90 |
| - + args.compilation_options) |
91 |
| - # print(cmd) |
92 |
| - subprocess.check_call(cmd) |
93 |
| - |
94 |
| - # 3. Collects list of passes before the desired pass and adds |
95 |
| - # -hlsl-passes-pause to write correct metadata |
96 |
| - passes_before, passes_after = SplitAtPass( |
97 |
| - all_passes, args.desired_pass, args.invocation) |
98 |
| - print('\nPasses before: {}\n\nRemaining passes: {}' |
99 |
| - .format(' '.join(passes_before), ' '.join(passes_after))) |
100 |
| - passes_before.append('-hlsl-passes-pause') |
101 |
| - |
102 |
| - # 4. Invokes dxopt to run passes on -fcgl output and write bitcode result |
103 |
| - bitcode_file = GetTempFilename('.bc') |
104 |
| - cmd = ['dxopt', '-o=' + bitcode_file, fcgl_file] + passes_before |
105 |
| - # print(cmd) |
106 |
| - subprocess.check_call(cmd) |
107 |
| - |
108 |
| - # 5. Disassembles bitcode to .ll file for use as a test |
109 |
| - temp_out = GetTempFilename('.ll') |
110 |
| - cmd = ['dxc', '/dumpbin', '-Fc', temp_out, bitcode_file] |
111 |
| - # print(cmd) |
112 |
| - subprocess.check_call(cmd) |
113 |
| - |
114 |
| - # 6. Inserts RUN line with -hlsl-passes-resume and desired pass |
115 |
| - with open(args.output_file, 'wt') as f: |
116 |
| - f.write('; RUN: %opt %s -hlsl-passes-resume {} -S | FileCheck %s\n\n' |
117 |
| - .format(args.desired_pass)) |
118 |
| - with open(temp_out, 'rt') as f_in: |
119 |
| - f.write(f_in.read()) |
120 |
| - |
121 |
| - # Clean up temp files |
122 |
| - os.unlink(fcgl_file) |
123 |
| - os.unlink(bitcode_file) |
124 |
| - os.unlink(temp_out) |
125 |
| - except: |
126 |
| - print(f'\nSomething went wrong!\nMost recent command and arguments: {cmd}\n') |
127 |
| - raise |
128 |
| - |
129 |
| -if __name__=='__main__': |
130 |
| - args = ParseArgs() |
131 |
| - main(args) |
132 |
| - print('\nSuccess! See output file:\n{}'.format(args.output_file)) |
| 100 | + try: |
| 101 | + # 1. Gets the pass list for an HLSL compilation using -Odump |
| 102 | + cmd = ["dxc", "/Odump", args.hlsl_file] + args.compilation_options |
| 103 | + # print(cmd) |
| 104 | + all_passes = subprocess.check_output(cmd, text=True) |
| 105 | + all_passes = all_passes.splitlines() |
| 106 | + |
| 107 | + # 2. Compiles HLSL with -fcgl and outputs to intermediate IR |
| 108 | + fcgl_file = GetTempFilename(".ll") |
| 109 | + cmd = [ |
| 110 | + "dxc", |
| 111 | + "-fcgl", |
| 112 | + "-Fc", |
| 113 | + fcgl_file, |
| 114 | + args.hlsl_file, |
| 115 | + ] + args.compilation_options |
| 116 | + # print(cmd) |
| 117 | + subprocess.check_call(cmd) |
| 118 | + |
| 119 | + # 3. Collects list of passes before the desired pass and adds |
| 120 | + # -hlsl-passes-pause to write correct metadata |
| 121 | + passes_before, passes_after = SplitAtPass( |
| 122 | + all_passes, args.desired_pass, args.invocation |
| 123 | + ) |
| 124 | + print( |
| 125 | + "\nPasses before: {}\n\nRemaining passes: {}".format( |
| 126 | + " ".join(passes_before), " ".join(passes_after) |
| 127 | + ) |
| 128 | + ) |
| 129 | + passes_before.append("-hlsl-passes-pause") |
| 130 | + |
| 131 | + # 4. Invokes dxopt to run passes on -fcgl output and write bitcode result |
| 132 | + bitcode_file = GetTempFilename(".bc") |
| 133 | + cmd = ["dxopt", "-o=" + bitcode_file, fcgl_file] + passes_before |
| 134 | + # print(cmd) |
| 135 | + subprocess.check_call(cmd) |
| 136 | + |
| 137 | + # 5. Disassembles bitcode to .ll file for use as a test |
| 138 | + temp_out = GetTempFilename(".ll") |
| 139 | + cmd = ["dxc", "/dumpbin", "-Fc", temp_out, bitcode_file] |
| 140 | + # print(cmd) |
| 141 | + subprocess.check_call(cmd) |
| 142 | + |
| 143 | + # 6. Inserts RUN line with -hlsl-passes-resume and desired pass |
| 144 | + with open(args.output_file, "wt") as f: |
| 145 | + f.write( |
| 146 | + "; RUN: %opt %s -hlsl-passes-resume {} -S | FileCheck %s\n\n".format( |
| 147 | + args.desired_pass |
| 148 | + ) |
| 149 | + ) |
| 150 | + with open(temp_out, "rt") as f_in: |
| 151 | + f.write(f_in.read()) |
| 152 | + |
| 153 | + # Clean up temp files |
| 154 | + os.unlink(fcgl_file) |
| 155 | + os.unlink(bitcode_file) |
| 156 | + os.unlink(temp_out) |
| 157 | + except: |
| 158 | + print(f"\nSomething went wrong!\nMost recent command and arguments: {cmd}\n") |
| 159 | + raise |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + args = ParseArgs() |
| 164 | + main(args) |
| 165 | + print("\nSuccess! See output file:\n{}".format(args.output_file)) |
0 commit comments