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

This PR adds a new SPIRV pass that generates a kernel named
"spirv$device$init" that iterates the pointers in the table pointed by
__init_array_start and __init_array_end and executes them. It also
generates symbols for each constructor with the form
__init_array_object_NAME_PRIORITY.

These symbols will be used by the Level Zero plugin in the liboffload
runtime (with the support introduced by #187510) to generate the
aforementioned table as spirv-link cannot create the table itself.

It also does the same thing for destructors, with the kernel name being
"spirv$device$fini", the table pointers __fini_array_start and
__fini_array_end, and the generated symbols prefix __fini_array_object.

The code was mostly generated by Claude 4.5 and has been reviewed by me
to the best of my ability.
This commit is contained in:
Alex Duran 2026-03-24 22:47:09 +01:00 committed by GitHub
parent 6a045c29a9
commit de0c366e4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 416 additions and 0 deletions

View File

@ -22,6 +22,7 @@ add_llvm_target(SPIRVCodeGen
SPIRVCallLowering.cpp
SPIRVInlineAsmLowering.cpp
SPIRVCommandLine.cpp
SPIRVCtorDtorLowering.cpp
SPIRVEmitIntrinsics.cpp
SPIRVGlobalRegistry.cpp
SPIRVInstrInfo.cpp

View File

@ -35,6 +35,7 @@ FunctionPass *createSPIRVPostLegalizerPass();
ModulePass *createSPIRVEmitIntrinsicsPass(SPIRVTargetMachine *TM);
ModulePass *createSPIRVPrepareGlobalsPass();
MachineFunctionPass *createSPIRVEmitNonSemanticDIPass(SPIRVTargetMachine *TM);
ModulePass *createSPIRVCtorDtorLoweringLegacyPass();
InstructionSelector *
createSPIRVInstructionSelector(const SPIRVTargetMachine &TM,
const SPIRVSubtarget &Subtarget,
@ -59,6 +60,7 @@ void initializeSPIRVPrepareGlobalsPass(PassRegistry &);
void initializeSPIRVStripConvergentIntrinsicsPass(PassRegistry &);
void initializeSPIRVLegalizeImplicitBindingPass(PassRegistry &);
void initializeSPIRVLegalizeZeroSizeArraysLegacyPass(PassRegistry &);
void initializeSPIRVCtorDtorLoweringLegacyPass(PassRegistry &);
} // namespace llvm
#endif // LLVM_LIB_TARGET_SPIRV_SPIRV_H

View File

@ -0,0 +1,253 @@
//===-- SPIRVCtorDtorLowering.cpp - Handle global ctors and dtors --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This pass creates a unified init and fini kernel with the required metadata
// to call global constructors and destructors on SPIR-V targets.
//
//===----------------------------------------------------------------------===//
#include "SPIRVCtorDtorLowering.h"
#include "MCTargetDesc/SPIRVBaseInfo.h"
#include "SPIRV.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Value.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MD5.h"
#include "llvm/Transforms/IPO/OpenMPOpt.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
using namespace llvm;
#define DEBUG_TYPE "spirv-lower-ctor-dtor"
static cl::opt<std::string>
GlobalStr("spirv-lower-global-ctor-dtor-id",
cl::desc("Override unique ID of ctor/dtor globals."),
cl::init(""), cl::Hidden);
static cl::opt<bool>
CreateKernels("spirv-emit-init-fini-kernel",
cl::desc("Emit kernels to call ctor/dtor globals."),
cl::init(true), cl::Hidden);
namespace {
constexpr int SPIRV_GLOBAL_AS = 1;
std::string getHash(StringRef Str) {
llvm::MD5 Hasher;
llvm::MD5::MD5Result Hash;
Hasher.update(Str);
Hasher.final(Hash);
return llvm::utohexstr(Hash.low(), /*LowerCase=*/true);
}
void addKernelAttrs(Function *F) {
F->setCallingConv(CallingConv::SPIR_KERNEL);
F->addFnAttr("uniform-work-group-size", "true");
}
Function *createInitOrFiniKernelFunction(Module &M, bool IsCtor) {
StringRef InitOrFiniKernelName =
IsCtor ? "spirv$device$init" : "spirv$device$fini";
if (M.getFunction(InitOrFiniKernelName))
return nullptr;
Function *InitOrFiniKernel = Function::createWithDefaultAttr(
FunctionType::get(Type::getVoidTy(M.getContext()), false),
GlobalValue::WeakODRLinkage, 0, InitOrFiniKernelName, &M);
addKernelAttrs(InitOrFiniKernel);
return InitOrFiniKernel;
}
// We create the IR required to call each callback in this section. This is
// equivalent to the following code. Normally, the linker would provide us with
// the definitions of the init and fini array sections. The 'spirv-link' linker
// does not do this so initializing these values is done by the offload runtime.
//
// extern "C" void **__init_array_start = nullptr;
// extern "C" void **__init_array_end = nullptr;
// extern "C" void **__fini_array_start = nullptr;
// extern "C" void **__fini_array_end = nullptr;
//
// using InitCallback = void();
// using FiniCallback = void();
//
// void call_init_array_callbacks() {
// for (auto start = __init_array_start; start != __init_array_end; ++start)
// reinterpret_cast<InitCallback *>(*start)();
// }
//
// void call_fini_array_callbacks() {
// size_t fini_array_size = __fini_array_end - __fini_array_start;
// for (size_t i = fini_array_size; i > 0; --i)
// reinterpret_cast<FiniCallback *>(__fini_array_start[i - 1])();
// }
void createInitOrFiniCalls(Function &F, bool IsCtor) {
Module &M = *F.getParent();
LLVMContext &C = M.getContext();
IRBuilder<> IRB(BasicBlock::Create(C, "entry", &F));
auto *LoopBB = BasicBlock::Create(C, "while.entry", &F);
auto *ExitBB = BasicBlock::Create(C, "while.end", &F);
Type *PtrTy = IRB.getPtrTy(SPIRV_GLOBAL_AS);
auto CreateGlobal = [&](const char *Name) -> GlobalVariable * {
auto *GV = new GlobalVariable(
M, PointerType::getUnqual(C),
/*isConstant=*/false, GlobalValue::WeakAnyLinkage,
Constant::getNullValue(PointerType::getUnqual(C)), Name,
/*InsertBefore=*/nullptr, GlobalVariable::NotThreadLocal,
/*AddressSpace=*/SPIRV_GLOBAL_AS);
GV->setVisibility(GlobalVariable::ProtectedVisibility);
return GV;
};
auto *Begin = M.getOrInsertGlobal(
IsCtor ? "__init_array_start" : "__fini_array_start",
PointerType::getUnqual(C), function_ref<GlobalVariable *()>([&]() {
return CreateGlobal(IsCtor ? "__init_array_start"
: "__fini_array_start");
}));
auto *End = M.getOrInsertGlobal(
IsCtor ? "__init_array_end" : "__fini_array_end",
PointerType::getUnqual(C), function_ref<GlobalVariable *()>([&]() {
return CreateGlobal(IsCtor ? "__init_array_end" : "__fini_array_end");
}));
auto *CallBackTy = FunctionType::get(IRB.getVoidTy(), {});
// The destructor array must be called in reverse order. Get an expression to
// the end of the array and iterate backwards in that case.
Value *BeginVal = IRB.CreateLoad(Begin->getType(), Begin, "begin");
Value *EndVal = IRB.CreateLoad(Begin->getType(), End, "stop");
if (!IsCtor) {
Value *OldBeginVal = BeginVal;
BeginVal =
IRB.CreateInBoundsGEP(PointerType::getUnqual(C), EndVal,
ArrayRef<Value *>(ConstantInt::getAllOnesValue(
IntegerType::getInt64Ty(C))),
"start");
EndVal = OldBeginVal;
}
IRB.CreateCondBr(
IRB.CreateCmp(IsCtor ? ICmpInst::ICMP_NE : ICmpInst::ICMP_UGE, BeginVal,
EndVal),
LoopBB, ExitBB);
IRB.SetInsertPoint(LoopBB);
auto *CallBackPHI = IRB.CreatePHI(PtrTy, 2, "ptr");
auto *CallBack = IRB.CreateLoad(IRB.getPtrTy(F.getAddressSpace()),
CallBackPHI, "callback");
IRB.CreateCall(CallBackTy, CallBack);
auto *NewCallBack =
IRB.CreateConstGEP1_64(PtrTy, CallBackPHI, IsCtor ? 1 : -1, "next");
auto *EndCmp = IRB.CreateCmp(IsCtor ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_ULT,
NewCallBack, EndVal, "end");
CallBackPHI->addIncoming(BeginVal, &F.getEntryBlock());
CallBackPHI->addIncoming(NewCallBack, LoopBB);
IRB.CreateCondBr(EndCmp, ExitBB, LoopBB);
IRB.SetInsertPoint(ExitBB);
IRB.CreateRetVoid();
}
bool createInitOrFiniGlobals(Module &M, GlobalVariable *GV, bool IsCtor) {
ConstantArray *GA = dyn_cast<ConstantArray>(GV->getInitializer());
if (!GA || GA->getNumOperands() == 0)
return false;
// SPIR-V has no way to emit variables at specific sections or support for
// the traditional constructor sections. Instead, we emit mangled global
// names so the runtime can build the list manually.
for (Value *V : GA->operands()) {
auto *CS = cast<ConstantStruct>(V);
auto *F = cast<Constant>(CS->getOperand(1));
uint64_t Priority = cast<ConstantInt>(CS->getOperand(0))->getSExtValue();
std::string PriorityStr = "." + std::to_string(Priority);
// We append a semi-unique hash and the priority to the global name.
std::string GlobalID =
!GlobalStr.empty() ? GlobalStr : getHash(M.getSourceFileName());
std::string NameStr =
((IsCtor ? "__init_array_object_" : "__fini_array_object_") +
F->getName() + "_" + GlobalID + "_" + std::to_string(Priority))
.str();
llvm::transform(NameStr, NameStr.begin(),
[](char c) { return c == '.' ? '_' : c; });
auto *GV = new GlobalVariable(M, F->getType(), /*IsConstant=*/true,
GlobalValue::ExternalLinkage, F, NameStr,
nullptr, GlobalValue::NotThreadLocal,
/*AddressSpace=*/SPIRV_GLOBAL_AS);
GV->setSection(IsCtor ? ".init_array" + PriorityStr
: ".fini_array" + PriorityStr);
GV->setVisibility(GlobalVariable::ProtectedVisibility);
}
return true;
}
bool createInitOrFiniKernel(Module &M, StringRef GlobalName, bool IsCtor) {
GlobalVariable *GV = M.getGlobalVariable(GlobalName);
if (!GV || !GV->hasInitializer())
return false;
if (!createInitOrFiniGlobals(M, GV, IsCtor))
return false;
if (!CreateKernels)
return true;
Function *InitOrFiniKernel = createInitOrFiniKernelFunction(M, IsCtor);
if (!InitOrFiniKernel)
return false;
createInitOrFiniCalls(*InitOrFiniKernel, IsCtor);
GV->eraseFromParent();
return true;
}
bool lowerCtorsAndDtors(Module &M) {
// Only run this pass for OpenMP offload compilation
if (!llvm::omp::isOpenMPDevice(M))
return false;
bool Modified = false;
Modified |= createInitOrFiniKernel(M, "llvm.global_ctors", /*IsCtor =*/true);
Modified |= createInitOrFiniKernel(M, "llvm.global_dtors", /*IsCtor =*/false);
return Modified;
}
class SPIRVCtorDtorLoweringLegacy final : public ModulePass {
public:
static char ID;
SPIRVCtorDtorLoweringLegacy() : ModulePass(ID) {}
bool runOnModule(Module &M) override { return lowerCtorsAndDtors(M); }
};
} // End anonymous namespace
PreservedAnalyses SPIRVCtorDtorLoweringPass::run(Module &M,
ModuleAnalysisManager &AM) {
return lowerCtorsAndDtors(M) ? PreservedAnalyses::none()
: PreservedAnalyses::all();
}
char SPIRVCtorDtorLoweringLegacy::ID = 0;
INITIALIZE_PASS(SPIRVCtorDtorLoweringLegacy, DEBUG_TYPE,
"SPIRV lower ctors and dtors", false, false)
ModulePass *llvm::createSPIRVCtorDtorLoweringLegacyPass() {
return new SPIRVCtorDtorLoweringLegacy();
}

View File

@ -0,0 +1,27 @@
//===-- SPIRVCtorDtorLowering.h --------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVCTORDTORLOWERING_H
#define LLVM_LIB_TARGET_SPIRV_SPIRVCTORDTORLOWERING_H
#include "llvm/IR/PassManager.h"
namespace llvm {
class Module;
class PassRegistry;
/// Lower llvm.global_ctors and llvm.global_dtors to special kernels.
class SPIRVCtorDtorLoweringPass
: public PassInfoMixin<SPIRVCtorDtorLoweringPass> {
public:
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};
} // namespace llvm
#endif // LLVM_LIB_TARGET_SPIRV_SPIRVCTORDTORLOWERING_H

View File

@ -66,6 +66,7 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTarget() {
initializeSPIRVPrepareFunctionsPass(PR);
initializeSPIRVPrepareGlobalsPass(PR);
initializeSPIRVStripConvergentIntrinsicsPass(PR);
initializeSPIRVCtorDtorLoweringLegacyPass(PR);
}
static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
@ -186,6 +187,7 @@ void SPIRVPassConfig::addIRPasses() {
}
addPass(createSPIRVRegularizerPass());
addPass(createSPIRVCtorDtorLoweringLegacyPass());
addPass(createSPIRVPrepareFunctionsPass(TM));
addPass(createSPIRVPrepareGlobalsPass());
}

View File

@ -0,0 +1,45 @@
; Test for SPIR-V constructor/destructor lowering pass at IR level.
; RUN: llc -mtriple=spirv64-intel-unknown --spirv-ext=+SPV_INTEL_function_pointers %s -o /dev/null -print-after=spirv-lower-ctor-dtor 2>&1 | FileCheck %s
; This test verifies that:
; 1. The init/fini kernels are created.
; 2. Symbol mangling includes function name, hash, and priority.
; 3. Array boundary globals are created.
; 4. The pass only runs when offload metadata is present.
define void @high_priority() addrspace(9) {
entry:
ret void
}
define void @low_priority() addrspace(9) {
entry:
ret void
}
@llvm.global_ctors = appending global [2 x { i32, ptr addrspace(9), ptr addrspace(9) }] [
{ i32, ptr addrspace(9), ptr addrspace(9) } { i32 100, ptr addrspace(9) @high_priority, ptr addrspace(9) null },
{ i32, ptr addrspace(9), ptr addrspace(9) } { i32 50, ptr addrspace(9) @low_priority, ptr addrspace(9) null }
]
@llvm.global_dtors = appending global [2 x { i32, ptr addrspace(9), ptr addrspace(9) }] [
{ i32, ptr addrspace(9), ptr addrspace(9) } { i32 100, ptr addrspace(9) @high_priority, ptr addrspace(9) null },
{ i32, ptr addrspace(9), ptr addrspace(9) } { i32 50, ptr addrspace(9) @low_priority, ptr addrspace(9) null }
]
; Mark this as an offload module (OpenMP target device).
!llvm.module.flags = !{!0}
!0 = !{i32 7, !"openmp-device", i32 50}
; CHECK: @__init_array_object_high_priority_{{[a-f0-9]+}}_100 = protected addrspace(1) constant ptr addrspace(9) @high_priority
; CHECK: @__init_array_object_low_priority_{{[a-f0-9]+}}_50 = protected addrspace(1) constant ptr addrspace(9) @low_priority
; CHECK: @__init_array_start = weak protected addrspace(1) global ptr null
; CHECK: @__init_array_end = weak protected addrspace(1) global ptr null
; CHECK: @__fini_array_object_high_priority_{{[a-f0-9]+}}_100 = protected addrspace(1) constant ptr addrspace(9) @high_priority
; CHECK: @__fini_array_object_low_priority_{{[a-f0-9]+}}_50 = protected addrspace(1) constant ptr addrspace(9) @low_priority
; CHECK: @__fini_array_start = weak protected addrspace(1) global ptr null
; CHECK: @__fini_array_end = weak protected addrspace(1) global ptr null
; CHECK: define weak_odr spir_kernel void @"spirv$device$init"()
; CHECK: define weak_odr spir_kernel void @"spirv$device$fini"()

View File

@ -0,0 +1,48 @@
; 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
}
@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 }
]
@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"
; CHECK-DAG: OpName %[[#FINI_START:]] "__fini_array_start"
; CHECK-DAG: OpName %[[#FINI_END:]] "__fini_array_end"
; CHECK-DAG: OpName %[[#INIT_OBJ:]] "__init_array_object_my_constructor_{{[a-f0-9]+}}_65535"
; CHECK-DAG: OpName %[[#FINI_OBJ:]] "__fini_array_object_my_destructor_{{[a-f0-9]+}}_65535"
; CHECK: %[[#INIT_KERNEL]] = OpFunction
; CHECK: %{{[0-9]+}} = OpBitcast %{{[0-9]+}} %[[#INIT_START]]
; CHECK: %{{[0-9]+}} = OpBitcast %{{[0-9]+}} %[[#INIT_END]]
; CHECK: OpFunctionEnd
; CHECK: %[[#FINI_KERNEL]] = OpFunction
; CHECK: %{{[0-9]+}} = OpBitcast %{{[0-9]+}} %[[#FINI_START]]
; CHECK: %{{[0-9]+}} = OpBitcast %{{[0-9]+}} %[[#FINI_END]]
; CHECK: OpFunctionEnd

View File

@ -0,0 +1,36 @@
; Test to ensure the SPIR-V constructor/destructor lowering pass is not run
; when OpenMP offload metadata is absent.
; RUN: llc -mtriple=spirv64-intel-unknown --spirv-ext=+SPV_INTEL_function_pointers %s -o /dev/null -print-after=spirv-lower-ctor-dtor 2>&1 | FileCheck %s
define void @my_ctor() addrspace(9) {
entry:
ret void
}
define void @my_dtor() addrspace(9) {
entry:
ret void
}
@llvm.global_ctors = appending global [1 x { i32, ptr addrspace(9), ptr addrspace(9) }] [
{ i32, ptr addrspace(9), ptr addrspace(9) } { i32 100, ptr addrspace(9) @my_ctor, ptr addrspace(9) null }
]
@llvm.global_dtors = appending global [1 x { i32, ptr addrspace(9), ptr addrspace(9) }] [
{ i32, ptr addrspace(9), ptr addrspace(9) } { i32 100, ptr addrspace(9) @my_dtor, ptr addrspace(9) null }
]
; Verify that NO init/fini kernels or symbols are created
; CHECK-NOT: @__init_array_object_high_priority_{{[a-f0-9]+}}_100
; CHECK-NOT: @__init_array_object_low_priority_{{[a-f0-9]+}}_50
; CHECK-NOT: @__init_array_start = weak protected addrspace(1)
; CHECK-NOT: @__init_array_end = weak protected addrspace(1)
; CHECK-NOT: @__fini_array_object_high_priority_{{[a-f0-9]+}}_100
; CHECK-NOT: @__fini_array_object_low_priority_{{[a-f0-9]+}}_50
; CHECK-NOT: @__fini_array_start = weak protected addrspace(1)
; CHECK-NOT: @__fini_array_end = weak protected addrspace(1)
; CHECK-NOT: define weak_odr spir_kernel void @"spirv$device$init"()
; CHECK-NOT: define weak_odr spir_kernel void @"spirv$device$fini"()

View File

@ -34,6 +34,7 @@
; SPIRV-O0-NEXT: Expand variadic functions
; SPIRV-O0-NEXT: FunctionPass Manager
; SPIRV-O0-NEXT: SPIR-V Regularizer
; SPIRV-O0-NEXT: SPIRV lower ctors and dtors
; SPIRV-O0-NEXT: SPIRV prepare functions
; SPIRV-O0-NEXT: SPIRV prepare global variables
; SPIRV-O0-NEXT: FunctionPass Manager
@ -140,6 +141,7 @@
; SPIRV-Opt-NEXT: Expand variadic functions
; SPIRV-Opt-NEXT: FunctionPass Manager
; SPIRV-Opt-NEXT: SPIR-V Regularizer
; SPIRV-Opt-NEXT: SPIRV lower ctors and dtors
; SPIRV-Opt-NEXT: SPIRV prepare functions
; SPIRV-Opt-NEXT: SPIRV prepare global variables
; SPIRV-Opt-NEXT: FunctionPass Manager