Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
undef_value = ir.Constant(self.datamodel.get_value_type(), None)
data = self.datamodel.as_data(builder, undef_value)
self.assertIsNot(data, NotImplemented,
"as_data returned NotImplemented")
self.assertEqual(data.type, self.datamodel.get_data_type())
rev_value = self.datamodel.from_data(builder, data)
self.assertEqual(rev_value.type,
self.datamodel.get_value_type())
builder.ret_void() # end function
# Ensure valid LLVM generation
materialized = ll.parse_assembly(str(self.module))
str(materialized)
def __init__(self, module_name):
initialize_llvm()
self._data_layout = None
self._llvm_module = ll.parse_assembly(
str(self._create_empty_module(module_name)))
self._llvm_module.name = "global_codegen_module"
self._rtlinker = RuntimeLinker()
self._init(self._llvm_module)
define i32 @sum(i32 %.1, i32 %.2) {
%.3 = add i32 %.1, %.2
%.4 = add i32 0, %.3
ret i32 %.4
}
define void @foo() {
call void asm sideeffect "nop", ""()
ret void
}
declare void @a_readonly_func(i8 *) readonly
'''
m = llvm.parse_assembly(ir)
for f in m.functions:
print(f'Function: {f.name}/`{f.type}`')
assert f.module is m
assert f.function is None
assert f.block is None
assert f.is_function and not (f.is_block or f.is_instruction)
print(f'Function attributes: {list(f.attributes)}')
for a in f.arguments:
print(f'Argument: {a.name}/`{a.type}`')
print(f'Argument attributes: {list(a.attributes)}')
for b in f.blocks:
print(f'Block: {b.name}/`{b.type}`\n{b}\nEnd of Block')
assert b.module is m
assert b.function is f
assert b.block is None
def compile_ir(engine, llvm_ir):
"""
Compile the LLVM IR string with the given engine.
The compiled module object is returned.
"""
# Create a LLVM module object from the IR
mod = llvm.parse_assembly(llvm_ir)
mod.verify()
# Now add the module and make sure it is ready for execution
engine.add_module(mod)
engine.finalize_object()
return mod
def compile_ir(engine, llvm_ir):
"""
Compile the LLVM IR string with the given engine.
The compiled module object is returned.
"""
# Create a LLVM module object from the IR
mod = llvm.parse_assembly(llvm_ir)
mod.verify()
# Now add the module and make sure it is ready for execution
engine.add_module(mod)
engine.finalize_object()
engine.run_static_constructors()
return mod
def __init__(self, codegen, name):
self._codegen = codegen
self._name = name
self._linking_libraries = set()
self._final_module = ll.parse_assembly(
str(self._codegen._create_empty_module(self._name)))
self._final_module.name = cgutils.normalize_ir_text(self._name)
self._shared_module = None
# Track names of the dynamic globals
self._dynamic_globals = []
def buildSharedObject(self, functions):
"""Add native definitions and return a BinarySharedObject representing the compiled code."""
module = self.converter.add_functions(functions)
try:
mod = llvm.parse_assembly(module.moduleText)
mod.verify()
except Exception:
print("failing: ", module)
raise
# Now add the module and make sure it is ready for execution
self.engine.add_module(mod)
if self.optimize:
self.module_pass_manager.run(mod)
return BinarySharedObject.fromModule(
mod,
module.globalVariableDefinitions,
module.functionNameToType,
)
def create_execution_engine():
"""
Create an ExecutionEngine suitable for JIT code generation on
the host CPU. The engine is reusable for an arbitrary number of
modules.
"""
# Create a target machine representing the host
target = llvm.Target.from_default_triple()
target_machine = target.create_target_machine()
# And an execution engine with an empty backing module
backing_mod = llvm.parse_assembly("")
engine = llvm.create_mcjit_compiler(backing_mod, target_machine)
return engine
Examples
--------
To get the address of the compiled functions, use::
addr = engine.get_function_address("")
"""
triple = re.search(
r'target\s+triple\s*=\s*"(?P[-\d\w\W_]+)"\s*$',
ir, re.M).group('triple')
# Create execution engine
target = llvm.Target.from_triple(triple)
target_machine = target.create_target_machine()
backing_mod = llvm.parse_assembly("")
engine = llvm.create_mcjit_compiler(backing_mod, target_machine)
# Create LLVM module and compile
mod = llvm.parse_assembly(ir)
mod.verify()
engine.add_module(mod)
engine.finalize_object()
engine.run_static_constructors()
return engine
def create_execution_engine():
if _engineCache:
return _engineCache[0]
pmb = llvm.create_pass_manager_builder()
pmb.opt_level = 3
pmb.size_level = 0
pmb.inlining_threshold = 1
pass_manager = llvm.create_module_pass_manager()
pmb.populate(pass_manager)
target_machine.add_analysis_passes(pass_manager)
# And an execution engine with an empty backing module
backing_mod = llvm.parse_assembly("")
engine = llvm.create_mcjit_compiler(backing_mod, target_machine)
_engineCache.append((engine, pass_manager))
return engine, pass_manager