Peano
Loading...
Searching...
No Matches
PipelineBuilder.py
Go to the documentation of this file.
1from collections import OrderedDict
2
4 xdsl_passes = [
5 "reuse-memory-allocations",
6 "fuse-memory-allocations",
7 "imperfect-nested-loops-to-perfect",
8 "nested-parallel-ops-to-multidimensional",
9 "nested-parallel-ops-to-for-ops",
10 "scf-parallel-loop-fusion",
11 "convert-scf-to-omp-tasks"
12 ]
13
14 def __init__(self):
15 self.use_gpu = False
16 self.gpu_vendor = None
17 self.gpu_architecture = None
18
19 self.use_omp = False
20 self.use_omp_tasks = False
21 self.omp_task_version="taskloop"
23
24 self.flatten_memrefs = False
25
28
29 self.loop_fusion = False
30
33
35 self.memory_reuse = False
36 self.max_patch_size = None
37
40
41 def generate_pass_string(self, passes, pass_name):
42 if passes[pass_name] =={}:
43 return pass_name
44
45 arguments = []
46 for argument in passes[pass_name].keys():
47 arguments.append(f"{argument}={passes[pass_name][argument]}")
48 return f"{pass_name}{{{' '.join(arguments)}}}"
49
51 passes = OrderedDict()
52
53 if self.memory_reuse:
54 passes.update({"reuse-memory-allocations" : {"max-allocation-size": str(self.max_patch_size)}})
55
56 if self.loop_fusion:
57 passes.update({"cse": {}})
58 passes.update({"scf-parallel-loop-fusion": {"cell_local_callees": "sourceTerm_bridge,flux_bridge,nonconservativeProduct_bridge,maxEigenvalue_bridge"}})
59
61 passes.update({"control-flow-sink" : {}})
62 passes.update({"imperfect-nested-loops-to-perfect" :{}})
63
65 raise Exception("These passes are contradictory")
67 passes.update({"nested-parallel-ops-to-multidimensional" : {}})
69 passes.update({"nested-parallel-ops-to-for-ops" : {"number-of-parallel-ops": str(self.max_nested_parallel_count)}})
70
71 if self.flatten_memrefs:
72 passes.update({"dslhype-linearize-nested-memrefs" : {}})
73 passes.update({"canonicalize" : {}})
74
76 passes.update({"fuse-memory-allocations": {}})
77
78 if self.use_gpu:
79 passes.update({f"""canonicalize,cse,loop-invariant-code-motion,dslhype-host-to-device-memspace{{mem-space=global}},dslhype-include-physics{{physics-file={self.polygeist_function_source_file} mem-space=global}},dslhype-host-to-device-memspace{{mem-space=global}},inline,expand-strided-metadata,fold-memref-alias-ops,symbol-dce,canonicalize,func.func(gpu-map-parallel-loops),convert-parallel-loops-to-gpu,fold-memref-alias-ops,lower-affine,gpu-kernel-outlining,func.func(gpu-async-region),canonicalize,convert-arith-to-llvm{{index-bitwidth=64}},convert-scf-to-cf,convert-cf-to-llvm{{index-bitwidth=64}},nvvm-attach-target{{chip={self.gpu_architecture}}},gpu.module(inline,convert-gpu-to-nvvm,canonicalize,cse),gpu-to-llvm,gpu-module-to-binary,finalize-memref-to-llvm{{index-bitwidth=64}},reconcile-unrealized-casts""": {}})
80 else:
81 if self.use_omp:
82 if self.use_omp_tasks:
83 task_args = {}
84 if self.omp_task_version == "taskloop":
85 task_args.update({"mode": "taskloop"})
86 else:
87 task_args.update({"mode": "task"})
88 task_args.update({"depend": "true"})
89 task_args.update({"chunk": "8"})
90 if self.omp_single_region:
91 task_args.update({"single_region": "true"})
92 passes.update({"convert-scf-to-omp-tasks" : task_args})
93 else:
94 passes.update({"convert-scf-to-openmp" : {}})
95
97 passes.update({"dslhype-include-physics" : {"physics-file": self.polygeist_function_source_file}})
98 passes.update({"inline" : {}})
99 passes.update({"canonicalize" : {}})
100
101
102 passes.update({"cse" : {}})
103 passes.update({"loop-invariant-code-motion" : {}})
104 passes.update({"canonicalize" : {}})
105 passes.update({"expand-strided-metadata" : {}})
106 passes.update({"fold-memref-alias-ops" : {}})
107 passes.update({"lower-affine" : {}})
108 passes.update({"loop-invariant-code-motion" : {}})
109 passes.update({"canonicalize" : {}})
110 passes.update({"cse" : {}})
111 passes.update({"convert-scf-to-cf" : {}})
112
113 if self.use_omp:
114 passes.update({"convert-openmp-to-llvm" : {}})
115
116 passes.update({"convert-cf-to-llvm" : {}})
117 passes.update({"convert-arith-to-llvm" : {}})
118 passes.update({"convert-math-to-llvm" : {}})
119 passes.update({"convert-func-to-llvm" : {}})
120 passes.update({"finalize-memref-to-llvm" : {"index-bitwidth": "64"}})
121 passes.update({"reconcile-unrealized-casts" : {}})
122 passes.update({"canonicalize" : {}})
123 passes.update({"cse" : {}})
124
125 pass_names = list(passes.keys())
126 initial_dslhype_opt_passes = []
127 index = 0
128
129 for pass_name in pass_names:
130 if pass_name in PipelineBuilder.xdsl_passes:
131 break
132 initial_dslhype_opt_passes.append(self.generate_pass_string(passes, pass_name))
133 index = index + 1
134
135 xdsl_passes_processed = False
136 pipeline_command = f"""dslhype-opt --mlir-print-op-generic --pass-pipeline="builtin.module({",".join(initial_dslhype_opt_passes)})" $<"""
137 while index < len(pass_names):
138 next_set_of_passes = []
139 if xdsl_passes_processed:
140 for pass_name in pass_names[index:]:
141 if pass_name in PipelineBuilder.xdsl_passes:
142 break
143 next_set_of_passes.append(self.generate_pass_string(passes, pass_name))
144 index = index + 1
145 pipeline_command = f"""{pipeline_command} | dslhype-opt --mlir-print-op-generic --pass-pipeline="builtin.module({",".join(next_set_of_passes)})" """
146 xdsl_passes_processed = False
147 else:
148 for pass_name in pass_names[index:]:
149 if pass_name not in PipelineBuilder.xdsl_passes:
150 break
151 next_set_of_passes.append('"' + self.generate_pass_string(passes, pass_name) + '"')
152 index = index + 1
153 pipeline_command = f"""{pipeline_command} | xdsl-opt -p {",".join(next_set_of_passes)},strip-visibility-property"""
154 xdsl_passes_processed = True
155
156 return pipeline_command
157
158 def enable_gpu(self, gpu_vendor, gpu_architecture, polygeist_function_source_file):
159 self.use_gpu = True
160 self.gpu_vendor = gpu_vendor
161 self.gpu_architecture = gpu_architecture
162
163 # GPU usage requires flattened memrefs and inlined functions
165 self.enable_inline_polygeist_functions(polygeist_function_source_file)
166
167 def enable_omp(self, use_tasks=False, omp_task_version="taskloop", omp_single_region=False):
168 self.use_omp = True
169 self.use_omp_tasks = use_tasks
170 self.omp_task_version=omp_task_version
171 self.omp_single_region=omp_single_region
172
174 self.flatten_memrefs = True
175
177 self.fuse_memory_allocations = True
178
179 def enable_memory_reuse(self, max_patch_size):
180 self.memory_reuse = True
181 self.max_patch_size = max_patch_size
182
183 def enable_inline_polygeist_functions(self, polygeist_function_source_file):
185 self.polygeist_function_source_file = polygeist_function_source_file
186
191 self.loop_fusion = True
192
197 def enable_nested_parallel_ops_to_for_ops(self, max_nested_parallel_count=-1):
199 self.max_nested_parallel_count = max_nested_parallel_count
enable_nested_parallel_ops_to_for_ops(self, max_nested_parallel_count=-1)
enable_gpu(self, gpu_vendor, gpu_architecture, polygeist_function_source_file)
enable_inline_polygeist_functions(self, polygeist_function_source_file)
generate_pass_string(self, passes, pass_name)
enable_omp(self, use_tasks=False, omp_task_version="taskloop", omp_single_region=False)
enable_memory_reuse(self, max_patch_size)