You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This patch ensures that OpenCL program-scope variables in the global
address space have an appropriate null value by default, following
the OpenCL C Specification s6.7.1.
This patch is needed to support linked SPIR-V modules.
An OpenCL program-scope variable in the global address space such as
"int foo;" is represented as follows in LLVM IR:
@foo = common dso_local local_unnamed_addr addrspace(1) global i32 0, align 4
Given the above IR, the LLVM to SPIR-V translator produces an exported
OpVariable without the default zero initializer, because
LLVMToSPIRV::transValueWithoutDecoration knows that variables with
common linkage type are initialized to 0 and that they can be
represented in SPIR-V as uninitialized variables with the "Export"
linkage type as shown:
OpDecorate %foo LinkageAttributes "foo" Export
%foo = OpVariable %_ptr_CrossWorkgroup_uint CrossWorkgroup
When the SPIR-V linker is used to create a program, and not a library,
it removes the "Export" decoration.
When translating back to LLVM, the SPIR-V reader knows in
SPIRVToLLVM::transLinkageType that an exported OpVariable with no
initializer corresponds to a global LLVM variable with common linkage
type and that, therefore, its initializer is zero (as per the LLVM IR
specification).
However, after linking, the variable no longer has the export decoration
and SPIRVToLLVM::transLinkageType assumes it has an internal linkage
type and prior to this patch SPIRVToLLVM::transValueWithoutDecoration no
longer knew its default value. This resulted in an invalid LLVM module:
@foo = internal addrspace(1) global i32, align 4
; missing initializer ^
Actually, llvm::verifyModule complains that:
Global is external, but doesn't have external or weak linkage!
i32 addrspace(1)* @foo
Side note: this message might seem a bit confusing at first. The
verifier thinks that this LLVM global variable is external because it is
a declaration (as it has no initializer). It has internal linkage type,
which is not valid for declarations.
This patch recovers the lost information, by ensuring that program-scope
variables in the global address space have a default value of zero and,
therefore, ensures that the translation from LLVM to SPIR-V and back to
LLVM is consistent even when the SPIR-V linker is involved.
Co-authored-by: Sven van Haastregt <sven.vanhaastregt@arm.com>
0 commit comments