Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions .github/workflows/build_linux_i686.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
name: Linux / CPU i686 on GH tests

permissions:
contents: read

on:
push:
branches:
- 'main'
- 'release*'
workflow_dispatch:
pull_request:
paths-ignore:
- 'doc/**'
- 'CHANGES'
- 'COPYING'
- 'CREDITS'
- 'LICENSE'
- 'README.*'
- 'tools/docker/**'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref || github.run_id }}
cancel-in-progress: true

jobs:
# Builds and tests 32-bit x86 PoCL in an i386 Debian container; setarch
# makes uname report i686, which CMake uses as the system processor.
# Debian testing because its LLVM is new enough for the CPU JIT on i386
# (JITLink supports R_386_GOT32X since LLVM 21).
i686_test:
name: LLVM ${{ matrix.llvm }} - i686
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
llvm: [21]

steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1

- name: Build and test PoCL
timeout-minutes: 120
env:
LLVM_VERSION: ${{ matrix.llvm }}
# Fail on i686 independently of this job: printf of LONG_MIN,
# signal handlers (SIGFPE avoidance is x86-64 only), allocations
# beyond the 32-bit address space and image conversion rounding.
KNOWN_FAILURES: "kernel/test_printf_(loopvec|cbs)$|regression/test_issue_2005|regression/test_signal_handlers|runtime/test_image_normalized_conv"
run: |
cat > ${{ runner.temp }}/build.sh <<'EOF'
set -ex
apt-get update -y
apt-get install -y --no-install-recommends \
build-essential cmake ninja-build pkg-config python3 zlib1g-dev \
libhwloc-dev clang-$LLVM_VERSION llvm-$LLVM_VERSION-dev \
libclang-$LLVM_VERSION-dev libclang-cpp$LLVM_VERSION-dev \
libpolly-$LLVM_VERSION-dev libllvmspirvlib-$LLVM_VERSION-dev \
llvm-spirv-$LLVM_VERSION
# Debian's i386 baseline has no SSE, which the FTZ handling
# requires (#2097).
cmake -S /pocl -B /build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DCMAKE_C_FLAGS=-msse2 -DCMAKE_CXX_FLAGS=-msse2 \
-DWITH_LLVM_CONFIG=/usr/bin/llvm-config-$LLVM_VERSION \
-DENABLE_ICD=OFF -DKERNELLIB_HOST_CPU_VARIANTS=distro
grep -q "define HOST_CPU_ENABLE_SPIRV" /build/config.h
ninja -C /build
cd /build
export POCL_CACHE_DIR=/tmp/pocl_cache
/pocl/tools/scripts/run_cpu_tests -j$(nproc) --output-on-failure \
-E "$KNOWN_FAILURES"
EOF
docker run --rm --platform linux/386 \
-v ${{ github.workspace }}:/pocl -v ${{ runner.temp }}:/scripts \
-e LLVM_VERSION -e KNOWN_FAILURES \
i386/debian:testing setarch i686 bash /scripts/build.sh
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,8 @@ file(RELATIVE_PATH POCL_INSTALL_FROM_LIB_TO_PRIVATE_LIBDIR
######################################################################################

set(HAVE_LIBSVML 0)
if(X86)
# oneAPI only ships SVML for x86-64; linking it into 32-bit kernels crashes them
if(X86_64)
set(ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
# use static libraries, so that the poclbinary is independent of system
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
Expand Down Expand Up @@ -1323,12 +1324,14 @@ endif()
find_package(Python3 3.8 COMPONENTS Interpreter)

# Determine whether we can use SPIRV on the host CPU device
# - we don't have a useful SPIRV wrapper for 32bit SPIRV
# - of the 32bit targets, the SPIRV wrapper generator only knows
# the ABI of i386 System V (not ARM32, RISC-V32 or 32bit Windows)
# - on Windows/MSVC there's a mangling mismatch between
# Clang (compiling the kernellib with MSVC function names)
# and the SPIRV wrapper (only supporting Itanium mangling)
set(HOST_CPU_ENABLE_SPIRV OFF)
if(ENABLE_HOST_CPU_DEVICES AND ENABLE_SPIRV AND (NOT MSVC) AND (HOST_DEVICE_ADDRESS_BITS MATCHES "64"))
if(ENABLE_HOST_CPU_DEVICES AND ENABLE_SPIRV AND (NOT MSVC) AND
((HOST_DEVICE_ADDRESS_BITS MATCHES "64") OR (I386 AND NOT WIN32)))
set(HOST_CPU_ENABLE_SPIRV ON)
endif()

Expand Down
3 changes: 3 additions & 0 deletions cmake/bitcode_rules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ function(generate_cpu_spir_wrapper ARCH SUBDIR SIZE OUTPUT)
if(ARCH STREQUAL "cpu_x86" AND LLC_TRIPLE MATCHES "-windows-gnu")
list(APPEND EXTRA_OPT "--mingw")
endif()
if(ARCH STREQUAL "cpu_x86")
list(APPEND EXTRA_OPT "--address-bits" "${HOST_DEVICE_ADDRESS_BITS}")
endif()

add_custom_command( OUTPUT "${FNAME}"
DEPENDS "${CMAKE_SOURCE_DIR}/lib/kernel/SPIR/generate_spir_wrapper.py"
Expand Down
9 changes: 9 additions & 0 deletions doc/sphinx/source/notes_7_3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ CPU driver
* Fixed use of an uninitialised kernel-library fallback name when
`LLC_HOST_CPU` is set.

* SPIR-V is now supported on 32-bit x86 (i386 System V, e.g. i686 Linux),
using a generated SPIR builtin wrapper instead of the old scalar-only one.
Other 32-bit hosts still have SPIR-V disabled.

* Struct types in SPIR-V modules keep the OpenCL C layout (8-byte aligned
`long` and `double`) on hosts whose ABI aligns these types less, such as
i386. Previously, field offsets, struct sizes and array strides silently
followed the host layout.

===================================
Deprecation/feature removal notices
===================================
Expand Down
Binary file added examples/example1/example1.spirv32
Binary file not shown.
15 changes: 12 additions & 3 deletions lib/CL/pocl_llvm_build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ using namespace llvm;
POP_COMPILER_DIAGS

#include "ProgramScopeVariables.h"
#include "SPIRStructLayout.h"
#include "linker.h"

//#define DEBUG_POCL_LLVM_API
Expand Down Expand Up @@ -884,10 +885,12 @@ int pocl_llvm_build_program(cl_program program,
}

/* converts a "spir-unknown-unknown" module triple to target (CPU/CUDA etc)
* triple, resets the datalayout to the target datalayout */
* triple, resets the datalayout to the target datalayout while keeping the
* memory layout of the SPIR struct types */
static int pocl_convert_spir_bitcode_to_target(llvm::Module *p,
llvm::Module *libmodule,
cl_device_id device) {
cl_device_id device,
std::string &Log) {

#if LLVM_MAJOR < 21
const std::string &ModTriple = p->getTargetTriple();
Expand All @@ -912,6 +915,9 @@ static int pocl_convert_spir_bitcode_to_target(llvm::Module *p,
device->llvm_target_triplet, ModTriple.c_str());
}

if (!pocl::preserveSPIRStructLayouts(*p, libmodule->getDataLayout(), Log))
return CL_LINK_PROGRAM_FAILURE;

/* Note this is a hack to get SPIR working. We'll be linking the
* host kernel library (plain LLVM IR) to the SPIR program.bc,
* so LLVM complains about incompatible DataLayouts.
Expand Down Expand Up @@ -986,8 +992,11 @@ int pocl_llvm_link_program(cl_program program, unsigned device_i,
}
}

std::string ConvertLog;
error = pocl_convert_spir_bitcode_to_target(TempModule.get(), LibraryModule,
device);
device, ConvertLog);
if (!ConvertLog.empty())
appendToProgramBuildLog(program, device_i, ConvertLog);
POCL_RETURN_ERROR_ON((error != CL_SUCCESS), CL_LINK_PROGRAM_FAILURE,
"could not convert SPIR to Target\n");

Expand Down
63 changes: 42 additions & 21 deletions lib/kernel/SPIR/generate_spir_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#
# output is LLVM IR text format.
#
# Usage: python3 generate_spir_wrapper.py [-q] [-g] [-m] [-t <target>] [-r <register_size>] OUTPUT
# Usage: python3 generate_spir_wrapper.py [-q] [-g] [-m] [-t <target>] [-r <register_size>]
# [-b <address_bits>] OUTPUT
# ... place the file in the target-specific lib/kernel subdirectory.
#
# Notes for CPU SPIR wrapper:
Expand All @@ -26,6 +27,9 @@
# 4) the above applies to the System V and AArch64/RISC-V ABIs. On x86-64 MinGW,
# clang passes and returns all vectors directly (the backend lowers the large
# ones itself), so the wrappers must not do any of this; see --mingw.
# 5) on 32-bit x86 (i386 System V), clang passes and returns all vectors directly,
# except that 64-bit integer vector arguments are coerced to i64. This does not
# depend on the available vector registers, so the register size is ignored.
#
# Notes for CUDA SPIR wrapper:
# 1) mangling is not required for CUDA
Expand All @@ -49,6 +53,11 @@
default=128, type=int, choices=[128,256,512],
help='largest register size (cpu target only)')

parser.add_argument('-b', '--address-bits',
dest='address_bits',
default=64, type=int, choices=[32,64],
help='device address bits, which determine size_t and the SPIR triple')

parser.add_argument('output',
type=argparse.FileType('w', encoding='utf-8'),
help='output file')
Expand All @@ -72,6 +81,8 @@

if args.mingw and args.target != 'cpu_x86':
parser.error('--mingw is only supported for the cpu_x86 target')
if args.address_bits == 32 and (args.target != 'cpu_x86' or args.mingw):
parser.error('32 address bits are only supported for the cpu_x86 target, without --mingw')

# function prefix used by PoCL's kernel library
POCL_LIB_PREFIX = "_cl_"
Expand All @@ -86,7 +97,8 @@
# if set to True, does target specific hacks like
# argument coercing (2xfloat -> double)
# use of byval/sret for args that don't fit into registers
X86_CALLING_ABI = (args.target == 'cpu_x86')
X86_CALLING_ABI = (args.target == 'cpu_x86') and args.address_bits == 64
I386_CALLING_ABI = (args.target == 'cpu_x86') and args.address_bits == 32
ARM_CALLING_ABI = (args.target == 'cpu_arm')
RISCV64_CALLING_ABI = (args.target == 'cpu_riscv')
SPIR_CALLING_ABI = (args.target == 'cuda')
Expand Down Expand Up @@ -328,6 +340,12 @@

"<8 x i8>": "double",
}
elif I386_CALLING_ABI:
COERCE_VECTOR_MAP = {
"<8 x i8>": "i64",
"<4 x i16>": "i64",
"<2 x i32>": "i64",
}
elif ARM_CALLING_ABI:
COERCE_VECTOR_MAP = {
"<2 x half>": "i32",
Expand Down Expand Up @@ -394,7 +412,11 @@
}


if CPU_ABI_REG_SIZE == 128:
if I386_CALLING_ABI:
BYVAL_VECTOR_MAP = {}
SRET_VECTOR_MAP = {}

elif CPU_ABI_REG_SIZE == 128:
BYVAL_VECTOR_MAP = {
"<3 x double>":"ptr noundef byval(<3 x double>) align 32",
"<3 x i64>":"ptr noundef byval(<3 x i64>) align 32",
Expand Down Expand Up @@ -815,8 +837,8 @@ def generate_function(name, ret_type, ret_type_ext, multiAS, *args):
# args without qualifiers, saved for mangling name compression
saved_pure_args = []

# ARM does not coerce return types, x86-64 & RISC-V does
if ARM_CALLING_ABI:
# ARM and i386 do not coerce return types, x86-64 & RISC-V does
if ARM_CALLING_ABI or I386_CALLING_ABI:
coerced_ret_type = ret_type
else:
coerced_ret_type = coerce_llvm_vector_type(ret_type)
Expand Down Expand Up @@ -1072,11 +1094,10 @@ def generate_function(name, ret_type, ret_type_ext, multiAS, *args):
}


triple = SPIR_MODULE_PREFIX[64]
if (len(sys.argv) > 1) and (sys.argv[1] == "32"):
triple = SPIR_MODULE_PREFIX[32]
print(SPIR_MODULE_PREFIX[args.address_bits])

print(triple)
# mangled name of size_t
SIZE_T = 'j' if args.address_bits == 32 else 'm'

MANG_TYPES = {
"half": {
Expand Down Expand Up @@ -1287,28 +1308,28 @@ def generate_function(name, ret_type, ret_type_ext, multiAS, *args):
arg = "Dv" + s + "_" + arg_type

if vector_size > 1:
generate_function("vload"+str(vector_size), SIG_TO_LLVM_TYPE_MAP[arg], '', True, 'm', PConstArg)
generate_function("vstore"+str(vector_size), SIG_TO_LLVM_TYPE_MAP['v'], '', True, arg, 'm', PArg)
generate_function("vload"+str(vector_size), SIG_TO_LLVM_TYPE_MAP[arg], '', True, SIZE_T, PConstArg)
generate_function("vstore"+str(vector_size), SIG_TO_LLVM_TYPE_MAP['v'], '', True, arg, SIZE_T, PArg)

if vector_size > 1:
PConstArg = 'PK' + arg
PArg = 'P' + arg

generate_function("prefetch", SIG_TO_LLVM_TYPE_MAP['v'], '', True, PConstArg, 'm')
generate_function("prefetch", SIG_TO_LLVM_TYPE_MAP['v'], '', True, PConstArg, SIZE_T)

generate_function("async_work_group_copy", 'ptr', '',
("global", "local", "none", "none", "none"),
PArg, PConstArg, 'm', '9ocl_event')
PArg, PConstArg, SIZE_T, '9ocl_event')
generate_function("async_work_group_copy", 'ptr', '',
("local", "global", "none", "none", "none"),
PArg, PConstArg, 'm', '9ocl_event')
PArg, PConstArg, SIZE_T, '9ocl_event')

generate_function("async_work_group_strided_copy", 'ptr', '',
("global", "local", "none", "none", "none"),
PArg, PConstArg, 'm', 'm', '9ocl_event')
PArg, PConstArg, SIZE_T, SIZE_T, '9ocl_event')
generate_function("async_work_group_strided_copy", 'ptr', '',
("local", "global", "none", "none", "none"),
PArg, PConstArg, 'm', 'm', '9ocl_event')
PArg, PConstArg, SIZE_T, SIZE_T, '9ocl_event')


# vload_half / vstore_half
Expand All @@ -1324,11 +1345,11 @@ def generate_function(name, ret_type, ret_type_ext, multiAS, *args):
suffix = 'half' + s
ret = "Dv" + s + "_" + ret
if ret_type == 'f':
generate_function("vload_"+suffix, SIG_TO_LLVM_TYPE_MAP[ret], '', True, 'm', PConstArg)
generate_function("vloada_"+suffix, SIG_TO_LLVM_TYPE_MAP[ret], '', True, 'm', PConstArg)
generate_function("vload_"+suffix, SIG_TO_LLVM_TYPE_MAP[ret], '', True, SIZE_T, PConstArg)
generate_function("vloada_"+suffix, SIG_TO_LLVM_TYPE_MAP[ret], '', True, SIZE_T, PConstArg)
for rounding in ['', '_rte', '_rtn', '_rtp', '_rtz']:
generate_function("vstore_"+suffix+rounding, SIG_TO_LLVM_TYPE_MAP['v'], '', True, ret, 'm', PArg)
generate_function("vstorea_"+suffix+rounding, SIG_TO_LLVM_TYPE_MAP['v'], '', True, ret, 'm', PArg)
generate_function("vstore_"+suffix+rounding, SIG_TO_LLVM_TYPE_MAP['v'], '', True, ret, SIZE_T, PArg)
generate_function("vstorea_"+suffix+rounding, SIG_TO_LLVM_TYPE_MAP['v'], '', True, ret, SIZE_T, PArg)

# Integer
for arg_type in IGENTYPES:
Expand Down Expand Up @@ -1460,7 +1481,7 @@ def generate_function(name, ret_type, ret_type_ext, multiAS, *args):
generate_function('get_image_depth', 'i32', '', True, img_type_llvm)
generate_function('get_image_dim', '<4 x i32>', '', True, img_type_llvm)
if img_type in ['image2d_array', 'image2d_array_depth', 'image1d_array']:
generate_function('get_image_array_size', 'i64', '', True, img_type_llvm)
generate_function('get_image_array_size', SIG_TO_LLVM_TYPE_MAP[SIZE_T], '', True, img_type_llvm)


# Atomics
Expand Down
7 changes: 6 additions & 1 deletion lib/kernel/host/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,12 @@ function(compile_sleef VARIANT SLEEF_CONFIG SLEEF_CONFIG_NEW SLEEF_BC)
# the "runtime/clGetKernelArgInfo" requires working images with spir,
# that only works if we're using the new (generated) wrappers
set(HAVE_WORKING_IMAGES_WITH_SPIR OFF CACHE BOOL "image functions work with SPIR" FORCE)
if(HOST_DEVICE_ADDRESS_BITS EQUAL 32)
if(I386)
# the i386 ABI passes vectors the same way regardless of the vector
# registers, so the 128bit wrapper works for every variant, including
# those without SSE2 that never reach the generation in the loop below
generate_cpu_spir_wrapper(cpu_x86 ${VARIANT} 128 SPIR_WRAPPER_FILE)
elseif(HOST_DEVICE_ADDRESS_BITS EQUAL 32)
set(SPIR_WRAPPER_FILE "host/spir_wrapper32.ll")
else()
if(RISCV)
Expand Down
2 changes: 2 additions & 0 deletions lib/llvmopencl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ set(LLVMPASSES_SOURCES "AllocasToEntry.cc"
"RemoveBarrierCalls.h"
"SanitizeUBofDivRem.cc"
"SanitizeUBofDivRem.h"
"SPIRStructLayout.cc"
"SPIRStructLayout.h"
"SubCFGFormation.cc"
"SubCFGFormation.h"
"UnreachablesToReturns.cc"
Expand Down
Loading
Loading