[llvm] [llvm][SPIRV] Add pass to lower Ctors/Dtors for SPIRV (PR #187509)

Nick Sarnie via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 23 08:37:54 PDT 2026


================
@@ -0,0 +1,49 @@
+; Test that the SPIR-V constructor/destructor lowering pass creates the
+; expected init kernel and symbols for offload compilation.
+
+; RUN: llc -mtriple=spirv64-intel-unknown --spirv-ext=+SPV_INTEL_function_pointers %s -o - | FileCheck %s
+; Fix when spir-val supports the SPV_INTEL_function_pointers extension:
+; FIXME: %if spirv-tools %{ llc -O0 -mtriple=spirv64-intel-unknown --spirv-ext=+SPV_INTEL_function_pointers %s -o - -filetype=obj | spirv-val %}
+
+define void @my_constructor() addrspace(9) {
+entry:
+  ret void
+}
+
+define void @my_destructor() addrspace(9) {
+entry:
+  ret void
+}
+
+define spir_kernel void @main() {
+entry:
+  ret void
+}
+
+ at llvm.global_ctors = appending global [1 x { i32, ptr addrspace(9), ptr addrspace(9) }] [
+  { i32, ptr addrspace(9), ptr addrspace(9) } { i32 65535, ptr addrspace(9) @my_constructor, ptr addrspace(9) null }
+]
+
+ at llvm.global_dtors = appending global [1 x { i32, ptr addrspace(9), ptr addrspace(9) }] [
+  { i32, ptr addrspace(9), ptr addrspace(9) } { i32 65535, ptr addrspace(9) @my_destructor, ptr addrspace(9) null }
+]
+
+; This module has the openmp.is_target_device metadata to indicate offload mode.
+!llvm.module.flags = !{!0}
+!0 = !{i32 7, !"openmp-device", i32 50}
+
+; CHECK-DAG: OpName %[[#INIT_KERNEL:]] "spirv$device$init"
+; CHECK-DAG: OpName %[[#FINI_KERNEL:]] "spirv$device$fini"
+; CHECK-DAG: OpName %[[#INIT_START:]] "__init_array_start"
+; CHECK-DAG: OpName %[[#INIT_END:]] "__init_array_end"
----------------
sarnex wrote:

should we check where the name is used to make sure it ends up in a `OpEntryPoint` or whatever?

https://github.com/llvm/llvm-project/pull/187509


More information about the llvm-commits mailing list