Skip to content

[SYCL][libclc] Remangler tool for consistent mangling #4207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 13, 2021
Merged
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
4 changes: 4 additions & 0 deletions buildbot/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def do_configure(args):
llvm_targets_to_build = 'X86'
llvm_enable_projects = 'clang;' + llvm_external_projects
libclc_targets_to_build = ''
libclc_gen_remangled_variants = 'OFF'
sycl_build_pi_cuda = 'OFF'
sycl_build_pi_esimd_cpu = 'ON'
sycl_build_pi_rocm = 'OFF'
Expand All @@ -53,6 +54,7 @@ def do_configure(args):
if args.cuda:
llvm_targets_to_build += ';NVPTX'
libclc_targets_to_build = 'nvptx64--;nvptx64--nvidiacl'
libclc_gen_remangled_variants = 'ON'
sycl_build_pi_cuda = 'ON'

if args.rocm:
Expand All @@ -62,6 +64,7 @@ def do_configure(args):
elif args.rocm_platform == 'NVIDIA' and not args.cuda:
llvm_targets_to_build += ';NVPTX'
libclc_targets_to_build += ';nvptx64--;nvptx64--nvidiacl'
libclc_gen_remangled_variants = 'ON'

sycl_build_pi_rocm_platform = args.rocm_platform
sycl_build_pi_rocm = 'ON'
Expand Down Expand Up @@ -99,6 +102,7 @@ def do_configure(args):
"-DLLVM_EXTERNAL_LIBDEVICE_SOURCE_DIR={}".format(libdevice_dir),
"-DLLVM_ENABLE_PROJECTS={}".format(llvm_enable_projects),
"-DLIBCLC_TARGETS_TO_BUILD={}".format(libclc_targets_to_build),
"-DLIBCLC_GENERATE_REMANGLED_VARIANTS={}".format(libclc_gen_remangled_variants),
"-DSYCL_BUILD_PI_CUDA={}".format(sycl_build_pi_cuda),
"-DSYCL_BUILD_PI_ROCM={}".format(sycl_build_pi_rocm),
"-DSYCL_BUILD_PI_ROCM_PLATFORM={}".format(sycl_build_pi_rocm_platform),
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Driver/ToolChains/Cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,8 @@ void CudaToolChain::addClangTargetOptions(
llvm::sys::path::append(WithInstallPath, Twine("../../../share/clc"));
LibraryPaths.emplace_back(WithInstallPath.c_str());

std::string LibSpirvTargetName = "libspirv-nvptx64--nvidiacl.bc";
std::string LibSpirvTargetName =
"remangled-l64-signed_char.libspirv-nvptx64--nvidiacl.bc";
for (StringRef LibraryPath : LibraryPaths) {
SmallString<128> LibSpirvTargetFile(LibraryPath);
llvm::sys::path::append(LibSpirvTargetFile, LibSpirvTargetName);
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Driver/ToolChains/HIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ void HIPToolChain::addClangTargetOptions(
llvm::sys::path::append(WithInstallPath, Twine("../../../share/clc"));
LibraryPaths.emplace_back(WithInstallPath.c_str());

std::string LibSpirvTargetName = "libspirv-amdgcn--amdhsa.bc";
std::string LibSpirvTargetName =
"remangled-l64-signed_char.libspirv-amdgcn--amdhsa.bc";
for (StringRef LibraryPath : LibraryPaths) {
SmallString<128> LibSpirvTargetFile(LibraryPath);
llvm::sys::path::append(LibSpirvTargetFile, LibSpirvTargetName);
Expand Down
16 changes: 7 additions & 9 deletions clang/lib/Sema/SPIRVBuiltins.td
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,8 @@ class ConstOCLSPVBuiltin<string _Name, list<Type> _Signature> :

// OpenCL v1.0/1.2/2.0 s6.1.1: Built-in Scalar Data Types.
def Bool : IntType<"bool", QualType<"Context.BoolTy">, 1>;
def TrueChar : IntType<"_char", QualType<"Context.CharTy", 0, 1>, 8>;
def Char : IntType<"char", QualType<"Context.SignedCharTy", 0, 1>, 8>;
def SChar : IntType<"schar", QualType<"Context.SignedCharTy", 0, 1>, 8>;
def Char : IntType<"char", QualType<"Context.CharTy", 0, 1>, 8>;
def SChar : IntType<"schar", QualType<"Context.SignedCharTy", 0, 1>, 8>;
def UChar : UIntType<"uchar", QualType<"Context.UnsignedCharTy">, 8>;
def Short : IntType<"short", QualType<"Context.ShortTy", 0, 1>, 16>;
def UShort : UIntType<"ushort", QualType<"Context.UnsignedShortTy">, 16>;
Expand Down Expand Up @@ -356,15 +355,13 @@ def Vec16 : IntList<"Vec16", [16]>;
def Vec1234 : IntList<"Vec1234", [1, 2, 3, 4]>;

// Type lists.
def TLAll : TypeList<[Char, UChar, Short, UShort, Int, UInt, Long, ULong, Float, Double, Half]>;
def TLAll : TypeList<[Char, UChar, Short, UShort, Int, UInt, Long, ULong, Float, Double, Half]>;
def TLAllUnsigned : TypeList<[UChar, UChar, UShort, UShort, UInt, UInt, ULong, ULong, UInt, ULong, UShort]>;
def TLFloat : TypeList<[Float, Double, Half]>;
// FIXME: handle properly char (signed or unsigned depending on host)
def TLSignedInts : TypeList<[Char, Short, Int, Long]>;
def TLUnsignedInts : TypeList<[UChar, UShort, UInt, ULong]>;

// Signed to Unsigned conversion
// FIXME: handle properly char (signed or unsigned depending on host)
def TLSToUSignedInts : TypeList<[Char, Short, Int, Long]>;
def TLSToUUnsignedInts : TypeList<[UChar, UShort, UInt, ULong]>;

Expand All @@ -375,7 +372,7 @@ def TLIntLongFloats : TypeList<[Int, UInt, Long, ULong, Float, Double, Half]>;
// uchar abs(uchar).
def TLAllUIntsTwice : TypeList<[UChar, UChar, UChar, UShort, UShort, UInt, UInt, ULong, ULong]>;

def TLAllInts : TypeList<[Char, UChar, Short, UShort, Int, UInt, Long, ULong]>;
def TLAllInts : TypeList<[Char, SChar, UChar, Short, UShort, Int, UInt, Long, ULong]>;

// GenType definitions for multiple base types (e.g. all floating point types,
// or all integer types).
Expand All @@ -402,7 +399,7 @@ def IntLongFloatGenType1 : GenericType<"IntLongFloatGenType1", TLIntLongFloats

// GenType definitions for every single base type (e.g. fp32 only).
// Names are like: GenTypeFloatVecAndScalar.
foreach Type = [Char, UChar, SChar, Short, UShort,
foreach Type = [Char, SChar, UChar, Short, UShort,
Int, UInt, Long, ULong,
Float, Double, Half] in {
foreach VecSizes = [VecAndScalar, VecNoScalar] in {
Expand Down Expand Up @@ -557,6 +554,7 @@ foreach name = ["s_clamp", "s_mad_hi", "s_mad_sat"] in {

foreach name = ["s_upsample"] in {
def : ConstOCLSPVBuiltin<name, [GenTypeShortVecAndScalar, GenTypeCharVecAndScalar, GenTypeUCharVecAndScalar]>;
def : ConstOCLSPVBuiltin<name, [GenTypeShortVecAndScalar, GenTypeSCharVecAndScalar, GenTypeUCharVecAndScalar]>;
def : ConstOCLSPVBuiltin<name, [GenTypeIntVecAndScalar, GenTypeShortVecAndScalar, GenTypeUShortVecAndScalar]>;
def : ConstOCLSPVBuiltin<name, [GenTypeLongVecAndScalar, GenTypeIntVecAndScalar, GenTypeUIntVecAndScalar]>;
}
Expand Down Expand Up @@ -724,7 +722,7 @@ foreach VSize1 = [Vec2, Vec4, Vec8, Vec16] in {

let IsVariadic = 1 in {
foreach name = ["printf"] in {
def : OCLSPVBuiltin<name, [Int, PointerType<ConstType<TrueChar>, ConstantAS>]>;
def : OCLSPVBuiltin<name, [Int, PointerType<ConstType<Char>, ConstantAS>]>;
}
}

Expand Down
10 changes: 9 additions & 1 deletion libclc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ set( LIBCLC_TARGETS_TO_BUILD "all"
option( ENABLE_RUNTIME_SUBNORMAL "Enable runtime linking of subnormal support."
OFF )

option( LIBCLC_GENERATE_REMANGLED_VARIANTS
"Generate remangled variants of enabled libclc targets."
OFF )

# mesa3d environment is only available since LLVM 4.0
if( ${LLVM_VERSION} VERSION_GREATER "3.9.0" )
set( LIBCLC_TARGETS_ALL ${LIBCLC_TARGETS_ALL} amdgcn-mesa-mesa3d )
Expand Down Expand Up @@ -119,15 +123,19 @@ find_program( LLVM_AS llvm-as PATHS ${LLVM_BINDIR} NO_DEFAULT_PATH )
find_program( LLVM_LINK llvm-link PATHS ${LLVM_BINDIR} NO_DEFAULT_PATH )
find_program( LLVM_OPT opt PATHS ${LLVM_BINDIR} NO_DEFAULT_PATH )
find_program( LLVM_SPIRV llvm-spirv PATHS ${LLVM_BINDIR} NO_DEFAULT_PATH )
find_program( LIBCLC_REMANGLER libclc-remangler PATHS ${LLVM_BINDIR}
NO_DEFAULT_PATH )

# Print toolchain
message( "clang: ${LLVM_CLANG}" )
message( "llvm-as: ${LLVM_AS}" )
message( "llvm-link: ${LLVM_LINK}" )
message( "opt: ${LLVM_OPT}" )
message( "llvm-spirv: ${LLVM_SPIRV}" )
message( "libclc-remangler: ${LIBCLC_REMANGLER}" )
message( "" )
if( NOT LLVM_CLANG OR NOT LLVM_OPT OR NOT LLVM_AS OR NOT LLVM_LINK )
if( NOT LLVM_CLANG OR NOT LLVM_OPT OR NOT LLVM_AS OR NOT LLVM_LINK
OR NOT LIBCLC_REMANGLER )
message( FATAL_ERROR "toolchain incomplete!" )
endif()

Expand Down
34 changes: 34 additions & 0 deletions libclc/cmake/modules/AddLibclc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,40 @@ macro(add_libclc_builtin_set arch_suffix)
install(
FILES ${LIBCLC_LIBRARY_OUTPUT_INTDIR}/${obj_suffix}
DESTINATION ${CMAKE_INSTALL_DATADIR}/clc )

# Generate remangled variants if requested
if( LIBCLC_GENERATE_REMANGLED_VARIANTS )
set(long_widths l32 l64)
set(char_signedness signed unsigned)
# All permutations of [l32, l64] and [signed, unsigned]
foreach(long_width ${long_widths})
foreach(signedness ${char_signedness})
# Remangle
set( builtins_remangle_path
"${LIBCLC_LIBRARY_OUTPUT_INTDIR}/remangled-${long_width}-${signedness}_char.${obj_suffix}" )
add_custom_command( OUTPUT "${builtins_remangle_path}"
COMMAND libclc-remangler
-o "${builtins_remangle_path}"
--long-width=${long_width}
--char-signedness=${signedness}
"$<TARGET_PROPERTY:prepare-${obj_suffix},TARGET_FILE>"
DEPENDS "prepare-${obj_suffix}" libclc-remangler )
add_custom_target( "remangled-${long_width}-${signedness}_char.${obj_suffix}" ALL
DEPENDS "${builtins_remangle_path}" )
set_target_properties("remangled-${long_width}-${signedness}_char.${obj_suffix}"
PROPERTIES TARGET_FILE "${builtins_remangle_path}")

# Add dependency to top-level pseudo target to ease making other
# targets dependent on libclc.
add_dependencies(${ARG_PARENT_TARGET} "remangled-${long_width}-${signedness}_char.${obj_suffix}")

# Keep remangled variants
install(
FILES ${builtins_remangle_path}
DESTINATION ${CMAKE_INSTALL_DATADIR}/clc )
endforeach()
endforeach()
endif()

# nvptx-- targets don't include workitem builtins
if( NOT ${t} MATCHES ".*ptx.*--$" )
Expand Down
4 changes: 4 additions & 0 deletions libclc/cmake/modules/HandleInLLVMTree.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ macro(configure_in_llvm_tree)
set(LLVM_AS ${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-as)
set(LLVM_LINK ${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-link)
set(LLVM_OPT ${LLVM_RUNTIME_OUTPUT_INTDIR}/opt)
set(LIBCLC_REMANGLER ${LLVM_RUNTIME_OUTPUT_INTDIR}/libclc-remangler)

if (NOT EXISTS ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
file(WRITE ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang "" )
Expand All @@ -16,6 +17,9 @@ macro(configure_in_llvm_tree)
if (NOT EXISTS ${LLVM_RUNTIME_OUTPUT_INTDIR}/opt)
file(WRITE ${LLVM_RUNTIME_OUTPUT_INTDIR}/opt "" )
endif (NOT EXISTS ${LLVM_RUNTIME_OUTPUT_INTDIR}/opt)
if (NOT EXISTS ${LLVM_RUNTIME_OUTPUT_INTDIR}/libclc-remangler)
file(WRITE ${LLVM_RUNTIME_OUTPUT_INTDIR}/libclc-remangler "" )
endif (NOT EXISTS ${LLVM_RUNTIME_OUTPUT_INTDIR}/libclc-remangler)

# Assume all works well
# We can't test the compilers as they haven't been built yet
Expand Down
1 change: 1 addition & 0 deletions libclc/cmake/modules/HandleOutOfTreeLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ macro(configure_out_of_tree_llvm)
get_property(LLVM_AS TARGET llvm-as PROPERTY LOCATION)
get_property(LLVM_LINK TARGET llvm-link PROPERTY LOCATION)
get_property(LLVM_OPT TARGET opt PROPERTY LOCATION)
get_property(LIBCLC_REMANGLER TARGET libclc-remangler PROPERTY LOCATION)

set(LLVM_ENABLE_PIC OFF)

Expand Down
14 changes: 5 additions & 9 deletions libclc/generic/gen_convert_common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# This file contains common variables and helper functions used by the
# `gen_convert.py` in both the libclc and libspirv libraries.

types = ['char', 'schar', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong', 'half', 'float', 'double']
int_types = ['char', 'schar', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong']
types = ['char', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong', 'half', 'float', 'double']
int_types = ['char', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong']
unsigned_types = ['uchar', 'ushort', 'uint', 'ulong']
signed_types = ['char', 'schar', 'short', 'int', 'long']
signed_types = ['char', 'short', 'int', 'long']
float_types = ['half', 'float', 'double']
int64_types = ['long', 'ulong']
float64_types = ['double']
Expand All @@ -18,8 +18,7 @@
float_suffix = {'float':'f', 'double':''}

bool_type = {'char' : 'char',
'schar' : 'schar',
'uchar' : 'schar',
'uchar' : 'char',
'short' : 'short',
'ushort' : 'short',
'int' : 'int',
Expand All @@ -31,7 +30,6 @@
'double' : 'long'}

unsigned_type = {'char' : 'uchar',
'schar' : 'uchar',
'uchar' : 'uchar',
'short' : 'ushort',
'ushort': 'ushort',
Expand All @@ -40,15 +38,14 @@
'long' : 'ulong',
'ulong' : 'ulong'}

sizeof_type = {'char' : 1, 'schar' : 1, 'uchar' : 1,
sizeof_type = {'char' : 1, 'uchar' : 1,
'short' : 2, 'ushort' : 2,
'int' : 4, 'uint' : 4,
'long' : 8, 'ulong' : 8,
'half' : 2, 'float' : 4,
'double': 8}

limit_max = {'char' : 'CHAR_MAX',
'schar' : 'CHAR_MAX',
'uchar' : 'UCHAR_MAX',
'short' : 'SHRT_MAX',
'ushort': 'USHRT_MAX',
Expand All @@ -58,7 +55,6 @@
'ulong' : 'ULONG_MAX'}

limit_min = {'char' : 'CHAR_MIN',
'schar' : 'CHAR_MIN',
'uchar' : '0',
'short' : 'SHRT_MIN',
'ushort': '0',
Expand Down
6 changes: 0 additions & 6 deletions libclc/generic/include/as_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define CLC_AS_TYPE

#define as_char(x) __builtin_astype(x, char)
#define as_schar(x) __builtin_astype(x, schar)
#define as_uchar(x) __builtin_astype(x, uchar)
#define as_short(x) __builtin_astype(x, short)
#define as_ushort(x) __builtin_astype(x, ushort)
Expand All @@ -13,7 +12,6 @@
#define as_float(x) __builtin_astype(x, float)

#define as_char2(x) __builtin_astype(x, char2)
#define as_schar2(x) __builtin_astype(x, schar2)
#define as_uchar2(x) __builtin_astype(x, uchar2)
#define as_short2(x) __builtin_astype(x, short2)
#define as_ushort2(x) __builtin_astype(x, ushort2)
Expand All @@ -24,7 +22,6 @@
#define as_float2(x) __builtin_astype(x, float2)

#define as_char3(x) __builtin_astype(x, char3)
#define as_schar3(x) __builtin_astype(x, schar3)
#define as_uchar3(x) __builtin_astype(x, uchar3)
#define as_short3(x) __builtin_astype(x, short3)
#define as_ushort3(x) __builtin_astype(x, ushort3)
Expand All @@ -35,7 +32,6 @@
#define as_float3(x) __builtin_astype(x, float3)

#define as_char4(x) __builtin_astype(x, char4)
#define as_schar4(x) __builtin_astype(x, schar4)
#define as_uchar4(x) __builtin_astype(x, uchar4)
#define as_short4(x) __builtin_astype(x, short4)
#define as_ushort4(x) __builtin_astype(x, ushort4)
Expand All @@ -46,7 +42,6 @@
#define as_float4(x) __builtin_astype(x, float4)

#define as_char8(x) __builtin_astype(x, char8)
#define as_schar8(x) __builtin_astype(x, schar8)
#define as_uchar8(x) __builtin_astype(x, uchar8)
#define as_short8(x) __builtin_astype(x, short8)
#define as_ushort8(x) __builtin_astype(x, ushort8)
Expand All @@ -57,7 +52,6 @@
#define as_float8(x) __builtin_astype(x, float8)

#define as_char16(x) __builtin_astype(x, char16)
#define as_schar16(x) __builtin_astype(x, schar16)
#define as_uchar16(x) __builtin_astype(x, uchar16)
#define as_short16(x) __builtin_astype(x, short16)
#define as_ushort16(x) __builtin_astype(x, ushort16)
Expand Down
40 changes: 0 additions & 40 deletions libclc/generic/include/clc/async/gentype.inc
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,6 @@
#undef __CLC_GENTYPE_MANGLED
#undef __CLC_GENTYPE

#ifndef __CLC_NO_SCHAR
#define __CLC_GENTYPE schar
#define __CLC_GENTYPE_MANGLED a
#include __CLC_BODY
#undef __CLC_GENTYPE_MANGLED
#undef __CLC_GENTYPE

#define __CLC_GENTYPE schar2
#define __CLC_GENTYPE_MANGLED Dv2_a
#include __CLC_BODY
#undef __CLC_GENTYPE_MANGLED
#undef __CLC_GENTYPE

#ifdef __CLC_GEN_VEC3
#define __CLC_GENTYPE schar3
#define __CLC_GENTYPE_MANGLED Dv3_a
#include __CLC_BODY
#undef __CLC_GENTYPE_MANGLED
#undef __CLC_GENTYPE
#endif

#define __CLC_GENTYPE schar4
#define __CLC_GENTYPE_MANGLED Dv4_a
#include __CLC_BODY
#undef __CLC_GENTYPE_MANGLED
#undef __CLC_GENTYPE

#define __CLC_GENTYPE schar8
#define __CLC_GENTYPE_MANGLED Dv8_a
#include __CLC_BODY
#undef __CLC_GENTYPE_MANGLED
#undef __CLC_GENTYPE

#define __CLC_GENTYPE schar16
#define __CLC_GENTYPE_MANGLED Dv16_a
#include __CLC_BODY
#undef __CLC_GENTYPE_MANGLED
#undef __CLC_GENTYPE
#endif

#define __CLC_GENTYPE uchar
#define __CLC_GENTYPE_MANGLED h
#include __CLC_BODY
Expand Down
2 changes: 0 additions & 2 deletions libclc/generic/include/clc/clc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
#endif

#define __CLC_NO_SCHAR

/* Function Attributes */
#include <func.h>

Expand Down
Loading