Skip to content

Commit 964b639

Browse files
leizhenyuanEikanWangxiaolil1RUIJIEZHONG66166chuanqi129
authored
LLvm rebase (#65)
* Rebase to LLVM master - 57882fe76e1826593cd0e53f73484b184c5007c4 * add spirv ConvertUToPtrOp and ConvertptrToUOp * add support for external call * changes for std::optional * fix for precommit * intergrate with llvm * using own llvm * fix typo * for preci and auto download * add script for build llvm * add TRITON_INTEL_LLVM * fix precommit * use TRITON_CODEGEN_INTEL_XPU_BACKEND * change llvm version * change to ubuntu 22.04 * remove unuse envs --------- Co-authored-by: Wang, Eikan <[email protected]> Co-authored-by: xiaolil1 <[email protected]> Co-authored-by: Zhong, Ruijie <[email protected]> Co-authored-by: Wang, Chuanqi <[email protected]>
1 parent f3b6e72 commit 964b639

File tree

9 files changed

+208
-30
lines changed

9 files changed

+208
-30
lines changed

.github/scripts/build_llvm.sh

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
set -e -x
2+
git clone https://github.com/leizhenyuan/llvm-project.git -b xpu_llvm_rebase ./private-llvm
3+
4+
CURRENT_DIR="$(pwd)"
5+
6+
SOURCE_DIR=$CURRENT_DIR/private-llvm
7+
install_prefix=""
8+
platform=""
9+
build_config=""
10+
arch="x86"
11+
num_jobs=8
12+
13+
usage() {
14+
echo "Usage: bash build_llvm.bash -o INSTALL_PREFIX -p PLATFORM -c CONFIG [-a ARCH] [-j NUM_JOBS]"
15+
echo "Ex: bash build_llvm.bash -o llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04 -p docker_ubuntu-18.04 -c assert -j 16"
16+
echo "INSTALL_PREFIX = <string> # \${INSTALL_PREFIX}.tar.xz is created"
17+
echo "PLATFORM = {local|docker_ubuntu_18.04|docker_centos7}"
18+
echo "CONFIG = {release|assert|debug}"
19+
echo "ARCH = {x86|arm64}"
20+
echo "NUM_JOBS = {1|2|3|...}"
21+
exit 1;
22+
}
23+
24+
while getopts "o:p:c:a:j:l" arg; do
25+
case "$arg" in
26+
o)
27+
install_prefix="$OPTARG"
28+
;;
29+
p)
30+
platform="$OPTARG"
31+
;;
32+
c)
33+
build_config="$OPTARG"
34+
;;
35+
a)
36+
arch="$OPTARG"
37+
;;
38+
j)
39+
num_jobs="$OPTARG"
40+
;;
41+
*)
42+
usage
43+
;;
44+
45+
esac
46+
done
47+
48+
if [ x"$install_prefix" == x ] || [ x"$platform" == x ] || [ x"$build_config" == x ]; then
49+
usage
50+
fi
51+
52+
# Set up CMake configurations
53+
CMAKE_CONFIGS="-DLLVM_ENABLE_PROJECTS=mlir -DLLVM_USE_LINKER=gold -DLLVM_ENABLE_LTO=OFF"
54+
if [ x"$arch" == x"arm64" ]; then
55+
CMAKE_CONFIGS="${CMAKE_CONFIGS}"
56+
else
57+
CMAKE_CONFIGS="${CMAKE_CONFIGS} -DLLVM_TARGETS_TO_BUILD=X86;NVPTX;AMDGPU"
58+
fi
59+
60+
if [ x"$build_config" == x"release" ]; then
61+
CMAKE_CONFIGS="${CMAKE_CONFIGS} -DCMAKE_BUILD_TYPE=Release"
62+
elif [ x"$build_config" == x"assert" ]; then
63+
CMAKE_CONFIGS="${CMAKE_CONFIGS} -DCMAKE_BUILD_TYPE=MinSizeRel -DLLVM_ENABLE_ASSERTIONS=True"
64+
elif [ x"$build_config" == x"debug" ]; then
65+
CMAKE_CONFIGS="${CMAKE_CONFIGS} -DCMAKE_BUILD_TYPE=Debug"
66+
else
67+
usage
68+
fi
69+
70+
71+
# Create a temporary build directory
72+
BUILD_DIR="$(mktemp -d)"
73+
echo "Using a temporary directory for the build: $BUILD_DIR"
74+
rm -rf "$BUILD_DIR"
75+
mkdir -p "$BUILD_DIR"
76+
77+
78+
# Build LLVM locally
79+
pushd "$BUILD_DIR"
80+
echo ${CMAKE_CONFIGS}
81+
cmake "$SOURCE_DIR/llvm" -DCMAKE_INSTALL_PREFIX="$BUILD_DIR/$install_prefix" $CMAKE_CONFIGS
82+
make -j${num_jobs} install
83+
tar -cJf "${SOURCE_DIR}/${install_prefix}.tar.xz" "$install_prefix"
84+
popd
85+
86+
mv $SOURCE_DIR/$install_prefix.tar.xz $(pwd)
87+
# Remove the temporary directory
88+
rm -rf "$BUILD_DIR"
89+
90+
echo "Completed!"

