Skip to content

Commit 0c258ff

Browse files
prex03Prerak Singh
andauthored
Added LLVM optimizations for bubble sort (#694)
Co-authored-by: Prerak Singh <[email protected]>
1 parent a2ba236 commit 0c258ff

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

pydatastructs/linear_data_structures/_backend/cpp/algorithms/llvm_algorithms.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ def _ensure_target_machine():
3232
binding.initialize_native_asmprinter()
3333

3434
target = binding.Target.from_default_triple()
35-
_target_machine = target.create_target_machine()
35+
_target_machine = target.create_target_machine(
36+
opt=3,
37+
features="+sse,+sse2,+sse3,+ssse3,+sse4.1,+sse4.2,+avx,+avx2"
38+
)
3639
except Exception as e:
3740
raise RuntimeError(f"Failed to initialize LLVM target machine: {e}")
3841

@@ -110,7 +113,7 @@ def _build_bubble_sort_ir(dtype: str) -> str:
110113
if isinstance(T, ir.IntType):
111114
should_swap = b.icmp_signed(">", val_j, val_jp1)
112115
else:
113-
should_swap = b.fcmp_ordered(">", val_j, val_jp1)
116+
should_swap = b.fcmp_ordered(">", val_j, val_jp1, fastmath=True)
114117

115118
b.cbranch(should_swap, b_swap, b_inner_latch)
116119

@@ -145,6 +148,32 @@ def _materialize(dtype: str) -> int:
145148
mod = binding.parse_assembly(llvm_ir)
146149
mod.verify()
147150

151+
pmb = binding.PassManagerBuilder()
152+
pmb.opt_level = 3
153+
pmb.loop_vectorize = True
154+
pmb.slp_vectorize = True
155+
156+
fpm = binding.create_function_pass_manager(mod)
157+
pm = binding.create_module_pass_manager()
158+
159+
pm.add_basic_alias_analysis_pass()
160+
pm.add_type_based_alias_analysis_pass()
161+
pm.add_instruction_combining_pass()
162+
pm.add_gvn_pass()
163+
pm.add_cfg_simplification_pass()
164+
pm.add_loop_unroll_pass()
165+
pm.add_loop_unswitch_pass()
166+
167+
pmb.populate(fpm)
168+
pmb.populate(pm)
169+
170+
fpm.initialize()
171+
for func in mod.functions:
172+
fpm.run(func)
173+
fpm.finalize()
174+
175+
pm.run(mod)
176+
148177
engine = binding.create_mcjit_compiler(mod, _target_machine)
149178
engine.finalize_object()
150179
engine.run_static_constructors()

0 commit comments

Comments
 (0)