inline gallivm_compile_module

This commit is contained in:
Christopher Milan
2025-10-12 14:18:25 -07:00
parent 299b489e33
commit b65983b151
3 changed files with 52 additions and 20 deletions

View File

@@ -501,6 +501,7 @@ generate_mesa() {
$MESA_SRC/src/nouveau/headers/nv_device_info.h \
$MESA_SRC/src/nouveau/compiler/nak.h \
$MESA_SRC/src/gallium/auxiliary/gallivm/lp_bld.h \
$MESA_SRC/src/gallium/auxiliary/gallivm/lp_bld_passmgr.h \
$MESA_SRC/src/gallium/auxiliary/gallivm/lp_bld_misc.h \
$MESA_SRC/src/gallium/auxiliary/gallivm/lp_bld_type.h \
$MESA_SRC/src/gallium/auxiliary/gallivm/lp_bld_init.h \

View File

@@ -15358,6 +15358,35 @@ try:
lp_context_destroy.argtypes = [ctypes.POINTER(struct_lp_context_ref)]
except AttributeError:
pass
class struct_lp_passmgr(Structure):
pass
class struct_LLVMOpaqueModule(Structure):
pass
LLVMModuleRef = ctypes.POINTER(struct_LLVMOpaqueModule)
try:
lp_passmgr_create = _libraries['libtinymesa_cpu.so'].lp_passmgr_create
lp_passmgr_create.restype = ctypes.c_bool
lp_passmgr_create.argtypes = [LLVMModuleRef, ctypes.POINTER(ctypes.POINTER(struct_lp_passmgr))]
except AttributeError:
pass
class struct_LLVMOpaqueTargetMachine(Structure):
pass
LLVMTargetMachineRef = ctypes.POINTER(struct_LLVMOpaqueTargetMachine)
try:
lp_passmgr_run = _libraries['libtinymesa_cpu.so'].lp_passmgr_run
lp_passmgr_run.restype = None
lp_passmgr_run.argtypes = [ctypes.POINTER(struct_lp_passmgr), LLVMModuleRef, LLVMTargetMachineRef, ctypes.POINTER(ctypes.c_char)]
except AttributeError:
pass
try:
lp_passmgr_dispose = _libraries['libtinymesa_cpu.so'].lp_passmgr_dispose
lp_passmgr_dispose.restype = None
lp_passmgr_dispose.argtypes = [ctypes.POINTER(struct_lp_passmgr)]
except AttributeError:
pass
class struct_lp_cached_code(Structure):
pass
@@ -15404,10 +15433,6 @@ except AttributeError:
class struct_LLVMOpaqueExecutionEngine(Structure):
pass
class struct_LLVMOpaqueModule(Structure):
pass
LLVMModuleRef = ctypes.POINTER(struct_LLVMOpaqueModule)
class struct_LLVMOpaqueMCJITMemoryManager(Structure):
pass
@@ -15507,9 +15532,6 @@ struct_lp_build_context._fields_ = [
class struct_LLVMOpaqueTargetData(Structure):
pass
class struct_lp_passmgr(Structure):
pass
class struct_LLVMOpaqueBuilder(Structure):
pass
@@ -17420,10 +17442,10 @@ __all__ = \
'LLVMModuleRef', 'LLVMPPC_FP128TypeKind', 'LLVMPointerTypeKind',
'LLVMScalableVectorTypeKind', 'LLVMStructTypeKind',
'LLVMTargetExtTypeKind', 'LLVMTargetLibraryInfoRef',
'LLVMTokenTypeKind', 'LLVMTypeKind', 'LLVMTypeKind__enumvalues',
'LLVMTypeRef', 'LLVMValueRef', 'LLVMVectorTypeKind',
'LLVMVoidTypeKind', 'LLVMX86_AMXTypeKind', 'LLVMX86_FP80TypeKind',
'LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV',
'LLVMTargetMachineRef', 'LLVMTokenTypeKind', 'LLVMTypeKind',
'LLVMTypeKind__enumvalues', 'LLVMTypeRef', 'LLVMValueRef',
'LLVMVectorTypeKind', 'LLVMVoidTypeKind', 'LLVMX86_AMXTypeKind',
'LLVMX86_FP80TypeKind', 'LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV',
'LP_BLD_TEX_MODIFIER_EXPLICIT_LOD',
'LP_BLD_TEX_MODIFIER_LOD_BIAS', 'LP_BLD_TEX_MODIFIER_LOD_ZERO',
'LP_BLD_TEX_MODIFIER_NONE', 'LP_BLD_TEX_MODIFIER_PROJECTED',
@@ -18153,7 +18175,8 @@ __all__ = \
'lp_llvm_buffer_num_elements', 'lp_llvm_descriptor_base',
'lp_mantissa', 'lp_native_vector_width',
'lp_nir_array_build_gather_values', 'lp_nir_call_context_args',
'lp_packed_img_op_from_intrinsic', 'lp_sampler_lod_property',
'lp_packed_img_op_from_intrinsic', 'lp_passmgr_create',
'lp_passmgr_dispose', 'lp_passmgr_run', 'lp_sampler_lod_property',
'lp_set_module_stack_alignment_override', 'lp_set_target_options',
'lp_sizeof_llvm_type', 'lp_translate_atomic_op', 'lp_type_fixed',
'lp_type_float', 'lp_type_float_vec', 'lp_type_from_format',
@@ -19741,10 +19764,11 @@ __all__ = \
'struct_LLVMOpaqueMetadata', 'struct_LLVMOpaqueModule',
'struct_LLVMOpaqueTargetData',
'struct_LLVMOpaqueTargetLibraryInfotData',
'struct_LLVMOpaqueType', 'struct_LLVMOpaqueValue',
'struct__IO_FILE', 'struct__IO_codecvt', 'struct__IO_marker',
'struct__IO_wide_data', 'struct___va_list_tag', 'struct_blob',
'struct_blob_reader', 'struct_c__SA_linear_opts',
'struct_LLVMOpaqueTargetMachine', 'struct_LLVMOpaqueType',
'struct_LLVMOpaqueValue', 'struct__IO_FILE', 'struct__IO_codecvt',
'struct__IO_marker', 'struct__IO_wide_data',
'struct___va_list_tag', 'struct_blob', 'struct_blob_reader',
'struct_c__SA_linear_opts',
'struct_c__SA_nir_input_to_output_deps',
'struct_c__SA_nir_input_to_output_deps_0',
'struct_c__SA_nir_output_clipper_var_groups',

View File

@@ -21,8 +21,8 @@ class LVPCompiler(NIRCompiler):
def compile(self, src) -> bytes:
shader, ctx, cache = deserialize(src, mesa.lvp_nir_options), llvm.LLVMGetGlobalContext(), mesa.struct_lp_cached_code()
gallivm = mesa.gallivm_create(None, mesa.lp_context_ref(ctypes.cast(ctx, ctypes.POINTER(mesa.struct_LLVMOpaqueContext)), True), cache)
module, builder = ctypes.cast(gallivm.contents.module, llvm.LLVMModuleRef), ctypes.cast(gallivm.contents.builder, llvm.LLVMBuilderRef)
gallivm = mesa.gallivm_create(None, mesa.lp_context_ref(ctypes.cast(ctx, ctypes.POINTER(mesa.struct_LLVMOpaqueContext)), True), cache).contents
module, builder = ctypes.cast(gallivm.module, llvm.LLVMModuleRef), ctypes.cast(gallivm.builder, llvm.LLVMBuilderRef)
params = mesa.struct_lp_build_tgsi_params(mesa.struct_lp_type(floating=True, sign=True, width=32, length=4),
resources_type=mesa.lp_build_jit_resources_type(gallivm), mask=ctypes.pointer(mesa.struct_lp_build_mask_context()))
@@ -39,9 +39,16 @@ class LVPCompiler(NIRCompiler):
mesa.lp_build_nir_soa(gallivm, shader, params, None)
llvm.LLVMBuildRetVoid(builder)
mesa.gallivm_verify_function(gallivm, ctypes.cast(fn, mesa.LLVMValueRef))
mesa.gallivm_compile_module(gallivm)
mesa.gallivm_jit_function(gallivm, ctypes.cast(fn, mesa.LLVMValueRef), shader.contents.info.name)
# gallivm_compile_module
llvm.LLVMSetDataLayout(module, b"")
mesa.lp_build_create_jit_compiler_for_module(gallivm.engine, gallivm.code, gallivm.cache, gallivm.module, gallivm.memorymgr, 2, None)
tgt = ctypes.cast(llvm.LLVMGetExecutionEngineTargetMachine(ctypes.cast(gallivm.engine, llvm.LLVMExecutionEngineRef)), mesa.LLVMTargetMachineRef)
mesa.lp_passmgr_run(gallivm.passmgr, gallivm.module, tgt, gallivm.module_name)
llvm.LLVMGetPointerToGlobal(ctypes.cast(gallivm.engine, llvm.LLVMExecutionEngineRef), fn) # triggers emitObject
ret = ctypes.string_at(cache.data, cache.data_size)
mesa.gallivm_destroy(gallivm)
mesa.ralloc_free(shader)
return ret