.github/workflows/triton_xpu_backend_ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ jobs:
6262
run: |
6363
source ${HOME}/miniconda3/bin/activate triton-preci
6464
source ${HOME}/env_triton.sh
65-
export LLVM_SYSPATH=${HOME}/triton-preci/llvm/build/
6665
pip install pybind11
6766
pip uninstall -y triton
6867
cd triton/python

.github/workflows/triton_xpu_backend_e2e_ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ jobs:
5757
run: |
5858
source ${HOME}/miniconda3/bin/activate triton-preci
5959
source ${HOME}/env_triton.sh
60-
export LLVM_SYSPATH=${HOME}/triton-preci/llvm/build/
6160
pip uninstall -y triton
6261
sudo update-ca-certificates --fresh
6362
export SSL_CERT_DIR=/etc/ssl/certs

.github/workflows/triton_xpu_backend_e2e_nightly.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ jobs:
5454
run: |
5555
source ${HOME}/miniconda3/bin/activate triton-nightly-test
5656
source ${HOME}/env_triton.sh
57-
export LLVM_SYSPATH=${HOME}/triton-nightly/llvm/build/
5857
pip uninstall -y triton
5958
sudo update-ca-certificates --fresh
6059
export SSL_CERT_DIR=/etc/ssl/certs

.github/workflows/triton_xpu_backend_nightly.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ jobs:
5656
run: |
5757
source ${HOME}/miniconda3/bin/activate triton-nightly-test
5858
source ${HOME}/env_triton.sh
59-
export LLVM_SYSPATH=${HOME}/triton-nightly/llvm/build/
6059
pip install pybind11
6160
pip uninstall -y triton
6261
cd triton/python

CMakeLists.txt

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,101 @@
11

22
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
33

4+
if (DEFINED ENV{TRITON_INTEL_LLVM_DIR})
5+
MESSAGE(STATUS "[intel_xpu_backend]: using user provide llvm path")
6+
else ()
7+
8+
if (DEFINED ENV{TRITON_CODEGEN_INTEL_XPU_BACKEND})
9+
MESSAGE(STATUS "[intel_xpu_backend]: using intel_llvm")
10+
11+
SET(TRITON_INTEL_LLVM_DIR $ENV{HOME}/.triton/intel_llvm)
12+
MESSAGE(STATUS "[intel_xpu_backend]: set intel llvm path ${TRITON_INTEL_LLVM_DIR}")
13+
if (EXISTS ${TRITON_INTEL_LLVM_DIR})
14+
MESSAGE(STATUS "[intel_xpu_backend]: using previous exists llvm")
15+
else()
16+
include(FetchContent)
17+
function(download_file url)
18+
19+
FetchContent_Declare(intel-llvm
20+
URL ${url}
21+
SOURCE_DIR ${TRITON_INTEL_LLVM_DIR}
22+
)
23+
24+
FetchContent_Populate(intel-llvm)
25+
endfunction(download_file)
26+
27+
MESSAGE(STATUS "[intel_xpu_backend]: downloading intel llvm for intel_xpu_backend, path ${TRITON_INTEL_LLVM_DIR}")
28+
download_file(
29+
https://github.com/intel/intel-xpu-backend-for-triton/releases/download/llvm_pkg_v0.1/llvm+mlir-17.0.0-x86_64-linux-gnu-ubuntu-22.04-release.tar.xz
30+
)
31+
endif()
32+
MESSAGE(STATUS "[intel_xpu_backend]:Triton uses LLVM version ${LLVM_PACKAGE_VERSION} with revision ${LLVM_GIT_REVISION}")
33+
MESSAGE(STATUS "[intel_xpu_backend]:Use the LLVM version with revision to re-config for the intel_xpu_backend")
34+
35+
# hints from https://stackoverflow.com/questions/10205986/how-to-capture-cmake-command-line-arguments
36+
# Propagate the CMake vars to the intel xpu backend
37+
38+
set(CMAKE_ARGS "${CMAKE_ARGS};-DLLVM_DIR=${TRITON_INTEL_LLVM_DIR}")
39+
set(CMAKE_ARGS "${CMAKE_ARGS};-DLLVM_INCLUDE_DIRS=${TRITON_INTEL_LLVM_DIR}/include")
40+
set(CMAKE_ARGS "${CMAKE_ARGS};-DLLVM_LIBRARY_DIR=${TRITON_INTEL_LLVM_DIR}/lib")
41+
42+
43+
else ()
44+
MESSAGE(STATUS "[intel_xpu_backend]: using original llvm ")
45+
46+
set(XPU_BACKEND_RECURSIVE ON)
47+
endif()
48+
endif()
49+
50+
if(XPU_BACKEND_RECURSIVE)
51+
set(TRITON_BUILD_PYTHON_MODULE ON)
452
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
553
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include) # Tablegen'd files
654

755
add_subdirectory(include)
856
add_subdirectory(lib)
957
add_subdirectory(python)
58+
else()
59+
60+
61+
62+
get_cmake_property(CACHE_VARS CACHE_VARIABLES)
63+
foreach(CACHE_VAR ${CACHE_VARS})
64+
get_property(CACHE_VAR_HELPSTRING CACHE ${CACHE_VAR} PROPERTY HELPSTRING)
65+
get_property(CACHE_VAR_TYPE CACHE ${CACHE_VAR} PROPERTY TYPE)
66+
if(CACHE_VAR_TYPE STREQUAL "UNINITIALIZED")
67+
set(CACHE_VAR_TYPE)
68+
else()
69+
set(CACHE_VAR_TYPE :${CACHE_VAR_TYPE})
70+
endif()
71+
72+
if(CACHE_VAR STREQUAL "TRITON_BUILD_TUTORIALS")
73+
set(CMAKE_ARGS "${CMAKE_ARGS};-D${CACHE_VAR}${CACHE_VAR_TYPE}=OFF")
74+
elseif(CACHE_VAR STREQUAL "TRITON_BUILD_PYTHON_MODULE")
75+
set(CMAKE_ARGS "${CMAKE_ARGS};-D${CACHE_VAR}${CACHE_VAR_TYPE}=OFF")
76+
elseif(CACHE_VAR STREQUAL "LLVM_LIBRARY_DIR")
77+
continue()
78+
elseif(CACHE_VAR STREQUAL "LLVM_DIR")
79+
continue()
80+
elseif(CACHE_VAR STREQUAL "LLVM_INCLUDE_DIRS")
81+
continue()
82+
else()
83+
set(CMAKE_ARGS "${CMAKE_ARGS};-D${CACHE_VAR}${CACHE_VAR_TYPE}=${${CACHE_VAR}}")
84+
endif()
85+
endforeach()
86+
87+
execute_process(COMMAND ${CMAKE_COMMAND}
88+
${CMAKE_ARGS}
89+
-DXPU_BACKEND_RECURSIVE=ON
90+
-S ${PROJECT_SOURCE_DIR}
91+
-B ${CMAKE_CURRENT_BINARY_DIR}/triton)
92+
93+
add_custom_target(intel_xpu_backend_for_triton ALL
94+
COMMAND
95+
${CMAKE_COMMAND} --build . --config ${CMAKE_BUILD_TYPE} --target intel_xpu_backend_for_triton
96+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/triton
97+
COMMENT "Building intel_xpu_backend_for_triton"
98+
VERBATIM
99+
USES_TERMINAL)
100+
101+
endif()

lib/Conversion/TritonGPUToSPIRV/ElementwiseOpToSPIRV.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,11 @@ struct FpToFpOpSPIRVConversion
394394
NamedAttrList attributes(extraAttrs);
395395
attributes.set("libname", StringAttr::get(v.getContext(), libName));
396396
attributes.set("libpath", StringAttr::get(v.getContext(), ""));
397-
attributes.set("linkage_attributes",
398-
ArrayAttr::get(v.getContext(),
399-
{
400-
StringAttr::get(v.getContext(), funcName),
401-
StringAttr::get(v.getContext(), "Import"),
402-
}));
397+
auto linkageTypeAttr =
398+
b.getAttr<::mlir::spirv::LinkageTypeAttr>(spirv::LinkageType::Import);
399+
auto linkageAttr = b.getAttr<::mlir::spirv::LinkageAttributesAttr>(
400+
funcName.lower(), linkageTypeAttr);
401+
attributes.set("linkage_attributes", linkageAttr);
403402
auto ret =
404403
b.create<spirv::FuncOp>(v.getLoc(), funcName, funcType,
405404
spirv::FunctionControl::Inline, attributes);
@@ -655,13 +654,11 @@ struct ExternElementwiseSPIRVConversion
655654
"libname", StringAttr::get(op->getContext(), op.getLibname()));
656655
ret.getOperation()->setAttr(
657656
"libpath", StringAttr::get(op->getContext(), op.getLibpath()));
658-
ret.getOperation()->setAttr(
659-
"linkage_attributes",
660-
ArrayAttr::get(op->getContext(),
661-
{
662-
StringAttr::get(op->getContext(), funcName),
663-
StringAttr::get(op->getContext(), "Import"),
664-
}));
657+
auto linkageTypeAttr =
658+
b.getAttr<::mlir::spirv::LinkageTypeAttr>(spirv::LinkageType::Import);
659+
auto linkageAttr = b.getAttr<::mlir::spirv::LinkageAttributesAttr>(
660+
funcName.lower(), linkageTypeAttr);
661+
ret.getOperation()->setAttr("linkage_attributes", linkageAttr);
665662
return ret;
666663
}
667664
};
@@ -1019,8 +1016,8 @@ void populateElementwiseOpToSPIRVPatterns(
10191016
POPULATE_UNARY_OP(math::SqrtOp, math::SqrtOp)
10201017
POPULATE_UNARY_OP(math::ExpOp, math::ExpOp)
10211018
POPULATE_UNARY_OP(triton::BitcastOp, spirv::BitcastOp)
1022-
POPULATE_UNARY_OP(triton::IntToPtrOp, spirv::BitcastOp)
1023-
POPULATE_UNARY_OP(triton::PtrToIntOp, spirv::BitcastOp)
1019+
POPULATE_UNARY_OP(triton::IntToPtrOp, spirv::ConvertUToPtrOp)
1020+
POPULATE_UNARY_OP(triton::PtrToIntOp, spirv::ConvertPtrToUOp)
10241021
#undef POPULATE_UNARY_OP
10251022

10261023
patterns.add<CmpIOpSPIRVConversion>(typeConverter, context, benefit);

lib/Conversion/TritonGPUToSPIRV/TypeConverter.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ MAP_FN(spirv::StorageClass::StorageBuffer, 0) \
4747
MAP_FN(spirv::StorageClass::HostOnlyINTEL, 23)
4848
#endif
4949

50-
Optional<spirv::StorageClass> getStorageClassForMemorySpace(unsigned space) {
50+
std::optional<spirv::StorageClass>
51+
getStorageClassForMemorySpace(unsigned space) {
5152
#define STORAGE_SPACE_MAP_FN(storage, space) \
5253
case space: \
5354
return storage;
@@ -63,38 +64,38 @@ Optional<spirv::StorageClass> getStorageClassForMemorySpace(unsigned space) {
6364
TritonGPUToSPIRVTypeConverter::TritonGPUToSPIRVTypeConverter(
6465
spirv::TargetEnvAttr &targetAttr, SPIRVConversionOptions &option)
6566
: SPIRVTypeConverter(targetAttr, option) {
66-
addConversion([&](triton::PointerType type) -> llvm::Optional<Type> {
67+
addConversion([&](triton::PointerType type) -> std::optional<Type> {
6768
return convertTritonPointerType(type);
6869
});
69-
addConversion([&](RankedTensorType type) -> llvm::Optional<Type> {
70+
addConversion([&](RankedTensorType type) -> std::optional<Type> {
7071
return convertTritonTensorType(type);
7172
});
72-
addConversion([&](mlir::VectorType type) -> llvm::Optional<Type> {
73+
addConversion([&](mlir::VectorType type) -> std::optional<Type> {
7374
// Recursively translate vector type
7475
return mlir::VectorType::get(type.getShape(),
7576
convertType(type.getElementType()));
7677
});
7778
// Internally store float8 as int8
78-
addConversion([&](mlir::Float8E4M3FNType type) -> llvm::Optional<Type> {
79+
addConversion([&](mlir::Float8E4M3FNType type) -> std::optional<Type> {
7980
llvm::report_fatal_error("SPIRV doesn't support fp8 type");
8081
return IntegerType::get(type.getContext(), 8);
8182
});
82-
addConversion([&](mlir::Float8E5M2Type type) -> llvm::Optional<Type> {
83+
addConversion([&](mlir::Float8E5M2Type type) -> std::optional<Type> {
8384
llvm::report_fatal_error("SPIRV doesn't support fp8 type");
8485
return IntegerType::get(type.getContext(), 8);
8586
});
8687
// Internally store bfloat16 as int16
87-
addConversion([&](BFloat16Type type) -> llvm::Optional<Type> {
88+
addConversion([&](BFloat16Type type) -> std::optional<Type> {
8889
return IntegerType::get(type.getContext(), 16);
8990
});
9091
addConversion(
91-
[&](IndexType type) -> llvm::Optional<Type> { return getIndexType(); });
92+
[&](IndexType type) -> std::optional<Type> { return getIndexType(); });
9293

9394
// Add generic source and target materializations to handle cases where
9495
// non-SPIRV types persist after an SPIRV conversion.
9596
addSourceMaterialization([&](OpBuilder &builder, Type resultType,
9697
ValueRange inputs,
97-
Location loc) -> Optional<Value> {
98+
Location loc) -> std::optional<Value> {
9899
if (inputs.size() != 1)
99100
return std::nullopt;
100101

@@ -103,7 +104,7 @@ TritonGPUToSPIRVTypeConverter::TritonGPUToSPIRVTypeConverter(
103104
});
104105
addTargetMaterialization([&](OpBuilder &builder, Type resultType,
105106
ValueRange inputs,
106-
Location loc) -> Optional<Value> {
107+
Location loc) -> std::optional<Value> {
107108
if (inputs.size() != 1)
108109
return std::nullopt;
109110

@@ -115,7 +116,7 @@ TritonGPUToSPIRVTypeConverter::TritonGPUToSPIRVTypeConverter(
115116
Type TritonGPUToSPIRVTypeConverter::convertTritonPointerType(
116117
triton::PointerType type) {
117118
// Recursively translate pointee type
118-
Optional<spirv::StorageClass> storageClass =
119+
std::optional<spirv::StorageClass> storageClass =
119120
getStorageClassForMemorySpace(type.getAddressSpace());
120121
assert(storageClass && "uncompatible pointer address type in SPIRV");
121122
return spirv::PointerType::get(convertType(type.getPointeeType()),

lib/Target/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
add_subdirectory(SPIRV)
2+
3+
set_target_properties(SPIRV-Tools-shared PROPERTIES EXCLUDE_FROM_ALL ON)

0 commit comments

Comments
 (0)